Skip to content

Commit

Permalink
Use timestamp instead of epoch
Browse files Browse the repository at this point in the history
  • Loading branch information
NindoK committed Jan 29, 2025
1 parent b57c54e commit 5ef0a60
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 16 deletions.
7 changes: 4 additions & 3 deletions overridden_contracts/src/Gateway.sol
Original file line number Diff line number Diff line change
Expand Up @@ -529,7 +529,7 @@ contract Gateway is IOGateway, IInitializable, IUpgradable {
}

(
uint256 epoch,
uint256 timestamp,
uint256 eraIndex,
uint256 totalPointsToken,
uint256 totalTokensInflated,
Expand All @@ -540,15 +540,16 @@ contract Gateway is IOGateway, IInitializable, IUpgradable {
Assets.mintForeignToken(foreignTokenId, middlewareAddress, totalTokensInflated);

address tokenAddress = Assets.tokenAddressOf(foreignTokenId);
uint48 epoch = IMiddlewareBasic(middlewareAddress).getEpochAtTs(uint48(timestamp));
try IMiddlewareBasic(middlewareAddress).distributeRewards(
epoch, eraIndex, totalPointsToken, totalTokensInflated, rewardsRoot, tokenAddress
) {} catch Error(string memory err) {
revert EUnableToProcessRewardsS(
epoch, eraIndex, tokenAddress, totalPointsToken, totalTokensInflated, rewardsRoot, err
timestamp, eraIndex, tokenAddress, totalPointsToken, totalTokensInflated, rewardsRoot, err
);
} catch (bytes memory err) {
revert EUnableToProcessRewardsB(
epoch, eraIndex, tokenAddress, totalPointsToken, totalTokensInflated, rewardsRoot, err
timestamp, eraIndex, tokenAddress, totalPointsToken, totalTokensInflated, rewardsRoot, err
);
}
}
Expand Down
33 changes: 20 additions & 13 deletions overridden_contracts/test/override_test/Gateway.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ contract GatewayTest is Test {
bytes32 public dotTokenID;

uint256 public constant SLASH_FRACTION = 500_000;
uint256 public constant ONE_DAY = 86400;

ChannelID internal constant PRIMARY_GOVERNANCE_CHANNEL_ID = ChannelID.wrap(bytes32(uint256(1)));
ChannelID internal constant SECONDARY_GOVERNANCE_CHANNEL_ID = ChannelID.wrap(bytes32(uint256(2)));
Expand Down Expand Up @@ -182,7 +183,7 @@ contract GatewayTest is Test {
}

function _makeReportRewardsCommand() public returns (Command, bytes memory, address) {
uint256 epoch = 0;
uint256 timestamp = ONE_DAY * 3;
uint256 eraIndex = 1;
uint256 totalPointsToken = 1 ether;
uint256 tokensInflatedToken = 1 ether;
Expand All @@ -200,7 +201,7 @@ contract GatewayTest is Test {

return (
Command.ReportRewards,
abi.encode(epoch, eraIndex, totalPointsToken, tokensInflatedToken, rewardsRoot, foreignTokenId),
abi.encode(timestamp, eraIndex, totalPointsToken, tokensInflatedToken, rewardsRoot, foreignTokenId),
tokenAddress
);
}
Expand Down Expand Up @@ -413,12 +414,14 @@ contract GatewayTest is Test {
bytes memory expectedError = bytes("no process slash");

// We mock the call so that it reverts
vm.mockCallRevert(address(1), abi.encodeWithSelector(IMiddlewareBasic.slash.selector), "no process slash");
vm.mockCallRevert(
address(middleware), abi.encodeWithSelector(IMiddlewareBasic.slash.selector), "no process slash"
);

// We mock the call so that it does not revert, but it will revert in the previous one
vm.mockCall(address(1), abi.encodeWithSelector(IMiddlewareBasic.getEpochAtTs.selector), abi.encode(10));
vm.mockCall(address(middleware), abi.encodeWithSelector(IMiddlewareBasic.getEpochAtTs.selector), abi.encode(10));

IOGateway(address(gateway)).setMiddleware(address(1));
IOGateway(address(gateway)).setMiddleware(address(middleware));

IOGateway.Slash memory expectedSlash =
IOGateway.Slash({operatorKey: bytes32(uint256(1)), slashFraction: SLASH_FRACTION, timestamp: 1});
Expand All @@ -445,12 +448,12 @@ contract GatewayTest is Test {
(Command command, bytes memory params) = _makeReportSlashesCommand(SLASH_FRACTION);

// We mock the call so that it does not revert
vm.mockCall(address(1), abi.encodeWithSelector(IMiddlewareBasic.slash.selector), abi.encode(10));
vm.mockCall(address(middleware), abi.encodeWithSelector(IMiddlewareBasic.slash.selector), abi.encode(10));

// We mock the call so that it does not revert
vm.mockCall(address(1), abi.encodeWithSelector(IMiddlewareBasic.getEpochAtTs.selector), abi.encode(10));
vm.mockCall(address(middleware), abi.encodeWithSelector(IMiddlewareBasic.getEpochAtTs.selector), abi.encode(10));

IOGateway(address(gateway)).setMiddleware(address(1));
IOGateway(address(gateway)).setMiddleware(address(middleware));

// Since we are asserting all fields, the last one is a true, therefore meaning
// that the dispatch went through correctly
Expand All @@ -475,15 +478,15 @@ contract GatewayTest is Test {
}

function testDecodeRewards() public {
uint256 epoch = 123_456_789;
uint256 timestamp = 123_456_789;
uint256 eraIndex = 42;
uint256 totalPointsToken = 123_456_789_012_345;
uint256 tokensInflatedToken = 987_654_321_098;
bytes32 rewardsRoot = 0xb6e16d27ac5ab427a7f68900ac5559ce272dc6c37c82b3e052246c82244c50e4;
bytes32 foreignTokenId = 0x0101010101010101010101010101010101010101010101010101010101010101;

assertEq(
abi.encode(epoch, eraIndex, totalPointsToken, tokensInflatedToken, rewardsRoot, foreignTokenId),
abi.encode(timestamp, eraIndex, totalPointsToken, tokensInflatedToken, rewardsRoot, foreignTokenId),
TEST_VECTOR_REWARDS_DATA
);
}
Expand All @@ -497,7 +500,7 @@ contract GatewayTest is Test {
vm.mockCall(
address(middleware), abi.encodeWithSelector(IMiddlewareBasic.distributeRewards.selector), abi.encode(true)
);

vm.mockCall(address(middleware), abi.encodeWithSelector(IMiddlewareBasic.getEpochAtTs.selector), abi.encode(10));
IOGateway(address(gateway)).setMiddleware(address(middleware));

// Expect the gateway to emit `InboundMessageDispatched`
Expand Down Expand Up @@ -572,7 +575,9 @@ contract GatewayTest is Test {

IOGateway(address(gateway)).setMiddleware(address(middleware));

uint256 expectedEpoch = 0;
vm.mockCall(address(middleware), abi.encodeWithSelector(IMiddlewareBasic.getEpochAtTs.selector), abi.encode(10));

uint256 expectedTimestamp = ONE_DAY * 3;
uint256 expectedEraIndex = 1;
uint256 expectedTotalPointsToken = 1 ether;
uint256 expectedTotalTokensInflated = 1 ether;
Expand All @@ -582,7 +587,7 @@ contract GatewayTest is Test {
address expectedTokenAddress = MockGateway(address(gateway)).tokenAddressOf(expectedForeignTokenId);
bytes memory expectedBytes = abi.encodeWithSelector(
Gateway.EUnableToProcessRewardsB.selector,
expectedEpoch,
expectedTimestamp,
expectedEraIndex,
expectedTokenAddress,
expectedTotalPointsToken,
Expand Down Expand Up @@ -617,6 +622,8 @@ contract GatewayTest is Test {

IOGateway(address(gateway)).setMiddleware(address(middleware));

vm.mockCall(address(middleware), abi.encodeWithSelector(IMiddlewareBasic.getEpochAtTs.selector), abi.encode(10));

vm.expectEmit(true, true, true, true);
emit IGateway.InboundMessageDispatched(assetHubParaID.into(), 1, messageID, true);

Expand Down

0 comments on commit 5ef0a60

Please sign in to comment.