-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Expand file tree
/
Copy pathGatewayConfig.sol
More file actions
866 lines (752 loc) · 34.5 KB
/
GatewayConfig.sol
File metadata and controls
866 lines (752 loc) · 34.5 KB
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
// SPDX-License-Identifier: BSD-3-Clause-Clear
pragma solidity ^0.8.24;
import { Ownable2StepUpgradeable } from "@openzeppelin/contracts-upgradeable/access/Ownable2StepUpgradeable.sol";
import { Strings } from "@openzeppelin/contracts/utils/Strings.sol";
import { IGatewayConfig } from "./interfaces/IGatewayConfig.sol";
import { IPauserSet } from "./interfaces/IPauserSet.sol";
import { decryptionAddress, inputVerificationAddress, pauserSetAddress } from "../addresses/GatewayAddresses.sol";
import { Decryption } from "./Decryption.sol";
import { InputVerification } from "./InputVerification.sol";
import { UUPSUpgradeableEmptyProxy } from "./shared/UUPSUpgradeableEmptyProxy.sol";
import { Pausable } from "./shared/Pausable.sol";
import { ProtocolMetadata, HostChain, Coprocessor, Custodian, KmsNode } from "./shared/Structs.sol";
/**
* @title GatewayConfig contract
* @notice See {IGatewayConfig}.
* @dev Add/remove methods will be added in the future for KMS nodes, coprocessors and host chains.
* See https://github.com/zama-ai/fhevm-gateway/issues/98 for more details.
*/
contract GatewayConfig is IGatewayConfig, Ownable2StepUpgradeable, UUPSUpgradeableEmptyProxy {
/**
* @notice The maximum chain ID.
*/
uint256 internal constant MAX_CHAIN_ID = type(uint64).max;
// ----------------------------------------------------------------------------------------------
// Contract information:
// ----------------------------------------------------------------------------------------------
/**
* @dev The following constants are used for versioning the contract. They are made private
* in order to force derived contracts to consider a different version. Note that
* they can still define their own private constants with the same name.
*/
string private constant CONTRACT_NAME = "GatewayConfig";
uint256 private constant MAJOR_VERSION = 0;
uint256 private constant MINOR_VERSION = 4;
uint256 private constant PATCH_VERSION = 0;
/**
* @dev Constant used for making sure the version number using in the `reinitializer` modifier is
* identical between `initializeFromEmptyProxy` and the reinitializeVX` method
* This constant does not represent the number of time a specific contract have been upgraded,
* as a contract deployed from version VX will have a REINITIALIZER_VERSION > 2.
*/
uint64 private constant REINITIALIZER_VERSION = 5;
/**
* @notice The address of the all gateway contracts
*/
Decryption private constant DECRYPTION = Decryption(decryptionAddress);
InputVerification private constant INPUT_VERIFICATION = InputVerification(inputVerificationAddress);
IPauserSet private constant PAUSER_SET = IPauserSet(pauserSetAddress);
/**
* @notice The contract's variable storage struct (@dev see ERC-7201)
*/
/// @custom:storage-location erc7201:fhevm_gateway.storage.GatewayConfig
struct GatewayConfigStorage {
// ----------------------------------------------------------------------------------------------
// Protocol metadata state variables:
// ----------------------------------------------------------------------------------------------
/// @notice The protocol's metadata
ProtocolMetadata protocolMetadata;
// ----------------------------------------------------------------------------------------------
// KMS nodes state variables:
// ----------------------------------------------------------------------------------------------
/// @notice The KMS nodes' transaction sender addresses
mapping(address kmsTxSenderAddress => bool isTxSender) isKmsTxSender;
/// @notice The KMS nodes' signer addresses
mapping(address kmsSignerAddress => bool isSigner) isKmsSigner;
/// @notice The KMS nodes' metadata
mapping(address kmsTxSenderAddress => KmsNode kmsNode) kmsNodes;
/// @notice The KMS nodes' transaction sender address list
address[] kmsTxSenderAddresses;
/// @notice The KMS nodes' signer address list
address[] kmsSignerAddresses;
/// @notice The MPC threshold
uint256 mpcThreshold;
/// @notice The threshold to consider for public decryption consensus
uint256 publicDecryptionThreshold;
/// @notice The threshold to consider for user decryption consensus
uint256 userDecryptionThreshold;
// ----------------------------------------------------------------------------------------------
// Coprocessors state variables:
// ----------------------------------------------------------------------------------------------
/// @notice The coprocessors' transaction sender addresses
mapping(address coprocessorTxSenderAddress => bool isTxSender) isCoprocessorTxSender;
/// @notice The coprocessors' signer addresses
mapping(address coprocessorSignerAddress => bool isSigner) isCoprocessorSigner;
/// @notice The coprocessors' metadata
mapping(address coprocessorTxSenderAddress => Coprocessor coprocessor) coprocessors;
/// @notice The coprocessors' transaction sender address list
address[] coprocessorTxSenderAddresses;
/// @notice The coprocessors' signer address list
address[] coprocessorSignerAddresses;
// ----------------------------------------------------------------------------------------------
// Host chain state variables:
// ----------------------------------------------------------------------------------------------
/// @notice The host chains' registered status
mapping(uint256 chainId => bool isRegistered) isHostChainRegistered;
/// @notice The host chains' metadata
HostChain[] hostChains;
// ----------------------------------------------------------------------------------------------
// Custodians state variables:
// ----------------------------------------------------------------------------------------------
/// @notice The custodians' metadata
mapping(address custodianTxSenderAddress => Custodian custodian) custodians;
/// @notice The custodians' transaction sender address list
address[] custodianTxSenderAddresses;
/// @notice The custodians' signer address list
address[] custodianSignerAddresses;
/// @notice The custodians' transaction sender addresses
mapping(address custodianTxSenderAddress => bool isTxSender) isCustodianTxSender;
/// @notice The custodians' signer addresses
mapping(address custodianSignerAddress => bool isSigner) isCustodianSigner;
/// @notice The threshold to consider for the KMS public material (FHE key, CRS) generation consensus.
uint256 kmsGenThreshold;
// ----------------------------------------------------------------------------------------------
// Coprocessor threshold state variables:
// ----------------------------------------------------------------------------------------------
/// @notice The threshold to consider for coprocessor consensus
uint256 coprocessorThreshold;
}
/**
* @dev Storage location has been computed using the following command:
* keccak256(abi.encode(uint256(keccak256("fhevm_gateway.storage.GatewayConfig")) - 1))
* & ~bytes32(uint256(0xff))
*/
bytes32 private constant GATEWAY_CONFIG_STORAGE_LOCATION =
0x86d3070a8993f6b209bee6185186d38a07fce8bbd97c750d934451b72f35b400;
/// @custom:oz-upgrades-unsafe-allow constructor
constructor() {
_disableInitializers();
}
modifier onlyPauser() {
if (!isPauser(msg.sender)) {
revert NotPauser(msg.sender);
}
_;
}
/**
* @notice Initializes the contract
* @dev This function needs to be public in order to be called by the UUPS proxy.
* @param initialMetadata Metadata of the protocol
* @param initialThresholds The operator thresholds
* @param initialKmsNodes List of KMS nodes
* @param initialCoprocessors List of coprocessors
* @param initialCustodians List of custodians
*/
/// @custom:oz-upgrades-validate-as-initializer
function initializeFromEmptyProxy(
ProtocolMetadata calldata initialMetadata,
Thresholds calldata initialThresholds,
KmsNode[] calldata initialKmsNodes,
Coprocessor[] calldata initialCoprocessors,
Custodian[] calldata initialCustodians
) public virtual onlyFromEmptyProxy reinitializer(REINITIALIZER_VERSION) {
__Ownable_init(owner());
GatewayConfigStorage storage $ = _getGatewayConfigStorage();
$.protocolMetadata = initialMetadata;
// Set the KMS nodes and their thresholds
_setKmsNodes(
initialKmsNodes,
initialThresholds.mpcThreshold,
initialThresholds.publicDecryptionThreshold,
initialThresholds.userDecryptionThreshold,
initialThresholds.kmsGenThreshold
);
// Set the coprocessors and their threshold
_setCoprocessors(initialCoprocessors, initialThresholds.coprocessorThreshold);
// Set the custodians
_setCustodians(initialCustodians);
emit InitializeGatewayConfig(
initialMetadata,
initialThresholds,
initialKmsNodes,
initialCoprocessors,
initialCustodians
);
}
/**
* @notice Re-initializes the contract from V3.
* @dev Define a `reinitializeVX` function once the contract needs to be upgraded.
*/
/// @custom:oz-upgrades-unsafe-allow missing-initializer-call
/// @custom:oz-upgrades-validate-as-initializer
function reinitializeV4() public virtual reinitializer(REINITIALIZER_VERSION) {}
/**
* @notice See {IGatewayConfig-isPauser}.
*/
function isPauser(address account) public view virtual returns (bool) {
return PAUSER_SET.isPauser(account);
}
/**
* @notice See {IGatewayConfig-updateKmsNodes}.
*/
function updateKmsNodes(
KmsNode[] calldata newKmsNodes,
uint256 newMpcThreshold,
uint256 newPublicDecryptionThreshold,
uint256 newUserDecryptionThreshold,
uint256 newKmsGenThreshold
) public virtual onlyOwner {
GatewayConfigStorage storage $ = _getGatewayConfigStorage();
// Remove the old KMS nodes
uint256 oldKmsTxSenderAddressesLength = $.kmsTxSenderAddresses.length;
for (uint256 i = 0; i < oldKmsTxSenderAddressesLength; i++) {
$.isKmsTxSender[$.kmsTxSenderAddresses[i]] = false;
$.isKmsSigner[$.kmsSignerAddresses[i]] = false;
delete $.kmsNodes[$.kmsTxSenderAddresses[i]];
}
delete $.kmsTxSenderAddresses;
delete $.kmsSignerAddresses;
// Set the new KMS nodes and their thresholds
_setKmsNodes(
newKmsNodes,
newMpcThreshold,
newPublicDecryptionThreshold,
newUserDecryptionThreshold,
newKmsGenThreshold
);
emit UpdateKmsNodes(
newKmsNodes,
newMpcThreshold,
newPublicDecryptionThreshold,
newUserDecryptionThreshold,
newKmsGenThreshold
);
}
/**
* @notice See {IGatewayConfig-updateCoprocessors}.
*/
function updateCoprocessors(
Coprocessor[] calldata newCoprocessors,
uint256 newCoprocessorThreshold
) external virtual onlyOwner {
GatewayConfigStorage storage $ = _getGatewayConfigStorage();
// Remove the old coprocessors
uint256 oldCoprocessorTxSenderAddressesLength = $.coprocessorTxSenderAddresses.length;
for (uint256 i = 0; i < oldCoprocessorTxSenderAddressesLength; i++) {
$.isCoprocessorTxSender[$.coprocessorTxSenderAddresses[i]] = false;
$.isCoprocessorSigner[$.coprocessorSignerAddresses[i]] = false;
delete $.coprocessors[$.coprocessorTxSenderAddresses[i]];
}
delete $.coprocessorTxSenderAddresses;
delete $.coprocessorSignerAddresses;
// Set the new coprocessors and their threshold
_setCoprocessors(newCoprocessors, newCoprocessorThreshold);
emit UpdateCoprocessors(newCoprocessors, newCoprocessorThreshold);
}
/**
* @notice See {IGatewayConfig-updateCustodians}.
*/
function updateCustodians(Custodian[] calldata newCustodians) external virtual onlyOwner {
GatewayConfigStorage storage $ = _getGatewayConfigStorage();
// Remove the old custodians
uint256 oldCustodianTxSenderAddressesLength = $.custodianTxSenderAddresses.length;
for (uint256 i = 0; i < oldCustodianTxSenderAddressesLength; i++) {
$.isCustodianTxSender[$.custodianTxSenderAddresses[i]] = false;
$.isCustodianSigner[$.custodianSignerAddresses[i]] = false;
delete $.custodians[$.custodianTxSenderAddresses[i]];
}
delete $.custodianTxSenderAddresses;
delete $.custodianSignerAddresses;
// Set the new custodians
_setCustodians(newCustodians);
emit UpdateCustodians(newCustodians);
}
/**
* @notice See {IGatewayConfig-updateMpcThreshold}.
*/
function updateMpcThreshold(uint256 newMpcThreshold) external virtual onlyOwner {
_setMpcThreshold(newMpcThreshold);
emit UpdateMpcThreshold(newMpcThreshold);
}
/**
* @notice See {IGatewayConfig-updatePublicDecryptionThreshold}.
*/
function updatePublicDecryptionThreshold(uint256 newPublicDecryptionThreshold) external virtual onlyOwner {
_setPublicDecryptionThreshold(newPublicDecryptionThreshold);
emit UpdatePublicDecryptionThreshold(newPublicDecryptionThreshold);
}
/**
* @notice See {IGatewayConfig-updateUserDecryptionThreshold}.
*/
function updateUserDecryptionThreshold(uint256 newUserDecryptionThreshold) external virtual onlyOwner {
_setUserDecryptionThreshold(newUserDecryptionThreshold);
emit UpdateUserDecryptionThreshold(newUserDecryptionThreshold);
}
/**
* @notice See {IGatewayConfig-updateKmsGenThreshold}.
*/
function updateKmsGenThreshold(uint256 newKmsGenThreshold) external virtual onlyOwner {
_setKmsGenThreshold(newKmsGenThreshold);
emit UpdateKmsGenThreshold(newKmsGenThreshold);
}
/**
* @notice See {IGatewayConfig-updateCoprocessorThreshold}.
*/
function updateCoprocessorThreshold(uint256 newCoprocessorThreshold) external virtual onlyOwner {
_setCoprocessorThreshold(newCoprocessorThreshold);
emit UpdateCoprocessorThreshold(newCoprocessorThreshold);
}
/**
* @notice See {IGatewayConfig-addHostChain}.
*/
function addHostChain(HostChain calldata hostChain) external virtual onlyOwner {
if (hostChain.chainId == 0) {
revert InvalidNullChainId();
}
if (hostChain.chainId > MAX_CHAIN_ID) {
revert ChainIdNotUint64(hostChain.chainId);
}
GatewayConfigStorage storage $ = _getGatewayConfigStorage();
if ($.isHostChainRegistered[hostChain.chainId]) {
revert HostChainAlreadyRegistered(hostChain.chainId);
}
$.hostChains.push(hostChain);
$.isHostChainRegistered[hostChain.chainId] = true;
emit AddHostChain(hostChain);
}
/**
* @notice See {IGatewayConfig-pauseAllGatewayContracts}.
* Contracts that are technically pausable but do not provide any pausable functions are not
* paused. If all of the contracts are already paused, the function will revert.
*/
function pauseAllGatewayContracts() external virtual onlyPauser {
if (DECRYPTION.paused() && INPUT_VERIFICATION.paused()) {
revert AllGatewayContractsAlreadyPaused();
}
if (!DECRYPTION.paused()) {
DECRYPTION.pause();
}
if (!INPUT_VERIFICATION.paused()) {
INPUT_VERIFICATION.pause();
}
emit PauseAllGatewayContracts();
}
/**
* @notice See {IGatewayConfig-unpauseAllGatewayContracts}.
* If all of the contracts are not paused, the function will revert.
*/
function unpauseAllGatewayContracts() external virtual onlyOwner {
if (!DECRYPTION.paused() && !INPUT_VERIFICATION.paused()) {
revert AllGatewayContractsAlreadyUnpaused();
}
if (DECRYPTION.paused()) {
DECRYPTION.unpause();
}
if (INPUT_VERIFICATION.paused()) {
INPUT_VERIFICATION.unpause();
}
emit UnpauseAllGatewayContracts();
}
/**
* @notice See {IGatewayConfig-isKmsTxSender}.
*/
function isKmsTxSender(address txSenderAddress) external view virtual returns (bool) {
GatewayConfigStorage storage $ = _getGatewayConfigStorage();
return $.isKmsTxSender[txSenderAddress];
}
/**
* @notice See {IGatewayConfig-isKmsSigner}.
*/
function isKmsSigner(address signerAddress) external view virtual returns (bool) {
GatewayConfigStorage storage $ = _getGatewayConfigStorage();
return $.isKmsSigner[signerAddress];
}
/**
* @notice See {IGatewayConfig-isCoprocessorTxSender}.
*/
function isCoprocessorTxSender(address txSenderAddress) external view virtual returns (bool) {
GatewayConfigStorage storage $ = _getGatewayConfigStorage();
return $.isCoprocessorTxSender[txSenderAddress];
}
/**
* @notice See {IGatewayConfig-isCoprocessorSigner}.
*/
function isCoprocessorSigner(address signerAddress) external view virtual returns (bool) {
GatewayConfigStorage storage $ = _getGatewayConfigStorage();
return $.isCoprocessorSigner[signerAddress];
}
/**
* @notice See {IGatewayConfig-isCustodianTxSender}.
*/
function isCustodianTxSender(address txSenderAddress) external view virtual returns (bool) {
GatewayConfigStorage storage $ = _getGatewayConfigStorage();
return $.isCustodianTxSender[txSenderAddress];
}
/**
* @notice See {IGatewayConfig-isCustodianSigner}.
*/
function isCustodianSigner(address signerAddress) external view virtual returns (bool) {
GatewayConfigStorage storage $ = _getGatewayConfigStorage();
return $.isCustodianSigner[signerAddress];
}
/**
* @notice See {IGatewayConfig-isHostChainRegistered}.
*/
function isHostChainRegistered(uint256 chainId) external view virtual returns (bool) {
GatewayConfigStorage storage $ = _getGatewayConfigStorage();
return $.isHostChainRegistered[chainId];
}
/**
* @notice See {IGatewayConfig-getProtocolMetadata}.
*/
function getProtocolMetadata() external view virtual returns (ProtocolMetadata memory) {
GatewayConfigStorage storage $ = _getGatewayConfigStorage();
return $.protocolMetadata;
}
/**
* @notice See {IGatewayConfig-getMpcThreshold}.
*/
function getMpcThreshold() external view virtual returns (uint256) {
GatewayConfigStorage storage $ = _getGatewayConfigStorage();
return $.mpcThreshold;
}
/**
* @notice See {IGatewayConfig-getPublicDecryptionThreshold}.
*/
function getPublicDecryptionThreshold() external view virtual returns (uint256) {
GatewayConfigStorage storage $ = _getGatewayConfigStorage();
return $.publicDecryptionThreshold;
}
/**
* @notice See {IGatewayConfig-getUserDecryptionThreshold}.
*/
function getUserDecryptionThreshold() external view virtual returns (uint256) {
GatewayConfigStorage storage $ = _getGatewayConfigStorage();
return $.userDecryptionThreshold;
}
/**
* @notice See {IGatewayConfig-getKmsGenThreshold}.
*/
function getKmsGenThreshold() external view virtual returns (uint256) {
GatewayConfigStorage storage $ = _getGatewayConfigStorage();
return $.kmsGenThreshold;
}
/**
* @notice See {IGatewayConfig-getCoprocessorMajorityThreshold}.
*/
function getCoprocessorMajorityThreshold() external view virtual returns (uint256) {
GatewayConfigStorage storage $ = _getGatewayConfigStorage();
return $.coprocessorThreshold;
}
/**
* @notice See {IGatewayConfig-getKmsNode}.
*/
function getKmsNode(address kmsTxSenderAddress) external view virtual returns (KmsNode memory) {
GatewayConfigStorage storage $ = _getGatewayConfigStorage();
return $.kmsNodes[kmsTxSenderAddress];
}
/**
* @notice See {IGatewayConfig-getKmsTxSenders}.
*/
function getKmsTxSenders() external view virtual returns (address[] memory) {
GatewayConfigStorage storage $ = _getGatewayConfigStorage();
return $.kmsTxSenderAddresses;
}
/**
* @notice See {IGatewayConfig-getKmsSigners}.
*/
function getKmsSigners() external view virtual returns (address[] memory) {
GatewayConfigStorage storage $ = _getGatewayConfigStorage();
return $.kmsSignerAddresses;
}
/**
* @notice See {IGatewayConfig-getCoprocessor}.
*/
function getCoprocessor(address coprocessorTxSenderAddress) external view virtual returns (Coprocessor memory) {
GatewayConfigStorage storage $ = _getGatewayConfigStorage();
return $.coprocessors[coprocessorTxSenderAddress];
}
/**
* @notice See {IGatewayConfig-getCoprocessorTxSenders}.
*/
function getCoprocessorTxSenders() external view virtual returns (address[] memory) {
GatewayConfigStorage storage $ = _getGatewayConfigStorage();
return $.coprocessorTxSenderAddresses;
}
/**
* @notice See {IGatewayConfig-getCoprocessorSigners}.
*/
function getCoprocessorSigners() external view virtual returns (address[] memory) {
GatewayConfigStorage storage $ = _getGatewayConfigStorage();
return $.coprocessorSignerAddresses;
}
/**
* @notice See {IGatewayConfig-getHostChain}.
*/
function getHostChain(uint256 index) external view virtual returns (HostChain memory) {
GatewayConfigStorage storage $ = _getGatewayConfigStorage();
return $.hostChains[index];
}
/**
* @notice See {IGatewayConfig-getHostChains}.
*/
function getHostChains() external view virtual returns (HostChain[] memory) {
GatewayConfigStorage storage $ = _getGatewayConfigStorage();
return $.hostChains;
}
/**
* @notice See {IGatewayConfig-getCustodian}.
*/
function getCustodian(address custodianTxSenderAddress) external view virtual returns (Custodian memory) {
GatewayConfigStorage storage $ = _getGatewayConfigStorage();
return $.custodians[custodianTxSenderAddress];
}
/**
* @notice See {IGatewayConfig-getCustodianTxSenders}.
*/
function getCustodianTxSenders() external view virtual returns (address[] memory) {
GatewayConfigStorage storage $ = _getGatewayConfigStorage();
return $.custodianTxSenderAddresses;
}
/**
* @notice See {IGatewayConfig-getCustodianSigners}.
*/
function getCustodianSigners() external view virtual returns (address[] memory) {
GatewayConfigStorage storage $ = _getGatewayConfigStorage();
return $.custodianSignerAddresses;
}
/**
* @notice See {IGatewayConfig-getVersion}.
*/
function getVersion() external pure virtual returns (string memory) {
return
string(
abi.encodePacked(
CONTRACT_NAME,
" v",
Strings.toString(MAJOR_VERSION),
".",
Strings.toString(MINOR_VERSION),
".",
Strings.toString(PATCH_VERSION)
)
);
}
/**
* @notice Sets the KMS nodes and their thresholds.
* @param newKmsNodes The new KMS nodes.
* @param newMpcThreshold The new MPC threshold.
* @param newPublicDecryptionThreshold The new public decryption threshold.
* @param newUserDecryptionThreshold The new user decryption threshold.
* @param newKmsGenThreshold The new key and CRS generation threshold.
*/
function _setKmsNodes(
KmsNode[] calldata newKmsNodes,
uint256 newMpcThreshold,
uint256 newPublicDecryptionThreshold,
uint256 newUserDecryptionThreshold,
uint256 newKmsGenThreshold
) internal virtual {
if (newKmsNodes.length == 0) {
revert EmptyKmsNodes();
}
GatewayConfigStorage storage $ = _getGatewayConfigStorage();
// Register the new KMS nodes
for (uint256 i = 0; i < newKmsNodes.length; i++) {
address newKmsTxSenderAddress = newKmsNodes[i].txSenderAddress;
address newKmsSignerAddress = newKmsNodes[i].signerAddress;
// Check for KMS transaction sender and signer duplicates
if ($.isKmsTxSender[newKmsTxSenderAddress]) {
revert KmsTxSenderAlreadyRegistered(newKmsTxSenderAddress);
}
if ($.isKmsSigner[newKmsSignerAddress]) {
revert KmsSignerAlreadyRegistered(newKmsSignerAddress);
}
// Register transaction sender
$.isKmsTxSender[newKmsTxSenderAddress] = true;
$.kmsTxSenderAddresses.push(newKmsTxSenderAddress);
// Register KMS node
$.kmsNodes[newKmsTxSenderAddress] = newKmsNodes[i];
// Register signer
$.isKmsSigner[newKmsSignerAddress] = true;
$.kmsSignerAddresses.push(newKmsSignerAddress);
}
// Setting the thresholds should be done after the KMS nodes have been registered as the functions
// reading the `kmsSignerAddresses` array.
_setMpcThreshold(newMpcThreshold);
_setPublicDecryptionThreshold(newPublicDecryptionThreshold);
_setUserDecryptionThreshold(newUserDecryptionThreshold);
_setKmsGenThreshold(newKmsGenThreshold);
}
/**
* @notice Sets the coprocessors and their threshold.
* @param newCoprocessors The new coprocessors.
* @param newCoprocessorThreshold The new coprocessor threshold.
*/
function _setCoprocessors(
Coprocessor[] calldata newCoprocessors,
uint256 newCoprocessorThreshold
) internal virtual {
if (newCoprocessors.length == 0) {
revert EmptyCoprocessors();
}
GatewayConfigStorage storage $ = _getGatewayConfigStorage();
// Register the new coprocessors
for (uint256 i = 0; i < newCoprocessors.length; i++) {
address newCoprocessorTxSenderAddress = newCoprocessors[i].txSenderAddress;
address newCoprocessorSignerAddress = newCoprocessors[i].signerAddress;
// Check for coprocessor transaction sender and signer duplicates
if ($.isCoprocessorTxSender[newCoprocessorTxSenderAddress]) {
revert CoprocessorTxSenderAlreadyRegistered(newCoprocessorTxSenderAddress);
}
if ($.isCoprocessorSigner[newCoprocessorSignerAddress]) {
revert CoprocessorSignerAlreadyRegistered(newCoprocessorSignerAddress);
}
// Register coprocessor transaction sender
$.isCoprocessorTxSender[newCoprocessorTxSenderAddress] = true;
$.coprocessorTxSenderAddresses.push(newCoprocessorTxSenderAddress);
// Register coprocessor
$.coprocessors[newCoprocessorTxSenderAddress] = newCoprocessors[i];
// Register coprocessor signer
$.isCoprocessorSigner[newCoprocessorSignerAddress] = true;
$.coprocessorSignerAddresses.push(newCoprocessorSignerAddress);
}
// Setting the coprocessor threshold should be done after the coprocessors have been
// registered as the functions reading the `coprocessorSignerAddresses` array.
_setCoprocessorThreshold(newCoprocessorThreshold);
}
/**
* @notice Sets the custodians.
* @param newCustodians The new custodians.
*/
function _setCustodians(Custodian[] calldata newCustodians) internal virtual {
if (newCustodians.length == 0) {
revert EmptyCustodians();
}
GatewayConfigStorage storage $ = _getGatewayConfigStorage();
// Register the new custodians
for (uint256 i = 0; i < newCustodians.length; i++) {
address newCustodianTxSenderAddress = newCustodians[i].txSenderAddress;
address newCustodianSignerAddress = newCustodians[i].signerAddress;
// Check for custodian transaction sender and signer duplicates
if ($.isCustodianTxSender[newCustodianTxSenderAddress]) {
revert CustodianTxSenderAlreadyRegistered(newCustodianTxSenderAddress);
}
if ($.isCustodianSigner[newCustodianSignerAddress]) {
revert CustodianSignerAlreadyRegistered(newCustodianSignerAddress);
}
// Register custodian transaction sender
$.isCustodianTxSender[newCustodianTxSenderAddress] = true;
$.custodianTxSenderAddresses.push(newCustodianTxSenderAddress);
// Register custodian
$.custodians[newCustodianTxSenderAddress] = newCustodians[i];
// Register custodian signer
$.isCustodianSigner[newCustodianSignerAddress] = true;
$.custodianSignerAddresses.push(newCustodianSignerAddress);
}
}
/**
* @notice Sets the MPC threshold.
* @param newMpcThreshold The new MPC threshold.
*/
function _setMpcThreshold(uint256 newMpcThreshold) internal virtual {
GatewayConfigStorage storage $ = _getGatewayConfigStorage();
uint256 nKmsNodes = $.kmsSignerAddresses.length;
// Check that the MPC threshold `t` is valid. It must verify:
// - `t >= 0` : it is already a uint256 so this is always true
// - `t < n` : it should be strictly less than the number of registered KMS nodes
if (newMpcThreshold >= nKmsNodes) {
revert InvalidHighMpcThreshold(newMpcThreshold, nKmsNodes);
}
$.mpcThreshold = newMpcThreshold;
}
/**
* @notice Sets the public decryption threshold.
* @param newPublicDecryptionThreshold The new public decryption threshold.
*/
function _setPublicDecryptionThreshold(uint256 newPublicDecryptionThreshold) internal virtual {
GatewayConfigStorage storage $ = _getGatewayConfigStorage();
uint256 nKmsNodes = $.kmsSignerAddresses.length;
// Check that the public decryption threshold `t` is valid. It must verify:
// - `t >= 1` : the public decryption consensus should require at least one vote
// - `t <= n` : it should be less than or equal to the number of registered KMS nodes
if (newPublicDecryptionThreshold == 0) {
revert InvalidNullPublicDecryptionThreshold();
}
if (newPublicDecryptionThreshold > nKmsNodes) {
revert InvalidHighPublicDecryptionThreshold(newPublicDecryptionThreshold, nKmsNodes);
}
$.publicDecryptionThreshold = newPublicDecryptionThreshold;
}
/**
* @notice Sets the user decryption threshold.
* @param newUserDecryptionThreshold The new user decryption threshold.
*/
function _setUserDecryptionThreshold(uint256 newUserDecryptionThreshold) internal virtual {
GatewayConfigStorage storage $ = _getGatewayConfigStorage();
uint256 nKmsNodes = $.kmsSignerAddresses.length;
// Check that the user decryption threshold `t` is valid. It must verify:
// - `t >= 1` : the user decryption consensus should require at least one vote
// - `t <= n` : it should be less than or equal to the number of registered KMS nodes
if (newUserDecryptionThreshold == 0) {
revert InvalidNullUserDecryptionThreshold();
}
if (newUserDecryptionThreshold > nKmsNodes) {
revert InvalidHighUserDecryptionThreshold(newUserDecryptionThreshold, nKmsNodes);
}
$.userDecryptionThreshold = newUserDecryptionThreshold;
}
/**
* @notice Sets the coprocessor threshold.
* @param newCoprocessorThreshold The new coprocessor threshold.
*/
function _setCoprocessorThreshold(uint256 newCoprocessorThreshold) internal virtual {
GatewayConfigStorage storage $ = _getGatewayConfigStorage();
uint256 nCoprocessors = $.coprocessorSignerAddresses.length;
// Check that the coprocessor threshold `t` is valid. It must verify:
// - `t >= 1` : the coprocessor consensus should require at least one vote
// - `t <= n` : it should be less than or equal to the number of registered coprocessors
if (newCoprocessorThreshold == 0) {
revert InvalidNullCoprocessorThreshold();
}
if (newCoprocessorThreshold > nCoprocessors) {
revert InvalidHighCoprocessorThreshold(newCoprocessorThreshold, nCoprocessors);
}
$.coprocessorThreshold = newCoprocessorThreshold;
}
/**
* @notice Sets the key and CRS generation threshold.
* @param newKmsGenThreshold The new key and CRS generation threshold.
*/
function _setKmsGenThreshold(uint256 newKmsGenThreshold) internal virtual {
GatewayConfigStorage storage $ = _getGatewayConfigStorage();
uint256 nKmsNodes = $.kmsSignerAddresses.length;
// Check that the key and CRS generation threshold `t` is valid. It must verify:
// - `t >= 1` : the key and CRS generation consensus should require at least one vote
// - `t <= n` : it should be less than or equal to the number of registered KMS nodes
if (newKmsGenThreshold == 0) {
revert InvalidNullKmsGenThreshold();
}
if (newKmsGenThreshold > nKmsNodes) {
revert InvalidHighKmsGenThreshold(newKmsGenThreshold, nKmsNodes);
}
$.kmsGenThreshold = newKmsGenThreshold;
}
/**
* @notice Checks if the sender is authorized to upgrade the contract and reverts otherwise.
*/
// solhint-disable-next-line no-empty-blocks
function _authorizeUpgrade(address _newImplementation) internal virtual override onlyOwner {}
/**
* @notice Returns the GatewayConfig storage location.
* @dev Note that this function is internal but not virtual: derived contracts should be able to
* access it, but if the underlying storage struct version changes, we force them to define a new
* getter function and use that one instead in order to avoid overriding the storage location.
*/
function _getGatewayConfigStorage() internal pure returns (GatewayConfigStorage storage $) {
// solhint-disable-next-line no-inline-assembly
assembly {
$.slot := GATEWAY_CONFIG_STORAGE_LOCATION
}
}
}