@rootzero/contracts 1.13.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.
Files changed (73) hide show
  1. package/CHANGELOG.md +45 -3
  2. package/Codec.sol +21 -0
  3. package/Commands.sol +14 -0
  4. package/Core.sol +2 -3
  5. package/Endpoints.sol +1 -5
  6. package/README.md +34 -30
  7. package/Utils.sol +2 -4
  8. package/codec/Blocks.sol +1606 -0
  9. package/codec/Buffers.sol +165 -0
  10. package/codec/Decoders.sol +558 -0
  11. package/codec/Descriptors.sol +124 -0
  12. package/{blocks → codec}/Keys.sol +4 -16
  13. package/codec/Readers.sol +114 -0
  14. package/{blocks → codec}/Schema.sol +38 -80
  15. package/codec/Specs.sol +218 -0
  16. package/codec/Writers.sol +487 -0
  17. package/commands/Allocate.sol +21 -20
  18. package/commands/Base.sol +71 -37
  19. package/commands/Burn.sol +18 -13
  20. package/commands/Credit.sol +21 -20
  21. package/commands/Debit.sol +25 -28
  22. package/commands/Deposit.sol +34 -35
  23. package/commands/Payout.sol +21 -15
  24. package/commands/Provision.sol +37 -38
  25. package/commands/Recover.sol +20 -19
  26. package/commands/Relay.sol +24 -22
  27. package/commands/Withdraw.sol +15 -18
  28. package/commands/admin/AllowAssets.sol +19 -16
  29. package/commands/admin/Allowance.sol +18 -13
  30. package/commands/admin/Appoint.sol +17 -15
  31. package/commands/admin/Authorize.sol +23 -14
  32. package/commands/admin/Base.sol +1 -1
  33. package/commands/admin/DenyAssets.sol +19 -16
  34. package/commands/admin/Dismiss.sol +17 -15
  35. package/commands/admin/Execute.sol +20 -19
  36. package/commands/admin/Label.sol +17 -13
  37. package/commands/admin/Schemas.sol +18 -14
  38. package/commands/admin/Unauthorize.sol +23 -14
  39. package/core/Calls.sol +3 -11
  40. package/core/Endpoint.sol +42 -127
  41. package/core/Pipeline.sol +16 -15
  42. package/core/Types.sol +1 -1
  43. package/docs/Schema.md +35 -29
  44. package/events/Endpoint.sol +2 -2
  45. package/events/Schema.sol +5 -5
  46. package/execution/Budget.sol +40 -0
  47. package/execution/Execution.sol +1083 -0
  48. package/guards/Base.sol +8 -9
  49. package/guards/Revoke.sol +11 -9
  50. package/package.json +1 -1
  51. package/ports/AllowAssets.sol +12 -15
  52. package/ports/Allowance.sol +11 -9
  53. package/ports/Base.sol +18 -11
  54. package/ports/Credit.sol +11 -9
  55. package/ports/Debit.sol +11 -9
  56. package/ports/DenyAssets.sol +10 -13
  57. package/ports/Dispatch.sol +13 -15
  58. package/ports/Pipe.sol +14 -12
  59. package/ports/Redeem.sol +12 -9
  60. package/ports/Settle.sol +11 -9
  61. package/queries/Assets.sol +15 -15
  62. package/queries/Balances.sol +17 -17
  63. package/queries/Base.sol +11 -12
  64. package/utils/Actions.sol +1 -0
  65. package/utils/Cursors.sol +308 -0
  66. package/utils/Lanes.sol +14 -0
  67. package/utils/Selectors.sol +2 -2
  68. package/utils/Utils.sol +46 -0
  69. package/Cursors.sol +0 -16
  70. package/blocks/Cursors.sol +0 -1529
  71. package/blocks/Writers.sol +0 -1036
  72. package/core/Payable.sol +0 -53
  73. package/utils/Value.sol +0 -43
package/docs/Schema.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # Schema
2
2
 
3
- Rootzero request and response data is encoded as a stream of typed blocks. A
3
+ Rootzero input and response data is encoded as a stream of typed blocks. A
4
4
  schema string describes the payload body for discovery events and tooling; the
5
5
  runtime block key is the compact type tag that identifies that payload layout in
6
6
  the active schema context. The block alias is published separately from the
@@ -30,7 +30,7 @@ are opaque `bytes4` tags and only need to be unique in the context where they ar
30
30
  used. A host can publish the meaning of a custom key with:
31
31
 
32
32
  ```solidity
33
- event Schema(uint indexed host, bytes4 key, string schema, bytes32 name);
33
+ event Schema(uint indexed host, uint spec, string body, bytes32 name);
34
34
  ```
35
35
 
36
36
  For example, a host-specific payment block can use a small literal, the command
@@ -50,7 +50,7 @@ schema: { bytes32 asset, uint amount }
50
50
  A block body can reference another block alias as a child item with `#`:
51
51
 
52
52
  ```txt
53
- { bytes32 account, #bytes as state, #bytes as request }
53
+ { bytes32 account, #bytes as state, #bytes as input }
54
54
  ```
