Exemple #1
0
def run_test(name):

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

    def _dec(x):
        if isinstance(x, (str, unicode)) and x.startswith('0x'):
            return x[2:].decode('hex')
        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'] == '0x' + t.root_hash.encode('hex'), (
            i, list(permut) + deletes)
Exemple #2
0
    def before_feature(self, context, feature):
        from pyethereum import trie
        self._create_dir()
        self._delete_db()

        context.trie = trie.Trie(self.db_path)
        self._load_fixture()
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 '')
Exemple #4
0
def run_test(name):

    logger.debug('testing %s', name)
    t = trie.Trie(tempfile.mktemp())
    pairs = load_tests()[name]

    for k, v in pairs['in'].items():
        if k[:2] == '0x':
            k = k[2:].decode('hex')
        if v[:2] == '0x':
            v = v[2:].decode('hex')
        logger.debug('updating with (%s, %s)', k, v)
        t.update(k, v)
    assert pairs['root'] == t.root_hash.encode('hex')
Exemple #5
0
def bad(n=21):
    # Build n levels
    t = trie.Trie(s.db)
    accum = ''
    for i in range(n):
        for j in range(1, 16):
            k = accum + '%X' % j
            if len(k) % 2 != 0: k += '0'
            k = zpadr(k.decode('hex'))
            print k.encode('hex')
            t.update(k, utils.sha3('cow'))
        accum += '%X' % 0
    k = zpadr(accum.decode('hex'))
    t.update(k, utils.sha3('cow'))
    return t
Exemple #6
0
def test_trie_state_root():
    """
    test to track down the difference of the genesis state root 
    between the py and cpp versions

    in cpp:
    https://github.com/ethereum/cpp-ethereum/blob/8503a71f4418ae3d4260270a927348a985e29d44/libethereum/State.cpp#L256
    s = AddressState(0, 0, h256(), EmptySHA3);
    """
    def _set_acct_item(state, address, param, value):
        if len(address) == 40:
            address = address.decode('hex')
        acct = state.get(address) or ['', '', '', '']
        encoder = utils.encoders[blocks.acct_structure_rev[param][1]]
        acct[blocks.acct_structure_rev[param][0]] = encoder(value)
        state.update(address, acct)

    state = trie.Trie(tempfile.mktemp(), '')
    for k, v in blocks.GENESIS_INITIAL_ALLOC.items():
        _set_acct_item(state, k, 'balance', v)
    assert state.root.encode('hex') == CPP_PoC5_GENESIS_STATE_ROOT_HEX_HASH
Exemple #7
0
import graphviz
from pyethereum import trie, tester, utils

s = tester.state()
t = trie.Trie(s.db)


def zpad(s):
    return '\x00' * (32 - len(s)) + s


def zpadr(s):
    return s + '\x00' * (32 - len(s))


def bad(n=21):
    # Build n levels
    t = trie.Trie(s.db)
    accum = ''
    for i in range(n):
        for j in range(1, 16):
            k = accum + '%X' % j
            if len(k) % 2 != 0: k += '0'
            k = zpadr(k.decode('hex'))
            print k.encode('hex')
            t.update(k, utils.sha3('cow'))
        accum += '%X' % 0
    k = zpadr(accum.decode('hex'))
    t.update(k, utils.sha3('cow'))
    return t