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, BalanceProof(None), ) partner_state = ChannelEndState( address2, balance2, BalanceProof(None), ) 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 restore_channel(self, serialized_channel): token_address = serialized_channel.token_address netting_channel = self.chain.netting_channel( serialized_channel.channel_address, ) # restoring balances from the blockchain since the serialized # value could be falling behind. channel_details = netting_channel.detail() # our_address is checked by detail assert channel_details['partner_address'] == serialized_channel.partner_address our_state = ChannelEndState( channel_details['our_address'], channel_details['our_balance'], BalanceProof(serialized_channel.our_balance_proof), ) partner_state = ChannelEndState( channel_details['partner_address'], channel_details['partner_balance'], BalanceProof(serialized_channel.partner_balance_proof), ) def register_channel_for_hashlock(channel, hashlock): self.register_channel_for_hashlock( token_address, channel, hashlock, ) external_state = ChannelExternalState( register_channel_for_hashlock, netting_channel, ) details = ChannelDetails( serialized_channel.channel_address, our_state, partner_state, external_state, serialized_channel.reveal_timeout, channel_details['settle_timeout'], ) graph = self.token_to_channelgraph[token_address] graph.add_channel(details) channel = graph.address_to_channel.get( serialized_channel.channel_address, ) channel.our_state.balance_proof = serialized_channel.our_balance_proof channel.partner_state.balance_proof = serialized_channel.partner_balance_proof
def test_channel_to_api_dict(): channel_address = b'\x00' * 20 token_address = b'\x01' * 20 our_address = b'\x02' * 20 partner_address = b'\x03' * 20 reveal_timeout = 10 settle_timeout = 50 opened_block = 900 our_balance = 13 partner_balance = 21 our_state = ChannelEndState( our_address, our_balance, BalanceProof(None), ) partner_state = ChannelEndState( partner_address, partner_balance, BalanceProof(None), ) # mock external state to provide the channel address class NettingChannel(object): address = channel_address class ExternalState(object): def __init__(self, opened_block): self.netting_channel = NettingChannel() self.settled_block = 0 self.closed_block = 0 self.opened_block = opened_block channel = Channel( our_state, partner_state, ExternalState(opened_block), token_address, reveal_timeout, settle_timeout, ) result = channel_to_api_dict(channel) expected_result = { 'channel_address': channel_address, 'token_address': token_address, 'partner_address': partner_address, 'settle_timeout': settle_timeout, 'reveal_timeout': reveal_timeout, 'balance': our_balance, 'state': CHANNEL_STATE_OPENED } assert result == expected_result
def test_channel_close_called_only_once(): class MockCheckCallsToClose(): def __init__(self): self.address = 'mockcheckcallstoclosemockcheckcallstoclo' self.close_calls = 0 def opened(self): return 1 def closed(self): return 0 def settled(self): return 0 def close(self, nonce, transferred_amount, locksroot, extra_hash, signature): self.close_calls += 1 netting_channel = NettingChannelMock() 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, BalanceProof(None)) partner_state = ChannelEndState(address2, balance2, BalanceProof(None)) channel_for_hashlock = list() netting_channel = MockCheckCallsToClose() external_state = ChannelExternalState( lambda *args: channel_for_hashlock.append(args), netting_channel, ) test_channel = Channel( our_state, partner_state, external_state, token_address, reveal_timeout, settle_timeout, ) test_channel.external_state.close(None) test_channel.external_state.close(None) assert netting_channel.close_calls == 1
def channel_from_nettingcontract(our_key, netting_contract, external_state, reveal_timeout): """ Create a `channel.Channel` for the `netting_contract`. Use this to make sure that both implementations (the smart contract and the python code) work in tandem. """ our_address = privatekey_to_address(our_key) token_address_hex = netting_contract.tokenAddress(sender=our_key) settle_timeout = netting_contract.settleTimeout(sender=our_key) address_balance = netting_contract.addressAndBalance(sender=our_key) address1_hex, balance1, address2_hex, balance2 = address_balance token_address = decode_hex(token_address_hex) address1 = decode_hex(address1_hex) address2 = decode_hex(address2_hex) if our_address == address1: our_balance = balance1 partner_address = address2 partner_balance = balance2 else: our_balance = balance2 partner_address = address1 partner_balance = balance1 our_state = ChannelEndState( our_address, our_balance, BalanceProof(None), ) partner_state = ChannelEndState( partner_address, partner_balance, BalanceProof(None), ) channel = Channel( our_state, partner_state, external_state, token_address, reveal_timeout, settle_timeout, ) return channel
def test_invalid_timeouts(): token_address = make_address() reveal_timeout = 5 settle_timeout = 15 address1 = make_address() address2 = make_address() balance1 = 10 balance2 = 10 our_state = ChannelEndState(address1, balance1, BalanceProof(None)) partner_state = ChannelEndState(address2, balance2, BalanceProof(None)) external_state = make_external_state() # do not allow a reveal timeout larger than the settle timeout with pytest.raises(ValueError): large_reveal_timeout = 50 small_settle_timeout = 49 Channel( our_state, partner_state, external_state, token_address, large_reveal_timeout, small_settle_timeout, ) for invalid_value in (-1, 0, 1.1, 1.0, 'a', [], {}): with pytest.raises(ValueError): Channel( our_state, partner_state, external_state, token_address, invalid_value, settle_timeout, ) with pytest.raises(ValueError): Channel( our_state, partner_state, external_state, token_address, reveal_timeout, invalid_value, )
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, BalanceProof(None)) partner_state = ChannelEndState(address2, balance2, BalanceProof(None)) 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 get_channel_details(self, token_address, netting_channel): channel_details = netting_channel.detail() our_state = ChannelEndState( channel_details['our_address'], channel_details['our_balance'], BalanceProof(None), ) partner_state = ChannelEndState( channel_details['partner_address'], channel_details['partner_balance'], BalanceProof(None), ) def register_channel_for_hashlock(channel, hashlock): self.register_channel_for_hashlock( token_address, channel, hashlock, ) channel_address = netting_channel.address reveal_timeout = self.config['reveal_timeout'] settle_timeout = channel_details['settle_timeout'] external_state = ChannelExternalState( register_channel_for_hashlock, netting_channel, ) channel_detail = ChannelDetails( channel_address, our_state, partner_state, external_state, reveal_timeout, settle_timeout, ) return channel_detail
def make_channel(self, token_address=make_address(), partner_address=make_address(), reveal_timeout=20, settle_timeout=800, balance=0, block_number=1): our_address = make_address() our_balance = balance partner_balance = balance our_state = ChannelEndState( our_address, our_balance, BalanceProof(None), ) partner_state = ChannelEndState( partner_address, partner_balance, BalanceProof(None), ) channel_for_hashlock = list() netting_channel = NettingChannelMock(make_address()) external_state = ChannelExternalState( lambda *args: channel_for_hashlock.append(args), netting_channel, ) self.tokens.add(token_address) return Channel( our_state, partner_state, external_state, token_address, reveal_timeout, settle_timeout, )
def test_end_state(): token_address = make_address() privkey1, address1 = make_privkey_address() address2 = make_address() channel_address = make_address() balance1 = 70 balance2 = 110 lock_secret = sha3('test_end_state') lock_amount = 30 lock_expiration = 10 lock_hashlock = sha3(lock_secret) state1 = ChannelEndState(address1, balance1, BalanceProof(None)) state2 = ChannelEndState(address2, balance2, BalanceProof(None)) assert state1.contract_balance == balance1 assert state2.contract_balance == balance2 assert state1.balance(state2) == balance1 assert state2.balance(state1) == balance2 assert state1.distributable(state2) == balance1 assert state2.distributable(state1) == balance2 assert state1.locked() == 0 assert state2.locked() == 0 assert state1.balance_proof.is_pending(lock_hashlock) is False assert state2.balance_proof.is_pending(lock_hashlock) is False assert state1.balance_proof.merkleroot_for_unclaimed() == EMPTY_MERKLE_ROOT assert state2.balance_proof.merkleroot_for_unclaimed() == EMPTY_MERKLE_ROOT assert state1.nonce is None assert state2.nonce is None lock = Lock( lock_amount, lock_expiration, lock_hashlock, ) lock_hash = sha3(lock.as_bytes) transferred_amount = 0 locksroot = state2.compute_merkleroot_with(lock) locked_transfer = LockedTransfer( 1, nonce=1, token=token_address, channel=channel_address, transferred_amount=transferred_amount, recipient=state2.address, locksroot=locksroot, lock=lock, ) transfer_target = make_address() transfer_initiator = make_address() fee = 0 mediated_transfer = locked_transfer.to_mediatedtransfer( transfer_target, transfer_initiator, fee, ) mediated_transfer.sign(privkey1, address1) state1.register_locked_transfer(mediated_transfer) assert state1.contract_balance == balance1 assert state2.contract_balance == balance2 assert state1.balance(state2) == balance1 assert state2.balance(state1) == balance2 assert state1.distributable(state2) == balance1 - lock_amount assert state2.distributable(state1) == balance2 assert state1.locked() == lock_amount assert state2.locked() == 0 assert state1.balance_proof.is_pending(lock_hashlock) is True assert state2.balance_proof.is_pending(lock_hashlock) is False assert state1.balance_proof.merkleroot_for_unclaimed() == lock_hash assert state2.balance_proof.merkleroot_for_unclaimed() == EMPTY_MERKLE_ROOT assert state1.nonce is 1 assert state2.nonce is None with pytest.raises(ValueError): state1.update_contract_balance(balance1 - 10) state1.update_contract_balance(balance1 + 10) assert state1.contract_balance == balance1 + 10 assert state2.contract_balance == balance2 assert state1.balance(state2) == balance1 + 10 assert state2.balance(state1) == balance2 assert state1.distributable(state2) == balance1 - lock_amount + 10 assert state2.distributable(state1) == balance2 assert state1.locked() == lock_amount assert state2.locked() == 0 assert state1.balance_proof.is_pending(lock_hashlock) is True assert state2.balance_proof.is_pending(lock_hashlock) is False assert state1.balance_proof.merkleroot_for_unclaimed() == lock_hash assert state2.balance_proof.merkleroot_for_unclaimed() == EMPTY_MERKLE_ROOT assert state1.nonce is 1 assert state2.nonce is None # registering the secret should not change the locked amount state1.register_secret(lock_secret) assert state1.contract_balance == balance1 + 10 assert state2.contract_balance == balance2 assert state1.balance(state2) == balance1 + 10 assert state2.balance(state1) == balance2 assert state1.distributable(state2) == balance1 - lock_amount + 10 assert state2.distributable(state1) == balance2 assert state1.locked() == lock_amount assert state2.locked() == 0 assert state1.balance_proof.is_pending(lock_hashlock) is False assert state2.balance_proof.is_pending(lock_hashlock) is False assert state1.balance_proof.merkleroot_for_unclaimed() == lock_hash assert state2.balance_proof.merkleroot_for_unclaimed() == EMPTY_MERKLE_ROOT assert state1.nonce is 1 assert state2.nonce is None secret_message = Secret( identifier=1, nonce=2, channel=channel_address, transferred_amount=transferred_amount + lock_amount, locksroot=EMPTY_MERKLE_ROOT, secret=lock_secret, ) secret_message.sign(privkey1, address1) state1.register_secretmessage(secret_message) assert state1.contract_balance == balance1 + 10 assert state2.contract_balance == balance2 assert state1.balance(state2) == balance1 + 10 - lock_amount assert state2.balance(state1) == balance2 + lock_amount assert state1.distributable(state2) == balance1 + 10 - lock_amount assert state2.distributable(state1) == balance2 + lock_amount assert state1.locked() == 0 assert state2.locked() == 0 assert state1.balance_proof.is_pending(lock_hashlock) is False assert state2.balance_proof.is_pending(lock_hashlock) is False assert state1.balance_proof.merkleroot_for_unclaimed() == EMPTY_MERKLE_ROOT assert state2.balance_proof.merkleroot_for_unclaimed() == EMPTY_MERKLE_ROOT assert state1.nonce is 2 assert state2.nonce is None
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, BalanceProof(None)) partner_state = ChannelEndState(address2, balance2, BalanceProof(None)) 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.balance == our_state.balance(partner_state) assert test_channel.transferred_amount == our_state.transferred_amount assert test_channel.distributable == our_state.distributable(partner_state) assert test_channel.outstanding == our_state.locked() assert test_channel.outstanding == 0 assert test_channel.locked == partner_state.locked() assert test_channel.our_state.locked() == 0 assert test_channel.partner_state.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.locked() == 0 assert test_channel.partner_state.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.locked() == amount2 assert test_channel.partner_state.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.locked() == 0 assert test_channel.partner_state.locked() == 0 assert test_channel.get_next_nonce() == 4
def test_receiver_cannot_spend_locked_amount(): token_address = make_address() privkey1, address1 = make_privkey_address() privkey2, address2 = make_privkey_address() balance1 = 33 balance2 = 11 reveal_timeout = 7 settle_timeout = 21 block_number = 7 our_state = ChannelEndState(address1, balance1, BalanceProof(None)) partner_state = ChannelEndState(address2, balance2, BalanceProof(None)) external_state = make_external_state() test_channel = Channel( our_state, partner_state, external_state, token_address, reveal_timeout, settle_timeout, ) amount1 = balance2 expiration = block_number + settle_timeout receive_mediated_transfer0 = test_channel.create_mediatedtransfer( address1, address2, fee=0, amount=amount1, identifier=1, expiration=expiration, hashlock=sha3('test_locked_amount_cannot_be_spent'), ) receive_mediated_transfer0.sign(privkey2, address2) test_channel.register_transfer( block_number, receive_mediated_transfer0, ) # trying to send one unit of the locked token amount2 = balance1 + 1 lock2 = Lock( amount=amount2, expiration=expiration, hashlock=sha3('test_locked_amount_cannot_be_spent2'), ) locksroot2 = Merkletree([sha3(lock2.as_bytes)]).merkleroot send_mediated_transfer0 = MediatedTransfer( identifier=1, nonce=1, token=token_address, channel=test_channel.channel_address, transferred_amount=0, recipient=address2, locksroot=locksroot2, lock=lock2, target=address2, initiator=address1, fee=0, ) send_mediated_transfer0.sign(privkey1, address1) # address1 balance is all locked with pytest.raises(InsufficientBalance): test_channel.register_transfer( block_number, send_mediated_transfer0, )
def test_sender_cannot_overspend(): 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, BalanceProof(None)) partner_state = ChannelEndState(address2, balance2, BalanceProof(None)) external_state = make_external_state() test_channel = Channel( our_state, partner_state, external_state, token_address, reveal_timeout, settle_timeout, ) amount = balance1 expiration = block_number + settle_timeout sent_mediated_transfer0 = test_channel.create_mediatedtransfer( address1, address2, fee=0, amount=amount, identifier=1, expiration=expiration, hashlock=sha3('test_locked_amount_cannot_be_spent'), ) sent_mediated_transfer0.sign(privkey1, address1) test_channel.register_transfer( block_number, sent_mediated_transfer0, ) lock2 = Lock( amount=amount, expiration=expiration, hashlock=sha3('test_locked_amount_cannot_be_spent2'), ) locksroot2 = Merkletree([ sha3(sent_mediated_transfer0.lock.as_bytes), sha3(lock2.as_bytes), ]).merkleroot sent_mediated_transfer1 = MediatedTransfer( identifier=2, nonce=sent_mediated_transfer0.nonce + 1, token=token_address, channel=test_channel.channel_address, transferred_amount=0, recipient=address2, locksroot=locksroot2, lock=lock2, target=address2, initiator=address1, fee=0, ) sent_mediated_transfer1.sign(privkey1, address1) # address1 balance is all locked with pytest.raises(InsufficientBalance): test_channel.register_transfer( block_number, sent_mediated_transfer1, )