예제 #1
0
def test_abicontract_interface():
    """ Test for issue #370. """
    tester_state = state()

    contract_path = path.join(CONTRACTS_DIR, 'simple_contract.sol')
    contract_name = 'Simple'
    simple_compiled = compile_file(contract_path)
    simple_data = solidity_get_contract_data(
        simple_compiled,
        contract_path,
        contract_name,
    )
    simple_address = tester_state.evm(simple_data['bin'])

    # ABIContract class must accept json_abi
    abi_json = json.dumps(simple_data['abi']).encode('utf-8')

    abi = ABIContract(
        _state=tester_state,
        _abi=abi_json,
        address=simple_address,
        listen=False,
        log_listener=None,
        default_key=None,
    )

    assert abi.test() == 1  # pylint: disable=no-member
예제 #2
0
def token(state, token_address, token_abi):
    translator = ContractTranslator(token_abi)

    return ABIContract(
        state,
        translator,
        token_address,
    )
예제 #3
0
def test_all_asset(asset_amount, state, channel, token, events):
    half_amount = asset_amount / 2
    assert token.transfer(tester.a1, half_amount) is True

    token1 = ABIContract(
        state,
        token.translator,
        token.address,
        default_key=tester.k1,
    )
    assert token.approve(channel.address, half_amount) is True
    assert token1.approve(channel.address, half_amount) is True

    channel1 = ABIContract(
        state,
        channel.translator,
        channel.address,
        default_key=tester.k1,
    )
    channel.deposit(half_amount)
    channel1.deposit(half_amount)

    _, deposit1, _, deposit2 = channel.addressAndBalance()

    assert deposit1 == half_amount
    assert deposit2 == half_amount

    assert token.balanceOf(channel.address) == asset_amount
    assert token.balanceOf(tester.a0) == 0
    assert token.balanceOf(tester.a1) == 0
예제 #4
0
def test_abicontract_interface():
    """ Test for issue #370. """
    tester_state = state()

    contract_path = path.join(CONTRACTS_DIR, 'simple_contract.sol')
    simple_compiled = compile_file(contract_path)
    simple_address = tester_state.evm(simple_compiled['Simple']['bin'])

    # ABIContract class must accept json_abi
    abi_json = json.dumps(simple_compiled['Simple']['abi']).encode('utf-8')

    abi = ABIContract(
        _state=tester_state,
        _abi=abi_json,
        address=simple_address,
        listen=False,
        log_listener=None,
        default_key=None,
    )

    assert abi.test() == 1  # pylint: disable=no-member
예제 #5
0
 def contract_at(self, address, abi):
     return ABIContract(self.s, abi, address)
예제 #6
0
def test_cmc(state, settle_timeout, netting_channel_abi, manager, events):  # pylint: disable=too-many-locals,too-many-statements
    address1 = sha3('address1')[:20]
    address3 = sha3('address3')[:20]
    inexisting_address = sha3('this_does_not_exist')[:20]

    netting_channel_translator = ContractTranslator(netting_channel_abi)

    assert len(manager.getChannelsParticipants()) == 0

    netting_channel_address1_hex = manager.newChannel(
        address1,
        settle_timeout,
    )

    # cannot have two channels at the same time
    with pytest.raises(TransactionFailed):
        manager.newChannel(address1, settle_timeout)

    # should trow if there is no channel for the given address
    with pytest.raises(TransactionFailed):
        manager.getChannelWith(inexisting_address)

    assert len(manager.getChannelsParticipants()) == 2

    netting_contract_proxy1 = ABIContract(
        state,
        netting_channel_translator,
        netting_channel_address1_hex,
    )

    assert netting_contract_proxy1.settleTimeout() == settle_timeout

    netting_channel_address2_hex = manager.newChannel(
        address3,
        settle_timeout,
    )

    assert manager.getChannelWith(address1) == netting_channel_address1_hex
    assert manager.getChannelWith(address3) == netting_channel_address2_hex

    msg_sender_channels = manager.nettingContractsByAddress(tester.DEFAULT_ACCOUNT)
    address1_channels = manager.nettingContractsByAddress(address1)
    inexisting_channels = manager.nettingContractsByAddress(inexisting_address)

    assert len(msg_sender_channels) == 2
    assert len(address1_channels) == 1
    assert len(inexisting_channels) == 0

    assert len(manager.getChannelsParticipants()) == 4

    assert len(events) == 2
    assert events[0]['_event_type'] == 'ChannelNew'
    assert events[0]['participant1'] == tester.a0.encode('hex')
    assert events[0]['participant2'] == address1.encode('hex')
    assert events[0]['nettingChannel'] == netting_channel_address1_hex
    assert events[0]['settleTimeout'] == 30
    assert events[1]['_event_type'] == 'ChannelNew'
    assert events[1]['participant1'] == tester.a0.encode('hex')
    assert events[1]['participant2'] == address3.encode('hex')
    assert events[1]['nettingChannel'] == netting_channel_address2_hex
    assert events[1]['settleTimeout'] == 30
