Repository navigation
Expand file tree
/
Copy pathTimelockLib.sol
More file actions
1413 lines (1275 loc) · 62.9 KB
/
Copy pathTimelockLib.sol
File metadata and controls
1413 lines (1275 loc) · 62.9 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
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
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// SPDX-License-Identifier: BUSL-1.1
// Copyright: OKcontract Pte. Ltd.
pragma solidity ^0.8.36;
import {SafeCast} from "@openzeppelin/contracts/utils/math/SafeCast.sol";
import {PagedIndex} from "../PagedIndex.sol";
import {GroupCriteria, Policy} from "../RegistryTypes.sol";
import {GovernanceData} from "./GovernanceData.sol";
import {PendingActionList} from "./PendingActionList.sol";
import {
ExecutionCatalogStorage,
GroupsStorage,
IndexingStorage,
PoliciesStorage,
StorageKeys
} from "./RegistryShared.sol";
import {DiamondCutStorage} from "./diamond/DiamondCutStorage.sol";
import {EnumerableSet} from "@openzeppelin/contracts/utils/structs/EnumerableSet.sol";
/// @title TimelockActionStatus
/// @notice Runtime status for tracked timelock actions.
enum TimelockActionStatus {
None,
Queued,
Ready,
Executing,
Executed,
Cancelled,
Expired
}
/// @title TimelockRequest
/// @notice Structured request parameters used when queueing a timelocked call.
/// @dev Mutating registry facets construct this struct and hand it to `TimelockLib.enforceOrQueue`.
struct TimelockRequest {
bytes32 category;
bytes32 scope;
bytes32 policyId;
uint64 delay;
uint64 gracePeriod;
}
/// @title TimelockResolvedConfig
/// @notice Resolved configuration after applying category defaults and overrides.
struct TimelockResolvedConfig {
uint64 delay;
uint64 gracePeriod;
bytes32 policyId;
}
/// @title TimelockApprovalGroupState
/// @notice Live approval progress for one group in a timelock action's current policy.
/// @dev `activeApprovals` is capped at `threshold`; approvals above the threshold do not affect readiness.
struct TimelockApprovalGroupState {
bytes32 groupHash;
uint256 threshold;
uint256 activeApprovals;
bool satisfied;
bool approverIsMember;
bool approverHasApproved;
bool approverCanContribute;
}
/// @title TimelockActionApprovalState
/// @notice Canonical live policy and approver state for one timelock action.
struct TimelockActionApprovalState {
bytes32 policyId;
TimelockActionStatus status;
bool policyExists;
bool approvalsSatisfied;
bool acceptingApprovals;
bool canApproverApprove;
bool approverWouldSatisfy;
TimelockApprovalGroupState[] groups;
}
/// @title ExecutionContextLib
/// @notice Tracks the logical actor and action metadata while a timelocked call is replaying or emulated.
/// @dev Facets use this to behave as though the original actor were calling directly, even though the actual call is
/// being replayed by the timelock executor. Immediate zero-delay paths that require context must enter it
/// explicitly, usually with a zero action id.
library ExecutionContextLib {
/// @notice Storage layout for the active timelock execution context.
struct Layout {
address actor;
bytes32 actionId;
bytes32 category;
bytes32 scope;
bool active;
}
/// @notice Return the namespaced execution-context storage.
function state() internal pure returns (Layout storage l) {
bytes32 slot = StorageKeys.EXECUTION_CONTEXT_SLOT;
assembly {
l.slot := slot
}
}
/// @notice Returns true when the current call is the Registry's timelock replay.
/// @dev The raw storage flag remains set across external callbacks made by the
/// replayed payload. Restricting visibility to Registry self-calls prevents
/// a callback from inheriting the queued actor or bypassing queue checks.
function isActive() internal view returns (bool) {
return state().active && msg.sender == address(this);
}
/// @notice Resolves the actor for a Registry self-replay, otherwise returns the real caller.
function actorOrSender() internal view returns (address) {
Layout storage l = state();
return l.active && msg.sender == address(this) ? l.actor : msg.sender;
}
/// @notice Returns the action id currently bound to the execution context.
function currentActionId() internal view returns (bytes32) {
return state().actionId;
}
/// @notice Returns the category currently bound to the execution context.
function currentCategory() internal view returns (bytes32) {
return state().category;
}
/// @notice Returns the scope currently bound to the execution context.
function currentScope() internal view returns (bytes32) {
return state().scope;
}
/// @notice Enters the execution context for an action, preventing nested re-entry.
function enter(bytes32 actionId, address actor_, bytes32 category, bytes32 scope) internal {
Layout storage l = state();
// Action status, semantics, and single execution context are per-action invariants and must fail closed.
// forge-lint: disable-next-line(require-revert-in-loop)
if (l.active) revert("TimelockCtx:active");
l.active = true;
l.actor = actor_;
l.actionId = actionId;
l.category = category;
l.scope = scope;
}
/// @notice Clear the active execution context after a timelocked call finishes.
function exit() internal {
Layout storage l = state();
l.active = false;
l.actor = address(0);
l.actionId = bytes32(0);
l.category = bytes32(0);
l.scope = bytes32(0);
}
/// @notice Ensures the stored execution context matches the expected category + scope pair.
function ensure(bytes32 category, bytes32 scope) internal view {
Layout storage l = state();
if (!isActive()) revert("TimelockCtx:inactive");
if (l.category != category) revert("TimelockCtx:category");
if (l.scope != scope) revert("TimelockCtx:scope");
}
}
/// @title TimelockConfigStorage
/// @notice Namespaced storage for category defaults, scope overrides, and policy reference counts.
/// @dev Categories define the broad rules for a type of action, while overrides let governance tighten or relax those
/// rules for a specific scope such as one contract key or one capability grant.
library TimelockConfigStorage {
/// @notice Default delay, grace, and policy settings for a whole action category.
struct CategoryConfig {
uint64 minDelay;
uint64 maxDelay;
uint64 defaultDelay;
uint64 gracePeriod;
bytes32 defaultPolicyId;
bool exists;
}
/// @notice User supplied overrides applied per category + scope pair.
struct ScopeOverride {
uint64 delay;
uint64 gracePeriod;
bytes32 policyId;
bool delaySet;
bool graceSet;
bool policySet;
}
/// @notice Full timelock configuration state.
struct Layout {
mapping(bytes32 => CategoryConfig) categories;
mapping(bytes32 => mapping(bytes32 => ScopeOverride)) overrides;
mapping(bytes32 => uint256) policyRefCount;
address policyProgramRunner;
}
/// @notice Return the namespaced timelock configuration storage.
function state() internal pure returns (Layout storage l) {
bytes32 slot = StorageKeys.TIMELOCK_CONFIG_SLOT;
assembly {
l.slot := slot
}
}
}
/// @title TimelockPayloadStorage
/// @notice Namespaced storage for calldata blobs queued behind the timelock.
/// @dev Payload bytes are stored separately from action metadata so queued action records remain compact.
library TimelockPayloadStorage {
/// @notice Storage layout for timelock calldata blobs keyed by action id.
struct Layout {
mapping(bytes32 => bytes) payloads;
}
/// @notice Return the namespaced payload storage.
function state() internal pure returns (Layout storage l) {
bytes32 slot = StorageKeys.TIMELOCK_PAYLOAD_SLOT;
assembly {
l.slot := slot
}
}
/// @notice Persist calldata for a queued action.
function store(bytes32 actionId, bytes memory payload) internal {
state().payloads[actionId] = payload;
}
/// @notice Load the queued calldata for `actionId`.
function load(bytes32 actionId) internal view returns (bytes memory) {
return state().payloads[actionId];
}
/// @notice Delete the stored calldata for `actionId`.
function clear(bytes32 actionId) internal {
delete state().payloads[actionId];
}
}
/// @title TimelockApprovalsStorage
/// @notice Namespaced storage for approval tallies attached to queued actions.
/// @dev Counts are stored per action and per policy group so the timelock can mirror the same threshold model used by
/// runtime operations.
library TimelockApprovalsStorage {
using EnumerableSet for EnumerableSet.AddressSet;
/// @notice Tracks approvals per action and per policy group.
struct Layout {
mapping(bytes32 => mapping(bytes32 => EnumerableSet.AddressSet)) groupApprovers;
}
/// @notice Return the namespaced approval-tally storage.
function state() internal pure returns (Layout storage l) {
bytes32 slot = StorageKeys.TIMELOCK_APPROVAL_SLOT;
assembly {
l.slot := slot
}
}
/// @notice Record an approver in one policy group.
function addGroupApprover(bytes32 actionId, bytes32 groupHash, address approver)
internal
returns (bool added, uint256 newCount)
{
Layout storage l = state();
added = l.groupApprovers[actionId][groupHash].add(approver);
newCount = l.groupApprovers[actionId][groupHash].length();
}
/// @notice Remove approvers that no longer belong to `groupHash`.
/// @dev Called before replacement approval so the stored set stays bounded across membership churn.
function compactGroupApprovers(bytes32 actionId, bytes32 groupHash) internal returns (uint256 liveCount) {
Layout storage l = state();
EnumerableSet.AddressSet storage approvers = l.groupApprovers[actionId][groupHash];
GroupsStorage.Layout storage groups = GroupsStorage.getLayout();
uint256 i = 0;
while (i < approvers.length()) {
address approver = approvers.at(i);
if (!GroupsStorage.isMember(groups, groupHash, approver)) {
approvers.remove(approver);
} else {
unchecked {
++i;
}
}
}
liveCount = approvers.length();
}
/// @dev Stop once `threshold` live approvals are found. This bounds the
/// execution-time scan by the approvals actually required by policy.
function liveGroupCount(bytes32 actionId, bytes32 groupHash, uint256 threshold)
internal
view
returns (uint256 count)
{
if (threshold == 0) return 0;
EnumerableSet.AddressSet storage approvers = state().groupApprovers[actionId][groupHash];
GroupsStorage.Layout storage groups = GroupsStorage.getLayout();
for (uint256 i; i < approvers.length(); ++i) {
if (GroupsStorage.isMember(groups, groupHash, approvers.at(i))) {
++count;
if (count == threshold) return count;
}
}
}
/// @notice Return the number of approvals recorded for one group within one action.
function groupCount(bytes32 actionId, bytes32 groupHash) internal view returns (uint256) {
return state().groupApprovers[actionId][groupHash].length();
}
/// @notice Return whether `approver` has a stored approval in one action policy group.
function hasGroupApproval(bytes32 actionId, bytes32 groupHash, address approver) internal view returns (bool) {
return state().groupApprovers[actionId][groupHash].contains(approver);
}
}
/// @title TimelockStorage
/// @notice Namespaced storage for queued and executed timelock actions.
/// @dev The paged indexes expose queue history to UIs and off-chain tooling without requiring log-only reconstruction.
library TimelockStorage {
using PagedIndex for PagedIndex.Index;
using EnumerableSet for EnumerableSet.Bytes32Set;
/// @notice Canonical representation of a queued timelock action.
struct Action {
bytes32 id;
bytes32 category;
bytes32 scope;
address actor;
bytes32 payloadHash;
bytes32 policyId;
uint64 scheduledAt;
uint64 delay;
uint64 eta;
uint64 expiresAt;
TimelockActionStatus status;
/// @notice Runtime-semantics generation under which this action was authorized.
uint64 semanticsGeneration;
}
struct CompactBundle {
address data;
uint8 policyPosition;
// Each child keeps its own readiness bit; approvals remain scoped by full action id.
uint256 ready;
}
/// @notice Full action queue state.
struct Layout {
mapping(bytes32 => Action) actions;
PagedIndex.Index pending;
PagedIndex.Index executed;
uint128 nonce;
mapping(bytes32 => EnumerableSet.Bytes32Set) pendingActionsByPolicy;
/// @dev Zero storage encodes the initial generation, one.
uint64 actionSemanticsGeneration;
// Conjunctive governance keeps each category/scope policy independently
// reviewable. All records in a bundle execute or terminate together.
mapping(bytes32 => bytes32) governanceCommitment;
mapping(bytes32 => bool) guardianCancellationBlocked;
// Compact bundles keep shared metadata and indexes on their root. Review ids use
// the root's reserved low 16 bits, and immutable code stores each scope/policy pair.
mapping(bytes32 => CompactBundle) compactBundleData;
mapping(bytes32 => PendingActionList.List) compactPendingPolicies;
}
/// @notice Return the namespaced core timelock storage.
function state() internal pure returns (Layout storage l) {
bytes32 slot = StorageKeys.TIMELOCK_CORE_SLOT;
assembly {
l.slot := slot
}
}
}
/// @title TimelockLib
/// @notice Shared queueing, approval, and execution helpers used by registry facets.
/// @dev Facets call `enforceOrQueue` before mutating state. If the relevant category/scope currently requires no
/// delay, execution continues immediately. Otherwise an action is queued, approvals accumulate against the
/// configured policy, and a later executor replays the original payload through the dispatcher.
library TimelockLib {
using PagedIndex for PagedIndex.Index;
using EnumerableSet for EnumerableSet.Bytes32Set;
error TimelockGovernanceChanged(bytes32 actionId);
event ActionBundled(bytes32 indexed rootActionId, bytes32 indexed actionId);
/// @notice A requirement without an approval policy is committed and timed by the root action.
event ActionRequirement(bytes32 indexed rootActionId, bytes32 indexed category, bytes32 indexed scope);
uint256 private constant REVIEW_MASK = type(uint16).max;
uint256 private constant REVIEW_BYTES = 66;
/// @notice Category key for organization writes.
bytes32 internal constant CATEGORY_ORG = keccak256("timelock.category.org");
/// @notice Category key for user allowlist writes.
bytes32 internal constant CATEGORY_USER = keccak256("timelock.category.user");
/// @notice Category key for group membership/configuration writes.
bytes32 internal constant CATEGORY_GROUP = keccak256("timelock.category.group");
/// @notice Category key for policy writes.
bytes32 internal constant CATEGORY_POLICY = keccak256("timelock.category.policy");
/// @notice Category key for contract directory writes.
bytes32 internal constant CATEGORY_CONTRACT = keccak256("timelock.category.contract");
/// @notice Category key for token directory writes.
bytes32 internal constant CATEGORY_TOKEN = keccak256("timelock.category.token");
/// @notice Category key for interaction bundle and private-commit writes.
bytes32 internal constant CATEGORY_INTERACTION = keccak256("timelock.category.interaction");
/// @notice Category key for contract/token/interaction activation changes.
bytes32 internal constant CATEGORY_ACTIVATION = keccak256("timelock.category.activation");
/// @notice Category key for metadata writes.
bytes32 internal constant CATEGORY_METADATA = keccak256("timelock.category.metadata");
/// @notice Category key for auth link writes.
bytes32 internal constant CATEGORY_AUTH = keccak256("timelock.category.auth");
/// @notice Category key for Safe allowlist writes.
bytes32 internal constant CATEGORY_SAFE = keccak256("timelock.category.safe");
/// @notice Category key for account allowlist writes.
bytes32 internal constant CATEGORY_ACCOUNT = keccak256("timelock.category.account");
/// @notice Category key for diamond upgrades and facet cuts.
bytes32 internal constant CATEGORY_DIAMOND = keccak256("timelock.category.diamond");
/// @notice Category key for governed registry role grants and revokes.
bytes32 internal constant CATEGORY_ROLE_ADMIN = keccak256("timelock.category.role.admin");
/// @notice Category key for timelock configuration changes themselves.
bytes32 internal constant CATEGORY_TIMELOCK = keccak256("timelock.category.config");
/// @notice Category key for capability grants/revokes.
bytes32 internal constant CATEGORY_CAPABILITY = keccak256("timelock.category.capability");
/// @notice Category key for source-registry allowlist changes.
bytes32 internal constant CATEGORY_SYNC_SOURCE = keccak256("timelock.category.sync.source");
/// @notice Category key for contract imports from approved source registries.
bytes32 internal constant CATEGORY_SYNC_CONTRACT = keccak256("timelock.category.sync.contract");
/// @notice Category key for token imports from approved source registries.
bytes32 internal constant CATEGORY_SYNC_TOKEN = keccak256("timelock.category.sync.token");
uint64 internal constant DEFAULT_MAX_DELAY = 30 days;
uint64 internal constant DEFAULT_GRACE_PERIOD = 30 days;
uint64 internal constant MAX_GRACE_PERIOD = type(uint24).max;
error TimelockScopeMismatch(bytes32 expected, bytes32 actual);
error TimelockCategoryMismatch(bytes32 expected, bytes32 actual);
error TimelockDelayOutOfBounds(uint64 delay, uint64 minDelay, uint64 maxDelay);
error TimelockDelayBelowRequiredMinimum(uint64 delay, uint64 requiredMinimum);
error TimelockCategoryNotConfigured(bytes32 category);
error TimelockAlreadyApproved(bytes32 actionId, address approver);
error TimelockNotEligible(bytes32 actionId, address approver);
error TimelockPolicyMissing(bytes32 policyId);
error TimelockWrongStatus(bytes32 actionId, TimelockActionStatus want, TimelockActionStatus got);
error TimelockExpired(bytes32 actionId);
error TimelockActionSemanticsStale(bytes32 actionId, uint64 queuedGeneration, uint64 currentGeneration);
event ActionQueued(
bytes32 indexed actionId,
bytes32 indexed category,
bytes32 indexed scope,
address actor,
uint64 eta,
uint64 expiresAt,
bytes32 payloadHash,
bytes32 policyId
);
event ActionReady(bytes32 indexed actionId);
event ActionCancelled(bytes32 indexed actionId, address indexed by);
event ActionExecuted(bytes32 indexed actionId, address indexed executor);
event ActionExpired(bytes32 indexed actionId);
event ActionSemanticsGenerationAdvanced(uint64 previousGeneration, uint64 newGeneration);
event ActionApproved(
bytes32 indexed actionId, address indexed approver, bytes32 indexed groupHash, uint256 newCount
);
function categoryOrg() internal pure returns (bytes32) {
return CATEGORY_ORG;
}
function categoryUser() internal pure returns (bytes32) {
return CATEGORY_USER;
}
function categoryGroup() internal pure returns (bytes32) {
return CATEGORY_GROUP;
}
function categoryPolicy() internal pure returns (bytes32) {
return CATEGORY_POLICY;
}
function categoryContract() internal pure returns (bytes32) {
return CATEGORY_CONTRACT;
}
function categoryToken() internal pure returns (bytes32) {
return CATEGORY_TOKEN;
}
function categoryInteraction() internal pure returns (bytes32) {
return CATEGORY_INTERACTION;
}
function categoryActivation() internal pure returns (bytes32) {
return CATEGORY_ACTIVATION;
}
function categoryMetadata() internal pure returns (bytes32) {
return CATEGORY_METADATA;
}
function categoryAuth() internal pure returns (bytes32) {
return CATEGORY_AUTH;
}
function categorySafe() internal pure returns (bytes32) {
return CATEGORY_SAFE;
}
function categoryAccount() internal pure returns (bytes32) {
return CATEGORY_ACCOUNT;
}
function categoryDiamond() internal pure returns (bytes32) {
return CATEGORY_DIAMOND;
}
function categoryRoleAdmin() internal pure returns (bytes32) {
return CATEGORY_ROLE_ADMIN;
}
function categoryTimelock() internal pure returns (bytes32) {
return CATEGORY_TIMELOCK;
}
function categoryCapability() internal pure returns (bytes32) {
return CATEGORY_CAPABILITY;
}
function categorySyncSource() internal pure returns (bytes32) {
return CATEGORY_SYNC_SOURCE;
}
function categorySyncContract() internal pure returns (bytes32) {
return CATEGORY_SYNC_CONTRACT;
}
function categorySyncToken() internal pure returns (bytes32) {
return CATEGORY_SYNC_TOKEN;
}
/// @notice Return whether `category` is part of the protocol-defined surface.
/// @dev Built-in bootstrap defaults are code-defined instead of materialized
/// as 20 storage records. This keeps Registry deployment below public
/// chain gas caps while unknown categories still fail closed.
function isProtocolCategory(bytes32 category) internal pure returns (bool) {
return category == CATEGORY_ORG || category == CATEGORY_USER || category == CATEGORY_GROUP
|| category == CATEGORY_POLICY || category == CATEGORY_CONTRACT || category == CATEGORY_TOKEN
|| category == CATEGORY_INTERACTION || category == CATEGORY_ACTIVATION || category == CATEGORY_METADATA
|| category == CATEGORY_AUTH || category == CATEGORY_SAFE || category == CATEGORY_ACCOUNT
|| category == CATEGORY_DIAMOND || category == CATEGORY_ROLE_ADMIN || category == CATEGORY_TIMELOCK
|| category == CATEGORY_CAPABILITY || category == CATEGORY_SYNC_SOURCE || category == CATEGORY_SYNC_CONTRACT
|| category == CATEGORY_SYNC_TOKEN;
}
/// @notice Resolve a stored category or the code-defined built-in default.
/// @dev The returned `exists` flag is false only for unknown, unconfigured
/// categories. Once governance stores a built-in override, storage wins.
function categoryConfig(bytes32 category)
internal
view
returns (TimelockConfigStorage.CategoryConfig memory config)
{
TimelockConfigStorage.CategoryConfig storage stored = TimelockConfigStorage.state().categories[category];
if (stored.exists) return stored;
if (!isProtocolCategory(category)) return config;
config.maxDelay = DEFAULT_MAX_DELAY;
config.gracePeriod = DEFAULT_GRACE_PERIOD;
config.exists = true;
}
function hasCategoryConfig(bytes32 category) internal view returns (bool) {
return categoryConfig(category).exists;
}
function maxGracePeriod() internal pure returns (uint64) {
return MAX_GRACE_PERIOD;
}
/// @notice Returns the number of timelock config entries currently referencing a policy.
function policyConfigRefCount(bytes32 policyId) internal view returns (uint256 count) {
count = TimelockConfigStorage.state().policyRefCount[policyId];
}
/// @notice Replaces one policy reference in timelock config bookkeeping.
function replaceConfigPolicyRef(bytes32 oldPolicyId, bytes32 newPolicyId) internal {
if (oldPolicyId == newPolicyId) {
return;
}
TimelockConfigStorage.Layout storage cfg = TimelockConfigStorage.state();
if (oldPolicyId != bytes32(0)) {
cfg.policyRefCount[oldPolicyId] -= 1;
}
if (newPolicyId != bytes32(0)) {
cfg.policyRefCount[newPolicyId] += 1;
}
}
/// @notice Removes one policy reference from timelock config bookkeeping.
function removeConfigPolicyRef(bytes32 policyId) internal {
if (policyId == bytes32(0)) {
return;
}
TimelockConfigStorage.state().policyRefCount[policyId] -= 1;
}
/// @notice Return the number of pending actions using `policyId`, excluding
/// the action currently replaying through the timelock context.
function pendingPolicyRefCount(bytes32 policyId) internal view returns (uint256 count) {
if (policyId == bytes32(0)) return 0;
TimelockStorage.Layout storage store = TimelockStorage.state();
count = store.pendingActionsByPolicy[policyId].length()
+ PendingActionList.count(store.compactPendingPolicies[policyId]);
if (count != 0 && ExecutionContextLib.isActive()) {
bytes32 currentActionId = ExecutionContextLib.currentActionId();
if (
store.pendingActionsByPolicy[policyId].contains(currentActionId)
|| PendingActionList.contains(store.compactPendingPolicies[policyId], currentActionId)
) {
--count;
}
}
}
/// @notice Return one pending action using `policyId`, excluding the action
/// currently replaying through the timelock context.
function pendingActionByPolicy(bytes32 policyId) internal view returns (bytes32 actionId) {
if (policyId == bytes32(0)) return bytes32(0);
TimelockStorage.Layout storage store = TimelockStorage.state();
EnumerableSet.Bytes32Set storage actions = store.pendingActionsByPolicy[policyId];
bytes32 currentActionId = ExecutionContextLib.isActive() ? ExecutionContextLib.currentActionId() : bytes32(0);
for (uint256 i; i < actions.length(); ++i) {
actionId = actions.at(i);
if (actionId != currentActionId) return actionId;
}
return PendingActionList.firstExcept(store.compactPendingPolicies[policyId], currentActionId);
}
/// @notice Return the generation used to bind queued actions to active runtime semantics.
/// @dev Derive the initial generation from zero storage without a redundant initialization write.
function actionSemanticsGeneration() internal view returns (uint64 generation) {
generation = TimelockStorage.state().actionSemanticsGeneration;
if (generation == 0) return 1;
}
/// @notice Invalidate every queued action authorized under earlier runtime semantics.
/// @dev Invoke only after transition postconditions pass. The action performing
/// the transition may finish, while sibling actions become stale immediately.
function bumpActionSemanticsGeneration() internal returns (uint64 nextGeneration) {
TimelockStorage.Layout storage store = TimelockStorage.state();
uint64 previousGeneration = actionSemanticsGeneration();
nextGeneration = previousGeneration + 1;
store.actionSemanticsGeneration = nextGeneration;
// Action/index helpers mutate local storage and emit together; execution callbacks finish before these
// final-state writes.
// forge-lint: disable-next-line(reentrancy-events)
emit ActionSemanticsGenerationAdvanced(previousGeneration, nextGeneration);
}
/// @notice Return whether an action was queued under current runtime semantics.
function isActionSemanticsCurrent(bytes32 actionId) internal view returns (bool) {
TimelockStorage.Action storage a = _sharedAction(actionId);
return a.status != TimelockActionStatus.None && a.semanticsGeneration == actionSemanticsGeneration();
}
/// @notice Revert unless an action was authorized under current runtime semantics.
function requireActionSemanticsCurrent(bytes32 actionId) internal view {
TimelockStorage.Action storage a = _sharedAction(actionId);
uint64 currentGeneration = actionSemanticsGeneration();
if (a.semanticsGeneration != currentGeneration) {
// Action status, semantics, and single execution context are per-action invariants and must fail
// closed.
// forge-lint: disable-next-line(require-revert-in-loop)
revert TimelockActionSemanticsStale(actionId, a.semanticsGeneration, currentGeneration);
}
}
/// @notice Resolves the effective delay / grace / policy by applying category defaults + scope overrides.
/// @dev Protocol categories have code-defined bootstrap defaults. Any unknown
/// category fails closed until governance intentionally configures it.
function _resolveConfig(TimelockRequest memory req, uint64 requiredMinimum)
private
view
returns (TimelockResolvedConfig memory resolved)
{
TimelockConfigStorage.Layout storage cfg = TimelockConfigStorage.state();
TimelockConfigStorage.CategoryConfig memory cat = categoryConfig(req.category);
if (!cat.exists) {
// Every bundled request must have explicit or protocol-defined governance; none may be skipped.
// forge-lint: disable-next-line(require-revert-in-loop)
revert TimelockCategoryNotConfigured(req.category);
}
uint64 delay = req.delay != 0 ? req.delay : cat.defaultDelay;
bytes32 policyId = req.policyId != bytes32(0) ? req.policyId : cat.defaultPolicyId;
uint64 grace = req.gracePeriod != 0 ? req.gracePeriod : cat.gracePeriod;
TimelockConfigStorage.ScopeOverride storage ov = cfg.overrides[req.category][req.scope];
if (ov.delaySet) {
delay = ov.delay;
}
if (ov.policySet) {
policyId = ov.policyId;
}
if (ov.graceSet) {
grace = ov.gracePeriod;
}
if (grace == 0) {
grace = DEFAULT_GRACE_PERIOD;
}
// Preserve the resolved tenant delay before applying the protocol floor.
// The resulting delay must still satisfy the category's bounds.
if (delay < requiredMinimum) delay = requiredMinimum;
if (delay < cat.minDelay || delay > cat.maxDelay) {
// Resolve each requirement within its own bounds before combining; invalid requirements reject all.
// forge-lint: disable-next-line(require-revert-in-loop)
revert TimelockDelayOutOfBounds(delay, cat.minDelay, cat.maxDelay);
}
resolved.delay = delay;
resolved.gracePeriod = grace;
resolved.policyId = policyId;
}
/// @notice Applies timelock enforcement for a call, queueing it when a delay or policy is required.
/// @dev Unknown categories fail closed. Immediate execution does not enter
/// `ExecutionContextLib`; callers that require context must wrap their
/// immediate execution path themselves.
/// @param req Timelock metadata describing the category/scope/delay overrides.
/// @param callDataBlob Encoded calldata that will be dispatched once the action executes.
/// @return executing True when the call may execute immediately.
/// @return queuedActionId The action id assigned when the call is queued (zero when executing immediately).
function enforceOrQueue(TimelockRequest memory req, bytes memory callDataBlob)
internal
returns (bool executing, bytes32 queuedActionId)
{
return _enforceOrQueue(req, callDataBlob, 0);
}
/// @notice Enforce a protocol-level minimum that category overrides cannot weaken.
/// @dev Used for constitutional changes whose delay is part of the contract's
/// security model. Longer tenant delays are preserved.
function enforceOrQueueWithMinimum(TimelockRequest memory req, bytes memory callDataBlob, uint64 requiredMinimum)
internal
returns (bool executing, bytes32 queuedActionId)
{
return _enforceOrQueue(req, callDataBlob, requiredMinimum);
}
function _enforceOrQueue(TimelockRequest memory req, bytes memory callDataBlob, uint64 requiredMinimum)
private
returns (bool executing, bytes32 queuedActionId)
{
if (ExecutionContextLib.isActive()) {
if (ExecutionContextLib.currentCategory() != req.category) {
revert TimelockCategoryMismatch(ExecutionContextLib.currentCategory(), req.category);
}
if (ExecutionContextLib.currentScope() != req.scope) {
revert TimelockScopeMismatch(ExecutionContextLib.currentScope(), req.scope);
}
uint64 queuedDelay = TimelockStorage.state().actions[ExecutionContextLib.currentActionId()].delay;
if (queuedDelay < requiredMinimum) {
revert TimelockDelayBelowRequiredMinimum(queuedDelay, requiredMinimum);
}
return (true, ExecutionContextLib.currentActionId());
}
TimelockResolvedConfig memory resolved = _resolveConfig(req, requiredMinimum);
if (resolved.delay == 0 && resolved.policyId == bytes32(0)) {
// Every immediate Registry mutation advances the transaction-local
// guard before control returns to the calling facet. A failed or
// no-op mutation rolls this write back with the surrounding call.
ExecutionCatalogStorage.bumpSemantics();
return (true, bytes32(0));
}
return (false, _queue(req, resolved, callDataBlob, false));
}
/// @notice Require every affected category/scope, preserving independent approval policies and programs.
/// @dev Each request is resolved against its own bounds and overrides before combining the longest delay
/// and shortest grace window. The first record supplies the sole replay context and payload dispatch.
/// The commitment binds the affected authorities; changing them while queued requires renewed review.
function enforceOrQueueBundle(
TimelockRequest[] memory requests,
bytes memory callDataBlob,
bytes32 commitment,
bool blockGuardianCancellation
) internal returns (bool executing, bytes32 rootActionId) {
TimelockStorage.Layout storage store = TimelockStorage.state();
if (ExecutionContextLib.isActive()) {
(executing, rootActionId) = enforceOrQueue(requests[0], callDataBlob);
if (store.governanceCommitment[rootActionId] != commitment) {
revert TimelockGovernanceChanged(rootActionId);
}
return (executing, rootActionId);
}
TimelockResolvedConfig[] memory resolved = new TimelockResolvedConfig[](requests.length);
uint64 delay = 0;
uint64 grace = type(uint64).max;
bool needsQueue = false;
uint256 actionCount = 1;
for (uint256 i; i < requests.length; ++i) {
resolved[i] = _resolveConfig(requests[i], 0);
if (resolved[i].delay > delay) delay = resolved[i].delay;
if (resolved[i].gracePeriod < grace) grace = resolved[i].gracePeriod;
if (resolved[i].delay != 0 || resolved[i].policyId != bytes32(0)) needsQueue = true;
if (i != 0 && resolved[i].policyId != bytes32(0)) ++actionCount;
}
if (!needsQueue) {
ExecutionCatalogStorage.bumpSemantics();
return (true, bytes32(0));
}
resolved[0].delay = delay;
resolved[0].gracePeriod = grace;
rootActionId = _queue(requests[0], resolved[0], callDataBlob, true);
bytes memory reviews = new bytes((actionCount - 1) * REVIEW_BYTES);
uint8[] memory positions = new uint8[](requests.length);
uint256 reviewIndex = 0;
for (uint256 i; i < requests.length; ++i) {
// Every scope still contributes its bounds, delay, grace, and replay commitment.
// Only a nonzero policy needs independent approval state and a policy-program context.
if (i != 0 && resolved[i].policyId == bytes32(0)) {
// Resolution and queueing use local storage and STATICCALL-only authority checks.
// forge-lint: disable-next-line(reentrancy-events)
emit ActionRequirement(rootActionId, requests[i].category, requests[i].scope);
continue;
}
bytes32 actionId = rootActionId;
bytes32 policyId = resolved[i].policyId;
if (policyId != bytes32(0)) {
positions[i] = _registerBundlePolicy(store, rootActionId, resolved, positions, i);
}
if (i != 0) {
_writeReview(reviews, reviewIndex, requests[i].category, requests[i].scope, policyId, positions[i]);
actionId = bytes32(uint256(rootActionId) | ++reviewIndex);
TimelockStorage.Action storage root = store.actions[rootActionId];
// Queue construction only queries authority with STATICCALL and updates local storage.
// forge-lint: disable-start(reentrancy-events)
emit ActionQueued(
actionId,
requests[i].category,
requests[i].scope,
root.actor,
root.eta,
root.expiresAt,
root.payloadHash,
policyId
);
// forge-lint: disable-end(reentrancy-events)
}
// Resolution and queueing touch local storage; any preceding authority call is STATICCALL-only.
// forge-lint: disable-next-line(reentrancy-events)
emit ActionBundled(rootActionId, actionId);
}
// Address one is a sentinel for a compact bundle with no independent policy reviews.
store.compactBundleData[rootActionId] = TimelockStorage.CompactBundle({
data: reviews.length == 0 ? address(1) : address(new GovernanceData(reviews)),
policyPosition: positions[0],
ready: 0
});
store.governanceCommitment[rootActionId] = commitment;
store.guardianCancellationBlocked[rootActionId] = blockGuardianCancellation;
}
function _registerBundlePolicy(
TimelockStorage.Layout storage store,
bytes32 root,
TimelockResolvedConfig[] memory resolved,
uint8[] memory positions,
uint256 index
) private returns (uint8) {
bytes32 policyId = resolved[index].policyId;
for (uint256 i; i < index; ++i) {
if (resolved[i].policyId == policyId) return positions[i];
}
return PendingActionList.add(store.compactPendingPolicies[policyId], root);
}
/// @notice Return the one action whose payload is dispatched for this governance bundle.
function bundleRoot(bytes32 actionId) internal view returns (bytes32) {
bytes32 compactRoot = bytes32(uint256(actionId) & ~REVIEW_MASK);
address pointer = TimelockStorage.state().compactBundleData[compactRoot].data;
if (pointer != address(0) && (uint256(actionId) & REVIEW_MASK) <= _reviewCount(pointer)) return compactRoot;
return actionId;
}
/// @notice Return every independently reviewed record, or just the unbundled action.
function bundleActions(bytes32 actionId) internal view returns (bytes32[] memory ids) {
bytes32 root = bundleRoot(actionId);
address pointer = TimelockStorage.state().compactBundleData[root].data;
if (pointer != address(0)) {
ids = new bytes32[](_reviewCount(pointer) + 1);
for (uint256 i; i < ids.length; ++i) {
ids[i] = bytes32(uint256(root) | i);
}
return ids;
}
ids = new bytes32[](1);
ids[0] = root;
}
function _queue(
TimelockRequest memory req,
TimelockResolvedConfig memory resolved,
bytes memory callDataBlob,
bool compactBundle
) private returns (bytes32 queuedActionId) {
TimelockStorage.Layout storage store = TimelockStorage.state();
address actor = ExecutionContextLib.actorOrSender();
bytes32 payloadHash;
assembly ("memory-safe") {
payloadHash := keccak256(add(callDataBlob, 0x20), mload(callDataBlob))
}
uint128 nonce = store.nonce;
store.nonce = nonce + 1;
bytes32 category = req.category;
bytes32 scopeValue = req.scope;
uint256 nonceWord = uint256(nonce);
assembly ("memory-safe") {
let ptr := mload(0x40)
mstore(ptr, actor)
mstore(add(ptr, 0x20), category)
mstore(add(ptr, 0x40), scopeValue)
mstore(add(ptr, 0x60), payloadHash)
mstore(add(ptr, 0x80), nonceWord)
queuedActionId := keccak256(ptr, 0xa0)
mstore(0x40, add(ptr, 0xa0))
}
if (compactBundle) queuedActionId = bytes32(uint256(queuedActionId) & ~REVIEW_MASK);
TimelockStorage.Action storage a = store.actions[queuedActionId];
if (a.status != TimelockActionStatus.None) {
// Action ids bind actor, payload, scope, and monotonic nonce; overwriting any record is forbidden.
revert("Timelock:action-exists");
}
uint64 scheduledAt = SafeCast.toUint64(block.timestamp);
uint64 eta = _saturatingAdd(scheduledAt, resolved.delay);
uint64 expiresAt = resolved.gracePeriod == 0 ? type(uint64).max : _saturatingAdd(eta, resolved.gracePeriod);
a.id = queuedActionId;
a.category = req.category;
a.scope = req.scope;
a.actor = actor;
a.payloadHash = payloadHash;
a.policyId = resolved.policyId;
a.scheduledAt = scheduledAt;
a.delay = resolved.delay;
a.eta = eta;
a.expiresAt = expiresAt;
a.status = TimelockActionStatus.Queued;
a.semanticsGeneration = actionSemanticsGeneration();
TimelockPayloadStorage.store(queuedActionId, callDataBlob);
store.pending.insert(queuedActionId);
if (!compactBundle && resolved.policyId != bytes32(0)) {
bool added = store.pendingActionsByPolicy[resolved.policyId].add(queuedActionId);
assert(added);
}
// Action/index helpers mutate local storage and emit together; execution callbacks finish before these
// final-state writes.
// forge-lint: disable-start(reentrancy-events)
emit ActionQueued(
queuedActionId, req.category, req.scope, actor, eta, expiresAt, payloadHash, resolved.policyId
);
// forge-lint: disable-end(reentrancy-events)
}
/// @dev Timelock timestamps use uint64 storage. Saturation preserves a
/// configured long delay instead of making an otherwise valid queue revert.
function _saturatingAdd(uint64 left, uint64 right) private pure returns (uint64) {
unchecked {
uint64 sum = left + right;
return sum < left ? type(uint64).max : sum;
}
}
function _reviewCount(address pointer) private view returns (uint256) {
return pointer == address(1) ? 0 : (pointer.code.length - 1) / REVIEW_BYTES;
}
function _writeReview(
bytes memory data,
uint256 index,
bytes32 category,
bytes32 scope,
bytes32 policyId,
uint8 position
) private pure {
uint256 kind = category == CATEGORY_GROUP ? 0 : category == CATEGORY_CAPABILITY ? 1 : 2;
assert(category == CATEGORY_GROUP || category == CATEGORY_CAPABILITY || category == CATEGORY_POLICY);
uint256 offset = index * REVIEW_BYTES;
assembly ("memory-safe") {
let ptr := add(add(data, 32), offset)
mstore8(ptr, kind)