@@ -8,6 +8,10 @@ import { TargetContractMock } from "test/mocks/TargetContractMock.sol";
88import { LZForwarder } from "src/forwarders/LZForwarder.sol " ;
99import { LZReceiver, Origin } from "src/receivers/LZReceiver.sol " ;
1010
11+ interface ILayerZeroEndpointV2 {
12+ function delegates (address sender ) external view returns (address );
13+ }
14+
1115contract LZReceiverTest is Test {
1216
1317 TargetContractMock target;
@@ -17,41 +21,82 @@ contract LZReceiverTest is Test {
1721 address destinationEndpoint = LZForwarder.ENDPOINT_BNB;
1822 address randomAddress = makeAddr ("randomAddress " );
1923 address sourceAuthority = makeAddr ("sourceAuthority " );
20-
24+ address delegate = makeAddr ("delegate " );
25+ address owner = makeAddr ("owner " );
26+
2127 uint32 srcEid = LZForwarder.ENDPOINT_ID_ETHEREUM;
2228
29+ error NoPeer (uint32 eid );
30+ error OnlyEndpoint (address addr );
31+ error OnlyPeer (uint32 eid , bytes32 sender );
32+
2333 function setUp () public {
34+ vm.createSelectFork (getChain ("bnb_smart_chain " ).rpcUrl);
35+
2436 target = new TargetContractMock ();
2537
2638 receiver = new LZReceiver (
2739 destinationEndpoint,
2840 srcEid,
2941 bytes32 (uint256 (uint160 (sourceAuthority))),
30- address (target)
42+ address (target),
43+ delegate,
44+ owner
3145 );
3246 }
3347
34- function test_constructor () public {
35- receiver = new LZReceiver (
36- destinationEndpoint,
37- srcEid,
38- bytes32 (uint256 (uint160 (sourceAuthority))),
39- address (target)
40- );
48+ function test_constructor () public view {
49+ assertEq (receiver.srcEid (), srcEid);
50+ assertEq (receiver.sourceAuthority (), bytes32 (uint256 (uint160 (sourceAuthority))));
51+ assertEq (receiver.target (), address (target));
52+ assertEq (receiver.owner (), owner);
53+ assertEq (receiver.peers (srcEid), bytes32 (uint256 (uint160 (sourceAuthority))));
4154
42- assertEq (receiver. destinationEndpoint (), destinationEndpoint);
43- assertEq ( receiver.srcEid (), srcEid);
44- assertEq (receiver. sourceAuthority (), bytes32 ( uint256 ( uint160 (sourceAuthority))));
45- assertEq (receiver. target (), address (target) );
55+ assertEq (
56+ ILayerZeroEndpointV2 ( address ( receiver.endpoint ())). delegates ( address (receiver)),
57+ delegate
58+ );
4659 }
4760
48- function test_lzReceive_invalidSender () public {
61+ function test_invalidEndpoint () public {
4962 vm.prank (randomAddress);
50- vm.expectRevert (" LZReceiver/invalid-sender " );
63+ vm.expectRevert (abi.encodeWithSelector (OnlyEndpoint. selector , randomAddress) );
5164 receiver.lzReceive (
5265 Origin ({
5366 srcEid: srcEid,
54- sender: bytes32 (uint256 (uint160 (sourceAuthority))),
67+ sender: bytes32 (uint256 (uint160 (randomAddress))),
68+ nonce: 1
69+ }),
70+ bytes32 (0 ),
71+ abi.encodeCall (TargetContractMock.increment, ()),
72+ address (0 ),
73+ ""
74+ );
75+ }
76+
77+ function test_lzReceive_revertsNoPeer () public {
78+ vm.prank (destinationEndpoint);
79+ vm.expectRevert (abi.encodeWithSelector (NoPeer.selector , 0 ));
80+ receiver.lzReceive (
81+ Origin ({
82+ srcEid: 0 ,
83+ sender: bytes32 (uint256 (uint160 (randomAddress))),
84+ nonce: 1
85+ }),
86+ bytes32 (0 ),
87+ abi.encodeCall (TargetContractMock.increment, ()),
88+ address (0 ),
89+ ""
90+ );
91+ }
92+
93+ function test_lzReceive_revertsOnlyPeer () public {
94+ vm.prank (destinationEndpoint);
95+ vm.expectRevert (abi.encodeWithSelector (OnlyPeer.selector , srcEid, bytes32 (uint256 (uint160 (randomAddress)))));
96+ receiver.lzReceive (
97+ Origin ({
98+ srcEid: srcEid,
99+ sender: bytes32 (uint256 (uint160 (randomAddress))),
55100 nonce: 1
56101 }),
57102 bytes32 (0 ),
@@ -62,6 +107,10 @@ contract LZReceiverTest is Test {
62107 }
63108
64109 function test_lzReceive_invalidSrcEid () public {
110+ // NOTE: To pass initial check, we set the peer.
111+ vm.prank (owner);
112+ receiver.setPeer (srcEid + 1 , bytes32 (uint256 (uint160 (sourceAuthority))));
113+
65114 vm.prank (destinationEndpoint);
66115 vm.expectRevert ("LZReceiver/invalid-srcEid " );
67116 receiver.lzReceive (
@@ -78,6 +127,10 @@ contract LZReceiverTest is Test {
78127 }
79128
80129 function test_lzReceive_invalidSourceAuthority () public {
130+ // NOTE: To pass initial check, we set the peer.
131+ vm.prank (owner);
132+ receiver.setPeer (srcEid, bytes32 (uint256 (uint160 (randomAddress))));
133+
81134 vm.prank (destinationEndpoint);
82135 vm.expectRevert ("LZReceiver/invalid-sourceAuthority " );
83136 receiver.lzReceive (
@@ -110,34 +163,45 @@ contract LZReceiverTest is Test {
110163 assertEq (target.count (), 1 );
111164 }
112165
113- function test_allowInitializePath () public view {
114- // Should return true when origin.srcEid == srcEid and origin.sender == sourceAuthority
166+ function test_allowInitializePath () public {
167+ // Should return true when origin.srcEid == srcEid, origin.sender == sourceAuthority and peers[origin.srcEid] == origin.sender
115168 assertTrue (receiver.allowInitializePath (Origin ({
116169 srcEid: srcEid,
117170 sender: bytes32 (uint256 (uint160 (sourceAuthority))),
118171 nonce: 1
119172 })));
120173
174+ // Should return false when peers[origin.srcEid] != origin.sender
175+
176+ assertFalse (receiver.allowInitializePath (Origin ({
177+ srcEid: srcEid,
178+ sender: bytes32 (uint256 (uint160 (randomAddress))),
179+ nonce: 1
180+ })));
181+
121182 // Should return false when origin.srcEid != srcEid
183+
184+ // NOTE: Setting peer to make `super.allowInitializePath(origin)` return true
185+ vm.prank (owner);
186+ receiver.setPeer (srcEid + 1 , bytes32 (uint256 (uint160 (sourceAuthority))));
187+
122188 assertFalse (receiver.allowInitializePath (Origin ({
123189 srcEid: srcEid + 1 ,
124190 sender: bytes32 (uint256 (uint160 (sourceAuthority))),
125191 nonce: 1
126192 })));
127193
128194 // Should return false when origin.sender != sourceAuthority
129- assertFalse (receiver.allowInitializePath (Origin ({
130- srcEid: srcEid,
131- sender: bytes32 (uint256 (uint160 (randomAddress))),
132- nonce: 1
133- })));
134195
135- // Should return false when origin.srcEid != srcEid and origin.sender != sourceAuthority
196+ // NOTE: Setting peer to make `super.allowInitializePath(origin)` return true
197+ vm.prank (owner);
198+ receiver.setPeer (srcEid, bytes32 (uint256 (uint160 (randomAddress))));
199+
136200 assertFalse (receiver.allowInitializePath (Origin ({
137- srcEid: srcEid + 1 ,
201+ srcEid: srcEid,
138202 sender: bytes32 (uint256 (uint160 (randomAddress))),
139203 nonce: 1
140204 })));
141205 }
142-
206+
143207}
0 commit comments