Skip to content

Commit

Permalink
Add API method for creating RequestMonitoring messages
Browse files Browse the repository at this point in the history
This adds a **python** API endpoint

        create_monitoring_request(
            balance_proof,
            reward_amount,
        )

that will create a signed `RequestMonitoring` message from it.

This implements the first bullet point of #1432

Unit tests are missing still, but an integration test ensures
- basic (de-)serialization works
- creation of the message from minimal input (see API above) works
  • Loading branch information
konradkonrad committed Feb 7, 2019
1 parent 014532e commit 0226881
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 2 deletions.
33 changes: 31 additions & 2 deletions raiden/api/python.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,15 @@
TokenNotRegistered,
UnknownTokenAddress,
)
from raiden.messages import RequestMonitoring, SignedBlindedBalanceProof
from raiden.settings import DEFAULT_RETRY_TIMEOUT
from raiden.transfer import architecture, views
from raiden.transfer.events import (
EventPaymentReceivedSuccess,
EventPaymentSentFailed,
EventPaymentSentSuccess,
)
from raiden.transfer.state import NettingChannelState
from raiden.transfer.state import BalanceProofSignedState, NettingChannelState
from raiden.transfer.state_change import ActionChannelClose
from raiden.utils import pex, typing
from raiden.utils.gas_reserve import has_enough_gas_reserve
Expand Down Expand Up @@ -347,7 +348,7 @@ def set_total_channel_deposit(
Raises:
InvalidAddress: If either token_address or partner_address is not
20 bytes long.
TransactionThrew: May happen for multiple reasons:
TransactionThrew: May happen for multiple reasons:
- If the token approval fails, e.g. the token may validate if
account has enough balance for the allowance.
- The deposit failed, e.g. the allowance did not set the token
Expand Down Expand Up @@ -826,3 +827,31 @@ def get_blockchain_events_channel(
))
returned_events.sort(key=lambda evt: evt.get('block_number'), reverse=True)
return returned_events

def create_monitoring_request(
self,
balance_proof: BalanceProofSignedState,
reward_amount: typing.TokenAmount,
) -> typing.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.
"""
assert isinstance(balance_proof, BalanceProofSignedState)

# create submessage `SignedBlindedBalanceProof` and create the `non_closing_signature`
onchain_balance_proof = SignedBlindedBalanceProof.from_balance_proof_signed_state(
balance_proof,
)
onchain_balance_proof.sign(self.raiden.signer)

# create RequestMonitoring message from the above + `reward_amount`
monitor_request = RequestMonitoring(
onchain_balance_proof=onchain_balance_proof,
reward_amount=reward_amount,
)
# sign RequestMonitoring and return
monitor_request.sign(self.raiden.signer)
return monitor_request
50 changes: 50 additions & 0 deletions raiden/tests/integration/test_pythonapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,11 @@
InsufficientGasReserve,
InvalidAddress,
)
from raiden.messages import RequestMonitoring
from raiden.tests.utils.client import burn_eth
from raiden.tests.utils.events import must_have_event, wait_for_state_change
from raiden.tests.utils.factories import make_address
from raiden.tests.utils.network import CHAIN
from raiden.tests.utils.smartcontracts import deploy_contract_web3
from raiden.tests.utils.transfer import (
assert_synced_channel_state,
Expand All @@ -25,6 +27,7 @@
from raiden.transfer import views
from raiden.transfer.events import EventPaymentReceivedSuccess, EventPaymentSentSuccess
from raiden.transfer.state_change import ContractReceiveNewTokenNetwork
from raiden.utils import create_default_identifier
from raiden.utils.gas_reserve import (
GAS_RESERVE_ESTIMATE_SECURITY_FACTOR,
get_required_gas_estimate,
Expand Down Expand Up @@ -468,3 +471,50 @@ def test_set_deposit_limit_crash(raiden_network, token_amount, contract_manager,
partner_address=partner_address,
total_deposit=10000000000000000000000,
)


@pytest.mark.parametrize('deposit', [10])
@pytest.mark.parametrize('channels_per_node', [CHAIN])
@pytest.mark.parametrize('number_of_nodes', [2])
def test_create_monitoring_request(
raiden_network,
number_of_nodes,
deposit,
token_addresses,
network_wait,
chain_id,
):
app0, app1 = raiden_network
token_address = token_addresses[0]
chain_state = views.state_from_app(app0)
payment_network_id = app0.raiden.default_registry.address
token_network_identifier = views.get_token_network_identifier_by_token_address(
chain_state=chain_state,
payment_network_id=payment_network_id,
token_address=token_address,
)

payment_identifier = create_default_identifier()
mediated_transfer(
initiator_app=app1,
target_app=app0,
token_network_identifier=token_network_identifier,
amount=1,
identifier=payment_identifier,
)
chain_state = views.state_from_raiden(app0.raiden)
channel_state = views.get_channelstate_by_token_network_and_partner(
chain_state,
token_network_identifier,
app1.raiden.address,
)
balance_proof = channel_state.partner_state.balance_proof
api = RaidenAPI(app0.raiden)
request = api.create_monitoring_request(
balance_proof=balance_proof,
reward_amount=1,
)
assert request
as_dict = request.to_dict()
from_dict = RequestMonitoring.from_dict(as_dict)
assert from_dict.to_dict() == as_dict

0 comments on commit 0226881

Please sign in to comment.