Esempio n. 1
0
def test_mediated_transfer():

    apps = create_network(num_nodes=10, num_assets=1, channels_per_node=2)
    a0 = apps[0]
    setup_messages_cb()

    # channels
    am0 = a0.raiden.assetmanagers.values()[0]

    # search for a path of length=2 A > B > C
    num_hops = 2
    source = a0.raiden.address
    paths = am0.channelgraph.get_paths_of_length(source, num_hops)
    assert len(paths)
    for p in paths:
        assert len(p) == num_hops + 1
        assert p[0] == source
    path = paths[0]
    target = path[-1]
    assert path in am0.channelgraph.get_paths(source, target)
    assert min(
        len(p)
        for p in am0.channelgraph.get_paths(source, target)) == num_hops + 1

    ams_by_address = dict(
        (a.raiden.address, a.raiden.assetmanagers) for a in apps)

    # addresses
    a, b, c = path

    # asset
    asset_address = am0.asset_address

    # channels
    c_ab = ams_by_address[a][asset_address].channels[b]
    c_ba = ams_by_address[b][asset_address].channels[a]
    c_bc = ams_by_address[b][asset_address].channels[c]
    c_cb = ams_by_address[c][asset_address].channels[b]

    # initial channel balances
    b_ab = c_ab.balance
    b_ba = c_ba.balance
    b_bc = c_bc.balance
    b_cb = c_cb.balance

    amount = 10
    # set shorter timeout for testing
    TransferTask.timeout_per_hop = 0.1

    a0.raiden.api.transfer(asset_address, amount, target)

    gevent.sleep(1.)

    # check
    assert b_ab - amount == c_ab.balance
    assert b_ba + amount == c_ba.balance
    assert b_bc - amount == c_bc.balance
    assert b_cb + amount == c_cb.balance
Esempio n. 2
0
def test_mediated_transfer():

    apps = create_network(num_nodes=10, num_assets=1, channels_per_node=2)
    a0 = apps[0]
    messages = setup_messages_cb()

    # channels
    am0 = a0.raiden.assetmanagers.values()[0]

    # search for a path of length=2 A > B > C
    num_hops = 2
    source = a0.raiden.address
    paths = am0.channelgraph.get_paths_of_length(source, num_hops)
    assert len(paths)
    for p in paths:
        assert len(p) == num_hops + 1
        assert p[0] == source
    path = paths[0]
    target = path[-1]
    assert path in am0.channelgraph.get_paths(source, target)
    assert min(len(p) for p in am0.channelgraph.get_paths(source, target)) == num_hops + 1

    ams_by_address = dict((a.raiden.address, a.raiden.assetmanagers) for a in apps)

    # addresses
    a, b, c = path

    # asset
    asset_address = am0.asset_address

    # channels
    c_ab = ams_by_address[a][asset_address].channels[b]
    c_ba = ams_by_address[b][asset_address].channels[a]
    c_bc = ams_by_address[b][asset_address].channels[c]
    c_cb = ams_by_address[c][asset_address].channels[b]

    # initial channel balances
    b_ab = c_ab.balance
    b_ba = c_ba.balance
    b_bc = c_bc.balance
    b_cb = c_cb.balance

    amount = 10
    # set shorter timeout for testing
    TransferTask.timeout_per_hop = 0.1

    a0.raiden.api.transfer(asset_address, amount, target)

    gevent.sleep(1.)

    # check
    assert b_ab - amount == c_ab.balance
    assert b_ba + amount == c_ba.balance
    assert b_bc - amount == c_bc.balance
    assert b_cb + amount == c_cb.balance
