-
Notifications
You must be signed in to change notification settings - Fork 53
/
Copy pathNttManager.t.sol
903 lines (722 loc) · 33.9 KB
/
NttManager.t.sol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
// SPDX-License-Identifier: Apache 2
pragma solidity >=0.8.8 <0.9.0;
import "forge-std/Test.sol";
import "forge-std/console.sol";
import "../src/NttManager/NttManager.sol";
import "../src/interfaces/INttManager.sol";
import "../src/interfaces/IRateLimiter.sol";
import "../src/interfaces/IManagerBase.sol";
import "../src/interfaces/IRateLimiterEvents.sol";
import "../src/NttManager/TransceiverRegistry.sol";
import "../src/libraries/PausableUpgradeable.sol";
import {Utils} from "./libraries/Utils.sol";
import "openzeppelin-contracts/contracts/token/ERC20/ERC20.sol";
import "openzeppelin-contracts/contracts/proxy/ERC1967/ERC1967Proxy.sol";
import "wormhole-solidity-sdk/interfaces/IWormhole.sol";
import "wormhole-solidity-sdk/testing/helpers/WormholeSimulator.sol";
import "wormhole-solidity-sdk/Utils.sol";
import "./libraries/TransceiverHelpers.sol";
import "./libraries/NttManagerHelpers.sol";
import "./interfaces/ITransceiverReceiver.sol";
import "./mocks/DummyTransceiver.sol";
import "../src/mocks/DummyToken.sol";
import "./mocks/MockNttManager.sol";
// TODO: set this up so the common functionality tests can be run against both
contract TestNttManager is Test, IRateLimiterEvents {
MockNttManagerContract nttManager;
MockNttManagerContract nttManagerOther;
MockNttManagerContract nttManagerZeroRateLimiter;
using TrimmedAmountLib for uint256;
using TrimmedAmountLib for TrimmedAmount;
// 0x99'E''T''T'
uint16 constant chainId = 7;
uint256 constant DEVNET_GUARDIAN_PK =
0xcfb12303a19cde580bb4dd771639b0d26bc68353645571a8cff516ab2ee113a0;
WormholeSimulator guardian;
uint256 initialBlockTimestamp;
DummyTransceiver dummyTransceiver;
function setUp() public {
string memory url = "https://ethereum-sepolia-rpc.publicnode.com";
IWormhole wormhole = IWormhole(0x4a8bc80Ed5a4067f1CCf107057b8270E0cC11A78);
vm.createSelectFork(url);
initialBlockTimestamp = vm.getBlockTimestamp();
guardian = new WormholeSimulator(address(wormhole), DEVNET_GUARDIAN_PK);
DummyToken t = new DummyToken();
NttManager implementation = new MockNttManagerContract(
address(t), IManagerBase.Mode.LOCKING, chainId, 1 days, false
);
NttManager otherImplementation = new MockNttManagerContract(
address(t), IManagerBase.Mode.LOCKING, chainId, 1 days, false
);
nttManager = MockNttManagerContract(address(new ERC1967Proxy(address(implementation), "")));
nttManager.initialize();
nttManagerOther =
MockNttManagerContract(address(new ERC1967Proxy(address(otherImplementation), "")));
nttManagerOther.initialize();
dummyTransceiver = new DummyTransceiver(address(nttManager));
nttManager.setTransceiver(address(dummyTransceiver));
}
// === pure unit tests
// naive implementation of countSetBits to test against
function simpleCount(uint64 n) public pure returns (uint8) {
uint8 count;
while (n > 0) {
count += uint8(n & 1);
n >>= 1;
}
return count;
}
function testFuzz_countSetBits(uint64 n) public {
assertEq(simpleCount(n), countSetBits(n));
}
// === Deployments with rate limiter disabled
function test_disabledRateLimiter() public {
DummyToken t = new DummyToken();
NttManager implementation =
new MockNttManagerContract(address(t), IManagerBase.Mode.LOCKING, chainId, 0, true);
nttManagerZeroRateLimiter =
MockNttManagerContract(address(new ERC1967Proxy(address(implementation), "")));
nttManagerZeroRateLimiter.initialize();
DummyTransceiver e = new DummyTransceiver(address(nttManagerZeroRateLimiter));
nttManagerZeroRateLimiter.setTransceiver(address(e));
address user_A = address(0x123);
address user_B = address(0x456);
uint8 decimals = t.decimals();
nttManagerZeroRateLimiter.setPeer(
chainId, toWormholeFormat(address(0x1)), 9, type(uint64).max
);
t.mintDummy(address(user_A), 5 * 10 ** decimals);
// Test outgoing transfers complete successfully with rate limit disabled
vm.startPrank(user_A);
t.approve(address(nttManagerZeroRateLimiter), 3 * 10 ** decimals);
uint64 s1 = nttManagerZeroRateLimiter.transfer(
1 * 10 ** decimals, chainId, toWormholeFormat(user_B)
);
uint64 s2 = nttManagerZeroRateLimiter.transfer(
1 * 10 ** decimals, chainId, toWormholeFormat(user_B)
);
uint64 s3 = nttManagerZeroRateLimiter.transfer(
1 * 10 ** decimals, chainId, toWormholeFormat(user_B)
);
vm.stopPrank();
assertEq(s1, 0);
assertEq(s2, 1);
assertEq(s3, 2);
// Test incoming transfer completes successfully with rate limit disabled
(DummyTransceiver e1,) = TransceiverHelpersLib.setup_transceivers(nttManagerZeroRateLimiter);
nttManagerZeroRateLimiter.setThreshold(2);
// register nttManager peer
bytes32 peer = toWormholeFormat(address(nttManager));
nttManagerZeroRateLimiter.setPeer(
TransceiverHelpersLib.SENDING_CHAIN_ID, peer, 9, type(uint64).max
);
TransceiverStructs.NttManagerMessage memory nttManagerMessage;
bytes memory transceiverMessage;
(nttManagerMessage, transceiverMessage) = TransceiverHelpersLib
.buildTransceiverMessageWithNttManagerPayload(
0,
bytes32(0),
peer,
toWormholeFormat(address(nttManagerZeroRateLimiter)),
abi.encode("payload")
);
e1.receiveMessage(transceiverMessage);
bytes32 hash = TransceiverStructs.nttManagerMessageDigest(
TransceiverHelpersLib.SENDING_CHAIN_ID, nttManagerMessage
);
assertEq(nttManagerZeroRateLimiter.messageAttestations(hash), 1);
}
// === ownership
function test_owner() public {
// TODO: implement separate governance contract
assertEq(nttManager.owner(), address(this));
}
function test_transferOwnership() public {
address newOwner = address(0x123);
nttManager.transferOwnership(newOwner);
assertEq(nttManager.owner(), newOwner);
}
function test_onlyOwnerCanTransferOwnership() public {
address notOwner = address(0x123);
vm.startPrank(notOwner);
vm.expectRevert(
abi.encodeWithSelector(OwnableUpgradeable.OwnableUnauthorizedAccount.selector, notOwner)
);
nttManager.transferOwnership(address(0x456));
}
function test_pauseUnpause() public {
assertEq(nttManager.isPaused(), false);
nttManager.pause();
assertEq(nttManager.isPaused(), true);
// When the NttManager is paused, initiating transfers, completing queued transfers on both source and destination chains,
// executing transfers and attesting to transfers should all revert
vm.expectRevert(
abi.encodeWithSelector(PausableUpgradeable.RequireContractIsNotPaused.selector)
);
nttManager.transfer(0, 0, bytes32(0));
vm.expectRevert(
abi.encodeWithSelector(PausableUpgradeable.RequireContractIsNotPaused.selector)
);
nttManager.completeOutboundQueuedTransfer(0);
vm.expectRevert(
abi.encodeWithSelector(PausableUpgradeable.RequireContractIsNotPaused.selector)
);
nttManager.completeInboundQueuedTransfer(bytes32(0));
vm.expectRevert(
abi.encodeWithSelector(PausableUpgradeable.RequireContractIsNotPaused.selector)
);
TransceiverStructs.NttManagerMessage memory message;
nttManager.executeMsg(0, bytes32(0), message);
bytes memory transceiverMessage;
(, transceiverMessage) = TransceiverHelpersLib.buildTransceiverMessageWithNttManagerPayload(
0,
bytes32(0),
toWormholeFormat(address(nttManagerOther)),
toWormholeFormat(address(nttManager)),
abi.encode("payload")
);
vm.expectRevert(
abi.encodeWithSelector(PausableUpgradeable.RequireContractIsNotPaused.selector)
);
dummyTransceiver.receiveMessage(transceiverMessage);
nttManager.unpause();
assertEq(nttManager.isPaused(), false);
}
// === transceiver registration
function test_registerTransceiver() public {
DummyTransceiver e = new DummyTransceiver(address(nttManager));
nttManager.setTransceiver(address(e));
}
function test_onlyOwnerCanModifyTransceivers() public {
DummyTransceiver e = new DummyTransceiver(address(nttManager));
nttManager.setTransceiver(address(e));
address notOwner = address(0x123);
vm.startPrank(notOwner);
vm.expectRevert(
abi.encodeWithSelector(OwnableUpgradeable.OwnableUnauthorizedAccount.selector, notOwner)
);
nttManager.setTransceiver(address(e));
vm.expectRevert(
abi.encodeWithSelector(OwnableUpgradeable.OwnableUnauthorizedAccount.selector, notOwner)
);
nttManager.removeTransceiver(address(e));
}
function test_cantEnableTransceiverTwice() public {
DummyTransceiver e = new DummyTransceiver(address(nttManager));
nttManager.setTransceiver(address(e));
vm.expectRevert(
abi.encodeWithSelector(
TransceiverRegistry.TransceiverAlreadyEnabled.selector, address(e)
)
);
nttManager.setTransceiver(address(e));
}
function test_disableReenableTransceiver() public {
DummyTransceiver e = new DummyTransceiver(address(nttManager));
nttManager.setTransceiver(address(e));
nttManager.removeTransceiver(address(e));
nttManager.setTransceiver(address(e));
}
function test_multipleTransceivers() public {
DummyTransceiver e1 = new DummyTransceiver(address(nttManager));
DummyTransceiver e2 = new DummyTransceiver(address(nttManager));
nttManager.setTransceiver(address(e1));
nttManager.setTransceiver(address(e2));
}
function test_transceiverIncompatibleNttManager() public {
// Transceiver instantiation reverts if the nttManager doesn't have the proper token method
vm.expectRevert(bytes(""));
new DummyTransceiver(address(0xBEEF));
}
function test_transceiverWrongNttManager() public {
// TODO: this is accepted currently. should we include a check to ensure
// only transceivers whose nttManager is us can be registered? (this would be
// a convenience check, not a security one)
DummyToken t = new DummyToken();
NttManager altNttManager = new MockNttManagerContract(
address(t), IManagerBase.Mode.LOCKING, chainId, 1 days, false
);
DummyTransceiver e = new DummyTransceiver(address(altNttManager));
nttManager.setTransceiver(address(e));
}
function test_noEnabledTransceivers() public {
nttManager.removeTransceiver(address(dummyTransceiver));
address user_A = address(0x123);
address user_B = address(0x456);
DummyToken token = DummyToken(nttManager.token());
uint8 decimals = token.decimals();
nttManager.setPeer(chainId, toWormholeFormat(address(0x1)), 9, type(uint64).max);
nttManager.setOutboundLimit(packTrimmedAmount(type(uint64).max, 8).untrim(decimals));
token.mintDummy(address(user_A), 5 * 10 ** decimals);
vm.startPrank(user_A);
token.approve(address(nttManager), 3 * 10 ** decimals);
vm.expectRevert(abi.encodeWithSelector(IManagerBase.NoEnabledTransceivers.selector));
nttManager.transfer(
1 * 10 ** decimals, chainId, toWormholeFormat(user_B), false, new bytes(1)
);
}
function test_notTransceiver() public {
// TODO: this is accepted currently. should we include a check to ensure
// only transceivers can be registered? (this would be a convenience check, not a security one)
nttManager.setTransceiver(address(0x123));
}
function test_maxOutTransceivers() public {
// Let's register a transceiver and then disable it. We now have 2 registered managers
// since we register 1 in the setup
DummyTransceiver e = new DummyTransceiver(address(nttManager));
nttManager.setTransceiver(address(e));
nttManager.removeTransceiver(address(e));
// We should be able to register 64 transceivers total
for (uint256 i = 0; i < 62; ++i) {
DummyTransceiver d = new DummyTransceiver(address(nttManager));
nttManager.setTransceiver(address(d));
}
// Registering a new transceiver should fail as we've hit the cap
DummyTransceiver c = new DummyTransceiver(address(nttManager));
vm.expectRevert(TransceiverRegistry.TooManyTransceivers.selector);
nttManager.setTransceiver(address(c));
// We should be able to renable an already registered transceiver at the cap
nttManager.setTransceiver(address(e));
}
// == threshold
function test_cantSetThresholdTooHigh() public {
// 1 transceiver set, so can't set threshold to 2
vm.expectRevert(abi.encodeWithSelector(IManagerBase.ThresholdTooHigh.selector, 2, 1));
nttManager.setThreshold(2);
}
function test_canSetThreshold() public {
DummyTransceiver e1 = new DummyTransceiver(address(nttManager));
DummyTransceiver e2 = new DummyTransceiver(address(nttManager));
nttManager.setTransceiver(address(e1));
nttManager.setTransceiver(address(e2));
nttManager.setThreshold(1);
nttManager.setThreshold(2);
nttManager.setThreshold(1);
}
function test_cantSetThresholdToZero() public {
DummyTransceiver e = new DummyTransceiver(address(nttManager));
nttManager.setTransceiver(address(e));
vm.expectRevert(abi.encodeWithSelector(IManagerBase.ZeroThreshold.selector));
nttManager.setThreshold(0);
}
function test_onlyOwnerCanSetThreshold() public {
address notOwner = address(0x123);
vm.startPrank(notOwner);
vm.expectRevert(
abi.encodeWithSelector(OwnableUpgradeable.OwnableUnauthorizedAccount.selector, notOwner)
);
nttManager.setThreshold(1);
}
// == threshold
function test_peerRegistrationLimitsCanBeUpdated() public {
bytes32 peer = toWormholeFormat(address(nttManager));
nttManager.setPeer(TransceiverHelpersLib.SENDING_CHAIN_ID, peer, 9, 0);
IRateLimiter.RateLimitParams memory params =
nttManager.getInboundLimitParams(TransceiverHelpersLib.SENDING_CHAIN_ID);
assertEq(params.limit.getAmount(), 0);
assertEq(params.limit.getDecimals(), 8);
nttManager.setInboundLimit(type(uint64).max, TransceiverHelpersLib.SENDING_CHAIN_ID);
params = nttManager.getInboundLimitParams(TransceiverHelpersLib.SENDING_CHAIN_ID);
assertEq(params.limit.getAmount(), type(uint64).max / 10 ** (18 - 8));
assertEq(params.limit.getDecimals(), 8);
}
// === attestation
function test_onlyEnabledTransceiversCanAttest() public {
(DummyTransceiver e1,) = TransceiverHelpersLib.setup_transceivers(nttManagerOther);
nttManagerOther.removeTransceiver(address(e1));
bytes32 peer = toWormholeFormat(address(nttManager));
nttManagerOther.setPeer(TransceiverHelpersLib.SENDING_CHAIN_ID, peer, 9, type(uint64).max);
bytes memory transceiverMessage;
(, transceiverMessage) = TransceiverHelpersLib.buildTransceiverMessageWithNttManagerPayload(
0, bytes32(0), peer, toWormholeFormat(address(nttManagerOther)), abi.encode("payload")
);
vm.expectRevert(
abi.encodeWithSelector(TransceiverRegistry.CallerNotTransceiver.selector, address(e1))
);
e1.receiveMessage(transceiverMessage);
}
function test_onlyPeerNttManagerCanAttest() public {
(DummyTransceiver e1,) = TransceiverHelpersLib.setup_transceivers(nttManagerOther);
nttManagerOther.setThreshold(2);
bytes32 peer = toWormholeFormat(address(nttManager));
TransceiverStructs.NttManagerMessage memory nttManagerMessage;
bytes memory transceiverMessage;
(nttManagerMessage, transceiverMessage) = TransceiverHelpersLib
.buildTransceiverMessageWithNttManagerPayload(
0, bytes32(0), peer, toWormholeFormat(address(nttManagerOther)), abi.encode("payload")
);
vm.expectRevert(
abi.encodeWithSelector(
INttManager.InvalidPeer.selector, TransceiverHelpersLib.SENDING_CHAIN_ID, peer
)
);
e1.receiveMessage(transceiverMessage);
}
function test_attestSimple() public {
(DummyTransceiver e1,) = TransceiverHelpersLib.setup_transceivers(nttManagerOther);
nttManagerOther.setThreshold(2);
// register nttManager peer
bytes32 peer = toWormholeFormat(address(nttManager));
nttManagerOther.setPeer(TransceiverHelpersLib.SENDING_CHAIN_ID, peer, 9, type(uint64).max);
TransceiverStructs.NttManagerMessage memory nttManagerMessage;
bytes memory transceiverMessage;
(nttManagerMessage, transceiverMessage) = TransceiverHelpersLib
.buildTransceiverMessageWithNttManagerPayload(
0, bytes32(0), peer, toWormholeFormat(address(nttManagerOther)), abi.encode("payload")
);
e1.receiveMessage(transceiverMessage);
bytes32 hash = TransceiverStructs.nttManagerMessageDigest(
TransceiverHelpersLib.SENDING_CHAIN_ID, nttManagerMessage
);
assertEq(nttManagerOther.messageAttestations(hash), 1);
}
function test_attestTwice() public {
(DummyTransceiver e1,) = TransceiverHelpersLib.setup_transceivers(nttManagerOther);
nttManagerOther.setThreshold(2);
// register nttManager peer
bytes32 peer = toWormholeFormat(address(nttManager));
nttManagerOther.setPeer(TransceiverHelpersLib.SENDING_CHAIN_ID, peer, 9, type(uint64).max);
TransceiverStructs.NttManagerMessage memory nttManagerMessage;
bytes memory transceiverMessage;
(nttManagerMessage, transceiverMessage) = TransceiverHelpersLib
.buildTransceiverMessageWithNttManagerPayload(
0, bytes32(0), peer, toWormholeFormat(address(nttManagerOther)), abi.encode("payload")
);
bytes32 hash = TransceiverStructs.nttManagerMessageDigest(
TransceiverHelpersLib.SENDING_CHAIN_ID, nttManagerMessage
);
e1.receiveMessage(transceiverMessage);
vm.expectRevert(
abi.encodeWithSelector(IManagerBase.TransceiverAlreadyAttestedToMessage.selector, hash)
);
e1.receiveMessage(transceiverMessage);
// can't double vote
assertEq(nttManagerOther.messageAttestations(hash), 1);
}
function test_attestDisabled() public {
(DummyTransceiver e1,) = TransceiverHelpersLib.setup_transceivers(nttManagerOther);
nttManagerOther.setThreshold(2);
bytes32 peer = toWormholeFormat(address(nttManager));
nttManagerOther.setPeer(TransceiverHelpersLib.SENDING_CHAIN_ID, peer, 9, type(uint64).max);
ITransceiverReceiver[] memory transceivers = new ITransceiverReceiver[](1);
transceivers[0] = e1;
TransceiverStructs.NttManagerMessage memory m;
(m,) = TransceiverHelpersLib.attestTransceiversHelper(
address(0x456),
0,
chainId,
nttManager,
nttManagerOther,
packTrimmedAmount(50, 8),
packTrimmedAmount(type(uint64).max, 8),
transceivers
);
nttManagerOther.removeTransceiver(address(e1));
bytes32 hash =
TransceiverStructs.nttManagerMessageDigest(TransceiverHelpersLib.SENDING_CHAIN_ID, m);
// a disabled transceiver's vote no longer counts
assertEq(nttManagerOther.messageAttestations(hash), 0);
nttManagerOther.setTransceiver(address(e1));
// it counts again when reenabled
assertEq(nttManagerOther.messageAttestations(hash), 1);
}
function test_transfer_sequences() public {
address user_A = address(0x123);
address user_B = address(0x456);
DummyToken token = DummyToken(nttManager.token());
uint8 decimals = token.decimals();
nttManager.setPeer(chainId, toWormholeFormat(address(0x1)), 9, type(uint64).max);
nttManager.setOutboundLimit(packTrimmedAmount(type(uint64).max, 8).untrim(decimals));
token.mintDummy(address(user_A), 5 * 10 ** decimals);
vm.startPrank(user_A);
token.approve(address(nttManager), 3 * 10 ** decimals);
uint64 s1 = nttManager.transfer(
1 * 10 ** decimals, chainId, toWormholeFormat(user_B), false, new bytes(1)
);
uint64 s2 = nttManager.transfer(
1 * 10 ** decimals, chainId, toWormholeFormat(user_B), false, new bytes(1)
);
uint64 s3 = nttManager.transfer(
1 * 10 ** decimals, chainId, toWormholeFormat(user_B), false, new bytes(1)
);
assertEq(s1, 0);
assertEq(s2, 1);
assertEq(s3, 2);
}
function test_transferWithAmountAndDecimalsThatCouldOverflow() public {
// The source chain has 18 decimals trimmed to 8, and the peer has 6 decimals trimmed to 6
nttManager.setPeer(chainId, toWormholeFormat(address(0x1)), 6, type(uint64).max);
address user_A = address(0x123);
address user_B = address(0x456);
DummyToken token = DummyToken(nttManager.token());
uint8 decimals = token.decimals();
assertEq(decimals, 18);
token.mintDummy(address(user_A), type(uint256).max);
vm.startPrank(user_A);
token.approve(address(nttManager), type(uint256).max);
// When transferring to a chain with 6 decimals the amount will get trimmed to 6 decimals
// and then scaled back up to 8 for local accounting. If we get the trimmed amount to be
// type(uint64).max, then when scaling up we could overflow. We safely cast to prevent this.
uint256 amount = type(uint64).max * 10 ** (decimals - 6);
vm.expectRevert("SafeCast: value doesn't fit in 64 bits");
nttManager.transfer(amount, chainId, toWormholeFormat(user_B), false, new bytes(1));
// A (slightly) more sensible amount should work normally
amount = (type(uint64).max * 10 ** (decimals - 6 - 2)) - 150000000000; // Subtract this to make sure we don't have dust
nttManager.transfer(amount, chainId, toWormholeFormat(user_B), false, new bytes(1));
}
function test_attestationQuorum() public {
address user_B = address(0x456);
(DummyTransceiver e1, DummyTransceiver e2) =
TransceiverHelpersLib.setup_transceivers(nttManagerOther);
TrimmedAmount transferAmount = packTrimmedAmount(50, 8);
TransceiverStructs.NttManagerMessage memory m;
bytes memory encodedEm;
{
ITransceiverReceiver[] memory transceivers = new ITransceiverReceiver[](2);
transceivers[0] = e1;
transceivers[1] = e2;
TransceiverStructs.TransceiverMessage memory em;
(m, em) = TransceiverHelpersLib.attestTransceiversHelper(
user_B,
0,
chainId,
nttManager,
nttManagerOther,
transferAmount,
packTrimmedAmount(type(uint64).max, 8),
transceivers
);
encodedEm = TransceiverStructs.encodeTransceiverMessage(
TransceiverHelpersLib.TEST_TRANSCEIVER_PAYLOAD_PREFIX, em
);
}
{
DummyToken token = DummyToken(nttManager.token());
assertEq(token.balanceOf(address(user_B)), transferAmount.untrim(token.decimals()));
}
// replay protection for transceiver
vm.recordLogs();
vm.expectRevert(
abi.encodeWithSelector(
IManagerBase.TransceiverAlreadyAttestedToMessage.selector,
TransceiverStructs.nttManagerMessageDigest(
TransceiverHelpersLib.SENDING_CHAIN_ID, m
)
)
);
e2.receiveMessage(encodedEm);
}
// TODO:
// currently there is no way to test the threshold logic and the duplicate
// protection logic without setting up the business logic as well.
//
// we should separate the business logic out from the transceiver handling.
// that way the functionality could be tested separately (and the contracts
// would also be more reusable)
// === storage
function test_noAutomaticSlot() public {
DummyToken t = new DummyToken();
MockNttManagerContract c =
new MockNttManagerContract(address(t), IManagerBase.Mode.LOCKING, 1, 1 days, false);
assertEq(c.lastSlot(), 0x0);
}
function test_constructor() public {
DummyToken t = new DummyToken();
vm.startStateDiffRecording();
new MockNttManagerContract(address(t), IManagerBase.Mode.LOCKING, 1, 1 days, false);
Utils.assertSafeUpgradeableConstructor(vm.stopAndReturnStateDiff());
}
// === token transfer logic
function test_dustReverts() public {
// transfer 3 tokens
address from = address(0x123);
address to = address(0x456);
DummyToken token = DummyToken(nttManager.token());
uint8 decimals = token.decimals();
uint256 maxAmount = 5 * 10 ** decimals;
token.mintDummy(from, maxAmount);
nttManager.setPeer(chainId, toWormholeFormat(address(0x1)), 9, type(uint64).max);
nttManager.setOutboundLimit(packTrimmedAmount(type(uint64).max, 8).untrim(decimals));
nttManager.setInboundLimit(
packTrimmedAmount(type(uint64).max, 8).untrim(decimals),
TransceiverHelpersLib.SENDING_CHAIN_ID
);
vm.startPrank(from);
uint256 transferAmount = 3 * 10 ** decimals;
assertEq(
transferAmount < maxAmount - 500, true, "Transferring more tokens than what exists"
);
uint256 dustAmount = 500;
uint256 amountWithDust = transferAmount + dustAmount; // An amount with 19 digits, which will result in dust due to 18 decimals
token.approve(address(nttManager), amountWithDust);
vm.expectRevert(
abi.encodeWithSelector(
INttManager.TransferAmountHasDust.selector, amountWithDust, dustAmount
)
);
nttManager.transfer(amountWithDust, chainId, toWormholeFormat(to), false, new bytes(1));
vm.stopPrank();
}
// === upgradeability
function expectRevert(
address contractAddress,
bytes memory encodedSignature,
bytes memory expectedRevert
) internal {
(bool success, bytes memory result) = contractAddress.call(encodedSignature);
require(!success, "call did not revert");
require(keccak256(result) == keccak256(expectedRevert), "call did not revert as expected");
}
function test_upgradeNttManager() public {
// The testing strategy here is as follows:
// Step 1: Deploy the nttManager contract with two transceivers and
// receive a message through it.
// Step 2: Upgrade it to a new nttManager contract an use the same transceivers to receive
// a new message through it.
// Step 3: Upgrade back to the standalone contract (with two
// transceivers) and receive a message through it.
// This ensures that the storage slots don't get clobbered through the upgrades.
address user_B = address(0x456);
DummyToken token = DummyToken(nttManager.token());
TrimmedAmount transferAmount = packTrimmedAmount(50, 8);
(ITransceiverReceiver e1, ITransceiverReceiver e2) =
TransceiverHelpersLib.setup_transceivers(nttManagerOther);
// Step 1 (contract is deployed by setUp())
ITransceiverReceiver[] memory transceivers = new ITransceiverReceiver[](2);
transceivers[0] = e1;
transceivers[1] = e2;
TransceiverStructs.NttManagerMessage memory m;
bytes memory encodedEm;
{
TransceiverStructs.TransceiverMessage memory em;
(m, em) = TransceiverHelpersLib.attestTransceiversHelper(
user_B,
0,
chainId,
nttManager,
nttManagerOther,
transferAmount,
packTrimmedAmount(type(uint64).max, 8),
transceivers
);
encodedEm = TransceiverStructs.encodeTransceiverMessage(
TransceiverHelpersLib.TEST_TRANSCEIVER_PAYLOAD_PREFIX, em
);
}
assertEq(token.balanceOf(address(user_B)), transferAmount.untrim(token.decimals()));
// Step 2 (upgrade to a new nttManager)
MockNttManagerContract newNttManager = new MockNttManagerContract(
nttManager.token(), IManagerBase.Mode.LOCKING, chainId, 1 days, false
);
nttManagerOther.upgrade(address(newNttManager));
TransceiverHelpersLib.attestTransceiversHelper(
user_B,
bytes32(uint256(1)),
chainId,
nttManager, // this is the proxy
nttManagerOther, // this is the proxy
transferAmount,
packTrimmedAmount(type(uint64).max, 8),
transceivers
);
assertEq(token.balanceOf(address(user_B)), transferAmount.untrim(token.decimals()) * 2);
}
function test_tokenUpgradedAndDecimalsChanged() public {
DummyToken dummy1 = new DummyTokenMintAndBurn();
// Make the token an upgradeable token
DummyTokenMintAndBurn t =
DummyTokenMintAndBurn(address(new ERC1967Proxy(address(dummy1), "")));
NttManager implementation = new MockNttManagerContract(
address(t), IManagerBase.Mode.LOCKING, chainId, 1 days, false
);
MockNttManagerContract newNttManager =
MockNttManagerContract(address(new ERC1967Proxy(address(implementation), "")));
newNttManager.initialize();
// register nttManager peer and transceiver
bytes32 peer = toWormholeFormat(address(nttManager));
newNttManager.setPeer(TransceiverHelpersLib.SENDING_CHAIN_ID, peer, 9, type(uint64).max);
{
DummyTransceiver e = new DummyTransceiver(address(newNttManager));
newNttManager.setTransceiver(address(e));
}
address user_A = address(0x123);
address user_B = address(0x456);
t.mintDummy(address(user_A), 5 * 10 ** t.decimals());
// Check that we can initiate a transfer
vm.startPrank(user_A);
t.approve(address(newNttManager), 3 * 10 ** t.decimals());
newNttManager.transfer(
1 * 10 ** t.decimals(),
TransceiverHelpersLib.SENDING_CHAIN_ID,
toWormholeFormat(user_B),
false,
new bytes(1)
);
vm.stopPrank();
// Check that we can receive a transfer
(DummyTransceiver e1,) = TransceiverHelpersLib.setup_transceivers(newNttManager);
newNttManager.setThreshold(1);
bytes memory transceiverMessage;
bytes memory tokenTransferMessage;
TrimmedAmount transferAmount = packTrimmedAmount(100, 8);
tokenTransferMessage = TransceiverStructs.encodeNativeTokenTransfer(
TransceiverStructs.NativeTokenTransfer({
amount: transferAmount,
sourceToken: toWormholeFormat(address(t)),
to: toWormholeFormat(user_B),
toChain: chainId
})
);
(, transceiverMessage) = TransceiverHelpersLib.buildTransceiverMessageWithNttManagerPayload(
0, bytes32(0), peer, toWormholeFormat(address(newNttManager)), tokenTransferMessage
);
e1.receiveMessage(transceiverMessage);
uint256 userBBalanceBefore = t.balanceOf(address(user_B));
assertEq(userBBalanceBefore, transferAmount.untrim(t.decimals()));
// If the token decimals change to the same trimmed amount, we should safely receive the correct number of tokens
DummyTokenDifferentDecimals dummy2 = new DummyTokenDifferentDecimals(10); // 10 gets trimmed to 8
t.upgrade(address(dummy2));
vm.startPrank(user_A);
newNttManager.transfer(
1 * 10 ** 10,
TransceiverHelpersLib.SENDING_CHAIN_ID,
toWormholeFormat(user_B),
false,
new bytes(1)
);
vm.stopPrank();
(, transceiverMessage) = TransceiverHelpersLib.buildTransceiverMessageWithNttManagerPayload(
bytes32("1"),
bytes32(0),
peer,
toWormholeFormat(address(newNttManager)),
tokenTransferMessage
);
e1.receiveMessage(transceiverMessage);
assertEq(
t.balanceOf(address(user_B)), userBBalanceBefore + transferAmount.untrim(t.decimals())
);
// Now if the token decimals change to a different trimmed amount, we shouldn't be able to send or receive
DummyTokenDifferentDecimals dummy3 = new DummyTokenDifferentDecimals(7); // 7 is 7 trimmed
t.upgrade(address(dummy3));
vm.startPrank(user_A);
vm.expectRevert(abi.encodeWithSelector(NumberOfDecimalsNotEqual.selector, 8, 7));
newNttManager.transfer(
1 * 10 ** 7,
TransceiverHelpersLib.SENDING_CHAIN_ID,
toWormholeFormat(user_B),
false,
new bytes(1)
);
vm.stopPrank();
(, transceiverMessage) = TransceiverHelpersLib.buildTransceiverMessageWithNttManagerPayload(
bytes32("2"),
bytes32(0),
peer,
toWormholeFormat(address(newNttManager)),
tokenTransferMessage
);
vm.expectRevert(abi.encodeWithSelector(NumberOfDecimalsNotEqual.selector, 8, 7));
e1.receiveMessage(transceiverMessage);
}
}