def get_next_in_mainchain(self, blockhash): if not self.indexdb.contains_block(blockhash): raise BlockNotFound(str(blockhash)) blockindex = self.indexdb.get_blockindex(blockhash) if (blockindex.hash_next == Uint256.zero()): return None return blockindex.hash_next
def test_serialize_getblocks_message(self): getblocks_msg = GetblocksMessage(BlockLocator(32200, [Uint256.from_hexstr("000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f")]), Uint256.zero()) serialized_msg = GetblocksMessageSerializer().serialize(getblocks_msg) self.assertEquals(hexstr(serialized_msg), "c87d0000016fe28c0ab6f1b372c1a6a246ae63f74f931e8365e15a089c68d61900000000000000000000000000000000000000000000000000000000000000000000000000")
def _find_fork(self, altchainhash): hash = altchainhash while hash != Uint256.zero(): handle = self.get_block_handle(hash) if handle.is_mainchain(): return hash hash = handle.get_blockheader().hash_prev return handle.hash
def get_median_time_past(self, hashprev): block_times = [] i = 0 while hashprev != Uint256.zero() and i < MEDIAN_TIME_SPAN: blk = self.database.get_block_handle(hashprev) blkheader = blk.get_blockheader() block_times.append(blkheader.time) hashprev = blkheader.hash_prev i += 1 return median(block_times)
def check_proof_of_work(self, hash, block): target = block.blockheader.target() if (target <= Uint256.zero() or target > PROOF_OF_WORK_LIMIT[self.runmode]): raise Exception("proof of work: value out of range : %x" % (block.blockheader.bits)) if (hash > target): raise Exception( "proof of work: hash doesn't match target hash:%s, target:%s" % (hash, target))
def append_block(self, blockhash, block): file, blockpos = self.blockstore.saveblock(block) prevblock = self.get_block_handle(block.blockheader.hash_prev) idx = DbBlockIndex(self.version, Uint256.zero(), file, blockpos, prevblock.get_height()+1, block.blockheader) self.indexdb.set_blockindex(blockhash, idx) if prevblock.hash == self.get_mainchain(): prevblock.blockindex.hash_next = blockhash self.indexdb.set_blockindex(prevblock.hash, prevblock.blockindex) self.indexdb.set_hashbestchain(blockhash) self._index_transactions(blockhash, block) return DBBlockHandle(self.log, self.indexdb, self.blockstore, blockhash, block=block)
def send_transaction(self, planned_tx, passphrases): try: self.wallet.unlock(passphrases) privkey_list = [] for outpoint, txout in planned_tx.selected_outputs: privkey_list.append( self.wallet.get_txout_private_key_secret(txout)) finally: self.wallet.lock() sign_transaction( planned_tx.tx, [txout for outpoint, txout in planned_tx.selected_outputs], privkey_list) txhash = hash_tx(planned_tx.tx) self.log.info( "Sending %f to %s (fee:%f), change address: %s, hash:%s" % (planned_tx.amount, str(planned_tx.address), planned_tx.fee, str(planned_tx.change_address), str(txhash))) #Initially, create an empty MerkleTx (the tx is not yet in a block) merkle_tx = MerkleTx(planned_tx.tx, Uint256.zero(), [], 4294967295) self.wallet.begin_updates() self.wallet.allocate_key(planned_tx.change_public_key, ischange=True) #Set the spend flags for the input transactions for outpoint, txout in planned_tx.selected_outputs: input_wallet_tx = self.wallet.get_transaction(outpoint.hash) input_wallet_tx.set_spent(outpoint.index) self.wallet.set_transaction(outpoint.hash, input_wallet_tx) #Add the wallet_tx (contains supporting transations) txtime = int(time.time()) wallet_tx = create_wallet_tx(self.blockchain, merkle_tx, txtime) self.wallet.add_transaction(txhash, wallet_tx) self.wallet.commit_updates() self.fire(self.EVT_NEW_TRANSACTION_ITEM, item=(planned_tx.tx, txhash, txtime, planned_tx.address, "", -planned_tx.amount, False)) self.compute_balances() # we could only compute delta here self.fire(self.EVT_PUBLISH_TRANSACTION, txhash=txhash, tx=planned_tx.tx) self.last_tx_publish[txhash] = txtime #update description of change address new_description = self.wallet.get_address_description( planned_tx.change_public_key) self.fire(self.EVT_NEW_ADDRESS_DESCRIPTION, public_key=planned_tx.change_public_key, description=new_description)
def send_transaction(self, planned_tx, passphrases): try: self.wallet.unlock(passphrases) privkey_list = [] for outpoint, txout in planned_tx.selected_outputs: privkey_list.append(self.wallet.get_txout_private_key_secret(txout)) finally: self.wallet.lock() sign_transaction(planned_tx.tx, [txout for outpoint, txout in planned_tx.selected_outputs], privkey_list) txhash = hash_tx(planned_tx.tx) self.log.info( "Sending %f to %s (fee:%f), change address: %s, hash:%s" % (planned_tx.amount, str(planned_tx.address), planned_tx.fee, str(planned_tx.change_address), str(txhash)) ) # Initially, create an empty MerkleTx (the tx is not yet in a block) merkle_tx = MerkleTx(planned_tx.tx, Uint256.zero(), [], 4294967295) self.wallet.begin_updates() self.wallet.allocate_key(planned_tx.change_public_key, ischange=True) # Set the spend flags for the input transactions for outpoint, txout in planned_tx.selected_outputs: input_wallet_tx = self.wallet.get_transaction(outpoint.hash) input_wallet_tx.set_spent(outpoint.index) self.wallet.set_transaction(outpoint.hash, input_wallet_tx) # Add the wallet_tx (contains supporting transations) txtime = int(time.time()) wallet_tx = create_wallet_tx(self.blockchain, merkle_tx, txtime) self.wallet.add_transaction(txhash, wallet_tx) self.wallet.commit_updates() self.fire( self.EVT_NEW_TRANSACTION_ITEM, item=(planned_tx.tx, txhash, txtime, planned_tx.address, "", -planned_tx.amount, False), ) self.compute_balances() # we could only compute delta here self.fire(self.EVT_PUBLISH_TRANSACTION, txhash=txhash, tx=planned_tx.tx) self.last_tx_publish[txhash] = txtime # update description of change address new_description = self.wallet.get_address_description(planned_tx.change_public_key) self.fire( self.EVT_NEW_ADDRESS_DESCRIPTION, public_key=planned_tx.change_public_key, description=new_description )
def is_null(self): return (self.hash == Uint256.zero() and self.index == NULL_OUTPOINT_INDEX)
def null(): return (Outpoint(Uint256.zero(), NULL_OUTPOINT_INDEX))
def on_version_exchange(self, event): peer_heigth = event.version_message.start_height my_height = self.blockchain.get_height() if (peer_heigth > my_height and self.firstrequest): self.push_getblocks(event.handler, Uint256.zero()) self.firstrequest = False
def check_proof_of_work(self, hash, block): target = block.blockheader.target() if (target <= Uint256.zero() or target > PROOF_OF_WORK_LIMIT[self.runmode]): raise Exception("proof of work: value out of range : %x" % (block.blockheader.bits)) if (hash > target): raise Exception("proof of work: hash doesn't match target hash:%s, target:%s" % (hash, target))
def hasprev(self): return self.blockindex.blockheader.hash_prev != Uint256.zero()
def is_mainchain(self): return (self.blockindex.hash_next != Uint256.zero() or self.hash == self.indexdb.get_hashbestchain())
def hasprev(self): return self.blockindex.blockheader.hash_prev != Uint256.zero()
def is_mainchain(self): return (self.blockindex.hash_next != Uint256.zero() or self.hash == self.indexdb.get_hashbestchain())
def on_version_exchange(self, event): peer_heigth = event.version_message.start_height my_height = self.blockchain.get_height() if (peer_heigth > my_height and self.firstrequest): self.push_getblocks(event.handler, Uint256.zero()) self.firstrequest = False
def null(): return (Outpoint(Uint256.zero(), NULL_OUTPOINT_INDEX))
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)
def is_null(self): return (self.hash == Uint256.zero() and self.index == NULL_OUTPOINT_INDEX)