Skip to content

Commit

Permalink
feat!: rename CMT_ABI_WORD_LENGTH to CMT_ABI_U256_LENGTH
Browse files Browse the repository at this point in the history
  • Loading branch information
mpolitzer committed May 23, 2024
1 parent 47d6db0 commit ce96c95
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 85 deletions.
16 changes: 8 additions & 8 deletions sys-utils/libcmt/include/libcmt/abi.h
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@
#include <stdbool.h>

enum {
CMT_WORD_LENGTH = 32, /**< length of a evm word in bytes */
CMT_ABI_U256_LENGTH = 32, /**< length of a evm word in bytes */
CMT_ADDRESS_LENGTH = 20, /**< length of a evm address in bytes */
};

Expand All @@ -136,7 +136,7 @@ typedef struct cmt_abi_address {

/** EVM u256 in big endian format */
typedef struct cmt_abi_u256 {
uint8_t data[CMT_WORD_LENGTH];
uint8_t data[CMT_ABI_U256_LENGTH];
} cmt_abi_u256_t;

typedef struct cmt_abi_bytes {
Expand Down Expand Up @@ -522,7 +522,7 @@ int cmt_abi_peek_bytes_d(const cmt_buf_t *start, cmt_buf_t of[1], cmt_buf_t *byt
* |-------:|---------------------------------------------------|
* | 0| success |
* | -EDOM| integer not representable in @p data_length bytes | */
int cmt_abi_encode_uint(size_t n, const void *data, uint8_t out[CMT_WORD_LENGTH]);
int cmt_abi_encode_uint(size_t n, const void *data, uint8_t out[CMT_ABI_U256_LENGTH]);

/** Encode @p n bytes of @p data into @p out (up to 32) in reverse order.
*
Expand All @@ -537,7 +537,7 @@ int cmt_abi_encode_uint(size_t n, const void *data, uint8_t out[CMT_WORD_LENGTH]
* | -EDOM| integer not representable in @p data_length bytes |
*
* @note use @ref cmt_abi_encode_uint instead */
int cmt_abi_encode_uint_nr(size_t n, const uint8_t *data, uint8_t out[CMT_WORD_LENGTH]);
int cmt_abi_encode_uint_nr(size_t n, const uint8_t *data, uint8_t out[CMT_ABI_U256_LENGTH]);

/** Encode @p n bytes of @p data into @p out (up to 32) in normal order.
*
Expand All @@ -552,7 +552,7 @@ int cmt_abi_encode_uint_nr(size_t n, const uint8_t *data, uint8_t out[CMT_WORD_L
* | -EDOM| integer not representable in @p data_length bytes |
*
* @note use @ref cmt_abi_encode_uint instead */
int cmt_abi_encode_uint_nn(size_t n, const uint8_t *data, uint8_t out[CMT_WORD_LENGTH]);
int cmt_abi_encode_uint_nn(size_t n, const uint8_t *data, uint8_t out[CMT_ABI_U256_LENGTH]);

/** Decode @p n bytes of @p data into @p out (up to 32).
*
Expand All @@ -565,7 +565,7 @@ int cmt_abi_encode_uint_nn(size_t n, const uint8_t *data, uint8_t out[CMT_WORD_L
* |-------:|---------------------------------------------------|
* | 0| success |
* | -EDOM| integer not representable in @p data_length bytes | */
int cmt_abi_decode_uint(const uint8_t data[CMT_WORD_LENGTH], size_t n, uint8_t *out);
int cmt_abi_decode_uint(const uint8_t data[CMT_ABI_U256_LENGTH], size_t n, uint8_t *out);

/** Decode @p n bytes of @p data into @p out (up to 32) in reverse order.
*
Expand All @@ -580,7 +580,7 @@ int cmt_abi_decode_uint(const uint8_t data[CMT_WORD_LENGTH], size_t n, uint8_t *
* | -EDOM| integer not representable in @p data_length bytes |
*
* @note if in doubt, use @ref cmt_abi_decode_uint */
int cmt_abi_decode_uint_nr(const uint8_t data[CMT_WORD_LENGTH], size_t n, uint8_t *out);
int cmt_abi_decode_uint_nr(const uint8_t data[CMT_ABI_U256_LENGTH], size_t n, uint8_t *out);

/** Decode @p n bytes of @p data into @p out (up to 32) in normal order.
*
Expand All @@ -595,7 +595,7 @@ int cmt_abi_decode_uint_nr(const uint8_t data[CMT_WORD_LENGTH], size_t n, uint8_
* | -EDOM| integer not representable in @p data_length bytes |
*
* @note if in doubt, use @ref cmt_abi_decode_uint */
int cmt_abi_decode_uint_nn(const uint8_t data[CMT_WORD_LENGTH], size_t n, uint8_t *out);
int cmt_abi_decode_uint_nn(const uint8_t data[CMT_ABI_U256_LENGTH], size_t n, uint8_t *out);

#endif /* CMT_ABI_H */
/** @} */
74 changes: 37 additions & 37 deletions sys-utils/libcmt/src/abi.c
Original file line number Diff line number Diff line change
Expand Up @@ -45,71 +45,71 @@ int cmt_abi_put_funsel(cmt_buf_t *me, uint32_t funsel) {
return 0;
}

int cmt_abi_encode_uint_nr(size_t n, const uint8_t *data, uint8_t out[CMT_WORD_LENGTH]) {
if (n > CMT_WORD_LENGTH) {
int cmt_abi_encode_uint_nr(size_t n, const uint8_t *data, uint8_t out[CMT_ABI_U256_LENGTH]) {
if (n > CMT_ABI_U256_LENGTH) {
return -EDOM;
}
for (size_t i = 0; i < n; ++i) {
out[CMT_WORD_LENGTH - 1 - i] = data[i];
out[CMT_ABI_U256_LENGTH - 1 - i] = data[i];
}
for (size_t i = n; i < CMT_WORD_LENGTH; ++i) {
out[CMT_WORD_LENGTH - 1 - i] = 0;
for (size_t i = n; i < CMT_ABI_U256_LENGTH; ++i) {
out[CMT_ABI_U256_LENGTH - 1 - i] = 0;
}
return 0;
}

int cmt_abi_encode_uint_nn(size_t n, const uint8_t *data, uint8_t out[CMT_WORD_LENGTH]) {
if (n > CMT_WORD_LENGTH) {
int cmt_abi_encode_uint_nn(size_t n, const uint8_t *data, uint8_t out[CMT_ABI_U256_LENGTH]) {
if (n > CMT_ABI_U256_LENGTH) {
return -EDOM;
}
for (size_t i = 0; i < CMT_WORD_LENGTH - n; ++i) {
for (size_t i = 0; i < CMT_ABI_U256_LENGTH - n; ++i) {
out[i] = 0;
}
for (size_t i = CMT_WORD_LENGTH - n; i < CMT_WORD_LENGTH; ++i) {
out[i] = data[i - CMT_WORD_LENGTH + n];
for (size_t i = CMT_ABI_U256_LENGTH - n; i < CMT_ABI_U256_LENGTH; ++i) {
out[i] = data[i - CMT_ABI_U256_LENGTH + n];
}
return 0;
}

int cmt_abi_encode_uint(size_t n, const void *data, uint8_t out[CMT_WORD_LENGTH]) {
int cmt_abi_encode_uint(size_t n, const void *data, uint8_t out[CMT_ABI_U256_LENGTH]) {
#if __BYTE_ORDER__ != __ORDER_LITTLE_ENDIAN__
return cmt_abi_encode_uint_nn(n, data, out);
#else
return cmt_abi_encode_uint_nr(n, data, out);
#endif
}

int cmt_abi_decode_uint_nr(const uint8_t data[CMT_WORD_LENGTH], size_t n, uint8_t *out) {
if (n > CMT_WORD_LENGTH) {
int cmt_abi_decode_uint_nr(const uint8_t data[CMT_ABI_U256_LENGTH], size_t n, uint8_t *out) {
if (n > CMT_ABI_U256_LENGTH) {
return -EDOM;
}
for (size_t i = 0; i < CMT_WORD_LENGTH - n; ++i) {
for (size_t i = 0; i < CMT_ABI_U256_LENGTH - n; ++i) {
if (data[i]) {
return -EDOM;
}
}
for (size_t i = CMT_WORD_LENGTH - n; i < CMT_WORD_LENGTH; ++i) {
out[CMT_WORD_LENGTH - 1 - i] = data[i];
for (size_t i = CMT_ABI_U256_LENGTH - n; i < CMT_ABI_U256_LENGTH; ++i) {
out[CMT_ABI_U256_LENGTH - 1 - i] = data[i];
}
return 0;
}

int cmt_abi_decode_uint_nn(const uint8_t data[CMT_WORD_LENGTH], size_t n, uint8_t *out) {
if (n > CMT_WORD_LENGTH) {
int cmt_abi_decode_uint_nn(const uint8_t data[CMT_ABI_U256_LENGTH], size_t n, uint8_t *out) {
if (n > CMT_ABI_U256_LENGTH) {
return -EDOM;
}
for (size_t i = 0; i < CMT_WORD_LENGTH - n; ++i) {
for (size_t i = 0; i < CMT_ABI_U256_LENGTH - n; ++i) {
if (data[i]) {
return -EDOM;
}
}
for (size_t i = CMT_WORD_LENGTH - n; i < CMT_WORD_LENGTH; ++i) {
out[i - CMT_WORD_LENGTH + n] = data[i];
for (size_t i = CMT_ABI_U256_LENGTH - n; i < CMT_ABI_U256_LENGTH; ++i) {
out[i - CMT_ABI_U256_LENGTH + n] = data[i];
}
return 0;
}

int cmt_abi_decode_uint(const uint8_t data[CMT_WORD_LENGTH], size_t n, uint8_t *out) {
int cmt_abi_decode_uint(const uint8_t data[CMT_ABI_U256_LENGTH], size_t n, uint8_t *out) {
#if __BYTE_ORDER__ != __ORDER_LITTLE_ENDIAN__
return cmt_abi_decode_uint_nn(data, n, out);
#else
Expand All @@ -119,28 +119,28 @@ int cmt_abi_decode_uint(const uint8_t data[CMT_WORD_LENGTH], size_t n, uint8_t *

int cmt_abi_put_uint(cmt_buf_t *me, size_t data_length, const void *data) {
cmt_buf_t x[1];
if (data_length > CMT_WORD_LENGTH) {
if (data_length > CMT_ABI_U256_LENGTH) {
return -EDOM;
}
if (cmt_buf_split(me, CMT_WORD_LENGTH, x, me)) {
if (cmt_buf_split(me, CMT_ABI_U256_LENGTH, x, me)) {
return -ENOBUFS;
}
return cmt_abi_encode_uint(data_length, data, x->begin);
}

int cmt_abi_put_uint_be(cmt_buf_t *me, size_t data_length, const void *data) {
cmt_buf_t x[1];
if (data_length > CMT_WORD_LENGTH) {
if (data_length > CMT_ABI_U256_LENGTH) {
return -EDOM;
}
if (cmt_buf_split(me, CMT_WORD_LENGTH, x, me)) {
if (cmt_buf_split(me, CMT_ABI_U256_LENGTH, x, me)) {
return -ENOBUFS;
}
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)) {
if (cmt_buf_split(me, CMT_ABI_U256_LENGTH, x, me)) {
return -ENOBUFS;
}
return cmt_abi_encode_uint_nn(sizeof(*value), value->data, x->begin);
Expand All @@ -153,23 +153,23 @@ int cmt_abi_put_bool(cmt_buf_t *me, bool value) {

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)) {
if (cmt_buf_split(me, CMT_ABI_U256_LENGTH, x, me)) {
return -ENOBUFS;
}
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) {
return cmt_buf_split(me, CMT_WORD_LENGTH, offset, me);
return cmt_buf_split(me, CMT_ABI_U256_LENGTH, offset, me);
}

int cmt_abi_reserve_bytes_d(cmt_buf_t *me, cmt_buf_t *of, size_t n, cmt_buf_t *out, const void *start) {
int rc = 0;
cmt_buf_t tmp[1];
cmt_buf_t sz[1];
size_t n32 = align_forward(n, CMT_WORD_LENGTH);
size_t n32 = align_forward(n, CMT_ABI_U256_LENGTH);

rc = cmt_buf_split(me, CMT_WORD_LENGTH, sz, tmp);
rc = cmt_buf_split(me, CMT_ABI_U256_LENGTH, sz, tmp);
if (rc) {
return rc;
}
Expand Down Expand Up @@ -228,10 +228,10 @@ int cmt_abi_check_funsel(cmt_buf_t *me, uint32_t expected) {
int cmt_abi_get_uint(cmt_buf_t *me, size_t n, void *data) {
cmt_buf_t x[1];

if (n > CMT_WORD_LENGTH) {
if (n > CMT_ABI_U256_LENGTH) {
return -EDOM;
}
int rc = cmt_buf_split(me, CMT_WORD_LENGTH, x, me);
int rc = cmt_buf_split(me, CMT_ABI_U256_LENGTH, x, me);
if (rc) {
return rc;
}
Expand All @@ -242,10 +242,10 @@ int cmt_abi_get_uint(cmt_buf_t *me, size_t n, void *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) {
if (n > CMT_ABI_U256_LENGTH) {
return -EDOM;
}
int rc = cmt_buf_split(me, CMT_WORD_LENGTH, x, me);
int rc = cmt_buf_split(me, CMT_ABI_U256_LENGTH, x, me);
if (rc) {
return rc;
}
Expand All @@ -266,15 +266,15 @@ int cmt_abi_get_bool(cmt_buf_t *me, bool *value) {
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);
int rc = cmt_buf_split(me, CMT_ABI_U256_LENGTH, x, me);
if (rc) {
return rc;
}
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]) {
return cmt_buf_split(me, CMT_WORD_LENGTH, of, me);
return cmt_buf_split(me, CMT_ABI_U256_LENGTH, of, me);
}

int cmt_abi_peek_bytes_d(const cmt_buf_t *start, cmt_buf_t of[1], cmt_buf_t *bytes) {
Expand Down
2 changes: 1 addition & 1 deletion sys-utils/libcmt/src/rollup.c
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,7 @@ int cmt_rollup_finish(cmt_rollup_t *me, cmt_rollup_finish_t *finish) {
}

cmt_merkle_get_root_hash(me->merkle, cmt_io_get_tx(me->io).begin);
me->fromhost_data = CMT_WORD_LENGTH;
me->fromhost_data = CMT_ABI_U256_LENGTH;
int reason = accepted(me->io, &me->fromhost_data);
if (reason < 0) {
return reason;
Expand Down
Loading

0 comments on commit ce96c95

Please sign in to comment.