Skip to content

Commit

Permalink
Merge branch 'develop' into 'master'
Browse files Browse the repository at this point in the history
Develop

See merge request in3/c/in3-core!349
  • Loading branch information
simon-jentzsch committed Sep 3, 2020
2 parents f09df5d + 82e565e commit eae7c5a
Show file tree
Hide file tree
Showing 40 changed files with 422 additions and 341 deletions.
1 change: 0 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,6 @@ ENDIF (WASM)
# build tests
if(TEST)
ADD_DEFINITIONS(-DTEST)
ADD_DEFINITIONS(-DIN3_DONT_HASH_KEYS)
ADD_DEFINITIONS(-DIN3_EXPORT_TEST=)
ADD_DEFINITIONS(-DIN3_IMPORT_TEST=extern)
ADD_DEFINITIONS(-DDEBUG)
Expand Down
26 changes: 12 additions & 14 deletions c/include/in3/data.h
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,8 @@ typedef struct json_parser {
size_t allocated; /**< amount of tokens allocated result */
size_t len; /**< number of tokens in result */
size_t depth; /**< max depth of tokens in result */
uint8_t* keys; // key-data
size_t keys_last; // points to the position of the last key.
} json_ctx_t;

/**
Expand Down Expand Up @@ -121,19 +123,21 @@ static inline int d_len(const d_token_t* item) {
}
bool d_eq(const d_token_t* a, const d_token_t* b); /**< compares 2 token and if the value is equal */
NONULL d_key_t keyn(const char* c, const size_t len); /**< generates the keyhash for the given stringrange as defined by len */
d_key_t ikey(json_ctx_t* ctx, const char* name); /**< returnes the indexed key for the given name. */

d_token_t* d_get(d_token_t* item, const uint16_t key); /**< returns the token with the given propertyname (only if item is a object) */
d_token_t* d_get_or(d_token_t* item, const uint16_t key1, const uint16_t key2); /**< returns the token with the given propertyname or if not found, tries the other. (only if item is a object) */
d_token_t* d_get_at(d_token_t* item, const uint32_t index); /**< returns the token of an array with the given index */
d_token_t* d_next(d_token_t* item); /**< returns the next sibling of an array or object */

NONULL void d_serialize_binary(bytes_builder_t* bb, d_token_t* t); /**< write the token as binary data into the builder */
NONULL 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! */
NONULL 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! */
NONULL json_ctx_t* parse_json(const char* js); /**< parses json-data, which needs to be freed after usage! */
NONULL void json_free(json_ctx_t* parser_ctx); /**< frees the parse-context after usage */
NONULL 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! */
NONULL char* d_create_json(d_token_t* item); /**< creates a json-string. It does not work for objects if the parsed data were binary!*/
NONULL void d_serialize_binary(bytes_builder_t* bb, d_token_t* t); /**< write the token as binary data into the builder */
NONULL 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! */
NONULL 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! */
NONULL json_ctx_t* parse_json(const char* js); /**< parses json-data, which needs to be freed after usage! */
NONULL json_ctx_t* parse_json_indexed(const char* js); /**< parses json-data, which needs to be freed after usage! */
NONULL void json_free(json_ctx_t* parser_ctx); /**< frees the parse-context after usage */
NONULL 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! */
NONULL char* d_create_json(json_ctx_t* ctx, d_token_t* item); /**< creates a json-string. It does not work for objects if the parsed data were binary!*/

