Skip to content

Commit

Permalink
Merge branch '238-rust-api-impl' into 'develop'
Browse files Browse the repository at this point in the history
Resolve "RUST API Impl"

Closes #238

See merge request in3/c/in3-core!190
  • Loading branch information
simon-jentzsch committed Apr 9, 2020
2 parents 99565a0 + e30a850 commit 82b7f21
Show file tree
Hide file tree
Showing 56 changed files with 18,358 additions and 188 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,4 @@ bin/
*.class
target/
.cproject
Cargo.lock
2 changes: 2 additions & 0 deletions .gitlab-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ stages:
- build
- bindings
- wasm
- rust
- python
- dotnet
- java
Expand All @@ -23,6 +24,7 @@ include:
- local: "/java/ci.yml"
- local: "/python/ci.yml"
- local: "/dotnet/ci.yml"
- local: "/rust/ci.yml"
##### local_docker_deploy_and_vulnerability_analysis #####

##### analyse #####
Expand Down
6 changes: 6 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ option(CODE_COVERAGE "Builds targets with code coverage instrumentation. (Requi
option(PAY_ETH "support for direct Eth-Payment" OFF)
option(USE_SCRYPT "integrate scrypt into the build in order to allow decrypt_key for scrypt encoded keys." ON)
option(USE_CURL "if true the curl transport will be built (with a dependency to libcurl)" ON)
option(DEV_NO_INTRN_PTR "(*dev option*) if true the client will NOT include a void pointer (named internal) for use by devs)" ON)

if (USE_PRECOMPUTED_EC)
ADD_DEFINITIONS(-DUSE_PRECOMPUTED_CP=1)
Expand Down Expand Up @@ -145,6 +146,11 @@ if(SEGGER_RTT)
ADD_DEFINITIONS(-DSEGGER_RTT)
endif(SEGGER_RTT)

if (DEV_NO_INTRN_PTR)
MESSAGE(STATUS "Disable dev opt (internal ptr)")
ADD_DEFINITIONS(-DDEV_NO_INTRN_PTR)
endif()

# handle version
if (TAG_VERSION)
set(PROJECT_VERSION "${TAG_VERSION}")
Expand Down
10 changes: 10 additions & 0 deletions c/include/in3.rs.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// AUTO-GENERATED FILE
// See scripts/build_includeh.sh
#include "../src/core/client/context_internal.h"
#include "in3/bytes.h"
#include "in3/client.h"
#include "in3/context.h"
#include "in3/error.h"
#include "in3/eth_api.h"
#include "in3/in3_init.h"
#include "in3/in3_curl.h"
6 changes: 3 additions & 3 deletions c/include/in3/bytes.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,11 @@ typedef struct bytes {

/** a byte-buffer to attach byte-functions. */
typedef struct {
uint32_t bsize; /**< size of the currently allocated bytes */
bytes_t b; /**< the bytes struct */
size_t bsize; /**< size of the currently allocated bytes */
bytes_t b; /**< the bytes struct */
} bytes_builder_t;

bytes_t* b_new(const char* data, int len); /**< allocates a new byte array with 0 filled */
bytes_t* b_new(const uint8_t* data, uint32_t len); /**< allocates a new byte array with 0 filled */
void b_print(const bytes_t* a); /**< prints a the bytes as hex to stdout */
void ba_print(const uint8_t* a, size_t l); /**< prints a the bytes as hex to stdout */
int b_cmp(const bytes_t* a, const bytes_t* b); /**< compares 2 byte arrays and returns 1 for equal and 0 for not equal*/
Expand Down
151 changes: 112 additions & 39 deletions c/include/in3/client.h
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,10 @@ typedef struct in3_node_weight {
uint32_t response_count; /**< counter for responses */
uint32_t total_response_time; /**< total of all response times */
uint64_t blacklisted_until; /**< if >0 this node is blacklisted until k. k is a unix timestamp */
#ifdef PAY
uint32_t price; /**< the price per request unit */
uint64_t payed; /**< already payed */
#endif
} in3_node_weight_t;

/**
Expand Down Expand Up @@ -271,17 +275,17 @@ typedef struct in3_chain {
* @returns the found result. if the key is found this function should return the values as bytes otherwise `NULL`.
**/
typedef bytes_t* (*in3_storage_get_item)(
void* cptr, /**< a custom pointer as set in the storage handler*/
char* key /**< the key to search in the cache */
void* cptr, /**< a custom pointer as set in the storage handler*/
const char* key /**< the key to search in the cache */
);

/**
* storage handler function for writing to the cache.
**/
typedef void (*in3_storage_set_item)(
void* cptr, /**< a custom pointer as set in the storage handler*/
char* key, /**< the key to store the value.*/
bytes_t* value /**< the value to store.*/
void* cptr, /**< a custom pointer as set in the storage handler*/
const char* key, /**< the key to store the value.*/
bytes_t* value /**< the value to store.*/
);

/**
Expand Down Expand Up @@ -343,26 +347,89 @@ typedef struct in3_signer {

} in3_signer_t;

/**
*
* payment prepearation function.
*
* allows the payment to handle things before the request will be send.
*
*/
typedef in3_ret_t (*in3_pay_prepare)(void* ctx, void* cptr);

/**
*
* called after receiving a parseable response with a in3-section.
*
*
*/
typedef in3_ret_t (*in3_pay_follow_up)(void* ctx, void* node, d_token_t* in3, d_token_t* error, void* cptr);

/**
*
* free function for the custom pointer.
*
*
*/
typedef void (*in3_pay_free)(void* cptr);

/**
*
* handles the request.
*
* this function is called when the in3-section of payload of the request is built and allows the handler to add properties.
*
*/
typedef in3_ret_t (*in3_pay_handle_request)(void* ctx, sb_t* sb, in3_request_config_t* rc, void* cptr);

/**
*
* the payment handler.
*
* if a payment handler is set it will be used when generating the request.
*
*/
typedef struct in3_pay {
/* payment prepearation function.*/
in3_pay_prepare prepare;

/* payment prepearation function.*/
in3_pay_follow_up follow_up;

/* this function is called when the in3-section of payload of the request is built and allows the handler to add properties. .*/
in3_pay_handle_request handle_request;

/* frees the custom pointer (cptr).*/
in3_pay_free free;

/* custom object whill will be passed to functions */
void* cptr;

} in3_pay_t;

/** response-object.
*
* if the error has a length>0 the response will be rejected
*/
typedef struct n3_response {
typedef struct in3_response {
sb_t error; /**< a stringbuilder to add any errors! */
sb_t result; /**< a stringbuilder to add the result */
} in3_response_t;

/* forward decl */
typedef struct in3_t_ in3_t;

/** request-object.
*
* represents a RPC-request
*/
typedef struct n3_request {
typedef struct in3_request {
char* payload; /**< the payload to send */
char** urls; /**< array of urls */
int urls_len; /**< number of urls */
in3_response_t* results; /**< the responses*/
uint32_t timeout; /**< the timeout 0= no timeout*/
uint32_t* times; /**< measured times (in ms) which will be used for ajusting the weights */
in3_t* in3; /**< pointer to associated IN3 instance */
} in3_request_t;

/** the transport function to be implemented by the transport provider.
Expand Down Expand Up @@ -408,7 +475,7 @@ typedef struct in3_filter_handler_t_ {
* This struct holds the configuration and also point to internal resources such as filters or chain configs.
*
*/
typedef struct in3_t_ {
struct in3_t_ {
/** number of seconds requests can be cached. */
uint32_t cache_timeout;

Expand Down Expand Up @@ -460,6 +527,10 @@ typedef struct in3_t_ {
/** signer-struct managing a wallet */
in3_signer_t* signer;

#ifdef PAY
/** payment handler. if set it will add payment to each request */
in3_pay_t* pay;
#endif
/** the transporthandler sending requests */
in3_transport_send transport;

Expand All @@ -478,7 +549,11 @@ typedef struct in3_t_ {
/** used to identify the capabilities of the node. */
in3_node_props_t node_props;

} in3_t;
#ifndef DEV_NO_INTRN_PTR
/** pointer to internal data */
void* internal;
#endif
};

/** creates a new Incubes configuration and returns the pointer.
*
Expand All @@ -496,20 +571,11 @@ typedef struct in3_t_ {
* // create new client
* in3_t* client = in3_new();
*
* // configure storage...
* in3_storage_handler_t storage_handler;
* storage_handler.get_item = storage_get_item;
* storage_handler.set_item = storage_set_item;
* storage_handler.clear = storage_clear;
*
* // configure transport
* client->transport = send_curl;
*
* // configure storage
* client->cache = &storage_handler;
*
* // init cache
* in3_cache_init(client);
* in3_set_storage_handler(c, storage_get_item, storage_set_item, storage_clear, NULL);
*
* // ready to use ...
* ```
Expand All @@ -534,20 +600,11 @@ in3_t* in3_new() __attribute__((deprecated("use in3_for_chain(ETH_CHAIN_ID_MULTI
* // create new client
* in3_t* client = in3_for_chain(ETH_CHAIN_ID_MAINNET);
*
* // configure storage...
* in3_storage_handler_t storage_handler;
* storage_handler.get_item = storage_get_item;
* storage_handler.set_item = storage_set_item;
* storage_handler.clear = storage_clear;
*
* // configure transport
* client->transport = send_curl;
*
* // configure storage
* client->cache = &storage_handler;
*
* // init cache
* in3_cache_init(client);
* in3_set_storage_handler(c, storage_get_item, storage_set_item, storage_clear, NULL);
*
* // ready to use ...
* ```
Expand All @@ -563,18 +620,18 @@ in3_t* in3_for_chain_default(

/** sends a request and stores the result in the provided buffer */
in3_ret_t in3_client_rpc(
in3_t* c, /**< [in] the pointer to the incubed client config. */
char* method, /**< [in] the name of the rpc-funcgtion to call. */
char* params, /**< [in] docs for input parameter v. */
char** result, /**< [in] pointer to string which will be set if the request was successfull. This will hold the result as json-rpc-string. (make sure you free this after use!) */
char** error /**< [in] pointer to a string containg the error-message. (make sure you free it after use!) */);
in3_t* c, /**< [in] the pointer to the incubed client config. */
const char* method, /**< [in] the name of the rpc-funcgtion to call. */
const char* params, /**< [in] docs for input parameter v. */
char** result, /**< [in] pointer to string which will be set if the request was successfull. This will hold the result as json-rpc-string. (make sure you free this after use!) */
char** error /**< [in] pointer to a string containg the error-message. (make sure you free it after use!) */);

/** sends a request and stores the result in the provided buffer */
in3_ret_t in3_client_rpc_raw(
in3_t* c, /**< [in] the pointer to the incubed client config. */
char* request, /**< [in] the rpc request including method and params. */
char** result, /**< [in] pointer to string which will be set if the request was successfull. This will hold the result as json-rpc-string. (make sure you free this after use!) */
char** error /**< [in] pointer to a string containg the error-message. (make sure you free it after use!) */);
in3_t* c, /**< [in] the pointer to the incubed client config. */
const char* request, /**< [in] the rpc request including method and params. */
char** result, /**< [in] pointer to string which will be set if the request was successfull. This will hold the result as json-rpc-string. (make sure you free this after use!) */
char** error /**< [in] pointer to a string containg the error-message. (make sure you free it after use!) */);

/** executes a request and returns result as string. in case of an error, the error-property of the result will be set.
* The resulting string must be free by the the caller of this function!
Expand Down Expand Up @@ -693,10 +750,26 @@ in3_signer_t* in3_create_signer(
* create a new storage handler-object to be set on the client.
* the caller will need to free this pointer after usage.
*/
in3_storage_handler_t* in3_create_storage_handler(
in3_storage_handler_t* in3_set_storage_handler(
in3_t* c, /**< the incubed client */
in3_storage_get_item get_item, /**< function pointer returning a stored value for the given key.*/
in3_storage_set_item set_item, /**< function pointer setting a stored value for the given key.*/
in3_storage_clear clear, /**< function pointer clearing all contents of cache.*/
void* cptr /**< custom pointer which will will be passed to functions */
);
#ifdef PAY
/**
* configure function for a payment.
*/
typedef char* (*pay_configure)(in3_t* c, d_token_t* config);

/**
* registers a payment provider
*/
void in3_register_payment(
char* name, /**< name of the payment-type */
pay_configure handler /**< pointer to the handler- */
);
#endif

#endif
14 changes: 7 additions & 7 deletions c/include/in3/context.h
Original file line number Diff line number Diff line change
Expand Up @@ -147,8 +147,8 @@ typedef enum state {
* *Important*: the req_data will not be cloned but used during the execution. The caller of the this function is also responsible for freeing this string afterwards.
*/
in3_ctx_t* ctx_new(
in3_t* client, /**< [in] the client-config. */
char* req_data /**< [in] the rpc-request as json string. */
in3_t* client, /**< [in] the client-config. */
const char* req_data /**< [in] the rpc-request as json string. */
);
/**
* sends a previously created context to nodes and verifies it.
Expand Down Expand Up @@ -357,8 +357,8 @@ in3_ret_t ctx_get_error(
* This context *MUST* be freed with ctx_free(ctx) after usage to release the resources.
*/
in3_ctx_t* in3_client_rpc_ctx_raw(
in3_t* c, /**< [in] the client config. */
char* request /**< [in] rpc request. */
in3_t* c, /**< [in] the client config. */
const char* request /**< [in] rpc request. */
);

/**
Expand All @@ -367,9 +367,9 @@ in3_ctx_t* in3_client_rpc_ctx_raw(
* This context *MUST* be freed with ctx_free(ctx) after usage to release the resources.
*/
in3_ctx_t* in3_client_rpc_ctx(
in3_t* c, /**< [in] the clientt config. */
char* method, /**< [in] rpc method. */
char* params /**< [in] params as string. */
in3_t* c, /**< [in] the clientt config. */
const char* method, /**< [in] rpc method. */
const char* params /**< [in] params as string. */
);

#endif
2 changes: 1 addition & 1 deletion c/include/in3/data.h
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ d_token_t* d_next(d_token_t* item);
void d_serialize_binary(bytes_builder_t* bb, d_token_t* t); /**< write the token as binary data into the builder */
json_ctx_t* parse_binary(const bytes_t* data); /**< parses the data and returns the context with the token, which needs to be freed after usage! */
json_ctx_t* parse_binary_str(const char* data, int len); /**< parses the data and returns the context with the token, which needs to be freed after usage! */
json_ctx_t* parse_json(char* js); /**< parses json-data, which needs to be freed after usage! */
json_ctx_t* parse_json(const char* js); /**< parses json-data, which needs to be freed after usage! */
void json_free(json_ctx_t* parser_ctx); /**< frees the parse-context after usage */
str_range_t d_to_json(const d_token_t* item); /**< returns the string for a object or array. This only works for json as string. For binary it will not work! */
char* d_create_json(d_token_t* item); /**< creates a json-string. It does not work for objects if the parsed data were binary!*/
Expand Down
Loading

0 comments on commit 82b7f21

Please sign in to comment.