def test_events():
    # create multiple nac instances and assert they are different contracts
    state = tester.state()
    nc.registry.register(EventNAC)

    # create proxies
    nc.listen_logs(state, Shout)
    c0 = nc.tester_nac(state, tester.k0, EventNAC.address)
    c0.afunc(1, 2)
def test_events():
    # create multiple nac instances and assert they are different contracts
    state = tester.state()
    nc.registry.register(EventNAC)

    # create proxies
    nc.listen_logs(state, Shout)
    c0 = nc.tester_nac(state, tester.k0, EventNAC.address)
    c0.afunc(1, 2)
def test_owned():

    class TestTSC(nc.NativeContract):

        address = utils.int_to_addr(2051)
        owner = nc.Scalar('address')

        def own(ctx, returns=None):
            if ctx.owner == '\0' * 20:
                ctx.owner = ctx.tx_origin
                assert ctx.owner == ctx.tx_origin

        def assert_owner(ctx):
            if ctx.tx_origin != ctx.owner:
                raise RuntimeError('not owner')

        @nc.constant
        def protected(ctx, returns='uint32'):
            ctx.assert_owner()
            return 1

    assert TestTSC.protected.is_constant == True

    state = tester.state()
    nc.registry.register(TestTSC)

    a0 = nc.tester_create_native_contract_instance(state, tester.k0, TestTSC)
    c0 = nc.tester_nac(state, tester.k0, a0)

    c0.own()
    assert c0.protected() == 1
    c0k1 = nc.tester_nac(state, tester.k1, a0)
    try:
        c0k1.protected()
    except tester.TransactionFailed:
        pass
    else:
        assert False, 'must not access protected if not owner'

    nc.registry.unregister(TestTSC)
def test_owned():
    class TestTSC(nc.NativeContract):

        address = utils.int_to_addr(2051)
        owner = nc.Scalar('address')

        def own(ctx, returns=None):
            if ctx.owner == '\0' * 20:
                ctx.owner = ctx.tx_origin
                assert ctx.owner == ctx.tx_origin

        def assert_owner(ctx):
            if ctx.tx_origin != ctx.owner:
                raise RuntimeError('not owner')

        @nc.constant
        def protected(ctx, returns='uint32'):
            ctx.assert_owner()
            return 1

    assert TestTSC.protected.is_constant is True

    state = tester.state()
    nc.registry.register(TestTSC)

    a0 = nc.tester_create_native_contract_instance(state, tester.k0, TestTSC)
    c0 = nc.tester_nac(state, tester.k0, a0)

    c0.own()
    assert c0.protected() == 1
    c0k1 = nc.tester_nac(state, tester.k1, a0)
    try:
        c0k1.protected()
    except tester.TransactionFailed:
        pass
    else:
        assert False, 'must not access protected if not owner'

    nc.registry.unregister(TestTSC)
def test_nac_instances():
    # create multiple nac instances and assert they are different contracts
    state = tester.state()
    nc.registry.register(SampleNAC)

    a0 = nc.tester_create_native_contract_instance(state, tester.k0, SampleNAC)
    a1 = nc.tester_create_native_contract_instance(state, tester.k0, SampleNAC)
    a2 = nc.tester_create_native_contract_instance(state, tester.k0, SampleNAC)

    assert a0 != a1 != a2
    assert len(a0) == 20

    # create proxies
    c0 = nc.tester_nac(state, tester.k0, a0)
    c1 = nc.tester_nac(state, tester.k0, a1)
    c2 = nc.tester_nac(state, tester.k0, a2)

    assert c0.get_address() == a0
    assert c1.get_address() == a1
    assert c2.get_address() == a2

    assert c0.afunc(5, 6) == 30
    assert c0.efunc([4, 8]) == 32
    nc.registry.unregister(SampleNAC)
def test_nac_instances():
    # create multiple nac instances and assert they are different contracts
    state = tester.state()
    nc.registry.register(SampleNAC)

    a0 = nc.tester_create_native_contract_instance(state, tester.k0, SampleNAC)
    a1 = nc.tester_create_native_contract_instance(state, tester.k0, SampleNAC)
    a2 = nc.tester_create_native_contract_instance(state, tester.k0, SampleNAC)

    assert a0 != a1 != a2
    assert len(a0) == 20

    # create proxies
    c0 = nc.tester_nac(state, tester.k0, a0)
    c1 = nc.tester_nac(state, tester.k0, a1)
    c2 = nc.tester_nac(state, tester.k0, a2)

    assert c0.get_address() == a0
    assert c1.get_address() == a1
    assert c2.get_address() == a2

    assert c0.afunc(5, 6) == 30
    assert c0.efunc([4, 8]) == 32
    nc.registry.unregister(SampleNAC)