Esempio n. 3
0
def test_transfer():
    apps = create_network(num_nodes=2, num_assets=1, channels_per_node=1)
    a0, a1 = apps
    messages = setup_messages_cb()
    mlogger = MessageLogger()

    # channels
    am0 = a0.raiden.assetmanagers.values()[0]
    am1 = a1.raiden.assetmanagers.values()[0]

    assert am0.asset_address == am1.asset_address

    c0 = am0.channels[a1.raiden.address]
    c1 = am1.channels[a0.raiden.address]

    b0 = c0.balance
    b1 = c1.balance

    amount = 10
    target = a1.raiden.address
    assert target in am0.channels
    a0.raiden.api.transfer(am0.asset_address, amount, target=target)

    gevent.sleep(1)

    assert len(messages) == 2  # Transfer, Ack
    mt = decode(messages[0])
    assert isinstance(mt, Transfer)
    assert mt.balance == b1 + amount
    ma = decode(messages[1])
    assert isinstance(ma, Ack)
    assert ma.echo == mt.hash

    assert b1 + amount == c1.balance
    assert b0 - amount == c0.balance

    assert c0.locked.root == c1.partner.locked.root == c1.locked.root == ''

    a0_messages = mlogger.get_node_messages(a0)
    assert len(a0_messages) == 2
    assert isinstance(a0_messages[0], Transfer)
    assert isinstance(a0_messages[1], Ack)

    a0_sent_messages = mlogger.get_node_messages(a0, only_sent=True)
    assert len(a0_sent_messages) == 1
    assert isinstance(a0_sent_messages[0], Transfer)

    a0_recv_messages = mlogger.get_node_messages(a0, only_recv=True)
    assert len(a0_recv_messages) == 1
    assert isinstance(a0_recv_messages[0], Ack)

    a1_messages = mlogger.get_node_messages(a1)
    assert len(a1_messages) == 2
    assert isinstance(a1_messages[0], Transfer)
    assert isinstance(a1_messages[1], Ack)

    a1_sent_messages = mlogger.get_node_messages(a1, only_sent=True)
    assert len(a1_sent_messages) == 1
    assert isinstance(a1_sent_messages[0], Ack)

    a1_recv_messages = mlogger.get_node_messages(a1, only_recv=True)
    assert len(a1_recv_messages) == 1
    assert isinstance(a1_recv_messages[0], Transfer)
Esempio n. 4
0
def test_settlement():
    apps = create_network(num_nodes=2, num_assets=1, channels_per_node=1)
    a0, a1 = apps
    setup_messages_cb()

    # channels
    am0 = a0.raiden.assetmanagers.values()[0]
    am1 = a1.raiden.assetmanagers.values()[0]

    assert am0.asset_address == am1.asset_address

    c0 = am0.channels[a1.raiden.address]
    c1 = am1.channels[a0.raiden.address]

    b0 = c0.balance
    b1 = c1.balance

    lr0 = c0.locked.root
    lr0p = c0.partner.locked.root
    lr1 = c1.locked.root
    lr1p = c1.partner.locked.root

    amount = 10
    secret = 'secret'
    hashlock = sha3(secret)
    target = a1.raiden.address
    expiration = 10
    assert target in am0.channels

    t = c0.create_lockedtransfer(amount, expiration, hashlock)
    c0.raiden.sign(t)
    c0.register_transfer(t)
    c1.register_transfer(t)

    # balances are unchanged, but locksroot changed

    assert b1 == c1.balance
    assert b0 == c0.balance

    assert c0.locked.root == lr0
    assert c0.partner.locked.root != lr0p

    assert c1.locked.root != lr1
    assert c1.partner.locked.root == lr1p

    assert c1.locked.root == c0.partner.locked.root

    # now Bob learns the secret, but alice did not send a signed updated balance to reflect this
    # Bob wants to settle

    sc = c0.contract
    assert sc == c1.contract

    last_sent_transfers = [t]

    # get proof, that locked transfer was in merkle tree, with locked.root
    assert c1.locked
    merkle_proof = c1.locked.get_proof(t)
    # assert merkle_proof
    # assert merkle_proof[0] == t.locked.asstring
    root = c1.locked.root
    assert check_proof(merkle_proof, root, sha3(t.lock.asstring))

    unlocked = [(merkle_proof, t.lock.asstring, secret)]

    chain = a0.raiden.chain
    chain.block_number = 1

    sc.close(a1.raiden.address, last_sent_transfers, *unlocked)
    chain.block_number += sc.locked_time

    r = sc.settle()
    assert r[c1.address] == b1 + amount
    assert r[c0.address] == b0 - amount