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!311
  • Loading branch information
simon-jentzsch committed Jul 12, 2020
2 parents 421c6f9 + da29c4f commit cfe7dec
Show file tree
Hide file tree
Showing 15 changed files with 166 additions and 121 deletions.
4 changes: 4 additions & 0 deletions c/compiler.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ endif(MSVC)
if (WASM)
set (CMAKE_C_FLAGS "-Wall -funsigned-char -Wextra -std=c99")
set (CMAKE_CXX_FLAGS "-Wall -funsigned-char -Wextra -std=c99 -D__FILENAME__='\"$(subst ${CMAKE_SOURCE_DIR}/,,$(abspath $<))\"'")
if (CMAKE_BUILD_TYPE MATCHES Debug)
set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fsanitize=undefined,address -fsanitize-minimal-runtime")
set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=undefined,address -fsanitize-minimal-runtime")
endif()
else (WASM)
set(CMAKE_POSITION_INDEPENDENT_CODE ON)
endif(WASM)
Expand Down
6 changes: 3 additions & 3 deletions c/src/api/usn/usn_api.c
Original file line number Diff line number Diff line change
Expand Up @@ -349,9 +349,9 @@ static int usn_add_booking(usn_device_t* device, address_t controller, uint64_t
return 0;
}
}
device->bookings = device->bookings
? _realloc(device->bookings, sizeof(usn_booking_t) * (device->num_bookings + 1), sizeof(usn_booking_t) * device->num_bookings)
: _malloc(sizeof(usn_booking_t) * device->num_bookings + 1);
device->bookings = device->bookings
? _realloc(device->bookings, sizeof(usn_booking_t) * (device->num_bookings + 1), sizeof(usn_booking_t) * device->num_bookings)
: _malloc(sizeof(usn_booking_t) * device->num_bookings + 1);
usn_booking_t* booking = device->bookings + device->num_bookings;
booking->rented_from = rented_from;
booking->rented_until = rented_until;
Expand Down
9 changes: 8 additions & 1 deletion c/src/cmd/in3/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -708,6 +708,13 @@ int main(int argc, char* argv[]) {
#ifndef USE_CURL
c->flags |= FLAGS_HTTP;
#endif
// handle clear cache opt before initializing cache
for (i = 1; i < argc; i++)
if (strcmp(argv[i], "-fi") == 0) {
recorder_update_cmd(argv[i + 1], &argc, &argv);
break;
}

// handle clear cache opt before initializing cache
for (i = 1; i < argc; i++)
if (strcmp(argv[i], "-ccache") == 0)
Expand Down Expand Up @@ -768,7 +775,7 @@ int main(int argc, char* argv[]) {
else if (strcmp(argv[i], "-thr") == 0)
run_test_request = 2;
else if (strcmp(argv[i], "-fo") == 0)
recorder_write_start(c, argv[++i]);
recorder_write_start(c, argv[++i], argc, argv);
else if (strcmp(argv[i], "-fi") == 0)
recorder_read_start(c, argv[++i]);
else if (strcmp(argv[i], "-nl") == 0)
Expand Down
149 changes: 93 additions & 56 deletions c/src/cmd/in3/recorder.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,31 +3,31 @@
#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 recorder_entry {
char* name;
char** args;
int argl;
sb_t content;
struct recorder_entry* next;

} recorder_entry_t;

typedef struct {
char* file;
in3_transport_send transport;
FILE* f;
in3_storage_handler_t* cache;
uint64_t time;
recorder_entry_t* queue;
} 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,
.queue = NULL,
.time = 0};

static int rand_out(void* s) {
Expand All @@ -38,64 +38,89 @@ static int rand_out(void* s) {
return r;
}

recorder_entry_t read_entry(sb_t* sb) {
recorder_entry_t entry = {0};
static inline bool match(recorder_entry_t* entry, const char* type, const char* first_arg) {
return entry && entry->name && strcmp(type, entry->name) == 0 && (first_arg == NULL || (entry->argl && strcmp(entry->args[0], first_arg) == 0));
}

char buffer[1024];
static recorder_entry_t* read_one_entry() {
recorder_entry_t* entry = NULL;
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);
if (!entry) {
entry = _calloc(sizeof(recorder_entry_t), 1);
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);
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);
sb_add_chars(&entry->content, buffer);
}

