Skip to content
This repository has been archived by the owner on May 9, 2024. It is now read-only.

Commit

Permalink
Updates event structure (#190)
Browse files Browse the repository at this point in the history
  • Loading branch information
ansermino authored Jun 29, 2020
1 parent 56412ec commit 849db56
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 35 deletions.
56 changes: 27 additions & 29 deletions contracts/Bridge.sol
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,15 @@ contract Bridge is Pausable, AccessControl, SafeMath {

enum Vote {No, Yes}

enum ProposalStatus {Inactive, Active, Passed, Transferred, Cancelled}
enum ProposalStatus {Inactive, Active, Passed, Executed, Cancelled}

struct Proposal {
bytes32 _resourceID;
bytes32 _dataHash;
address[] _yesVotes;
address[] _noVotes;
bytes32 _resourceID;
bytes32 _dataHash;
address[] _yesVotes;
address[] _noVotes;
ProposalStatus _status;
uint256 _proposedBlock;
uint256 _proposedBlock;
}

// destinationChainID => number of deposits
Expand All @@ -58,19 +58,17 @@ contract Bridge is Pausable, AccessControl, SafeMath {
uint8 indexed originChainID,
uint64 indexed depositNonce,
ProposalStatus indexed status,
bytes32 resourceID,
bytes32 dataHash
bytes32 resourceID,
bytes32 dataHash
);

event ProposalVote(
uint8 indexed originChainID,
uint64 indexed depositNonce,
bytes32 resourceID,
ProposalStatus status
ProposalStatus indexed status,
bytes32 resourceID
);



bytes32 public constant RELAYER_ROLE = keccak256("RELAYER_ROLE");

modifier onlyAdmin() {
Expand All @@ -90,7 +88,7 @@ contract Bridge is Pausable, AccessControl, SafeMath {

function _onlyAdminOrRelayer() private {
require(hasRole(DEFAULT_ADMIN_ROLE, msg.sender) || hasRole(RELAYER_ROLE, msg.sender),
"sender is not relayer or admin");
"sender is not relayer or admin");
}

function _onlyAdmin() private {
Expand Down Expand Up @@ -312,7 +310,7 @@ contract Bridge is Pausable, AccessControl, SafeMath {
@param chainID ID of chain deposit originated from.
@param depositNonce ID of deposited generated by origin Bridge contract.
@param dataHash Hash of data provided when deposit was made.
@notice Proposal must not have already been passed or transferred.
@notice Proposal must not have already been passed or executed.
@notice {msg.sender} must not have already voted on proposal.
@notice Emits {ProposalEvent} event with status indicating the proposal status.
@notice Emits {ProposalVote} event.
Expand All @@ -323,18 +321,18 @@ contract Bridge is Pausable, AccessControl, SafeMath {
Proposal storage proposal = _proposals[nonceAndID][dataHash];

require(_resourceIDToHandlerAddress[resourceID] != address(0), "no handler for resourceID");
require(uint(proposal._status) <= 1, "proposal already passed/transferred/cancelled");
require(uint(proposal._status) <= 1, "proposal already passed/executed/cancelled");
require(!_hasVotedOnProposal[nonceAndID][dataHash][msg.sender], "relayer already voted");

if (uint(proposal._status) == 0) {
++_totalProposals;
_proposals[nonceAndID][dataHash] = Proposal({
_resourceID: resourceID,
_dataHash: dataHash,
_yesVotes: new address[](1),
_noVotes: new address[](0),
_status: ProposalStatus.Active,
_proposedBlock: block.number
_resourceID : resourceID,
_dataHash : dataHash,
_yesVotes : new address[](1),
_noVotes : new address[](0),
_status : ProposalStatus.Active,
_proposedBlock : block.number
});

proposal._yesVotes[0] = msg.sender;
Expand All @@ -355,13 +353,13 @@ contract Bridge is Pausable, AccessControl, SafeMath {
}
if (proposal._status != ProposalStatus.Cancelled) {
_hasVotedOnProposal[nonceAndID][dataHash][msg.sender] = true;
emit ProposalVote(chainID, depositNonce, resourceID, proposal._status);
emit ProposalVote(chainID, depositNonce, proposal._status, resourceID);

// If _depositThreshold is set to 1, then auto finalize
// or if _relayerThreshold has been exceeded
if (_relayerThreshold <= 1 || proposal._yesVotes.length >= _relayerThreshold) {
proposal._status = ProposalStatus.Passed;

emit ProposalEvent(chainID, depositNonce, ProposalStatus.Passed, resourceID, dataHash);
}
}
Expand All @@ -383,7 +381,7 @@ contract Bridge is Pausable, AccessControl, SafeMath {

require(proposal._status != ProposalStatus.Cancelled, "Proposal already cancelled");
require(sub(block.number, proposal._proposedBlock) > _expiry, "Proposal not at expiry threshold");

proposal._status = ProposalStatus.Cancelled;
emit ProposalEvent(chainID, depositNonce, ProposalStatus.Cancelled, proposal._resourceID, proposal._dataHash);

Expand All @@ -398,7 +396,7 @@ contract Bridge is Pausable, AccessControl, SafeMath {
@param data Data originally provided when deposit was made.
@notice Proposal must have Passed status.
@notice Hash of {data} must equal proposal's {dataHash}.
@notice Emits {ProposalEvent} event with status {Transferred}.
@notice Emits {ProposalEvent} event with status {Executed}.
*/
function executeProposal(uint8 chainID, uint64 depositNonce, bytes calldata data, bytes32 resourceID) external onlyRelayers whenNotPaused {
address handler = _resourceIDToHandlerAddress[resourceID];
Expand All @@ -410,12 +408,12 @@ contract Bridge is Pausable, AccessControl, SafeMath {
require(proposal._status == ProposalStatus.Passed, "proposal already transferred");
require(dataHash == proposal._dataHash, "data doesn't match datahash");

proposal._status = ProposalStatus.Transferred;
proposal._status = ProposalStatus.Executed;

IDepositExecute depositHandler = IDepositExecute(_resourceIDToHandlerAddress[proposal._resourceID]);
depositHandler.executeProposal(proposal._resourceID, data);

emit ProposalEvent(chainID, depositNonce, ProposalStatus.Transferred, proposal._resourceID, proposal._dataHash);
emit ProposalEvent(chainID, depositNonce, proposal._status, proposal._resourceID, proposal._dataHash);
}

/**
Expand All @@ -425,7 +423,7 @@ contract Bridge is Pausable, AccessControl, SafeMath {
@param amounts Array of amonuts to transfer to {addrs}.
*/
function transferFunds(address payable[] calldata addrs, uint[] calldata amounts) external onlyAdmin {
for (uint i = 0;i < addrs.length; i++) {
for (uint i = 0; i < addrs.length; i++) {
addrs[i].transfer(amounts[i]);
}
}
Expand Down
6 changes: 3 additions & 3 deletions test/contractBridge/cancelDepositProposal.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ contract('Bridge - [voteProposal with relayerThreshold == 3]', async (accounts)

const depositProposal = await BridgeInstance.getProposal(originChainID, expectedDepositNonce, depositDataHash);
assert.deepInclude(Object.assign({}, depositProposal), expectedDepositProposal);
await TruffleAssert.reverts(vote(relayer3Address), "proposal already passed/transferred/cancelled.")
await TruffleAssert.reverts(vote(relayer3Address), "proposal already passed/executed/cancelled.")

});

Expand All @@ -138,7 +138,7 @@ contract('Bridge - [voteProposal with relayerThreshold == 3]', async (accounts)
await TruffleAssert.passes(BridgeInstance.cancelProposal(originChainID, expectedDepositNonce, depositDataHash))
const depositProposal = await BridgeInstance.getProposal(originChainID, expectedDepositNonce, depositDataHash);
assert.deepInclude(Object.assign({}, depositProposal), expectedDepositProposal);
await TruffleAssert.reverts(vote(relayer4Address), "proposal already passed/transferred/cancelled.")
await TruffleAssert.reverts(vote(relayer4Address), "proposal already passed/executed/cancelled.")

});

Expand All @@ -159,7 +159,7 @@ contract('Bridge - [voteProposal with relayerThreshold == 3]', async (accounts)
await TruffleAssert.passes(BridgeInstance.cancelProposal(originChainID, expectedDepositNonce, depositDataHash))
const depositProposal = await BridgeInstance.getProposal(originChainID, expectedDepositNonce, depositDataHash);
assert.deepInclude(Object.assign({}, depositProposal), expectedDepositProposal);
await TruffleAssert.reverts(vote(relayer2Address), "proposal already passed/transferred/cancelled.")
await TruffleAssert.reverts(vote(relayer2Address), "proposal already passed/executed/cancelled.")

});

Expand Down
2 changes: 1 addition & 1 deletion test/contractBridge/createDepositProposal.js
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ contract('Bridge - [create a deposit proposal (voteProposal) with relayerThresho
assert.isTrue(hasVoted);
});

it('DepositProposalCreated event should be emitted with expected values', async () => {
it('DepositProposalCreated event should be emitted with expected values', async () => {
const proposalTx = await BridgeInstance.voteProposal(
originChainID,
expectedDepositNonce,
Expand Down
4 changes: 2 additions & 2 deletions test/contractBridge/voteDepositProposal.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ contract('Bridge - [voteProposal with relayerThreshold == 3]', async (accounts)

await TruffleAssert.passes(vote(relayer3Address));

await TruffleAssert.reverts(vote(relayer4Address), 'proposal already passed/transferred/cancelled.');
await TruffleAssert.reverts(vote(relayer4Address), 'proposal already passed/executed/cancelled.');
});

it("depositProposal shouldn't be voted on if it has a Transferred status", async () => {
Expand All @@ -118,7 +118,7 @@ contract('Bridge - [voteProposal with relayerThreshold == 3]', async (accounts)

await TruffleAssert.passes(executeProposal(relayer1Address));

await TruffleAssert.reverts(vote(relayer4Address), 'proposal already passed/transferred/cancelled.');
await TruffleAssert.reverts(vote(relayer4Address), 'proposal already passed/executed/cancelled.');

});

Expand Down

0 comments on commit 849db56

Please sign in to comment.