예제 #7
0
def test_channelmanager(tester_state, tester_token, tester_events,
                        tester_channelmanager_library_address, settle_timeout,
                        netting_channel_abi):  # pylint: disable=too-many-locals,too-many-statements

    address0 = tester.DEFAULT_ACCOUNT
    address1 = tester.a1
    address2 = tester.a2
    nonexisting_address = sha3('this_does_not_exist')[:20]

    channelmanager_path = get_contract_path('ChannelManagerContract.sol')
    channel_manager = tester_state.abi_contract(
        None,
        path=channelmanager_path,
        language='solidity',
        constructor_parameters=[tester_token.address],
        contract_name='ChannelManagerContract',
        log_listener=tester_events.append,
        libraries={
            'ChannelManagerLibrary':
            tester_channelmanager_library_address.encode('hex'),
        })

    participants_count = len(channel_manager.getChannelsParticipants())
    assert participants_count == 0, 'newly deployed contract must be empty'

    netting_channel_translator = ContractTranslator(netting_channel_abi)

    previous_events = list(tester_events)
    netting_channel_address1_hex = channel_manager.newChannel(
        address1,
        settle_timeout,
    )
    assert len(previous_events) + 1 == len(
        tester_events), 'ChannelNew event must be fired.'

    channelnew_event = tester_events[-1]
    assert channelnew_event == {
        '_event_type': 'ChannelNew',
        'participant1': address0.encode('hex'),
        'participant2': address1.encode('hex'),
        'netting_channel': netting_channel_address1_hex,
        'settle_timeout': settle_timeout,
    }

    # should fail if settleTimeout is too low
    with pytest.raises(TransactionFailed):
        channel_manager.newChannel(address1, 5)

    # cannot have two channels at the same time
    with pytest.raises(TransactionFailed):
        channel_manager.newChannel(address1, settle_timeout)

    # should be zero address if there is no channel for the given address
    assert channel_manager.getChannelWith(nonexisting_address) == '0' * 40

    assert len(channel_manager.getChannelsParticipants()) == 2

    netting_contract_proxy1 = ABIContract(
        tester_state,
        netting_channel_translator,
        netting_channel_address1_hex,
    )

    assert netting_contract_proxy1.settleTimeout() == settle_timeout

    previous_events = list(tester_events)
    netting_channel_address2_hex = channel_manager.newChannel(
        address2,
        settle_timeout,
    )
    assert len(previous_events) + 1 == len(
        tester_events), 'ChannelNew event must be fired.'

    assert channel_manager.getChannelWith(
        address1) == netting_channel_address1_hex
    assert channel_manager.getChannelWith(
        address2) == netting_channel_address2_hex

    msg_sender_channels = channel_manager.nettingContractsByAddress(
        tester.DEFAULT_ACCOUNT)
    address1_channels = channel_manager.nettingContractsByAddress(address1)
    nonexisting_channels = channel_manager.nettingContractsByAddress(
        nonexisting_address)

    assert len(msg_sender_channels) == 2
    assert len(address1_channels) == 1
    assert len(nonexisting_channels) == 0

    assert len(channel_manager.getChannelsParticipants()) == 4

    channelnew_event = tester_events[-1]
    assert channelnew_event == {
        '_event_type': 'ChannelNew',
        'participant1': address0.encode('hex'),
        'participant2': address2.encode('hex'),
        'netting_channel': netting_channel_address2_hex,
        'settle_timeout': settle_timeout,
    }
