Skip to content

Commit

Permalink
test: refactor state setup
Browse files Browse the repository at this point in the history
  • Loading branch information
ChiTimesChi committed Oct 3, 2024
1 parent ad70449 commit 693b432
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 60 deletions.
16 changes: 2 additions & 14 deletions packages/contracts-rfq/test/FastBridgeV2.Dst.ArbitraryCall.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -20,20 +20,8 @@ contract FastBridgeV2DstArbitraryCallTest is FastBridgeV2DstExclusivityTest {

function createFixturesV2() public virtual override {
super.createFixturesV2();
tokenParamsV2.callParams = CALL_PARAMS;
ethParamsV2.callParams = CALL_PARAMS;
tokenTx.callParams = CALL_PARAMS;
ethTx.callParams = CALL_PARAMS;
}

function setTokenTestCallParams(bytes memory callParams) public {
tokenParamsV2.callParams = callParams;
tokenTx.callParams = callParams;
}

function setEthTestCallParams(bytes memory callParams) public {
ethParamsV2.callParams = callParams;
ethTx.callParams = callParams;
setTokenTestCallParams(CALL_PARAMS);
setEthTestCallParams(CALL_PARAMS);
}

/// @notice We override the "expect event" function to also check for the arbitrary call
Expand Down
10 changes: 2 additions & 8 deletions packages/contracts-rfq/test/FastBridgeV2.GasBench.Dst.Excl.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,7 @@ contract FastBridgeV2DstExclusivityTest is FastBridgeV2DstGasBenchmarkTest {
}

function createFixturesV2() public virtual override {
tokenParamsV2.quoteRelayer = relayerA;
tokenParamsV2.quoteExclusivitySeconds = int256(EXCLUSIVITY_PERIOD);
ethParamsV2.quoteRelayer = relayerA;
ethParamsV2.quoteExclusivitySeconds = int256(EXCLUSIVITY_PERIOD);
tokenTx.exclusivityRelayer = relayerA;
tokenTx.exclusivityEndTime = block.timestamp + EXCLUSIVITY_PERIOD;
ethTx.exclusivityRelayer = relayerA;
ethTx.exclusivityEndTime = block.timestamp + EXCLUSIVITY_PERIOD;
setTokenTestExclusivityParams(relayerA, EXCLUSIVITY_PERIOD);
setEthTestExclusivityParams(relayerA, EXCLUSIVITY_PERIOD);
}
}
12 changes: 2 additions & 10 deletions packages/contracts-rfq/test/FastBridgeV2.GasBench.Src.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -104,11 +104,7 @@ contract FastBridgeV2GasBenchmarkSrcTest is FastBridgeV2SrcBaseTest {
}

