示例#1
0
 def open_or_create(self, genesisblock):
     self.genesisblock = genesisblock
     self.genesishash = hash_block(genesisblock)
    
     if self.exists():
         self.open()
     else:
         self.create(genesisblock)
示例#2
0
 def append_block(self, block):
     """
     """
     blkhash = hash_block(block)
     self.indexed_blocks[blkhash] = block
     self.block_heights[blkhash] = len(self.blocks)
     if self.blocks:
         self.next_main_chain[hash_block(self.blocks[-1])] = blkhash
     self.next_main_chain[hash_block(block)] = None
     self.blocks.append(block)
     for tx in block.transactions:
         txhash = hash_tx(tx)
         self.indexed_tx[txhash] = tx
         self.tx_blkhashes[txhash] = blkhash
         self.spend_outputs[txhash] = [None] * len(tx.out_list)
         if not tx.iscoinbase():
             for txin in tx.in_list:
                 self.spend_outputs[txin.previous_output.hash][txin.previous_output.index] = txhash
示例#3
0
 def on_block(self, event):
     peer, message = event.handler, event.message
     hash = hash_block(message.block)
     self.log.debug("on_block : %s" % (str(hash)))
     if (hash not in self.requested_blocks):
         self.misbehaving(peer, "peer sending unrequest 'block' : %s" % hash)
         return
     del self.requested_blocks[hash]
     if not self.requested_blocks:
         self.downloading = False
     self.blocks_to_process.append( (peer, hash, message.block))
     self.start_processing()
示例#4
0
 def __init__(self, block, block_view):
     block_view.set_hash(hash_block(block).get_hexstr())
     block_view.set_previous_block(block.blockheader.hash_prev.get_hexstr())
     
     str_blocktime = time.strftime("%Y-%m-%d %H:%m:%S", time.gmtime(block.blockheader.time))
     block_view.set_time(str_blocktime)
     block_view.set_difficulty("%x" % (block.blockheader.bits))
     
     block_view.set_merkle(block.blockheader.hash_merkle.get_hexstr())
     block_view.set_nonce(str(block.blockheader.nonce))
     for tx in block.transactions:
         amount_out = sum(out.value for out in tx.out_list)
         block_view.add_transaction(hash_tx(tx).get_hexstr(), str(amount_out/ COIN) , "0", "")
示例#5
0
 def test_mine_genesis_block(self):
     """ Mine the unitnet GENESIS block """
     time_source = MockTimeSource(time=1356446436)
     miner = BitcoinMiner()
     block, template = miner.mine_block(hash_prev=Uint256.from_hexstr("0000000000000000000000000000000000000000000000000000000000000000"), 
                                 block_height=0,
                                 time_source=time_source, 
                                 difficulty_bits=524287999, 
                                 transactions=[], 
                                 coinbase_txout_list=[TxOut(5000000000, Script([push_data_instruction(decodehexstr("04678afdb0fe5548271967f1a67130b7105cd6a828e03909a67962e0ea1f61deb649f6bc3f4cef38c4f35504e51ec112de5c384df7ba0b8d578a4c702b6bf11d5f")),Instruction(OP_CHECKSIG)]))],
                                 coinbase_flags=["/P2SH/", "The Times 03/Jan/2009 Chancellor on brink of second bailout for banks"])
     self.assertEquals(block.blockheader.nonce, 1260)
     self.assertEquals(block.blockheader.hash_merkle, Uint256.from_hexstr("cf7ac9a3b387cf5e9fc14e03e2a5bbfc16d1ba00d2af6c96869ab4da949bd240"))
     self.assertEquals(hash_block(block), Uint256.from_hexstr("003ee3cf880906caa5662f10d4b4fb1c86c1853230dee8a7b8a62f434c73da5f"))
