@riseandshaheen/libcma 0.1.0-alpha.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 (33) hide show
  1. package/ARCHITECTURE.md +348 -0
  2. package/LICENSE +21 -0
  3. package/README.md +157 -0
  4. package/binding.gyp +52 -0
  5. package/deps/machine-asset-tools/LICENSE +202 -0
  6. package/deps/machine-asset-tools/README.md +237 -0
  7. package/deps/machine-asset-tools/include/libcma/ledger.h +161 -0
  8. package/deps/machine-asset-tools/include/libcma/parser.h +305 -0
  9. package/deps/machine-asset-tools/include/libcma/types.h +22 -0
  10. package/deps/machine-guest-tools/LICENSE +202 -0
  11. package/deps/machine-guest-tools/README.md +57 -0
  12. package/deps/machine-guest-tools/sys-utils/libcmt/README.md +130 -0
  13. package/deps/machine-guest-tools/sys-utils/libcmt/include/libcmt/abi.h +578 -0
  14. package/deps/machine-guest-tools/sys-utils/libcmt/include/libcmt/buf.h +82 -0
  15. package/deps/machine-guest-tools/sys-utils/libcmt/include/libcmt/io.h +168 -0
  16. package/deps/machine-guest-tools/sys-utils/libcmt/include/libcmt/keccak.h +131 -0
  17. package/deps/machine-guest-tools/sys-utils/libcmt/include/libcmt/merkle.h +118 -0
  18. package/deps/machine-guest-tools/sys-utils/libcmt/include/libcmt/rollup.h +260 -0
  19. package/deps/machine-guest-tools/sys-utils/libcmt/include/libcmt/util.h +34 -0
  20. package/lib/index.cjs +445 -0
  21. package/lib/index.cjs.map +1 -0
  22. package/lib/index.d.cts +156 -0
  23. package/lib/index.d.ts +156 -0
  24. package/lib/index.js +428 -0
  25. package/lib/index.js.map +1 -0
  26. package/native/addon.cc +268 -0
  27. package/native/ledger_backend.h +28 -0
  28. package/native/mock/ledger_mock.cc +185 -0
  29. package/native/real/ledger_real.cc +271 -0
  30. package/package.json +74 -0
  31. package/scripts/build-libcma-riscv64.sh +80 -0
  32. package/scripts/build-native-riscv64.sh +40 -0
  33. package/scripts/install-native.mjs +94 -0
