@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.
Files changed (86) hide show
  1. package/CHANGELOG.md +87 -3
  2. package/Codec.sol +21 -0
  3. package/Commands.sol +14 -0
  4. package/Core.sol +8 -6
  5. package/Endpoints.sol +2 -7
  6. package/Events.sol +1 -2
  7. package/README.md +70 -37
  8. package/Utils.sol +2 -4
  9. package/annotations/Action.sol +17 -0
  10. package/annotations/Label.sol +23 -0
  11. package/annotations/Schema.sol +53 -0
  12. package/codec/Blocks.sol +1643 -0
  13. package/codec/Buffers.sol +165 -0
  14. package/codec/Decoders.sol +570 -0
  15. package/codec/Descriptors.sol +124 -0
  16. package/{blocks → codec}/Keys.sol +10 -18
  17. package/codec/Readers.sol +114 -0
  18. package/{blocks → codec}/Schema.sol +43 -80
  19. package/codec/Specs.sol +240 -0
  20. package/codec/Writers.sol +485 -0
  21. package/commands/Allocate.sol +21 -20
  22. package/commands/Base.sol +83 -45
  23. package/commands/Burn.sol +23 -14
  24. package/commands/Credit.sol +21 -20
  25. package/commands/Debit.sol +25 -28
  26. package/commands/Deposit.sol +41 -36
  27. package/commands/Payout.sol +26 -16
  28. package/commands/Provision.sol +37 -38
  29. package/commands/Recover.sol +20 -19
  30. package/commands/Relay.sol +24 -22
  31. package/commands/Withdraw.sol +20 -19
  32. package/commands/admin/AllowAssets.sol +19 -16
  33. package/commands/admin/Allowance.sol +18 -13
  34. package/commands/admin/Annotate.sol +36 -0
  35. package/commands/admin/Appoint.sol +19 -16
  36. package/commands/admin/Authorize.sol +23 -14
  37. package/commands/admin/Base.sol +9 -2
  38. package/commands/admin/DenyAssets.sol +19 -16
  39. package/commands/admin/Dismiss.sol +19 -16
  40. package/commands/admin/Execute.sol +21 -19
  41. package/commands/admin/Unauthorize.sol +23 -14
  42. package/core/Access.sol +91 -49
  43. package/core/Calls.sol +30 -33
  44. package/core/Endpoint.sol +46 -143
  45. package/core/Host.sol +63 -21
  46. package/core/Pipeline.sol +16 -15
  47. package/core/Types.sol +1 -1
  48. package/docs/Schema.md +48 -31
  49. package/events/Annotation.sol +24 -0
  50. package/events/Endpoint.sol +2 -2
  51. package/events/Guardian.sol +2 -2
  52. package/events/Introduction.sol +3 -2
  53. package/execution/Budget.sol +40 -0
  54. package/execution/Execution.sol +1104 -0
  55. package/guards/Base.sol +11 -12
  56. package/guards/Revoke.sol +11 -9
  57. package/package.json +1 -1
  58. package/ports/AllowAssets.sol +12 -15
  59. package/ports/Allowance.sol +11 -9
  60. package/ports/Base.sol +35 -16
  61. package/ports/Credit.sol +11 -9
  62. package/ports/Debit.sol +11 -9
  63. package/ports/DenyAssets.sol +10 -13
  64. package/ports/Dispatch.sol +13 -15
  65. package/ports/Pipe.sol +14 -12
  66. package/ports/Redeem.sol +12 -9
  67. package/ports/Settle.sol +16 -10
  68. package/queries/Assets.sol +15 -15
  69. package/queries/Balances.sol +17 -17
  70. package/queries/Base.sol +26 -12
  71. package/utils/Accounts.sol +0 -23
  72. package/utils/Actions.sol +1 -0
  73. package/utils/Cursors.sol +367 -0
  74. package/utils/Lanes.sol +14 -0
  75. package/utils/Layout.sol +0 -2
  76. package/utils/Selectors.sol +2 -2
  77. package/utils/Utils.sol +46 -0
  78. package/Cursors.sol +0 -16
  79. package/blocks/Cursors.sol +0 -1529
  80. package/blocks/Writers.sol +0 -1036
  81. package/commands/admin/Label.sol +0 -32
  82. package/commands/admin/Schemas.sol +0 -32
  83. package/core/Payable.sol +0 -53
  84. package/events/Labeled.sol +0 -21
  85. package/events/Schema.sol +0 -23
  86. package/utils/Value.sol +0 -43