示例#6
0
 def on_getdata(self, event):
     # todo: deserializing 500 block from disk is slow, gui freezes
     for inv in event.message.invitems:
         if inv.type == INV_TX and self.txpool.contains_transaction(inv.hash):
             tx = self.txpool.get_transaction(inv.hash)
             self.node.send_message(event.handler, TxMessage(tx))
         if inv.type == INV_BLOCK and self.blockchain.contains_block(inv.hash):
             block = self.blockchain.get_block(inv.hash)
             self.node.send_message(event.handler, BlockMessage(block))
             self.log.debug("sending block: %s" % (str(hash_block(block))))
                 
             if self.hash_continue and inv.hash == self.hash_continue:
                 hash_best = self.blockchain.database.get_mainchain()
                 self.log.info("sending hashContinue: %s" % (str(hash_best)))
                 self.node.send_message(event.handler, InvMessage([Invitem(INV_BLOCK, hash_best)]))
示例#7
0
 def pop_block(self):
     """
      raises
         RemovingGenesisException: if the blockchain only contains the genesis block.
     """
     block = self.blocks.pop()
     blkhash = hash_block(block)
     #remove indexes
     del self.next_main_chain[blkhash]
     self.next_main_chain[block.blockheader.hash_prev] = None
     del self.indexed_blocks[blkhash]
     del self.block_heights[blkhash]
     for tx in block.transactions:
         txhash = hash_tx(tx)
         if not tx.iscoinbase():
             for txin in tx.in_list:
                 self.spend_outputs[txin.previous_output.hash][txin.previous_output.index] = None
         del self.indexed_tx[txhash]
         del self.tx_blkhashes[txhash]
         del self.spend_outputs[txhash]
     return block
示例#8
0
 def test_mine_block(self):
     """ Mine a block changing both nonce and extra_nonce"""
     def nonce_changer(template):
         if template.nonce >= 20:
             template.set_extra_nonce(template.extra_nonce + 1)
             template.set_nonce(0)
         else:
             template.set_nonce(template.nonce + 1)
     time_source = MockTimeSource(time=1356446436)
     miner = BitcoinMiner()
     block, template = miner.mine_block(hash_prev=Uint256.from_hexstr("0000000000000000000000000000000000000000000000000000000000000000"), 
                                        block_height=0,
                                        time_source=time_source, 
                                        difficulty_bits=524287999, 
                                        transactions=[], 
                                        coinbase_txout_list=[TxOut(5000000000, Script([Instruction(OP_PUSHDATA, decodehexstr("04678afdb0fe5548271967f1a67130b7105cd6a828e03909a67962e0ea1f61deb649f6bc3f4cef38c4f35504e51ec112de5c384df7ba0b8d578a4c702b6bf11d5f")),Instruction(OP_CHECKSIG)]))],
                                        nonce_changer=nonce_changer)
     self.assertEquals(block.blockheader.nonce, 8)
     self.assertEquals(template.nonce, 8)
     self.assertEquals(template.extra_nonce, 14)
     self.assertEquals(block.blockheader.hash_merkle, Uint256.from_hexstr("ca839450c8702d6768d1803bb6d99c6d059a56240933e5bf72cb2936f6c9e211"))
     self.assertEquals(hash_block(block), Uint256.from_hexstr("003c7a06c7efe128cb3bea692e1a485f7400f3670df7986c020083e9b10e295d"))
示例#9
0
 def pop_block(self):
     """
      raises
         RemovingGenesisException: if the blockchain only contains the genesis block.
     """
     block = self.blocks.pop()
     blkhash = hash_block(block)
     #remove indexes
     del self.next_main_chain[blkhash]
     self.next_main_chain[block.blockheader.hash_prev] = None
     del self.indexed_blocks[blkhash]
     del self.block_heights[blkhash]
     for tx in block.transactions:
         txhash = hash_tx(tx)
         if not tx.iscoinbase():
             for txin in tx.in_list:
                 self.spend_outputs[txin.previous_output.hash][
                     txin.previous_output.index] = None
         del self.indexed_tx[txhash]
         del self.tx_blkhashes[txhash]
         del self.spend_outputs[txhash]
     return block
