def test_transfer(): apps = create_network(num_nodes=2, num_assets=1, channels_per_node=1) a0, a1 = apps c0 = a0.raiden.assetmanagers.values()[0].channels.values()[0] c1 = a1.raiden.assetmanagers.values()[0].channels.values()[0] assert c0.contract == c1.contract assert c0.balance == c0.distributable == c0.contract.participants[ c0.address]['deposit'] assert c1.balance == c1.distributable == c1.contract.participants[ c1.address]['deposit'] amount = 10 assert amount < c0.distributable b0, b1 = c0.balance, c1.balance pb0, pb1 = c0.partner.balance, c1.partner.balance assert b0 == pb1 assert b1 == pb0 t = c0.create_transfer(amount=amount) c0.raiden.sign(t) c0.register_transfer(t) c1.register_transfer(t) assert c0.balance == c0.distributable == b0 - amount == c1.partner.balance assert c1.balance == c1.distributable == b1 + amount == c0.partner.balance assert c0.locked.outstanding == 0 assert c1.locked.outstanding == 0
def transfer_speed(num_transfers=100, max_locked=100): apps = create_network(num_nodes=2, num_assets=1, channels_per_node=1) a0, a1 = apps c0 = a0.raiden.assetmanagers.values()[0].channels.values()[0] c1 = a1.raiden.assetmanagers.values()[0].channels.values()[0] amounts = [a % 100 + 1 for a in range(1, num_transfers + 1)] secrets = [str(i) for i in range(num_transfers)] expiration = a0.raiden.chain.block_number + 100 st = time.time() for i, amount in enumerate(amounts): hashlock = sha3(secrets[i]) t = c0.create_lockedtransfer(amount=amount, expiration=expiration, hashlock=hashlock) t.sign(c0.address) c0.register_transfer(t) c1.register_transfer(t) if i > max_locked: idx = i - max_locked secret = secrets[idx] c0.claim_locked(secret) c1.claim_locked(secret) elapsed = time.time() - st print '%d transfers per second' % (num_transfers / elapsed)
def test_transfer(): apps = create_network(num_nodes=2, num_assets=1, channels_per_node=1) a0, a1 = apps c0 = a0.raiden.assetmanagers.values()[0].channels.values()[0] c1 = a1.raiden.assetmanagers.values()[0].channels.values()[0] assert c0.contract == c1.contract assert c0.balance == c0.distributable == c0.contract.participants[c0.address]['deposit'] assert c1.balance == c1.distributable == c1.contract.participants[c1.address]['deposit'] amount = 10 assert amount < c0.distributable b0, b1 = c0.balance, c1.balance pb0, pb1 = c0.partner.balance, c1.partner.balance assert b0 == pb1 assert b1 == pb0 t = c0.create_transfer(amount=amount) t.sign(c0.address) c0.register_transfer(t) c1.register_transfer(t) assert c0.balance == c0.distributable == b0 - amount == c1.partner.balance assert c1.balance == c1.distributable == b1 + amount == c0.partner.balance assert c0.locked.outstanding == 0 assert c1.locked.outstanding == 0
def transfer_speed(num_transfers=100, max_locked=100): apps = create_network(num_nodes=2, num_assets=1, channels_per_node=1) a0, a1 = apps c0 = a0.raiden.assetmanagers.values()[0].channels.values()[0] c1 = a1.raiden.assetmanagers.values()[0].channels.values()[0] amounts = [a % 100 + 1 for a in range(1, num_transfers + 1)] secrets = [str(i) for i in range(num_transfers)] expiration = a0.raiden.chain.block_number + 100 st = time.time() for i, amount in enumerate(amounts): hashlock = sha3(secrets[i]) t = c0.create_lockedtransfer(amount=amount, expiration=expiration, hashlock=hashlock) c0.raiden.sign(t) c0.register_transfer(t) c1.register_transfer(t) if i > max_locked: idx = i - max_locked secret = secrets[idx] c0.claim_locked(secret) c1.claim_locked(secret) elapsed = time.time() - st print '%d transfers per second' % (num_transfers / elapsed)
def test_locked_transfer(): apps = create_network(num_nodes=2, num_assets=1, channels_per_node=1) a0, a1 = apps c0 = a0.raiden.assetmanagers.values()[0].channels.values()[0] c1 = a1.raiden.assetmanagers.values()[0].channels.values()[0] b0, b1 = c0.balance, c1.balance amount = 10 secret = 'secret' expiration = a0.raiden.chain.block_number + 100 hashlock = sha3(secret) t = c0.create_lockedtransfer(amount=amount, expiration=expiration, hashlock=hashlock) c0.raiden.sign(t) c0.register_transfer(t) c1.register_transfer(t) assert hashlock in c1.locked assert hashlock in c0.partner.locked assert len(c0.locked) == 0 assert len(c0.partner.locked) == 1 assert len(c1.locked) == 1 assert len(c1.partner.locked) == 0 assert c0.balance == b0 assert c0.distributable == c0.balance - amount assert c1.balance == c1.distributable == b1 assert c0.locked.outstanding == 0 assert c0.partner.locked.outstanding == amount assert c1.locked.outstanding == amount assert c1.partner.locked.outstanding == 0 assert c0.locked.root == '' assert c1.partner.locked.root == '' assert c1.locked.root == merkleroot( [sha3(tx.lock.asstring) for tx in c1.locked.locked.values()]) assert c0.partner.locked.root == c1.locked.root # reveal secret c0.claim_locked(secret) c1.claim_locked(secret) assert c0.balance == b0 - amount assert c0.distributable == c0.balance assert c1.balance == c1.distributable == b1 + amount assert c0.locked.outstanding == 0 assert c0.partner.locked.outstanding == 0 assert c1.locked.outstanding == 0 assert c1.partner.locked.outstanding == 0 assert len(c0.locked) == 0 assert len(c0.partner.locked) == 0 assert len(c1.locked) == 0 assert len(c1.partner.locked) == 0 assert c0.locked.root == '' assert c1.partner.locked.root == '' assert c1.locked.root == '' assert c0.partner.locked.root == ''
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
def test_mediated_transfer(num_transfers=100): apps = create_network( num_nodes=10, num_assets=1, channels_per_node=2, transport_class=UDPTransport) a0 = apps[0] # 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 assetmanagers_by_address = dict((a.raiden.address, a.raiden.assetmanagers) for a in apps) # addresses a, b, c = path # asset asset_address = am0.asset_address amount = 10 # set shorter timeout for testing TransferTask.timeout_per_hop = 0.1 st = time.time() finished = gevent.event.Event() def _completion_cb(task, success): print print 'task completed', task, success, _completion_cb.num_transfers _completion_cb.num_transfers -= 1 if _completion_cb.num_transfers > 0: a0.raiden.api.transfer(asset_address, amount, target) else: finished.set() _completion_cb.num_transfers = num_transfers am1 = assetmanagers_by_address[b][asset_address] am1.transfermanager.on_task_completed_callbacks.append(_completion_cb) a0.raiden.api.transfer(asset_address, amount, target) finished.wait() elapsed = time.time() - st completed_transfers = num_transfers - _completion_cb.num_transfers tps = completed_transfers / elapsed print 'transfers completed:{} per second:{}'.format(completed_transfers, tps)
def test_setup(): apps = create_network(num_nodes=2, num_assets=1, channels_per_node=1) a0, a1 = apps s = a0.raiden.chain.channelmanager_by_asset(a0.raiden.chain.asset_addresses[0]) assert s.nettingcontracts assert s.nettingcontracts_by_address(a0.raiden.address) assert a0.raiden.assetmanagers.keys() == a1.raiden.assetmanagers.keys() assert len(a0.raiden.assetmanagers) == 1
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
def test_setup(): apps = create_network(num_nodes=2, num_assets=1, channels_per_node=1) a0, a1 = apps s = a0.raiden.chain.channelmanager_by_asset( a0.raiden.chain.asset_addresses[0]) assert s.nettingcontracts assert s.nettingcontracts_by_address(a0.raiden.address) assert a0.raiden.assetmanagers.keys() == a1.raiden.assetmanagers.keys() assert len(a0.raiden.assetmanagers) == 1
def test_ping(): apps = create_network(num_nodes=2, num_assets=0, channels_per_node=0) a0, a1 = apps messages = setup_messages_cb() p = Ping(nonce=0) a0.raiden.sign(p) a0.raiden.protocol.send(a1.raiden.address, p) gevent.sleep(0.1) assert len(messages) == 2 # Ping, Ack assert decode(messages[0]) == p a = decode(messages[1]) assert isinstance(a, Ack) assert a.echo == p.hash
def test_ping_dropped_message(): apps = create_network(num_nodes=2, num_assets=0, channels_per_node=0, transport_class=UnreliableTransport) a0, a1 = apps # mock transport with packet loss, every 3nd is lost, starting with first message UnreliableTransport.droprate = 3 RaidenProtocol.try_interval = 0.1 # for fast tests RaidenProtocol.repeat_messages = True messages = setup_messages_cb() UnreliableTransport.network.counter = 0 p = Ping(nonce=0) a0.raiden.sign(p) a0.raiden.protocol.send(a1.raiden.address, p) gevent.sleep(1) assert len(messages) == 3 # Ping(dropped), Ping, Ack for i in [0, 1]: assert decode(messages[i]) == p for i in [2]: a = decode(messages[i]) assert isinstance(a, Ack) assert a.echo == p.hash # try failing Ack messages = setup_messages_cb() assert not messages UnreliableTransport.network.counter = 2 # first message sent, 2nd dropped p = Ping(nonce=0) a0.raiden.sign(p) a0.raiden.protocol.send(a1.raiden.address, p) gevent.sleep(1) for m in messages: print decode(m) assert len(messages) == 4 # Ping, Ack(dropped), Ping, Ack for i in [0, 2]: assert decode(messages[i]) == p for i in [1, 3]: a = decode(messages[i]) assert isinstance(a, Ack) assert a.echo == p.hash RaidenProtocol.repeat_messages = False
def test_create_network(): print 'hej' apps = create_network(num_nodes=10, num_assets=2, channels_per_node=4) assert len(apps) == 10 # All apps must reference the same chain for app in apps: assert app.raiden.chain == apps[0].raiden.chain # All apps must have 2 asset managers (one per each asset) for app in apps: assert len(set(app.raiden.assetmanagers.keys())) == 2 # All apps must have uniq private keys assert len(set([app.raiden.privkey for app in apps])) == 10
def test_interwoven_transfers(num=100): apps = create_network(num_nodes=2, num_assets=1, channels_per_node=1) a0, a1 = apps c0 = a0.raiden.assetmanagers.values()[0].channels.values()[0] c1 = a1.raiden.assetmanagers.values()[0].channels.values()[0] b0, b1 = c0.balance, c1.balance amounts = range(1, num + 1) secrets = [str(i) for i in range(num)] expiration = a0.raiden.chain.block_number + 100 claimed = [] for i, amount in enumerate(amounts): hashlock = sha3(secrets[i]) t = c0.create_lockedtransfer(amount=amount, expiration=expiration, hashlock=hashlock) c0.raiden.sign(t) c0.register_transfer(t) c1.register_transfer(t) claimed_amount = sum([amounts[j] for j in claimed]) distributed_amount = sum(amounts[:i + 1]) # print i, claimed_amount, distributed_amount, amounts[:i + 1] assert c0.balance == b0 - claimed_amount assert c0.distributable == b0 - distributed_amount assert c0.distributable == c0.balance - distributed_amount + claimed_amount assert c1.balance == c1.distributable == b1 + claimed_amount assert c0.locked.outstanding == 0 assert c1.locked.outstanding == c0.partner.locked.outstanding == sum( amounts[:i + 1]) - claimed_amount assert c1.partner.locked.outstanding == 0 assert c0.partner.locked.root == c1.locked.root if i > 0 and i % 2 == 0: idx = i - 1 # print 'claiming', idx, amounts[idx] claimed.append(idx) secret = secrets[idx] c0.claim_locked(secret) c1.claim_locked(secret)
def test_interwoven_transfers(num=100): apps = create_network(num_nodes=2, num_assets=1, channels_per_node=1) a0, a1 = apps c0 = a0.raiden.assetmanagers.values()[0].channels.values()[0] c1 = a1.raiden.assetmanagers.values()[0].channels.values()[0] b0, b1 = c0.balance, c1.balance amounts = range(1, num + 1) secrets = [str(i) for i in range(num)] expiration = a0.raiden.chain.block_number + 100 claimed = [] for i, amount in enumerate(amounts): hashlock = sha3(secrets[i]) t = c0.create_lockedtransfer(amount=amount, expiration=expiration, hashlock=hashlock) t.sign(c0.address) c0.register_transfer(t) c1.register_transfer(t) claimed_amount = sum([amounts[j] for j in claimed]) distributed_amount = sum(amounts[:i + 1]) # print i, claimed_amount, distributed_amount, amounts[:i + 1] assert c0.balance == b0 - claimed_amount assert c0.distributable == b0 - distributed_amount assert c0.distributable == c0.balance - distributed_amount + claimed_amount assert c1.balance == c1.distributable == b1 + claimed_amount assert c0.locked.outstanding == 0 assert c1.locked.outstanding == c0.partner.locked.outstanding == sum( amounts[:i + 1]) - claimed_amount assert c1.partner.locked.outstanding == 0 assert c0.partner.locked.root == c1.locked.root if i > 0 and i % 2 == 0: idx = i - 1 # print 'claiming', idx, amounts[idx] claimed.append(idx) secret = secrets[idx] c0.claim_locked(secret) c1.claim_locked(secret)
def test_transfer(): apps = create_network(num_nodes=2, num_assets=1, channels_per_node=1) a0, a1 = apps messages = 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 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 == ''
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)
def test_settlement(): apps = create_network(num_nodes=2, num_assets=1, channels_per_node=1) a0, a1 = apps messages = setup_messages_cb(a0.transport) # 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
def test_mediated_transfer(num_transfers=100, num_nodes=10, num_assets=1, channels_per_node=2): apps = create_network( num_nodes=num_nodes, num_assets=num_assets, channels_per_node=channels_per_node, transport_class=UDPTransport) def start_transfers(idx, num_transfers): a0 = apps[idx] # channels assets = sorted(a0.raiden.assetmanagers.keys()) asset = assets[idx] am0 = a0.raiden.assetmanagers[asset] # 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 assetmanagers_by_address = dict((a.raiden.address, a.raiden.assetmanagers) for a in apps) # addresses a, b, c = path # asset asset_address = am0.asset_address amount = 10 # set shorter timeout for testing TransferTask.timeout_per_hop = 0.1 finished = gevent.event.Event() def _completion_cb(task, success): print print 'task completed', task, success, _completion_cb.num_transfers _completion_cb.num_transfers -= 1 if _completion_cb.num_transfers > 0: a0.raiden.api.transfer(asset_address, amount, target) else: finished.set() _completion_cb.num_transfers = num_transfers am1 = assetmanagers_by_address[b][asset_address] am1.transfermanager.on_task_completed_callbacks.append(_completion_cb) a0.raiden.api.transfer(asset_address, amount, target) return finished # Start timer to report elapsed time st = time.time() finished_events = [] assert num_assets <= num_nodes # Start all transfers for i in range(num_assets): f = start_transfers(i, num_transfers) finished_events.append(f) # Wait until all transfers are done gevent.wait(finished_events) elapsed = time.time() - st completed_transfers = num_transfers * num_assets tps = completed_transfers / elapsed print 'transfers completed:{} per second:{}'.format(completed_transfers, tps)
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
def test_mediated_transfer(num_transfers=100, num_nodes=10, num_assets=1, channels_per_node=2): # pylint: disable=too-many-locals apps = create_network( num_nodes=num_nodes, num_assets=num_assets, channels_per_node=channels_per_node, ) assert len(apps) > num_assets def start_transfers(idx, num_transfers): a0 = apps[idx] # channels assets = sorted(a0.raiden.assetmanagers.keys()) asset = assets[idx] am0 = a0.raiden.assetmanagers[asset] # 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_shortest_paths(source, target) assert min( len(p) for p in am0.channelgraph.get_shortest_paths( source, target)) == num_hops + 1 assetmanagers_by_address = dict( (a.raiden.address, a.raiden.assetmanagers) for a in apps) # addresses a, b, c = path # asset asset_address = am0.asset_address amount = 10 finished = gevent.event.Event() def _completion_cb(task, success): _completion_cb.num_transfers -= 1 if _completion_cb.num_transfers > 0: a0.raiden.api.transfer(asset_address, amount, target) else: finished.set() _completion_cb.num_transfers = num_transfers am1 = assetmanagers_by_address[b][asset_address] am1.transfermanager.on_task_completed_callbacks.append(_completion_cb) a0.raiden.api.transfer(asset_address, amount, target) return finished # Start timer to report elapsed time st = time.time() finished_events = [] assert num_assets <= num_nodes # Start all transfers for i in range(num_assets): print "finished", i f = start_transfers(i, num_transfers) finished_events.append(f) # Wait until all transfers are done gevent.wait(finished_events) elapsed = time.time() - st completed_transfers = num_transfers * num_assets tps = completed_transfers / elapsed print 'Completed {} transfers at {} tps'.format(completed_transfers, tps)