Skip to content

Commit 307767f

Browse files
ams9198grantmike
authored andcommitted
[No ticket] Fast-follow to PR 19: improve fuzz tests (circlefin#20)
### Summary In PR 19 we added a bunch of fuzz-based tests for `MessageTransmitterV2`: https://github.com/circlefin/evm-cctp-contracts-private/pull/19. I realized that we were not capturing the condition where: `destinationCaller is 0, so we should use random addresses to call receiveMessage()`. Instead, we were always just spoofing the 0-address due to this line: https://github.com/circlefin/evm-cctp-contracts-private/blob/220b071ecf7d3c221dfd71ad6f8802b25611ac89/test/v2/MessageTransmitterV2.t.sol#L1039, which is equivalent to the tests for non-zero value destinationCallers. ### Changes This updates the fuzz tests to take in a random caller. If the destinationCaller is set, we spoof that address, otherwise we call with a random fuzzed address.
1 parent b3445db commit 307767f

File tree

1 file changed

+38
-20
lines changed

1 file changed

+38
-20
lines changed

test/v2/MessageTransmitterV2.t.sol

Lines changed: 38 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -626,7 +626,8 @@ contract MessageTransmitterV2Test is TestUtils {
626626
address _destinationCaller,
627627
uint32 _minFinalityThreshold,
628628
uint32 _finalityThresholdExecuted,
629-
bytes calldata _messageBody
629+
bytes calldata _messageBody,
630+
address _randomCaller
630631
) public {
631632
bytes memory _message = _formatMessageForReceive(
632633
version,
@@ -641,7 +642,7 @@ contract MessageTransmitterV2Test is TestUtils {
641642
_messageBody
642643
);
643644
bytes memory _signature = _sign1of1Message(_message);
644-
_receiveMessage(_message, _signature);
645+
_receiveMessage(_message, _signature, _randomCaller);
645646

646647
// Try again
647648
vm.prank(_destinationCaller);
@@ -690,7 +691,8 @@ contract MessageTransmitterV2Test is TestUtils {
690691
address _destinationCaller,
691692
uint32 _minFinalityThreshold,
692693
uint32 _finalityThresholdExecuted,
693-
bytes calldata _messageBody
694+
bytes calldata _messageBody,
695+
address _randomCaller
694696
) public {
695697
bytes memory _message = _formatMessageForReceive(
696698
version,
@@ -705,7 +707,7 @@ contract MessageTransmitterV2Test is TestUtils {
705707
_messageBody
706708
);
707709
bytes memory _signature = _sign1of1Message(_message);
708-
_receiveMessage(_message, _signature);
710+
_receiveMessage(_message, _signature, _randomCaller);
709711
}
710712

711713
function testReceiveMessage_succeedsWith2of2Signing(
@@ -716,7 +718,8 @@ contract MessageTransmitterV2Test is TestUtils {
716718
address _destinationCaller,
717719
uint32 _minFinalityThreshold,
718720
uint32 _finalityThresholdExecuted,
719-
bytes calldata _messageBody
721+
bytes calldata _messageBody,
722+
address _randomCaller
720723
) public {
721724
_setup2of2Multisig();
722725

@@ -733,7 +736,7 @@ contract MessageTransmitterV2Test is TestUtils {
733736
_messageBody
734737
);
735738
bytes memory _signature = _sign2OfNMultisigMessage(_message);
736-
_receiveMessage(_message, _signature);
739+
_receiveMessage(_message, _signature, _randomCaller);
737740
}
738741

739742
function testReceiveMessage_succeedsWith2of3Signing(
@@ -744,7 +747,8 @@ contract MessageTransmitterV2Test is TestUtils {
744747
address _destinationCaller,
745748
uint32 _minFinalityThreshold,
746749
uint32 _finalityThresholdExecuted,
747-
bytes calldata _messageBody
750+
bytes calldata _messageBody,
751+
address _randomCaller
748752
) public {
749753
_setup2of3Multisig();
750754

@@ -761,7 +765,7 @@ contract MessageTransmitterV2Test is TestUtils {
761765
_messageBody
762766
);
763767
bytes memory _signature = _sign2OfNMultisigMessage(_message);
764-
_receiveMessage(_message, _signature);
768+
_receiveMessage(_message, _signature, _randomCaller);
765769
}
766770

767771
function testReceiveMessage_succeedsWithFinalizedMessage(
@@ -772,7 +776,8 @@ contract MessageTransmitterV2Test is TestUtils {
772776
address _destinationCaller,
773777
uint32 _minFinalityThreshold,
774778
uint32 _finalityThresholdExecuted,
775-
bytes calldata _messageBody
779+
bytes calldata _messageBody,
780+
address _randomCaller
776781
) public {
777782
vm.assume(
778783
_finalityThresholdExecuted >=
@@ -791,7 +796,7 @@ contract MessageTransmitterV2Test is TestUtils {
791796
_messageBody
792797
);
793798
bytes memory _signature = _sign1of1Message(_message);
794-
_receiveMessage(_message, _signature);
799+
_receiveMessage(_message, _signature, _randomCaller);
795800
}
796801

797802
function testReceiveMessage_succeedsWithUnfinalizedMessage(
@@ -802,7 +807,8 @@ contract MessageTransmitterV2Test is TestUtils {
802807
address _destinationCaller,
803808
uint32 _minFinalityThreshold,
804809
uint32 _finalityThresholdExecuted,
805-
bytes calldata _messageBody
810+
bytes calldata _messageBody,
811+
address _randomCaller
806812
) public {
807813
vm.assume(
808814
_finalityThresholdExecuted <
@@ -821,7 +827,7 @@ contract MessageTransmitterV2Test is TestUtils {
821827
_messageBody
822828
);
823829
bytes memory _signature = _sign1of1Message(_message);
824-
_receiveMessage(_message, _signature);
830+
_receiveMessage(_message, _signature, _randomCaller);
825831
}
826832

827833
function testReceiveMessage_succeedsWithNonZeroDestinationCaller(
@@ -848,7 +854,7 @@ contract MessageTransmitterV2Test is TestUtils {
848854
_messageBody
849855
);
850856
bytes memory _signature = _sign1of1Message(_message);
851-
_receiveMessage(_message, _signature);
857+
_receiveMessage(_message, _signature, _destinationCaller);
852858
}
853859

854860
function testReceiveMessage_succeedsWithZeroDestinationCaller(
@@ -858,8 +864,10 @@ contract MessageTransmitterV2Test is TestUtils {
858864
address _recipient,
859865
uint32 _minFinalityThreshold,
860866
uint32 _finalityThresholdExecuted,
861-
bytes calldata _messageBody
867+
bytes calldata _messageBody,
868+
address _randomCaller
862869
) public {
870+
vm.assume(_randomCaller != address(0));
863871
bytes memory _message = _formatMessageForReceive(
864872
version,
865873
_sourceDomain,
@@ -873,7 +881,7 @@ contract MessageTransmitterV2Test is TestUtils {
873881
_messageBody
874882
);
875883
bytes memory _signature = _sign1of1Message(_message);
876-
_receiveMessage(_message, _signature);
884+
_receiveMessage(_message, _signature, _randomCaller);
877885
}
878886

879887
function testSendMaxMessageBodySize_revertsOnNonOwner(
@@ -1009,9 +1017,12 @@ contract MessageTransmitterV2Test is TestUtils {
10091017
vm.stopPrank();
10101018
}
10111019

1020+
// Calls receiveMessage with msg.destinationCaller if set; otherwise
1021+
// with `_randomCaller`
10121022
function _receiveMessage(
10131023
bytes memory _message,
1014-
bytes memory _signature
1024+
bytes memory _signature,
1025+
address _randomCaller
10151026
) internal {
10161027
bytes29 _msg = _message.ref(0);
10171028
address _recipient = AddressUtils.bytes32ToAddress(
@@ -1034,10 +1045,17 @@ contract MessageTransmitterV2Test is TestUtils {
10341045
vm.mockCall(_recipient, _encodedMessageHandlerCall, abi.encode(true));
10351046
vm.expectCall(_recipient, _encodedMessageHandlerCall, 1);
10361047

1037-
// Spoof the destination caller
1038-
address _caller = AddressUtils.bytes32ToAddress(
1039-
_msg._getDestinationCaller()
1040-
);
1048+
// Spoof the destination caller if needed
1049+
address _caller;
1050+
if (_msg._getDestinationCaller() == bytes32(0)) {
1051+
// Don't spoof the 0-address; defeats the purpose of the test
1052+
vm.assume(_randomCaller != address(0));
1053+
_caller = _randomCaller;
1054+
} else {
1055+
_caller = AddressUtils.bytes32ToAddress(
1056+
_msg._getDestinationCaller()
1057+
);
1058+
}
10411059

10421060
// assert that a MessageReceive event was logged with expected message bytes
10431061
vm.expectEmit(true, true, true, true);

0 commit comments

Comments
 (0)