예제 #8
0
def test_reopen_channel(tester_state, tester_events, tester_channelmanager,
                        tester_channels, settle_timeout, netting_channel_abi):

    privatekey0_raw, privatekey1_raw, nettingchannel, channel0, _ = tester_channels[
        0]

    privatekey0 = PrivateKey(privatekey0_raw)
    address0 = privatekey_to_address(privatekey0_raw)
    address1 = privatekey_to_address(privatekey1_raw)
    address2 = tester.a2

    # We need to close the channel before it can be deleted, to do so we need
    # one transfer to pass in close()
    transfer_amount = 10
    identifier = 1
    direct_transfer = channel0.create_directtransfer(
        transfer_amount,
        identifier,
    )
    direct_transfer.sign(privatekey0, address0)
    direct_transfer_data = str(direct_transfer.packed().data)

    should_be_nonce = nettingchannel.opened(sender=privatekey0_raw) * (2**32)
    should_be_nonce_plus_one = (nettingchannel.opened(sender=privatekey0_raw) +
                                1) * (2**32)
    assert should_be_nonce <= direct_transfer.nonce < should_be_nonce_plus_one

    # settle the channel should not change the channel manager state
    nettingchannel.close(
        direct_transfer_data,
        sender=privatekey1_raw,
    )
    tester_state.mine(number_of_blocks=settle_timeout + 1)

    nettingchannel.settle(sender=privatekey0_raw)

    tester_state.mine(1)

    # now a single new channel can be opened
    # if channel with address is settled a new can be opened
    # old entry will be deleted when calling newChannel
    netting_channel_address1_hex = tester_channelmanager.newChannel(
        address1,
        settle_timeout,
        sender=privatekey0_raw,
    )

    channeldelete_event = tester_events[-2]
    assert channeldelete_event == {
        '_event_type': 'ChannelDeleted',
        'caller_address': address0.encode('hex'),
        'partner': address1.encode('hex')
    }

    netting_channel_translator = ContractTranslator(netting_channel_abi)

    netting_contract_proxy1 = ABIContract(
        tester_state,
        netting_channel_translator,
        netting_channel_address1_hex,
    )

    # transfer not in nonce range
    with pytest.raises(TransactionFailed):
        netting_contract_proxy1.close(
            direct_transfer_data,
            sender=privatekey0_raw,
        )

    # channel already exists
    with pytest.raises(TransactionFailed):
        tester_channelmanager.newChannel(
            address1,
            settle_timeout,
            sender=privatekey0_raw,
        )

    # opening a new channel that did not exist before
    tester_channelmanager.newChannel(
        address2,
        settle_timeout,
        sender=privatekey0_raw,
    )
예제 #9
0
def test_cmc(state, settle_timeout, netting_channel_abi, manager, events):  # pylint: disable=too-many-locals,too-many-statements
    address1 = sha3('address1')[:20]
    address3 = sha3('address3')[:20]
    inexisting_address = sha3('this_does_not_exist')[:20]

    netting_channel_translator = ContractTranslator(netting_channel_abi)

    assert len(manager.getChannelsParticipants()) == 0

    netting_channel_address1_hex = manager.newChannel(
        address1,
        settle_timeout,
    )

    # cannot have two channels at the same time
    with pytest.raises(TransactionFailed):
        manager.newChannel(address1, settle_timeout)

    # should trow if there is no channel for the given address
    with pytest.raises(TransactionFailed):
        manager.getChannelWith(inexisting_address)

    assert len(manager.getChannelsParticipants()) == 2

    netting_contract_proxy1 = ABIContract(
        state,
        netting_channel_translator,
        netting_channel_address1_hex,
    )

    assert netting_contract_proxy1.settleTimeout() == settle_timeout

    netting_channel_address2_hex = manager.newChannel(
        address3,
        settle_timeout,
    )

    assert manager.getChannelWith(address1) == netting_channel_address1_hex
    assert manager.getChannelWith(address3) == netting_channel_address2_hex

    msg_sender_channels = manager.nettingContractsByAddress(
        tester.DEFAULT_ACCOUNT)
    address1_channels = manager.nettingContractsByAddress(address1)
    inexisting_channels = manager.nettingContractsByAddress(inexisting_address)

    assert len(msg_sender_channels) == 2
    assert len(address1_channels) == 1
    assert len(inexisting_channels) == 0

    assert len(manager.getChannelsParticipants()) == 4

    assert len(events) == 2
    assert events[0]['_event_type'] == 'ChannelNew'
    assert events[0]['participant1'] == tester.a0.encode('hex')
    assert events[0]['participant2'] == address1.encode('hex')
    assert events[0]['nettingChannel'] == netting_channel_address1_hex
    assert events[0]['settleTimeout'] == 30
    assert events[1]['_event_type'] == 'ChannelNew'
    assert events[1]['participant1'] == tester.a0.encode('hex')
    assert events[1]['participant2'] == address3.encode('hex')
    assert events[1]['nettingChannel'] == netting_channel_address2_hex
    assert events[1]['settleTimeout'] == 30
