Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add getter function to get message status for a given message hash. #602

Merged
merged 10 commits into from
Jan 22, 2019
52 changes: 46 additions & 6 deletions contracts/gateway/GatewayBase.sol
Original file line number Diff line number Diff line change
Expand Up @@ -338,20 +338,60 @@ contract GatewayBase is Organized {
emit BountyChangeConfirmed(previousBountyAmount_, changedBountyAmount_);
}

/**
* @notice Method to get the outbox message status for the given message
* hash.
*
* @dev If message hash do not exists then it will return undeclared status.
deepesh-kn marked this conversation as resolved.
Show resolved Hide resolved
*
* @param _messageHash Message hash to get the status.
*
* @return status_ Message status.
*/
function getOutboxMessageStatus(
bytes32 _messageHash
)
external
view
returns (MessageBus.MessageStatus status_)
{
status_ = messageBox.outbox[_messageHash];
}

/**
* @notice Method to get the inbox message status for the given message
* hash.
*
* @dev If message hash do not exists then it will return undeclared status.
deepesh-kn marked this conversation as resolved.
Show resolved Hide resolved
*
* @param _messageHash Message hash to get the status.
*
* @return status_ Message status.
*/
function getInboxMessageStatus(
bytes32 _messageHash
)
external
view
returns (MessageBus.MessageStatus status_)
{
status_ = messageBox.inbox[_messageHash];
}


/* Internal Functions */

