示例#1
0
def run_test(name):

    logger.debug('testing %s' % name)
    pairs = load_tests()[name]

    def _dec(x):
        if is_string(x) and x.startswith(b'0x'):
            return decode_hex(x[2:])
        return x

    pairs['in'] = [(_dec(k), _dec(v)) for k, v in pairs['in']]
    deletes = [(k, v) for k, v in pairs['in'] if v is None]

    N_PERMUTATIONS = 1000
    for i, permut in enumerate(itertools.permutations(pairs['in'])):
        if i > N_PERMUTATIONS:
            break
        t = trie.Trie(db.EphemDB())
        for k, v in permut:
            #logger.debug('updating with (%s, %s)' %(k, v))
            if v is not None:
                t.update(k, v)
            else:
                t.delete(k)
        # make sure we have deletes at the end
        for k, v in deletes:
            t.delete(k)
        assert pairs['root'] == b'0x' + encode_hex(t.root_hash), (i, list(permut) + deletes)
示例#2
0
def run_test(name, pairs):

    logger.debug('testing %s' % name)

    #print('testing %s' % name)
    def _dec(x):
        if is_string(x) and x.startswith(b'0x'):
            return decode_hex(x[2:])
        return x

    pairs['in'] = [(_dec(k), _dec(v)) for k, v in pairs['in']]
    deletes = [(k, v) for k, v in pairs['in'] if v is None]

    N_PERMUTATIONS = 1000
    for i, permut in enumerate(itertools.permutations(pairs['in'])):
        print(i, permut)
        if i > N_PERMUTATIONS:
            break
        t = trie.Trie(db.EphemDB())
        for k, v in permut:
            #logger.debug('updating with (%s, %s)' %(k, v))
            #print('updating with (%s, %s)' %(k, v))
            if v is not None:
                t.update(k, v)
            else:
                t.delete(k)
        # make sure we have deletes at the end
        for k, v in deletes:
            t.delete(k)
        #print('pairs["root"] should be equal with %s' % (b'0x' + encode_hex(t.root_hash)))
        if pairs['root'] != b'0x' + encode_hex(t.root_hash):
            raise Exception(
                "Mismatch: %r %r %r %r" %
                (name, pairs['root'], b'0x' + encode_hex(t.root_hash),
                 (i, list(permut) + deletes)))
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
示例#4
0
def run_test(name):

    logger.debug('testing %s' % name)
    t = trie.Trie(new_db())
    data = load_tests()[name]

    for k in data['in']:
        logger.debug('updating with (%s, %s)' % (k, k))
        t.update(k, k)
    for point, prev, nxt in data['tests']:
        assert nxt == (t.next(point) or '')
        assert prev == (t.prev(point) or '')
def run_test(name):

    logger.debug('testing %s' % name)
    t = trie.Trie(new_db())
    data = fixture_to_bytes(load_tests()[name])

    for k in data['in']:
        logger.debug('updating with (%s, %s)' % (k, k))
        k = to_string(k)
        t.update(k, k)
    for point, prev, nxt in data['tests']:
        assert to_string(nxt) == (t.next(point) or b'')
        assert to_string(prev) == (t.prev(point) or b'')
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
示例#7
0
def benchmark(size):
    t = trie.Trie(db.EphemDB())
    for i in range(size):
        t.update(utils.sha3('k' + str(i)), utils.sha3('v' + str(i)))
    sz = sum([len(v) for k, v in t.db.db.items()])
    nsz = []
    ldb = db.ListeningDB(t.db.db)
    for i in range(min(size, 100)):
        ldb2 = db.ListeningDB(ldb)
        odb = t.db
        t.db = ldb2
        t.get(utils.sha3('k' + str(i)))
        nsz.append(sum([len(v) for k, v in ldb2.kv.items()]))
        t.db = odb
    ldbsz = sum([len(v) for k, v in ldb.kv.items()])
    print sz, sum(nsz) // len(nsz), ldbsz
示例#8
0
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)
示例#9
0
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
示例#10
0
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)
示例#11
0
def mk_receipt_sha(receipts):
    t = trie.Trie(EphemDB())
    for i, receipt in enumerate(receipts):
        t.update(rlp.encode(i), rlp.encode(receipt))
    return t.root_hash
示例#12
0
import rlp
from codecs import encode, decode
from ethereum import trie, utils, db

## EX1
print('Exercice 1 ... \n')

#initialize trie
state = trie.Trie(db.EphemDB())
key = utils.to_string('\x01\x01\x02')

state.update(key, rlp.encode(['hello']))
print('Root hash: ', encode(state.root_hash, 'hex'))

k, v = state.root_node
print('Root node: ', [k, v])
print('HP encoded key, in hex:', encode(k, 'hex'))

primaryRoot = encode(state.root_hash, 'hex')

## EX2 - A
input('\n\nExercice 2 - A... \n')

#initialize trie from previous hash; add new entry with same key.
state = trie.Trie(state.db, decode(primaryRoot, 'hex'))
print('Root hash: ', encode(state.root_hash, 'hex'))
print('Root node: ', state.root_node)

print('\n')

state.update(key, rlp.encode(['hellothere']))
示例#13
0
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()
示例#14
0
import sys
from ethereum import trie, utils, rlp

#initialize trie from previous hash; add new [key, value] where key has common prefix
state = trie.Trie(
    'triedb',
    'b5e187f15f1a250e51a78561e29ccfc0a7f48e06d19ce02f98dd61159e81f71d'.decode(
        'hex'))
print 'using root hash from ex2b'
print rlp.decode(state.get('\x01\x01\x03'))
print ''
state = trie.Trie(
    'triedb',
    'fcb2e3098029e816b04d99d7e1bba22d7b77336f9fe8604f2adfb04bcf04a727'.decode(
        'hex'))
print 'using root hash from ex3'
print rlp.decode(state.get('\x01\x01\x02'))
print rlp.decode(state.get('\x01\x01\x02\x55'))
print rlp.decode(state.get('\x01\x01\x02\x57'))
示例#15
0
import sys
sys.path.append('src')
from ethereum import trie
import rlp
from ethereum import db

#initialize trie
state = trie.Trie(db._EphemDB(), trie.BLANK_ROOT)
state.update(b'\x01\x01\x02', rlp.encode(['hello']))
print 'root hash', state.root_hash.encode('hex')
k, v = state.root_node
print 'root node:', [k, v]
print 'hp encoded key, in hex:', k.encode('hex')