def maybe_add_tokennetwork( chain_state: ChainState, token_network_registry_address: TokenNetworkRegistryAddress, token_network_state: TokenNetworkState, ) -> None: token_network_address = token_network_state.address token_address = token_network_state.token_address token_network_registry_state, token_network_state_previous = views.get_networks( chain_state, token_network_registry_address, token_address ) if token_network_registry_state is None: token_network_registry_state = TokenNetworkRegistryState( token_network_registry_address, [token_network_state] ) ids_to_payments = chain_state.identifiers_to_tokennetworkregistries ids_to_payments[token_network_registry_address] = token_network_registry_state if token_network_state_previous is None: token_network_registry_state.add_token_network(token_network_state) mapping = chain_state.tokennetworkaddresses_to_tokennetworkregistryaddresses mapping[token_network_address] = token_network_registry_address
def test_get_networks(chain_state, token_network_address): orig_chain_state = deepcopy(chain_state) token_address = factories.make_address() token_network_registry_empty = TokenNetworkRegistryState( address=factories.make_address(), token_network_list=[]) chain_state.identifiers_to_tokennetworkregistries[ token_network_registry_empty.address] = token_network_registry_empty assert get_networks( chain_state=chain_state, token_network_registry_address=token_network_registry_empty.address, token_address=token_address, ) == (token_network_registry_empty, None) chain_state = orig_chain_state token_network = TokenNetworkState( address=token_network_address, token_address=token_address, network_graph=TokenNetworkGraphState( token_network_address=token_network_address), ) token_network_registry = TokenNetworkRegistryState( address=factories.make_address(), token_network_list=[token_network]) assert get_networks( chain_state=chain_state, token_network_registry_address=token_network_registry.address, token_address=token_address, ) == (None, None) chain_state.identifiers_to_tokennetworkregistries[ token_network_registry.address] = token_network_registry assert get_networks( chain_state=chain_state, token_network_registry_address=token_network_registry.address, token_address=token_address, ) == (token_network_registry, token_network)
def initialize_all(self, block_number, random, random_seed): self.random_seed = random_seed self.block_number = block_number self.block_hash = factories.make_block_hash() self.random = random self.token_network_address = factories.UNIT_TOKEN_NETWORK_ADDRESS self.token_id = factories.UNIT_TOKEN_ADDRESS self.token_network_state = TokenNetworkState( address=self.token_network_address, token_address=self.token_id, network_graph=TokenNetworkGraphState(self.token_network_address), ) self.token_network_registry_address = factories.make_token_network_registry_address() self.token_network_registry_state = TokenNetworkRegistryState( self.token_network_registry_address, [self.token_network_state] ) channels = [] for _ in range(self.number_of_clients): private_key, address = factories.make_privkey_address() self.address_to_privkey[address] = private_key chain_state = ChainState( pseudo_random_generator=self.random, block_number=self.block_number, block_hash=self.block_hash, our_address=address, chain_id=factories.UNIT_CHAIN_ID, ) chain_state.identifiers_to_tokennetworkregistries[ self.token_network_registry_address ] = self.token_network_registry_state chain_state.tokennetworkaddresses_to_tokennetworkregistryaddresses[ self.token_network_address ] = self.token_network_registry_address self.address_to_client[address] = Client(chain_state=chain_state) channels.extend( self.new_channel_with_transaction(client_address=address) for _ in range(self.initial_number_of_channels) ) return multiple(*channels)
def test_handle_new_token_network_registry(chain_state, token_network_address): token_address = factories.make_address() token_network = TokenNetworkState( address=token_network_address, token_address=token_address, ) token_network_registry = TokenNetworkRegistryState( address=factories.make_address(), token_network_list=[token_network]) state_change = ContractReceiveNewTokenNetworkRegistry( transaction_hash=factories.make_transaction_hash(), token_network_registry=token_network_registry, block_hash=make_block_hash(), block_number=1, ) assert token_network_registry.address not in chain_state.identifiers_to_tokennetworkregistries transition_result = handle_contract_receive_new_token_network_registry( chain_state, state_change) assert transition_result.new_state == chain_state msg = "handle_new_token_network_registry did not add to chain_state mapping" assert token_network_registry.address in chain_state.identifiers_to_tokennetworkregistries, msg
def token_network_registry_state(chain_state, token_network_registry_address): token_network_registry = TokenNetworkRegistryState( token_network_registry_address, []) chain_state.identifiers_to_tokennetworkregistries[ token_network_registry_address] = token_network_registry return token_network_registry
def test_subdispatch_by_canonical_id(chain_state): our_model, _ = create_model(balance=10, num_pending_locks=1) partner_model, _ = create_model(balance=0, num_pending_locks=0) channel_state = create_channel_from_models( our_model, partner_model, factories.make_privatekey_bin() ) canonical_identifier = channel_state.canonical_identifier token_network = TokenNetworkState( address=canonical_identifier.token_network_address, token_address=factories.make_address(), network_graph=TokenNetworkGraphState( token_network_address=channel_state.token_network_address ), ) token_network.partneraddresses_to_channelidentifiers[ partner_model.participant_address ] = canonical_identifier.channel_identifier token_network.channelidentifiers_to_channels[ canonical_identifier.channel_identifier ] = channel_state token_network_registry = TokenNetworkRegistryState( address=factories.make_address(), token_network_list=[token_network] ) chain_state.identifiers_to_tokennetworkregistries[ token_network_registry.address ] = token_network_registry chain_state.tokennetworkaddresses_to_tokennetworkregistryaddresses[ canonical_identifier.token_network_address ] = token_network_registry.address # dispatching a Block will be ignored previous_state = deepcopy(chain_state) state_change = Block( block_number=chain_state.block_number, gas_limit=GAS_LIMIT, block_hash=chain_state.block_hash, ) transition_result = subdispatch_by_canonical_id( chain_state=chain_state, canonical_identifier=canonical_identifier, state_change=state_change, ) assert transition_result.new_state == previous_state assert transition_result.events == [] state_change = ActionChannelClose(canonical_identifier=canonical_identifier) # dispatching for an unknown canonical_identifier will not emit events transition_result = subdispatch_by_canonical_id( chain_state=chain_state, canonical_identifier=CanonicalIdentifier( chain_identifier=chain_state.chain_id, token_network_address=factories.make_address(), channel_identifier=factories.make_channel_identifier(), ), state_change=state_change, ) assert not transition_result.events, transition_result assert get_status(channel_state) == ChannelState.STATE_OPENED transition_result = subdispatch_by_canonical_id( chain_state=chain_state, canonical_identifier=canonical_identifier, state_change=state_change, ) assert get_status(channel_state) == ChannelState.STATE_CLOSING assert transition_result.new_state == chain_state, transition_result
def test_detect_balance_proof_change(): prng = random.Random() block_hash = make_block_hash() our_address = make_address() empty_chain = ChainState( pseudo_random_generator=prng, block_number=1, block_hash=block_hash, our_address=our_address, chain_id=3, ) assert empty(detect_balance_proof_change(empty_chain, empty_chain)), MSG_NO_CHANGE assert empty( detect_balance_proof_change(empty_chain, deepcopy(empty_chain))), MSG_NO_CHANGE token_network_registry_address = make_address() chain_with_registry_no_bp = deepcopy(empty_chain) chain_with_registry_no_bp.identifiers_to_tokennetworkregistries[ token_network_registry_address] = TokenNetworkRegistryState( token_network_registry_address, []) assert empty( detect_balance_proof_change(empty_chain, chain_with_registry_no_bp)), MSG_NO_CHANGE assert empty( detect_balance_proof_change( chain_with_registry_no_bp, deepcopy(chain_with_registry_no_bp))), MSG_NO_CHANGE token_network_address = make_address() token_address = make_address() chain_with_token_network_no_bp = deepcopy(chain_with_registry_no_bp) chain_with_token_network_no_bp.identifiers_to_tokennetworkregistries[ token_network_registry_address].tokennetworkaddresses_to_tokennetworks[ token_network_address] = TokenNetworkState( address=token_network_address, token_address=token_address, network_graph=TokenNetworkGraphState(token_network_address), ) assert empty( detect_balance_proof_change( empty_chain, chain_with_token_network_no_bp)), MSG_NO_CHANGE assert empty( detect_balance_proof_change( chain_with_registry_no_bp, chain_with_token_network_no_bp)), MSG_NO_CHANGE assert empty( detect_balance_proof_change( chain_with_token_network_no_bp, deepcopy(chain_with_token_network_no_bp))), MSG_NO_CHANGE partner_address = make_address() canonical_identifier = make_canonical_identifier() channel_no_bp = NettingChannelState( canonical_identifier=canonical_identifier, token_address=token_address, token_network_registry_address=token_network_registry_address, reveal_timeout=1, settle_timeout=2, our_state=NettingChannelEndState(address=our_address, contract_balance=1), partner_state=NettingChannelEndState(address=partner_address, contract_balance=0), open_transaction=TransactionExecutionStatus(result="success"), settle_transaction=None, update_transaction=None, close_transaction=None, fee_schedule=FeeScheduleState(), ) chain_with_channel_no_bp = deepcopy(chain_with_token_network_no_bp) chain_with_token_network_no_bp.identifiers_to_tokennetworkregistries[ token_network_registry_address].tokennetworkaddresses_to_tokennetworks[ token_network_address].channelidentifiers_to_channels[ canonical_identifier.channel_identifier] = channel_no_bp assert empty( detect_balance_proof_change(empty_chain, chain_with_channel_no_bp)), MSG_NO_CHANGE assert empty( detect_balance_proof_change(chain_with_registry_no_bp, chain_with_channel_no_bp)), MSG_NO_CHANGE assert empty( detect_balance_proof_change(chain_with_token_network_no_bp, chain_with_channel_no_bp)), MSG_NO_CHANGE assert empty( detect_balance_proof_change( chain_with_channel_no_bp, deepcopy(chain_with_channel_no_bp))), MSG_NO_CHANGE channel_with_sent_bp = deepcopy(channel_no_bp) channel_with_sent_bp.our_state.balance_proof = create( BalanceProofUnsignedState) chain_with_sent_bp = deepcopy(chain_with_token_network_no_bp) chain_with_sent_bp.identifiers_to_tokennetworkregistries[ token_network_registry_address].tokennetworkaddresses_to_tokennetworks[ token_network_address].channelidentifiers_to_channels[ canonical_identifier.channel_identifier] = channel_with_sent_bp assert not empty( detect_balance_proof_change( empty_chain, chain_with_sent_bp)), MSG_BALANCE_PROOF_SHOULD_BE_DETECTED assert not empty( detect_balance_proof_change( chain_with_registry_no_bp, chain_with_sent_bp)), MSG_BALANCE_PROOF_SHOULD_BE_DETECTED assert not empty( detect_balance_proof_change( chain_with_token_network_no_bp, chain_with_sent_bp)), MSG_BALANCE_PROOF_SHOULD_BE_DETECTED assert not empty( detect_balance_proof_change( chain_with_channel_no_bp, chain_with_sent_bp)), MSG_BALANCE_PROOF_SHOULD_BE_DETECTED assert empty( detect_balance_proof_change( chain_with_sent_bp, deepcopy(chain_with_sent_bp))), MSG_NO_CHANGE channel_with_received_bp = deepcopy(channel_no_bp) channel_with_received_bp.partner_state.balance_proof = create( BalanceProofUnsignedState) chain_with_received_bp = deepcopy(chain_with_token_network_no_bp) chain_with_received_bp.identifiers_to_tokennetworkregistries[ token_network_registry_address].tokennetworkaddresses_to_tokennetworks[ token_network_address].channelidentifiers_to_channels[ canonical_identifier.channel_identifier] = channel_with_sent_bp # asserting with `channel_with_received_bp` and `channel_with_sent_bp` # doesn't make sense, because one of the balance proofs would have to # disappear (which is a bug) assert not empty( detect_balance_proof_change( empty_chain, chain_with_received_bp)), MSG_BALANCE_PROOF_SHOULD_BE_DETECTED assert not empty( detect_balance_proof_change( chain_with_registry_no_bp, chain_with_received_bp)), MSG_BALANCE_PROOF_SHOULD_BE_DETECTED assert not empty( detect_balance_proof_change( chain_with_token_network_no_bp, chain_with_received_bp)), MSG_BALANCE_PROOF_SHOULD_BE_DETECTED assert not empty( detect_balance_proof_change( chain_with_channel_no_bp, chain_with_received_bp)), MSG_BALANCE_PROOF_SHOULD_BE_DETECTED assert empty( detect_balance_proof_change( chain_with_received_bp, deepcopy(chain_with_received_bp))), MSG_NO_CHANGE chain_with_sent_and_received_bp = deepcopy(chain_with_token_network_no_bp) ta_to_tn = chain_with_sent_and_received_bp.identifiers_to_tokennetworkregistries channel_with_sent_and_recived_bp = ( ta_to_tn[token_network_registry_address]. tokennetworkaddresses_to_tokennetworks[token_network_address]. channelidentifiers_to_channels[canonical_identifier.channel_identifier] ) channel_with_sent_and_recived_bp.partner_state.balance_proof = deepcopy( channel_with_received_bp.partner_state.balance_proof) channel_with_sent_and_recived_bp.our_state.balance_proof = deepcopy( channel_with_received_bp.our_state.balance_proof) assert not empty( detect_balance_proof_change(empty_chain, chain_with_sent_and_received_bp) ), MSG_BALANCE_PROOF_SHOULD_BE_DETECTED assert not empty( detect_balance_proof_change(chain_with_registry_no_bp, chain_with_sent_and_received_bp) ), MSG_BALANCE_PROOF_SHOULD_BE_DETECTED assert not empty( detect_balance_proof_change(chain_with_token_network_no_bp, chain_with_sent_and_received_bp) ), MSG_BALANCE_PROOF_SHOULD_BE_DETECTED assert not empty( detect_balance_proof_change(chain_with_channel_no_bp, chain_with_sent_and_received_bp) ), MSG_BALANCE_PROOF_SHOULD_BE_DETECTED assert not empty( detect_balance_proof_change(chain_with_received_bp, chain_with_sent_and_received_bp) ), MSG_BALANCE_PROOF_SHOULD_BE_DETECTED assert not empty( detect_balance_proof_change(chain_with_sent_bp, chain_with_sent_and_received_bp) ), MSG_BALANCE_PROOF_SHOULD_BE_DETECTED assert empty( detect_balance_proof_change( chain_with_sent_and_received_bp, deepcopy(chain_with_sent_and_received_bp))), MSG_NO_CHANGE