Skip to content

Commit

Permalink
Merge pull request #578 from deepesh-kn/gh_424
Browse files Browse the repository at this point in the history
Unit test cases for progressStakeWithProof
  • Loading branch information
0xsarvesh authored Jan 18, 2019
2 parents 826f63d + d5e3dd8 commit d3f6094
Show file tree
Hide file tree
Showing 13 changed files with 649 additions and 109 deletions.
22 changes: 11 additions & 11 deletions contracts/gateway/EIP20Gateway.sol
Original file line number Diff line number Diff line change
Expand Up @@ -445,30 +445,23 @@ contract EIP20Gateway is GatewayBase {
{
require(
_messageHash != bytes32(0),
"Message hash must not be zero"
"Message hash must not be zero."
);
require(
_rlpParentNodes.length > 0,
"RLP encoded parent nodes must not be zero"
"RLP encoded parent nodes must not be zero."
);

bytes32 storageRoot = storageRoots[_blockHeight];

require(
storageRoot != bytes32(0),
"Storage root must not be zero"
"Storage root must not be zero."
);

// Get the message object
MessageBus.Message storage message = messages[_messageHash];

(staker_, stakeAmount_) = progressStakeInternal(
_messageHash,
message,
bytes32(0),
true
);

MessageBus.progressOutboxWithProof(
messageBox,
message,
Expand All @@ -477,6 +470,14 @@ contract EIP20Gateway is GatewayBase {
storageRoot,
MessageBus.MessageStatus(_messageStatus)
);

(staker_, stakeAmount_) = progressStakeInternal(
_messageHash,
message,
bytes32(0),
true
);

}

/**
Expand Down Expand Up @@ -737,7 +738,6 @@ contract EIP20Gateway is GatewayBase {
* facilitator while initiating the redeem
*
* @return redeemer_ Redeemer address
* @return beneficiary_ Address to which the tokens will be transferred.
* @return redeemAmount_ Total amount for which the redeem was
* initiated. The reward amount is deducted from the
* total redeem amount and is given to the
Expand Down
66 changes: 34 additions & 32 deletions contracts/lib/MessageBus.sol
Original file line number Diff line number Diff line change
Expand Up @@ -258,21 +258,34 @@ library MessageBus {
external
returns (bytes32 messageHash_)
{
// The inbox message status must be either `Declared` or `Progressed`.
require(
_messageStatus == MessageStatus.Declared ||
_messageStatus == MessageStatus.Progressed,
"Message on target must be Declared or Progressed."
);

messageHash_ = messageDigest(_message);

// The message status must be `Declared` or DeclaredRevocation`.
require(
_messageBox.outbox[messageHash_] == MessageStatus.Declared ||
_messageBox.outbox[messageHash_] == MessageStatus.DeclaredRevocation,
"Message on source must be Declared."
);
if(_messageBox.outbox[messageHash_] == MessageStatus.Declared) {

/*
* The inbox message status of target must be either `Declared` or
* `Progressed` when outbox message status at source is `Declared`.
*/
require(
_messageStatus == MessageStatus.Declared ||
_messageStatus == MessageStatus.Progressed,
"Message on target must be Declared or Progressed."
);

} else if (_messageBox.outbox[messageHash_] == MessageStatus.DeclaredRevocation) {

/*
* The inbox message status of target must be either `Progressed`
* when outbox message status at source is `DeclaredRevocation`.
*/
require(
_messageStatus == MessageStatus.Progressed,
"Message on target must be Progressed."
);

} else {
revert("Status of message on source must be Declared or DeclareRevocation.");
}

bytes memory storagePath = bytes32ToBytes(
storageVariablePathForStruct(
Expand Down Expand Up @@ -683,31 +696,20 @@ library MessageBus {
pure
returns(bytes32 storagePath_)
{
if(_offset > 0){
_structPosition = _structPosition + _offset;
}

bytes memory indexBytes = BytesLib.leftPad(
bytes32ToBytes(
bytes32(uint256(_structPosition))
)
bytes32ToBytes(bytes32(uint256(_structPosition)))
);

bytes memory keyBytes = BytesLib.leftPad(bytes32ToBytes(_key));
bytes memory path = BytesLib.concat(keyBytes, indexBytes);

bytes32 structPath = keccak256(
abi.encodePacked(
keccak256(
abi.encodePacked(path)
)
)
storagePath_ = keccak256(
abi.encodePacked(keccak256(abi.encodePacked(path)))
);

if (_offset == 0) {
return structPath;
}
bytes32 storagePath;
uint8 offset = _offset;
assembly {
storagePath := add(structPath, offset)
}
storagePath_ = keccak256(abi.encodePacked(storagePath));
}

/**
Expand Down
17 changes: 17 additions & 0 deletions contracts/test/TestEIP20Gateway.sol
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,23 @@ contract TestEIP20Gateway is EIP20Gateway {
messageBox.outbox[_messageHash] = _status;
}

/**
* @notice It sets the storage root for given block height.
*
* @dev This is used for testing purpose.
*
* @param _blockHeight Mocked block height for testing.
* @param _storageRoot Mocked storage root for merkle proof testing.
*/
function setStorageRoot(
uint256 _blockHeight,
bytes32 _storageRoot
)
public
{
storageRoots[_blockHeight] = _storageRoot;
}

/**
* @notice It sets the status of inbox.
*
Expand Down
66 changes: 34 additions & 32 deletions contracts/test/lib/MockMessageBus.sol
Original file line number Diff line number Diff line change
Expand Up @@ -258,21 +258,34 @@ library MockMessageBus {
external
returns (bytes32 messageHash_)
{
// The inbox message status must be either `Declared` or `Progressed`.
require(
_messageStatus == MessageStatus.Declared ||
_messageStatus == MessageStatus.Progressed,
"Message on target must be Declared or Progressed."
);

messageHash_ = messageDigest(_message);

// The message status must be `Declared` or DeclaredRevocation`.
require(
_messageBox.outbox[messageHash_] == MessageStatus.Declared ||
_messageBox.outbox[messageHash_] == MessageStatus.DeclaredRevocation,
"Message on source must be Declared."
);
if(_messageBox.outbox[messageHash_] == MessageStatus.Declared) {

/*
* The inbox message status of target must be either `Declared` or
* `Progressed` when outbox message status at source is `Declared`.
*/
require(
_messageStatus == MessageStatus.Declared ||
_messageStatus == MessageStatus.Progressed,
"Message on target must be Declared or Progressed."
);

} else if (_messageBox.outbox[messageHash_] == MessageStatus.DeclaredRevocation) {

/*
* The inbox message status of target must be either `Progressed`
* when outbox message status at source is `DeclaredRevocation`.
*/
require(
_messageStatus == MessageStatus.Progressed,
"Message on target must be Progressed."
);

} else {
revert("Status of message on source must be Declared or DeclareRevocation.");
}

bytes memory storagePath = bytes32ToBytes(
storageVariablePathForStruct(
Expand Down Expand Up @@ -689,31 +702,20 @@ library MockMessageBus {
pure
returns(bytes32 storagePath_)
{
if(_offset > 0){
_structPosition = _structPosition + _offset;
}

bytes memory indexBytes = BytesLib.leftPad(
bytes32ToBytes(
bytes32(uint256(_structPosition))
)
bytes32ToBytes(bytes32(uint256(_structPosition)))
);

bytes memory keyBytes = BytesLib.leftPad(bytes32ToBytes(_key));
bytes memory path = BytesLib.concat(keyBytes, indexBytes);

bytes32 structPath = keccak256(
abi.encodePacked(
keccak256(
abi.encodePacked(path)
)
)
storagePath_ = keccak256(
abi.encodePacked(keccak256(abi.encodePacked(path)))
);

if (_offset == 0) {
return structPath;
}
bytes32 storagePath;
uint8 offset = _offset;
assembly {
storagePath := add(structPath, offset)
}
storagePath_ = keccak256(abi.encodePacked(storagePath));
}

/**
Expand Down
1 change: 1 addition & 0 deletions test/data/redeem_progressed_1.json

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions test/data/redeem_progressed_2.json

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions test/data/redeem_revoked_1.json

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions test/data/redeem_revoked_2.json

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions test/data/stake_progressed_1.json

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions test/data/stake_progressed_2.json

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions test/data/stake_revoked_2.json

Large diffs are not rendered by default.

Loading

0 comments on commit d3f6094

Please sign in to comment.