def test_channel_withdraw_must_not_change_merkletree(): our_model1, _ = create_model(70) partner_model1, privkey2 = create_model(100) channel_state = create_channel_from_models(our_model1, partner_model1) payment_network_identifier = factories.make_address() lock_amount = 10 lock_expiration = 100 lock_secret = sha3(b'test_channelstate_lockedtransfer_overspent') lock_secrethash = sha3(lock_secret) lock = HashTimeLockState( lock_amount, lock_expiration, lock_secrethash, ) nonce = 1 transferred_amount = 0 receive_lockedtransfer = make_receive_transfer_mediated( channel_state, privkey2, nonce, transferred_amount, lock, ) is_valid, _, msg = channel.handle_receive_lockedtransfer( channel_state, receive_lockedtransfer, ) assert is_valid, msg assert merkleroot(channel_state.partner_state.merkletree) == lock.lockhash assert channel.is_lock_pending(channel_state.partner_state, lock.secrethash) closed_block_number = lock_expiration - channel_state.reveal_timeout - 1 state_change = ContractReceiveChannelClosed( payment_network_identifier, channel_state.token_address, channel_state.identifier, partner_model1.participant_address, closed_block_number, ) iteration = channel.handle_channel_closed(channel_state, state_change) new_channel = iteration.new_state withdraw = ContractReceiveChannelWithdraw( payment_network_identifier, channel_state.token_address, channel_state.identifier, lock_secret, channel_state.our_state.address, ) iteration = channel.handle_channel_withdraw(new_channel, withdraw) new_channel = iteration.new_state assert merkleroot(new_channel.partner_state.merkletree) == lock.lockhash assert not channel.is_lock_pending(new_channel.partner_state, lock.secrethash)
def test_write_read_log(): wal = new_wal() block_number = 1337 block = Block(block_number) contract_receive_withdraw = ContractReceiveChannelWithdraw( factories.make_address(), factories.make_address(), factories.ADDR, factories.UNIT_SECRET, factories.HOP1 ) state_changes1 = wal.storage.get_statechanges_by_identifier( from_identifier=0, to_identifier='latest', ) count1 = len(state_changes1) wal.log_and_dispatch(block, block_number) state_changes2 = wal.storage.get_statechanges_by_identifier( from_identifier=0, to_identifier='latest', ) count2 = len(state_changes2) assert count1 + 1 == count2 wal.log_and_dispatch(contract_receive_withdraw, block_number) state_changes3 = wal.storage.get_statechanges_by_identifier( from_identifier=0, to_identifier='latest', ) count3 = len(state_changes3) assert count2 + 1 == count3 result1, result2 = state_changes3[-2:] assert isinstance(result1, Block) assert result1.block_number == block_number assert isinstance(result2, ContractReceiveChannelWithdraw) assert result2.channel_identifier == factories.ADDR assert result2.secret == factories.UNIT_SECRET assert result2.receiver == factories.HOP1 # Make sure state snapshot can only go for corresponding state change ids with pytest.raises(sqlite3.IntegrityError): wal.storage.write_state_snapshot(34, 'AAAA') # Make sure we can only have a single state snapshot assert wal.storage.get_state_snapshot() is None wal.storage.write_state_snapshot(1, 'AAAA') assert wal.storage.get_state_snapshot() == (1, 'AAAA') wal.storage.write_state_snapshot(2, 'BBBB') assert wal.storage.get_state_snapshot() == (2, 'BBBB')
def handle_channel_withdraw(raiden, event): channel_identifier = event.originating_contract data = event.event_data payment_network_identifier = raiden.default_registry.address channel_state = views.search_for_channel( views.state_from_raiden(raiden), payment_network_identifier, channel_identifier, ) if channel_state: withdrawn_state_change = ContractReceiveChannelWithdraw( payment_network_identifier, channel_state.token_address, channel_identifier, data['secret'], data['receiver_address'], ) raiden.handle_state_change(withdrawn_state_change)
def contractreceivechannelwithdraw_from_event( event: DecodedEvent, fee_config: MediationFeeConfig ) -> ContractReceiveChannelWithdraw: data = event.event_data args = data["args"] channel_identifier = args["channel_identifier"] participant = args["participant"] total_withdraw = args["total_withdraw"] return ContractReceiveChannelWithdraw( canonical_identifier=CanonicalIdentifier( chain_identifier=event.chain_id, token_network_address=TokenNetworkAddress(event.originating_contract), channel_identifier=channel_identifier, ), total_withdraw=total_withdraw, participant=participant, transaction_hash=event.transaction_hash, block_number=event.block_number, block_hash=event.block_hash, fee_config=fee_config, )