function test_bridge_token_withExclusivity() public {
tokenParamsV2.quoteRelayer = relayerA;
tokenParamsV2.quoteExclusivitySeconds = int256(EXCLUSIVITY_PERIOD);
tokenParamsV2.quoteId = bytes("Created by Relayer A");
tokenTx.exclusivityRelayer = relayerA;
tokenTx.exclusivityEndTime = block.timestamp + EXCLUSIVITY_PERIOD;
setTokenTestExclusivityParams(relayerA, EXCLUSIVITY_PERIOD);
bridge({caller: userA, msgValue: 0, params: tokenParams, paramsV2: tokenParamsV2});
assertEq(fastBridge.bridgeStatuses(getTxId(tokenTx)), IFastBridgeV2.BridgeStatus.REQUESTED);
assertEq(srcToken.balanceOf(userA), initialUserBalanceToken - tokenParams.originAmount);
Expand Down Expand Up @@ -185,11 +181,7 @@ contract FastBridgeV2GasBenchmarkSrcTest is FastBridgeV2SrcBaseTest {
}

function test_bridge_eth_withExclusivity() public {
ethParamsV2.quoteRelayer = relayerA;
ethParamsV2.quoteExclusivitySeconds = int256(EXCLUSIVITY_PERIOD);
ethParamsV2.quoteId = bytes("Created by Relayer A");
ethTx.exclusivityRelayer = relayerA;
ethTx.exclusivityEndTime = block.timestamp + EXCLUSIVITY_PERIOD;
setEthTestExclusivityParams(relayerA, EXCLUSIVITY_PERIOD);
bridge({caller: userA, msgValue: ethParams.originAmount, params: ethParams, paramsV2: ethParamsV2});
assertEq(fastBridge.bridgeStatuses(getTxId(ethTx)), IFastBridgeV2.BridgeStatus.REQUESTED);
assertEq(userA.balance, initialUserBalanceEth - ethParams.originAmount);
Expand Down
16 changes: 6 additions & 10 deletions packages/contracts-rfq/test/FastBridgeV2.Src.ArbitraryCall.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -9,39 +9,35 @@ contract FastBridgeV2SrcArbitraryCallTest is FastBridgeV2SrcExclusivityTest {

function createFixturesV2() public virtual override {
super.createFixturesV2();
tokenParamsV2.callParams = CALL_PARAMS;
ethParamsV2.callParams = CALL_PARAMS;
tokenTx.callParams = CALL_PARAMS;
ethTx.callParams = CALL_PARAMS;
setTokenTestCallParams(CALL_PARAMS);
setEthTestCallParams(CALL_PARAMS);
}

// Contract should accept callParams with length up to 2^16 - 1,
// so that the callParams.length is encoded in 2 bytes.

function test_bridge_token_callParamsLengthMax() public {
bytes memory callParams = new bytes(2 ** 16 - 1);
tokenParamsV2.callParams = callParams;
tokenTx.callParams = callParams;
setTokenTestCallParams(callParams);
test_bridge_token();
}

function test_bridge_eth_callParamsLengthMax() public {
bytes memory callParams = new bytes(2 ** 16 - 1);
ethParamsV2.callParams = callParams;
ethTx.callParams = callParams;
setEthTestCallParams(callParams);
test_bridge_eth();
}

function test_bridge_token_revert_callParamsLengthAboveMax() public {
bytes memory callParams = new bytes(2 ** 16);
tokenParamsV2.callParams = callParams;
setTokenTestCallParams(callParams);
vm.expectRevert(CallParamsLengthAboveMax.selector);
bridge({caller: userA, msgValue: 0, params: tokenParams, paramsV2: tokenParamsV2});
}

function test_bridge_eth_revert_callParamsLengthAboveMax() public {
bytes memory callParams = new bytes(2 ** 16);
ethParamsV2.callParams = callParams;
setEthTestCallParams(callParams);
vm.expectRevert(CallParamsLengthAboveMax.selector);
bridge({caller: userA, msgValue: ethParams.originAmount, params: ethParams, paramsV2: ethParamsV2});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,13 @@ contract FastBridgeV2SrcExclusivityNegativeTest is FastBridgeV2SrcTest {
uint256 public constant EXCLUSIVITY_PERIOD_ABS = 60 seconds;

function createFixturesV2() public virtual override {
tokenParamsV2.quoteRelayer = relayerA;
// Populate the fields using the absolute exclusivity period
setTokenTestExclusivityParams(relayerA, EXCLUSIVITY_PERIOD_ABS);
setEthTestExclusivityParams(relayerB, EXCLUSIVITY_PERIOD_ABS);
// Override with negative exclusivity period
tokenParamsV2.quoteExclusivitySeconds = -int256(EXCLUSIVITY_PERIOD_ABS);
tokenParamsV2.quoteId = bytes("Created by Relayer A");
ethParamsV2.quoteRelayer = relayerB;
ethParamsV2.quoteExclusivitySeconds = -int256(EXCLUSIVITY_PERIOD_ABS);
ethParamsV2.quoteId = bytes("Created by Relayer B");

tokenTx.exclusivityRelayer = relayerA;
tokenTx.exclusivityEndTime = block.timestamp - EXCLUSIVITY_PERIOD_ABS;
ethTx.exclusivityRelayer = relayerB;
ethTx.exclusivityEndTime = block.timestamp - EXCLUSIVITY_PERIOD_ABS;
}

Expand Down
13 changes: 2 additions & 11 deletions packages/contracts-rfq/test/FastBridgeV2.Src.Exclusivity.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,8 @@ contract FastBridgeV2SrcExclusivityTest is FastBridgeV2SrcTest {
uint256 public constant EXCLUSIVITY_PERIOD = 60 seconds;

function createFixturesV2() public virtual override {
tokenParamsV2.quoteRelayer = relayerA;
tokenParamsV2.quoteExclusivitySeconds = int256(EXCLUSIVITY_PERIOD);
tokenParamsV2.quoteId = bytes("Created by Relayer A");
ethParamsV2.quoteRelayer = relayerB;
ethParamsV2.quoteExclusivitySeconds = int256(EXCLUSIVITY_PERIOD);
ethParamsV2.quoteId = bytes("Created by Relayer B");

tokenTx.exclusivityRelayer = relayerA;
tokenTx.exclusivityEndTime = block.timestamp + EXCLUSIVITY_PERIOD;
ethTx.exclusivityRelayer = relayerB;
ethTx.exclusivityEndTime = block.timestamp + EXCLUSIVITY_PERIOD;
setTokenTestExclusivityParams(relayerA, EXCLUSIVITY_PERIOD);
setEthTestExclusivityParams(relayerB, EXCLUSIVITY_PERIOD);
}

function bridge(address caller, uint256 msgValue, IFastBridge.BridgeParams memory params) public virtual override {
Expand Down
38 changes: 38 additions & 0 deletions packages/contracts-rfq/test/FastBridgeV2.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,34 @@ abstract contract FastBridgeV2Test is Test, IFastBridgeV2Errors {
txV2.nonce = txV1.nonce;
}

function setTokenTestCallParams(bytes memory callParams) public {
tokenParamsV2.callParams = callParams;
tokenTx.callParams = callParams;
}

function setTokenTestExclusivityParams(address relayer, uint256 exclusivitySeconds) public {
tokenParamsV2.quoteRelayer = relayer;
tokenParamsV2.quoteExclusivitySeconds = int256(exclusivitySeconds);
tokenParamsV2.quoteId = bytes.concat("Token:", getMockQuoteId(relayer));

tokenTx.exclusivityRelayer = relayer;
tokenTx.exclusivityEndTime = block.timestamp + exclusivitySeconds;
}

function setEthTestCallParams(bytes memory callParams) public {
ethParamsV2.callParams = callParams;
ethTx.callParams = callParams;
}

function setEthTestExclusivityParams(address relayer, uint256 exclusivitySeconds) public {
ethParamsV2.quoteRelayer = relayer;
ethParamsV2.quoteExclusivitySeconds = int256(exclusivitySeconds);
ethParamsV2.quoteId = bytes.concat("ETH:", getMockQuoteId(relayer));

ethTx.exclusivityRelayer = relayer;
ethTx.exclusivityEndTime = block.timestamp + exclusivitySeconds;
}

function extractV1(IFastBridgeV2.BridgeTransactionV2 memory txV2)
public
pure
Expand All @@ -182,6 +210,16 @@ abstract contract FastBridgeV2Test is Test, IFastBridgeV2Errors {
txV1.nonce = txV2.nonce;
}

function getMockQuoteId(address relayer) public view returns (bytes memory) {
if (relayer == relayerA) {
return bytes("created by Relayer A");
} else if (relayer == relayerB) {
return bytes("created by Relayer B");
} else {
return bytes("created by unknown relayer");
}
}

function getTxId(IFastBridgeV2.BridgeTransactionV2 memory bridgeTx) public pure returns (bytes32) {
return keccak256(abi.encode(bridgeTx));
}
Expand Down

0 comments on commit 693b432

Please sign in to comment.