def test_nested_typed_storage_dict():

    td = dict()

    def _get(k):
        if k not in td:
            td[k] = 0
        return td[k]

    def _set(k, v):
        td[k] = v

    b = nc.Dict(nc.Dict('uint16'))
    d = nc.List('uint16')

    b.setup(b'b', _get, _set)
    d.setup(b'd', _get, _set)

    # nested dicts

    da = b['A']
    assert isinstance(da, nc.Dict)
    assert da['B'] == 0
    assert b['C']['D'] == 0

    b['A']['B'] = 12
    assert b['A']['B'] == 12

    assert da['B'] == 12

    # test list
    assert d[2] == 0
    d[2] = 1
    class TestTSC(nc.NativeContract):

        address = utils.int_to_addr(2051)
        size = nc.Scalar('uint32')
        numbers = nc.List('uint32')
        words = nc.Dict('bytes')

        def setup_numbers(ctx, size='uint32', returns=None):
            ctx.size = size
            assert isinstance(ctx.numbers, nc.List)
            for i in range(size):
                ctx.numbers.append(i)

        def sum_numbers(ctx, returns='uint32'):
            assert ctx.size == len(ctx.numbers)
            return sum(ctx.numbers[i] for i in range(len(ctx.numbers)))

        def setup_words(ctx, num='uint32', returns=None):
            for i in range(num):
                key = 'key%d' % i
                word = 'word%d' % i
                ctx.words[key] = word
                assert ctx.words[key] == word

        def get_word(ctx, key='bytes', returns='bytes'):
            r = ctx.words[key]
            return r

        def muladdsize(ctx, val='uint32', returns='uint32'):
            ctx.size += val
            ctx.size *= val
            return ctx.size
def test_nested_typed_storage_list_in_dict():

    # the storage cannot be defined globally as the calls would interfere
    td = dict()

    def _get(k):
        if k not in td:
            td[k] = 0
        return td[k]

    def _set(k, v):
        td[k] = v

    a = nc.Dict(nc.List('uint16'))
    b = nc.List('uint32')

    a.setup(b'a', _get, _set)
    b.setup(b'b', _get, _set)

    b.append(10)

    # list nested in dict
    assert isinstance(a, nc.Dict)
    key = b'test'
    idx = 0
    l = a[key]
    assert isinstance(l, nc.List)  # initialized with list
    with pytest.raises(NotImplementedError):
        assert len(a) == 0
    assert len(l) == 0

    a[key][idx] = 33
    assert l[idx] == 33
    assert len(a[key]) == 1
    assert len(l) == 1
    assert a[key][idx] == 33
    a[key][idx] = 66
    assert len(l) == 1
    assert a[key][idx] == 66
    a[key][idx + 1] = 67
    assert len(l) == 2

    for i in range(65500, 66000):
        l[i] = 1
        l[i % 100] = 2
        l[i % 300] = 3
        assert len(l) == i + 1

    # second key

    key = b'test2'
    l = a[key]
    assert isinstance(l, nc.List)  # initialized with list
    assert len(l) == 0
    a[key][idx] = 0
def test_nested_typed_storage_invalid_types():

    td = dict()

    def _get(k):
        if k not in td:
            td[k] = 0
        return td[k]

    def _set(k, v):
        td[k] = v

    a = nc.Dict(nc.List('uint16'))
    c = nc.List(nc.Dict('uint16'))
    k = nc.List(nc.List('address'))

    a.setup(b'a', _get, _set)
    c.setup(b'c', _get, _set)
    k.setup(b'k', _get, _set)

    # test invalid types

    with pytest.raises(AttributeError):
        a.b == 81

    with pytest.raises(ValueError):
        a['one']['two'] = 63432

    with pytest.raises(TypeError):
        a['one'][2] = 'somestr'

    with pytest.raises(NotImplementedError):
        k[1] = 2

    with pytest.raises(NotImplementedError):
        c[1] = 2
