def mk_transaction_spv_proof(block, tx): trie.proof.push(trie.RECORDING) processblock.apply_transaction(block, tx) o = trie.proof.get_nodelist() trie.proof.pop() o2 = map(rlp.decode, list(set(map(rlp.encode, o)))) return o2
def verify_transaction_spv_proof(block, tx, proof): trie.proof.push(trie.VERIFYING, proof) try: processblock.apply_transaction(block, tx) trie.proof.pop() return True except Exception, e: print e trie.proof.pop() return False
def add_transaction(self, transaction): old_state_root = self.block.state_root # revert finalization self.block.state_root = self.pre_finalize_state_root try: success, output = processblock.apply_transaction( self.block, transaction) except processblock.InvalidTransaction as e: # if unsuccessfull the prerequistes were not fullfilled # and the tx isinvalid, state must not have changed logger.debug('Invalid Transaction %r: %r', transaction, e) success = False # finalize self.pre_finalize_state_root = self.block.state_root self.block.finalize() if not success: logger.debug('transaction %r not applied', transaction) assert old_state_root == self.block.state_root return False else: assert transaction in self.block.get_transactions() logger.debug( 'transaction %r applied to %r res: %r', transaction, self.block, output) assert old_state_root != self.block.state_root return True
def add_transaction(self, transaction): old_state_root = self.block.state_root # revert finalization self.block.state_root = self.pre_finalize_state_root try: success, output = processblock.apply_transaction( self.block, transaction) except processblock.InvalidTransaction as e: # if unsuccessfull the prerequistes were not fullfilled # and the tx isinvalid, state must not have changed logger.debug('Invalid Transaction %r: %r', transaction, e) success = False # finalize self.pre_finalize_state_root = self.block.state_root self.block.finalize() if not success: logger.debug('transaction %r not applied', transaction) assert old_state_root == self.block.state_root return False else: assert transaction in self.block.get_transactions() logger.debug('transaction %r applied to %r res: %r', transaction, self.block, output) assert old_state_root != self.block.state_root return True
def deserialize_child(self, rlpdata): """ deserialization w/ replaying transactions """ header_args, transaction_list, uncles = rlp.decode(rlpdata) assert len(header_args) == len(block_structure) kargs = dict(transaction_list=transaction_list, uncles=uncles) # Deserialize all properties for i, (name, typ, default) in enumerate(block_structure): kargs[name] = utils.decoders[typ](header_args[i]) block = Block.init_from_parent(self, kargs['coinbase'], extra_data=kargs['extra_data'], timestamp=kargs['timestamp'], uncles=uncles) # replay transactions for tx_lst_serialized, _state_root, _gas_used_encoded in \ transaction_list: tx = transactions.Transaction.create(tx_lst_serialized) # logger.debug('state:\n%s', utils.dump_state(block.state)) # logger.debug('applying %r', tx) success, output = processblock.apply_transaction(block, tx) #block.add_transaction_to_list(tx) # < this is done by processblock # logger.debug('state:\n%s', utils.dump_state(block.state)) logger.debug('d %s %s', _gas_used_encoded, block.gas_used) assert utils.decode_int(_gas_used_encoded) == block.gas_used, \ "Gas mismatch (ours %d, theirs %d) on block: %r" % \ (block.gas_used, _gas_used_encoded, block.to_dict(False, True, True)) assert _state_root == block.state.root_hash, \ "State root mismatch (ours %r theirs %r) on block: %r" % \ (block.state.root_hash.encode('hex'), _state_root.encode('hex'), block.to_dict(False, True, True)) block.finalize() block.uncles_hash = kargs['uncles_hash'] block.nonce = kargs['nonce'] block.min_gas_price = kargs['min_gas_price'] # checks assert block.prevhash == self.hash assert block.gas_used == kargs['gas_used'] assert block.gas_limit == kargs['gas_limit'] assert block.timestamp == kargs['timestamp'] assert block.difficulty == kargs['difficulty'] assert block.number == kargs['number'] assert block.extra_data == kargs['extra_data'] assert utils.sha3(rlp.encode(block.uncles)) == kargs['uncles_hash'] assert block.tx_list_root == kargs['tx_list_root'] assert block.state.root_hash == kargs['state_root'], ( block.state.root_hash, kargs['state_root']) return block
def deserialize_child(self, rlpdata): """ deserialization w/ replaying transactions """ header_args, transaction_list, uncles = rlp.decode(rlpdata) assert len(header_args) == len(block_structure) kargs = dict(transaction_list=transaction_list, uncles=uncles) # Deserialize all properties for i, (name, typ, default) in enumerate(block_structure): kargs[name] = utils.decoders[typ](header_args[i]) block = Block.init_from_parent(self, kargs['coinbase'], extra_data=kargs['extra_data'], timestamp=kargs['timestamp'], uncles=uncles) # replay transactions for tx_lst_serialized, _state_root, _gas_used_encoded in \ transaction_list: tx = transactions.Transaction.create(tx_lst_serialized) # logger.debug('state:\n%s', utils.dump_state(block.state)) # logger.debug('applying %r', tx) success, output = processblock.apply_transaction(block, tx) #block.add_transaction_to_list(tx) # < this is done by processblock # logger.debug('state:\n%s', utils.dump_state(block.state)) logger.debug('d %s %s', _gas_used_encoded, block.gas_used) assert utils.decode_int(_gas_used_encoded) == block.gas_used, \ "Gas mismatch (ours %d, theirs %d) on block: %r" % \ (block.gas_used, _gas_used_encoded, block.to_dict(False, True, True)) assert _state_root == block.state.root_hash, \ "State root mismatch (ours %r theirs %r) on block: %r" % \ (block.state.root_hash.encode('hex'), _state_root.encode('hex'), block.to_dict(False, True, True)) block.finalize() block.uncles_hash = kargs['uncles_hash'] block.nonce = kargs['nonce'] block.min_gas_price = kargs['min_gas_price'] # checks assert block.prevhash == self.hash assert block.gas_used == kargs['gas_used'] assert block.gas_limit == kargs['gas_limit'] assert block.timestamp == kargs['timestamp'] assert block.difficulty == kargs['difficulty'] assert block.number == kargs['number'] assert block.extra_data == kargs['extra_data'] assert utils.sha3(rlp.encode(block.uncles)) == kargs['uncles_hash'] assert block.tx_list_root == kargs['tx_list_root'] assert block.state.root_hash == kargs['state_root'], (block.state.root_hash, kargs['state_root']) return block
def deserialize_child(self, rlpdata): """ deserialization w/ replaying transactions """ header_args, transaction_list, uncles = rlp.decode(rlpdata) assert len(header_args) == len(block_structure) kargs = dict(transaction_list=transaction_list, uncles=uncles) # Deserialize all properties for i, (name, typ, default) in enumerate(block_structure): kargs[name] = self.decoders[typ](header_args[i]) block = Block.init_from_parent(self, kargs['coinbase'], extra_data=kargs['extra_data'], timestamp=kargs['timestamp'], uncles=uncles) # bloom_bits_expected = bloom.bits_in_number(kargs['bloom']) # replay transactions for tx_lst_serialized in transaction_list: tx = transactions.Transaction.create(tx_lst_serialized) success, output = processblock.apply_transaction(block, tx) block.finalize() block.uncles_hash = kargs['uncles_hash'] block.nonce = kargs['nonce'] # checks set_aux(block.to_dict()) must_equal('prev_hash', block.prevhash, self.hash) must_equal('gas_used', block.gas_used, kargs['gas_used']) must_equal('gas_limit', block.gas_limit, kargs['gas_limit']) must_equal('timestamp', block.timestamp, kargs['timestamp']) must_equal('difficulty', block.difficulty, kargs['difficulty']) must_equal('number', block.number, kargs['number']) must_equal('extra_data', block.extra_data, kargs['extra_data']) must_equal('uncles', utils.sha3rlp(block.uncles), kargs['uncles_hash']) must_equal('state_root', block.state.root_hash, kargs['state_root']) must_equal('tx_list_root', block.tx_list_root, kargs['tx_list_root']) # bloom_bits = bloom.bits_in_number(block.bloom) # bloom_bits_expected = bloom.bits_in_number(kargs['bloom']) # print 'computed', bloom_bits # print 'expected', bloom_bits_expected # print 'missing', sorted(set(bloom_bits_expected) - set(bloom_bits)) # print 'wrong', sorted(set(bloom_bits) - set(bloom_bits_expected)) must_equal('bloom', block.bloom, kargs['bloom']) must_equal('receipts_root', block.receipts.root_hash, kargs['receipts_root']) set_aux(None) if not check_header_pow(block.list_header()): raise VerificationFailed('invalid nonce') return block
def applytx(blockdata, txdata, debug=0, limit=2**100): block = blocks.Block.hex_deserialize(blockdata) tx = transactions.Transaction.hex_deserialize(txdata) if tx.startgas > limit: raise Exception("Transaction is asking for too much gas!") if debug: processblock.debug = 1 success, o = processblock.apply_transaction(block, tx) return { "block": block.hex_serialize(), "result": ''.join(o).encode('hex') if tx.to else ''.join(o) }
def deserialize_child(self, rlpdata): """ deserialization w/ replaying transactions """ header_args, transaction_list, uncles = rlp.decode(rlpdata) assert len(header_args) == len(block_structure) kargs = dict(transaction_list=transaction_list, uncles=uncles) # Deserialize all properties for i, (name, typ, default) in enumerate(block_structure): kargs[name] = utils.decoders[typ](header_args[i]) block = Block.init_from_parent( self, kargs["coinbase"], extra_data=kargs["extra_data"], timestamp=kargs["timestamp"], uncles=uncles ) # replay transactions for tx_lst_serialized, _state_root, _gas_used_encoded in transaction_list: tx = transactions.Transaction.create(tx_lst_serialized) # logger.debug('state:\n%s', utils.dump_state(block.state)) # logger.debug('applying %r', tx) success, output = processblock.apply_transaction(block, tx) # block.add_transaction_to_list(tx) # < this is done by processblock # logger.debug('state:\n%s', utils.dump_state(block.state)) logger.debug("d %s %s", _gas_used_encoded, block.gas_used) assert utils.decode_int(_gas_used_encoded) == block.gas_used assert _state_root == block.state.root_hash block.finalize() block.uncles_hash = kargs["uncles_hash"] block.nonce = kargs["nonce"] block.min_gas_price = kargs["min_gas_price"] # checks assert block.prevhash == self.hash assert block.gas_used == kargs["gas_used"] assert block.gas_limit == kargs["gas_limit"] assert block.timestamp == kargs["timestamp"] assert block.difficulty == kargs["difficulty"] assert block.number == kargs["number"] assert block.extra_data == kargs["extra_data"] assert utils.sha3(rlp.encode(block.uncles)) == kargs["uncles_hash"] assert block.tx_list_root == kargs["tx_list_root"] assert block.state.root_hash == kargs["state_root"], (block.state.root_hash, kargs["state_root"]) return block
def deserialize_child(self, rlpdata): """ deserialization w/ replaying transactions """ header_args, transaction_list, uncles = rlp.decode(rlpdata) assert len(header_args) == len(block_structure) kargs = dict(transaction_list=transaction_list, uncles=uncles) # Deserialize all properties for i, (name, typ, default) in enumerate(block_structure): kargs[name] = utils.decoders[typ](header_args[i]) block = Block.init_from_parent(self, kargs['coinbase'], extra_data=kargs['extra_data'], timestamp=kargs['timestamp']) # replay transactions for tx_lst_serialized, _state_root, _gas_used_encoded in transaction_list: tx = transactions.Transaction.create(tx_lst_serialized) success, output = processblock.apply_transaction(block, tx) block.add_transaction_to_list(tx) assert utils.decode_int(_gas_used_encoded) == block.gas_used assert _state_root == block.state.root_hash block.finalize() block.uncles_hash = kargs['uncles_hash'] block.nonce = kargs['nonce'] block.min_gas_price = kargs['min_gas_price'] # checks assert block.prevhash == self.hash assert block.gas_used == kargs['gas_used'] assert block.gas_limit == kargs['gas_limit'] assert block.timestamp == kargs['timestamp'] assert block.difficulty == kargs['difficulty'] assert block.number == kargs['number'] assert block.extra_data == kargs['extra_data'] assert utils.sha3(rlp.encode(block.uncles)) == kargs['uncles_hash'] assert block.tx_list_root == kargs['tx_list_root'] assert block.state.root_hash == kargs['state_root'] return block
def add_transaction(self, transaction): block_state = self.block.state_root try: success, output = processblock.apply_transaction( self.block, transaction) except processblock.InvalidTransaction as e: # if unsuccessfull the prerequistes were not fullfilled # and the tx isinvalid, state must not have changed logger.debug('Invalid Transaction %r: %r', transaction, e) assert block_state == self.block.state_root return False if not success: logger.debug('transaction %r not applied', transaction) assert block_state == self.block.state_root else: assert transaction in self.block.get_transactions() logger.debug('transaction %r applied to %r res: %r', transaction, self.block, output) assert block_state != self.block.state_root return True
def add_transaction(self, transaction): block_state = self.block.state_root try: success, output = processblock.apply_transaction( self.block, transaction) except processblock.InvalidTransaction as e: # if unsuccessfull the prerequistes were not fullfilled # and the tx isinvalid, state must not have changed logger.debug('Invalid Transaction %r: %r', transaction, e) assert block_state == self.block.state_root return False if not success: logger.debug('transaction %r not applied', transaction) assert block_state == self.block.state_root else: assert transaction in self.block.get_transactions() logger.debug( 'transaction %r applied to %r res: %r', transaction, self.block, output) assert block_state != self.block.state_root return True