Skip to content

Commit

Permalink
Merge pull request #65 from OpenSTFoundation/integration_test_revert
Browse files Browse the repository at this point in the history
Integration test revert
  • Loading branch information
benjaminbollen authored Dec 14, 2017
2 parents f9c2ccc + 3ce7cc8 commit 8cc829e
Show file tree
Hide file tree
Showing 8 changed files with 732 additions and 74 deletions.
60 changes: 34 additions & 26 deletions contracts/OpenSTUtility.sol
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,7 @@ contract OpenSTUtility is Hasher, OpsManaged {
require(_stakingUnlockHeight > 0);
require(_stakingIntentHash != "");

expirationHeight = block.number + BLOCKS_TO_WAIT_SHORT;
expirationHeight = block.number + blocksToWaitShort();
nonces[_staker] = _stakerNonce;

bytes32 stakingIntentHash = hashStakingIntent(
Expand Down Expand Up @@ -459,7 +459,7 @@ contract OpenSTUtility is Hasher, OpsManaged {
require(token.allowance(msg.sender, address(this)) >= _amountBT);
require(token.transferFrom(msg.sender, address(this), _amountBT));

unlockHeight = block.number + BLOCKS_TO_WAIT_LONG;
unlockHeight = block.number + blocksToWaitLong();

redemptionIntentHash = hashRedemptionIntent(
_uuid,
Expand Down Expand Up @@ -502,7 +502,7 @@ contract OpenSTUtility is Hasher, OpsManaged {
nonces[msg.sender] = _nonce;

amountSTP = msg.value;
unlockHeight = block.number + BLOCKS_TO_WAIT_LONG;
unlockHeight = block.number + blocksToWaitLong();

redemptionIntentHash = hashRedemptionIntent(
uuidSTPrime,
Expand Down Expand Up @@ -618,32 +618,40 @@ contract OpenSTUtility is Hasher, OpsManaged {
registeredToken.registrar);
}

/*
* Administrative functions
*/
function initiateProtocolTransfer(
ProtocolVersioned _token,
address _proposedProtocol)
public
onlyAdmin
returns (bool)
{
_token.initiateProtocolTransfer(_proposedProtocol);
/*
* Administrative functions
*/
function initiateProtocolTransfer(
ProtocolVersioned _token,
address _proposedProtocol)
public
onlyAdmin
returns (bool)
{
_token.initiateProtocolTransfer(_proposedProtocol);

return true;
}
return true;
}

// on the very first released version v0.9.1 there is no need
// to completeProtocolTransfer from a previous version
// on the very first released version v0.9.1 there is no need
// to completeProtocolTransfer from a previous version

function revokeProtocolTransfer(
ProtocolVersioned _token)
public
onlyAdmin
returns (bool)
{
_token.revokeProtocolTransfer();
function revokeProtocolTransfer(
ProtocolVersioned _token)
public
onlyAdmin
returns (bool)
{
_token.revokeProtocolTransfer();

return true;
}

function blocksToWaitLong() public pure returns (uint256) {
return BLOCKS_TO_WAIT_LONG;
}

return true;
function blocksToWaitShort() public pure returns (uint256) {
return BLOCKS_TO_WAIT_SHORT;
}
}
49 changes: 49 additions & 0 deletions contracts/OpenSTUtilityMock.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
pragma solidity ^0.4.17;

// Copyright 2017 OpenST Ltd.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// ----------------------------------------------------------------------------
// Utility chain: OpenSTUtilityMock.sol
//
// http://www.simpletoken.org/
//
// ----------------------------------------------------------------------------

import "./OpenSTUtility.sol";

/// @title OpenSTUtilityMock
/// @dev Overrides certain durational constants and getters to ease testing OpenSTUtility
contract OpenSTUtilityMock is OpenSTUtility {
uint256 private constant BLOCKS_TO_WAIT_LONG = 8;
uint256 private constant BLOCKS_TO_WAIT_SHORT = 5;

/*
* Public functions
*/
function OpenSTUtilityMock(
uint256 _chainIdValue,
uint256 _chainIdUtility,
address _registrar)
OpenSTUtility(_chainIdValue, _chainIdUtility, _registrar)
public { }

function blocksToWaitLong() public pure returns (uint256) {
return BLOCKS_TO_WAIT_LONG;
}

function blocksToWaitShort() public pure returns (uint256) {
return BLOCKS_TO_WAIT_SHORT;
}
}
16 changes: 12 additions & 4 deletions contracts/OpenSTValue.sol
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,9 @@ contract OpenSTValue is OpsManaged, Hasher {
uint8 public constant TOKEN_DECIMALS = 18;
uint256 public constant DECIMALSFACTOR = 10**uint256(TOKEN_DECIMALS);
// ~2 weeks, assuming ~15s per block
uint256 public constant BLOCKS_TO_WAIT_LONG = 80667;
uint256 private constant BLOCKS_TO_WAIT_LONG = 80667;
// ~1hour, assuming ~15s per block
uint256 public constant BLOCKS_TO_WAIT_SHORT = 240;
uint256 private constant BLOCKS_TO_WAIT_SHORT = 240;

/*
* Structures
Expand Down Expand Up @@ -184,7 +184,7 @@ contract OpenSTValue is OpsManaged, Hasher {
require(valueToken.transferFrom(tx.origin, address(this), _amountST));

amountUT = _amountST.mul(utilityToken.conversionRate);
unlockHeight = block.number + BLOCKS_TO_WAIT_LONG;
unlockHeight = block.number + blocksToWaitLong();

nonces[tx.origin]++;
nonce = nonces[tx.origin];
Expand Down Expand Up @@ -315,7 +315,7 @@ contract OpenSTValue is OpsManaged, Hasher {

require(_redemptionIntentHash == redemptionIntentHash);

expirationHeight = block.number + BLOCKS_TO_WAIT_SHORT;
expirationHeight = block.number + blocksToWaitShort();

UtilityToken storage utilityToken = utilityTokens[_uuid];
// minimal precision to unstake 1 STWei
Expand Down Expand Up @@ -442,6 +442,14 @@ contract OpenSTValue is OpsManaged, Hasher {
utilityToken.stakingAccount);
}

function blocksToWaitLong() public pure returns (uint256) {
return BLOCKS_TO_WAIT_LONG;
}

function blocksToWaitShort() public pure returns (uint256) {
return BLOCKS_TO_WAIT_SHORT;
}

/*
* Registrar functions
*/
Expand Down
49 changes: 49 additions & 0 deletions contracts/OpenSTValueMock.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
pragma solidity ^0.4.17;

// Copyright 2017 OpenST Ltd.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// ----------------------------------------------------------------------------
// Value chain: OpenSTValueMock.sol
//
// http://www.simpletoken.org/
//
// ----------------------------------------------------------------------------

import "./OpenSTValue.sol";

/// @title OpenSTValueMock
/// @dev Overrides certain durational constants and getters to ease testing OpenSTValue
contract OpenSTValueMock is OpenSTValue {
uint256 private constant BLOCKS_TO_WAIT_LONG = 8;
uint256 private constant BLOCKS_TO_WAIT_SHORT = 5;

/*
* Public functions
*/
function OpenSTValueMock(
uint256 _chainIdValue,
EIP20Interface _eip20token,
address _registrar)
OpenSTValue(_chainIdValue, _eip20token, _registrar)
public { }

function blocksToWaitLong() public pure returns (uint256) {
return BLOCKS_TO_WAIT_LONG;
}

function blocksToWaitShort() public pure returns (uint256) {
return BLOCKS_TO_WAIT_SHORT;
}
}
36 changes: 31 additions & 5 deletions test/OpenSTUtility_utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -197,9 +197,35 @@ module.exports.checkProcessedRedemptionEvent = (event, _uuid, _redemptionIntentH
}

assert.equal(event.event, "ProcessedRedemption");
assert.equal(event.args._uuid, _uuid)
assert.equal(event.args._redemptionIntentHash, _redemptionIntentHash)
assert.equal(event.args._token, _token)
assert.equal(event.args._redeemer, _redeemer)
assert.equal(event.args._amount.toNumber(), _amount.toNumber())
assert.equal(event.args._uuid, _uuid);
assert.equal(event.args._redemptionIntentHash, _redemptionIntentHash);
assert.equal(event.args._token, _token);
assert.equal(event.args._redeemer, _redeemer);
assert.equal(event.args._amount.toNumber(), _amount.toNumber());
}

module.exports.checkRevertedMintEvent = (event, _uuid, _stakingIntentHash, _staker, _beneficiary, _amount) => {
if (Number.isInteger(_amount)) {
_amount = new BigNumber(_amount);
}

assert.equal(event.event, "RevertedMint");
assert.equal(event.args._uuid, _uuid)
assert.equal(event.args._stakingIntentHash, _stakingIntentHash)
assert.equal(event.args._staker, _staker)
assert.equal(event.args._beneficiary, _beneficiary)
assert.equal(event.args._amountUT.toNumber(), _amount.toNumber())
}

module.exports.checkRevertedRedemption = (event, _uuid, _redemptionIntentHash, _redeemer, _amountUT) => {
if (Number.isInteger(_amountUT)) {
_amount = new BigNumber(_amountUT);
}

assert.equal(event.event, "RevertedRedemption");
assert.equal(event.args._uuid, _uuid);
assert.equal(event.args._redemptionIntentHash, _redemptionIntentHash);
assert.equal(event.args._redeemer, _redeemer);
assert.equal(event.args._amountUT.toNumber(), _amountUT.toNumber());

}
64 changes: 32 additions & 32 deletions test/OpenSTValue_utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,38 +118,6 @@ module.exports.checkStakingIntentDeclaredEvent = (event, _uuid, _staker, _staker
assert.equal(event.args._chainIdUtility, _chainIdUtility);
}


module.exports.checkStakingIntentDeclaredEventProtocol = (formattedDecodedEvents, _uuid, _staker, _stakerNonce, _beneficiary,
_amountST, _amountUT, _chainIdUtility) => {

var event = formattedDecodedEvents['StakingIntentDeclared'];

if (Number.isInteger(_stakerNonce)) {
_stakerNonce = new BigNumber(_stakerNonce);
}

if (Number.isInteger(_amountST)) {
_amountST = new BigNumber(_amountST);
}

if (Number.isInteger(_amountUT)) {
_amountUT = new BigNumber(_amountUT);
}

if (Number.isInteger(_chainIdUtility)) {
_chainIdUtility = new BigNumber(_chainIdUtility);
}

assert.equal(event.event, "StakingIntentDeclared");
assert.equal(event._uuid, _uuid);
assert.equal(event._staker, _staker);
assert.equal(event._stakerNonce, _stakerNonce.toNumber());
assert.equal(event._beneficiary, _beneficiary);
assert.equal(event._amountST, _amountST.toNumber());
assert.equal(event._amountUT, _amountUT.toNumber());
assert.equal(event._chainIdUtility, _chainIdUtility.toNumber());
}

module.exports.checkStakingIntentDeclaredEventProtocol = (event, _uuid, _staker, _stakerNonce, _beneficiary,
_amountST, _amountUT, _chainIdUtility) => {

Expand Down Expand Up @@ -252,4 +220,36 @@ module.exports.checkProcessedUnstakeEvent = (event, _uuid, _redemptionIntentHash
assert.equal(event.args.stake, stake);
assert.equal(event.args._redeemer, _redeemer);
assert.equal(event.args._amountST.toNumber(), _amountST.toNumber());
}

module.exports.checkRevertStakingEventProtocol = (event, _uuid, _stakingIntentHash, _staker, _amountST, _amountUT) => {

if (Number.isInteger(_amountUT)) {
_amountUT = new BigNumber(_amountUT);
}

if (Number.isInteger(_amountST)) {
_amountST = new BigNumber(_amountST);
}

assert.equal(event.event, "RevertedStake");
assert.equal(event.args._uuid, _uuid);
assert.equal(event.args._staker, _staker);
assert.equal(event.args._stakingIntentHash, _stakingIntentHash);
assert.equal(event.args._amountST, _amountST.toNumber());
assert.equal(event.args._amountUT, _amountUT.toNumber());
}

module.exports.checkRevertedUnstake = (event, _uuid, _redemptionIntentHash, _redeemer, _amountST) => {

if (Number.isInteger(_amountST)) {
_amountST = new BigNumber(_amountST);
}

assert.equal(event.event, "RevertedUnstake");
assert.equal(event.args._uuid, _uuid);
assert.equal(event.args._redemptionIntentHash, _redemptionIntentHash);
assert.equal(event.args._redeemer, _redeemer);
assert.equal(event.args._amountST, _amountST.toNumber());

}
Loading

0 comments on commit 8cc829e

Please sign in to comment.