@@ -0,0 +1,260 @@
1
+ /* Copyright Cartesi and individual authors (see AUTHORS)
2
+ * SPDX-License-Identifier: Apache-2.0
3
+ *
4
+ * Licensed under the Apache License, Version 2.0 (the "License");
5
+ * you may not use this file except in compliance with the License.
6
+ * You may obtain a copy of the License at
7
+ *
8
+ * http://www.apache.org/licenses/LICENSE-2.0
9
+ *
10
+ * Unless required by applicable law or agreed to in writing, software
11
+ * distributed under the License is distributed on an "AS IS" BASIS,
12
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ * See the License for the specific language governing permissions and
14
+ * limitations under the License.
15
+ */
16
+ /** @file
17
+ * @defgroup libcmt_rollup rollup
18
+ * Rollup abstraction layer
19
+ *
20
+ * Takes care of @ref libcmt_io_driver interactions, @ref libcmt_abi
21
+ * encoding/decoding and @ref libcmt_merkle tree handling.
22
+ *
23
+ * Mocked version has support for simulating I/O via environment variables:
24
+ * @p CMT_INPUTS="0:input.bin,..." and verbose output with @p CMT_DEBUG=yes.
25
+ *
26
+ * Lets look at some code:
27
+ *
28
+ * @include doc/examples/rollup.c
29
+ *
30
+ * @ingroup libcmt
31
+ * @{ */
32
+ #ifndef CMT_ROLLUP_H
33
+ #define CMT_ROLLUP_H
34
+ #include "abi.h"
35
+ #include "io.h"
36
+ #include "merkle.h"
37
+
38
+ typedef struct cmt_rollup {
39
+ union cmt_io_driver io[1];
40
+ uint32_t fromhost_data;
41
+ cmt_merkle_t merkle[1];
42
+
43
+ // cache merkle values and repeat them on finish when the tree doesn't change
44
+ uint8_t finish_root_hash[CMT_KECCAK_LENGTH];
45
+ uint64_t finish_leaf_count;
46
+ } cmt_rollup_t;
47
+
48
+ /** Public struct with the advance state contents */
49
+ typedef struct cmt_rollup_advance {
50
+ uint64_t chain_id; /**< network */
51
+ cmt_abi_address_t app_contract; /**< application contract address */
52
+ cmt_abi_address_t msg_sender; /**< input sender address */
53
+ uint64_t block_number; /**< block number of this input */
54
+ uint64_t block_timestamp; /**< block timestamp of this input UNIX epoch format) */
55
+ cmt_abi_u256_t prev_randao; /**< The latest RANDAO mix of the post beacon state of the previous block */
56
+ uint64_t index; /**< input index (in relation to all inputs ever sent to the DApp) */
57
+ cmt_abi_bytes_t payload; /**< payload for this input */
58
+ } cmt_rollup_advance_t;
59
+
60
+ /** Public struct with the inspect state contents */
61
+ typedef struct cmt_rollup_inspect {
62
+ cmt_abi_bytes_t payload; /**< payload for this input */
63
+ } cmt_rollup_inspect_t;
64
+
65
+ /** Public struct with the finish state contents */
66
+ typedef struct cmt_rollup_finish_s {
67
+ bool accept_previous_request;
68
+ int next_request_type;
69
+ uint32_t next_request_payload_length;
70
+ } cmt_rollup_finish_t;
71
+
72
+ /** Public struct with generic io request/response */
73
+ typedef struct cmt_gio {
74
+ uint16_t domain; /**< domain for the gio request */
75
+ uint32_t id_length; /**< length of id */
76
+ void *id; /**< id for the request */
77
+ uint16_t response_code; /**< response */
78
+ uint32_t response_data_length; /**< length of response data */
79
+ void *response_data; /**< response data */
80
+ } cmt_gio_t;
81
+
82
+ /** Initialize a @ref cmt_rollup_t state.
83
+ *
84
+ * @param [in] me uninitialized state
85
+ *
86
+ * @return
87
+ * | | |
88
+ * |--:|-----------------------------|
89
+ * | 0| success |
90
+ * |< 0| failure with a -errno value | */
91
+ int cmt_rollup_init(cmt_rollup_t *me);
92
+
93
+ /** Finalize a @ref cmt_rollup_t state previously initialized with @ref
94
+ * cmt_rollup_init
95
+ *
96
+ * @param [in] me initialized state
97
+ *
98
+ * @note use of @p me after this call is undefined behavior. */
99
+ void cmt_rollup_fini(cmt_rollup_t *me);
100
+
101
+ /** Emit a voucher
102
+ *
103
+ * Equivalent to the `Voucher(address,uint256,bytes)` solidity call.
104
+ *
105
+ * @param [in,out] me initialized @ref cmt_rollup_t instance
106
+ * @param [in] address destination data
107
+ * @param [in] value value data
108
+ * @param [in] data message contents
109
+ * @param [out] index index of emitted voucher, if successful
110
+ *
111
+ * @return
112
+ * | | |
113
+ * |--:|-----------------------------|
114
+ * | 0| success |
115
+ * |< 0| failure with a -errno value | */
116
+ int cmt_rollup_emit_voucher(cmt_rollup_t *me, const cmt_abi_address_t *address, const cmt_abi_u256_t *value, const cmt_abi_bytes_t *data, uint64_t *index);
117
+
118
+ /** Emit a delegate call voucher
119
+ *
120
+ * Equivalent to the `DelegateCallVoucher(address,bytes)` solidity call.
121
+ *
122
+ * @param [in,out] me initialized @ref cmt_rollup_t instance
123
+ * @param [in] address destination data
124
+ * @param [in] data message contents
125
+ * @param [out] index index of emitted voucher, if successful
126
+ *
127
+ * @return
128
+ * | | |
129
+ * |--:|-----------------------------|
130
+ * | 0| success |
131
+ * |< 0| failure with a -errno value | */
132
+ int cmt_rollup_emit_delegate_call_voucher(cmt_rollup_t *me, const cmt_abi_address_t *address, const cmt_abi_bytes_t *data, uint64_t *index);
133
+
134
+ /** Emit a notice
135
+ *
136
+ * @param [in,out] me initialized cmt_rollup_t instance
137
+ * @param [in] data message contents
138
+ * @param [out] index index of emitted notice, if successful
139
+ *
140
+ * @return
141
+ * | | |
142
+ * |--:|-----------------------------|
143
+ * | 0| success |
144
+ * |< 0| failure with a -errno value | */
145
+ int cmt_rollup_emit_notice(cmt_rollup_t *me, const cmt_abi_bytes_t *payload, uint64_t *index);
146
+
147
+ /** Emit a report
148
+ * @param [in,out] me initialized cmt_rollup_t instance
149
+ * @param [in] n sizeof @p data in bytes
150
+ * @param [in] data message contents
151
+ *
152
+ * @return
153
+ * | | |
154
+ * |--:|-----------------------------|
155
+ * | 0| success |
156
+ * |< 0| failure with a -errno value | */
157
+ int cmt_rollup_emit_report(cmt_rollup_t *me, const cmt_abi_bytes_t *payload);
158
+
159
+ /** Emit a exception
160
+ * @param [in,out] me initialized cmt_rollup_t instance
161
+ * @param [in] data_length data length in bytes
162
+ * @param [in] data message contents
163
+ *
164
+ * @return
165
+ * | | |
166
+ * |--:|-----------------------------|
167
+ * | 0| success |
168
+ * |< 0| failure with a -errno value | */
169
+ int cmt_rollup_emit_exception(cmt_rollup_t *me, const cmt_abi_bytes_t *payload);
170
+
171
+ /** Report progress
172
+ *
173
+ * @param [in,out] me initialized cmt_rollup_t instance
174
+ * @param [in] progress progress value to be set
175
+ *
176
+ * @return
177
+ * | | |
178
+ * |--:|-----------------------------|
179
+ * | 0| success |
180
+ * |< 0| failure with a -errno value | */
181
+ int cmt_rollup_progress(cmt_rollup_t *me, uint32_t progress);
182
+
183
+ /** Read advance state
184
+ *
185
+ * @param [in,out] me initialized cmt_rollup_t instance
186
+ * @param [out] advance cmt_rollup_advance_t instance (may be uninitialized)
187
+ *
188
+ * @return
189
+ * | | |
190
+ * |--:|-----------------------------|
191
+ * | 0| success |
192
+ * |< 0| failure with a -errno value | */
193
+ int cmt_rollup_read_advance_state(cmt_rollup_t *me, cmt_rollup_advance_t *advance);
194
+
195
+ /** Read inspect state
196
+ *
197
+ * @param [in,out] me initialized cmt_rollup_t instance
198
+ * @param [out] inspect cmt_rollup_inspect_t instance (may be uninitialized)
199
+ *
200
+ * @return
201
+ * | | |
202
+ * |--:|-----------------------------|
203
+ * | 0| success |
204
+ * |< 0| failure with a -errno value | */
205
+ int cmt_rollup_read_inspect_state(cmt_rollup_t *me, cmt_rollup_inspect_t *inspect);
206
+
207
+ /** Finish processing of current advance or inspect.
208
+ * Waits for and returns the next advance or inspect query when available.
209
+ *
210
+ * @param [in,out] me initialized cmt_rollup_t instance
211
+ * @param [in,out] finish initialized cmt_rollup_finish_t instance
212
+ *
213
+ * @return
214
+ * | | |
215
+ * |--:|-----------------------------|
216
+ * | 0| success |
217
+ * |< 0| failure with a -errno value | */
218
+ int cmt_rollup_finish(cmt_rollup_t *me, cmt_rollup_finish_t *finish);
219
+
220
+ /** Performs a generic IO request
221
+ *
222
+ * @param [in,out] me initialized cmt_rollup_t instance
223
+ * @param [in,out] req initialized cmt_gio_t structure
224
+ *
225
+ * @return
226
+ * | | |
227
+ * |--:|-----------------------------|
228
+ * | 0| success |
229
+ * |< 0| failure with a -errno value | */
230
+ int cmt_gio_request(cmt_rollup_t *me, cmt_gio_t *req);
231
+
232
+ /** Retrieve the merkle tree and intermediate state from a file @p path
233
+ * @param [in,out] me initialized cmt_rollup_t instance
234
+ * @param [in] file path to file (parent directories must exist)
235
+ *
236
+ * @return
237
+ * | | |
238
+ * |--:|-----------------------------|
239
+ * | 0| success |
240
+ * |< 0| failure with a -errno value | */
241
+ int cmt_rollup_load_merkle(cmt_rollup_t *me, const char *path);
242
+
243
+ /** Store the merkle tree and intermediate state to a file @p path
244
+ *
245
+ * @param [in,out] me initialized cmt_rollup_t instance
246
+ * @param [in] file path to file (parent directories must exist)
247
+ *
248
+ * @return
249
+ * | | |
250
+ * |--:|-----------------------------|
251
+ * | 0| success |
252
+ * |< 0| failure with a -errno value | */
253
+ int cmt_rollup_save_merkle(cmt_rollup_t *me, const char *path);
254
+
255
+ /** Resets the merkle tree to pristine conditions
256
+ *
257
+ * @param [in,out] me initialized cmt_rollup_t instance */
258
+ void cmt_rollup_reset_merkle(cmt_rollup_t *me);
259
+
260
+ #endif /* CMT_ROLLUP_H */
@@ -0,0 +1,34 @@
1
+ #ifndef CMT_UTIL_H
2
+ #define CMT_UTIL_H
3
+ #include <stdbool.h>
4
+
5
+ /**
6
+ */
7
+ bool cmt_util_debug_enabled(void);
8
+
9
+ /** Read whole file `name` contents into `data` and set `length`.
10
+ * @param name[in] - file path
11
+ * @param max[in] - size of `data` in bytes
12
+ * @param data[out] - file contents
13
+ * @param length[out] - actual size in `bytes` written to `data`
14
+ *
15
+ * @return
16
+ * | | |
17
+ * |-----|--------------------|
18
+ * | 0 |success |
19
+ * | < 0 |negative errno value| */
20
+ int cmt_util_read_whole_file(const char *name, size_t max, void *data, size_t *length);
21
+
22
+ /** Write the contents of `data` into file `name`.
23
+ * @param name[in] - file path
24
+ * @param length[in] - size of `data` in bytes
25
+ * @param data[out] - file contents
26
+ *
27
+ * @return
28
+ * | | |
29
+ * |-----|--------------------|
30
+ * | 0 |success |
31
+ * | < 0 |negative errno value| */
32
+ int cmt_util_write_whole_file(const char *name, size_t length, const void *data);
33
+
34
+ #endif /* CMT_UTIL_H */