55
55
 
56
56
  The empty schema string `""` means the block has no structured payload. This is
@@ -72,8 +72,8 @@ length, fixed fields may appear before, after, or between child blocks.
72
72
 
73
73
  ```txt
74
74
  { uint target, uint resources, #bytes as payload }
75
- { bytes32 account, #bytes as state, #bytes as request }
76
- { bytes4 key, #string as body, bytes32 name }
75
+ { bytes32 account, #bytes as state, #bytes as input }
76
+ { uint spec, #string as body, bytes32 name }
77
77
  { #bytes as left, uint op, #bytes as right }
78
78
  ```
79
79
 
@@ -108,24 +108,30 @@ generic list block; it does not repeat the item in place.
108
108
  ## Endpoint Lanes
109
109
 
110
110
  Endpoint descriptors identify each lane with a block key and group size. In
111
- Solidity, endpoint definition helpers accept `bytes9` lane values, with plain
112
- `bytes4` keys and the `bytes8` values returned by `many(item)` widening
113
- implicitly. A plain key or `many(item)` stores a zero group byte that readers
114
- interpret as group size 1, while `bytes9(0)` or `Keys.Empty` means the endpoint
115
- has no blocks in that lane. Use
116
- `group(lane, size)` when a lane needs an explicit group size other than 1.
111
+ Solidity, endpoint definition helpers accept block specs such as `Specs.Amount`.
112
+ A zero group is interpreted as group size 1, while `Specs.Empty` means the
113
+ endpoint has no blocks in that lane. Use `group(spec, size)` when a lane needs
114
+ an explicit group size other than 1.
117
115
 
118
- The packed descriptor stores each lane key as an 8-byte value:
116
+ The packed descriptor uses these lane layouts:
119
117
 
120
118
  ```txt
121
- [key bytes4][item bytes4]
119
+ state [key:4][group:1]
120
+ input [key:4][item:4][group:1]
121
+ output [key:4][min:4][max:4][hint:4][group:1]
122
122
  ```
123
123
 
124
- A plain block key is widened into `[key][0]`, so normal endpoint declarations can
125
- pass standard `bytes4` keys directly. A lane with a nonzero `item` describes a
126
- generic container block: `key` is the top-level wire key and `item` is the
127
- contained item key. The built-in `many(item)` helper creates `[Keys.List][item]`
128
- with the default group size 1, matching the DSL form `many #item`.
124
+ Containers are exclusive to input. A plain input spec is compressed into
125
+ `[spec.key][0]`. A spec with a nonzero container is compressed into
126
+ `[spec.container][spec.key]`: the container is the top-level wire key and the
127
+ item is its contained block key. The built-in `many(spec)` helper annotates the
128
+ spec with `Specs.List` as its container, matching the DSL form `many #item`.
129
+ Output lanes retain their size bounds and allocation hint so execution can
130
+ reconstruct the output spec and initialize its writer directly. The Solidity
131
+ output decoder returns this as a left-aligned, writer-ready spec that retains
132
+ its encoded group. Its container and reserved fields are cleared. `Specs.group`
133
+ returns the effective group, interpreting an encoded zero as one for a
134
+ non-empty spec.
129
135
 
130
136
  Any non-empty lane resolves its key to a block alias and schema body through the
131
137
  active schema context. If the item slot is nonzero, tooling also resolves that
@@ -299,33 +305,33 @@ invalid in any path segment.
299
305
  - `#string`: UTF-8 string bytes, written without a body
300
306
  - `#list`: generic list wrapper emitted by `many`
301
307
 
302
- Custom input shapes should define their own context-local block key and publish
303
- that key with a `Schema` event. Endpoint contracts can use `schema(...)` for
304
- that publication:
308
+ Custom input shapes should define their own context-local block spec and publish
309
+ it with a `Schema` event. Endpoint contracts can use `schema(...)` to construct
310
+ and publish that spec:
305
311
 
306
312
  ```solidity
307
- bytes4 input = schema(1, "{ bytes32 asset, uint amount }");
313
+ uint input = schema(1, 64, 64, 64, "{ bytes32 asset, uint amount }", bytes32(0));
308
314
  ```
309
315
 
310
316
  Use different numeric keys when a host needs more than one local block key. The
311
317
  key can also be a selector or any other `bytes4` value that is unique in the
312
- context where it is used. The alias names the block; the schema string describes
313
- only the payload body.
318
+ context where it is used. The numeric arguments after the key are the minimum,
319
+ maximum, and allocation hint payload sizes. The alias names the block; the
320
+ schema string describes only the payload body.
314
321
 
315
322
  ## Standard Blocks
316
323
 
317
- Common protocol schemas live in `contracts/blocks/Schema.sol`:
324
+ Common protocol schemas live in `contracts/codec/Schema.sol`:
318
325
 
