Skip to content

Commit

Permalink
Modfiy Gravity.sol to reserve event nonce 1
Browse files Browse the repository at this point in the history
Now that we are emitting validator set update events with an event nonce
we have an event that is using event nonce zero. This is a big problem
on the Cosmos side as the logic has all been developed with the
assumption that event nonce 0 is a special unused value.
  • Loading branch information
jkilpatr committed May 10, 2021
1 parent 054c46f commit 006a772
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 8 deletions.
6 changes: 4 additions & 2 deletions orchestrator/orchestrator/src/oracle_resync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -169,11 +169,11 @@ pub async fn get_last_checked_block(
// no other events can come before it, therefore either there's been a parsing error
// or no events have been submitted on this chain yet.
if valset.valset_nonce == 0 && last_event_nonce == 1u8.into() {
return latest_block;
return event.block_number.unwrap();
}
// if we're looking for a later event nonce and we find the deployment of the contract
// we must have failed to parse the event we're looking for. The oracle can not start
if valset.valset_nonce == 0 && last_event_nonce > 1u8.into() {
else if valset.valset_nonce == 0 && last_event_nonce > 1u8.into() {
panic!("Could not find the last event relayed by {}, Last Event nonce is {} but no event matching that could be found!", our_cosmos_address, last_event_nonce)
}
}
Expand All @@ -183,6 +183,8 @@ pub async fn get_last_checked_block(
current_block = end_search;
}

// we should exit above when we find the zero valset, if we have the wrong contract address through we could be at it a while as we go over
// the entire history to 'prove' it.
panic!("You have reached the end of block history without finding the Gravity contract deploy event! You must have the wrong contract address!");
}

Expand Down
4 changes: 3 additions & 1 deletion solidity/contracts/Gravity.sol
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@ contract Gravity is ReentrancyGuard {
mapping(address => uint256) public state_lastBatchNonces;
mapping(bytes32 => uint256) public state_invalidationMapping;
uint256 public state_lastValsetNonce = 0;
uint256 public state_lastEventNonce = 0;
// event nonce zero is reserved by the Cosmos module as a special
// value indicating that no events have yet been submitted
uint256 public state_lastEventNonce = 1;

// These are set once at initialization
bytes32 public state_gravityId;
Expand Down
2 changes: 1 addition & 1 deletion solidity/test/deployERC20.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ async function runTest(opts: {}) {
_name: 'Atom',
_symbol: 'ATOM',
_decimals: 6,
_eventNonce: BigNumber.from(1)
_eventNonce: BigNumber.from(2)
})


Expand Down
8 changes: 4 additions & 4 deletions solidity/test/sendToCosmos.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,11 @@ async function runTest(opts: {}) {
await signers[0].getAddress(),
ethers.utils.formatBytes32String("myCosmosAddress"),
1000,
1
2
);

expect((await testERC20.functions.balanceOf(gravity.address))[0]).to.equal(1000);
expect((await gravity.functions.state_lastEventNonce())[0]).to.equal(1);
expect((await gravity.functions.state_lastEventNonce())[0]).to.equal(2);



Expand All @@ -65,11 +65,11 @@ async function runTest(opts: {}) {
await signers[0].getAddress(),
ethers.utils.formatBytes32String("myCosmosAddress"),
1000,
2
3
);

expect((await testERC20.functions.balanceOf(gravity.address))[0]).to.equal(2000);
expect((await gravity.functions.state_lastEventNonce())[0]).to.equal(2);
expect((await gravity.functions.state_lastEventNonce())[0]).to.equal(3);
}

describe("sendToCosmos tests", function () {
Expand Down

0 comments on commit 006a772

Please sign in to comment.