Skip to content

Commit

Permalink
feat: set custom exit period on contract initialization
Browse files Browse the repository at this point in the history
  • Loading branch information
pgebal committed Feb 5, 2019
1 parent 3d00901 commit 50653d5
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 21 deletions.
5 changes: 0 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
# Plasma Contracts

**IMPORTANT NOTICE** / **TODO**:
This implementation includes a brute-force shortening of the exit periods, see [here](https://github.com/omisego/plasma-contracts/blob/master/contracts/RootChain.sol#L36).
This will be made configurable on contract deployment and should be _much longer_ for any secure deployment.
**DO NOT DEPLOY TO MAINNET**

Root chain contracts for Plasma MVP, work in progress.

## Contents
Expand Down
26 changes: 15 additions & 11 deletions contracts/RootChain.sol
Original file line number Diff line number Diff line change
Expand Up @@ -32,17 +32,16 @@ contract RootChain {
/*
* Storage
*/

// NOTE: this is the "middle" period. Exit period for fresh utxos we'll double that while IFE phase is half that
// TODO: this is a value sensible only for testing and demo purposes, **DO NOT DEPLOY TO MAINNET**
uint256 constant public MIN_EXIT_PERIOD = 4 minutes;
uint256 constant public CHILD_BLOCK_INTERVAL = 1000;

// WARNING: These placeholder bond values are entirely arbitrary.
uint256 public standardExitBond = 31415926535 wei;
uint256 public inFlightExitBond = 31415926535 wei;
uint256 public piggybackBond = 31415926535 wei;

// NOTE: this is the "middle" period. Exit period for fresh utxos we'll double that while IFE phase is half that
uint256 public minExitPeriod;

address public operator;

uint256 public nextChildBlock;
Expand Down Expand Up @@ -175,13 +174,18 @@ contract RootChain {
* Public functions
*/

// @dev Required to be called before any operations on the contract
// Split from `constructor` to fit into block gas limit
function init()
/**
* @dev Required to be called before any operations on the contract
* Split from `constructor` to fit into block gas limit
* @param _minExitPeriod standard exit period in seconds
*/
function init(uint256 _minExitPeriod)
public
{
_initOperator();

minExitPeriod = _minExitPeriod;

nextChildBlock = CHILD_BLOCK_INTERVAL;
nextDepositBlock = 1;

Expand Down Expand Up @@ -904,11 +908,11 @@ contract RootChain {
{
uint256 blknum = _outputId.getBlknum();
if (blknum % CHILD_BLOCK_INTERVAL == 0) {
return Math.max(blocks[blknum].timestamp + (MIN_EXIT_PERIOD * 2), block.timestamp + MIN_EXIT_PERIOD);
return Math.max(blocks[blknum].timestamp + (minExitPeriod * 2), block.timestamp + minExitPeriod);
}
else {
// High priority exit for the deposit.
return block.timestamp + MIN_EXIT_PERIOD;
return block.timestamp + minExitPeriod;
}
}

Expand All @@ -922,7 +926,7 @@ contract RootChain {
view
returns (uint256)
{
return ((block.timestamp + (2 * MIN_EXIT_PERIOD)) << 192) | _feeExitId;
return ((block.timestamp + (2 * minExitPeriod)) << 192) | _feeExitId;
}


Expand Down Expand Up @@ -998,7 +1002,7 @@ contract RootChain {
view
returns (uint256)
{
uint256 periodTime = MIN_EXIT_PERIOD / 2;
uint256 periodTime = minExitPeriod / 2;
return ((block.timestamp - clearFlag(_inFlightExit.exitStartTimestamp)) / periodTime) + 1;
}

Expand Down
3 changes: 1 addition & 2 deletions plasma_core/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,5 @@
WEEK = 60 * 60 * 24 * 7
DAY = 60 * 60 * 24

# TODO: this is dictated by the hard-coded constant in RootChain.sol for now. After parametrized, it could be used for
# suitable test contract instantiation and can even be shorter.
# used for suitable test contract instantiation
MIN_EXIT_PERIOD = 4 * MINUTE
17 changes: 16 additions & 1 deletion tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from solc import link_code


EXIT_PERIOD = 4 * 60 # 4 minutes
GAS_LIMIT = 8000000
START_GAS = GAS_LIMIT - 1000000
config_metropolis['BLOCK_GAS_LIMIT'] = GAS_LIMIT
Expand Down Expand Up @@ -68,12 +69,16 @@ def create_contract(path, args=(), sender=ethtester.k0, libraries=dict()):

@pytest.fixture
def root_chain(ethtester, get_contract):
return initialized_contract(ethtester, get_contract, EXIT_PERIOD)


def initialized_contract(ethtester, get_contract, exit_period):
pql = get_contract('PriorityQueueLib')
pqf = get_contract('PriorityQueueFactory', libraries={'PriorityQueueLib': pql.address})
ethtester.chain.mine()
contract = get_contract('RootChain', libraries={'PriorityQueueFactory': pqf.address})
ethtester.chain.mine()
contract.init(sender=ethtester.k0)
contract.init(exit_period, sender=ethtester.k0)
ethtester.chain.mine()
return contract

Expand All @@ -90,6 +95,16 @@ def testlang(root_chain, ethtester):
return TestingLanguage(root_chain, ethtester)


@pytest.fixture
def root_chain_short_exit_period(ethtester, get_contract):
return initialized_contract(ethtester, get_contract, 0)


@pytest.fixture
def testlang_root_chain_short_exit_period(root_chain_short_exit_period, ethtester):
return TestingLanguage(root_chain_short_exit_period, ethtester)


@pytest.fixture
def utxo(testlang):
return testlang.create_utxo()
Expand Down
21 changes: 19 additions & 2 deletions tests/contracts/root_chain/test_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,27 @@
from ethereum.tools.tester import TransactionFailed


EXIT_PERIOD = 4 * 60


@pytest.fixture
def utxo(testlang_root_chain_short_exit_period):
return testlang_root_chain_short_exit_period.create_utxo()


def test_cant_ever_init_twice(ethtester, root_chain):
ethtester.chain.mine()
with pytest.raises(TransactionFailed):
root_chain.init(sender=ethtester.k0)
root_chain.init(EXIT_PERIOD, sender=ethtester.k0)


def test_exit_period_setting_has_effect(testlang_root_chain_short_exit_period):
owner = testlang_root_chain_short_exit_period.accounts[0]
deposit_id = testlang_root_chain_short_exit_period.deposit(owner, 100)

spend_id = testlang_root_chain_short_exit_period.spend_utxo([deposit_id], [owner.key])

testlang_root_chain_short_exit_period.start_in_flight_exit(spend_id)

with pytest.raises(TransactionFailed):
root_chain.init(sender=ethtester.k1)
testlang_root_chain_short_exit_period.piggyback_in_flight_exit_input(spend_id, 0, owner.key)

0 comments on commit 50653d5

Please sign in to comment.