def test_nested_typed_storage_list():

    td = dict()

    def _get(k):
        if k not in td:
            td[k] = 0
        return td[k]

    def _set(k, v):
        td[k] = v

    c = nc.List(nc.Dict('uint16'))
    d = nc.List('uint16')
    l = nc.List(nc.List('uint16'))
    m = nc.List(nc.List('uint16'))
    n = nc.List(nc.Scalar('string'))

    c.setup(b'c', _get, _set)
    d.setup(b'd', _get, _set)
    l.setup(b'l', _get, _set)
    m.setup(b'm', _get, _set)
    n.setup(b'n', _get, _set)

    # test list
    assert d[2] == 0
    d[2] = 1

    # nested in lists

    la = c[3]
    assert isinstance(la, nc.Dict)
    assert len(c) == 0
    la['test'] = 1
    assert len(c) == 4
    assert c[3]['test'] == 1
    c[2]['test2'] = 9
    assert len(c) == 4

    l[5][6] = 8
    m[5][6] = 9
    assert l[5][6] != m[5][6]

    n[4] = 'someaddress'
    assert n[4] == 'someaddress'
Beispiel #6
0
    class TestTSC(nc.TypedStorageContract):

        address = utils.int_to_addr(2050)
        a = nc.Scalar('uint32')
        b = nc.List('uint16')
        c = nc.Dict('uint32')

        def _safe_call(ctx):
            # skalar
            assert ctx.a == 0
            ctx.a = 1
            assert ctx.a == 1

            ctx.a = 2
            assert ctx.a == 2

            # list
            assert isinstance(ctx.b, nc.List)
            ctx.b[0] = 10
            assert ctx.b[0] == 10

            ctx.b[1000] = 12
            assert ctx.b[1000] == 12

            assert len(ctx.b) == 1001
            ctx.b[1000] = 66
            assert ctx.b[1000] == 66
            assert len(ctx.b) == 1001

            ctx.b.append(99)
            assert len(ctx.b) == 1002
            ctx.b.append(99)
            assert len(ctx.b) == 1003

            # mapping
            assert isinstance(ctx.c, nc.Dict)
            key = b'test'
            assert ctx.c[key] == 0
            ctx.c[key] = 33
            assert ctx.c[key] == 33
            ctx.c[key] = 66
            assert ctx.c[key] == 66

            return 1, 1, []
