Skip to content

Commit

Permalink
refactor!: return cm_error enumeration in C API functions
Browse files Browse the repository at this point in the history
  • Loading branch information
edubart committed Oct 4, 2024
1 parent 14ee258 commit 9c3216a
Show file tree
Hide file tree
Showing 6 changed files with 218 additions and 218 deletions.
26 changes: 13 additions & 13 deletions src/jsonrpc-machine-c-api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ static const cartesi::jsonrpc_mgr_ptr *convert_from_c(const cm_jsonrpc_mgr *mgr)
return reinterpret_cast<const cartesi::jsonrpc_mgr_ptr *>(mgr);
}

int32_t cm_jsonrpc_create_mgr(const char *remote_address, cm_jsonrpc_mgr **mgr) try {
cm_error cm_jsonrpc_create_mgr(const char *remote_address, cm_jsonrpc_mgr **mgr) try {
if (mgr == nullptr) {
throw std::invalid_argument("invalid stub output");
}
Expand All @@ -52,7 +52,7 @@ void cm_jsonrpc_destroy_mgr(const cm_jsonrpc_mgr *mgr) {
delete mgr_wrapper;
}

int32_t cm_jsonrpc_create_machine(const cm_jsonrpc_mgr *mgr, const char *config, const char *runtime_config,
cm_error cm_jsonrpc_create_machine(const cm_jsonrpc_mgr *mgr, const char *config, const char *runtime_config,
cm_machine **new_machine) try {
if (new_machine == nullptr) {
throw std::invalid_argument("invalid new machine output");
Expand All @@ -70,7 +70,7 @@ int32_t cm_jsonrpc_create_machine(const cm_jsonrpc_mgr *mgr, const char *config,
return cm_result_failure();
}

int32_t cm_jsonrpc_load_machine(const cm_jsonrpc_mgr *mgr, const char *dir, const char *runtime_config,
cm_error cm_jsonrpc_load_machine(const cm_jsonrpc_mgr *mgr, const char *dir, const char *runtime_config,
cm_machine **new_machine) try {
if (new_machine == nullptr) {
throw std::invalid_argument("invalid new machine output");
Expand All @@ -90,7 +90,7 @@ int32_t cm_jsonrpc_load_machine(const cm_jsonrpc_mgr *mgr, const char *dir, cons
return cm_result_failure();
}

int32_t cm_jsonrpc_get_machine(const cm_jsonrpc_mgr *mgr, cm_machine **new_machine) try {
cm_error cm_jsonrpc_get_machine(const cm_jsonrpc_mgr *mgr, cm_machine **new_machine) try {
if (new_machine == nullptr) {
throw std::invalid_argument("invalid new machine output");
}
Expand All @@ -102,7 +102,7 @@ int32_t cm_jsonrpc_get_machine(const cm_jsonrpc_mgr *mgr, cm_machine **new_machi
return cm_result_failure();
}

int32_t cm_jsonrpc_get_default_config(const cm_jsonrpc_mgr *mgr, const char **config) try {
cm_error cm_jsonrpc_get_default_config(const cm_jsonrpc_mgr *mgr, const char **config) try {
if (config == nullptr) {
throw std::invalid_argument("invalid config output");
}
Expand All @@ -116,7 +116,7 @@ int32_t cm_jsonrpc_get_default_config(const cm_jsonrpc_mgr *mgr, const char **co
return cm_result_failure();
}

int32_t cm_jsonrpc_verify_step_uarch(const cm_jsonrpc_mgr *mgr, const cm_hash *root_hash_before, const char *log,
cm_error cm_jsonrpc_verify_step_uarch(const cm_jsonrpc_mgr *mgr, const cm_hash *root_hash_before, const char *log,
const cm_hash *root_hash_after) try {
if (log == nullptr) {
throw std::invalid_argument("invalid access log");
Expand All @@ -137,7 +137,7 @@ int32_t cm_jsonrpc_verify_step_uarch(const cm_jsonrpc_mgr *mgr, const cm_hash *r
return cm_result_failure();
}

int32_t cm_jsonrpc_verify_reset_uarch(const cm_jsonrpc_mgr *mgr, const cm_hash *root_hash_before, const char *log,
cm_error cm_jsonrpc_verify_reset_uarch(const cm_jsonrpc_mgr *mgr, const cm_hash *root_hash_before, const char *log,
const cm_hash *root_hash_after) try {
if (log == nullptr) {
throw std::invalid_argument("invalid access log");
Expand All @@ -159,7 +159,7 @@ int32_t cm_jsonrpc_verify_reset_uarch(const cm_jsonrpc_mgr *mgr, const cm_hash *
return cm_result_failure();
}

int32_t cm_jsonrpc_fork(const cm_jsonrpc_mgr *mgr, const char **address, int32_t *pid) try {
cm_error cm_jsonrpc_fork(const cm_jsonrpc_mgr *mgr, const char **address, int32_t *pid) try {
if (address == nullptr) {
throw std::invalid_argument("invalid address output");
}
Expand All @@ -182,7 +182,7 @@ int32_t cm_jsonrpc_fork(const cm_jsonrpc_mgr *mgr, const char **address, int32_t
return cm_result_failure();
}

int32_t cm_jsonrpc_rebind(const cm_jsonrpc_mgr *mgr, const char *address, const char **new_address) try {
cm_error cm_jsonrpc_rebind(const cm_jsonrpc_mgr *mgr, const char *address, const char **new_address) try {
const auto *cpp_mgr = convert_from_c(mgr);
const std::string cpp_new_address = cartesi::jsonrpc_virtual_machine::rebind(*cpp_mgr, address);
if (new_address) {
Expand All @@ -198,7 +198,7 @@ int32_t cm_jsonrpc_rebind(const cm_jsonrpc_mgr *mgr, const char *address, const
return cm_result_failure();
}

int32_t cm_jsonrpc_get_reg_address(const cm_jsonrpc_mgr *mgr, cm_reg reg, uint64_t *val) try {
cm_error cm_jsonrpc_get_reg_address(const cm_jsonrpc_mgr *mgr, cm_reg reg, uint64_t *val) try {
if (val == nullptr) {
throw std::invalid_argument("invalid val output");
}
Expand All @@ -210,7 +210,7 @@ int32_t cm_jsonrpc_get_reg_address(const cm_jsonrpc_mgr *mgr, cm_reg reg, uint64
return cm_result_failure();
}

int32_t cm_jsonrpc_get_version(const cm_jsonrpc_mgr *mgr, const char **version) try {
cm_error cm_jsonrpc_get_version(const cm_jsonrpc_mgr *mgr, const char **version) try {
if (version == nullptr) {
throw std::invalid_argument("invalid version output");
}
Expand All @@ -224,15 +224,15 @@ int32_t cm_jsonrpc_get_version(const cm_jsonrpc_mgr *mgr, const char **version)
return cm_result_failure();
}

int32_t cm_jsonrpc_shutdown(const cm_jsonrpc_mgr *mgr) try {
cm_error cm_jsonrpc_shutdown(const cm_jsonrpc_mgr *mgr) try {
const auto *cpp_mgr = convert_from_c(mgr);
cartesi::jsonrpc_virtual_machine::shutdown(*cpp_mgr);
return cm_result_success();
} catch (...) {
return cm_result_failure();
}

int32_t cm_jsonrpc_verify_send_cmio_response(const cm_jsonrpc_mgr *mgr, uint16_t reason, const uint8_t *data,
cm_error cm_jsonrpc_verify_send_cmio_response(const cm_jsonrpc_mgr *mgr, uint16_t reason, const uint8_t *data,
uint64_t length, const cm_hash *root_hash_before, const char *log, const cm_hash *root_hash_after) try {
if (log == nullptr) {
throw std::invalid_argument("invalid access log");
Expand Down
28 changes: 14 additions & 14 deletions src/jsonrpc-machine-c-api.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ typedef struct cm_jsonrpc_mgr cm_jsonrpc_mgr;
/// \param remote_address Address of the remote machine server to connect to.
/// \param mgr Receives new JSONRPC connection manager instance.
/// \returns 0 for success, non zero code for error.
CM_API int32_t cm_jsonrpc_create_mgr(const char *remote_address, cm_jsonrpc_mgr **mgr);
CM_API cm_error cm_jsonrpc_create_mgr(const char *remote_address, cm_jsonrpc_mgr **mgr);

/// \brief Destroys a connection manager instance.
/// \param mgr Pointer a JSONRPC connection manager (can be NULL).
Expand All @@ -48,41 +48,41 @@ CM_API void cm_jsonrpc_destroy_mgr(const cm_jsonrpc_mgr *mgr);
/// remains valid until the next time this same function is called on the same thread.
/// \param pid Receives the forked child process id if function execution succeeds or 0 otherwise.
/// \returns 0 for success, non zero code for error.
CM_API int32_t cm_jsonrpc_fork(const cm_jsonrpc_mgr *mgr, const char **address, int32_t *pid);
CM_API cm_error cm_jsonrpc_fork(const cm_jsonrpc_mgr *mgr, const char **address, int32_t *pid);

/// \brief Changes the address the remote server is listening to.
/// \param mgr Pointer to a valid JSONRPC connection manager.
/// \param address New address that the remote server should bind to.
/// \param new_address Receives the new address that the remote server actually bound to,
/// remains valid until the next time this same function is called on the same thread.
/// \returns 0 for success, non zero code for error.
CM_API int32_t cm_jsonrpc_rebind(const cm_jsonrpc_mgr *mgr, const char *address, const char **new_address);
CM_API cm_error cm_jsonrpc_rebind(const cm_jsonrpc_mgr *mgr, const char *address, const char **new_address);

/// \brief Shutdowns remote server.
/// \param mgr Pointer to a valid JSONRPC connection manager.
/// \returns 0 for success, non zero code for error.
CM_API int32_t cm_jsonrpc_shutdown(const cm_jsonrpc_mgr *mgr);
CM_API cm_error cm_jsonrpc_shutdown(const cm_jsonrpc_mgr *mgr);

/// \brief Gets the semantic version of the remote server.
/// \param mgr Pointer to a valid JSONRPC connection manager.
/// \param semantic_version Receives the semantic version as a JSON string,
/// remains valid until the next time this same function is called on the same thread.
/// \returns 0 for success, non zero code for error.
CM_API int32_t cm_jsonrpc_get_version(const cm_jsonrpc_mgr *mgr, const char **version);
CM_API cm_error cm_jsonrpc_get_version(const cm_jsonrpc_mgr *mgr, const char **version);

/// \brief Gets a JSON string for the default machine config from the remote server.
/// \param mgr Pointer to a valid JSONRPC connection manager.
/// \param config Receives the default configuration,
/// remains valid until the next time this same function is called on the same thread.
/// \returns 0 for success, non zero code for error.
CM_API int32_t cm_jsonrpc_get_default_config(const cm_jsonrpc_mgr *mgr, const char **config);
CM_API cm_error cm_jsonrpc_get_default_config(const cm_jsonrpc_mgr *mgr, const char **config);

/// \brief Gets the address of any register from the remote server.
/// \param mgr Pointer to a valid JSONRPC connection manager.
/// \param reg The register.
/// \param val Receives address of the register.
/// \returns 0 for success, non zero code for error.
CM_API int32_t cm_jsonrpc_get_reg_address(const cm_jsonrpc_mgr *mgr, cm_reg reg, uint64_t *val);
CM_API cm_error cm_jsonrpc_get_reg_address(const cm_jsonrpc_mgr *mgr, cm_reg reg, uint64_t *val);

// -------------------------------------
// Machine API functions
Expand All @@ -93,7 +93,7 @@ CM_API int32_t cm_jsonrpc_get_reg_address(const cm_jsonrpc_mgr *mgr, cm_reg reg,
/// \param runtime_config Machine runtime configuration as a JSON string, it can be NULL.
/// \param new_machine Receives the pointer to new remote machine instance.
/// \returns 0 for success, non zero code for error.
CM_API int32_t cm_jsonrpc_create_machine(const cm_jsonrpc_mgr *mgr, const char *config, const char *runtime_config,
CM_API cm_error cm_jsonrpc_create_machine(const cm_jsonrpc_mgr *mgr, const char *config, const char *runtime_config,
cm_machine **new_machine);

/// \brief Creates a remote machine instance from previously stored directory in the remote server.
Expand All @@ -102,14 +102,14 @@ CM_API int32_t cm_jsonrpc_create_machine(const cm_jsonrpc_mgr *mgr, const char *
/// \param runtime_config Machine runtime configuration as a JSON string, it can be NULL.
/// \param new_machine Receives the pointer to new remote machine instance.
/// \returns 0 for success, non zero code for error.
CM_API int32_t cm_jsonrpc_load_machine(const cm_jsonrpc_mgr *mgr, const char *dir, const char *runtime_config,
CM_API cm_error cm_jsonrpc_load_machine(const cm_jsonrpc_mgr *mgr, const char *dir, const char *runtime_config,
cm_machine **new_machine);

/// \brief Get remote machine instance that was previously created in the remote server.
/// \param mgr Pointer to a valid JSONRPC connection manager.
/// \param new_machine Receives the pointer to remote machine instance.
/// \returns 0 for success, non zero code for error.
CM_API int32_t cm_jsonrpc_get_machine(const cm_jsonrpc_mgr *mgr, cm_machine **new_machine);
CM_API cm_error cm_jsonrpc_get_machine(const cm_jsonrpc_mgr *mgr, cm_machine **new_machine);

// -------------------------------------
// Verifying
Expand All @@ -122,8 +122,8 @@ CM_API int32_t cm_jsonrpc_get_machine(const cm_jsonrpc_mgr *mgr, cm_machine **ne
/// \returns 0 for success, non zero code for error.
/// \details In case both root_hash_before and root_hash_after are NULL,
/// then it just verifies the access log integrity.
CM_API int32_t cm_jsonrpc_verify_step_uarch(const cm_jsonrpc_mgr *mgr, const cm_hash *root_hash_before, const char *log,
const cm_hash *root_hash_after);
CM_API cm_error cm_jsonrpc_verify_step_uarch(const cm_jsonrpc_mgr *mgr, const cm_hash *root_hash_before,
const char *log, const cm_hash *root_hash_after);

/// \brief Checks the validity of a state transition produced by a microarchitecture state reset.
/// \param mgr Pointer to a valid JSONRPC connection manager.
Expand All @@ -133,7 +133,7 @@ CM_API int32_t cm_jsonrpc_verify_step_uarch(const cm_jsonrpc_mgr *mgr, const cm_
/// \returns 0 for success, non zero code for error.
/// \details In case both root_hash_before and root_hash_after are NULL,
/// then it just verifies the access log integrity.
CM_API int32_t cm_jsonrpc_verify_reset_uarch(const cm_jsonrpc_mgr *mgr, const cm_hash *root_hash_before,
CM_API cm_error cm_jsonrpc_verify_reset_uarch(const cm_jsonrpc_mgr *mgr, const cm_hash *root_hash_before,
const char *log, const cm_hash *root_hash_after);

/// \brief Checks the validity of state transitions produced by a send cmio response.
Expand All @@ -147,7 +147,7 @@ CM_API int32_t cm_jsonrpc_verify_reset_uarch(const cm_jsonrpc_mgr *mgr, const cm
/// \returns 0 for success, non zero code for error.
/// \details In case both root_hash_before and root_hash_after are NULL,
/// then it just verifies the access log integrity.
CM_API int32_t cm_jsonrpc_verify_send_cmio_response(const cm_jsonrpc_mgr *mgr, uint16_t reason, const uint8_t *data,
CM_API cm_error cm_jsonrpc_verify_send_cmio_response(const cm_jsonrpc_mgr *mgr, uint16_t reason, const uint8_t *data,
uint64_t length, const cm_hash *root_hash_before, const char *log, const cm_hash *root_hash_after);

#ifdef __cplusplus
Expand Down
4 changes: 2 additions & 2 deletions src/machine-c-api-internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@
#include "semantic-version.h"

/// \brief Helper function that returns error result from C api function
int32_t cm_result_failure();
cm_error cm_result_failure();

/// \brief Helper function that returns success result from C api function
int32_t cm_result_success();
cm_error cm_result_success();

/// \brief Helper function that parses hash from C api structure
cartesi::machine_merkle_tree::hash_type convert_from_c(const cm_hash *c_hash);
Expand Down
Loading

0 comments on commit 9c3216a

Please sign in to comment.