Skip to content

Commit

Permalink
more tests for payPacketFeeAsync and timeout fee
Browse files Browse the repository at this point in the history
  • Loading branch information
WenweiX committed Aug 18, 2023
1 parent 5484939 commit 85e21be
Showing 1 changed file with 42 additions and 37 deletions.
79 changes: 42 additions & 37 deletions test/fee.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -17,23 +17,46 @@ contract FeeTest is PacketSenderTest {

PacketFee[] packetFees;

modifier useFeeTestCases() {
// initialize an array of PacketFee with different values
packetFees.push(PacketFee(0, 0, 0));
packetFees.push(PacketFee(1, 0, 0));
packetFees.push(PacketFee(0, 1, 0));
packetFees.push(PacketFee(0, 0, 1));
packetFees.push(PacketFee(1, 1, 1));
packetFees.push(PacketFee(1, 2, 3));
packetFees.push(PacketFee(2, 2, 1));
packetFees.push(PacketFee(1, 1, 2));
packetFees.push(PacketFee(1, 2, 2));
packetFees.push(PacketFee(3, 2, 1));

for (uint i = 0; i < packetFees.length; i++) {
fee = packetFees[i];
_;
}
}

// pay extra packet fee after packet creation
function test_payPacketFeeAsync() public {
PacketFee memory accu = mergeFees(fee, fee);
function test_payPacketFeeAsync() public useFeeTestCases {
// merge fees to calculate expected packet fee
PacketFee memory extraFee = PacketFee(1, 2, 3);
PacketFee memory expectedFee = mergeFees(fee, extraFee);

for (uint64 index = 0; index < 3; index++) {
uint64 packetSeq = index + 1;
// create packet and pay escrow fee
sendPacket();

mars.greet{value: Ibc.calcEscrowFee(fee)}(
IbcDispatcher(dispatcher),
payloadStr,
// pay packet fee asynchronously
dispatcher.payPacketFeeAsync{value: Ibc.calcEscrowFee(extraFee)}(
address(mars),
channelId,
maxTimeout,
fee
sentPacket.sequence,
extraFee
);
dispatcher.payPacketFeeAsync{value: Ibc.calcEscrowFee(fee)}(address(mars), channelId, packetSeq, fee);

PacketFee memory packetFee = dispatcher.getTotalPacketFees(address(mars), channelId, packetSeq);
assertEq(keccak256(abi.encode(packetFee)), keccak256(abi.encode(accu)));
// check that the total packet fee is correct
PacketFee memory actualFee = dispatcher.getTotalPacketFees(address(mars), channelId, sentPacket.sequence);
assertEq(keccak256(abi.encode(actualFee)), keccak256(abi.encode(expectedFee)), 'Packet fee is incorrect');
}
}

Expand All @@ -49,25 +72,6 @@ contract FeeTest is PacketSenderTest {
);
}

modifier useFeeTestCases() {
// initialize an array of PacketFee with different values
packetFees.push(PacketFee(0, 0, 0));
packetFees.push(PacketFee(1, 0, 0));
packetFees.push(PacketFee(0, 1, 0));
packetFees.push(PacketFee(0, 0, 1));
packetFees.push(PacketFee(1, 1, 1));
packetFees.push(PacketFee(1, 2, 3));
packetFees.push(PacketFee(2, 2, 1));
packetFees.push(PacketFee(1, 1, 2));
packetFees.push(PacketFee(1, 2, 2));
packetFees.push(PacketFee(3, 2, 1));

for (uint i = 0; i < packetFees.length; i++) {
fee = packetFees[i];
_;
}
}

// claim ack fee on receving ack
function test_ack_fee() public useFeeTestCases {
// save balances of forward and reverse relayer payees; assert balances changes after ack
Expand Down Expand Up @@ -99,21 +103,22 @@ contract FeeTest is PacketSenderTest {
}

// claim timeout fee on receving timeout
function test_timeout_fee() public {
assertEq(address(forwardRelayerPayee).balance, 0);
assertEq(address(reverseRelayerPayee).balance, 0);
function test_timeout_fee() public useFeeTestCases {
uint balanceReverseRelayer1 = address(reverseRelayerPayee).balance;
address refundPayee = address(mars);
uint balanceRefund1 = refundPayee.balance;

vm.startPrank(relayer);
sendPacket();

uint balance1 = address(mars).balance;

vm.startPrank(reverseRelayerPayee, reverseRelayerPayee);
dispatcher.timeout(IbcReceiver(mars), sentPacket, validProof);

assertEq(address(reverseRelayerPayee).balance, fee.timeoutFee);
assertEq(address(reverseRelayerPayee).balance, fee.timeoutFee + balanceReverseRelayer1);
// no forward relayer is invovled in timeout case
assertEq(address(forwardRelayerPayee).balance, 0);
assertEq(refundPayee.balance, Ibc.timeoutRefundAmount(fee) + balanceRefund1);
assertEq(escrow.balance, 0);
assertEq(address(mars).balance, balance1 + Ibc.timeoutRefundAmount(fee));

// cannot claim timeout fee twice
vm.expectRevert();
Expand Down

0 comments on commit 85e21be

Please sign in to comment.