def update_monitoring_service_from_balance_proof( raiden: 'RaidenService', chain_state: 'ChainState', new_balance_proof: BalanceProofSignedState, ) -> None: if raiden.config['services']['monitoring_enabled'] is False: return channel_state = views.get_channelstate_by_canonical_identifier( chain_state=chain_state, canonical_identifier=new_balance_proof.canonical_identifier, ) msg = ( f'Failed to update monitoring service due to inability to find ' f'channel: {new_balance_proof.channel_identifier} ' f'token_network_address: {pex(new_balance_proof.token_network_identifier)}.' ) assert channel_state, msg balance = channel.get_balance( sender=channel_state.our_state, receiver=channel_state.partner_state, ) if balance < MONITORING_MIN_CAPACITY: log.warn( f'Skipping update to Monitoring service. ' f'Available balance of {balance} is less than configured ' f'minimum capacity of {MONITORING_MIN_CAPACITY}', ) return rei_balance = raiden.user_deposit.effective_balance( raiden.address, 'latest') if rei_balance < MONITORING_REWARD: rdn_balance = to_rdn(rei_balance) rdn_reward = to_rdn(MONITORING_REWARD) log.warn( f'Skipping update to Monitoring service. ' f'Your deposit balance {rdn_balance} is less than ' f'the required monitoring service reward of {rdn_reward}', ) return log.info( 'Received new balance proof, creating message for Monitoring Service.', balance_proof=new_balance_proof, ) monitoring_message = RequestMonitoring.from_balance_proof_signed_state( new_balance_proof, MONITORING_REWARD, ) monitoring_message.sign(raiden.signer) raiden.transport.send_global( constants.MONITORING_BROADCASTING_ROOM, monitoring_message, )
def create_monitoring_request( self, balance_proof: BalanceProofSignedState, reward_amount: TokenAmount ) -> Optional[RequestMonitoring]: """ This method can be used to create a `RequestMonitoring` message. It will contain all data necessary for an external monitoring service to - send an updateNonClosingBalanceProof transaction to the TokenNetwork contract, for the `balance_proof` that we received from a channel partner. - claim the `reward_amount` from the UDC. """ # create RequestMonitoring message from the above + `reward_amount` monitor_request = RequestMonitoring.from_balance_proof_signed_state( balance_proof=balance_proof, reward_amount=reward_amount ) # sign RequestMonitoring and return monitor_request.sign(self.raiden.signer) return monitor_request
def handle_received_balance_proof( raiden: RaidenService, new_balance_proof_event: EventNewBalanceProofReceived, ): log.info( 'Received balance proof, creating message for Monitoring Service', evt=new_balance_proof_event, ) reward_amount = 0 # FIXME: default reward is 0, should come from elsewhere monitoring_message = RequestMonitoring.from_balance_proof_signed_state( new_balance_proof_event.balance_proof, reward_amount, ) monitoring_message.sign(raiden.signer) raiden.transport.send_global( MONITORING_BROADCASTING_ROOM, monitoring_message, )
def update_monitoring_service_from_balance_proof( raiden: RaidenService, new_balance_proof: BalanceProofSignedState, ): if raiden.config['services']['monitoring_enabled'] is False: return None log.info( 'Received new balance proof, creating message for Monitoring Service.', balance_proof=new_balance_proof, ) reward_amount = 0 # FIXME: default reward is 0, should come from elsewhere monitoring_message = RequestMonitoring.from_balance_proof_signed_state( new_balance_proof, reward_amount, ) monitoring_message.sign(raiden.signer) raiden.transport.send_global( constants.MONITORING_BROADCASTING_ROOM, monitoring_message, )
def test_request_monitoring(): partner_signer = LocalSigner(PARTNER_PRIVKEY) balance_proof = make_balance_proof(signer=partner_signer, amount=1) partner_signed_balance_proof = SignedBlindedBalanceProof.from_balance_proof_signed_state( balance_proof, ) request_monitoring = RequestMonitoring( onchain_balance_proof=partner_signed_balance_proof, reward_amount=55, ) assert request_monitoring with pytest.raises(ValueError): request_monitoring.to_dict() request_monitoring.sign(signer) as_dict = request_monitoring.to_dict() assert RequestMonitoring.from_dict(as_dict) == request_monitoring packed = request_monitoring.pack(request_monitoring.packed()) assert RequestMonitoring.unpack(packed) == request_monitoring # RequestMonitoring can be created directly from BalanceProofSignedState direct_created = RequestMonitoring.from_balance_proof_signed_state( balance_proof, reward_amount=55, ) with pytest.raises(ValueError): # equality test uses `validated` packed format assert direct_created == request_monitoring direct_created.sign(signer) # Instances created from same balance proof are equal assert direct_created == request_monitoring other_balance_proof = make_balance_proof(signer=partner_signer, amount=2) other_instance = RequestMonitoring.from_balance_proof_signed_state( other_balance_proof, reward_amount=55, ) other_instance.sign(signer) # different balance proof ==> non-equality assert other_instance != request_monitoring # test signature verification reward_proof_data = pack_reward_proof( request_monitoring.balance_proof.channel_identifier, request_monitoring.reward_amount, request_monitoring.balance_proof.token_network_address, request_monitoring.balance_proof.chain_id, request_monitoring.balance_proof.nonce, ) assert recover(reward_proof_data, request_monitoring.reward_proof_signature) == ADDRESS blinded_data = pack_balance_proof_update( nonce=request_monitoring.balance_proof.nonce, balance_hash=request_monitoring.balance_proof.balance_hash, additional_hash=request_monitoring.balance_proof.additional_hash, canonical_identifier=CanonicalIdentifier( chain_identifier=request_monitoring.balance_proof.chain_id, token_network_address=request_monitoring.balance_proof. token_network_address, channel_identifier=request_monitoring.balance_proof. channel_identifier, ), partner_signature=request_monitoring.balance_proof.signature, ) assert recover(blinded_data, request_monitoring.non_closing_signature) == ADDRESS balance_proof_data = pack_balance_proof( nonce=request_monitoring.balance_proof.nonce, balance_hash=request_monitoring.balance_proof.balance_hash, additional_hash=request_monitoring.balance_proof.additional_hash, canonical_identifier=CanonicalIdentifier( chain_identifier=request_monitoring.balance_proof.chain_id, token_network_address=request_monitoring.balance_proof. token_network_address, channel_identifier=request_monitoring.balance_proof. channel_identifier, ), ) assert recover( balance_proof_data, request_monitoring.balance_proof.signature, ) == PARTNER_ADDRESS assert request_monitoring.verify_request_monitoring( PARTNER_ADDRESS, ADDRESS)