@rootzero/contracts 1.13.0 → 1.15.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +87 -3
- package/Codec.sol +21 -0
- package/Commands.sol +14 -0
- package/Core.sol +8 -6
- package/Endpoints.sol +2 -7
- package/Events.sol +1 -2
- package/README.md +70 -37
- package/Utils.sol +2 -4
- package/annotations/Action.sol +17 -0
- package/annotations/Label.sol +23 -0
- package/annotations/Schema.sol +53 -0
- package/codec/Blocks.sol +1643 -0
- package/codec/Buffers.sol +165 -0
- package/codec/Decoders.sol +570 -0
- package/codec/Descriptors.sol +124 -0
- package/{blocks → codec}/Keys.sol +10 -18
- package/codec/Readers.sol +114 -0
- package/{blocks → codec}/Schema.sol +43 -80
- package/codec/Specs.sol +240 -0
- package/codec/Writers.sol +485 -0
- package/commands/Allocate.sol +21 -20
- package/commands/Base.sol +83 -45
- package/commands/Burn.sol +23 -14
- package/commands/Credit.sol +21 -20
- package/commands/Debit.sol +25 -28
- package/commands/Deposit.sol +41 -36
- package/commands/Payout.sol +26 -16
- package/commands/Provision.sol +37 -38
- package/commands/Recover.sol +20 -19
- package/commands/Relay.sol +24 -22
- package/commands/Withdraw.sol +20 -19
- package/commands/admin/AllowAssets.sol +19 -16
- package/commands/admin/Allowance.sol +18 -13
- package/commands/admin/Annotate.sol +36 -0
- package/commands/admin/Appoint.sol +19 -16
- package/commands/admin/Authorize.sol +23 -14
- package/commands/admin/Base.sol +9 -2
- package/commands/admin/DenyAssets.sol +19 -16
- package/commands/admin/Dismiss.sol +19 -16
- package/commands/admin/Execute.sol +21 -19
- package/commands/admin/Unauthorize.sol +23 -14
- package/core/Access.sol +91 -49
- package/core/Calls.sol +30 -33
- package/core/Endpoint.sol +46 -143
- package/core/Host.sol +63 -21
- package/core/Pipeline.sol +16 -15
- package/core/Types.sol +1 -1
- package/docs/Schema.md +48 -31
- package/events/Annotation.sol +24 -0
- package/events/Endpoint.sol +2 -2
- package/events/Guardian.sol +2 -2
- package/events/Introduction.sol +3 -2
- package/execution/Budget.sol +40 -0
- package/execution/Execution.sol +1104 -0
- package/guards/Base.sol +11 -12
- package/guards/Revoke.sol +11 -9
- package/package.json +1 -1
- package/ports/AllowAssets.sol +12 -15
- package/ports/Allowance.sol +11 -9
- package/ports/Base.sol +35 -16
- package/ports/Credit.sol +11 -9
- package/ports/Debit.sol +11 -9
- package/ports/DenyAssets.sol +10 -13
- package/ports/Dispatch.sol +13 -15
- package/ports/Pipe.sol +14 -12
- package/ports/Redeem.sol +12 -9
- package/ports/Settle.sol +16 -10
- package/queries/Assets.sol +15 -15
- package/queries/Balances.sol +17 -17
- package/queries/Base.sol +26 -12
- package/utils/Accounts.sol +0 -23
- package/utils/Actions.sol +1 -0
- package/utils/Cursors.sol +367 -0
- package/utils/Lanes.sol +14 -0
- package/utils/Layout.sol +0 -2
- package/utils/Selectors.sol +2 -2
- package/utils/Utils.sol +46 -0
- package/Cursors.sol +0 -16
- package/blocks/Cursors.sol +0 -1529
- package/blocks/Writers.sol +0 -1036
- package/commands/admin/Label.sol +0 -32
- package/commands/admin/Schemas.sol +0 -32
- package/core/Payable.sol +0 -53
- package/events/Labeled.sol +0 -21
- package/events/Schema.sol +0 -23
- package/utils/Value.sol +0 -43
package/codec/Specs.sol
ADDED
|
@@ -0,0 +1,240 @@
|
|
|
1
|
+
// SPDX-License-Identifier: GPL-3.0-only
|
|
2
|
+
pragma solidity ^0.8.33;
|
|
3
|
+
|
|
4
|
+
import {Keys} from "./Keys.sol";
|
|
5
|
+
import {max24, replace8, replace32} from "../utils/Utils.sol";
|
|
6
|
+
|
|
7
|
+
/// @title Sizes
|
|
8
|
+
/// @notice Total byte sizes for fixed-width block types, including the 8-byte header (4-byte key + 4-byte payloadLen).
|
|
9
|
+
library Sizes {
|
|
10
|
+
/// @dev Shared block header size: 4-byte key + 4-byte payload length.
|
|
11
|
+
uint constant Header = 8;
|
|
12
|
+
/// @dev One fixed-width payload word.
|
|
13
|
+
uint constant Word = 32;
|
|
14
|
+
/// @dev 8 header + 32 payload = 40 bytes total.
|
|
15
|
+
uint constant B32 = Header + Word;
|
|
16
|
+
/// @dev 8 header + 64 payload = 72 bytes total.
|
|
17
|
+
uint constant B64 = Header + 2 * Word;
|
|
18
|
+
/// @dev 8 header + 96 payload = 104 bytes total.
|
|
19
|
+
uint constant B96 = Header + 3 * Word;
|
|
20
|
+
/// @dev 8 header + 128 payload = 136 bytes total.
|
|
21
|
+
uint constant B128 = Header + 4 * Word;
|
|
22
|
+
/// @dev 8 header + 160 payload = 168 bytes total.
|
|
23
|
+
uint constant B160 = Header + 5 * Word;
|
|
24
|
+
/// @dev STATUS block: 8 header + 32 status code = 40 bytes
|
|
25
|
+
uint constant Status = B32;
|
|
26
|
+
/// @dev AMOUNT block: 8 header + 32 asset + 32 amount = 72 bytes
|
|
27
|
+
uint constant Amount = B64;
|
|
28
|
+
/// @dev BALANCE block: 8 header + 32 asset + 32 amount = 72 bytes
|
|
29
|
+
uint constant Balance = B64;
|
|
30
|
+
/// @dev ALLOCATION/CUSTODY block: 8 header + 32 host + 32 asset + 32 amount = 104 bytes
|
|
31
|
+
uint constant HostAmount = B96;
|
|
32
|
+
/// @dev TRANSACTION block: 8 header + 32 from + 32 to + 32 asset + 32 amount = 136 bytes
|
|
33
|
+
uint constant Transaction = B128;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/// @title Specs
|
|
37
|
+
/// @notice Word-aligned block specifications encoded as
|
|
38
|
+
/// `[key:4][min:4][max:4][hint:3][stride:1][container:4][reserved:12]`.
|
|
39
|
+
/// The upper eight bytes of a fixed-layout spec are its encoded block header,
|
|
40
|
+
/// allowing the entire spec word to be written directly as that header.
|
|
41
|
+
/// A maximum of zero means unbounded and requires a growable writer.
|
|
42
|
+
library Specs {
|
|
43
|
+
/// @dev A payload is incompatible with its block specification.
|
|
44
|
+
error InvalidSpec();
|
|
45
|
+
/// @dev A direct specification cannot contain a wrapper block.
|
|
46
|
+
error InvalidContainer();
|
|
47
|
+
|
|
48
|
+
uint private constant SizeFields = (uint(1) << 192) | (uint(1) << 160) | (uint(1) << 136);
|
|
49
|
+
|
|
50
|
+
// Reusable field shapes keep public specs readable while remaining valid
|
|
51
|
+
// compile-time constant expressions.
|
|
52
|
+
uint private constant Exact32 = 32 * SizeFields;
|
|
53
|
+
uint private constant Exact64 = 64 * SizeFields;
|
|
54
|
+
uint private constant Exact96 = 96 * SizeFields;
|
|
55
|
+
uint private constant Exact128 = 128 * SizeFields;
|
|
56
|
+
uint private constant UnboundedHint128 = uint(128) << 136;
|
|
57
|
+
uint private constant UnboundedMin40Hint256 = (uint(40) << 192) | (uint(256) << 136);
|
|
58
|
+
uint private constant UnboundedMin72Hint256 = (uint(72) << 192) | (uint(256) << 136);
|
|
59
|
+
uint private constant UnboundedMin48Hint512 = (uint(48) << 192) | (uint(512) << 136);
|
|
60
|
+
uint private constant UnboundedMin104Hint256 = (uint(104) << 192) | (uint(256) << 136);
|
|
61
|
+
|
|
62
|
+
uint constant Empty = uint(bytes32(Keys.Empty));
|
|
63
|
+
uint constant Any = uint(bytes32(Keys.Any)) | UnboundedHint128;
|
|
64
|
+
uint constant Amount = uint(bytes32(Keys.Amount)) | Exact64;
|
|
65
|
+
uint constant Balance = uint(bytes32(Keys.Balance)) | Exact64;
|
|
66
|
+
uint constant Allocation = uint(bytes32(Keys.Allocation)) | Exact96;
|
|
67
|
+
uint constant Allowance = uint(bytes32(Keys.Allowance)) | Exact96;
|
|
68
|
+
uint constant Custody = uint(bytes32(Keys.Custody)) | Exact96;
|
|
69
|
+
uint constant List = uint(bytes32(Keys.List)) | UnboundedHint128;
|
|
70
|
+
uint constant Evm = uint(bytes32(Keys.Evm)) | UnboundedHint128;
|
|
71
|
+
uint constant Bytes = uint(bytes32(Keys.Bytes)) | UnboundedHint128;
|
|
72
|
+
uint constant String = uint(bytes32(Keys.String)) | UnboundedHint128;
|
|
73
|
+
uint constant Account = uint(bytes32(Keys.Account)) | Exact32;
|
|
74
|
+
uint constant Transaction = uint(bytes32(Keys.Transaction)) | Exact128;
|
|
75
|
+
uint constant Step = uint(bytes32(Keys.Step)) | UnboundedMin72Hint256;
|
|
76
|
+
uint constant Relay = uint(bytes32(Keys.Relay)) | UnboundedMin72Hint256;
|
|
77
|
+
uint constant Context = uint(bytes32(Keys.Context)) | UnboundedMin48Hint512;
|
|
78
|
+
uint constant Recover = uint(bytes32(Keys.Recover)) | UnboundedMin104Hint256;
|
|
79
|
+
uint constant Dispatch = uint(bytes32(Keys.Dispatch)) | UnboundedMin72Hint256;
|
|
80
|
+
uint constant Call = uint(bytes32(Keys.Call)) | UnboundedMin72Hint256;
|
|
81
|
+
uint constant Asset = uint(bytes32(Keys.Asset)) | Exact32;
|
|
82
|
+
uint constant Node = uint(bytes32(Keys.Node)) | Exact32;
|
|
83
|
+
uint constant Label = uint(bytes32(Keys.Label)) | UnboundedMin40Hint256;
|
|
84
|
+
uint constant Annotation = uint(bytes32(Keys.Annotation)) | UnboundedMin40Hint256;
|
|
85
|
+
uint constant Action = uint(bytes32(Keys.Action)) | Exact32;
|
|
86
|
+
uint constant Schema = uint(bytes32(Keys.Schema)) | UnboundedMin72Hint256;
|
|
87
|
+
|
|
88
|
+
uint constant Status = uint(bytes32(Keys.Status)) | Exact32;
|
|
89
|
+
uint constant AccountAsset = uint(bytes32(Keys.AccountAsset)) | Exact64;
|
|
90
|
+
uint constant AccountAmount = uint(bytes32(Keys.AccountAmount)) | Exact96;
|
|
91
|
+
uint constant HostAmount = uint(bytes32(Keys.HostAmount)) | Exact96;
|
|
92
|
+
uint constant HostAccountAsset = uint(bytes32(Keys.HostAccountAsset)) | Exact96;
|
|
93
|
+
uint constant HostAccountAmount = uint(bytes32(Keys.HostAccountAmount)) | Exact128;
|
|
94
|
+
|
|
95
|
+
/// @notice Construct a block specification from its encoded fields.
|
|
96
|
+
/// @param blockkey Encoded block key.
|
|
97
|
+
/// @param min Minimum accepted payload length.
|
|
98
|
+
/// @param max Maximum accepted payload length; zero means unbounded.
|
|
99
|
+
/// @param hint Initial per-block payload capacity.
|
|
100
|
+
/// @return spec Packed block specification.
|
|
101
|
+
function create(bytes4 blockkey, uint32 min, uint32 max, uint32 hint) internal pure returns (uint spec) {
|
|
102
|
+
return create(uint32(blockkey), min, max, hint);
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
/// @notice Construct a block specification from its numeric key and encoded fields.
|
|
106
|
+
/// @param blockkey Numeric block key.
|
|
107
|
+
/// @param min Minimum accepted payload length.
|
|
108
|
+
/// @param max Maximum accepted payload length; zero means unbounded.
|
|
109
|
+
/// @param hint Initial per-block payload capacity.
|
|
110
|
+
/// @return spec Packed block specification.
|
|
111
|
+
function create(uint32 blockkey, uint32 min, uint32 max, uint32 hint) internal pure returns (uint spec) {
|
|
112
|
+
spec |= uint(blockkey) << 224;
|
|
113
|
+
spec |= uint(min) << 192;
|
|
114
|
+
spec |= uint(max) << 160;
|
|
115
|
+
spec |= uint(max24(hint)) << 136;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
/// @notice Construct an exact-size block specification from a numeric key.
|
|
119
|
+
/// @dev Sets the minimum, maximum, and allocation hint to `size`.
|
|
120
|
+
/// @param blockkey Numeric block key.
|
|
121
|
+
/// @param size Exact payload length and initial per-block payload capacity.
|
|
122
|
+
/// @return spec Packed exact-size block specification.
|
|
123
|
+
function create(uint32 blockkey, uint32 size) internal pure returns (uint spec) {
|
|
124
|
+
return create(blockkey, size, size, size);
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
/// @notice Decode the block key and accepted payload range from `spec`.
|
|
128
|
+
/// @param spec Packed block specification.
|
|
129
|
+
/// @return blockkey Encoded block key.
|
|
130
|
+
/// @return min Minimum accepted payload length.
|
|
131
|
+
/// @return max Maximum accepted payload length; zero means unbounded.
|
|
132
|
+
function decode(uint spec) internal pure returns (bytes4 blockkey, uint32 min, uint32 max) {
|
|
133
|
+
blockkey = bytes4(uint32(spec >> 224));
|
|
134
|
+
min = uint32(spec >> 192);
|
|
135
|
+
max = uint32(spec >> 160);
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
/// @notice Return the block key encoded in `spec`.
|
|
139
|
+
/// @param spec Packed block specification.
|
|
140
|
+
/// @return Encoded block key.
|
|
141
|
+
function key(uint spec) internal pure returns (bytes4) {
|
|
142
|
+
return bytes4(uint32(spec >> 224));
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
/// @notice Return the raw stride encoded in `spec`.
|
|
146
|
+
/// @dev Contextual defaults are applied by helpers such as `count`.
|
|
147
|
+
/// @param spec Packed block specification.
|
|
148
|
+
/// @return Encoded block stride.
|
|
149
|
+
function stride(uint spec) internal pure returns (uint8) {
|
|
150
|
+
return uint8(spec >> 128);
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
/// @notice Return the canonical form of `spec` with implicit defaults resolved.
|
|
154
|
+
/// @param spec Packed block specification.
|
|
155
|
+
/// @param direct Whether the specification must not contain a wrapper block.
|
|
156
|
+
/// @dev A present spec with an encoded zero stride receives stride one;
|
|
157
|
+
/// the empty spec remains unchanged.
|
|
158
|
+
/// @return Canonical specification.
|
|
159
|
+
function normalize(uint spec, bool direct) internal pure returns (uint) {
|
|
160
|
+
if (direct && uint32(spec >> 96) != 0) revert InvalidContainer();
|
|
161
|
+
if (stride(spec) == 0 && key(spec) != bytes4(0)) spec |= uint(1) << 128;
|
|
162
|
+
return spec;
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
/// @notice Return the effective outer block key and optional wrapped child key.
|
|
166
|
+
/// @dev A direct spec returns its own key as `outer` and a zero `child`.
|
|
167
|
+
/// @param spec Packed block specification.
|
|
168
|
+
/// @return outer Effective outer block key.
|
|
169
|
+
/// @return child Wrapped child key, or zero for a direct specification.
|
|
170
|
+
function keys(uint spec) internal pure returns (bytes4 outer, bytes4 child) {
|
|
171
|
+
child = key(spec);
|
|
172
|
+
outer = bytes4(uint32(spec >> 96));
|
|
173
|
+
|
|
174
|
+
if (outer == bytes4(0)) {
|
|
175
|
+
outer = child;
|
|
176
|
+
child = bytes4(0);
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
/// @notice Return whether a payload size lies within a specification's bounds.
|
|
181
|
+
/// @param spec Packed block specification.
|
|
182
|
+
/// @param size Payload length to test.
|
|
183
|
+
/// @return Whether the payload length is accepted.
|
|
184
|
+
function accepts(uint spec, uint size) internal pure returns (bool) {
|
|
185
|
+
uint32 min = uint32(spec >> 192);
|
|
186
|
+
uint32 max = uint32(spec >> 160);
|
|
187
|
+
return size >= min && (max == 0 || size <= max);
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
/// @notice Validate a payload size against a specification's bounds.
|
|
191
|
+
/// @param spec Packed block specification.
|
|
192
|
+
/// @param size Payload length to validate.
|
|
193
|
+
function validate(uint spec, uint size) internal pure {
|
|
194
|
+
if (!accepts(spec, size)) revert InvalidSpec();
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
/// @notice Return an exact payload size constrained to an accepted range.
|
|
198
|
+
/// @param spec Packed block specification.
|
|
199
|
+
/// @param min Smallest exact size accepted by the caller.
|
|
200
|
+
/// @param max Largest exact size accepted by the caller.
|
|
201
|
+
/// @return size Exact payload size encoded by the specification.
|
|
202
|
+
function exact(uint spec, uint min, uint max) internal pure returns (uint size) {
|
|
203
|
+
size = uint32(spec >> 192);
|
|
204
|
+
if (size != uint32(spec >> 160) || size < min || size > max) revert InvalidSpec();
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
/// @notice Return the number of blocks represented by `groups`.
|
|
208
|
+
/// @param spec Packed block specification.
|
|
209
|
+
/// @param groups Number of groups.
|
|
210
|
+
/// @return Number of blocks across all groups.
|
|
211
|
+
function count(uint spec, uint groups) internal pure returns (uint) {
|
|
212
|
+
return groups * stride(normalize(spec, false));
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
/// @notice Return the buffer configuration for `groups` of `spec`.
|
|
216
|
+
/// @dev Dynamic specs use their allocation hint and a maximum of zero means growable.
|
|
217
|
+
/// @param spec Packed block specification.
|
|
218
|
+
/// @param groups Number of groups to allocate.
|
|
219
|
+
/// @return capacity Initial encoded byte capacity.
|
|
220
|
+
/// @return growable Whether the buffer may grow beyond its initial capacity.
|
|
221
|
+
function allocation(uint spec, uint groups) internal pure returns (uint capacity, bool growable) {
|
|
222
|
+
capacity = count(spec, groups) * (Sizes.Header + uint24(spec >> 136));
|
|
223
|
+
growable = uint32(spec >> 160) == 0;
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
/// @notice Return `spec` annotated as a generic LIST item.
|
|
227
|
+
/// @param spec Child block specification.
|
|
228
|
+
/// @return Specification wrapped in a LIST container.
|
|
229
|
+
function many(uint spec) internal pure returns (uint) {
|
|
230
|
+
return replace32(spec, 96, uint32(key(List)));
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
/// @notice Return `spec` grouped with an explicit stride.
|
|
234
|
+
/// @param spec Packed block specification.
|
|
235
|
+
/// @param n Number of blocks per group.
|
|
236
|
+
/// @return Specification with its stride replaced by `n`.
|
|
237
|
+
function group(uint spec, uint8 n) internal pure returns (uint) {
|
|
238
|
+
return replace8(spec, 128, n);
|
|
239
|
+
}
|
|
240
|
+
}
|