예제 #10
0
def test_settle(state, channel, token, asset_amount, events):
    half_amount = asset_amount / 2
    assert token.transfer(tester.a1, half_amount) is True

    token1 = ABIContract(
        state,
        token.translator,
        token.address,
        default_key=tester.k1,
    )
    assert token.approve(channel.address, half_amount) is True
    assert token1.approve(channel.address, half_amount) is True

    channel1 = ABIContract(
        state,
        channel.translator,
        channel.address,
        default_key=tester.k1,
    )
    channel.deposit(half_amount)
    channel1.deposit(half_amount)

    secret1 = 'x' * 32
    hashlock1 = sha3(secret1)
    lock_amount1 = 29
    lock_expiration1 = 1158003
    lock1 = Lock(lock_amount1, lock_expiration1, hashlock1)
    lockhash1 = sha3(lock1.as_bytes)
    merkleproof1 = [lockhash1]
    locksroot1 = merkleroot([lockhash1], merkleproof1)

    nonce1 = 1
    asset = token.address
    transfered_amount1 = 1
    recipient = tester.a1
    locksroot = locksroot1

    msg1 = DirectTransfer(
        nonce1,
        asset,
        transfered_amount1,
        recipient,
        locksroot,
    )
    msg1.sign(tester.k0)
    packed = msg1.packed()
    direct_transfer1 = str(packed.data)

    secret2 = 'y' * 32
    hashlock2 = sha3(secret2)
    lock_amount2 = 20
    lock_expiration2 = 1158005
    lock2 = Lock(lock_amount2, lock_expiration2, hashlock2)
    lockhash2 = sha3(lock2.as_bytes)
    merkleproof2 = [lockhash2]
    locksroot2 = merkleroot([lockhash2], merkleproof2)

    locksroot = locksroot2
    nonce2 = 2
    transfered_amount2 = 3

    msg2 = DirectTransfer(
        nonce2,
        token.address,  # asset
        transfered_amount2,
        tester.a0,  # recipient
        locksroot,
    )
    msg2.sign(tester.k1)
    packed = msg2.packed()
    direct_transfer2 = str(packed.data)

    # not yet closed. should fail
    with pytest.raises(TransactionFailed):
        channel.settle()

    channel.close(direct_transfer1, direct_transfer2)

    channel.unlock(
        str(lock1.as_bytes),
        ''.join(merkleproof1),
        secret1,
    )
    channel.unlock(str(lock2.as_bytes),
                   ''.join(merkleproof2),
                   secret2,
                   sender=tester.k1)

    secret4 = 'k' * 32
    hashlock4 = sha3(secret4)
    lock_amount4 = 23
    lock_expiration4 = 31
    lock4 = Lock(lock_amount4, lock_expiration4, hashlock4)
    hashlock4 = sha3(lock4.as_bytes)
    merkleproof4 = [hashlock4]

    # has now message, should fail
    with pytest.raises(TransactionFailed):
        channel.unlock(
            str(lock4.as_bytes),
            ''.join(merkleproof4),
            secret4,
            sender=tester.k1,
        )

    # still timeout
    with pytest.raises(TransactionFailed):
        channel.settle()

    state.block.number = state.block.number + 40  # timeout over
    channel.settle()

    balance1 = half_amount + (transfered_amount2 -
                              transfered_amount1) + lock_amount1 - lock_amount2
    balance2 = half_amount + (transfered_amount1 -
                              transfered_amount2) - lock_amount1 + lock_amount2
    assert token.balanceOf(tester.a0) == balance1
    assert token.balanceOf(tester.a1) == balance2

    # can settle only once
    with pytest.raises(TransactionFailed):
        channel.settle()

    assert len(events) == 6
    assert events[0]['_event_type'] == 'ChannelNewBalance'
    assert events[0]['assetAddress'] == token.address.encode('hex')
    assert events[0]['participant'] == tester.a0.encode('hex')
    assert events[0]['balance'] == 50
    assert events[1]['_event_type'] == 'ChannelNewBalance'
    assert events[1]['assetAddress'] == token.address.encode('hex')
    assert events[1]['participant'] == tester.a1.encode('hex')
    assert events[1]['balance'] == 50
    assert events[2]['_event_type'] == 'ChannelClosed'
    assert events[2]['closingAddress'] == tester.a0.encode('hex')
    assert events[2]['blockNumber'] == 1158002
    assert events[3]['_event_type'] == 'ChannelSecretRevealed'
    assert events[3]['secret'] == 'x' * 32
    assert events[4]['_event_type'] == 'ChannelSecretRevealed'
    assert events[4]['secret'] == 'y' * 32
    assert events[5]['_event_type'] == 'ChannelSettled'
    assert events[5]['blockNumber'] == state.block.number
