diff --git a/rollup-http/rollup-http-server/src/rollup/mod.rs b/rollup-http/rollup-http-server/src/rollup/mod.rs index d631b1a..288f943 100644 --- a/rollup-http/rollup-http-server/src/rollup/mod.rs +++ b/rollup-http/rollup-http-server/src/rollup/mod.rs @@ -148,7 +148,7 @@ impl From for AdvanceMetadata { let mut app_contract = "0x".to_string(); app_contract.push_str(&hex::encode(&other.app_contract)); let mut prev_randao = "0x".to_string(); - prev_randao.push_str(&hex::encode(&other.prev_randao)); + prev_randao.push_str(&hex::encode(&other.prev_randao.data)); AdvanceMetadata { chain_id: other.chain_id, app_contract: app_contract, @@ -258,7 +258,9 @@ pub fn rollup_read_advance_state_request( app_contract: Default::default(), block_number: 0, block_timestamp: 0, - prev_randao: Default::default(), + prev_randao: { cmt_u256_t { + data: Default::default(), + }}, index: 0, payload_length: 0, payload: std::ptr::null::<::std::os::raw::c_uchar>() as *mut c_void, diff --git a/sys-utils/libcmt/src/abi.c b/sys-utils/libcmt/src/abi.c index ac33963..6372915 100644 --- a/sys-utils/libcmt/src/abi.c +++ b/sys-utils/libcmt/src/abi.c @@ -26,6 +26,14 @@ uint32_t cmt_abi_funsel(uint8_t a, uint8_t b, uint8_t c, uint8_t d) { return CMT_ABI_FUNSEL(a, b, c, d); } +int cmt_abi_mark_frame(const cmt_buf_t *me, cmt_buf_t *frame) { + if (!me || !frame) { + return -EINVAL; + } + *frame = *me; + return 0; +} + int cmt_abi_put_funsel(cmt_buf_t *me, uint32_t funsel) { cmt_buf_t x[1]; int rc = cmt_buf_split(me, sizeof(funsel), x, me); @@ -130,18 +138,25 @@ int cmt_abi_put_uint_be(cmt_buf_t *me, size_t data_length, const void *data) { } return cmt_abi_encode_uint_nn(data_length, data, x->begin); } +int cmt_abi_put_uint256(cmt_buf_t *me, const cmt_abi_u256_t *value) { + cmt_buf_t x[1]; + if (cmt_buf_split(me, CMT_WORD_LENGTH, x, me)) { + return -ENOBUFS; + } + return cmt_abi_encode_uint_nn(sizeof(*value), value->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); } -int cmt_abi_put_address(cmt_buf_t *me, const uint8_t address[20]) { +int cmt_abi_put_address(cmt_buf_t *me, const cmt_abi_address_t *address) { cmt_buf_t x[1]; if (cmt_buf_split(me, CMT_WORD_LENGTH, x, me)) { return -ENOBUFS; } - return cmt_abi_encode_uint_nn(CMT_ADDRESS_LENGTH, address, x->begin); + return cmt_abi_encode_uint_nn(sizeof(*address), address->data, x->begin); } int cmt_abi_put_bytes_s(cmt_buf_t *me, cmt_buf_t *offset) { @@ -179,14 +194,14 @@ int cmt_abi_reserve_bytes_d(cmt_buf_t *me, cmt_buf_t *of, size_t n, cmt_buf_t *o return 0; } -int cmt_abi_put_bytes_d(cmt_buf_t *me, cmt_buf_t *offset, size_t n, const void *data, const void *start) { +int cmt_abi_put_bytes_d(cmt_buf_t *me, cmt_buf_t *offset, const cmt_buf_t *frame, const cmt_abi_bytes_t *payload) { cmt_buf_t res[1]; - int rc = cmt_abi_reserve_bytes_d(me, offset, n, res, start); + int rc = cmt_abi_reserve_bytes_d(me, offset, payload->length, res, frame->begin); if (rc) { return rc; } // NOLINTNEXTLINE(clang-analyzer-security.insecureAPI.DeprecatedOrUnsafeBufferHandling) - memcpy(res->begin, data, n); + memcpy(res->begin, payload->data, payload->length); return 0; } @@ -248,15 +263,14 @@ int cmt_abi_get_bool(cmt_buf_t *me, bool *value) { return 0; } -int cmt_abi_get_address(cmt_buf_t *me, uint8_t address[CMT_ADDRESS_LENGTH]) { +int cmt_abi_get_address(cmt_buf_t *me, cmt_abi_address_t *address) { cmt_buf_t x[1]; int rc = cmt_buf_split(me, CMT_WORD_LENGTH, x, me); if (rc) { return rc; } - - return cmt_abi_decode_uint_nn(x->begin, CMT_ADDRESS_LENGTH, address); + return cmt_abi_decode_uint_nn(x->begin, sizeof(*address), address->data); } int cmt_abi_get_bytes_s(cmt_buf_t *me, cmt_buf_t of[1]) { diff --git a/sys-utils/libcmt/src/abi.h b/sys-utils/libcmt/src/abi.h index 936c680..8bbb444 100644 --- a/sys-utils/libcmt/src/abi.h +++ b/sys-utils/libcmt/src/abi.h @@ -129,7 +129,20 @@ enum { (((uint32_t) (A) << 000) | ((uint32_t) (B) << 010) | ((uint32_t) (C) << 020) | ((uint32_t) (D) << 030)) #endif -// put section --------------------------------------------------------------- +/** EVM address */ +typedef struct cmt_abi_address { + uint8_t data[CMT_ADDRESS_LENGTH]; +} cmt_abi_address_t; + +/** EVM u256 in big endian format */ +typedef struct cmt_abi_u256 { + uint8_t data[CMT_WORD_LENGTH]; +} cmt_abi_u256_t; + +typedef struct cmt_abi_bytes { + size_t length; + void *data; +} cmt_abi_bytes_t; /** Create a function selector from an array of bytes * @param [in] funsel function selector bytes @@ -137,6 +150,21 @@ enum { * - function selector converted to big endian (as expected by EVM) */ uint32_t cmt_abi_funsel(uint8_t a, uint8_t b, uint8_t c, uint8_t d); +/** Start a frame for dynamic section + * @param [in] me reader or writer buffer + * @param [out] frame start of the parameters frame + * + * @return + * @return + * | | | + * |--:|-----------------------------| + * | 0| success | + * |< 0| failure with a -errno value | + */ +int cmt_abi_mark_frame(const cmt_buf_t *me, cmt_buf_t *frame); + +// put section --------------------------------------------------------------- + /** Encode a function selector into the buffer @p me * * @param [in,out] me a initialized buffer working as iterator @@ -152,7 +180,8 @@ uint32_t cmt_abi_funsel(uint8_t a, uint8_t b, uint8_t c, uint8_t d); * It is always represented in big endian. */ int cmt_abi_put_funsel(cmt_buf_t *me, uint32_t funsel); -/** Encode a unsigned integer of up to 32bytes of data into the buffer +/** Encode a native endianness unsigned integer of up to 32bytes of data into + * the buffer * * @param [in,out] me a initialized buffer working as iterator * @param [in] n size of @p data in bytes @@ -172,11 +201,11 @@ int cmt_abi_put_funsel(cmt_buf_t *me, uint32_t funsel); * uint64_t x = UINT64_C(0xdeadbeef); * cmt_abi_put_uint(&it, sizeof x, &x); * ... - * @endcode - * @note This function takes care of endianness conversions */ + * @endcode */ int cmt_abi_put_uint(cmt_buf_t *me, size_t data_length, const void *data); -/** Encode a big-endian value of up to 32bytes of data into the buffer +/** Encode a big endian unsigned integer 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 @@ -211,6 +240,30 @@ int cmt_abi_put_uint(cmt_buf_t *me, size_t data_length, const void *data); * @note This function takes care of endianness conversions */ int cmt_abi_put_uint_be(cmt_buf_t *me, size_t data_length, const void *data); +/** Encode a @ref cmt_abi_u256_t into the buffer + * + * @param [in,out] me a initialized buffer working as iterator + * @param [in] data pointer to a @ref cmt_abi_u256_t + * + * @return + * | | | + * |-------:|---------------------------------------------------| + * | 0| success | + * |-ENOBUFS| no space left in @p me | + * + * @code + * ... + * cmt_buf_t wr = ...; + * cmt_abi_u256_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_uint256(&wr, &small); + * @endcode */ +int cmt_abi_put_uint256(cmt_buf_t *me, const cmt_abi_u256_t *value); + /** Encode a bool into the buffer * * @param [in,out] me a initialized buffer working as iterator @@ -234,14 +287,14 @@ int cmt_abi_put_bool(cmt_buf_t *me, bool value); /** Encode @p address (exactly @ref CMT_ADDRESS_LENGTH bytes) into the buffer * * @param [in,out] me initialized buffer - * @param [in] address exactly @ref CMT_ADDRESS_LENGTH bytes + * @param [in] address a value of type @ref cmt_abi_address_t * * @return * | | | * |-------:|------------------------| * | 0| success | * |-ENOBUFS| no space left in @p me | */ -int cmt_abi_put_address(cmt_buf_t *me, const uint8_t address[CMT_ADDRESS_LENGTH]); +int cmt_abi_put_address(cmt_buf_t *me, const cmt_abi_address_t *address); /** Encode the static part of @b bytes into the message, * used in conjunction with @ref cmt_abi_put_bytes_d @@ -270,7 +323,8 @@ int cmt_abi_put_bytes_s(cmt_buf_t *me, cmt_buf_t *offset); * |-------:|------------------------| * | 0| success | * |-ENOBUFS| no space left in @p me | */ -int cmt_abi_put_bytes_d(cmt_buf_t *me, cmt_buf_t *offset, size_t n, const void *data, const void *start); +//int cmt_abi_put_bytes_d(cmt_buf_t *me, cmt_buf_t *offset, size_t n, const void *data, const void *start); +int cmt_abi_put_bytes_d(cmt_buf_t *me, cmt_buf_t *offset, const cmt_buf_t *frame, const cmt_abi_bytes_t *payload); /** Reserve @b n bytes of data from the buffer into @b res to be filled by the * caller @@ -327,7 +381,19 @@ uint32_t cmt_abi_peek_funsel(cmt_buf_t *me); * |-EBADMSG| funsel mismatch | */ int cmt_abi_check_funsel(cmt_buf_t *me, uint32_t expected); -/** Decode a unsigned integer of up to 32bytes from the buffer +/** Decode a @ref cmt_abi_u256_t from the buffer + * + * @param [in,out] me initialized buffer + * @param [out] data value of type @ref cmt_abi_u256_t + * + * @return + * | | | + * |-------:|---------------------------------------------------| + * | 0| success | + * |-ENOBUFS| no space left in @p me | */ +int cmt_abi_get_uint256(cmt_buf_t *me, cmt_abi_u256_t *value); + +/** Decode a unsigned integer of up to 32bytes, in native endianness, from the buffer * * @param [in,out] me initialized buffer * @param [in] n size of @p data in bytes @@ -380,14 +446,25 @@ int cmt_abi_get_bool(cmt_buf_t *me, bool *value); /** Consume and decode @b address from the buffer * * @param [in,out] me initialized buffer - * @param [out] address exactly 20 bytes + * @param [out] address value of type @ref cmt_abi_address_t + * + * @return + * | | | + * |-------:|------------------------| + * | 0| success | + * |-ENOBUFS| no space left in @p me | */ +int cmt_abi_get_address(cmt_buf_t *me, cmt_abi_address_t *value); + +/** Create a frame of reference for the dynamic section * + * @param [in,out] me initialized buffer + * @param [out] frame used when encoding dynamic values * @return * | | | * |-------:|------------------------| * | 0| success | * |-ENOBUFS| no space left in @p me | */ -int cmt_abi_get_address(cmt_buf_t *me, uint8_t address[CMT_ADDRESS_LENGTH]); +int cmt_abi_start_frame(cmt_buf_t *me, void *frame); /** Consume and decode the offset @p of * diff --git a/sys-utils/libcmt/src/rollup.c b/sys-utils/libcmt/src/rollup.c index 8c46d69..9d241e8 100644 --- a/sys-utils/libcmt/src/rollup.c +++ b/sys-utils/libcmt/src/rollup.c @@ -66,29 +66,27 @@ void cmt_rollup_fini(cmt_rollup_t *me) { cmt_merkle_fini(me->merkle); } -int cmt_rollup_emit_voucher(cmt_rollup_t *me, uint32_t address_length, const void *address_data, uint32_t value_length, - const void *value_data, uint32_t length, const void *data, uint64_t *index) { +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 *payload, uint64_t *index) { if (!me) { return -EINVAL; } - if (!data && length) { + if (!payload || (!payload->data && payload->length)) { return -EINVAL; } cmt_buf_t tx[1] = {cmt_io_get_tx(me->io)}; cmt_buf_t wr[1] = {*tx}; cmt_buf_t of[1]; - void *params_base = tx->begin + 4; // after funsel - - if (address_length != CMT_ADDRESS_LENGTH) - return -EINVAL; + cmt_buf_t frame[1]; // clang-format off if (DBG(cmt_abi_put_funsel(wr, VOUCHER)) - || DBG(cmt_abi_put_address(wr, address_data)) - || DBG(cmt_abi_put_uint_be(wr, value_length, value_data)) + || DBG(cmt_abi_mark_frame(wr, frame)) + || DBG(cmt_abi_put_address(wr, address)) + || DBG(cmt_abi_put_uint256(wr, value)) || DBG(cmt_abi_put_bytes_s(wr, of)) - || DBG(cmt_abi_put_bytes_d(wr, of, length, data, params_base))) { + || DBG(cmt_abi_put_bytes_d(wr, of, frame, payload))) { return -ENOBUFS; } // clang-format on @@ -119,23 +117,24 @@ int cmt_rollup_emit_voucher(cmt_rollup_t *me, uint32_t address_length, const voi return 0; } -int cmt_rollup_emit_notice(cmt_rollup_t *me, uint32_t length, const void *data, uint64_t *index) { +int cmt_rollup_emit_notice(cmt_rollup_t *me, const cmt_abi_bytes_t *payload, uint64_t *index) { if (!me) { return -EINVAL; } - if (!data && length) { + if (!payload || (!payload->data && payload->length)) { return -EINVAL; } cmt_buf_t tx[1] = {cmt_io_get_tx(me->io)}; cmt_buf_t wr[1] = {*tx}; cmt_buf_t of[1]; - void *params_base = tx->begin + 4; // after funsel + cmt_buf_t frame[1]; // clang-format off if (DBG(cmt_abi_put_funsel(wr, NOTICE)) + || DBG(cmt_abi_mark_frame(wr, frame)) || DBG(cmt_abi_put_bytes_s(wr, of)) - || DBG(cmt_abi_put_bytes_d(wr, of, length, data, params_base))) { + || DBG(cmt_abi_put_bytes_d(wr, of, frame, payload))) { return -ENOBUFS; } // clang-format on @@ -166,55 +165,55 @@ int cmt_rollup_emit_notice(cmt_rollup_t *me, uint32_t length, const void *data, return 0; } -int cmt_rollup_emit_report(cmt_rollup_t *me, uint32_t length, const void *data) { +int cmt_rollup_emit_report(cmt_rollup_t *me, const cmt_abi_bytes_t *payload) { if (!me) { return -EINVAL; } - if (!data && length) { + if (!payload || (!payload->data && payload->length)) { return -EINVAL; } cmt_buf_t tx[1] = {cmt_io_get_tx(me->io)}; cmt_buf_t wr[1] = {*tx}; - if (cmt_buf_split(tx, length, wr, tx)) { + if (cmt_buf_split(tx, payload->length, wr, tx)) { return -ENOBUFS; } - if (data) { - memcpy(wr->begin, data, length); + if (payload->data) { + memcpy(wr->begin, payload->data, payload->length); } struct cmt_io_yield req[1] = {{ .dev = HTIF_DEVICE_YIELD, .cmd = HTIF_YIELD_CMD_AUTOMATIC, .reason = HTIF_YIELD_AUTOMATIC_REASON_TX_REPORT, - .data = length, + .data = payload->length, }}; return DBG(cmt_io_yield(me->io, req)); } -int cmt_rollup_emit_exception(cmt_rollup_t *me, uint32_t length, const void *data) { +int cmt_rollup_emit_exception(cmt_rollup_t *me, const cmt_abi_bytes_t *payload) { if (!me) { return -EINVAL; } - if (!data && length) { + if (!payload || (!payload->data && payload->length)) { return -EINVAL; } cmt_buf_t tx[1] = {cmt_io_get_tx(me->io)}; cmt_buf_t wr[1] = {*tx}; cmt_buf_t _[1]; - if (cmt_buf_split(tx, length, wr, _)) { + if (cmt_buf_split(tx, payload->length, wr, _)) { return -ENOBUFS; } - if (data) { - memcpy(wr->begin, data, length); + if (payload->data) { + memcpy(wr->begin, payload->data, payload->length); } struct cmt_io_yield req[1] = {{ .dev = HTIF_DEVICE_YIELD, .cmd = HTIF_YIELD_CMD_MANUAL, .reason = HTIF_YIELD_MANUAL_REASON_TX_EXCEPTION, - .data = length, + .data = payload->length, }}; return DBG(cmt_io_yield(me->io, req)); } @@ -237,27 +236,25 @@ int cmt_rollup_read_advance_state(cmt_rollup_t *me, cmt_rollup_advance_t *advanc if (cmt_rollup_get_rx(me, rd)) { return -ENOBUFS; } - cmt_buf_t anchor[1] = {{rd->begin + 4, rd->end}}; + cmt_buf_t frame[1]; cmt_buf_t of[1]; - size_t payload_length = 0; - // clang-format off if (DBG(cmt_abi_check_funsel(rd, EVM_ADVANCE)) + || DBG(cmt_abi_mark_frame(rd, frame)) || DBG(cmt_abi_get_uint(rd, sizeof(advance->chain_id), &advance->chain_id)) - || DBG(cmt_abi_get_address(rd, advance->app_contract)) - || DBG(cmt_abi_get_address(rd, advance->msg_sender)) + || DBG(cmt_abi_get_address(rd, &advance->app_contract)) + || DBG(cmt_abi_get_address(rd, &advance->msg_sender)) || DBG(cmt_abi_get_uint(rd, sizeof(advance->block_number), &advance->block_number)) || DBG(cmt_abi_get_uint(rd, sizeof(advance->block_timestamp), &advance->block_timestamp)) - || DBG(cmt_abi_get_uint(rd, sizeof(advance->prev_randao), advance->prev_randao)) + || DBG(cmt_abi_get_uint(rd, sizeof(advance->prev_randao), advance->prev_randao.data)) || DBG(cmt_abi_get_uint(rd, sizeof(advance->index), &advance->index)) || DBG(cmt_abi_get_bytes_s(rd, of)) - || DBG(cmt_abi_get_bytes_d(anchor, of, &payload_length, &advance->payload))) { + || DBG(cmt_abi_get_bytes_d(frame, of, &advance->payload.length, &advance->payload.data))) { return -ENOBUFS; } // clang-format on - advance->payload_length = payload_length; return 0; } diff --git a/sys-utils/libcmt/src/rollup.h b/sys-utils/libcmt/src/rollup.h index b522245..b4f6a93 100644 --- a/sys-utils/libcmt/src/rollup.h +++ b/sys-utils/libcmt/src/rollup.h @@ -44,14 +44,13 @@ typedef struct cmt_rollup { /** Public struct with the advance state contents */ typedef struct cmt_rollup_advance { uint64_t chain_id; /**< network */ - uint8_t app_contract[CMT_ADDRESS_LENGTH]; /**< application contract address */ - uint8_t msg_sender[CMT_ADDRESS_LENGTH]; /**< input sender address */ + cmt_abi_address_t app_contract; /**< application contract address */ + cmt_abi_address_t msg_sender; /**< input sender address */ uint64_t block_number; /**< block number of this input */ uint64_t block_timestamp; /**< block timestamp of this input UNIX epoch format) */ - uint8_t prev_randao[CMT_WORD_LENGTH]; /**< The latest RANDAO mix of the post beacon state of the previous block */ + cmt_abi_u256_t prev_randao; /**< The latest RANDAO mix of the post beacon state of the previous block */ uint64_t index; /**< input index (in relation to all inputs ever sent to the DApp) */ - uint32_t payload_length; /**< length in bytes of the payload field */ - void *payload; /**< payload for this input */ + cmt_abi_bytes_t payload; /**< payload for this input */ } cmt_rollup_advance_t; /** Public struct with the inspect state contents */ @@ -101,11 +100,8 @@ void cmt_rollup_fini(cmt_rollup_t *me); * Equivalent to the `Voucher(address,uint256,bytes)` solidity call. * * @param [in,out] me initialized @ref cmt_rollup_t instance - * @param [in] address_length destination length in bytes * @param [in] address destination data - * @param [in] value_length value length in bytes * @param [in] value value data - * @param [in] data_length data length in bytes * @param [in] data message contents * @param [out] index index of emitted voucher, if successful * @@ -114,13 +110,11 @@ void cmt_rollup_fini(cmt_rollup_t *me); * |--:|-----------------------------| * | 0| success | * |< 0| failure with a -errno value | */ -int cmt_rollup_emit_voucher(cmt_rollup_t *me, uint32_t address_length, const void *address_data, uint32_t value_length, - const void *value_data, uint32_t length, const void *data, uint64_t *index); +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); /** Emit a notice * * @param [in,out] me initialized cmt_rollup_t instance - * @param [in] data_length data length in bytes * @param [in] data message contents * @param [out] index index of emitted notice, if successful * @@ -129,7 +123,7 @@ int cmt_rollup_emit_voucher(cmt_rollup_t *me, uint32_t address_length, const voi * |--:|-----------------------------| * | 0| success | * |< 0| failure with a -errno value | */ -int cmt_rollup_emit_notice(cmt_rollup_t *me, uint32_t data_length, const void *data, uint64_t *index); +int cmt_rollup_emit_notice(cmt_rollup_t *me, const cmt_abi_bytes_t *payload, uint64_t *index); /** Emit a report * @param [in,out] me initialized cmt_rollup_t instance @@ -141,7 +135,7 @@ int cmt_rollup_emit_notice(cmt_rollup_t *me, uint32_t data_length, const void *d * |--:|-----------------------------| * | 0| success | * |< 0| failure with a -errno value | */ -int cmt_rollup_emit_report(cmt_rollup_t *me, uint32_t data_length, const void *data); +int cmt_rollup_emit_report(cmt_rollup_t *me, const cmt_abi_bytes_t *payload); /** Emit a exception * @param [in,out] me initialized cmt_rollup_t instance @@ -153,7 +147,7 @@ int cmt_rollup_emit_report(cmt_rollup_t *me, uint32_t data_length, const void *d * |--:|-----------------------------| * | 0| success | * |< 0| failure with a -errno value | */ -int cmt_rollup_emit_exception(cmt_rollup_t *me, uint32_t data_length, const void *data); +int cmt_rollup_emit_exception(cmt_rollup_t *me, const cmt_abi_bytes_t *payload); /** Report progress * diff --git a/sys-utils/libcmt/tests/abi-multi.c b/sys-utils/libcmt/tests/abi-multi.c index 1c5fc88..009d266 100644 --- a/sys-utils/libcmt/tests/abi-multi.c +++ b/sys-utils/libcmt/tests/abi-multi.c @@ -62,19 +62,23 @@ static int test_request(void) { cmt_buf_t bb[1] = {{mem, mem + sizeof mem}}; cmt_buf_t wr[1] = {*bb}; cmt_buf_t of[1]; + cmt_buf_t frame[1]; - uint8_t address[20] = {0}; + cmt_abi_address_t address = {0}; uint8_t bytes[] = {0xde, 0xad, 0xbe, 0xef}; cmt_abi_put_funsel(wr, REQUEST); - uint8_t *frame = wr->begin; // dynamic frame begins after funsel - cmt_abi_put_address(wr, address); + cmt_abi_mark_frame(wr, frame); + cmt_abi_put_address(wr, &address); cmt_abi_put_uint(wr, sizeof(int), &(int[]){1}); cmt_abi_put_uint(wr, sizeof(int), &(int[]){2}); cmt_abi_put_uint(wr, sizeof(int), &(int[]){3}); cmt_abi_put_bytes_s(wr, of); - cmt_abi_put_bytes_d(wr, of, sizeof bytes, bytes, frame); + cmt_abi_put_bytes_d(wr, of, frame, &(cmt_abi_bytes_t){sizeof bytes, bytes}); + fprintf(stderr, "%zu, %zu\n", sizeof request, wr->begin - bb->begin); + cmt_buf_xxd(request+4, (&request)[1], 0x20); + cmt_buf_xxd(mem+4, mem + sizeof mem, 0x20); return sizeof request != (wr->begin - bb->begin) || memeq(request, bb->begin, sizeof request, __FILE__, __LINE__); } @@ -98,16 +102,20 @@ static int test_reply(void) { cmt_buf_t bb[1] = {{mem, mem + sizeof mem}}; cmt_buf_t wr[1] = {*bb}; cmt_buf_t of[1]; + cmt_buf_t frame[1]; - uint8_t address[20] = {0}; + cmt_abi_address_t address = {0}; uint8_t bytes[] = {0xde, 0xad, 0xbe, 0xef}; cmt_abi_put_funsel(wr, REPLY); - uint8_t *frame = wr->begin; // dynamic frame begins after funsel - cmt_abi_put_address(wr, address); + cmt_abi_mark_frame(wr, frame); + cmt_abi_put_address(wr, &address); cmt_abi_put_bytes_s(wr, of); - cmt_abi_put_bytes_d(wr, of, sizeof bytes, bytes, frame); + cmt_abi_put_bytes_d(wr, of, frame, &(cmt_abi_bytes_t){sizeof bytes, bytes}); + //fprintf(stderr, "%zu, %zu\n", sizeof reply, wr->begin - bb->begin); + //cmt_buf_xxd(reply+4, (&reply)[1], 0x20); + //cmt_buf_xxd(bb->begin+4, wr->begin, 0x20); return sizeof reply != (wr->begin - bb->begin) || memeq(reply, bb->begin, sizeof reply, __FILE__, __LINE__); } diff --git a/sys-utils/libcmt/tests/abi-single.c b/sys-utils/libcmt/tests/abi-single.c index cf63e63..56359b7 100644 --- a/sys-utils/libcmt/tests/abi-single.c +++ b/sys-utils/libcmt/tests/abi-single.c @@ -284,31 +284,29 @@ static void put_bool(void) { } static void put_address(void) { - uint8_t x[CMT_ADDRESS_LENGTH] = { - // clang-format off + // clang-format off + cmt_abi_address_t x = {{ 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef, 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef, 0x01, 0x23, 0x45, 0x67, - // clang-format on - }; - uint8_t be[CMT_WORD_LENGTH] = { - // clang-format off + }}; + cmt_abi_address_t be = {{ 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, - // clang-format on - }; + }}; + // clang-format on CMT_BUF_DECL(b, 64); cmt_buf_t it[1] = {*b}; - assert(cmt_abi_put_address(it, x) == 0); - assert(memcmp(b->begin, be, sizeof(be)) == 0); + assert(cmt_abi_put_address(it, &x) == 0); + assert(memcmp(b->begin, be.data, sizeof(be)) == 0); } static void put_address_enobufs(void) { - uint8_t x[CMT_ADDRESS_LENGTH] = {0}; + cmt_abi_address_t x = {{0}}; CMT_BUF_DECL(b, CMT_WORD_LENGTH - 1); cmt_buf_t it[1] = {*b}; - assert(cmt_abi_put_address(it, x) == -ENOBUFS); + assert(cmt_abi_put_address(it, &x) == -ENOBUFS); } static void put_bytes(void) { @@ -325,22 +323,24 @@ static void put_bytes(void) { CMT_BUF_DECL(b, 128); cmt_buf_t it[1] = {*b}; cmt_buf_t of[1]; + cmt_buf_t frame[1]; + assert(cmt_abi_mark_frame(it, frame) == 0); assert(cmt_abi_put_bytes_s(it, of) == 0); - assert(cmt_abi_put_bytes_d(it, of, sizeof(x), &x, b->begin) == 0); + assert(cmt_abi_put_bytes_d(it, of, frame, &(cmt_abi_bytes_t){sizeof(x), &x}) == 0); assert(memcmp(b->begin, be, sizeof(be)) == 0); } static void put_bytes_enobufs(void) { uint64_t x = UINT64_C(0x0123456789abcdef); cmt_buf_t of[1]; + cmt_buf_t frame[1]; - { - CMT_BUF_DECL(b, 3 * 32 - 1); - cmt_buf_t it[1] = {*b}; - assert(cmt_abi_put_bytes_s(it, of) == 0); - assert(cmt_abi_put_bytes_d(it, of, sizeof(x), &x, b->begin) == -ENOBUFS); - } + CMT_BUF_DECL(b, 3 * 32 - 1); + cmt_buf_t it[1] = {*b}; + assert(cmt_abi_mark_frame(it, frame) == 0); + assert(cmt_abi_put_bytes_s(it, of) == 0); + assert(cmt_abi_put_bytes_d(it, of, frame, &(cmt_abi_bytes_t){sizeof(x), &x}) == -ENOBUFS); } static void get_funsel(void) { @@ -446,37 +446,35 @@ static void get_bool_enobufs(void) { } static void get_address(void) { - uint8_t x[CMT_ADDRESS_LENGTH]; + // clang-format off + cmt_abi_address_t x; uint8_t ex[CMT_ADDRESS_LENGTH] = { - // clang-format off 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef, 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef, 0x01, 0x23, 0x45, 0x67 - // clang-format on }; - uint8_t be[CMT_WORD_LENGTH] = { - // clang-format off + uint8_t be[] = { 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, - // clang-format on }; + // clang-format on CMT_BUF_DECL3(b, sizeof(be), be); cmt_buf_t it[1] = {*b}; - assert(cmt_abi_get_address(it, x) == 0); - assert(memcmp(x, ex, sizeof(ex)) == 0); + assert(cmt_abi_get_address(it, &x) == 0); + assert(memcmp(x.data, ex, sizeof(ex)) == 0); } static void get_address_enobufs(void) { - uint8_t x[CMT_ADDRESS_LENGTH]; + cmt_abi_address_t x; + // clang-format off uint8_t be[CMT_WORD_LENGTH - 1] = { - // clang-format off 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, - // clang-format on }; + // clang-format on CMT_BUF_DECL3(b, sizeof(be), be); cmt_buf_t it[1] = {*b}; - assert(cmt_abi_get_address(it, x) == -ENOBUFS); + assert(cmt_abi_get_address(it, &x) == -ENOBUFS); } static void get_bytes(void) { @@ -501,15 +499,15 @@ static void get_bytes(void) { } static void get_bytes_enobufs(void) { + // clang-format off uint8_t be[] = { - // clang-format off 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, - // clang-format on }; + // clang-format on { // when offset of dynamic reagion failed CMT_BUF_DECL3(b, 1 * CMT_WORD_LENGTH - 1, be); diff --git a/sys-utils/libcmt/tests/rollup.c b/sys-utils/libcmt/tests/rollup.c index b6e871e..3afec4f 100644 --- a/sys-utils/libcmt/tests/rollup.c +++ b/sys-utils/libcmt/tests/rollup.c @@ -64,25 +64,25 @@ static void check_first_input(cmt_rollup_t *rollup) { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, }; - uint8_t expected_prev_randao[CMT_WORD_LENGTH] = { + cmt_abi_u256_t expected_prev_randao = {{ 0x06, 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, - }; + }}; char expected_payload[] = "advance-0"; // clang-format on // verify the parsed data assert(advance.chain_id == 1); - assert(memcmp(advance.app_contract, expected_app_contract, CMT_ADDRESS_LENGTH) == 0); - assert(memcmp(advance.msg_sender, expected_msg_sender, CMT_ADDRESS_LENGTH) == 0); + assert(memcmp(advance.app_contract.data, expected_app_contract, CMT_ADDRESS_LENGTH) == 0); + assert(memcmp(advance.msg_sender.data, expected_msg_sender, CMT_ADDRESS_LENGTH) == 0); assert(advance.block_number == 4); assert(advance.block_timestamp == 5); - assert(memcmp(advance.prev_randao, expected_prev_randao, CMT_WORD_LENGTH) == 0); + assert(memcmp(&advance.prev_randao.data, expected_prev_randao.data, CMT_WORD_LENGTH) == 0); assert(advance.index == 7); - assert(advance.payload_length == strlen(expected_payload)); - assert(memcmp(advance.payload, expected_payload, strlen(expected_payload)) == 0); + assert(advance.payload.length == strlen(expected_payload)); + assert(memcmp(advance.payload.data, expected_payload, strlen(expected_payload)) == 0); } static void check_second_input(cmt_rollup_t *rollup) { @@ -132,73 +132,78 @@ void test_rollup_outputs_reports_and_exceptions(void) { size_t read_size = 0; // clang-format off - uint8_t address[CMT_ADDRESS_LENGTH] = { + cmt_abi_address_t address = {{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01 - }; + }}; // clang-format on assert(cmt_rollup_init(&rollup) == 0); // voucher - uint8_t value[] = {0xde, 0xad, 0xbe, 0xef}; + cmt_abi_u256_t value = {{ + 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, + 0xde, 0xad, 0xbe, 0xef + }}; char voucher_data[] = "voucher-0"; - assert(cmt_rollup_emit_voucher(&rollup, sizeof address, address, sizeof value, value, strlen(voucher_data), - voucher_data, &index) == 0); + assert(cmt_rollup_emit_voucher(&rollup, &address, &value, &(cmt_abi_bytes_t){strlen(voucher_data), voucher_data}, &index) == 0); assert(index == 0); assert(cmt_util_read_whole_file("none.output-0.bin", sizeof buffer, buffer, &read_size) == 0); assert(sizeof valid_voucher_0 == read_size); assert(memcmp(valid_voucher_0, buffer, sizeof valid_voucher_0) == 0); // voucher (invalid) - assert(cmt_rollup_emit_voucher(NULL, sizeof address, address, sizeof value, value, strlen(voucher_data), - voucher_data, &index) == -EINVAL); - assert(cmt_rollup_emit_voucher(&rollup, sizeof address - 1, address, sizeof value, value, strlen(voucher_data), - NULL, &index) == -EINVAL); - assert(cmt_rollup_emit_voucher(&rollup, sizeof address - 1, address, sizeof value, value, strlen(voucher_data), - voucher_data, &index) == -EINVAL); - assert(cmt_rollup_emit_voucher(&rollup, sizeof address, address, sizeof value, value, UINT32_MAX, voucher_data, + assert(cmt_rollup_emit_voucher(NULL, &address, &value, &(cmt_abi_bytes_t){strlen(voucher_data), voucher_data}, &index) == -EINVAL); + assert(cmt_rollup_emit_voucher(&rollup, &address, &value, &(cmt_abi_bytes_t){strlen(voucher_data), + NULL}, &index) == -EINVAL); + assert(cmt_rollup_emit_voucher(&rollup, &address, &value, &(cmt_abi_bytes_t){UINT32_MAX, voucher_data}, &index) == -ENOBUFS); // notice char notice_data[] = "notice-0"; - assert(cmt_rollup_emit_notice(&rollup, strlen(notice_data), notice_data, &index) == 0); + assert(cmt_rollup_emit_notice(&rollup, &(cmt_abi_bytes_t){strlen(notice_data), notice_data}, &index) == 0); assert(cmt_util_read_whole_file("none.output-1.bin", sizeof buffer, buffer, &read_size) == 0); assert(sizeof valid_notice_0 == read_size); assert(memcmp(valid_notice_0, buffer, sizeof valid_notice_0) == 0); assert(index == 1); // notice (invalid) - assert(cmt_rollup_emit_notice(NULL, strlen(notice_data), notice_data, &index) == -EINVAL); - assert(cmt_rollup_emit_notice(&rollup, strlen(notice_data), NULL, &index) == -EINVAL); - assert(cmt_rollup_emit_notice(&rollup, UINT32_MAX, notice_data, &index) == -ENOBUFS); + assert(cmt_rollup_emit_notice(NULL, &(cmt_abi_bytes_t){strlen(notice_data), notice_data}, &index) == -EINVAL); + assert(cmt_rollup_emit_notice(&rollup, &(cmt_abi_bytes_t){strlen(notice_data), NULL}, &index) == -EINVAL); + assert(cmt_rollup_emit_notice(&rollup, &(cmt_abi_bytes_t){UINT32_MAX, notice_data}, &index) == -ENOBUFS); // report char report_data[] = "report-0"; - assert(cmt_rollup_emit_report(&rollup, strlen(report_data), report_data) == 0); + assert(cmt_rollup_emit_report(&rollup, &(cmt_abi_bytes_t){strlen(report_data), report_data}) == 0); assert(cmt_util_read_whole_file("none.report-0.bin", sizeof buffer, buffer, &read_size) == 0); assert(sizeof valid_report_0 == read_size); assert(memcmp(valid_report_0, buffer, sizeof valid_report_0) == 0); // report (invalid) - assert(cmt_rollup_emit_report(NULL, strlen(report_data), report_data) == -EINVAL); - assert(cmt_rollup_emit_report(&rollup, strlen(report_data), NULL) == -EINVAL); - assert(cmt_rollup_emit_report(&rollup, UINT32_MAX, report_data) == -ENOBUFS); + assert(cmt_rollup_emit_report(NULL, &(cmt_abi_bytes_t){strlen(report_data), report_data}) == -EINVAL); + assert(cmt_rollup_emit_report(&rollup, &(cmt_abi_bytes_t){strlen(report_data), NULL}) == -EINVAL); + assert(cmt_rollup_emit_report(&rollup, &(cmt_abi_bytes_t){UINT32_MAX, report_data}) == -ENOBUFS); // exception char exception_data[] = "exception-0"; - assert(cmt_rollup_emit_exception(&rollup, strlen(exception_data), exception_data) == 0); + assert(cmt_rollup_emit_exception(&rollup, &(cmt_abi_bytes_t){strlen(exception_data), exception_data}) == 0); assert(cmt_util_read_whole_file("none.exception-0.bin", sizeof buffer, buffer, &read_size) == 0); assert(sizeof valid_exception_0 == read_size); assert(memcmp(valid_exception_0, buffer, sizeof valid_exception_0) == 0); // exception (invalid) - assert(cmt_rollup_emit_exception(NULL, strlen(exception_data), exception_data) == -EINVAL); - assert(cmt_rollup_emit_exception(&rollup, strlen(exception_data), NULL) == -EINVAL); - assert(cmt_rollup_emit_exception(&rollup, UINT32_MAX, exception_data) == -ENOBUFS); + assert(cmt_rollup_emit_exception(NULL, &(cmt_abi_bytes_t){strlen(exception_data), exception_data}) == -EINVAL); + assert(cmt_rollup_emit_exception(&rollup, &(cmt_abi_bytes_t){strlen(exception_data), NULL}) == -EINVAL); + assert(cmt_rollup_emit_exception(&rollup, &(cmt_abi_bytes_t){UINT32_MAX, exception_data}) == -ENOBUFS); cmt_rollup_fini(&rollup); printf("test_rollup_outputs_reports_and_exceptions passed!\n");