Skip to content

Commit

Permalink
chore: format source code
Browse files Browse the repository at this point in the history
  • Loading branch information
vfusco committed Mar 22, 2024
1 parent 666cc62 commit e09a253
Show file tree
Hide file tree
Showing 14 changed files with 813 additions and 164 deletions.
1 change: 0 additions & 1 deletion sys-utils/libcmt/src/abi.h
Original file line number Diff line number Diff line change
Expand Up @@ -501,4 +501,3 @@ int cmt_abi_decode_uint_nn(const uint8_t data[CMT_WORD_LENGTH], size_t n, uint8_

#endif /* CMT_ABI_H */
/** @} */

1 change: 0 additions & 1 deletion sys-utils/libcmt/src/buf.h
Original file line number Diff line number Diff line change
Expand Up @@ -80,4 +80,3 @@ bool cmt_buf_split_by_comma(cmt_buf_t *x, cmt_buf_t *xs);

#endif /* CMT_BUF_H */
/** @} */

13 changes: 6 additions & 7 deletions sys-utils/libcmt/src/io-mock.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
#include <stdlib.h>
#include <string.h>


static int read_whole_file(const char *name, size_t max, void *data, size_t *length);
static int write_whole_file(const char *name, size_t length, const void *data);

Expand Down Expand Up @@ -116,8 +115,8 @@ static int store_output(cmt_io_driver_mock_t *me, const char *filepath, struct c
if (rr->data > cmt_buf_length(me->tx))
return -ENOBUFS;

//char filepath[128 + 1 + 8 + 16];
//snprintf(filepath, sizeof filepath, "%s.%s%d%s", me->input_filename, ns, *seq, me->input_fileext);
// char filepath[128 + 1 + 8 + 16];
// snprintf(filepath, sizeof filepath, "%s.%s%d%s", me->input_filename, ns, *seq, me->input_fileext);

int rc = write_whole_file(filepath, rr->data, me->tx->begin);
if (rc) {
Expand All @@ -128,7 +127,7 @@ static int store_output(cmt_io_driver_mock_t *me, const char *filepath, struct c
fprintf(stderr, "wrote filename: \"%s\" (%u)\n", filepath, rr->data);
}

//seq[0] += 1;
// seq[0] += 1;
return 0;
}

Expand All @@ -155,10 +154,10 @@ static int mock_rx_accepted(cmt_io_driver_mock_t *me, struct cmt_io_yield *rr) {
}
if (me->input_seq++) { // skip the first
char filepath[128 + 32 + 8 + 16];
snprintf(filepath, sizeof filepath,
"%s.outputs_root_hash%s", me->input_filename, me->input_fileext);
snprintf(filepath, sizeof filepath, "%s.outputs_root_hash%s", me->input_filename, me->input_fileext);
int rc = store_output(me, filepath, rr);
if (rc) return rc;
if (rc)
return rc;
}
if (load_next_input(me, rr))
return -ENODATA;
Expand Down
3 changes: 1 addition & 2 deletions sys-utils/libcmt/src/io.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@

#include <linux/cartesi/cmio.h>


int cmt_io_init(cmt_io_driver_t *_me) {
int rc = 0;

Expand Down Expand Up @@ -95,7 +94,7 @@ cmt_buf_t cmt_io_get_tx(cmt_io_driver_t *me) {
}

static uint32_t min(uint32_t a, uint32_t b) {
return a < b? a: b;
return a < b ? a : b;
}

cmt_buf_t cmt_io_get_rx(cmt_io_driver_t *me) {
Expand Down
13 changes: 6 additions & 7 deletions sys-utils/libcmt/src/io.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,16 +70,16 @@ enum {

/** Automatic reasons */
enum {
HTIF_YIELD_AUTOMATIC_REASON_PROGRESS = 1, /**< Progress */
HTIF_YIELD_AUTOMATIC_REASON_TX_OUTPUT = 2, /**< emit an output */
HTIF_YIELD_AUTOMATIC_REASON_TX_REPORT = 4, /**< emit a report */
HTIF_YIELD_AUTOMATIC_REASON_PROGRESS = 1, /**< Progress */
HTIF_YIELD_AUTOMATIC_REASON_TX_OUTPUT = 2, /**< emit an output */
HTIF_YIELD_AUTOMATIC_REASON_TX_REPORT = 4, /**< emit a report */
};

/** Manual reasons */
enum {
HTIF_YIELD_MANUAL_REASON_RX_ACCEPTED = 1, /**< Accept and load next input */
HTIF_YIELD_MANUAL_REASON_RX_REJECTED = 2, /**< Reject and revert */
HTIF_YIELD_MANUAL_REASON_TX_EXCEPTION = 4, /**< emit a exception and halt execution */
HTIF_YIELD_MANUAL_REASON_RX_ACCEPTED = 1, /**< Accept and load next input */
HTIF_YIELD_MANUAL_REASON_RX_REJECTED = 2, /**< Reject and revert */
HTIF_YIELD_MANUAL_REASON_TX_EXCEPTION = 4, /**< emit a exception and halt execution */
};

/** Reply reason when requesting @ref HTIF_YIELD_REASON_RX_ACCEPTED or HTIF_YIELD_REASON_RX_REJECTED */
Expand Down Expand Up @@ -165,4 +165,3 @@ cmt_buf_t cmt_io_get_rx(cmt_io_driver_t *me);
int cmt_io_yield(cmt_io_driver_t *me, cmt_io_yield_t *rr);

#endif /* CMT_IO_H */

2 changes: 1 addition & 1 deletion sys-utils/libcmt/src/keccak.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "abi.h"
#include "keccak.h"
#include "abi.h"

#include <string.h>

Expand Down
1 change: 0 additions & 1 deletion sys-utils/libcmt/src/keccak.h
Original file line number Diff line number Diff line change
Expand Up @@ -129,4 +129,3 @@ uint8_t *cmt_keccak_data(size_t length, const void *data, uint8_t md[CMT_KECCAK_
uint32_t cmt_keccak_funsel(const char *decl);
#endif /* CMT_KECCAK_H */
/** $@} */

6 changes: 4 additions & 2 deletions sys-utils/libcmt/src/merkle.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include <string.h>
#include <strings.h>

// clang-format off
static const uint8_t pristine_hash[CMT_MERKLE_TREE_HEIGHT+1][CMT_KECCAK_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,0x00},
{0xad,0x32,0x28,0xb6,0x76,0xf7,0xd3,0xcd,0x42,0x84,0xa5,0x44,0x3f,0x17,0xf1,0x96,0x2b,0x36,0xe4,0x91,0xb3,0x0a,0x40,0xb2,0x40,0x58,0x49,0xe5,0x97,0xba,0x5f,0xb5},
Expand Down Expand Up @@ -88,6 +89,7 @@ static const uint8_t pristine_hash[CMT_MERKLE_TREE_HEIGHT+1][CMT_KECCAK_LENGTH]
{0x0a,0x16,0x29,0x46,0xe5,0x61,0x58,0xba,0xc0,0x67,0x3e,0x6d,0xd3,0xbd,0xfd,0xc1,0xe4,0xa0,0xe7,0x74,0x4a,0x12,0x0f,0xdb,0x64,0x00,0x50,0xc8,0xd7,0xab,0xe1,0xc6},
{0xe2,0xc3,0xed,0x40,0x52,0xee,0xb1,0xd6,0x05,0x14,0xb4,0xc3,0x8e,0xce,0x8d,0x73,0xa2,0x7f,0x37,0xfa,0x5b,0x36,0xdc,0xbf,0x33,0x8e,0x70,0xde,0x95,0x79,0x8c,0xaa}
};
// clang-format on

static void copy_hash(const uint8_t src[CMT_KECCAK_LENGTH], uint8_t out[CMT_KECCAK_LENGTH]) {
memcpy(out, src, CMT_KECCAK_LENGTH);
Expand Down Expand Up @@ -173,7 +175,7 @@ int cmt_merkle_push_back(cmt_merkle_t *me, const uint8_t hash[CMT_KECCAK_LENGTH]
if (me->leaf_count & bit) {
/* ...concat it with current running right and replace running right with it. */
concat_hash(me->state[i], right, right);
/* otherwise... */
/* otherwise... */
} else {
/* ...just copy the current subtree hash to the state and we are done. */
copy_hash(right, me->state[i]);
Expand All @@ -192,7 +194,7 @@ void cmt_merkle_get_root_hash(cmt_merkle_t *me, uint8_t root[CMT_KECCAK_LENGTH])
if (me->leaf_count & bit) {
/* ...concat it with current root on the right and replace root */
concat_hash(me->state[i], root, root);
/* otherwise... */
/* otherwise... */
} else {
/* ...concat root with pristine tree on the right and replace root */
concat_hash(root, pristine_hash[i], root);
Expand Down
3 changes: 1 addition & 2 deletions sys-utils/libcmt/src/merkle.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ enum {
/** Opaque Merkle tree state.
* initialize with: @ref cmt_merkle_init */
typedef struct {
uint64_t leaf_count; /**< number of leaves in tree */
uint64_t leaf_count; /**< number of leaves in tree */
uint8_t state[CMT_MERKLE_TREE_HEIGHT][CMT_KECCAK_LENGTH]; /**< hashes of complete subtrees */
} cmt_merkle_t;

Expand Down Expand Up @@ -100,4 +100,3 @@ int cmt_merkle_push_back_data(cmt_merkle_t *me, size_t length, const void *data)
void cmt_merkle_get_root_hash(cmt_merkle_t *me, uint8_t root[CMT_KECCAK_LENGTH]);

#endif /* CMT_MERKLE_H */

22 changes: 8 additions & 14 deletions sys-utils/libcmt/src/rollup.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
#include <string.h>

// Voucher(address,uint256,bytes)
#define VOUCHER CMT_ABI_FUNSEL(0x23, 0x7a, 0x81, 0x6f)
#define VOUCHER CMT_ABI_FUNSEL(0x23, 0x7a, 0x81, 0x6f)

// Notice(bytes)
#define NOTICE CMT_ABI_FUNSEL(0xc2, 0x58, 0xd6, 0xe5)
Expand Down Expand Up @@ -72,10 +72,8 @@ 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, 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) {
if (!me)
return -EINVAL;
if (!data && length)
Expand All @@ -86,11 +84,9 @@ int cmt_rollup_emit_voucher(cmt_rollup_t *me,
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_uint_be(wr, address_length, address_data))
|| 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)))
if (DBG(cmt_abi_put_funsel(wr, VOUCHER)) || DBG(cmt_abi_put_uint_be(wr, address_length, address_data)) ||
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;
Expand Down Expand Up @@ -209,12 +205,10 @@ int cmt_rollup_read_advance_state(cmt_rollup_t *me, cmt_rollup_advance_t *advanc
size_t payload_length = 0;
if (DBG(cmt_abi_check_funsel(rd, EVM_ADVANCE)) ||
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->index), &advance->index)) ||
DBG(cmt_abi_get_bytes_s(rd, of)) ||
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(st, of, &payload_length, &advance->payload)))
return -ENOBUFS;
advance->payload_length = payload_length;
Expand Down
29 changes: 11 additions & 18 deletions sys-utils/libcmt/src/rollup.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,12 @@ typedef struct cmt_rollup_finish {

/** Public struct with generic io request/response */
typedef struct cmt_gio {
uint16_t domain; /**< domain for the gio request */
uint32_t id_length; /**< length of id */
void *id; /**< id for the request */
uint16_t response_code; /**< response */
uint32_t response_length; /**< length of response data */
void *response; /**< response data */
uint16_t domain; /**< domain for the gio request */
uint32_t id_length; /**< length of id */
void *id; /**< id for the request */
uint16_t response_code; /**< response */
uint32_t response_length; /**< length of response data */
void *response; /**< response data */
} cmt_gio_request_t;

/** Initialize a @ref cmt_rollup_t state.
Expand Down Expand Up @@ -113,10 +113,8 @@ 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, 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);

/** Emit a notice
*
Expand All @@ -130,8 +128,7 @@ int cmt_rollup_emit_voucher(cmt_rollup_t *me,
* |--:|-----------------------------|
* | 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, uint32_t data_length, const void *data, uint64_t *index);

/** Emit a report
* @param [in,out] me initialized cmt_rollup_t instance
Expand All @@ -143,8 +140,7 @@ int cmt_rollup_emit_notice(cmt_rollup_t *me,
* |--:|-----------------------------|
* | 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, uint32_t data_length, const void *data);

/** Emit a exception
* @param [in,out] me initialized cmt_rollup_t instance
Expand All @@ -156,8 +152,7 @@ int cmt_rollup_emit_report(cmt_rollup_t *me,
* |--:|-----------------------------|
* | 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, uint32_t data_length, const void *data);

/** Read advance state
*
Expand Down Expand Up @@ -196,7 +191,6 @@ int cmt_rollup_read_inspect_state(cmt_rollup_t *me, cmt_rollup_inspect_t *inspec
* |< 0| failure with a -errno value | */
int cmt_rollup_finish(cmt_rollup_t *me, cmt_rollup_finish_t *finish);


/** Performs a generic IO request
*
* @param [in,out] me initialized cmt_rollup_t instance
Expand Down Expand Up @@ -238,4 +232,3 @@ int cmt_rollup_save_merkle(cmt_rollup_t *me, const char *path);
int cmt_rollup_reset_merkle(cmt_rollup_t *me);

#endif /* CMT_ROLLUP_H */

Loading

0 comments on commit e09a253

Please sign in to comment.