当前位置: 首页>>代码示例>>Python>>正文


Python Block.getHash方法代码示例

本文整理汇总了Python中Block.Block.getHash方法的典型用法代码示例。如果您正苦于以下问题:Python Block.getHash方法的具体用法?Python Block.getHash怎么用?Python Block.getHash使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在Block.Block的用法示例。


在下文中一共展示了Block.getHash方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: testMethods

# 需要导入模块: from Block import Block [as 别名]
# 或者: from Block.Block import getHash [as 别名]
    def testMethods(self):
        genesis = Block(b'', scroogePubKey)
        genesis.finalize()
        blockChain = BlockChain(genesis)
        blockHandler = BlockHandler(blockChain)
        
        # Genesis block test
        self.assertEqual(genesis.getHash(), blockChain.getMaxHeightBlock().getHash(), \
                         'genesis should be max height block')
        self.assertEqual(blockChain.getMaxHeightBlock(), genesis, \
                         'genesis should be max height block')
        self.assertEqual(len(blockChain.getMaxHeightUTXOPool().getAllUTXO()), 1, \
                         'UTXOPool should have one output')
        self.assertEqual(len(blockChain.getTransactionPool().getTransactions()), 0, \
                         'transaction pool should be empty')

        # Spend the genesis coinbase transaction in many outputs
        tx = Transaction()
        tx.addInput(genesis.getCoinbase().getHash(), 0)
        numGenOuts = int(COINBASE)
        for i in range(numGenOuts):
            tx.addOutput(1.0, scroogePubKey)
        signInput(tx, scroogePriKey, 0)
        tx.finalize()
        
        # Add one transaction test. No block has been added.
        blockHandler.processTx(tx)       
        self.assertEqual(blockChain.getMaxHeightBlock(), genesis, \
                         'genesis should be max height block')
        self.assertEqual(len(blockChain.getMaxHeightUTXOPool().getAllUTXO()), 1, \
                         'UTXOPool should have one output')
        self.assertEqual(len(blockChain.getTransactionPool().getTransactions()), 1, \
                         'transaction pool should have one entry')
        self.assertIsNotNone(blockChain.getTransactionPool().getTransaction(tx.getHash()), \
                            'tx should be in txPool')
        
        # Build out the chain
        depth = 15;
        chainedBlocks = []
        for i in range(depth):
            # Spend the new outputs, one tx per block
            tx2 = Transaction()
            tx2.addInput(tx.getHash(), i)
            tx2.addOutput(1.0, scroogePubKey)
            signInput(tx2, scroogePriKey, 0)
            tx2.finalize()
            blockHandler.processTx(tx2);
            chainedBlocks.append(blockHandler.createBlock(scroogePubKey))
            
            # Deep chain test
            self.assertIsNotNone(chainedBlocks[i], 'ith block should exist')
            self.assertEqual(blockChain.getMaxHeightBlock(), chainedBlocks[i], \
                             'ith block should be max height')
            self.assertEqual(len(blockChain.getMaxHeightUTXOPool().getAllUTXO()), numGenOuts + i + 1, \
                             'wrong number UTXOs when i = ' + str(i))
            self.assertEqual(len(blockChain.getTransactionPool().getTransactions()), 0, \
                             'txPool should be empty')
        
        # Remember the current max height block
        maxBlockBefore = blockChain.getMaxHeightBlock()
        
        # Make another block on the deepest block that should still work
        sideBlock = Block(chainedBlocks[depth - CUT_OFF_AGE - 1].getHash(), scroogePubKey)
        sideBlock.finalize()
        retVal = blockChain.addBlock(sideBlock)
        
        # Add valid side chain block test
        self.assertTrue(retVal, 'side block should have been added')
        self.assertEqual(blockChain.getMaxHeightBlock(), maxBlockBefore, \
                         'max height block should not have changed')
        
        # Make another block that is too deep
        tooDeep = Block(chainedBlocks[depth - CUT_OFF_AGE - 2].getHash(), scroogePubKey)
        tooDeep.finalize()
        retVal2 = blockChain.addBlock(tooDeep)
        
        # Too deep test
        self.assertFalse(retVal2, 'too deep block should not be added')
 
        # Build on the side chain
        prevBlock = sideBlock
        for i in range(CUT_OFF_AGE - 1):
            # Spend the previous coinbase transaction
            tx2 = Transaction();
            tx2.addInput(prevBlock.getCoinbase().getHash(), 0)
            tx2.addOutput(10.0, scroogePubKey)
            signInput(tx2, scroogePriKey, 0)
            tx2.finalize();
            
            newBlock = Block(prevBlock.getHash(), scroogePubKey)
            newBlock.addTransaction(tx2)
            newBlock.finalize()
            retVal3 = blockChain.addBlock(newBlock)
            
            self.assertTrue(retVal3, 'side blocks should be added')
            
            prevBlock = newBlock
            
        # The side chain should be the same length as the previous chain
        # and so the max height block should not have changed.
#.........这里部分代码省略.........
开发者ID:shsentoff,项目名称:moocs,代码行数:103,代码来源:TestSuite.py


注:本文中的Block.Block.getHash方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。