Exemple #7
0
def test_fungible_instance():
    state = tester.state()
    creator_address = tester.a0
    creator_key = tester.k0

    nc.registry.register(Fungible)

    # Create proxy
    EUR_address = nc.tester_create_native_contract_instance(
        state, creator_key, Fungible)
    fungible_as_creator = nc.tester_nac(state, creator_key, EUR_address)
    # Initalize fungible with a fixed quantity of fungibles.
    fungible_total = 1000000
    fungible_as_creator.init(fungible_total)
    assert fungible_as_creator.balanceOf(creator_address) == fungible_total
    nc.registry.unregister(Fungible)
Exemple #8
0
def test_iou_template():
    """
    Tests;
        IOU initialization as Issuer,
        Testing issue funds, get_issued_amount
    """

    # Register Contract Fungible
    nc.registry.register(IOU)

    # Initialize Participants and Fungible contract
    state = tester.state()
    logs = []
    issuer_address = tester.a0
    issuer_key = tester.k0
    # create listeners
    for evt_class in IOU.events:
        nc.listen_logs(state, evt_class, callback=lambda e: logs.append(e))

    # Initialization
    iou_address = nc.tester_create_native_contract_instance(
        state, issuer_key, IOU)
    iou_as_issuer = nc.tester_nac(state, issuer_key, iou_address)
    iou_as_issuer.init()
    assert iou_as_issuer.balanceOf(issuer_address) == 0
    amount_issued = 200000
    iou_as_issuer.issue_funds(amount_issued, '')
    assert iou_as_issuer.balanceOf(issuer_address) == amount_issued

    iou_as_issuer.issue_funds(amount_issued, '')
    assert iou_as_issuer.balanceOf(issuer_address) == 2 * amount_issued
    assert iou_as_issuer.get_issued_amount(issuer_address) == 2 * amount_issued

    print logs
    while logs and logs.pop():
        pass

    nc.registry.unregister(IOU)
