From 3c9d53a27bcf6265136b5ee993f8adeaab0cf7d0 Mon Sep 17 00:00:00 2001 From: Marcelo Politzer <251334+mpolitzer@users.noreply.github.com> Date: Mon, 29 Jan 2024 11:03:06 -0300 Subject: [PATCH] feat!: add value to voucher --- sys-utils/libcmt/README.md | 132 ++++ sys-utils/libcmt/doc/examples/rollup.c | 2 +- sys-utils/libcmt/include/libcmt/abi.h | 45 ++ sys-utils/libcmt/include/libcmt/rollup.h | 10 +- sys-utils/libcmt/src/abi.c | 23 + sys-utils/libcmt/src/rollup.c | 19 +- sys-utils/libcmt/src/tests/abi-single.c | 844 +++-------------------- sys-utils/libcmt/src/tests/rollup_echo.c | 23 +- 8 files changed, 341 insertions(+), 757 deletions(-) diff --git a/sys-utils/libcmt/README.md b/sys-utils/libcmt/README.md index 2f80b968..0e9b891f 100644 --- a/sys-utils/libcmt/README.md +++ b/sys-utils/libcmt/README.md @@ -14,3 +14,135 @@ And finally, a couple of utility modules used by the high level API are also exp The header files and a compiled RISC-V version of this library can be found [here](https://github.com/cartesi/machine-emulator-tools/). We also provide `.pc` (pkg-config) files to facilitate linking. + +# mock and testing + +Both @ref libcmt\_rollup and libcmt\_io\_driver have a mock implementation. Run +`make mock.build` to build the mock version of the library. The install +destination can be specified with the `PREFIX` variable. An example: +``` +make mock.install PREFIX=_install +``` +will install the library and headers on `$PWD/_install` directory. + +## testing + +Use the environment variable @p CMT\_INPUTS to inject inputs to the mock. +Outputs will be written on files. + +example: +``` +CMT_INPUTS="0:advance.bin" ./application +``` + +with syntax: +``` +CMT_INPUTS=" ':' ( ',' ':' ) *" +``` + +In addition to @p CMT\_INPUTS, the is also the @p CMT\_DEBUG variable. Enable it +for a verbose version of the low level calls. + +``` +CMT_DEBUG=yes ./application +``` + +## generating inputs + +Inputs and Outputs are expected to be EVM-ABI encoded. Encoding and decoding +can be acheived multiple ways, including writing tools with this library. The +Recommended way for testing is to use the @p cast tool from +[foundry](http://book.getfoundry.sh/reference/cast/cast.html) and `xxd`. + +Encoding an @p EvmAdvance: +``` +cast calldata "EvmAdvance(address,uint256,uint256,uint256,bytes)" \ + 0x0000000000000000000000000000000000000000 \ + 0x0000000000000000000000000000000000000000 \ + 0x0000000000000000000000000000000000000000 \ + 0x0000000000000000000000000000000000000000 \ + 0x`echo "advance-0" | xxd -p -c0` | xxd -r -p > 0.bin +``` + +Encoding an @p EvmInspect: +``` +cast calldata "EvmInspect(bytes)" \ + 0x`echo "inspect-0" | xxd -p -c0` | xxd -r -p > 1.bin +``` + +## parsing outputs + +Decoding a voucher with the same tool: +``` +cast calldata-decode "Voucher(address,uint256,bytes)" 0x`xxd -p -c0 "$1"` | ( + read address + read value + read bytes + + echo "{" + printf '\t"address" : "%s",\n' $address + printf '\t"value" : "%s",\n' $value + printf '\t"bytes" : "%s"\n' $bytes + echo "}" +) + +# sh decode-voucher.sh $1 | jq '.bytes' | xxd -r +``` + +# binds + +Assuming the mock was installed at `_install`: + +Go example, compile with `go build main.go`: +``` +package main + +/* +#cgo CFLAGS: -I../_install/include/ +#cgo LDFLAGS: -L../_install/lib/ -lcmt + +#include "libcmt/rollup.h" +*/ +import "C" + +import ( + "math/big" + "fmt" + "unsafe" +) + +func main() { + var rollup C.cmt_rollup_t + err := C.cmt_rollup_init(&rollup) + if err != 0 { + fmt.Printf("initialization failed\n") + } + + bytes_s := []byte{ + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, + } + wei := new(big.Int).SetBytes(bytes_s) + + finish := C.cmt_rollup_finish_t{ + accept_previous_request: true, + } + for { + var advance C.cmt_rollup_advance_t + err = C.cmt_rollup_finish(&rollup, &finish) + if err != 0 { return; } + + err = C.cmt_rollup_read_advance_state(&rollup, &advance); + if err != 0 { return; } + + bytes:= wei.Bytes() + size := len(bytes) + C.cmt_rollup_emit_voucher(&rollup, &advance.sender[0], + C.uint(size), unsafe.Pointer(&bytes[0]), + advance.length, advance.data) + C.cmt_rollup_emit_report(&rollup, advance.length, advance.data) + } +} +``` diff --git a/sys-utils/libcmt/doc/examples/rollup.c b/sys-utils/libcmt/doc/examples/rollup.c index 68e7702d..d641546e 100644 --- a/sys-utils/libcmt/doc/examples/rollup.c +++ b/sys-utils/libcmt/doc/examples/rollup.c @@ -24,7 +24,7 @@ int main(void) { break; } - rc = cmt_rollup_emit_voucher(&rollup, advance.sender, advance.length, advance.data); + rc = cmt_rollup_emit_voucher(&rollup, advance.sender, 0, NULL, advance.length, advance.data); if (rc < 0) { fprintf(stderr, "%s:%d Error on voucher %s (%d)\n", __FILE__, __LINE__, strerror(-rc), (-rc)); break; diff --git a/sys-utils/libcmt/include/libcmt/abi.h b/sys-utils/libcmt/include/libcmt/abi.h index 8ab862a1..a704a7fb 100644 --- a/sys-utils/libcmt/include/libcmt/abi.h +++ b/sys-utils/libcmt/include/libcmt/abi.h @@ -169,6 +169,39 @@ int cmt_abi_put_funsel(cmt_buf_t *me, uint32_t funsel); * @note This function takes care of endianess conversions */ int cmt_abi_put_uint(cmt_buf_t *me, size_t n, const void *data); +/** Encode a big-endian value of up to 32bytes of data into the buffer + * + * @param [in,out] me a initialized buffer working as iterator + * @param [in] length size of @p data in bytes + * @param [in] data poiter to a integer + * + * @return + * - 0 success + * - ENOBUFS no space left in @p me + * - EDOM requested @p n is too large + * + * @code + * ... + * cmt_buf_t it = ...; + * uint8_t small[] = { + * 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + * 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + * 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + * 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, + * }; + * cmt_abi_put_uint(&it, sizeof small, &small); + * ... + * uint8_t big[] = { + * 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + * 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + * 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + * 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + * }; + * cmt_abi_put_uint(&it, sizeof big, &big); + * @endcode + * @note This function takes care of endianess conversions */ +int cmt_abi_put_uint_be(cmt_buf_t *me, size_t n, const void *data); + /** Encode a bool into the buffer * * @param [in,out] me a initialized buffer working as iterator @@ -282,6 +315,18 @@ int cmt_abi_check_funsel(cmt_buf_t *me, uint32_t expected); * - EDOM value won't fit into @p n bytes. */ int cmt_abi_get_uint(cmt_buf_t *me, size_t n, void *data); +/** Decode @p length big-endian bytes, up to 32, from the buffer into @p data + * + * @param [in,out] me initialized buffer + * @param [in] length size of @p data in bytes + * @param [out] data pointer to a integer + * + * @return + * - 0 success + * - ENOBUFS no space left in @p me + * - EDOM value won't fit into @p n bytes. */ +int cmt_abi_get_uint_be(cmt_buf_t *me, size_t n, void *data); + /** Consume and decode @b address from the buffer * * @param [in,out] me initialized buffer diff --git a/sys-utils/libcmt/include/libcmt/rollup.h b/sys-utils/libcmt/include/libcmt/rollup.h index 36852721..9bfaae73 100644 --- a/sys-utils/libcmt/include/libcmt/rollup.h +++ b/sys-utils/libcmt/include/libcmt/rollup.h @@ -18,7 +18,7 @@ * Rollup abstraction layer * * Takes care of @ref libcmt_io_driver interactions, @ref libcmt_abi - * encoding/decoding and @ref libcmt_merkle tree interactions. + * encoding/decoding and @ref libcmt_merkle tree handling. * * Mocked version has support for simulating I/O via environment variables: * @p CMT_INPUTS="0:input.bin,..." and verbose ouput with @p CMT_DEBUG=yes. @@ -89,7 +89,7 @@ void cmt_rollup_fini(cmt_rollup_t *me); * @return * - 0 success * - -ENOBUFS no space left in @p me */ -int cmt_rollup_emit_voucher(cmt_rollup_t *me, uint8_t address[CMT_ADDRESS_LENGTH], size_t n, const void *data); +int cmt_rollup_emit_voucher(cmt_rollup_t *me, uint8_t address[CMT_ADDRESS_LENGTH], uint32_t value_length, const void *value_data, uint32_t length, const void *data); /** Emit a notice * @@ -99,7 +99,7 @@ int cmt_rollup_emit_voucher(cmt_rollup_t *me, uint8_t address[CMT_ADDRESS_LENGTH * @return * - 0 success * - -ENOBUFS no space left in @p me */ -int cmt_rollup_emit_notice(cmt_rollup_t *me, size_t n, const void *data); +int cmt_rollup_emit_notice(cmt_rollup_t *me, uint32_t length, const void *data); /** Emit a report * @param [in,out] me initialized cmt_rollup_t instance @@ -108,7 +108,7 @@ int cmt_rollup_emit_notice(cmt_rollup_t *me, size_t n, const void *data); * @return * - 0 success * - -ENOBUFS no space left in @p me */ -int cmt_rollup_emit_report(cmt_rollup_t *me, size_t n, const void *data); +int cmt_rollup_emit_report(cmt_rollup_t *me, uint32_t length, const void *data); /** Emit a exception * @param [in,out] me initialized cmt_rollup_t instance @@ -117,7 +117,7 @@ int cmt_rollup_emit_report(cmt_rollup_t *me, size_t n, const void *data); * @return * - 0 success * - -ENOBUFS no space left in @p me */ -int cmt_rollup_emit_exception(cmt_rollup_t *me, size_t n, const void *data); +int cmt_rollup_emit_exception(cmt_rollup_t *me, uint32_t length, const void *data); /** Read advance state * diff --git a/sys-utils/libcmt/src/abi.c b/sys-utils/libcmt/src/abi.c index 1df60b0e..ba8ad11a 100644 --- a/sys-utils/libcmt/src/abi.c +++ b/sys-utils/libcmt/src/abi.c @@ -109,6 +109,17 @@ int cmt_abi_put_uint(cmt_buf_t *me, size_t n, const void *data) { return cmt_abi_encode_uint(n, data, x->begin); } +int cmt_abi_put_uint_be(cmt_buf_t *me, size_t n, const void *data) { + cmt_buf_t x[1]; + + if (n > CMT_WORD_LENGTH) + return -EDOM; + if (cmt_buf_split(me, CMT_WORD_LENGTH, x, me)) + return -ENOBUFS; + + return cmt_abi_encode_uint_nn(n, data, x->begin); +} + int cmt_abi_put_bool(cmt_buf_t *me, bool value) { uint8_t boolean = !!value; return cmt_abi_put_uint(me, sizeof(boolean), &boolean); @@ -188,6 +199,18 @@ int cmt_abi_get_uint(cmt_buf_t *me, size_t n, void *data) { return cmt_abi_decode_uint(x->begin, n, data); } +int cmt_abi_get_uint_be(cmt_buf_t *me, size_t n, void *data) { + cmt_buf_t x[1]; + + if (n > CMT_WORD_LENGTH) + return -EDOM; + int rc = cmt_buf_split(me, CMT_WORD_LENGTH, x, me); + if (rc) + return rc; + + return cmt_abi_decode_uint_nn(x->begin, n, data); +} + int cmt_abi_get_bool(cmt_buf_t *me, bool *value) { uint8_t boolean = 0; int rc = cmt_abi_put_uint(me, sizeof(boolean), &boolean); diff --git a/sys-utils/libcmt/src/rollup.c b/sys-utils/libcmt/src/rollup.c index df894ca9..0ac9c404 100644 --- a/sys-utils/libcmt/src/rollup.c +++ b/sys-utils/libcmt/src/rollup.c @@ -23,8 +23,8 @@ #include #include -// Voucher(address,bytes) -#define VOUCHER CMT_ABI_FUNSEL(0xef, 0x61, 0x5e, 0x2f) +// Voucher(address,uint256,bytes) +#define VOUCHER CMT_ABI_FUNSEL(0x23, 0x7a, 0x81, 0x6f) // Notice(bytes) #define NOTICE CMT_ABI_FUNSEL(0xc2, 0x58, 0xd6, 0xe5) @@ -71,7 +71,7 @@ void cmt_rollup_fini(cmt_rollup_t *me) { cmt_merkle_fini(me->merkle); } -int cmt_rollup_emit_voucher(cmt_rollup_t *me, uint8_t address[20], size_t length, const void *data) { +int cmt_rollup_emit_voucher(cmt_rollup_t *me, uint8_t address[CMT_ADDRESS_LENGTH], uint32_t value_length, const void *value_data, uint32_t length, const void *data) { if (!me) return -EINVAL; if (!data && length) @@ -82,8 +82,11 @@ int cmt_rollup_emit_voucher(cmt_rollup_t *me, uint8_t address[20], size_t length cmt_buf_t of[1]; void *params_base = tx->begin + 4; // after funsel - if (DBG(cmt_abi_put_funsel(wr, VOUCHER)) || DBG(cmt_abi_put_address(wr, address)) || - DBG(cmt_abi_put_bytes_s(wr, of)) || DBG(cmt_abi_put_bytes_d(wr, of, length, data, params_base))) + if (DBG(cmt_abi_put_funsel(wr, VOUCHER)) + || DBG(cmt_abi_put_address(wr, address)) + || DBG(cmt_abi_put_uint_be(wr, value_length, value_data)) + || DBG(cmt_abi_put_bytes_s(wr, of)) + || DBG(cmt_abi_put_bytes_d(wr, of, length, data, params_base))) return -ENOBUFS; size_t m = wr->begin - tx->begin; @@ -99,7 +102,7 @@ int cmt_rollup_emit_voucher(cmt_rollup_t *me, uint8_t address[20], size_t length return cmt_merkle_push_back_data(me->merkle, m, tx->begin); } -int cmt_rollup_emit_notice(cmt_rollup_t *me, size_t length, const void *data) { +int cmt_rollup_emit_notice(cmt_rollup_t *me, uint32_t length, const void *data) { if (!me) return -EINVAL; if (!data && length) @@ -127,7 +130,7 @@ int cmt_rollup_emit_notice(cmt_rollup_t *me, size_t length, const void *data) { return cmt_merkle_push_back_data(me->merkle, m, tx->begin); } -int cmt_rollup_emit_report(cmt_rollup_t *me, size_t length, const void *data) { +int cmt_rollup_emit_report(cmt_rollup_t *me, uint32_t length, const void *data) { if (!me) return -EINVAL; if (!data && length) @@ -148,7 +151,7 @@ int cmt_rollup_emit_report(cmt_rollup_t *me, size_t length, const void *data) { return DBG(cmt_io_yield(me->io, req)); } -int cmt_rollup_emit_exception(cmt_rollup_t *me, size_t length, const void *data) { +int cmt_rollup_emit_exception(cmt_rollup_t *me, uint32_t length, const void *data) { if (!me) return -EINVAL; if (!data && length) diff --git a/sys-utils/libcmt/src/tests/abi-single.c b/sys-utils/libcmt/src/tests/abi-single.c index 1fc0901b..3406c403 100644 --- a/sys-utils/libcmt/src/tests/abi-single.c +++ b/sys-utils/libcmt/src/tests/abi-single.c @@ -40,38 +40,10 @@ static void encode_u8() { uint8_t x = 0x01; uint8_t en[CMT_WORD_LENGTH]; uint8_t be[CMT_WORD_LENGTH] = { - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x01, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, }; cmt_abi_encode_uint(sizeof(x), (void *) &x, en); assert(memcmp(en, be, sizeof(be)) == 0); @@ -81,38 +53,10 @@ static void encode_u16() { uint16_t x = UINT16_C(0x0123); uint8_t en[CMT_WORD_LENGTH]; uint8_t be[CMT_WORD_LENGTH] = { - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x01, - 0x23, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x23, }; cmt_abi_encode_uint(sizeof(x), (void *) &x, en); assert(memcmp(en, be, sizeof(be)) == 0); @@ -122,38 +66,10 @@ static void encode_u32() { uint32_t x = UINT32_C(0x01234567); uint8_t en[CMT_WORD_LENGTH]; uint8_t be[CMT_WORD_LENGTH] = { - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x01, - 0x23, - 0x45, - 0x67, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x01, 0x23, 0x45, 0x67, }; cmt_abi_encode_uint(sizeof(x), (void *) &x, en); assert(memcmp(en, be, sizeof(be)) == 0); @@ -163,38 +79,10 @@ static void encode_u64() { uint64_t x = UINT64_C(0x0123456789abcdef); uint8_t en[CMT_WORD_LENGTH]; uint8_t be[CMT_WORD_LENGTH] = { - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x01, - 0x23, - 0x45, - 0x67, - 0x89, - 0xab, - 0xcd, - 0xef, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef, }; cmt_abi_encode_uint(sizeof(x), (void *) &x, en); assert(memcmp(en, be, sizeof(be)) == 0); @@ -202,73 +90,17 @@ static void encode_u64() { static void encode_u256() { uint8_t x[CMT_WORD_LENGTH] = { - 0x1f, - 0x1e, - 0x1d, - 0x1c, - 0x1b, - 0x1a, - 0x19, - 0x18, - 0x17, - 0x16, - 0x15, - 0x14, - 0x13, - 0x12, - 0x11, - 0x10, - 0x0f, - 0x0e, - 0x0d, - 0x0c, - 0x0b, - 0x0a, - 0x09, - 0x08, - 0x07, - 0x06, - 0x05, - 0x04, - 0x03, - 0x02, - 0x01, - 0x00, + 0x1f, 0x1e, 0x1d, 0x1c, 0x1b, 0x1a, 0x19, 0x18, + 0x17, 0x16, 0x15, 0x14, 0x13, 0x12, 0x11, 0x10, + 0x0f, 0x0e, 0x0d, 0x0c, 0x0b, 0x0a, 0x09, 0x08, + 0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01, 0x00, }; uint8_t en[CMT_WORD_LENGTH]; uint8_t be[CMT_WORD_LENGTH] = { - 0x00, - 0x01, - 0x02, - 0x03, - 0x04, - 0x05, - 0x06, - 0x07, - 0x08, - 0x09, - 0x0a, - 0x0b, - 0x0c, - 0x0d, - 0x0e, - 0x0f, - 0x10, - 0x11, - 0x12, - 0x13, - 0x14, - 0x15, - 0x16, - 0x17, - 0x18, - 0x19, - 0x1a, - 0x1b, - 0x1c, - 0x1d, - 0x1e, - 0x1f, + 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, + 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, + 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, + 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, }; cmt_abi_encode_uint(sizeof(x), (void *) &x, en); assert(memcmp(en, be, sizeof(be)) == 0); @@ -276,38 +108,10 @@ static void encode_u256() { static void encode_edom() { uint8_t x[CMT_WORD_LENGTH + 1] = { - 0x00, - 0x01, - 0x02, - 0x03, - 0x04, - 0x05, - 0x06, - 0x07, - 0x08, - 0x09, - 0x0a, - 0x0b, - 0x0c, - 0x0d, - 0x0e, - 0x0f, - 0x10, - 0x11, - 0x12, - 0x13, - 0x14, - 0x15, - 0x16, - 0x17, - 0x18, - 0x19, - 0x1a, - 0x1b, - 0x1c, - 0x1d, - 0x1e, - 0x1f, + 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, + 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, + 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, + 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20, }; uint8_t en[CMT_WORD_LENGTH]; @@ -318,38 +122,10 @@ static void encode_edom() { static void decode_u8() { uint8_t x, ex = 0x01; uint8_t be[CMT_WORD_LENGTH] = { - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x01, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, }; assert(cmt_abi_decode_uint(be, sizeof(x), (void *) &x) == 0); assert(x == ex); @@ -358,38 +134,10 @@ static void decode_u8() { static void decode_u16() { uint16_t x, ex = UINT16_C(0x0123); uint8_t be[CMT_WORD_LENGTH] = { - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x01, - 0x23, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x23, }; assert(cmt_abi_decode_uint(be, sizeof(x), (void *) &x) == 0); assert(x == ex); @@ -398,38 +146,10 @@ static void decode_u16() { static void decode_u32() { uint32_t x, ex = UINT32_C(0x01234567); uint8_t be[CMT_WORD_LENGTH] = { - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x01, - 0x23, - 0x45, - 0x67, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x01, 0x23, 0x45, 0x67, }; assert(cmt_abi_decode_uint(be, sizeof(x), (void *) &x) == 0); assert(x == ex); @@ -438,38 +158,10 @@ static void decode_u32() { static void decode_u64() { uint64_t x, ex = UINT64_C(0x0123456789abcdef); uint8_t be[CMT_WORD_LENGTH] = { - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x01, - 0x23, - 0x45, - 0x67, - 0x89, - 0xab, - 0xcd, - 0xef, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef, }; assert(cmt_abi_decode_uint(be, sizeof(x), (void *) &x) == 0); assert(x == ex); @@ -478,72 +170,16 @@ static void decode_u64() { static void decode_u256() { uint8_t x[CMT_WORD_LENGTH], ex[CMT_WORD_LENGTH] = { - 0x1f, - 0x1e, - 0x1d, - 0x1c, - 0x1b, - 0x1a, - 0x19, - 0x18, - 0x17, - 0x16, - 0x15, - 0x14, - 0x13, - 0x12, - 0x11, - 0x10, - 0x0f, - 0x0e, - 0x0d, - 0x0c, - 0x0b, - 0x0a, - 0x09, - 0x08, - 0x07, - 0x06, - 0x05, - 0x04, - 0x03, - 0x02, - 0x01, - 0x00, + 0x1f, 0x1e, 0x1d, 0x1c, 0x1b, 0x1a, 0x19, 0x18, + 0x17, 0x16, 0x15, 0x14, 0x13, 0x12, 0x11, 0x10, + 0x0f, 0x0e, 0x0d, 0x0c, 0x0b, 0x0a, 0x09, 0x08, + 0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01, 0x00, }; uint8_t be[CMT_WORD_LENGTH] = { - 0x00, - 0x01, - 0x02, - 0x03, - 0x04, - 0x05, - 0x06, - 0x07, - 0x08, - 0x09, - 0x0a, - 0x0b, - 0x0c, - 0x0d, - 0x0e, - 0x0f, - 0x10, - 0x11, - 0x12, - 0x13, - 0x14, - 0x15, - 0x16, - 0x17, - 0x18, - 0x19, - 0x1a, - 0x1b, - 0x1c, - 0x1d, - 0x1e, - 0x1f, + 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, + 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, + 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, + 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, }; assert(cmt_abi_decode_uint(be, sizeof(x), x) == 0); assert(memcmp(x, ex, sizeof(ex)) == 0); @@ -553,38 +189,10 @@ static void decode_u256() { static void decode_edom() { uint64_t x; uint8_t be[CMT_WORD_LENGTH] = { - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0xAA, - 0x01, - 0x23, - 0x45, - 0x67, - 0x89, - 0xab, - 0xcd, - 0xef, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xAA, + 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef, }; assert(cmt_abi_decode_uint(be, sizeof(x), (void *) &x) == -EDOM); } @@ -602,38 +210,10 @@ static void put_funsel() { static void put_uint() { uint64_t x = UINT64_C(0x0123456789abcdef); uint8_t be[CMT_WORD_LENGTH] = { - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x01, - 0x23, - 0x45, - 0x67, - 0x89, - 0xab, - 0xcd, - 0xef, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef, }; CMT_BUF_DECL(b, 64); cmt_buf_t it[1] = {*b}; @@ -643,41 +223,16 @@ static void put_uint() { } static void put_address() { - uint8_t x[20] = {0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef, 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef, - 0x01, 0x23, 0x45, 0x67}; + uint8_t x[20] = { + 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef, + 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef, + 0x01, 0x23, 0x45, 0x67 + }; uint8_t be[CMT_WORD_LENGTH] = { - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x01, - 0x23, - 0x45, - 0x67, - 0x89, - 0xab, - 0xcd, - 0xef, - 0x01, - 0x23, - 0x45, - 0x67, - 0x89, - 0xab, - 0xcd, - 0xef, - 0x01, - 0x23, - 0x45, - 0x67, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x01, 0x23, 0x45, 0x67, + 0x89, 0xab, 0xcd, 0xef, 0x01, 0x23, 0x45, 0x67, + 0x89, 0xab, 0xcd, 0xef, 0x01, 0x23, 0x45, 0x67, }; CMT_BUF_DECL(b, 64); cmt_buf_t it[1] = {*b}; @@ -689,86 +244,16 @@ static void put_address() { static void put_bytes() { uint64_t x = UINT64_C(0x0123456789abcdef); uint8_t be[] = { - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x20, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x08, - 0xef, - 0xcd, - 0xab, - 0x89, - 0x67, - 0x45, - 0x23, - 0x01, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x20, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x08, + 0xef, 0xcd, 0xab, 0x89, 0x67, 0x45, 0x23, 0x01, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, }; CMT_BUF_DECL(b, 128); cmt_buf_t it[1] = {*b}, of[1]; @@ -791,38 +276,10 @@ static void get_funsel() { static void get_uint() { uint64_t x, ex = UINT64_C(0x0123456789abcdef); uint8_t be[CMT_WORD_LENGTH] = { - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x01, - 0x23, - 0x45, - 0x67, - 0x89, - 0xab, - 0xcd, - 0xef, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef, }; CMT_BUF_DECL3(b, sizeof(be), be); cmt_buf_t rd[1] = {*b}; @@ -832,42 +289,17 @@ static void get_uint() { } static void get_address() { - uint8_t x[20], - ex[20] = {0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef, 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef, 0x01, - 0x23, 0x45, 0x67}; + uint8_t x[20]; + uint8_t ex[20] = { + 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef, + 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef, + 0x01, 0x23, 0x45, 0x67 + }; uint8_t be[CMT_WORD_LENGTH] = { - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x01, - 0x23, - 0x45, - 0x67, - 0x89, - 0xab, - 0xcd, - 0xef, - 0x01, - 0x23, - 0x45, - 0x67, - 0x89, - 0xab, - 0xcd, - 0xef, - 0x01, - 0x23, - 0x45, - 0x67, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x01, 0x23, 0x45, 0x67, + 0x89, 0xab, 0xcd, 0xef, 0x01, 0x23, 0x45, 0x67, + 0x89, 0xab, 0xcd, 0xef, 0x01, 0x23, 0x45, 0x67, }; CMT_BUF_DECL3(b, sizeof(be), be); cmt_buf_t it[1] = {*b}; @@ -879,86 +311,16 @@ static void get_address() { static void get_bytes() { uint64_t ex = UINT64_C(0x0123456789abcdef); uint8_t be[] = { - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x20, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x08, - 0xef, - 0xcd, - 0xab, - 0x89, - 0x67, - 0x45, - 0x23, - 0x01, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x20, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x08, + 0xef, 0xcd, 0xab, 0x89, 0x67, 0x45, 0x23, 0x01, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, }; CMT_BUF_DECL3(b, sizeof(be), be); cmt_buf_t it[1] = {*b}, of[1], bytes[1]; diff --git a/sys-utils/libcmt/src/tests/rollup_echo.c b/sys-utils/libcmt/src/tests/rollup_echo.c index 53ec0435..2fe5e7df 100644 --- a/sys-utils/libcmt/src/tests/rollup_echo.c +++ b/sys-utils/libcmt/src/tests/rollup_echo.c @@ -1,3 +1,15 @@ +/* Data encoding: + * - cast calldata "EvmAdvance(address,uint256,uint256,uint256,bytes)" \ + * 0x0000000000000000000000000000000000000000 \ + * 0x0000000000000000000000000000000000000001 \ + * 0x0000000000000000000000000000000000000002 \ + * 0x0000000000000000000000000000000000000003 \ + * 0x`echo "hello world" | xxd -r -p -c0` > "" + * + * Data decoding: + * - cast calldata-decode "Voucher(address,uint256,bytes)" 0x`xxd -p -c0 ""` + * + */ #include "libcmt/rollup.h" #include #include @@ -6,10 +18,17 @@ int main(void) { cmt_rollup_t rollup; - if (!cmt_rollup_init(&rollup)) + + if (cmt_rollup_init(&rollup)) return EXIT_FAILURE; // cmt_rollup_load_merkle(rollup, "/tmp/merkle.dat"); + uint8_t small[] = { + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, + }; for (;;) { int rc; cmt_rollup_finish_t finish = {.accept_previous_request = true}; @@ -27,7 +46,7 @@ int main(void) { break; } - rc = cmt_rollup_emit_voucher(&rollup, advance.sender, advance.length, advance.data); + rc = cmt_rollup_emit_voucher(&rollup, advance.sender, sizeof small, small, advance.length, advance.data); if (rc < 0) { fprintf(stderr, "%s:%d Error on voucher %s (%d)\n", __FILE__, __LINE__, strerror(-rc), (-rc)); break;