return entry;
}
static void entry_free(recorder_entry_t* e, sb_t* sb) {

recorder_entry_t* next_entry(const char* type, const char* firs_arg) {

recorder_entry_t *last = rec.queue, *end = NULL;
for (recorder_entry_t* n = last; n; last = n, n = n->next) {
if (match((end = n), type, firs_arg)) {
if (last == n)
rec.queue = n->next;
else
last->next = n->next;
return n;
}
}

do {
if (match((last = read_one_entry()), type, firs_arg))
return last;
if (!last) {
fprintf(stderr, COLORT_RED "Error: expected entry %s %s but did not find it!" COLORT_RESET "\n", type, firs_arg ? firs_arg : "");
exit(EXIT_FAILURE);
}
if (end)
end->next = last;
else
rec.queue = last;
end = last;

} while (true);
}

static void entry_free(recorder_entry_t* e) {
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};
if (e->content.data) _free(e->content.data);
_free(e);
}
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);

recorder_entry_t* entry = next_entry("rand", NULL);
int r = atoi(entry->args[0]);
entry_free(entry);
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);
entry_free(next_entry("request", NULL));
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);
recorder_entry_t* entry = next_entry("response", d_get_stringk(req->ctx->requests[0], K_METHOD));
in3_response_t* r = req->ctx->raw_response + atoi(entry->args[1]);
sb_add_chars(&r->data, entry->content.data);
r->state = atoi(entry->args[3]);
r->time = atoi(entry->args[4]);
entry_free(entry);
}

return 0;
Expand All @@ -116,7 +141,7 @@ static in3_ret_t recorder_transport_out(in3_request_t* req) {
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);
fprintf(rec.f, ":: response %s %i %s %i %i\n", d_get_stringk(req->ctx->requests[0], K_METHOD), i, 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);
Expand All @@ -130,13 +155,9 @@ static in3_ret_t recorder_transport_out(in3_request_t* req) {
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);
recorder_entry_t* entry = next_entry("cache", key);
bytes_t* found = atoi(entry->args[1]) ? hex_to_new_bytes(entry->content.data, entry->content.len) : NULL;
entry_free(entry);

return found;
}
Expand Down Expand Up @@ -177,13 +198,16 @@ uint64_t static_time(void* t) {
return rec.time;
}

void recorder_write_start(in3_t* c, char* file) {
void recorder_write_start(in3_t* c, char* file, int argc, char* argv[]) {
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);
fprintf(rec.f, ":: cmd");
for (int i = 0; i < argc; i++) fprintf(rec.f, " %s", strcmp(argv[i], "-fo") ? argv[i] : "-fi");
fprintf(rec.f, "\n\n");
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));
}
Expand All @@ -195,9 +219,22 @@ void recorder_read_start(in3_t* c, char* file) {
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);
recorder_entry_t* entry = next_entry("time", NULL);
rec.time = entry->argl >= 1 ? atoll(entry->args[0]) : 0;
entry_free(entry);
in3_set_func_time(static_time);
}

void recorder_update_cmd(char* file, int* argc, char** argv[]) {
rec.file = file;
rec.f = fopen(file, "r");
recorder_entry_t* entry = next_entry("cmd", NULL);
*argc = entry->argl;
*argv = entry->args;
for (int i = 0; i < entry->argl; i++) {
if (strcmp(entry->args[i], "-fi") == 0) entry->args[i + 1] = file;
}
fclose(rec.f);
rec.f = NULL;
rec.queue = NULL;
}
3 changes: 2 additions & 1 deletion c/src/cmd/in3/recorder.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@
#include <stdlib.h>
#include <string.h>

void recorder_write_start(in3_t* c, char* file);
void recorder_write_start(in3_t* c, char* file, int argc, char* argv[]);
void recorder_read_start(in3_t* c, char* file);
void recorder_update_cmd(char* file, int* argc, char** argv[]);
6 changes: 3 additions & 3 deletions c/src/core/client/client.h
Original file line number Diff line number Diff line change
Expand Up @@ -280,10 +280,10 @@ typedef struct in3_storage_handler {
void* cptr; /**< custom pointer which will be passed to functions */
} in3_storage_handler_t;

#define IN3_SIGN_ERR_REJECTED -1 /**< return value used by the signer if the the signature-request was rejected. */
#define IN3_SIGN_ERR_REJECTED -1 /**< return value used by the signer if the the signature-request was rejected. */
#define IN3_SIGN_ERR_ACCOUNT_NOT_FOUND -2 /**< return value used by the signer if the requested account was not found. */
#define IN3_SIGN_ERR_INVALID_MESSAGE -3 /**< return value used by the signer if the message was invalid. */
#define IN3_SIGN_ERR_GENERAL_ERROR -4 /**< return value used by the signer for unspecified errors. */
#define IN3_SIGN_ERR_INVALID_MESSAGE -3 /**< return value used by the signer if the message was invalid. */
#define IN3_SIGN_ERR_GENERAL_ERROR -4 /**< return value used by the signer for unspecified errors. */