示例#10
0
 def test_mine_genesis_block(self):
     """ Mine the unitnet GENESIS block """
     time_source = MockTimeSource(time=1356446436)
     miner = BitcoinMiner()
     block, template = miner.mine_block(
         hash_prev=Uint256.from_hexstr(
             "0000000000000000000000000000000000000000000000000000000000000000"
         ),
         block_height=0,
         time_source=time_source,
         difficulty_bits=524287999,
         transactions=[],
         coinbase_txout_list=[
             TxOut(
                 5000000000,
                 Script([
                     push_data_instruction(
                         decodehexstr(
                             "04678afdb0fe5548271967f1a67130b7105cd6a828e03909a67962e0ea1f61deb649f6bc3f4cef38c4f35504e51ec112de5c384df7ba0b8d578a4c702b6bf11d5f"
                         )),
                     Instruction(OP_CHECKSIG)
                 ]))
         ],
         coinbase_flags=[
             "/P2SH/",
             "The Times 03/Jan/2009 Chancellor on brink of second bailout for banks"
         ])
     self.assertEquals(block.blockheader.nonce, 1260)
     self.assertEquals(
         block.blockheader.hash_merkle,
         Uint256.from_hexstr(
             "cf7ac9a3b387cf5e9fc14e03e2a5bbfc16d1ba00d2af6c96869ab4da949bd240"
         ))
     self.assertEquals(
         hash_block(block),
         Uint256.from_hexstr(
             "003ee3cf880906caa5662f10d4b4fb1c86c1853230dee8a7b8a62f434c73da5f"
         ))
示例#11
0
 def get_genesis_hash(self):
     return hash_block(self.blocks[0])
示例#12
0
 def get_best_hash(self):
     return hash_block(self.blocks[-1])
示例#13
0
 def get_genesis_hash(self):
     return hash_block(self.blocks[0])
示例#14
0
 def get_best_hash(self):
     return hash_block(self.blocks[-1])
示例#15
0
 def create(self, genesis_block):
     file, blockpos = self.blockstore.saveblock(genesis_block)
     self.blockstore.commit()
     genesis_index = DbBlockIndex(self.version, Uint256.zero(), file, blockpos, 0, genesis_block.blockheader)
     self.indexdb.create(hash_block(genesis_block), genesis_index)
示例#16
0
        [TxOut(5000000000, #value
                Script([push_data_instruction(decodehexstr("04678afdb0fe5548271967f1a67130b7105cd6a828e03909a67962e0ea1f61deb649f6bc3f4cef38c4f35504e51ec112de5c384df7ba0b8d578a4c702b6bf11d5f")),Instruction(OP_CHECKSIG)]))],
                0) #locktime         
     ])
GENESIS = {MAIN : GENESIS_MAIN, TESTNET : GENESIS_TESTNET, TESTNET3: GENESIS_TESTNET3, UNITNET : GENESIS_UNITNET }

if __name__ == '__main__':
    from coinpy.lib.blocks.hash_block import hash_block
    from coinpy.tools.hex import hexstr
    from coinpy.lib.serialization.structures.s11n_blockheader import BlockheaderSerializer
    from coinpy.tools.bitcoin.sha256 import doublesha256
    from coinpy.lib.transactions.hash_tx import hash_tx
    from coinpy.lib.serialization.structures.s11n_tx import TxSerializer

    print GENESIS_MAIN
    print hash_block(GENESIS_MAIN)
    assert GENESIS_MAIN.blockheader.hash_merkle == compute_merkle_root(GENESIS_MAIN.transactions)

    print GENESIS_TESTNET
    print hash_block(GENESIS_TESTNET)
    assert GENESIS_TESTNET.blockheader.hash_merkle == compute_merkle_root(GENESIS_TESTNET.transactions)

    print GENESIS_TESTNET3
    print hash_block(GENESIS_TESTNET3)
    assert GENESIS_TESTNET3.blockheader.hash_merkle == compute_merkle_root(GENESIS_TESTNET3.transactions)

    print GENESIS_UNITNET
    print hash_block(GENESIS_UNITNET)
    print GENESIS_UNITNET.blockheader.hash_merkle
    assert GENESIS_UNITNET.blockheader.hash_merkle == compute_merkle_root(GENESIS_UNITNET.transactions)