json_ctx_t* json_create();
NONULL d_token_t* json_create_null(json_ctx_t* jp);
Expand All @@ -147,20 +151,14 @@ NONULL d_token_t* json_object_add_prop(d_token_t* object, d_key_t key, d_token_t
NONULL d_token_t* json_array_add_value(d_token_t* object, d_token_t* value);

// Helper function to map string to 2byte keys (only for tests or debugging)
char* d_get_keystr(d_key_t k); /**< returns the string for a key. This only works track_keynames was activated before! */
void d_track_keynames(uint8_t v); /**< activates the keyname-cache, which stores the string for the keys when parsing. */
void d_clear_keynames(); /**< delete the cached keynames */
char* d_get_keystr(json_ctx_t* json, d_key_t k); /**< returns the string for a key. This only works for index keys or known keys! */

#ifndef IN3_DONT_HASH_KEYS
NONULL static inline d_key_t key(const char* c) {
uint16_t val = 0;
size_t l = strlen(c);
for (; l; l--, c++) val ^= *c | val << 7;
return val;
}
#else
d_key_t key(const char* c);
#endif

static inline char* d_get_stringk(d_token_t* r, d_key_t k) { return d_string(d_get(r, k)); } /**< reads token of a property as string. */
static inline char* d_get_string(d_token_t* r, char* k) { return d_get_stringk(r, key(k)); } /**< reads token of a property as string. */
Expand Down
4 changes: 2 additions & 2 deletions c/src/api/eth1/eth_api.c
Original file line number Diff line number Diff line change
Expand Up @@ -415,7 +415,7 @@ static char* wait_for_receipt(in3_t* in3, char* params, int timeout, int count)
}
else {
//
char* c = d_create_json(result);
char* c = d_create_json(ctx->response_context, result);
ctx_free(ctx);
return c;
}
Expand All @@ -436,7 +436,7 @@ char* eth_wait_for_receipt(in3_t* in3, bytes32_t tx_hash) {
in3_ret_t eth_newFilter(in3_t* in3, json_ctx_t* options) {
rpc_init;
if (options) {
char* p = d_create_json(options->result);
char* p = d_create_json(options, options->result);
sb_add_chars(params, p);
_free(p);
}
Expand Down
2 changes: 1 addition & 1 deletion c/src/api/eth1/rpc_api.c
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ static in3_ret_t in3_abiDecode(in3_rpc_handle_ctx_t* ctx, d_token_t* params) {
else if (!(res = req_parse_result(req, data)))
ret = ctx_set_error(ctx->ctx, "the input data can not be decoded", IN3_EINVAL);
else {
char* result = d_create_json(res->result);
char* result = d_create_json(res, res->result);
ret = in3_rpc_handle_with_string(ctx, result);
_free(result);
}
Expand Down
17 changes: 9 additions & 8 deletions c/src/cmd/in3/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,7 @@ static void execute(in3_t* c, FILE* f) {
else {
d_token_t* result = d_get(ctx->responses[0], K_RESULT);
d_token_t* error = d_get(ctx->responses[0], K_ERROR);
char* r = d_create_json(result ? result : error);
char* r = d_create_json(ctx->response_context, result ? result : error);
if (result)
printf("{\"jsonrpc\":\"2.0\",\"id\":%i,\"result\":%s}\n", id, r);
else
Expand Down Expand Up @@ -417,8 +417,10 @@ uint64_t getchain_id(char* name) {

// set the chain_id in the client
void set_chain_id(in3_t* c, char* id) {
c->chain_id = strstr(id, "://") ? 0xFFFFL : getchain_id(id);
if (c->chain_id == 0xFFFFL) {
c->chain_id = strstr(id, "://") ? CHAIN_ID_LOCAL : getchain_id(id);
if (c->chain_id == CHAIN_ID_LOCAL) {
BIT_CLEAR(c->chain_id, FLAGS_AUTO_UPDATE_LIST);
c->proof = PROOF_NONE;
in3_chain_t* chain = in3_get_chain(c);
if (strstr(id, "://")) { // its a url
if (!chain->nodelist)
Expand Down Expand Up @@ -980,7 +982,7 @@ int main(int argc, char* argv[]) {
}
json_ctx_t* res = req_parse_result(parseSignature(sig), d_to_bytes(d_get_at(parse_json(params)->result, 0)));
if (json)
printf("%s\n", d_create_json(res->result));
printf("%s\n", d_create_json(res, res->result));
else
print_val(res->result);
return 0;
Expand Down Expand Up @@ -1197,9 +1199,8 @@ int main(int argc, char* argv[]) {
}
else
json = (char*) readFile(stdin).data;
d_track_keynames(1);
json_ctx_t* j = parse_json(json);
chainspec_t* spec = chainspec_create_from_json(j->result);
json_ctx_t* j = parse_json_indexed(json);
chainspec_t* spec = chainspec_create_from_json(j);
if (validators) {
// first PoA without validators-list
for (uint32_t i = 0; i < spec->consensus_transitions_len; i++) {
Expand Down Expand Up @@ -1347,7 +1348,7 @@ int main(int argc, char* argv[]) {
uint8_t* tmp = alloca(l + 1);
json_ctx_t* res = req_parse_result(req, bytes(tmp, hex_to_bytes(result, -1, tmp, l + 1)));
if (json)
printf("%s\n", d_create_json(res->result));
printf("%s\n", d_create_json(res, res->result));
else
print_val(res->result);
}
Expand Down
18 changes: 2 additions & 16 deletions c/src/cmd/tools/data.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@
#include "../../core/util/data.h"
#include "../../core/util/colors.h"
#include "../../core/util/mem.h"
#include "../../core/util/used_keys.h"
#include "../../core/util/utils.h"
#include <stdint.h>
#include <stdio.h>
Expand Down Expand Up @@ -88,18 +87,6 @@ bytes_t read_from_stdin(FILE* file) {
#define C_LPURPLE "1;35"
#define C_LCYAN "1;36"

static inline d_key_t keyhash(const char* c) {
uint16_t val = 0;
size_t l = strlen(c);
for (; l; l--, c++) val ^= *c | val << 7;
return val;
}

static void init_keys() {
for (int i = 0; USED_KEYS[i]; i++)
add_keyname(USED_KEYS[i], keyhash(USED_KEYS[i]), strlen(USED_KEYS[i]));
}

static void print_hex(uint8_t* d, uint32_t l, char* color) {
if (color) printf(COLORT_SELECT, color);
for (uint32_t i = 0; i < l; i++)
Expand Down Expand Up @@ -146,7 +133,7 @@ static int read_token(uint8_t* d, size_t* p, int level, int* index, int keyval)

for (int i = 0; i < level; i++) printf(COLORT_BBLACK "." COLORT_RESET);
if (keyval >= 0) {
char* keyname = d_get_keystr((d_key_t) keyval);
char* keyname = d_get_keystr(NULL, (d_key_t) keyval);
if (keyname)
printf(COLORT_RMAGENTA "%s" COLORT_RESET, keyname);
else {
Expand Down Expand Up @@ -241,7 +228,6 @@ int main(int argc, char* argv[]) {
bool debug = false;

int i;
init_keys();
// fill from args
for (i = 1; i < argc; i++) {
if (strcmp(argv[i], "-f") == 0)
Expand Down Expand Up @@ -280,7 +266,7 @@ int main(int argc, char* argv[]) {
printf("Invalid binary data!\n");
return 1;
}
printf("%s\n", d_create_json(ctx->result));
printf("%s\n", d_create_json(ctx, ctx->result));
return 0;
}

Expand Down
6 changes: 3 additions & 3 deletions c/src/core/client/client.c
Original file line number Diff line number Diff line change
Expand Up @@ -116,10 +116,10 @@ static in3_ret_t ctx_rpc(in3_ctx_t* ctx, char** result, char** error) {
*error = _strdupn(d_string(r), -1);
else if (d_type(r) == T_OBJECT) {
char* msg = d_get_stringk(r, K_MESSAGE);
*error = msg ? _strdupn(msg, -1) : d_create_json(r);
*error = msg ? _strdupn(msg, -1) : d_create_json(ctx->response_context, r);
}
else
*error = d_create_json(r);
*error = d_create_json(ctx->response_context, r);
res = IN3_ERPC;
goto clean;
}
Expand All @@ -132,7 +132,7 @@ static in3_ret_t ctx_rpc(in3_ctx_t* ctx, char** result, char** error) {
}

// we have a result and copy it
if (result) *result = d_create_json(r);
if (result) *result = d_create_json(ctx->response_context, r);

clean:
ctx_free(ctx);
Expand Down
6 changes: 4 additions & 2 deletions c/src/core/client/client_init.c
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ void in3_register_payment(
goto cleanup; \
})
#define EXPECT_CFG_NCP_ERR(cond, err) EXPECT(cond, { res = err; goto cleanup; })
#define EXPECT_TOK(token, cond, err) EXPECT_CFG_NCP_ERR(cond, config_err(d_get_keystr(token->key), err))
#define EXPECT_TOK(token, cond, err) EXPECT_CFG_NCP_ERR(cond, config_err(d_get_keystr(cnf, token->key), err))
#define EXPECT_TOK_BOOL(token) EXPECT_TOK(token, d_type(token) == T_BOOLEAN, "expected boolean value")
#define EXPECT_TOK_STR(token) EXPECT_TOK(token, d_type(token) == T_STRING, "expected string value")
#define EXPECT_TOK_ARR(token) EXPECT_TOK(token, d_type(token) == T_ARRAY, "expected array")
Expand All @@ -109,7 +109,7 @@ void in3_register_payment(
#define EXPECT_TOK_U16(token) EXPECT_TOK(token, IS_D_UINT16(token), "expected uint16 value")
#define EXPECT_TOK_U32(token) EXPECT_TOK(token, IS_D_UINT32(token), "expected uint32 value")
#define EXPECT_TOK_U64(token) EXPECT_TOK(token, IS_D_UINT64(token), "expected uint64 value")
#define EXPECT_TOK_KEY_HEXSTR(token) EXPECT_TOK(token, is_hex_str(d_get_keystr(token->key)), "expected hex str")
#define EXPECT_TOK_KEY_HEXSTR(token) EXPECT_TOK(token, is_hex_str(d_get_keystr(cnf, token->key)), "expected hex str")

// set the defaults
typedef struct default_fn {
Expand Down Expand Up @@ -611,12 +611,14 @@ static chain_id_t chain_id(d_token_t* t) {
}

static inline char* config_err(const char* keyname, const char* err) {
if (!keyname) keyname = "unknown";
char* s = _malloc(strlen(keyname) + strlen(err) + 4);
sprintf(s, "%s: %s!", keyname, err);
return s;
}

static inline bool is_hex_str(const char* str) {
if (!str) return false;
if (str[0] == '0' && str[1] == 'x')
str += 2;
return str[strspn(str, "0123456789abcdefABCDEF")] == 0;
Expand Down
2 changes: 1 addition & 1 deletion c/src/core/client/context.c
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,7 @@ in3_ret_t ctx_send_sub_request(in3_ctx_t* parent, char* method, char* params, ch
}

// create the call
req = use_cache ? _strdupn(req, -1) : _malloc(strlen(params) + strlen(method) + 20 + (in3 ? 5 + strlen(in3) : 0));
req = use_cache ? _strdupn(req, -1) : _malloc(strlen(params) + strlen(method) + 26 + (in3 ? 7 + strlen(in3) : 0));
if (!use_cache) {
if (in3)
sprintf(req, "{\"method\":\"%s\",\"params\":[%s],\"in3\":%s}", method, params, in3);
Expand Down
Loading

0 comments on commit eae7c5a

Please sign in to comment.