Skip to content

Commit

Permalink
feat!: stronger types
Browse files Browse the repository at this point in the history
  • Loading branch information
mpolitzer committed May 22, 2024
1 parent 6942b71 commit aced6d8
Show file tree
Hide file tree
Showing 8 changed files with 236 additions and 141 deletions.
6 changes: 4 additions & 2 deletions rollup-http/rollup-http-server/src/rollup/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ impl From<cmt_rollup_advance_t> 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,
Expand Down Expand Up @@ -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,
Expand Down
30 changes: 22 additions & 8 deletions sys-utils/libcmt/src/abi.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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]) {
Expand Down
99 changes: 88 additions & 11 deletions sys-utils/libcmt/src/abi.h
Original file line number Diff line number Diff line change
Expand Up @@ -129,14 +129,42 @@ 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
* @return
* - 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
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
*
Expand Down
Loading

0 comments on commit aced6d8

Please sign in to comment.