319
326
  ```txt
320
327
  amount { bytes32 asset, uint amount }
321
328
  balance { bytes32 asset, uint amount }
322
329
  custody { uint host, bytes32 asset, uint amount }
323
330
  call { uint target, uint resources, #bytes as payload }
324
- step { uint target, uint resources, #bytes as request }
325
- context { bytes32 account, #bytes as state, #bytes as request }
331
+ step { uint cmd, uint resources, #bytes as input }
332
+ context { bytes32 account, #bytes as state, #bytes as input }
326
333
  recover { uint handler, uint resources, bytes32 key, #bytes as witness }
327
- auth { uint cid, uint deadline, #bytes as proof }
328
- schema { bytes4 key, #string as body, bytes32 name }
334
+ schema { uint spec, #string as body, bytes32 name }
329
335
  ```
330
336
 
331
337
  `Keys.sol` contains the corresponding standard runtime keys.
@@ -6,12 +6,12 @@ import {EventEmitter} from "./Emitter.sol";
6
6
  /// @title EndpointEvent
7
7
  /// @notice Emitted during host deployment to publish a callable endpoint descriptor.
8
8
  abstract contract EndpointEvent is EventEmitter {
9
- string private constant ABI = "event Endpoint(uint indexed host, uint id, bytes32 descriptor)";
9
+ string private constant ABI = "event Endpoint(uint indexed host, uint id, uint descriptor)";
10
10
 
11
11
  /// @param host Host node ID that exposes the endpoint.
12
12
  /// @param id Endpoint node ID.
13
13
  /// @param descriptor Packed endpoint lane metadata and flags.
14
- event Endpoint(uint indexed host, uint id, bytes32 descriptor);
14
+ event Endpoint(uint indexed host, uint id, uint descriptor);
15
15
 
16
16
  constructor() {
17
17
  emit EventAbi(ABI);
package/events/Schema.sol CHANGED
@@ -4,18 +4,18 @@ pragma solidity ^0.8.33;
4
4
  import {EventEmitter} from "./Emitter.sol";
5
5
 
6
6
  /// @title SchemaEvent
7
- /// @notice Emitted during host deployment to publish a block key and payload schema.
7
+ /// @notice Emitted during host deployment to publish a block spec and payload schema.
8
8
  /// Block keys are opaque `bytes4` tags. Standard protocol blocks use
9
9
  /// keccak-derived keys by convention, but custom block keys only need to be
10
10
  /// unique within the publishing host/schema context.
11
11
  abstract contract SchemaEvent is EventEmitter {
12
- string private constant ABI = "event Schema(uint indexed host, bytes4 key, string schema, bytes32 name)";
12
+ string private constant ABI = "event Schema(uint indexed host, uint spec, string body, bytes32 name)";
13
13
 
14
14
  /// @param host Host node ID that publishes this block schema.
15
- /// @param key Block type key being defined by `host`.
16
- /// @param schema Schema DSL string describing the block payload body.
15
+ /// @param spec Block specification being defined by `host`.
16
+ /// @param body Schema DSL string describing the block payload body.
17
17
  /// @param name Optional block alias used by endpoint descriptors and nested schemas.
18
- event Schema(uint indexed host, bytes4 key, string schema, bytes32 name);
18
+ event Schema(uint indexed host, uint spec, string body, bytes32 name);
19
19
 
20
20
  constructor() {
21
21
  emit EventAbi(ABI);
@@ -0,0 +1,40 @@
1
+ // SPDX-License-Identifier: GPL-3.0-only
2
+ pragma solidity ^0.8.33;
3
+
4
+ /// @notice Mutable native-value budget shared across internal calls.
5
+ struct Budget {
6
+ /// @dev Remaining unspent native value in wei.
7
+ uint remaining;
8
+ }
9
+
10
+ /// @title Budgets
11
+ /// @notice Opening and mutation helpers for standalone native-value budgets.
12
+ library Budgets {
13
+ /// @dev Thrown when an operation attempts to spend more value than remains.
14
+ error InsufficientValue();
15
+
16
+ /// @notice Open a standalone budget containing the current call value.
17
+ /// @return budget Budget initialized with `msg.value`.
18
+ function open() internal view returns (Budget memory budget) {
19
+ budget.remaining = msg.value;
20
+ }
21
+
22
+ /// @notice Deduct the EVM value lane of `resources` from `budget`.
23
+ /// @dev EVM resources use the low 128 bits as native value/endowment.
24
+ /// @param budget Mutable budget to debit.
25
+ /// @param resources Packed resources whose low 128 bits contain native value.
26
+ /// @return value Native value to forward in wei.
27
+ function use(Budget memory budget, uint resources) internal pure returns (uint128 value) {
28
+ value = uint128(resources);
29
+ if (value > budget.remaining) revert InsufficientValue();
30
+ budget.remaining -= value;
31
+ }
32
+
33
+ /// @notice Remove and return all remaining value from `budget`.
34
+ /// @param budget Mutable budget to drain.
35
+ /// @return value Native value removed from the budget.
36
+ function drain(Budget memory budget) internal pure returns (uint value) {
37
+ value = budget.remaining;
38
+ budget.remaining = 0;
39
+ }
40
+ }