예제 #11
0
def test_channelmanager(
    tester_state,
    tester_token,
    tester_events,
    tester_channelmanager_library_address,
    settle_timeout,
    netting_channel_abi,
):
    # pylint: disable=too-many-locals,too-many-statements

    address0 = tester.DEFAULT_ACCOUNT
    address1 = tester.a1
    address2 = tester.a2
    inexisting_address = sha3("this_does_not_exist")[:20]

    channelmanager_path = get_contract_path("ChannelManagerContract.sol")
    channel_manager = tester_state.abi_contract(
        None,
        path=channelmanager_path,
        language="solidity",
        constructor_parameters=[tester_token.address],
        contract_name="ChannelManagerContract",
        log_listener=tester_events.append,
        libraries={"ChannelManagerLibrary": tester_channelmanager_library_address.encode("hex")},
    )

    participants_count = len(channel_manager.getChannelsParticipants())
    assert participants_count == 0, "newly deployed contract must be empty"

    netting_channel_translator = ContractTranslator(netting_channel_abi)

    previous_events = list(tester_events)
    netting_channel_address1_hex = channel_manager.newChannel(address1, settle_timeout)
    assert len(previous_events) + 1 == len(tester_events), "ChannelNew event must be fired."

    channelnew_event = tester_events[-1]
    assert channelnew_event == {
        "_event_type": "ChannelNew",
        "participant1": address0.encode("hex"),
        "participant2": address1.encode("hex"),
        "netting_channel": netting_channel_address1_hex,
        "settle_timeout": settle_timeout,
    }

    # should fail if settleTimeout is too low
    with pytest.raises(TransactionFailed):
        channel_manager.newChannel(address1, 5)

    # cannot have two channels at the same time
    with pytest.raises(TransactionFailed):
        channel_manager.newChannel(address1, settle_timeout)

    # should trow if there is no channel for the given address
    with pytest.raises(TransactionFailed):
        channel_manager.getChannelWith(inexisting_address)

    assert len(channel_manager.getChannelsParticipants()) == 2

    netting_contract_proxy1 = ABIContract(tester_state, netting_channel_translator, netting_channel_address1_hex)

    assert netting_contract_proxy1.settleTimeout() == settle_timeout

    previous_events = list(tester_events)
    netting_channel_address2_hex = channel_manager.newChannel(address2, settle_timeout)
    assert len(previous_events) + 1 == len(tester_events), "ChannelNew event must be fired."

    assert channel_manager.getChannelWith(address1) == netting_channel_address1_hex
    assert channel_manager.getChannelWith(address2) == netting_channel_address2_hex

    msg_sender_channels = channel_manager.nettingContractsByAddress(tester.DEFAULT_ACCOUNT)
    address1_channels = channel_manager.nettingContractsByAddress(address1)
    inexisting_channels = channel_manager.nettingContractsByAddress(inexisting_address)

    assert len(msg_sender_channels) == 2
    assert len(address1_channels) == 1
    assert len(inexisting_channels) == 0

    assert len(channel_manager.getChannelsParticipants()) == 4

    channelnew_event = tester_events[-1]
    assert channelnew_event == {
        "_event_type": "ChannelNew",
        "participant1": address0.encode("hex"),
        "participant2": address2.encode("hex"),
        "netting_channel": netting_channel_address2_hex,
        "settle_timeout": settle_timeout,
    }