/**
* @notice Calculate the fee amount which is rewarded to facilitator for
* performing message transfers.
*
* @param _gasConsumed gas consumption during message confirmation.
* @param _gasLimit maximum amount of gas can be used for reward.
* @param _gasPrice price at which reward is calculated
* @param _initialGas initial gas at the start of the process
* @param _gasConsumed Gas consumption during message confirmation.
* @param _gasLimit Maximum amount of gas can be used for reward.
* @param _gasPrice Gas price at which reward is calculated.
* @param _initialGas Initial gas at the start of the process.
*
* @return fee amount
* @return totalGasConsumed_ total gas consumed during message transfer
* @return fee_ Fee amount.
* @return totalGasConsumed_ Total gas consumed during message transfer.
*/
function feeAmount(
uint256 _gasConsumed,
Expand Down
179 changes: 179 additions & 0 deletions contracts/test/gateway/TestGatewayBase.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,179 @@
pragma solidity ^0.5.0;

// Copyright 2019 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.
//
// ----------------------------------------------------------------------------
//
// http://www.simpletoken.org/
//
// ----------------------------------------------------------------------------

import "../../StateRootInterface.sol";
import "../../gateway/GatewayBase.sol";
import "../../lib/OrganizationInterface.sol";

/**
* @title TestGatewayBase contract.
*
* @notice Used for test only.
*/
contract TestGatewayBase is GatewayBase {

/* Constructor */

/**
* @notice This is used for testing.
*
* @param _stateRootProvider Contract address which implements
* StateRootInterface.
* @param _bounty The amount that facilitator will stakes to initiate the
* stake process.
deepesh-kn marked this conversation as resolved.
Show resolved Hide resolved
* @param _organization Address of a contract that manages workers.
*/
constructor(
StateRootInterface _stateRootProvider,
uint256 _bounty,
OrganizationInterface _organization
)
public
GatewayBase(
_stateRootProvider,
_bounty,
_organization
)
{}

/* external functions */

/**
* @notice It is used to set a message.
*
* @dev This is used for testing purpose.
*
* @param _intentHash Intent hash.
* @param _nonce Nonce of the message sender address.
* @param _gasPrice Gas price that message sender is ready to pay to
* transfer message.
* @param _gasLimit Gas limit that message sender is ready to pay.
* @param _sender Message sender address.
* @param _hashLock Hash Lock provided by the facilitator.
*
* @return messageHash_ Hash unique for every request.
*/
function setMessage(
bytes32 _intentHash,
uint256 _nonce,
uint256 _gasPrice,
uint256 _gasLimit,
address _sender,
bytes32 _hashLock
)
external
returns (bytes32 messageHash_)
{
MessageBus.Message memory message = getMessage(
_intentHash,
_nonce,
_gasPrice,
_gasLimit,
_sender,
_hashLock
);

messageHash_ = MessageBus.messageDigest(
message.intentHash,
message.nonce,
message.gasPrice,
message.gasLimit,
message.sender,
message.hashLock
);

messages[messageHash_] = message;

}

/**
* @notice It sets the status of inbox.
*
* @dev This is used for testing purpose.
*
* @param _messageHash It sets the status of the message.
* @param _status It sets the state of the message.
*/
function setInboxStatus(
bytes32 _messageHash,
MessageBus.MessageStatus _status
)
external
{
messageBox.inbox[_messageHash] = _status;
}

/**
* @notice It sets the status of outbox.
*
* @dev This is used for testing purpose.
*
* @param _messageHash MessageHash for which status is the be set.
* @param _status Status of the message to be set.
*/
function setOutboxStatus(
bytes32 _messageHash,
MessageBus.MessageStatus _status
)
external
{
messageBox.outbox[_messageHash] = _status;
}

/**
* @notice It sets the message hash for active inbox process.
*
* @dev This is used for testing purpose.
*
* @param _account Account address.
* @param _nonce Nonce of account address.
* @param _messageHash MessageHash for which status is the be set.
*/
function setInboxProcess(
address _account,
uint256 _nonce,
bytes32 _messageHash
)
external
{
super.registerInboxProcess(_account, _nonce, _messageHash);
}

/**
* @notice It sets the message hash for active outbox process.
*
* @dev This is used for testing purpose.
*
* @param _account Account address.
* @param _nonce Nonce of account address.
* @param _messageHash MessageHash for which status is the be set.
*/
function setOutboxProcess(
address _account,
uint256 _nonce,
bytes32 _messageHash
)
external
{
super.registerOutboxProcess(_account, _nonce, _messageHash);
}
}
5 changes: 3 additions & 2 deletions migrations/2_deploy_contracts.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const GatewayBase = artifacts.require("./gateway/GatewayBase.sol");
const EIP20Gateway = artifacts.require("EIP20Gateway");
const MockGatewayLib = artifacts.require("MockGatewayLib");
const MockGatewayBase = artifacts.require("MockGatewayBase");
const TestGatewayBase = artifacts.require("TestGatewayBase");
const MetaBlock = artifacts.require("../contracts/lib/MetaBlock.sol");
const BlockStore = artifacts.require("../contracts/BlockStore.sol");
const TestEIP20Gateway = artifacts.require("TestEIP20Gateway");
Expand Down Expand Up @@ -37,8 +38,8 @@ module.exports = function (deployer) {
deployer.deploy(GatewayLib);
deployer.deploy(MockGatewayLib);
deployer.deploy(MetaBlock);
deployer.link(GatewayLib, [GatewayBase, EIP20Gateway, TestEIP20Gateway, EIP20CoGateway, TestEIP20CoGateway]);
deployer.link(MessageBus, [EIP20CoGateway, TestEIP20CoGateway, TestEIP20Gateway, EIP20Gateway]);
deployer.link(GatewayLib, [GatewayBase, TestGatewayBase, EIP20Gateway, TestEIP20Gateway, EIP20CoGateway, TestEIP20CoGateway]);
deployer.link(MessageBus, [EIP20CoGateway, TestEIP20CoGateway, TestEIP20Gateway, EIP20Gateway, TestGatewayBase]);
deployer.link(MockGatewayLib, [MockGatewayBase, TestEIP20Gateway]);
deployer.link(MetaBlock, [BlockStore, AuxiliaryBlockStore]);

Expand Down
91 changes: 91 additions & 0 deletions test/gateway/gateway_base/get_inbox_message_status.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
// Copyright 2019 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.
//
// ----------------------------------------------------------------------------
//
// http://www.simpletoken.org/
//
// ----------------------------------------------------------------------------

const GatewayBase = artifacts.require('./TestGatewayBase.sol');
const BN = require("bn.js");
const web3 = require('../../test_lib/web3.js');
const messageBus = require('../../test_lib/message_bus.js');
const MessageStatusEnum = messageBus.MessageStatusEnum;

contract('GatewayBase.getInboxMessageStatus()', function (accounts) {

let gatewayBase, messageHash;

beforeEach(async function () {

gatewayBase = await GatewayBase.new(
accounts[0],
new BN(100),
accounts[1],
);

messageHash = web3.utils.sha3("message_hash");

});

it('should return correct message status', async function () {

let status = await gatewayBase.getInboxMessageStatus(messageHash);

assert.strictEqual(
status.eqn(MessageStatusEnum.Undeclared),
true,
`Message status ${status.toString(10)} must be equal to ${MessageStatusEnum.Undeclared}`,
);

await gatewayBase.setInboxStatus(messageHash, MessageStatusEnum.Declared);
status = await gatewayBase.getInboxMessageStatus(messageHash);

assert.strictEqual(
status.eqn(MessageStatusEnum.Declared),
true,
`Message status ${status.toString(10)} must be equal to ${MessageStatusEnum.Declared}`,
);

await gatewayBase.setInboxStatus(messageHash, MessageStatusEnum.Progressed);
status = await gatewayBase.getInboxMessageStatus(messageHash);

assert.strictEqual(
status.eqn(MessageStatusEnum.Progressed),
true,
`Message status ${status.toString(10)} must be equal to ${MessageStatusEnum.Progressed}`,
);

await gatewayBase.setInboxStatus(messageHash, MessageStatusEnum.DeclaredRevocation);
status = await gatewayBase.getInboxMessageStatus(messageHash);

assert.strictEqual(
status.eqn(MessageStatusEnum.DeclaredRevocation),
true,
`Message status ${status.toString(10)} must be equal to ${MessageStatusEnum.DeclaredRevocation}`,
);

await gatewayBase.setInboxStatus(messageHash, MessageStatusEnum.Revoked);
status = await gatewayBase.getInboxMessageStatus(messageHash);

assert.strictEqual(
status.eqn(MessageStatusEnum.Revoked),
true,
`Message status ${status.toString(10)} must be equal to ${MessageStatusEnum.Revoked}`,
);

});

});
Loading