Skip to content

Commit

Permalink
Add deprecation switch tests
Browse files Browse the repository at this point in the history
- setting the deprecation switch + edge cases
- making sure channels cannot be opened and deposits cannot be made after the deprecation switch is set to `True`
  • Loading branch information
loredanacirstea committed Sep 19, 2018
1 parent bda68dc commit cf0a840
Showing 1 changed file with 170 additions and 5 deletions.
175 changes: 170 additions & 5 deletions raiden_contracts/tests/test_deprecation_switch.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
TEST_SETTLE_TIMEOUT_MIN,
TEST_SETTLE_TIMEOUT_MAX,
)
from raiden_contracts.utils.utils import get_pending_transfers_tree
from raiden_contracts.tests.fixtures.config import (
EMPTY_BALANCE_HASH,
EMPTY_LOCKSROOT,
Expand All @@ -17,6 +18,8 @@
EMPTY_ADDRESS,
FAKE_ADDRESS,
)
from raiden_contracts.tests.utils import ChannelValues
from raiden_contracts.tests.fixtures.channel import call_settle


def test_administrator(
Expand All @@ -27,12 +30,12 @@ def test_administrator(
custom_token,
get_accounts,
):
(A, B) = get_accounts(2)
(administrator, B) = get_accounts(2)

json_contract = contracts_manager.get_contract(CONTRACT_TOKEN_NETWORK_REGISTRY)
token_network_registry = deploy_contract(
web3,
A,
administrator,
json_contract['abi'],
json_contract['bin'],
[
Expand All @@ -44,7 +47,7 @@ def test_administrator(
)

# Make sure deployer is administrator
assert token_network_registry.functions.administrator().call() == A
assert token_network_registry.functions.administrator().call() == administrator

# Only the administrator can deploy TokenNetwork contracts
with pytest.raises(TransactionFailed):
Expand All @@ -58,7 +61,7 @@ def test_administrator(
tx_hash = token_network_registry.functions.createERC20TokenNetwork(
custom_token.address,
).transact(
{'from': A},
{'from': administrator},
)
tx_receipt = web3.eth.getTransactionReceipt(tx_hash)
event_abi = contracts_manager.get_event_abi(
Expand All @@ -72,4 +75,166 @@ def test_administrator(
address=token_network_address,
)

assert token_network.functions.administrator().call() == A
assert token_network.functions.administrator().call() == administrator


def test_set_deprecation_switch(get_accounts, token_network):
(A) = get_accounts(1)[0]
administrator = token_network.functions.administrator().call()

assert token_network.functions.safety_deprecation_switch().call() == False

with pytest.raises(TransactionFailed):
token_network.functions.setSafetyDeprecationSwitch(False).transact({
'from': administrator
})
with pytest.raises(TransactionFailed):
token_network.functions.setSafetyDeprecationSwitch(False).transact({
'from': A
})

token_network.functions.setSafetyDeprecationSwitch(True).transact({
'from': administrator
})
assert token_network.functions.safety_deprecation_switch().call() == True


def test_deprecation_switch(get_accounts, token_network, create_channel, channel_deposit):
administrator = token_network.functions.administrator().call()
(A, B, C, D) = get_accounts(4)
deposit = 100
bigger_deposit = 200

channel_identifier = create_channel(A, B)[0]
channel_deposit(channel_identifier, A, deposit, B)
channel_deposit(channel_identifier, B, deposit, A)

token_network.functions.setSafetyDeprecationSwitch(True).transact({
'from': administrator
})
assert token_network.functions.safety_deprecation_switch().call() == True

# Now we cannot deposit in existent channels
with pytest.raises(TransactionFailed):
channel_deposit(channel_identifier, A, bigger_deposit, B)
with pytest.raises(TransactionFailed):
channel_deposit(channel_identifier, B, bigger_deposit, A)

# Now we cannot open channels anymore
with pytest.raises(TransactionFailed):
channel_identifier = create_channel(C, D)[0]

# If we set the deprecation switch to False again, we can open & deposit
token_network.functions.setSafetyDeprecationSwitch(False).transact({
'from': administrator
})
assert token_network.functions.safety_deprecation_switch().call() == False

channel_deposit(channel_identifier, A, bigger_deposit, B)
channel_deposit(channel_identifier, B, bigger_deposit, A)
channel_identifier = create_channel(C, D)[0]


def test_deprecation_switch_settle(
web3,
get_accounts,
token_network,
custom_token,
reveal_secrets,
create_channel,
channel_deposit,
close_and_update_channel,
):
administrator = token_network.functions.administrator().call()
(A, B) = get_accounts(2)
deposit = 100
bigger_deposit = 200

(vals_A, vals_B) = (
ChannelValues(deposit=deposit, withdrawn=0, transferred=5, claimable_locked=2, unclaimable_locked=4),
ChannelValues(deposit=deposit, withdrawn=0, transferred=10, claimable_locked=4, unclaimable_locked=6),
)

pre_balance_A = custom_token.functions.balanceOf(A).call()
pre_balance_B = custom_token.functions.balanceOf(B).call()
pre_balance_contract = custom_token.functions.balanceOf(token_network.address).call()

channel_identifier = create_channel(A, B)[0]
channel_deposit(channel_identifier, A, vals_A.deposit, B)
channel_deposit(channel_identifier, B, vals_B.deposit, A)

# Mock pending transfers data for A -> B
pending_transfers_tree_A = get_pending_transfers_tree(
web3,
unlockable_amount=vals_A.claimable_locked,
expired_amount=vals_A.unclaimable_locked,
)
vals_A.locksroot = pending_transfers_tree_A.merkle_root
# Reveal A's secrets.
reveal_secrets(A, pending_transfers_tree_A.unlockable)

# Mock pending transfers data for B -> A
pending_transfers_tree_B = get_pending_transfers_tree(
web3,
unlockable_amount=vals_B.claimable_locked,
expired_amount=vals_B.unclaimable_locked,
)
vals_B.locksroot = pending_transfers_tree_B.merkle_root
# Reveal B's secrets
reveal_secrets(B, pending_transfers_tree_B.unlockable)

# Set the deprecation switch to true
token_network.functions.setSafetyDeprecationSwitch(True).transact({
'from': administrator
})
assert token_network.functions.safety_deprecation_switch().call() == True

# We cannot deposit in existent channels
with pytest.raises(TransactionFailed):
token_network.functions.setTotalDeposit(
channel_identifier,
A,
bigger_deposit,
B,
).transact({'from': A})
with pytest.raises(TransactionFailed):
token_network.functions.setTotalDeposit(
channel_identifier,
B,
bigger_deposit,
A,
).transact({'from': A})

# We need to make sure we can still close, settle & unlock the channels
close_and_update_channel(
channel_identifier,
A,
vals_A,
B,
vals_B,
)
web3.testing.mine(TEST_SETTLE_TIMEOUT_MIN)

call_settle(token_network, channel_identifier, A, vals_A, B, vals_B)

# Unlock B's pending transfers that were sent to A
token_network.functions.unlock(
channel_identifier,
A,
B,
pending_transfers_tree_B.packed_transfers,
).transact()

# Unlock A's pending transfers that were sent to B
token_network.functions.unlock(
channel_identifier,
B,
A,
pending_transfers_tree_A.packed_transfers,
).transact()

assert custom_token.functions.balanceOf(A).call() == pre_balance_A + 107
assert custom_token.functions.balanceOf(B).call() == pre_balance_B + 93
assert custom_token.functions.balanceOf(
token_network.address,
).call() == pre_balance_contract

0 comments on commit cf0a840

Please sign in to comment.