/** type of the requested signature */
typedef enum {
Expand Down
6 changes: 3 additions & 3 deletions c/src/core/client/client_init.c
Original file line number Diff line number Diff line change
Expand Up @@ -383,9 +383,9 @@ in3_ret_t in3_client_add_node(in3_t* c, chain_id_t chain_id, char* url, in3_node
chain->nodelist = chain->nodelist
? _realloc(chain->nodelist, sizeof(in3_node_t) * (chain->nodelist_length + 1), sizeof(in3_node_t) * chain->nodelist_length)
: _calloc(chain->nodelist_length + 1, sizeof(in3_node_t));
chain->weights = chain->weights
? _realloc(chain->weights, sizeof(in3_node_weight_t) * (chain->nodelist_length + 1), sizeof(in3_node_weight_t) * chain->nodelist_length)
: _calloc(chain->nodelist_length + 1, sizeof(in3_node_weight_t));
chain->weights = chain->weights
? _realloc(chain->weights, sizeof(in3_node_weight_t) * (chain->nodelist_length + 1), sizeof(in3_node_weight_t) * chain->nodelist_length)
: _calloc(chain->nodelist_length + 1, sizeof(in3_node_weight_t));
if (!chain->nodelist || !chain->weights) return IN3_ENOMEM;
node = chain->nodelist + chain->nodelist_length;
node->address = b_new(address, 20);
Expand Down
12 changes: 7 additions & 5 deletions c/src/core/client/context.c
Original file line number Diff line number Diff line change
Expand Up @@ -82,11 +82,13 @@ char* ctx_get_error_data(in3_ctx_t* ctx) {
}

char* ctx_get_response_data(in3_ctx_t* ctx) {
str_range_t rr = d_to_json(ctx->responses[0]), rin3;
if ((ctx->client->flags & FLAGS_KEEP_IN3) == 0 && (rin3 = d_to_json(d_get(ctx->responses[0], K_IN3))).data) {
while (*rin3.data != ',' && rin3.data > rr.data) rin3.data--;
*rin3.data = '}';
rr.len = rin3.data - rr.data + 1;
str_range_t rr = d_to_json(ctx->responses[0]);
char* start = NULL;
if ((ctx->client->flags & FLAGS_KEEP_IN3) == 0 && (start = d_to_json(d_get(ctx->responses[0], K_IN3)).data) && start < rr.data + rr.len) {
while (*start != ',' && start > rr.data) start--;
char* res = _strdupn(rr.data, start - rr.data + 1);
res[start - rr.data] = '}';
return res;
}
return _strdupn(rr.data, rr.len);
}
Expand Down
3 changes: 2 additions & 1 deletion c/src/core/client/execute.c
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,8 @@ NONULL static in3_ret_t pick_signers(in3_ctx_t* ctx, d_token_t* request) {
return IN3_OK;

// For nodeList request, we always ask for proof & atleast one signature
uint8_t total_sig_cnt = c->signature_count ? c->signature_count : auto_ask_sig(ctx) ? 1 : 0;
uint8_t total_sig_cnt = c->signature_count ? c->signature_count : auto_ask_sig(ctx) ? 1
: 0;

if (total_sig_cnt) {
node_match_t* signer_nodes = NULL;
Expand Down
6 changes: 3 additions & 3 deletions c/src/core/client/nodelist.c
Original file line number Diff line number Diff line change
Expand Up @@ -362,9 +362,9 @@ IN3_EXPORT_TEST bool in3_node_props_match(const in3_node_props_t np_config, cons
}

uint32_t in3_node_calculate_weight(in3_node_weight_t* n, uint32_t capa, uint64_t now) {
const uint32_t avg = (n->response_count > 4 && n->total_response_time)
? (n->total_response_time / n->response_count)
: (10000 / (max(capa, 100) + 100));
const uint32_t avg = (n->response_count > 4 && n->total_response_time)
? (n->total_response_time / n->response_count)
: (10000 / (max(capa, 100) + 100));
const uint32_t blacklist_factor = ((now - n->blacklisted_until) < BLACKLISTWEIGHT)
? ((now - n->blacklisted_until) * 100 / (BLACKLISTWEIGHT))
: 100;
Expand Down
2 changes: 1 addition & 1 deletion c/src/core/util/data.c
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ d_key_t keyn(const char* c, const size_t len) {
return kn->key;
kn = kn->next;
}
val = __keynames_len;
val = __keynames_len;
#endif
return val;
}
Expand Down
Loading

0 comments on commit cfe7dec

Please sign in to comment.