def test_nested_typed_storage_struct():
    def _key(self, k):
        k = zpad(k, 32)
        return b'%s:%s' % (self._prefix, k)

    original_key = nc.TypedStorage._key
    nc.TypedStorage._key = _key

    # the storage cannot be defined globally as the calls would interfere
    td = dict()

    def _get(k):
        if k not in td:
            td[k] = 0
        return td[k]

    def _set(k, v):
        td[k] = v

    g = nc.Struct(x=nc.List('uint32'), y=nc.Scalar('address'))
    h = nc.IterableDict(nc.Struct(x=nc.List('uint32'), y=nc.Scalar('address')))
    i = nc.List(
        nc.Struct(x=nc.Scalar('uint16'),
                  y=nc.Dict('uint32'),
                  z=nc.List('uint16')))
    j = nc.Struct(v=nc.Struct(
        x=nc.List('uint32'), y=nc.Scalar('address'), w=nc.Dict('uint16')))

    g.setup(b'g', _get, _set)
    h.setup(b'h', _get, _set)
    i.setup(b'i', _get, _set)
    j.setup(b'j', _get, _set)

    # test Struct

    g.x[538] = 78
    assert g.x[538] == 78
    assert (
        'g'
        ':\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
        '\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00x'
        ':\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
        '\x00\x00\x00\x00\x00\x00\x00\x00538') in td

    with pytest.raises(AttributeError):
        assert g.idontexist == 0

    with pytest.raises(TypeError):
        g[2354645] = 2540

    with pytest.raises(TypeError):
        assert g['imnotadict'] == 0

    h['abcde'].x[4891] = 875
    assert h['abcde'].x[4891] == 875
    assert (
        'h'
        ':\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
        '\x00\x00\x00\x00\x00\x00abcde'
        ':\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
        '\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00x'
        ':\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
        '\x00\x00\x00\x00\x00\x00\x004891') in td

    i[3].y['here'] = 634
    assert i[3].y['here'] == 634
    assert (
        'i'
        ':\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
        '\x00\x00\x00\x00\x00\x00\x00\x00\x00\x003'
        ':\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
        '\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00y'
        ':\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
        '\x00\x00\x00\x00\x00\x00\x00here') in td

    i[4].z[41] = 88
    # assert i[4].z[41] == 88
    assert (
        'i'
        ':\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
        '\x00\x00\x00\x00\x00\x00\x00\x00\x00\x004'
        ':\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
        '\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00z'
        ':\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
        '\x00\x00\x00\x00\x00\x00\x00\x00\x0041') in td

    assert len(i) == 5
    assert len(i[4].z) == 42

    j.v.w['then'] = 34
    assert j.v.w['then'] == 34
    assert (
        'j'
        ':\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
        '\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00v'
        ':\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
        '\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00w'
        ':\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
        '\x00\x00\x00\x00\x00\x00\x00then') in td

    j.v.x[471734] = 7
    assert j.v.x[471734] == 7
    assert (
        'j'
        ':\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
        '\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00v'
        ':\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
        '\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00x'
        ':\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
        '\x00\x00\x00\x00\x00471734') in td

    nc.TypedStorage._key = original_key
    class TestTSC(nc.TypedStorageContract):

        address = utils.int_to_addr(2050)
        a = nc.Scalar('uint32')
        b = nc.List('uint16')
        c = nc.Dict('uint32')
        d = nc.IterableDict('uint256')
        e = nc.IterableDict('string')

        def _safe_call(ctx):
            # skalar
            assert ctx.a == 0
            ctx.a = 1
            assert ctx.a == 1

            ctx.a = 2
            assert ctx.a == 2

            # list
            assert isinstance(ctx.b, nc.List)
            ctx.b[0] = 10
            assert ctx.b[0] == 10

            ctx.b[1000] = 12
            assert ctx.b[1000] == 12

            assert len(ctx.b) == 1001
            ctx.b[1000] = 66
            assert ctx.b[1000] == 66
            assert len(ctx.b) == 1001

            ctx.b.append(99)
            assert len(ctx.b) == 1002
            ctx.b.append(99)
            assert len(ctx.b) == 1003

            # mapping
            assert isinstance(ctx.c, nc.Dict)
            key = b'test'
            assert ctx.c[key] == 0
            ctx.c[key] = 33
            assert ctx.c[key] == 33
            ctx.c[key] = 66
            assert ctx.c[key] == 66

            k = '\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x17q'
            v = 2146209080

            ctx.c[k] = v
            assert ctx.c[k] == v

            # iterable dict

            ctx.d[k] = v
            assert len(ctx.d) == 1
            assert ctx.d[k] == v
            ctx.d[k] = 0

            N = 10
            for i in range(1, N + 1):
                v = i**2
                k = bytes(i)
                ctx.d[k] = v
                assert ctx.d[k] == v
                assert len(list(ctx.d.keys())) == i
                assert set(ctx.d.keys()) == set(
                    [bytes(j) for j in range(1, i + 1)])
                assert set(ctx.d.values()) == set(
                    [j**2 for j in range(1, i + 1)])

            # iterable dict with strings
            N = 10
            for i in range(1, N + 1):
                v = str(i**2)
                k = bytes(i)
                ctx.e[k] = v
                # log.DEV('kv', k=k, v=v)
                assert ctx.e[k] == v, ctx.e[k]
                assert len(list(ctx.e.keys())) == i
                assert set(ctx.e.keys()) == set(
                    [bytes(j) for j in range(1, i + 1)])
                assert set(ctx.e.values()) == set(
                    [str(j**2) for j in range(1, i + 1)])

            print list(ctx.e.keys())
            print list(ctx.e.values())
            print len(list(ctx.e.keys()))

            return 1, 1, []