Skip to content

Commit

Permalink
Merge branch 'cleanup_exec' into 'develop'
Browse files Browse the repository at this point in the history
Cleanup exec

See merge request in3/c/in3-core!307
  • Loading branch information
simon-jentzsch committed Jul 9, 2020
2 parents fce9f2b + 087d7bf commit bac6eb0
Show file tree
Hide file tree
Showing 31 changed files with 624 additions and 231 deletions.
17 changes: 11 additions & 6 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,15 @@
"name": "in3 cmd",
"program": "${workspaceFolder}/build/bin/in3",
"cwd": "${workspaceFolder}",
"externalConsole": true,
"args": [
"-c",
"btc",
"getblock",
"000000000000000000103b2395f6cd94221b10d02eb9be5850303c0534307220",
"-ccache",
"-a",
"3",
"call",
"totalNodes():uint",
"-to",
"0x6c095a05764a23156efd9d603eada144a9b1af33",
"-debug"
]
},
Expand All @@ -60,7 +64,8 @@
"name": "run test",
"program": "${workspaceFolder}/build/test/runner",
"args": [
"../c/test/testdata/requests/in3_invalid_block.json",
"../c/test/testdata/requests/in3_total_nodes.json",
"-d",
"-t",
"1"
],
Expand All @@ -77,7 +82,7 @@
"type": "lldb",
"request": "launch",
"name": "run rpcapi test",
"program": "${workspaceFolder}/build/test/test_rpc_api",
"program": "${workspaceFolder}/build/test/test_request",
"cwd": "${workspaceFolder}/build"
},
{
Expand Down
6 changes: 3 additions & 3 deletions c/include/in3/client.h
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ typedef struct in3_chain {
chain_id_t chain_id; /**< chain_id, which could be a free or based on the public ethereum networkId*/
in3_chain_type_t type; /**< chaintype */
uint64_t last_block; /**< last blocknumber the nodeList was updated, which is used to detect changed in the nodelist*/
int nodelist_length; /**< number of nodes in the nodeList */
unsigned int nodelist_length; /**< number of nodes in the nodeList */
in3_node_t* nodelist; /**< array of nodes */
in3_node_weight_t* weights; /**< stats and weights recorded for each node */
bytes_t** init_addresses; /**< array of addresses of nodes that should always part of the nodeList */
Expand Down Expand Up @@ -615,8 +615,8 @@ NONULL in3_ret_t in3_cache_init(
* My return NULL if not found.
*/
NONULL in3_chain_t* in3_find_chain(
in3_t* c /**< the incubed client */,
chain_id_t chain_id /**< chain_id */
const in3_t* c /**< the incubed client */,
chain_id_t chain_id /**< chain_id */
);

/**
Expand Down
17 changes: 12 additions & 5 deletions c/include/in3/context.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,11 @@ typedef enum ctx_type {
* This will be used when picking the nodes to send the request to. A linked list of these structs desribe the result.
*/
typedef struct weight {
in3_node_t* node; /**< the node definition including the url */
in3_node_weight_t* weight; /**< the current weight and blacklisting-stats */
float s; /**< The starting value */
float w; /**< weight value */
struct weight* next; /**< next in the linkedlist or NULL if this is the last element*/
unsigned int index; /**< index of the node in the nodelist */
bool blocked; /**< if true this node has been blocked for sending wrong responses */
uint32_t s; /**< The starting value */
uint32_t w; /**< weight value */
struct weight* next; /**< next in the linkedlist or NULL if this is the last element*/
} node_match_t;

/**
Expand Down Expand Up @@ -445,4 +445,11 @@ NONULL void in3_ctx_add_response(
int data_len /**< the length of the data or the the string (use -1 if data is a null terminated string)*/
);

NONULL static inline in3_node_t* ctx_get_node(const in3_chain_t* chain, const node_match_t* node) {
return node->index < chain->nodelist_length ? chain->nodelist + node->index : NULL;
}
NONULL static inline in3_node_weight_t* ctx_get_node_weight(const in3_chain_t* chain, const node_match_t* node) {
return node->index < chain->nodelist_length ? chain->weights + node->index : NULL;
}

#endif
3 changes: 3 additions & 0 deletions c/include/in3/log.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ typedef enum { LOG_TRACE,
#define in3_log_set_prefix(prefix) in3_log_set_prefix_(prefix)
#define in3_log_enable_prefix() in3_log_enable_prefix_()
#define in3_log_disable_prefix() in3_log_disable_prefix_()
#define in3_log_is_prefix_enabled() in3_log_is_prefix_enabled_()
#define in3_log(...) in3_log_(__VA_ARGS__)
#else
#define in3_log_trace(...)
Expand All @@ -57,6 +58,7 @@ typedef enum { LOG_TRACE,
#define in3_log_get_level() LOG_TRACE
#define in3_log_set_quiet(enable)
#define in3_log_set_prefix(prefix)
#define in3_log_is_prefix_enabled() 0
#define in3_log_enable_prefix()
#define in3_log_disable_prefix()
#define in3_log(level, file, function, line, ...) \
Expand All @@ -82,6 +84,7 @@ void in3_log_set_quiet_(int enable);
void in3_log_set_prefix_(const char* prefix);
void in3_log_enable_prefix_();
void in3_log_disable_prefix_();
int in3_log_is_prefix_enabled_();

/* in3_log() function can be made thread-safe using the in3_log_set_lock() function */
void in3_log_(in3_log_level_t level, const char* file, const char* function, int line, const char* fmt, ...);
Expand Down
1 change: 1 addition & 0 deletions c/include/in3/stringbuilder.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,5 +72,6 @@ sb_t* sb_add_bytes(sb_t* sb, const char* prefix, const bytes_t* bytes, int len,
sb_t* sb_add_hexuint_l(sb_t* sb, uintmax_t uint, size_t l); /**< add a integer value as hexcoded, 0x-prefixed string*/
sb_t* sb_add_escaped_chars(sb_t* sb, const char* chars); /**< add chars but escapes all quotes */
sb_t* sb_add_int(sb_t* sb, uint64_t val); /**< adds a numeric value to the stringbuilder */
char* format_json(const char* json); /**< format a json string and returns a new string, which needs to be freed */

#endif
2 changes: 1 addition & 1 deletion c/src/cmd/in3/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ if (LEDGER_NANO)
set(LIBS ${LIBS} ledger_signer)
endif()

add_executable(in3 main.c in3_storage.c)
add_executable(in3 main.c in3_storage.c recorder.c)
target_compile_definitions(in3 PRIVATE _XOPEN_SOURCE=600)

target_link_libraries(in3 init pk_signer ${LIBS} -lm)
Expand Down
9 changes: 7 additions & 2 deletions c/src/cmd/in3/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@
#include "../../verifier/eth1/nano/chainspec.h"
#include "../../verifier/in3_init.h"
#include "in3_storage.h"
#include "recorder.h"
#include <inttypes.h>
#include <math.h>
#include <stdint.h>
Expand Down Expand Up @@ -766,6 +767,10 @@ int main(int argc, char* argv[]) {
run_test_request = 1;
else if (strcmp(argv[i], "-thr") == 0)
run_test_request = 2;
else if (strcmp(argv[i], "-fo") == 0)
recorder_write_start(c, argv[++i]);
else if (strcmp(argv[i], "-fi") == 0)
recorder_read_start(c, argv[++i]);
else if (strcmp(argv[i], "-nl") == 0)
set_nodelist(c, argv[++i], false);
else if (strcmp(argv[i], "-bn") == 0)
Expand Down Expand Up @@ -954,7 +959,7 @@ int main(int argc, char* argv[]) {
if (run_test_request == 1) more = "WEIGHT : LAST_BLOCK";
if (run_test_request == 2) more = "WEIGHT : NAME VERSION : RUNNING : HEALTH : LAST_BLOCK";
printf(" : %-45s : %7s : %5s : %5s: %s\n------------------------------------------------------------------------------------------------\n", "URL", "BL", "CNT", "AVG", more);
for (int i = 0; i < chain->nodelist_length; i++) {
for (unsigned int i = 0; i < chain->nodelist_length; i++) {
in3_ctx_t* ctx = NULL;
char* health_s = NULL;
if (run_test_request) {
Expand Down Expand Up @@ -1251,7 +1256,7 @@ int main(int argc, char* argv[]) {

// if the result is a string, we remove the quotes
if (result[0] == '"' && result[strlen(result) - 1] == '"') {
memmove(result, result + 1, strlen(result) + 1);
memmove(result, result + 1, strlen(result));
result[strlen(result) - 1] = 0;
}

Expand Down
203 changes: 203 additions & 0 deletions c/src/cmd/in3/recorder.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,203 @@
#include "recorder.h"
#include "../../core/client/context_internal.h"
#include "../../core/client/keys.h"
#include <math.h>
#include <stdio.h>
static void die(char* msg) {
fprintf(stderr, COLORT_RED "Error: %s" COLORT_RESET "\n", msg);
exit(EXIT_FAILURE);
}

typedef struct {
char* file;
in3_transport_send transport;
FILE* f;
in3_storage_handler_t* cache;
uint64_t time;
} recorder_t;

typedef struct {
char* name;
char** args;
int argl;

} recorder_entry_t;

static recorder_t rec = {
.file = NULL,
.transport = NULL,
.f = NULL,
.cache = NULL,
.time = 0};

static int rand_out(void* s) {
UNUSED_VAR(s);
int r = rand();
fprintf(rec.f, ":: rand %i\n\n", r);
fflush(rec.f);
return r;
}

recorder_entry_t read_entry(sb_t* sb) {
recorder_entry_t entry = {0};

char buffer[1024];
while (fgets(buffer, 1023, rec.f)) {
int l = strlen(buffer);
if (buffer[l - 1] == '\n')
buffer[--l] = 0;
if (!l) break;
if (!entry.name) {
char* ptr = strtok(buffer + 3, " ");
entry.name = _strdupn(ptr, -1);
while ((ptr = strtok(NULL, " "))) {
entry.args = entry.argl ? _realloc(entry.args, sizeof(char*) * (entry.argl + 1), sizeof(char*) * entry.argl) : _malloc(sizeof(char*));
entry.args[entry.argl++] = _strdupn(ptr, -1);
}
} else
sb_add_chars(sb, buffer);
}

return entry;
}
static void entry_free(recorder_entry_t* e, sb_t* sb) {
if (e->name) _free(e->name);
for (int i = 0; i < e->argl; i++) _free(e->args[i]);
_free(e->args);
if (sb && sb->data) _free(sb->data);
if (sb) *sb = (sb_t){0};
}
static int rand_in(void* s) {
UNUSED_VAR(s);
sb_t sb = {0};
recorder_entry_t entry = read_entry(&sb);
if (!entry.name || strcmp(entry.name, "rand")) die("expected rand in recorder!");
if (entry.argl != 1) die("expect one arg for random");
int r = atoi(entry.args[0]);
entry_free(&entry, &sb);

return r;
}

static in3_ret_t recorder_transport_in(in3_request_t* req) {
sb_t sb = {0};
recorder_entry_t entry = {0};
if (req->action == REQ_ACTION_SEND) {
entry = read_entry(&sb);
if (!entry.name || strcmp(entry.name, "request")) die("expected request in recorder!");
entry_free(&entry, &sb);
req->cptr = &rec;
}
if (req->action != REQ_ACTION_CLEANUP) {
entry = read_entry(&sb);
if (!entry.name || strcmp(entry.name, "response")) die("expected response in recorder!");
in3_response_t* r = req->ctx->raw_response + atoi(entry.args[0]);
sb_add_chars(&r->data, sb.data);
r->state = atoi(entry.args[3]);
r->time = atoi(entry.args[4]);
entry_free(&entry, &sb);
}

return 0;
}
static in3_ret_t recorder_transport_out(in3_request_t* req) {
in3_chain_t* chain = in3_find_chain(req->ctx->client, req->ctx->client->chain_id);
node_match_t* m = req->ctx->nodes;
in3_ret_t res = rec.transport(req);
if (req->action == REQ_ACTION_SEND) {
fprintf(rec.f, ":: request ");
for (int i = 0; m; i++, m = m->next)
fprintf(rec.f, "%s ", ctx_get_node(chain, m)->url);
fprintf(rec.f, "\n %s\n\n", req->payload);
fflush(rec.f);
}
if (req->action != REQ_ACTION_CLEANUP) {
m = req->ctx->nodes;
for (int i = 0; m; i++, m = m->next) {
in3_response_t* r = req->ctx->raw_response + i;
if (r->time) {
fprintf(rec.f, ":: response %i %s %s %i %i\n", i, d_get_stringk(req->ctx->requests[0], K_METHOD), ctx_get_node(chain, m)->url, r->state, r->time);
char* data = format_json(r->data.data ? r->data.data : "");
fprintf(rec.f, "%s\n\n", data);
fflush(rec.f);
_free(data);
}
}
}
return res;
}

bytes_t* rec_get_item_in(void* cptr, const char* key) {
UNUSED_VAR(cptr);
UNUSED_VAR(key);
sb_t sb = {0};
recorder_entry_t entry = read_entry(&sb);
if (!entry.name || strcmp(entry.name, "cache")) die("expected cache in recorder!");
if (entry.argl != 2) die("expect 2 args for cache");
if (strcmp(key, entry.args[0])) die("wrong cache key");
bytes_t* found = atoi(entry.args[1]) ? hex_to_new_bytes(sb.data, sb.len) : NULL;
entry_free(&entry, &sb);

return found;
}

void rec_set_item_in(void* cptr, const char* key, bytes_t* content) {
UNUSED_VAR(cptr);
UNUSED_VAR(key);
UNUSED_VAR(content);
}

void rec_clear_in(void* cptr) {
UNUSED_VAR(cptr);
}

bytes_t* rec_get_item_out(void* cptr, const char* key) {
if (!rec.cache) return NULL;
bytes_t* found = rec.cache->get_item(cptr, key);
fprintf(rec.f, ":: cache %s %i\n", key, found ? 1 : 0);
if (found) {
char* hex = alloca(found->len * 2 + 1);
bytes_to_hex(found->data, found->len, hex);
fprintf(rec.f, "%s\n\n", hex);
} else
fprintf(rec.f, "\n");

return found;
}

void rec_set_item_out(void* cptr, const char* key, bytes_t* content) {
if (rec.cache) rec.cache->set_item(cptr, key, content);
}

void rec_clear_out(void* cptr) {
if (rec.cache) rec.cache->clear(cptr);
}
uint64_t static_time(void* t) {
UNUSED_VAR(t);
return rec.time;
}

void recorder_write_start(in3_t* c, char* file) {
rec.file = file;
rec.transport = c->transport;
c->transport = recorder_transport_out;
rec.f = fopen(file, "w");
rec.cache = c->cache;
in3_set_func_rand(rand_out);
in3_set_storage_handler(c, rec_get_item_out, rec_set_item_out, rec_clear_out, &rec);
fprintf(rec.f, ":: time %u\n\n", (uint32_t) in3_time(NULL));
}

void recorder_read_start(in3_t* c, char* file) {
rec.file = file;
rec.transport = c->transport;
c->transport = recorder_transport_in;
rec.f = fopen(file, "r");
in3_set_func_rand(rand_in);
in3_set_storage_handler(c, rec_get_item_in, rec_set_item_in, rec_clear_in, &rec);
sb_t sb = {0};
recorder_entry_t entry = read_entry(&sb);
rec.time = entry.argl >= 1 ? atoll(entry.args[0]) : 0;
entry_free(&entry, &sb);
in3_set_func_time(static_time);
}
11 changes: 11 additions & 0 deletions c/src/cmd/in3/recorder.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#include "../../core/client/client.h"
#include "../../core/util/data.h"
#include "../../core/util/debug.h"
#include "../../core/util/log.h"
#include "../../core/util/mem.h"
#include <stdint.h>
#include <stdlib.h>
#include <string.h>

void recorder_write_start(in3_t* c, char* file);
void recorder_read_start(in3_t* c, char* file);
2 changes: 1 addition & 1 deletion c/src/core/client/cache.c
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ in3_ret_t in3_cache_store_nodelist(in3_t* c, in3_chain_t* chain) {
bb_write_int(bb, chain->nodelist_length);
bb_write_raw_bytes(bb, chain->weights, chain->nodelist_length * sizeof(in3_node_weight_t));

for (int i = 0; i < chain->nodelist_length; i++) {
for (unsigned int i = 0; i < chain->nodelist_length; i++) {
const in3_node_t* n = chain->nodelist + i;
bb_write_int(bb, n->capacity);
bb_write_int(bb, n->index);
Expand Down
Loading

0 comments on commit bac6eb0

Please sign in to comment.