Skip to content

Commit

Permalink
Make deposit limits configurable at TokenNetowk
Browse files Browse the repository at this point in the history
deployment time.

After some trial and error I believe that the best approach to
accomodate the different use cases of TokenNetwork is to make it
configurable at the deployment time, as opposed to providing multiple
flavors or versions of the source code.
  • Loading branch information
pirapira committed Mar 2, 2019
1 parent 7f5a218 commit 9d5b0f2
Show file tree
Hide file tree
Showing 15 changed files with 244 additions and 92 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ Documents changes that result in:

## Unreleased

- TokenNetwork's constructor takes two additional arguments `_channel_participant_deposit_limit` and `_token_network_deposit_limit`
- TokenNetworkRegistry's constructor takes an additional argument `max_number_of_token_networks`
- Removed flavors

Expand Down
20 changes: 12 additions & 8 deletions raiden_contracts/contracts/raiden/TokenNetwork.sol
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,10 @@ contract TokenNetwork is Utils {
115792089237316195423570985008687907853269984665640564039457584007913129639935
);

// Red Eyes release deposit limits
// The combined deposit of one channel is limited to 0.15 ETH.
// So 0.075 ETH per participant.
uint256 constant public channel_participant_deposit_limit = 75000000000000000 wei;
// The total combined deposit of all channels across the whole network is
// limited to 250 ETH.
uint256 constant public token_network_deposit_limit = 250000000000000000000 wei;
// The deposit limit per channel per participant.
uint256 public channel_participant_deposit_limit;
// The total combined deposit of all channels across the whole network
uint256 public token_network_deposit_limit;

