def test_channel_must_accept_expired_locks(): """ A node may go offline for an undetermined period of time, and when it comes back online it must accept the messages that are waiting, otherwise the partner node won't make progress with its queue. If a N node goes offline for a number B of blocks, and the partner does not close the channel, when N comes back online some of the messages from its partner may become expired. Neverthless these messages are ordered and must be accepted for the partner to make progress with its queue. Note: Accepting a message with an expired lock does *not* imply the token transfer happened, and the receiver node must *not* forward the transfer, only accept the message allowing the partner to progress with its message queue. """ balance1 = 70 balance2 = 110 reveal_timeout = 5 settle_timeout = 15 privkey1, address1 = make_privkey_address() privkey2, address2 = make_privkey_address() token_address = make_address() our_state = ChannelEndState( address1, balance1, None, EMPTY_MERKLE_TREE, ) partner_state = ChannelEndState( address2, balance2, None, EMPTY_MERKLE_TREE, ) external_state = make_external_state() test_channel = Channel( our_state, partner_state, external_state, token_address, reveal_timeout, settle_timeout, ) block_number = 10 transfer = make_mediated_transfer( nonce=test_channel.get_next_nonce(), token=test_channel.token_address, channel=test_channel.channel_address, expiration=block_number + settle_timeout, recipient=address1, ) transfer.sign(privkey2, address2) test_channel.register_transfer( block_number + settle_timeout + 1, transfer, )
def test_channel_increase_nonce_and_transferred_amount(): """ The nonce must increase with each new transfer. """ token_address = make_address() privkey1, address1 = make_privkey_address() address2 = make_address() balance1 = 70 balance2 = 110 reveal_timeout = 5 settle_timeout = 15 our_state = ChannelEndState(address1, balance1, None, EMPTY_MERKLE_TREE) partner_state = ChannelEndState(address2, balance2, None, EMPTY_MERKLE_TREE) external_state = make_external_state() test_channel = Channel( our_state, partner_state, external_state, token_address, reveal_timeout, settle_timeout, ) previous_nonce = test_channel.get_next_nonce() previous_transferred = test_channel.transferred_amount amount = 7 block_number = 1 for _ in range(10): direct_transfer = test_channel.create_directtransfer(amount, identifier=1) direct_transfer.sign(privkey1, address1) test_channel.register_transfer(block_number, direct_transfer) new_nonce = test_channel.get_next_nonce() new_transferred = test_channel.transferred_amount assert new_nonce == previous_nonce + 1 assert new_transferred == previous_transferred + amount previous_nonce = new_nonce previous_transferred = new_transferred
def test_python_channel(): token_address = make_address() privkey1, address1 = make_privkey_address() address2 = make_address() balance1 = 70 balance2 = 110 reveal_timeout = 5 settle_timeout = 15 block_number = 10 our_state = ChannelEndState(address1, balance1, None, EMPTY_MERKLE_TREE) partner_state = ChannelEndState(address2, balance2, None, EMPTY_MERKLE_TREE) external_state = make_external_state() test_channel = Channel( our_state, partner_state, external_state, token_address, reveal_timeout, settle_timeout, ) assert test_channel.contract_balance == our_state.contract_balance assert test_channel.transferred_amount == our_state.transferred_amount assert test_channel.distributable == our_state.contract_balance assert test_channel.outstanding == our_state.amount_locked assert test_channel.outstanding == 0 assert test_channel.locked == partner_state.amount_locked assert test_channel.our_state.amount_locked == 0 assert test_channel.partner_state.amount_locked == 0 assert test_channel.get_next_nonce() == 1 with pytest.raises(ValueError): test_channel.create_directtransfer( -10, identifier=1, ) with pytest.raises(ValueError): test_channel.create_directtransfer( balance1 + 10, identifier=1, ) amount1 = 10 directtransfer = test_channel.create_directtransfer( amount1, identifier=1, ) directtransfer.sign(privkey1, address1) test_channel.register_transfer( block_number, directtransfer, ) assert test_channel.contract_balance == balance1 assert test_channel.balance == balance1 - amount1 assert test_channel.transferred_amount == amount1 assert test_channel.distributable == balance1 - amount1 assert test_channel.outstanding == 0 assert test_channel.locked == 0 assert test_channel.our_state.amount_locked == 0 assert test_channel.partner_state.amount_locked == 0 assert test_channel.get_next_nonce() == 2 secret = sha3('test_channel') hashlock = sha3(secret) amount2 = 10 fee = 0 expiration = block_number + settle_timeout - 5 identifier = 1 mediatedtransfer = test_channel.create_mediatedtransfer( address1, address2, fee, amount2, identifier, expiration, hashlock, ) mediatedtransfer.sign(privkey1, address1) test_channel.register_transfer( block_number, mediatedtransfer, ) assert test_channel.contract_balance == balance1 assert test_channel.balance == balance1 - amount1 assert test_channel.transferred_amount == amount1 assert test_channel.distributable == balance1 - amount1 - amount2 assert test_channel.outstanding == 0 assert test_channel.locked == amount2 assert test_channel.our_state.amount_locked == amount2 assert test_channel.partner_state.amount_locked == 0 assert test_channel.get_next_nonce() == 3 secret_message = test_channel.create_secret(identifier, secret) secret_message.sign(privkey1, address1) test_channel.register_transfer(block_number, secret_message) assert test_channel.contract_balance == balance1 assert test_channel.balance == balance1 - amount1 - amount2 assert test_channel.transferred_amount == amount1 + amount2 assert test_channel.distributable == balance1 - amount1 - amount2 assert test_channel.outstanding == 0 assert test_channel.locked == 0 assert test_channel.our_state.amount_locked == 0 assert test_channel.partner_state.amount_locked == 0 assert test_channel.get_next_nonce() == 4
def test_python_channel(): token_address = make_address() privkey1, address1 = make_privkey_address() address2 = make_address() balance1 = 70 balance2 = 110 reveal_timeout = 5 settle_timeout = 15 block_number = 10 our_state = ChannelEndState(address1, balance1, None, EMPTY_MERKLE_TREE) partner_state = ChannelEndState(address2, balance2, None, EMPTY_MERKLE_TREE) external_state = make_external_state() test_channel = Channel( our_state, partner_state, external_state, token_address, reveal_timeout, settle_timeout, ) assert test_channel.contract_balance == our_state.contract_balance assert test_channel.transferred_amount == our_state.transferred_amount assert test_channel.distributable == our_state.contract_balance assert test_channel.outstanding == our_state.amount_locked assert test_channel.outstanding == 0 assert test_channel.locked == partner_state.amount_locked assert test_channel.our_state.amount_locked == 0 assert test_channel.partner_state.amount_locked == 0 assert test_channel.get_next_nonce() == 1 with pytest.raises(ValueError): test_channel.create_directtransfer( -10, identifier=1, ) with pytest.raises(ValueError): test_channel.create_directtransfer( balance1 + 10, identifier=1, ) amount1 = 10 directtransfer = test_channel.create_directtransfer( amount1, identifier=1, ) directtransfer.sign(privkey1, address1) test_channel.register_transfer( block_number, directtransfer, ) assert test_channel.contract_balance == balance1 assert test_channel.balance == balance1 - amount1 assert test_channel.transferred_amount == amount1 assert test_channel.distributable == balance1 - amount1 assert test_channel.outstanding == 0 assert test_channel.locked == 0 assert test_channel.our_state.amount_locked == 0 assert test_channel.partner_state.amount_locked == 0 assert test_channel.get_next_nonce() == 2 secret = sha3(b'test_channel') hashlock = sha3(secret) amount2 = 10 fee = 0 expiration = block_number + settle_timeout - 5 identifier = 1 mediatedtransfer = test_channel.create_mediatedtransfer( address1, address2, fee, amount2, identifier, expiration, hashlock, ) mediatedtransfer.sign(privkey1, address1) test_channel.register_transfer( block_number, mediatedtransfer, ) assert test_channel.contract_balance == balance1 assert test_channel.balance == balance1 - amount1 assert test_channel.transferred_amount == amount1 assert test_channel.distributable == balance1 - amount1 - amount2 assert test_channel.outstanding == 0 assert test_channel.locked == amount2 assert test_channel.our_state.amount_locked == amount2 assert test_channel.partner_state.amount_locked == 0 assert test_channel.get_next_nonce() == 3 secret_message = test_channel.create_secret(identifier, secret) secret_message.sign(privkey1, address1) test_channel.register_transfer(block_number, secret_message) assert test_channel.contract_balance == balance1 assert test_channel.balance == balance1 - amount1 - amount2 assert test_channel.transferred_amount == amount1 + amount2 assert test_channel.distributable == balance1 - amount1 - amount2 assert test_channel.outstanding == 0 assert test_channel.locked == 0 assert test_channel.our_state.amount_locked == 0 assert test_channel.partner_state.amount_locked == 0 assert test_channel.get_next_nonce() == 4