Exemple #9
0
def test_fungible_template():
    """
    Tests;
        Fungible initialization as Creator,
        Creator sends Fungibles to Alice,
        Alice sends Fungibles to Bob,
        Bob approves Creator to spend Fungibles on his behalf,
        Creator allocates these Fungibles from Bob to Alice,
        Testing of non-standardized functions of the Fungible contract.
    Events;
        Checking logs from Transfer and Approval Events
    """

    # Register Contract Fungible
    nc.registry.register(Fungible)

    # Initialize Participants and Fungible contract
    state = tester.state()
    logs = []
    creator_address = tester.a0
    creator_key = tester.k0
    alice_address = tester.a1
    alice_key = tester.k1
    bob_address = tester.a2
    bob_key = tester.k2
    # Create proxy
    nc.listen_logs(state, Transfer, callback=lambda e: logs.append(e))
    nc.listen_logs(state, Approval, callback=lambda e: logs.append(e))
    fungible_as_creator = nc.tester_nac(state, creator_key, Fungible.address)
    # Initalize fungible with a fixed quantity of fungibles.
    fungible_total = 1000000
    fungible_as_creator.init(fungible_total)
    assert fungible_as_creator.balanceOf(creator_address) == fungible_total

    # Creator transfers Fungibles to Alice
    send_amount_alice = 700000
    fungible_as_creator.transfer(alice_address, send_amount_alice)
    assert fungible_as_creator.balanceOf(
        creator_address) == fungible_total - send_amount_alice
    assert fungible_as_creator.balanceOf(alice_address) == send_amount_alice
    # Check logs data of Transfer Event
    assert len(logs) == 1
    l = logs[0]
    assert l['event_type'] == 'Transfer'
    assert l['from'] == creator_address
    assert l['to'] == alice_address
    # Build transaction Log arguments and check sent amount
    assert l['value'] == send_amount_alice

    # Alice transfers Fungibles to Bob
    send_amount_bob = 400000
    # Create proxy for Alice
    fungible_as_alice = nc.tester_nac(state, alice_key, Fungible.address)
    fungible_as_alice.transfer(bob_address, send_amount_bob)
    # Test balances of Creator, Alice and Bob
    creator_balance = fungible_total - send_amount_alice
    alice_balance = send_amount_alice - send_amount_bob
    bob_balance = send_amount_bob
    assert fungible_as_alice.balanceOf(creator_address) == creator_balance
    assert fungible_as_alice.balanceOf(alice_address) == alice_balance
    assert fungible_as_alice.balanceOf(bob_address) == bob_balance

    # Create proxy for Bob
    fungible_as_bob = nc.tester_nac(state, bob_key, Fungible.address)
    approved_amount_bob = 100000
    assert fungible_as_bob.allowance(creator_address) == 0
    # Bob approves Creator to spend Fungibles
    assert fungible_as_bob.allowance(creator_address) == 0
    fungible_as_bob.approve(creator_address, approved_amount_bob)
    assert fungible_as_bob.allowance(creator_address) == approved_amount_bob

    # Test transferFrom function, i.e. direct debit.
    fungible_as_creator.transferFrom(bob_address, alice_address,
                                     approved_amount_bob)
    # Test balances
    alice_balance += approved_amount_bob
    bob_balance -= approved_amount_bob
    assert fungible_as_alice.balanceOf(creator_address) == creator_balance
    assert fungible_as_alice.balanceOf(alice_address) == alice_balance
    assert fungible_as_alice.balanceOf(bob_address) == bob_balance
    # Check logs data of Transfer Event
    assert len(logs) == 4
    l = logs[-1]
    assert l['event_type'] == 'Transfer'
    assert l['from'] == bob_address
    assert l['to'] == alice_address
    # Build transaction Log arguments and check sent amount
    assert l['value'] == approved_amount_bob

    # Testing account information
    # Now we should have three Fungible accounts
    assert 3 == fungible_as_alice.num_accounts()
    r = fungible_as_creator.get_creator()
    assert r == creator_address
    r = fungible_as_creator.get_accounts()
    assert set(r) == set([creator_address, alice_address, bob_address])

    print logs
    while logs and logs.pop():
        pass

    nc.registry.unregister(Fungible)
def test_nativeabicontract_with_storage():

    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

    state = tester.state()
    nc.registry.register(TestTSC)

    # deploy two instances
    a0 = nc.tester_create_native_contract_instance(state, tester.k0, TestTSC)
    a1 = nc.tester_create_native_contract_instance(state, tester.k0, TestTSC)

    # create proxies
    c0 = nc.tester_nac(state, tester.k0, a0)
    c1 = nc.tester_nac(state, tester.k0, a1)

    size = 20
    c0.setup_numbers(size)
    assert c1.sum_numbers() == 0
    assert c0.sum_numbers() == sum(range(size))
    c1.setup_numbers(size)
    assert c0.sum_numbers() == sum(range(size))
    assert c1.sum_numbers() == sum(range(size))

    param = 5
    assert c0.muladdsize(param) == (size + param) * param

    # words
    c1.setup_words(param)
    assert c1.get_word(b'key2') == b'word2'

    assert c0.get_word(b'key2') == b''
    c0.setup_words(param)
    assert c0.get_word(b'key2') == b'word2'

    nc.registry.unregister(TestTSC)
def test_nativeabicontract_with_storage():
    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

    state = tester.state()
    nc.registry.register(TestTSC)

    # deploy two instances
    a0 = nc.tester_create_native_contract_instance(state, tester.k0, TestTSC)
    a1 = nc.tester_create_native_contract_instance(state, tester.k0, TestTSC)

    # create proxies
    c0 = nc.tester_nac(state, tester.k0, a0)
    c1 = nc.tester_nac(state, tester.k0, a1)

    size = 20
    c0.setup_numbers(size)
    assert c1.sum_numbers() == 0
    assert c0.sum_numbers() == sum(range(size))
    c1.setup_numbers(size)
    assert c0.sum_numbers() == sum(range(size))
    assert c1.sum_numbers() == sum(range(size))

    param = 5
    assert c0.muladdsize(param) == (size + param) * param

    # words
    c1.setup_words(param)
    assert c1.get_word(b'key2') == b'word2'

    assert c0.get_word(b'key2') == b''
    c0.setup_words(param)
    assert c0.get_word(b'key2') == b'word2'

    nc.registry.unregister(TestTSC)