Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add fields to NewBalanceProofReceived event #373

Merged
merged 9 commits into from
Dec 17, 2018
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions raiden_contracts/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,14 @@ class MessageTypeId(IntEnum):
COOPERATIVE_SETTLE = 4


# Message types used by MonitoringService contract
class MonitoringServiceEvent(str, Enum):
NEW_DEPOSIT = 'NewDeposit'
NEW_BALANCE_PROOF_RECEIVED = 'NewBalanceProofReceived'
REWARD_CLAIMED = 'RewardClaimed'
WITHDRAWN = 'Withdrawn'


# Network configurations
START_QUERY_BLOCK_KEY = 'DefaultStartBlock'

Expand Down
4 changes: 4 additions & 0 deletions raiden_contracts/contracts/services/MonitoringService.sol
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ contract MonitoringService is Utils {

event NewDeposit(address indexed receiver, uint amount);
event NewBalanceProofReceived(
address token_network_address,
uint256 channel_identifier,
uint256 reward_amount,
uint256 indexed nonce,
address indexed ms_address,
Expand Down Expand Up @@ -219,6 +221,8 @@ contract MonitoringService is Utils {
);

emit NewBalanceProofReceived(
token_network_address,
channel_identifier,
reward_amount,
nonce,
msg.sender,
Expand Down
2 changes: 1 addition & 1 deletion raiden_contracts/data/contracts.json

Large diffs are not rendered by default.

60 changes: 53 additions & 7 deletions raiden_contracts/tests/test_monitoring_service.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
import pytest
from raiden_contracts.constants import ChannelEvent
from raiden_contracts.utils.events import check_channel_closed
from eth_abi import encode_single
from web3 import Web3

from raiden_contracts.constants import ChannelEvent, MonitoringServiceEvent
from raiden_contracts.utils.events import (
check_channel_closed,
check_ms_new_deposit,
check_new_balance_proof_received,
check_reward_claimed,
)
from raiden_contracts.utils.sign import sign_reward_proof
from raiden_contracts.utils.merkle import EMPTY_MERKLE_ROOT

Expand Down Expand Up @@ -51,7 +59,8 @@ def test_msc_happy_path(
custom_token,
):
# setup: two parties + MS
ev_handler = event_handler(token_network)
token_network_ev_handler = event_handler(token_network)
ms_ev_handler = event_handler(monitoring_service_external)
(A, B, MS) = get_accounts(3)
reward_amount = 10
# mint some tokens
Expand All @@ -64,12 +73,19 @@ def test_msc_happy_path(
ms_balance_after_deposit = monitoring_service_external.functions.balances(MS).call()
# raiden node deposit
custom_token.functions.approve(monitoring_service_external.address, 20).transact({'from': B})
monitoring_service_external.functions.deposit(B, 20).transact({'from': B})
txn_hash = monitoring_service_external.functions.deposit(B, 20).transact({'from': B})
ms_ev_handler.add(
txn_hash,
MonitoringServiceEvent.NEW_DEPOSIT,
check_ms_new_deposit(B, 20),
)
ms_ev_handler.check()

# 1) open a channel (c1, c2)
channel_identifier = create_channel(A, B)[0]
txn_hash = channel_deposit(channel_identifier, A, 20, B)
txn_hash = channel_deposit(channel_identifier, B, 20, A)

# 2) create balance proof
balance_proof_A = create_balance_proof(channel_identifier, B, transferred_amount=10, nonce=1)
balance_proof_B = create_balance_proof(channel_identifier, A, transferred_amount=20, nonce=2)
Expand All @@ -86,18 +102,19 @@ def test_msc_happy_path(
token_network.address,
nonce=balance_proof_B[1],
)

# 3) c1 closes channel
txn_hash = token_network.functions.closeChannel(
channel_identifier, B, *balance_proof_A,
).transact({'from': A})
ev_handler.add(
token_network_ev_handler.add(
txn_hash,
ChannelEvent.CLOSED,
check_channel_closed(channel_identifier, A, balance_proof_A[1]),
)
ev_handler.check()
# 4) MS calls `MSC::monitor()` using c1's BP and reward proof
token_network_ev_handler.check()

# 4) MS calls `MSC::monitor()` using c1's BP and reward proof
txn_hash = monitoring_service_external.functions.monitor(
A,
B,
Expand All @@ -110,6 +127,20 @@ def test_msc_happy_path(
token_network.address, # token network address
reward_proof[5], # reward proof signature
).transact({'from': MS})
ms_ev_handler.add(
txn_hash,
MonitoringServiceEvent.NEW_BALANCE_PROOF_RECEIVED,
check_new_balance_proof_received(
token_network.address,
channel_identifier,
reward_amount,
balance_proof_B[1],
MS,
B,
),
)
ms_ev_handler.check()

# 5) MSC calls TokenNetwork updateTransfer
# 6) channel is settled
token_network.web3.testing.mine(8)
Expand All @@ -124,12 +155,27 @@ def test_msc_happy_path(
0, # participant1_locked_amount
EMPTY_MERKLE_ROOT, # participant1_locksroot
).transact()

# 7) MS claims the reward
monitoring_service_external.functions.claimReward(
channel_identifier,
token_network.address,
A,
B,
).transact({'from': MS})
reward_identifier = Web3.sha3(
encode_single('uint256', channel_identifier) +
Web3.toBytes(hexstr=token_network.address),
)
ms_ev_handler.add(
txn_hash,
MonitoringServiceEvent.REWARD_CLAIMED,
check_reward_claimed(
MS,
reward_amount,
reward_identifier,
),
)
ms_ev_handler.check()
ms_balance_after_reward = monitoring_service_external.functions.balances(MS).call()
assert ms_balance_after_reward == (ms_balance_after_deposit + reward_amount)
35 changes: 35 additions & 0 deletions raiden_contracts/utils/events.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ def get(event):
return get


# Check TokenNetwork.ChannelNewDeposit events. Use check_ms_new_deposit for
# MonitoringService.NewDeposit events.
def check_new_deposit(channel_identifier, participant, deposit):
def get(event):
assert event['args']['channel_identifier'] == channel_identifier
Expand Down Expand Up @@ -91,3 +93,36 @@ def get(event):
assert event['args']['participant1_amount'] == participant1_amount
assert event['args']['participant2_amount'] == participant2_amount
return get


def check_ms_new_deposit(receiver, amount):
def get(event):
assert event['args']['receiver'] == receiver
assert event['args']['amount'] == amount
return get


def check_new_balance_proof_received(
token_network_address,
channel_identifier,
reward_amount,
nonce,
ms_address,
raiden_node_address,
):
def get(event):
assert event['args']['token_network_address'] == token_network_address
assert event['args']['channel_identifier'] == channel_identifier
assert event['args']['reward_amount'] == reward_amount
assert event['args']['nonce'] == nonce
assert event['args']['ms_address'] == ms_address
assert event['args']['raiden_node_address'] == raiden_node_address
return get


def check_reward_claimed(ms_address, amount, reward_identifier):
def get(event):
assert event['args']['ms_address'] == ms_address
assert event['args']['amount'] == amount
assert event['args']['reward_identifier'] == reward_identifier
return get