// Global, monotonically increasing counter that keeps track of all the
// opened channels in this contract
Expand Down Expand Up @@ -214,7 +211,9 @@ contract TokenNetwork is Utils {
uint256 _chain_id,
uint256 _settlement_timeout_min,
uint256 _settlement_timeout_max,
address _deprecation_executor
address _deprecation_executor,
uint256 _channel_participant_deposit_limit,
uint256 _token_network_deposit_limit
)
public
{
Expand All @@ -226,6 +225,9 @@ contract TokenNetwork is Utils {
require(_settlement_timeout_max > _settlement_timeout_min);
require(contractExists(_token_address));
require(contractExists(_secret_registry));
require(_channel_participant_deposit_limit > 0);
require(_token_network_deposit_limit > 0);
require(_token_network_deposit_limit >= _channel_participant_deposit_limit);

token = Token(_token_address);

Expand All @@ -238,6 +240,8 @@ contract TokenNetwork is Utils {
require(token.totalSupply() > 0);

deprecation_executor = _deprecation_executor;
channel_participant_deposit_limit = _channel_participant_deposit_limit;
token_network_deposit_limit = _token_network_deposit_limit;
}

function deprecate() isSafe onlyDeprecationExecutor public {
Expand Down
10 changes: 8 additions & 2 deletions raiden_contracts/contracts/raiden/TokenNetworkRegistry.sol
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,11 @@ contract TokenNetworkRegistry is Utils {
/// `_token_address`.
/// @param _token_address Ethereum address of an already deployed token, to
/// be used in the new TokenNetwork contract.
function createERC20TokenNetwork(address _token_address)
function createERC20TokenNetwork(
address _token_address,
uint256 _channel_participant_deposit_limit,
uint256 _token_network_deposit_limit
)
canCreateTokenNetwork
external
returns (address token_network_address)
Expand All @@ -79,7 +83,9 @@ contract TokenNetworkRegistry is Utils {
chain_id,
settlement_timeout_min,
settlement_timeout_max,
deprecation_executor
deprecation_executor,
_channel_participant_deposit_limit,
_token_network_deposit_limit
);

token_network_address = address(token_network);
Expand Down
12 changes: 9 additions & 3 deletions raiden_contracts/contracts/test/TokenNetworkInternalsTest.sol
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ contract TokenNetworkInternalStorageTest is TokenNetwork {
_chain_id,
_settlement_timeout_min,
_settlement_timeout_max,
msg.sender
msg.sender,
MAX_SAFE_UINT256,
MAX_SAFE_UINT256
)
public
{
Expand Down Expand Up @@ -152,7 +154,9 @@ contract TokenNetworkSignatureTest is TokenNetwork {
_chain_id,
_settlement_timeout_min,
_settlement_timeout_max,
msg.sender
msg.sender,
MAX_SAFE_UINT256,
MAX_SAFE_UINT256
)
public
{
Expand Down Expand Up @@ -256,7 +260,9 @@ contract TokenNetworkUtilsTest is TokenNetwork {
_chain_id,
_settlement_timeout_min,
_settlement_timeout_max,
msg.sender
msg.sender,
MAX_SAFE_UINT256,
MAX_SAFE_UINT256
)
public
{
Expand Down
104 changes: 60 additions & 44 deletions raiden_contracts/data/contracts.json

Large diffs are not rendered by default.

24 changes: 17 additions & 7 deletions raiden_contracts/data/gas.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,21 @@
{
"EndpointRegistry.registerEndpoint": 48408,
"MonitoringService.claimReward": 42679,
"MonitoringService.monitor": 203858,
"OneToN.claim": 90847,
"SecretRegistry.registerSecret": 45757,
"TokenNetwork.closeChannel": 111155,
"TokenNetwork.openChannel": 97555,
"TokenNetwork.setTotalDeposit": 44509,
"TokenNetwork.settleChannel": 123370,
"TokenNetwork.unlock 1 locks": 32192,
"TokenNetwork.unlock 6 locks": 66009,
"TokenNetwork.updateNonClosingBalanceProof": 93786
"TokenNetwork DEPLOYMENT": 4031224,
"TokenNetwork.closeChannel": 111162,
"TokenNetwork.openChannel": 97745,
"TokenNetwork.setTotalDeposit": 44919,
"TokenNetwork.settleChannel": 123380,
"TokenNetwork.unlock 1 locks": 32118,
"TokenNetwork.unlock 6 locks": 66029,
"TokenNetwork.updateNonClosingBalanceProof": 93759,
"TokenNetworkRegistry DEPLOYMENT": 4753616,
"TokenNetworkRegistry createERC20TokenNetwork": 3110454,
"UserDeposit.deposit": 101311,
"UserDeposit.deposit (increase balance)": 28156,
"UserDeposit.planWithdraw": 64021,
"UserDeposit.withdraw": 40079
}
20 changes: 12 additions & 8 deletions raiden_contracts/data/source/raiden/TokenNetwork.sol
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,10 @@ contract TokenNetwork is Utils {
115792089237316195423570985008687907853269984665640564039457584007913129639935
);

// Red Eyes release deposit limits
// The combined deposit of one channel is limited to 0.15 ETH.
// So 0.075 ETH per participant.
uint256 constant public channel_participant_deposit_limit = 75000000000000000 wei;
// The total combined deposit of all channels across the whole network is
// limited to 250 ETH.
uint256 constant public token_network_deposit_limit = 250000000000000000000 wei;
// The deposit limit per channel per participant.
uint256 public channel_participant_deposit_limit;
// The total combined deposit of all channels across the whole network
uint256 public token_network_deposit_limit;

// Global, monotonically increasing counter that keeps track of all the
// opened channels in this contract
Expand Down Expand Up @@ -214,7 +211,9 @@ contract TokenNetwork is Utils {
uint256 _chain_id,
uint256 _settlement_timeout_min,
uint256 _settlement_timeout_max,
address _deprecation_executor
address _deprecation_executor,
uint256 _channel_participant_deposit_limit,
uint256 _token_network_deposit_limit
)
public
{
Expand All @@ -226,6 +225,9 @@ contract TokenNetwork is Utils {
require(_settlement_timeout_max > _settlement_timeout_min);
require(contractExists(_token_address));
require(contractExists(_secret_registry));
require(_channel_participant_deposit_limit > 0);
require(_token_network_deposit_limit > 0);
require(_token_network_deposit_limit >= _channel_participant_deposit_limit);

token = Token(_token_address);

Expand All @@ -238,6 +240,8 @@ contract TokenNetwork is Utils {
require(token.totalSupply() > 0);

deprecation_executor = _deprecation_executor;
channel_participant_deposit_limit = _channel_participant_deposit_limit;
token_network_deposit_limit = _token_network_deposit_limit;
}

function deprecate() isSafe onlyDeprecationExecutor public {
Expand Down
10 changes: 8 additions & 2 deletions raiden_contracts/data/source/raiden/TokenNetworkRegistry.sol
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,11 @@ contract TokenNetworkRegistry is Utils {
/// `_token_address`.
/// @param _token_address Ethereum address of an already deployed token, to
/// be used in the new TokenNetwork contract.
function createERC20TokenNetwork(address _token_address)
function createERC20TokenNetwork(
address _token_address,
uint256 _channel_participant_deposit_limit,
uint256 _token_network_deposit_limit
)
canCreateTokenNetwork
external
returns (address token_network_address)
Expand All @@ -79,7 +83,9 @@ contract TokenNetworkRegistry is Utils {
chain_id,
settlement_timeout_min,
settlement_timeout_max,
deprecation_executor
deprecation_executor,
_channel_participant_deposit_limit,
_token_network_deposit_limit
);

token_network_address = address(token_network);
Expand Down
12 changes: 9 additions & 3 deletions raiden_contracts/data/source/test/TokenNetworkInternalsTest.sol
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ contract TokenNetworkInternalStorageTest is TokenNetwork {
_chain_id,
_settlement_timeout_min,
_settlement_timeout_max,
msg.sender
msg.sender,
MAX_SAFE_UINT256,
MAX_SAFE_UINT256
)
public
{
Expand Down Expand Up @@ -152,7 +154,9 @@ contract TokenNetworkSignatureTest is TokenNetwork {
_chain_id,
_settlement_timeout_min,
_settlement_timeout_max,
msg.sender
msg.sender,
MAX_SAFE_UINT256,
MAX_SAFE_UINT256
)
public
{
Expand Down Expand Up @@ -256,7 +260,9 @@ contract TokenNetworkUtilsTest is TokenNetwork {
_chain_id,
_settlement_timeout_min,
_settlement_timeout_max,
msg.sender
msg.sender,
MAX_SAFE_UINT256,
MAX_SAFE_UINT256
)
public
{
Expand Down
32 changes: 28 additions & 4 deletions raiden_contracts/tests/fixtures/token_network_fixtures.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
EVENT_TOKEN_NETWORK_CREATED,
TEST_SETTLE_TIMEOUT_MIN,
TEST_SETTLE_TIMEOUT_MAX,
MAX_ETH_CHANNEL_PARTICIPANT,
MAX_ETH_TOKEN_NETWORK,
)
from web3.contract import get_event_data

Expand All @@ -31,9 +33,15 @@ def register_token_network(
):
"""Returns a function that uses token_network_registry fixture to register
and deploy a new token network"""
def get(token_address):
def get(
token_address,
channel_participant_deposit_limit: int,
token_network_deposit_limit: int,
):
tx_hash = token_network_registry_contract.functions.createERC20TokenNetwork(
token_address,
channel_participant_deposit_limit,
token_network_deposit_limit,
).transact({'from': contract_deployer_address})
tx_receipt = web3.eth.getTransactionReceipt(tx_hash)
event_abi = contracts_manager.get_event_abi(
Expand All @@ -50,13 +58,29 @@ def get(token_address):
return get


@pytest.fixture
def channel_participant_deposit_limit():
return MAX_ETH_CHANNEL_PARTICIPANT


@pytest.fixture
def token_network_deposit_limit():
return MAX_ETH_TOKEN_NETWORK


@pytest.fixture
def token_network(
register_token_network,
custom_token,
register_token_network,
custom_token,
channel_participant_deposit_limit,
token_network_deposit_limit,
):
"""Register a new token network for a custom token"""
return register_token_network(custom_token.address)
return register_token_network(
custom_token.address,
channel_participant_deposit_limit,
token_network_deposit_limit,
)


@pytest.fixture
Expand Down
4 changes: 4 additions & 0 deletions raiden_contracts/tests/fixtures/token_network_registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,16 @@ def add_and_register_token(
deploy_token_contract,
contract_deployer_address,
contracts_manager,
channel_participant_deposit_limit,
token_network_deposit_limit,
):
"""Deploy a token and register it in TokenNetworkRegistry"""
def f(initial_amount: int, decimals: int, token_name: str, token_symbol: str):
token_contract = deploy_token_contract(initial_amount, decimals, token_name, token_symbol)
txid = token_network_registry_contract.functions.createERC20TokenNetwork(
token_contract.address,
channel_participant_deposit_limit,
token_network_deposit_limit,
).transact({'from': contract_deployer_address})
(tx_receipt, _) = check_succesful_tx(web3, txid)
assert len(tx_receipt['logs']) == 1
Expand Down
4 changes: 4 additions & 0 deletions raiden_contracts/tests/property/test_tokennetwork.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
from raiden_contracts.constants import (
CONTRACT_SECRET_REGISTRY,
CONTRACT_TOKEN_NETWORK_REGISTRY,
MAX_ETH_CHANNEL_PARTICIPANT,
MAX_ETH_TOKEN_NETWORK,
TEST_SETTLE_TIMEOUT_MAX,
TEST_SETTLE_TIMEOUT_MIN,
)
Expand Down Expand Up @@ -147,6 +149,8 @@ def __init__(self):

self.token_network_registry.functions.createERC20TokenNetwork(
self.token.address,
MAX_ETH_CHANNEL_PARTICIPANT,
MAX_ETH_TOKEN_NETWORK,
).transact()

token_network_address = self.token_network_registry.functions.token_to_token_networks(
Expand Down
26 changes: 22 additions & 4 deletions raiden_contracts/tests/test_contract_limits.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ def test_register_three_but_not_four(
get_token_network_registry,
secret_registry_contract,
custom_token_factory,
channel_participant_deposit_limit,
token_network_deposit_limit,
):
""" Check that TokenNetworkRegistry observes the max number of tokens """
token_network_registry = get_token_network_registry([
Expand All @@ -28,11 +30,27 @@ def test_register_three_but_not_four(
token1 = custom_token_factory()
token2 = custom_token_factory()
token3 = custom_token_factory()
token_network_registry.functions.createERC20TokenNetwork(token0.address).transact()
token_network_registry.functions.createERC20TokenNetwork(token1.address).transact()
token_network_registry.functions.createERC20TokenNetwork(token2.address).transact()
token_network_registry.functions.createERC20TokenNetwork(
token0.address,
channel_participant_deposit_limit,
token_network_deposit_limit,
).transact()
token_network_registry.functions.createERC20TokenNetwork(
token1.address,
channel_participant_deposit_limit,
token_network_deposit_limit,
).transact()
token_network_registry.functions.createERC20TokenNetwork(
token2.address,
channel_participant_deposit_limit,
token_network_deposit_limit,
).transact()
with pytest.raises(TransactionFailed):
token_network_registry.functions.createERC20TokenNetwork(token3.address).transact()
token_network_registry.functions.createERC20TokenNetwork(
token3.address,
channel_participant_deposit_limit,
token_network_deposit_limit,
).transact()


def test_channel_participant_deposit_limit_value(token_network):
Expand Down
Loading

0 comments on commit 9d5b0f2

Please sign in to comment.