def new_block_callback(block_hash): total = 0 # print("New Block: {0}".format(block_hash)) block = web3.eth.getBlock(block_hash, full_transactions=True) state = trie.Trie(db.DB(), trie.BLANK_ROOT) txs_root = bytes.fromhex(block[u'transactionsRoot'][2:]) list_tx_hash = [] list_tx_raw = [] # print(block) # print("total transactions: " + str(len(block[u'transactions']))) for i, tx_rpc in enumerate(block[u'transactions']): list_tx_hash.append(tx_rpc[u'hash']) rlp_i = rlp.encode(i) try: tx_raw = tx_rpc[u'raw'][2:] # Parity RPC have raw attribute except: tx_raw = getRawTransaction()[2:] # Geth haven't raw attribute list_tx_raw.append(tx_raw) state.update(rlp_i, bytes.fromhex(tx_raw)) # print("RLP " + bytes.hex(rlp_i)) for j, (tx_hash, tx_raw) in enumerate(zip(list_tx_hash, list_tx_raw)): if len(tx_raw) <= 4096: timestamp = create_proof(block, state, tx_hash, j, tx_raw) # print(tx_hash) # print(timestamp.str_tree()) msg = last_timestamp_msg(timestamp) if msg != txs_root: print("ERROR " + tx_hash) print() total += 1 return total
def make_trie(block): state = trie.Trie(db.DB(), trie.BLANK_ROOT) txs_root = bytes.fromhex(block['transactionsRoot'][2:]) list_tx_hash = [] list_tx_raw = [] # print(block) # print("total transactions: " + str(len(block[u'transactions']))) for i, tx_rpc in enumerate(block['transactions']): list_tx_hash.append(tx_rpc['hash']) rlp_i = rlp.encode(i) tx_raw = tx_rpc['raw'][2:] # Parity RPC have raw attribute list_tx_raw.append(tx_raw) state.update(rlp_i, bytes.fromhex(tx_raw)) return state
def main(): import sys from ethereum import db _db = db.DB() t = Trie(_db) def encode_node(nd): if is_string(nd): return encode_hex(nd) else: return encode_hex(rlp_encode(nd)) t.update(to_string(1), to_string(111)) print(encode_node(t.root_hash)) t.update(to_string(2), to_string(222)) print(encode_node(t.root_hash)) t.update(to_string(3), to_string(333)) print(encode_node(t.root_hash)) print(t.get(to_string(3)))
def new_block_callback(block_hash): print("New Block: {0}".format(block_hash)) block = web3.eth.getBlock(block_hash, full_transactions=True) state = trie.Trie(db.DB(), trie.BLANK_ROOT) list_tx_hash = [] list_tx_raw = [] print block print "total transactions: " + str(len(block[u'transactions'])) for i, tx_rpc in enumerate(block[u'transactions']): list_tx_hash.append(tx_rpc[u'hash']) rlp_i = rlp.encode(i) try: tx_raw = tx_rpc[u'raw'][2:] # Parity RPC have raw attribute except: tx_raw = getRawTransaction()[2:] # Geth haven't raw attribute list_tx_raw.append(tx_raw) state.update(rlp_i, tx_raw.decode('hex')) print "RLP " + rlp_i.encode('hex') for i, (tx_hash, tx_raw) in enumerate(zip(list_tx_hash, list_tx_raw)): create_proof(block, state, tx_hash, i, tx_raw)
import sys sys.path.append('src') from ethereum import trie, db import rlp state = trie.Trie(db.DB(), trie.BLANK_ROOT) state.update(b'\x01\x01\x02', rlp.encode(['hello'])) #initialize trie from previous hash; add new entry with same key. print state.root_hash.encode('hex') print state.root_node print '' state.update('\x01\x01\x02', rlp.encode(['hellothere'])) print state.root_hash.encode('hex') print state.root_node # we now have two tries, addressed in the database by their respective hashes, though they each have the same key
from ethereum import trie, db key1 = b'\x01\x01\x02' key2 = b'\x01\x01\x03' element1 = 'ciao' element2 = 'bao' state1 = trie.Trie(db.DB(), trie.BLANK_ROOT) state1.update(key1, element1) state1.update(key1, element1) state2 = trie.Trie(db.DB(), trie.BLANK_ROOT)
def get_proof(block_number, timestamping_tx_hash, timestamping_root): block = web3.eth.getBlock(block_number) tx_root = block[u'transactionsRoot'] state = trie.Trie(db.DB(), trie.BLANK_ROOT) print tx_root for i, tx_hash in enumerate(block[u'transactions']): print print tx_hash tx_raw = getRawTransaction(tx_hash) print tx_raw rlp_i = rlp.encode(i) state.update(rlp_i, tx_raw[2:].decode('hex')) if tx_hash[2:] == timestamping_tx_hash: my_i = rlp_i assert state.root_hash.encode('hex') == tx_root[2:] val = trie.bin_to_nibbles(my_i) current_node = state.root_node ops = [] for i, el in enumerate(val): print print el print[cur_el.encode('hex') for cur_el in current_node] encoded = rlp.encode(current_node) current_node_encoded = encoded.encode('hex') print encoded.encode('hex') print utils.sha3(encoded).encode('hex') if len(current_node) == 17: current_el = current_node[el] current_el_hex = current_el.encode('hex') [prepend, append] = getAppendAndPrepend(current_el_hex, current_node_encoded) ops.append("keccak") ops.append("append " + append) ops.append("prepend " + prepend) current_node = state._decode_to_node(current_el) else: if len(current_node) == 2: if str(el) == current_node[0]: tx_raw_hex = current_node[1].encode('hex') [prepend, append] = getAppendAndPrepend(tx_raw_hex, current_node_encoded) ops.append("keccak") ops.append("append " + append) ops.append("prepend " + prepend) print print tx_raw_hex print timestamping_root [prepend, append] = getAppendAndPrepend(timestamping_root, tx_raw_hex) print print "File sha256 hash: " + timestamp_root_hex print "Timestamp:" print "prepend " + prepend print "append " + append # print "keccak" # result is tx hash 0x1328e8a3eb0db1376182d7d53fc6024be3c06553295ca36eaed0bbac395f125c while len(ops): print ops.pop()