@rootzero/contracts 1.12.0 → 1.14.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 +76 -8
- package/Codec.sol +21 -0
- package/Commands.sol +14 -0
- package/Core.sol +4 -4
- package/Endpoints.sol +5 -7
- package/README.md +62 -35
- package/Utils.sol +2 -4
- package/codec/Blocks.sol +1606 -0
- package/codec/Buffers.sol +165 -0
- package/codec/Decoders.sol +558 -0
- package/codec/Descriptors.sol +124 -0
- package/{blocks → codec}/Keys.sol +4 -28
- package/codec/Readers.sol +114 -0
- package/{blocks → codec}/Schema.sol +38 -80
- package/codec/Specs.sol +218 -0
- package/codec/Writers.sol +487 -0
- package/commands/Allocate.sol +50 -0
- package/commands/Base.sol +71 -37
- package/commands/Burn.sol +19 -13
- package/commands/Credit.sol +25 -31
- package/commands/Debit.sol +28 -38
- package/commands/Deposit.sol +39 -39
- package/commands/Payout.sol +22 -15
- package/commands/Provision.sol +40 -40
- package/commands/Recover.sol +21 -20
- package/commands/Relay.sol +25 -23
- package/commands/Withdraw.sol +18 -20
- package/commands/admin/AllowAssets.sol +20 -16
- package/commands/admin/Allowance.sol +19 -13
- package/commands/admin/Appoint.sol +18 -15
- package/commands/admin/Authorize.sol +24 -14
- package/commands/admin/Base.sol +1 -1
- package/commands/admin/DenyAssets.sol +20 -16
- package/commands/admin/Dismiss.sol +18 -15
- package/commands/admin/Execute.sol +20 -18
- package/commands/admin/Label.sol +18 -13
- package/commands/admin/Schemas.sol +19 -14
- package/commands/admin/Unauthorize.sol +24 -14
- package/core/Calls.sol +7 -14
- package/core/Endpoint.sol +43 -126
- package/core/Host.sol +10 -2
- package/core/Pipeline.sol +29 -25
- package/core/Settlement.sol +39 -0
- package/core/Types.sol +1 -1
- package/docs/Schema.md +38 -32
- package/events/Endpoint.sol +2 -2
- package/events/Introduction.sol +4 -3
- package/events/Schema.sol +5 -5
- package/execution/Budget.sol +40 -0
- package/execution/Execution.sol +1083 -0
- package/guards/Base.sol +8 -9
- 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 +18 -11
- 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 +13 -13
- package/queries/Assets.sol +15 -15
- package/queries/Balances.sol +17 -17
- package/queries/Base.sol +11 -12
- package/utils/Actions.sol +1 -0
- package/utils/Cursors.sol +308 -0
- package/utils/Lanes.sol +14 -0
- package/utils/Selectors.sol +2 -2
- package/utils/Utils.sol +46 -0
- package/Cursors.sol +0 -16
- package/blocks/Cursors.sol +0 -1400
- package/blocks/Writers.sol +0 -1028
- package/core/Payable.sol +0 -53
- package/utils/Value.sol +0 -43
|
@@ -0,0 +1,487 @@
|
|
|
1
|
+
// SPDX-License-Identifier: GPL-3.0-only
|
|
2
|
+
pragma solidity ^0.8.33;
|
|
3
|
+
|
|
4
|
+
import {AssetAmount, AccountAmount, HostAmount, Tx} from "../core/Types.sol";
|
|
5
|
+
import {Blocks} from "./Blocks.sol";
|
|
6
|
+
import {Buffers} from "./Buffers.sol";
|
|
7
|
+
import {Sizes, Specs} from "./Specs.sol";
|
|
8
|
+
|
|
9
|
+
/// @notice Sequential block stream writer backed by a pre-allocated memory buffer.
|
|
10
|
+
struct Writer {
|
|
11
|
+
/// @dev Packed cursor metadata. `len` is logical capacity and flag bit 0
|
|
12
|
+
/// indicates whether the backing buffer may grow.
|
|
13
|
+
uint cur;
|
|
14
|
+
/// @dev Destination buffer. Physical capacity may be padded up to a full 32-byte word;
|
|
15
|
+
/// final length is set to the packed write position by `finish`.
|
|
16
|
+
bytes dst;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/// @title Writers
|
|
20
|
+
/// @notice Response block stream builder for the rootzero protocol.
|
|
21
|
+
/// Initializes logical capacity from raw metadata or a block specification,
|
|
22
|
+
/// then lazily allocates and writes binary-encoded blocks sequentially.
|
|
23
|
+
/// Physical allocation is rounded up to whole 32-byte words for scratch space,
|
|
24
|
+
/// while the writer cursor length tracks logical capacity. Call `finish` to trim the buffer.
|
|
25
|
+
library Writers {
|
|
26
|
+
// -------------------------------------------------------------------------
|
|
27
|
+
// Initialization helpers
|
|
28
|
+
// -------------------------------------------------------------------------
|
|
29
|
+
|
|
30
|
+
/// @notice Initialize writer metadata without allocating a backing buffer.
|
|
31
|
+
/// @param len Initial logical byte capacity of the writer.
|
|
32
|
+
/// @param growable Whether append helpers may expand the capacity.
|
|
33
|
+
/// @return writer Unallocated writer positioned at index 0.
|
|
34
|
+
function init(uint len, bool growable) internal pure returns (Writer memory writer) {
|
|
35
|
+
writer.cur = Buffers.cursor(len, 0, growable, 0);
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/// @notice Initialize writer metadata for `groups` of blocks described by `spec`.
|
|
39
|
+
/// @param spec Packed block key, payload bounds, allocation hint, and flags.
|
|
40
|
+
/// @param groups Number of descriptor groups the writer is expected to encode.
|
|
41
|
+
/// @return writer Unallocated writer with capacity derived from the specification,
|
|
42
|
+
/// or an inert empty writer when its capacity is zero.
|
|
43
|
+
function init(uint spec, uint groups) internal pure returns (Writer memory writer) {
|
|
44
|
+
(uint capacity, bool growable) = Specs.allocation(spec, groups);
|
|
45
|
+
if (capacity == 0) return writer;
|
|
46
|
+
|
|
47
|
+
writer.cur = Buffers.cursor(capacity, groups, growable, 0);
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
// -------------------------------------------------------------------------
|
|
51
|
+
// Writer state
|
|
52
|
+
// -------------------------------------------------------------------------
|
|
53
|
+
|
|
54
|
+
/// @dev Reserve through a `Writer` and update it in place.
|
|
55
|
+
/// @param writer Writer whose cursor and buffer are updated.
|
|
56
|
+
/// @param advance Logical number of bytes written.
|
|
57
|
+
/// @param touch Number of bytes that must be addressable by the write.
|
|
58
|
+
/// @return i Relative offset reserved for the write.
|
|
59
|
+
function reserve(Writer memory writer, uint advance, uint touch) private pure returns (uint i) {
|
|
60
|
+
(writer.cur, writer.dst, i) = Buffers.reserve(writer.cur, writer.dst, advance, touch);
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/// @dev Reserve an exact number of bytes through `writer`.
|
|
64
|
+
/// @param writer Writer whose cursor and buffer are updated.
|
|
65
|
+
/// @param size Number of bytes to reserve and touch.
|
|
66
|
+
/// @return i Relative offset reserved for the write.
|
|
67
|
+
function reserve(Writer memory writer, uint size) private pure returns (uint i) {
|
|
68
|
+
return reserve(writer, size, size);
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
// -------------------------------------------------------------------------
|
|
72
|
+
// Append helpers
|
|
73
|
+
// -------------------------------------------------------------------------
|
|
74
|
+
|
|
75
|
+
/// @notice Append arbitrary bytes to the writer.
|
|
76
|
+
/// @param writer Destination writer; `i` is advanced by `data.length`.
|
|
77
|
+
/// @param data Bytes to append.
|
|
78
|
+
function append(Writer memory writer, bytes memory data) internal pure {
|
|
79
|
+
uint i = reserve(writer, data.length, data.length);
|
|
80
|
+
Buffers.write(writer.dst, i, data);
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/// @notice Append a raw 32-byte word without a block header.
|
|
84
|
+
/// @param writer Destination writer; `i` is advanced by `keep`.
|
|
85
|
+
/// @param value Word to append.
|
|
86
|
+
/// @param keep Number of bytes to keep from the word (1..32).
|
|
87
|
+
function append32(Writer memory writer, bytes32 value, uint keep) internal pure {
|
|
88
|
+
uint i = reserve(writer, keep, 32);
|
|
89
|
+
Buffers.write32(writer.dst, i, value);
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/// @notice Append two raw 32-byte words without a block header.
|
|
93
|
+
/// @param writer Destination writer; `i` is advanced by `32 + keep`.
|
|
94
|
+
/// @param a First word to append.
|
|
95
|
+
/// @param b Second word to append.
|
|
96
|
+
/// @param keep Number of bytes to keep from the final word (1..32).
|
|
97
|
+
function append64(Writer memory writer, bytes32 a, bytes32 b, uint keep) internal pure {
|
|
98
|
+
uint i = reserve(writer, 32 + keep, 64);
|
|
99
|
+
Buffers.write64(writer.dst, i, a, b);
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
/// @notice Append three raw 32-byte words without a block header.
|
|
103
|
+
/// @param writer Destination writer; `i` is advanced by `64 + keep`.
|
|
104
|
+
/// @param a First word to append.
|
|
105
|
+
/// @param b Second word to append.
|
|
106
|
+
/// @param c Third word to append.
|
|
107
|
+
/// @param keep Number of bytes to keep from the final word (1..32).
|
|
108
|
+
function append96(Writer memory writer, bytes32 a, bytes32 b, bytes32 c, uint keep) internal pure {
|
|
109
|
+
uint i = reserve(writer, 64 + keep, 96);
|
|
110
|
+
Buffers.write96(writer.dst, i, a, b, c);
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
/// @notice Append a dynamic protocol block.
|
|
114
|
+
/// @param writer Destination writer.
|
|
115
|
+
/// @param spec Block specification used to validate and encode the payload.
|
|
116
|
+
/// @param data Block payload.
|
|
117
|
+
function appendBlock(Writer memory writer, uint spec, bytes memory data) internal pure {
|
|
118
|
+
Specs.validate(spec, data.length);
|
|
119
|
+
uint size = Sizes.Header + data.length;
|
|
120
|
+
uint i = reserve(writer, size, size);
|
|
121
|
+
Blocks.write(writer.dst, i, Specs.key(spec), data);
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
/// @notice Append a custom fixed block with up to 32 payload bytes.
|
|
125
|
+
/// @param writer Destination writer.
|
|
126
|
+
/// @param spec Exact block specification.
|
|
127
|
+
/// @param a Payload word.
|
|
128
|
+
function appendBlock32(Writer memory writer, uint spec, bytes32 a) internal pure {
|
|
129
|
+
uint keep = Specs.exact(spec, 1, 32);
|
|
130
|
+
uint i = reserve(writer, Sizes.Header + keep, Sizes.B32);
|
|
131
|
+
Blocks.write32(writer.dst, i, Specs.key(spec), a);
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
/// @notice Append an ACCOUNT block.
|
|
135
|
+
/// @param writer Destination writer.
|
|
136
|
+
/// @param account Account identifier to encode.
|
|
137
|
+
function appendAccount(Writer memory writer, bytes32 account) internal pure {
|
|
138
|
+
uint i = reserve(writer, Sizes.B32);
|
|
139
|
+
Blocks.writeAccount(writer.dst, i, account);
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
/// @notice Append an ASSET block.
|
|
143
|
+
/// @param writer Destination writer.
|
|
144
|
+
/// @param asset Asset identifier to encode.
|
|
145
|
+
function appendAsset(Writer memory writer, bytes32 asset) internal pure {
|
|
146
|
+
uint i = reserve(writer, Sizes.B32);
|
|
147
|
+
Blocks.writeAsset(writer.dst, i, asset);
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
/// @notice Append a NODE block.
|
|
151
|
+
/// @param writer Destination writer.
|
|
152
|
+
/// @param node Node identifier to encode.
|
|
153
|
+
function appendNode(Writer memory writer, uint node) internal pure {
|
|
154
|
+
uint i = reserve(writer, Sizes.B32);
|
|
155
|
+
Blocks.writeNode(writer.dst, i, node);
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
/// @notice Append a STATUS block.
|
|
159
|
+
/// @param writer Destination writer.
|
|
160
|
+
/// @param code Status code to encode.
|
|
161
|
+
function appendStatus(Writer memory writer, uint code) internal pure {
|
|
162
|
+
uint i = reserve(writer, Sizes.B32);
|
|
163
|
+
Blocks.writeStatus(writer.dst, i, code);
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
/// @notice Append an AMOUNT block.
|
|
167
|
+
/// @param writer Destination writer.
|
|
168
|
+
/// @param asset Asset identifier to encode.
|
|
169
|
+
/// @param amount Asset amount to encode.
|
|
170
|
+
function appendAmount(Writer memory writer, bytes32 asset, uint amount) internal pure {
|
|
171
|
+
uint i = reserve(writer, Sizes.B64);
|
|
172
|
+
Blocks.writeAmount(writer.dst, i, asset, amount);
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
/// @notice Append a structured AMOUNT value.
|
|
176
|
+
/// @param writer Destination writer.
|
|
177
|
+
/// @param value Structured asset amount to encode.
|
|
178
|
+
function appendAmount(Writer memory writer, AssetAmount memory value) internal pure {
|
|
179
|
+
appendAmount(writer, value.asset, value.amount);
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
/// @notice Append a BALANCE block.
|
|
183
|
+
/// @param writer Destination writer.
|
|
184
|
+
/// @param asset Asset identifier to encode.
|
|
185
|
+
/// @param amount Balance amount to encode.
|
|
186
|
+
function appendBalance(Writer memory writer, bytes32 asset, uint amount) internal pure {
|
|
187
|
+
uint i = reserve(writer, Sizes.B64);
|
|
188
|
+
Blocks.writeBalance(writer.dst, i, asset, amount);
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
/// @notice Append a structured BALANCE value.
|
|
192
|
+
/// @param writer Destination writer.
|
|
193
|
+
/// @param value Structured asset balance to encode.
|
|
194
|
+
function appendBalance(Writer memory writer, AssetAmount memory value) internal pure {
|
|
195
|
+
appendBalance(writer, value.asset, value.amount);
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
/// @notice Append an ACCOUNT_ASSET block.
|
|
199
|
+
/// @param writer Destination writer.
|
|
200
|
+
/// @param account Account identifier to encode.
|
|
201
|
+
/// @param asset Asset identifier to encode.
|
|
202
|
+
function appendAccountAsset(Writer memory writer, bytes32 account, bytes32 asset) internal pure {
|
|
203
|
+
uint i = reserve(writer, Sizes.B64);
|
|
204
|
+
Blocks.writeAccountAsset(writer.dst, i, account, asset);
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
/// @notice Append an ALLOCATION block.
|
|
208
|
+
/// @param writer Destination writer.
|
|
209
|
+
/// @param host Host identifier to encode.
|
|
210
|
+
/// @param asset Asset identifier to encode.
|
|
211
|
+
/// @param amount Allocation amount to encode.
|
|
212
|
+
function appendAllocation(Writer memory writer, uint host, bytes32 asset, uint amount) internal pure {
|
|
213
|
+
uint i = reserve(writer, Sizes.B96);
|
|
214
|
+
Blocks.writeAllocation(writer.dst, i, host, asset, amount);
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
/// @notice Append an ALLOWANCE block.
|
|
218
|
+
/// @param writer Destination writer.
|
|
219
|
+
/// @param host Host identifier to encode.
|
|
220
|
+
/// @param asset Asset identifier to encode.
|
|
221
|
+
/// @param amount Allowance amount to encode.
|
|
222
|
+
function appendAllowance(Writer memory writer, uint host, bytes32 asset, uint amount) internal pure {
|
|
223
|
+
uint i = reserve(writer, Sizes.B96);
|
|
224
|
+
Blocks.writeAllowance(writer.dst, i, host, asset, amount);
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
/// @notice Append a CUSTODY block.
|
|
228
|
+
/// @param writer Destination writer.
|
|
229
|
+
/// @param host Host identifier to encode.
|
|
230
|
+
/// @param asset Asset identifier to encode.
|
|
231
|
+
/// @param amount Custody amount to encode.
|
|
232
|
+
function appendCustody(Writer memory writer, uint host, bytes32 asset, uint amount) internal pure {
|
|
233
|
+
uint i = reserve(writer, Sizes.B96);
|
|
234
|
+
Blocks.writeCustody(writer.dst, i, host, asset, amount);
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
/// @notice Append a CUSTODY block for `host` and a structured amount.
|
|
238
|
+
/// @param writer Destination writer.
|
|
239
|
+
/// @param host Host identifier to encode.
|
|
240
|
+
/// @param value Structured asset amount to encode.
|
|
241
|
+
function appendCustody(Writer memory writer, uint host, AssetAmount memory value) internal pure {
|
|
242
|
+
appendCustody(writer, host, value.asset, value.amount);
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
/// @notice Append a structured CUSTODY value.
|
|
246
|
+
/// @param writer Destination writer.
|
|
247
|
+
/// @param value Structured host asset amount to encode.
|
|
248
|
+
function appendCustody(Writer memory writer, HostAmount memory value) internal pure {
|
|
249
|
+
appendCustody(writer, value.host, value.asset, value.amount);
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
/// @notice Append an ACCOUNT_AMOUNT block.
|
|
253
|
+
/// @param writer Destination writer.
|
|
254
|
+
/// @param account Account identifier to encode.
|
|
255
|
+
/// @param asset Asset identifier to encode.
|
|
256
|
+
/// @param amount Account amount to encode.
|
|
257
|
+
function appendAccountAmount(Writer memory writer, bytes32 account, bytes32 asset, uint amount) internal pure {
|
|
258
|
+
uint i = reserve(writer, Sizes.B96);
|
|
259
|
+
Blocks.writeAccountAmount(writer.dst, i, account, asset, amount);
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
/// @notice Append a structured ACCOUNT_AMOUNT value.
|
|
263
|
+
/// @param writer Destination writer.
|
|
264
|
+
/// @param value Structured account asset amount to encode.
|
|
265
|
+
function appendAccountAmount(Writer memory writer, AccountAmount memory value) internal pure {
|
|
266
|
+
appendAccountAmount(writer, value.account, value.asset, value.amount);
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
/// @notice Append a HOST_AMOUNT block.
|
|
270
|
+
/// @param writer Destination writer.
|
|
271
|
+
/// @param host Host identifier to encode.
|
|
272
|
+
/// @param asset Asset identifier to encode.
|
|
273
|
+
/// @param amount Host amount to encode.
|
|
274
|
+
function appendHostAmount(Writer memory writer, uint host, bytes32 asset, uint amount) internal pure {
|
|
275
|
+
uint i = reserve(writer, Sizes.B96);
|
|
276
|
+
Blocks.writeHostAmount(writer.dst, i, host, asset, amount);
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
/// @notice Append a HOST_ACCOUNT_ASSET block.
|
|
280
|
+
/// @param writer Destination writer.
|
|
281
|
+
/// @param host Host identifier to encode.
|
|
282
|
+
/// @param account Account identifier to encode.
|
|
283
|
+
/// @param asset Asset identifier to encode.
|
|
284
|
+
function appendHostAccountAsset(
|
|
285
|
+
Writer memory writer,
|
|
286
|
+
uint host,
|
|
287
|
+
bytes32 account,
|
|
288
|
+
bytes32 asset
|
|
289
|
+
) internal pure {
|
|
290
|
+
uint i = reserve(writer, Sizes.B96);
|
|
291
|
+
Blocks.writeHostAccountAsset(writer.dst, i, host, account, asset);
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
/// @notice Append a TRANSACTION block.
|
|
295
|
+
/// @param writer Destination writer.
|
|
296
|
+
/// @param from Debit account identifier.
|
|
297
|
+
/// @param to Credit account identifier.
|
|
298
|
+
/// @param asset Asset identifier to encode.
|
|
299
|
+
/// @param amount Transaction amount to encode.
|
|
300
|
+
function appendTransaction(
|
|
301
|
+
Writer memory writer,
|
|
302
|
+
bytes32 from,
|
|
303
|
+
bytes32 to,
|
|
304
|
+
bytes32 asset,
|
|
305
|
+
uint amount
|
|
306
|
+
) internal pure {
|
|
307
|
+
uint i = reserve(writer, Sizes.B128);
|
|
308
|
+
Blocks.writeTransaction(writer.dst, i, from, to, asset, amount);
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
/// @notice Append a structured TRANSACTION value.
|
|
312
|
+
/// @param writer Destination writer.
|
|
313
|
+
/// @param value Structured transaction to encode.
|
|
314
|
+
function appendTransaction(Writer memory writer, Tx memory value) internal pure {
|
|
315
|
+
appendTransaction(writer, value.from, value.to, value.asset, value.amount);
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
/// @notice Append a HOST_ACCOUNT_AMOUNT block.
|
|
319
|
+
/// @param writer Destination writer.
|
|
320
|
+
/// @param host Host identifier to encode.
|
|
321
|
+
/// @param account Account identifier to encode.
|
|
322
|
+
/// @param asset Asset identifier to encode.
|
|
323
|
+
/// @param amount Host account amount to encode.
|
|
324
|
+
function appendHostAccountAmount(
|
|
325
|
+
Writer memory writer,
|
|
326
|
+
uint host,
|
|
327
|
+
bytes32 account,
|
|
328
|
+
bytes32 asset,
|
|
329
|
+
uint amount
|
|
330
|
+
) internal pure {
|
|
331
|
+
uint i = reserve(writer, Sizes.B128);
|
|
332
|
+
Blocks.writeHostAccountAmount(writer.dst, i, host, account, asset, amount);
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
/// @notice Append a LIST block.
|
|
336
|
+
/// @param writer Destination writer.
|
|
337
|
+
/// @param value Encoded list payload.
|
|
338
|
+
function appendList(Writer memory writer, bytes memory value) internal pure {
|
|
339
|
+
uint size = Sizes.Header + value.length;
|
|
340
|
+
uint i = reserve(writer, size);
|
|
341
|
+
Blocks.writeList(writer.dst, i, value);
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
/// @notice Append an EVM block.
|
|
345
|
+
/// @param writer Destination writer.
|
|
346
|
+
/// @param value EVM payload to encode.
|
|
347
|
+
function appendEvm(Writer memory writer, bytes memory value) internal pure {
|
|
348
|
+
uint size = Sizes.Header + value.length;
|
|
349
|
+
uint i = reserve(writer, size);
|
|
350
|
+
Blocks.writeEvm(writer.dst, i, value);
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
/// @notice Append a BYTES block.
|
|
354
|
+
/// @param writer Destination writer.
|
|
355
|
+
/// @param value Byte payload to encode.
|
|
356
|
+
function appendBytes(Writer memory writer, bytes memory value) internal pure {
|
|
357
|
+
uint size = Sizes.Header + value.length;
|
|
358
|
+
uint i = reserve(writer, size);
|
|
359
|
+
Blocks.writeBytes(writer.dst, i, value);
|
|
360
|
+
}
|
|
361
|
+
|
|
362
|
+
/// @notice Append a STRING block.
|
|
363
|
+
/// @param writer Destination writer.
|
|
364
|
+
/// @param value String payload to encode.
|
|
365
|
+
function appendString(Writer memory writer, string memory value) internal pure {
|
|
366
|
+
uint size = Sizes.Header + bytes(value).length;
|
|
367
|
+
uint i = reserve(writer, size);
|
|
368
|
+
Blocks.writeString(writer.dst, i, value);
|
|
369
|
+
}
|
|
370
|
+
|
|
371
|
+
/// @notice Append a STEP block.
|
|
372
|
+
/// @param writer Destination writer.
|
|
373
|
+
/// @param cmd Command identifier to encode.
|
|
374
|
+
/// @param resources Packed resources to encode.
|
|
375
|
+
/// @param input Command input to encode.
|
|
376
|
+
function appendStep(Writer memory writer, uint cmd, uint resources, bytes memory input) internal pure {
|
|
377
|
+
uint size = Sizes.B64 + Sizes.Header + input.length;
|
|
378
|
+
uint i = reserve(writer, size);
|
|
379
|
+
Blocks.writeStep(writer.dst, i, cmd, resources, input);
|
|
380
|
+
}
|
|
381
|
+
|
|
382
|
+
/// @notice Append a CALL block.
|
|
383
|
+
/// @param writer Destination writer.
|
|
384
|
+
/// @param target Call target to encode.
|
|
385
|
+
/// @param resources Packed resources to encode.
|
|
386
|
+
/// @param payload Call payload to encode.
|
|
387
|
+
function appendCall(Writer memory writer, uint target, uint resources, bytes memory payload) internal pure {
|
|
388
|
+
uint size = Sizes.B64 + Sizes.Header + payload.length;
|
|
389
|
+
uint i = reserve(writer, size);
|
|
390
|
+
Blocks.writeCall(writer.dst, i, target, resources, payload);
|
|
391
|
+
}
|
|
392
|
+
|
|
393
|
+
/// @notice Append a RELAY block.
|
|
394
|
+
/// @param writer Destination writer.
|
|
395
|
+
/// @param portal Destination portal to encode.
|
|
396
|
+
/// @param resources Packed resources to encode.
|
|
397
|
+
/// @param input Relay input to encode.
|
|
398
|
+
function appendRelay(Writer memory writer, uint portal, uint resources, bytes memory input) internal pure {
|
|
399
|
+
uint size = Sizes.B64 + Sizes.Header + input.length;
|
|
400
|
+
uint i = reserve(writer, size);
|
|
401
|
+
Blocks.writeRelay(writer.dst, i, portal, resources, input);
|
|
402
|
+
}
|
|
403
|
+
|
|
404
|
+
/// @notice Append a DISPATCH block.
|
|
405
|
+
/// @param writer Destination writer.
|
|
406
|
+
/// @param portal Destination portal to encode.
|
|
407
|
+
/// @param resources Packed resources to encode.
|
|
408
|
+
/// @param payload Dispatch payload to encode.
|
|
409
|
+
function appendDispatch(Writer memory writer, uint portal, uint resources, bytes memory payload) internal pure {
|
|
410
|
+
uint size = Sizes.B64 + Sizes.Header + payload.length;
|
|
411
|
+
uint i = reserve(writer, size);
|
|
412
|
+
Blocks.writeDispatch(writer.dst, i, portal, resources, payload);
|
|
413
|
+
}
|
|
414
|
+
|
|
415
|
+
/// @notice Append a CONTEXT block.
|
|
416
|
+
/// @param writer Destination writer.
|
|
417
|
+
/// @param account Account identifier to encode.
|
|
418
|
+
/// @param state State payload to encode.
|
|
419
|
+
/// @param input Input payload to encode.
|
|
420
|
+
function appendContext(
|
|
421
|
+
Writer memory writer,
|
|
422
|
+
bytes32 account,
|
|
423
|
+
bytes memory state,
|
|
424
|
+
bytes memory input
|
|
425
|
+
) internal pure {
|
|
426
|
+
uint size = Sizes.B32 + 2 * Sizes.Header + state.length + input.length;
|
|
427
|
+
uint i = reserve(writer, size);
|
|
428
|
+
Blocks.writeContext(writer.dst, i, account, state, input);
|
|
429
|
+
}
|
|
430
|
+
|
|
431
|
+
/// @notice Append a RECOVER block.
|
|
432
|
+
/// @param writer Destination writer.
|
|
433
|
+
/// @param handler Recovery handler to encode.
|
|
434
|
+
/// @param resources Packed resources to encode.
|
|
435
|
+
/// @param recoverykey Recovery key to encode.
|
|
436
|
+
/// @param witness Recovery witness to encode.
|
|
437
|
+
function appendRecover(
|
|
438
|
+
Writer memory writer,
|
|
439
|
+
uint handler,
|
|
440
|
+
uint resources,
|
|
441
|
+
bytes32 recoverykey,
|
|
442
|
+
bytes memory witness
|
|
443
|
+
) internal pure {
|
|
444
|
+
uint size = Sizes.B96 + Sizes.Header + witness.length;
|
|
445
|
+
uint i = reserve(writer, size);
|
|
446
|
+
Blocks.writeRecover(writer.dst, i, handler, resources, recoverykey, witness);
|
|
447
|
+
}
|
|
448
|
+
|
|
449
|
+
/// @notice Append a LABEL block.
|
|
450
|
+
/// @param writer Destination writer.
|
|
451
|
+
/// @param id Node identifier to encode.
|
|
452
|
+
/// @param namespace Label namespace to encode.
|
|
453
|
+
/// @param name Label text to encode.
|
|
454
|
+
function appendLabel(
|
|
455
|
+
Writer memory writer,
|
|
456
|
+
uint id,
|
|
457
|
+
bytes32 namespace,
|
|
458
|
+
string memory name
|
|
459
|
+
) internal pure {
|
|
460
|
+
uint size = Sizes.B64 + Sizes.Header + bytes(name).length;
|
|
461
|
+
uint i = reserve(writer, size);
|
|
462
|
+
Blocks.writeLabel(writer.dst, i, id, namespace, name);
|
|
463
|
+
}
|
|
464
|
+
|
|
465
|
+
/// @notice Append a SCHEMA block.
|
|
466
|
+
/// @param writer Destination writer.
|
|
467
|
+
/// @param spec Block specification to encode.
|
|
468
|
+
/// @param body Schema body to encode.
|
|
469
|
+
/// @param name Schema name to encode.
|
|
470
|
+
function appendSchema(Writer memory writer, uint spec, string memory body, bytes32 name) internal pure {
|
|
471
|
+
uint size = Sizes.B64 + Sizes.Header + bytes(body).length;
|
|
472
|
+
uint i = reserve(writer, size);
|
|
473
|
+
Blocks.writeSchema(writer.dst, i, spec, body, name);
|
|
474
|
+
}
|
|
475
|
+
|
|
476
|
+
// -------------------------------------------------------------------------
|
|
477
|
+
// Finalisation
|
|
478
|
+
// -------------------------------------------------------------------------
|
|
479
|
+
|
|
480
|
+
/// @notice Return an empty buffer when unused, otherwise trim `dst` to the bytes written.
|
|
481
|
+
/// Sets the `bytes` length slot in memory to the packed write position without copying.
|
|
482
|
+
/// @param writer Completed writer.
|
|
483
|
+
/// @return out The written block stream; length equals the packed write position.
|
|
484
|
+
function finish(Writer memory writer) internal pure returns (bytes memory out) {
|
|
485
|
+
out = Buffers.finish(writer.cur, writer.dst);
|
|
486
|
+
}
|
|
487
|
+
}
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
// SPDX-License-Identifier: GPL-3.0-only
|
|
2
|
+
pragma solidity ^0.8.33;
|
|
3
|
+
|
|
4
|
+
import {Execution, Executions, CommandBase, HostAmount, Lanes, Specs} from "./Base.sol";
|
|
5
|
+
|
|
6
|
+
using Executions for Execution;
|
|
7
|
+
|
|
8
|
+
/// @notice Shared allocation hook used by `Allocate`.
|
|
9
|
+
abstract contract AllocateHook {
|
|
10
|
+
/// @notice Override to allocate a live balance into custody on a host.
|
|
11
|
+
/// Called once per paired BALANCE state block and NODE input block.
|
|
12
|
+
/// Implementations should perform only the custody side effect; output
|
|
13
|
+
/// blocks are written by the caller.
|
|
14
|
+
/// @param account Caller's account identifier.
|
|
15
|
+
/// @param custody Host-scoped amount to allocate into custody.
|
|
16
|
+
function allocate(bytes32 account, HostAmount memory custody) internal virtual;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/// @title Allocate
|
|
20
|
+
/// @notice Command that allocates BALANCE state to custody on requested hosts.
|
|
21
|
+
/// Each BALANCE state block is paired with one NODE input block at the same
|
|
22
|
+
/// position; the output is a matching CUSTODY state stream.
|
|
23
|
+
abstract contract Allocate is CommandBase, AllocateHook {
|
|
24
|
+
uint private immutable descriptor;
|
|
25
|
+
|
|
26
|
+
constructor() {
|
|
27
|
+
(, descriptor) = command("allocate", Specs.Balance, Specs.Node, Specs.Custody, 0, false, false);
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/// @notice Allocate BALANCE state blocks to matching NODE input blocks.
|
|
31
|
+
/// @param state BALANCE block stream.
|
|
32
|
+
/// @param input Matching NODE block stream.
|
|
33
|
+
/// @return CUSTODY block stream matching the allocated balances.
|
|
34
|
+
/// @return Empty transaction stream.
|
|
35
|
+
function allocate(
|
|
36
|
+
bytes32 account,
|
|
37
|
+
bytes calldata state,
|
|
38
|
+
bytes calldata input
|
|
39
|
+
) external onlyCommand returns (bytes memory, bytes memory) {
|
|
40
|
+
Execution memory exec = openCommand(state, input, descriptor, 0);
|
|
41
|
+
|
|
42
|
+
while (exec.more()) {
|
|
43
|
+
HostAmount memory custody = exec.unpackBalanceForHost(Lanes.State, exec.unpackNode(Lanes.Input));
|
|
44
|
+
allocate(account, custody);
|
|
45
|
+
exec.outputCustody(custody);
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
return close(exec, account);
|
|
49
|
+
}
|
|
50
|
+
}
|