@@ -0,0 +1,165 @@
1
+ // SPDX-License-Identifier: GPL-3.0-only
2
+ pragma solidity ^0.8.33;
3
+
4
+ import {Cursors} from "../utils/Cursors.sol";
5
+
6
+ /// @title Buffers
7
+ /// @notice Allocation and finalization helpers for mutable memory byte buffers.
8
+ /// @dev Packed buffer positions use `Cursors`; flag bit 0 denotes growth policy.
9
+ library Buffers {
10
+ using Cursors for uint;
11
+
12
+ uint8 internal constant Growable = 1;
13
+
14
+ /// @dev A reserved memory write exceeds the physical backing buffer.
15
+ error BufferOverflow();
16
+ /// @dev A buffer cursor does not match its logical or physical capacity.
17
+ error IncompleteBuffer();
18
+
19
+ /// @notice Create a packed buffer cursor at write position zero.
20
+ /// @param len Initial logical byte capacity.
21
+ /// @param groups Number of logical groups represented by the buffer.
22
+ /// @param growable Whether writes may expand the logical capacity.
23
+ /// @param tag Cursor identity tag.
24
+ /// @return cur Packed buffer cursor.
25
+ function cursor(uint len, uint groups, bool growable, uint8 tag) internal pure returns (uint cur) {
26
+ cur = Cursors.create(0, len, groups, growable ? Growable : 0, tag);
27
+ }
28
+
29
+ /// @notice Reserve relative write space and return the updated packed buffer cursor.
30
+ /// @dev `touch` may exceed `advance` by at most 31 bytes; `alloc` reserves one
31
+ /// trailing word so lazy allocation can safely return without another bounds check.
32
+ /// @param cur Current packed buffer cursor.
33
+ /// @param buffer Current backing buffer.
34
+ /// @param advance Logical number of bytes appended.
35
+ /// @param touch Physical number of bytes the write may touch.
36
+ /// @return updated Updated packed buffer cursor.
37
+ /// @return dst Original or resized backing buffer.
38
+ /// @return i Original write offset.
39
+ function reserve(
40
+ uint cur,
41
+ bytes memory buffer,
42
+ uint advance,
43
+ uint touch
44
+ ) internal pure returns (uint updated, bytes memory dst, uint i) {
45
+ uint len;
46
+ (i, , len) = cur.decode();
47
+ uint required = i + (advance > touch ? advance : touch);
48
+ bool empty = buffer.length == 0;
49
+ dst = buffer;
50
+
51
+ if (cur.flagged(Growable) && required > len) {
52
+ len = len == 0 ? 64 : len * 2;
53
+ while (len < required) {
54
+ len *= 2;
55
+ }
56
+ cur = cur.resize(len);
57
+ if (!empty) dst = resize(dst, i, len);
58
+ }
59
+
60
+ updated = cur.advance(advance);
61
+
62
+ if (empty) dst = alloc(len);
63
+ if (required > dst.length) revert BufferOverflow();
64
+ }
65
+
66
+ /// @notice Allocate a buffer with `capacity` logical bytes and trailing write space.
67
+ /// @dev The returned byte-array length is its padded physical capacity. Callers must
68
+ /// track the logical capacity separately and trim the buffer before returning it.
69
+ /// @param capacity Requested logical byte capacity.
70
+ /// @return buffer Newly allocated padded buffer.
71
+ function alloc(uint capacity) internal pure returns (bytes memory buffer) {
72
+ uint padded = ((capacity + 31) & ~uint(31)) + 32;
73
+ buffer = new bytes(padded);
74
+ }
75
+
76
+ /// @notice Allocate a buffer with `capacity` and copy its written prefix.
77
+ /// @dev Performs no bounds checks; the caller must ensure `written <= buffer.length`.
78
+ /// @param buffer Current buffer.
79
+ /// @param written Number of leading bytes to copy.
80
+ /// @param capacity Requested capacity of the new buffer.
81
+ /// @return resized Newly allocated buffer containing the written prefix.
82
+ function resize(bytes memory buffer, uint written, uint capacity) internal pure returns (bytes memory resized) {
83
+ resized = alloc(capacity);
84
+ assembly ("memory-safe") {
85
+ mcopy(add(resized, 0x20), add(buffer, 0x20), written)
86
+ }
87
+ }
88
+
89
+ /// @notice Write raw bytes at byte offset `i`.
90
+ /// @param buffer Destination buffer.
91
+ /// @param i Destination byte offset.
92
+ /// @param value Bytes to copy.
93
+ /// @return next Position immediately after the copied bytes.
94
+ function write(bytes memory buffer, uint i, bytes memory value) internal pure returns (uint next) {
95
+ next = i + value.length;
96
+ if (next > buffer.length) revert BufferOverflow();
97
+ assembly ("memory-safe") {
98
+ mcopy(add(add(buffer, 0x20), i), add(value, 0x20), mload(value))
99
+ }
100
+ }
101
+
102
+ /// @notice Write one complete raw word at byte offset `i`.
103
+ /// @param buffer Destination buffer.
104
+ /// @param i Destination byte offset.
105
+ /// @param value Word to write.
106
+ function write32(bytes memory buffer, uint i, bytes32 value) internal pure {
107
+ if (i + 32 > buffer.length) revert BufferOverflow();
108
+ assembly ("memory-safe") {
109
+ mstore(add(add(buffer, 0x20), i), value)
110
+ }
111
+ }
112
+
113
+ /// @notice Write two complete raw words at byte offset `i`.
114
+ /// @param buffer Destination buffer.
115
+ /// @param i Destination byte offset.
116
+ /// @param a First word to write.
117
+ /// @param b Second word to write.
118
+ function write64(bytes memory buffer, uint i, bytes32 a, bytes32 b) internal pure {
119
+ if (i + 64 > buffer.length) revert BufferOverflow();
120
+ assembly ("memory-safe") {
121
+ let p := add(add(buffer, 0x20), i)
122
+ mstore(p, a)
123
+ mstore(add(p, 0x20), b)
124
+ }
125
+ }
126
+
127
+ /// @notice Write three complete raw words at byte offset `i`.
128
+ /// @param buffer Destination buffer.
129
+ /// @param i Destination byte offset.
130
+ /// @param a First word to write.
131
+ /// @param b Second word to write.
132
+ /// @param c Third word to write.
133
+ function write96(bytes memory buffer, uint i, bytes32 a, bytes32 b, bytes32 c) internal pure {
134
+ if (i + 96 > buffer.length) revert BufferOverflow();
135
+ assembly ("memory-safe") {
136
+ let p := add(add(buffer, 0x20), i)
137
+ mstore(p, a)
138
+ mstore(add(p, 0x20), b)
139
+ mstore(add(p, 0x40), c)
140
+ }
141
+ }
142
+
143
+ /// @notice Set a buffer's visible length to the number of bytes actually written.
144
+ /// @param buffer Buffer to trim in place.
145
+ /// @param len Logical byte length to expose.
146
+ /// @return The same buffer with its length set to `len`.
147
+ function trim(bytes memory buffer, uint len) internal pure returns (bytes memory) {
148
+ if (len > buffer.length) revert BufferOverflow();
149
+ assembly ("memory-safe") {
150
+ mstore(buffer, len)
151
+ }
152
+ return buffer;
153
+ }
154
+
155
+ /// @notice Return an empty buffer when unused, otherwise trim it to the cursor position.
156
+ /// @param cur Packed buffer cursor.
157
+ /// @param buffer Backing byte buffer.
158
+ /// @return out Empty bytes when unused, otherwise the written prefix.
159
+ function finish(uint cur, bytes memory buffer) internal pure returns (bytes memory out) {
160
+ (uint i, , uint len) = cur.decode();
161
+ if (i == 0) return new bytes(0);
162
+ if (i > len || i > buffer.length) revert IncompleteBuffer();
163
+ out = trim(buffer, i);
164
+ }
165
+ }