Skip to content

Commit cfe7dec

Browse files
Merge branch 'develop' into 'master'
Develop See merge request in3/c/in3-core!311
2 parents 421c6f9 + da29c4f commit cfe7dec

File tree

15 files changed

+166
-121
lines changed

15 files changed

+166
-121
lines changed

Diff for: c/compiler.cmake

+4
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ endif(MSVC)
1212
if (WASM)
1313
set (CMAKE_C_FLAGS "-Wall -funsigned-char -Wextra -std=c99")
1414
set (CMAKE_CXX_FLAGS "-Wall -funsigned-char -Wextra -std=c99 -D__FILENAME__='\"$(subst ${CMAKE_SOURCE_DIR}/,,$(abspath $<))\"'")
15+
if (CMAKE_BUILD_TYPE MATCHES Debug)
16+
set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fsanitize=undefined,address -fsanitize-minimal-runtime")
17+
set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=undefined,address -fsanitize-minimal-runtime")
18+
endif()
1519
else (WASM)
1620
set(CMAKE_POSITION_INDEPENDENT_CODE ON)
1721
endif(WASM)

Diff for: c/src/api/usn/usn_api.c

+3-3
Original file line numberDiff line numberDiff line change
@@ -349,9 +349,9 @@ static int usn_add_booking(usn_device_t* device, address_t controller, uint64_t
349349
return 0;
350350
}
351351
}
352-
device->bookings = device->bookings
353-
? _realloc(device->bookings, sizeof(usn_booking_t) * (device->num_bookings + 1), sizeof(usn_booking_t) * device->num_bookings)
354-
: _malloc(sizeof(usn_booking_t) * device->num_bookings + 1);
352+
device->bookings = device->bookings
353+
? _realloc(device->bookings, sizeof(usn_booking_t) * (device->num_bookings + 1), sizeof(usn_booking_t) * device->num_bookings)
354+
: _malloc(sizeof(usn_booking_t) * device->num_bookings + 1);
355355
usn_booking_t* booking = device->bookings + device->num_bookings;
356356
booking->rented_from = rented_from;
357357
booking->rented_until = rented_until;

Diff for: c/src/cmd/in3/main.c

+8-1
Original file line numberDiff line numberDiff line change
@@ -708,6 +708,13 @@ int main(int argc, char* argv[]) {
708708
#ifndef USE_CURL
709709
c->flags |= FLAGS_HTTP;
710710
#endif
711+
// handle clear cache opt before initializing cache
712+
for (i = 1; i < argc; i++)
713+
if (strcmp(argv[i], "-fi") == 0) {
714+
recorder_update_cmd(argv[i + 1], &argc, &argv);
715+
break;
716+
}
717+
711718
// handle clear cache opt before initializing cache
712719
for (i = 1; i < argc; i++)
713720
if (strcmp(argv[i], "-ccache") == 0)
@@ -768,7 +775,7 @@ int main(int argc, char* argv[]) {
768775
else if (strcmp(argv[i], "-thr") == 0)
769776
run_test_request = 2;
770777
else if (strcmp(argv[i], "-fo") == 0)
771-
recorder_write_start(c, argv[++i]);
778+
recorder_write_start(c, argv[++i], argc, argv);
772779
else if (strcmp(argv[i], "-fi") == 0)
773780
recorder_read_start(c, argv[++i]);
774781
else if (strcmp(argv[i], "-nl") == 0)

Diff for: c/src/cmd/in3/recorder.c

+93-56
Original file line numberDiff line numberDiff line change
@@ -3,31 +3,31 @@
33
#include "../../core/client/keys.h"
44
#include <math.h>
55
#include <stdio.h>
6-
static void die(char* msg) {
7-
fprintf(stderr, COLORT_RED "Error: %s" COLORT_RESET "\n", msg);
8-
exit(EXIT_FAILURE);
9-
}
6+
7+
typedef struct recorder_entry {
8+
char* name;
9+
char** args;
10+
int argl;
11+
sb_t content;
12+
struct recorder_entry* next;
13+
14+
} recorder_entry_t;
1015

1116
typedef struct {
1217
char* file;
1318
in3_transport_send transport;
1419
FILE* f;
1520
in3_storage_handler_t* cache;
1621
uint64_t time;
22+
recorder_entry_t* queue;
1723
} recorder_t;
1824

19-
typedef struct {
20-
char* name;
21-
char** args;
22-
int argl;
23-
24-
} recorder_entry_t;
25-
2625
static recorder_t rec = {
2726
.file = NULL,
2827
.transport = NULL,
2928
.f = NULL,
3029
.cache = NULL,
30+
.queue = NULL,
3131
.time = 0};
3232

3333
static int rand_out(void* s) {
@@ -38,64 +38,89 @@ static int rand_out(void* s) {
3838
return r;
3939
}
4040

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

44-
char buffer[1024];
45+
static recorder_entry_t* read_one_entry() {
46+
recorder_entry_t* entry = NULL;
47+
char buffer[1024];
4548
while (fgets(buffer, 1023, rec.f)) {
4649
int l = strlen(buffer);
4750
if (buffer[l - 1] == '\n')
4851
buffer[--l] = 0;
4952
if (!l) break;
50-
if (!entry.name) {
51-
char* ptr = strtok(buffer + 3, " ");
52-
entry.name = _strdupn(ptr, -1);
53+
if (!entry) {
54+
entry = _calloc(sizeof(recorder_entry_t), 1);
55+
char* ptr = strtok(buffer + 3, " ");
56+
entry->name = _strdupn(ptr, -1);
5357
while ((ptr = strtok(NULL, " "))) {
54-
entry.args = entry.argl ? _realloc(entry.args, sizeof(char*) * (entry.argl + 1), sizeof(char*) * entry.argl) : _malloc(sizeof(char*));
55-
entry.args[entry.argl++] = _strdupn(ptr, -1);
58+
entry->args = entry->argl ? _realloc(entry->args, sizeof(char*) * (entry->argl + 1), sizeof(char*) * entry->argl) : _malloc(sizeof(char*));
59+
entry->args[entry->argl++] = _strdupn(ptr, -1);
5660
}
5761
} else
58-
sb_add_chars(sb, buffer);
62+
sb_add_chars(&entry->content, buffer);
5963
}
60-
6164
return entry;
6265
}
63-
static void entry_free(recorder_entry_t* e, sb_t* sb) {
66+
67+
recorder_entry_t* next_entry(const char* type, const char* firs_arg) {
68+
69+
recorder_entry_t *last = rec.queue, *end = NULL;
70+
for (recorder_entry_t* n = last; n; last = n, n = n->next) {
71+
if (match((end = n), type, firs_arg)) {
72+
if (last == n)
73+
rec.queue = n->next;
74+
else
75+
last->next = n->next;
76+
return n;
77+
}
78+
}
79+
80+
do {
81+
if (match((last = read_one_entry()), type, firs_arg))
82+
return last;
83+
if (!last) {
84+
fprintf(stderr, COLORT_RED "Error: expected entry %s %s but did not find it!" COLORT_RESET "\n", type, firs_arg ? firs_arg : "");
85+
exit(EXIT_FAILURE);
86+
}
87+
if (end)
88+
end->next = last;
89+
else
90+
rec.queue = last;
91+
end = last;
92+
93+
} while (true);
94+
}
95+
96+
static void entry_free(recorder_entry_t* e) {
6497
if (e->name) _free(e->name);
6598
for (int i = 0; i < e->argl; i++) _free(e->args[i]);
6699
_free(e->args);
67-
if (sb && sb->data) _free(sb->data);
68-
if (sb) *sb = (sb_t){0};
100+
if (e->content.data) _free(e->content.data);
101+
_free(e);
69102
}
70103
static int rand_in(void* s) {
71104
UNUSED_VAR(s);
72-
sb_t sb = {0};
73-
recorder_entry_t entry = read_entry(&sb);
74-
if (!entry.name || strcmp(entry.name, "rand")) die("expected rand in recorder!");
75-
if (entry.argl != 1) die("expect one arg for random");
76-
int r = atoi(entry.args[0]);
77-
entry_free(&entry, &sb);
78-
105+
recorder_entry_t* entry = next_entry("rand", NULL);
106+
int r = atoi(entry->args[0]);
107+
entry_free(entry);
79108
return r;
80109
}
81110

82111
static in3_ret_t recorder_transport_in(in3_request_t* req) {
83-
sb_t sb = {0};
84-
recorder_entry_t entry = {0};
112+
85113
if (req->action == REQ_ACTION_SEND) {
86-
entry = read_entry(&sb);
87-
if (!entry.name || strcmp(entry.name, "request")) die("expected request in recorder!");
88-
entry_free(&entry, &sb);
114+
entry_free(next_entry("request", NULL));
89115
req->cptr = &rec;
90116
}
91117
if (req->action != REQ_ACTION_CLEANUP) {
92-
entry = read_entry(&sb);
93-
if (!entry.name || strcmp(entry.name, "response")) die("expected response in recorder!");
94-
in3_response_t* r = req->ctx->raw_response + atoi(entry.args[0]);
95-
sb_add_chars(&r->data, sb.data);
96-
r->state = atoi(entry.args[3]);
97-
r->time = atoi(entry.args[4]);
98-
entry_free(&entry, &sb);
118+
recorder_entry_t* entry = next_entry("response", d_get_stringk(req->ctx->requests[0], K_METHOD));
119+
in3_response_t* r = req->ctx->raw_response + atoi(entry->args[1]);
120+
sb_add_chars(&r->data, entry->content.data);
121+
r->state = atoi(entry->args[3]);
122+
r->time = atoi(entry->args[4]);
123+
entry_free(entry);
99124
}
100125

101126
return 0;
@@ -116,7 +141,7 @@ static in3_ret_t recorder_transport_out(in3_request_t* req) {
116141
for (int i = 0; m; i++, m = m->next) {
117142
in3_response_t* r = req->ctx->raw_response + i;
118143
if (r->time) {
119-
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);
144+
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);
120145
char* data = format_json(r->data.data ? r->data.data : "");
121146
fprintf(rec.f, "%s\n\n", data);
122147
fflush(rec.f);
@@ -130,13 +155,9 @@ static in3_ret_t recorder_transport_out(in3_request_t* req) {
130155
bytes_t* rec_get_item_in(void* cptr, const char* key) {
131156
UNUSED_VAR(cptr);
132157
UNUSED_VAR(key);
133-
sb_t sb = {0};
134-
recorder_entry_t entry = read_entry(&sb);
135-
if (!entry.name || strcmp(entry.name, "cache")) die("expected cache in recorder!");
136-
if (entry.argl != 2) die("expect 2 args for cache");
137-
if (strcmp(key, entry.args[0])) die("wrong cache key");
138-
bytes_t* found = atoi(entry.args[1]) ? hex_to_new_bytes(sb.data, sb.len) : NULL;
139-
entry_free(&entry, &sb);
158+
recorder_entry_t* entry = next_entry("cache", key);
159+
bytes_t* found = atoi(entry->args[1]) ? hex_to_new_bytes(entry->content.data, entry->content.len) : NULL;
160+
entry_free(entry);
140161

141162
return found;
142163
}
@@ -177,13 +198,16 @@ uint64_t static_time(void* t) {
177198
return rec.time;
178199
}
179200

180-
void recorder_write_start(in3_t* c, char* file) {
201+
void recorder_write_start(in3_t* c, char* file, int argc, char* argv[]) {
181202
rec.file = file;
182203
rec.transport = c->transport;
183204
c->transport = recorder_transport_out;
184205
rec.f = fopen(file, "w");
185206
rec.cache = c->cache;
186207
in3_set_func_rand(rand_out);
208+
fprintf(rec.f, ":: cmd");
209+
for (int i = 0; i < argc; i++) fprintf(rec.f, " %s", strcmp(argv[i], "-fo") ? argv[i] : "-fi");
210+
fprintf(rec.f, "\n\n");
187211
in3_set_storage_handler(c, rec_get_item_out, rec_set_item_out, rec_clear_out, &rec);
188212
fprintf(rec.f, ":: time %u\n\n", (uint32_t) in3_time(NULL));
189213
}
@@ -195,9 +219,22 @@ void recorder_read_start(in3_t* c, char* file) {
195219
rec.f = fopen(file, "r");
196220
in3_set_func_rand(rand_in);
197221
in3_set_storage_handler(c, rec_get_item_in, rec_set_item_in, rec_clear_in, &rec);
198-
sb_t sb = {0};
199-
recorder_entry_t entry = read_entry(&sb);
200-
rec.time = entry.argl >= 1 ? atoll(entry.args[0]) : 0;
201-
entry_free(&entry, &sb);
222+
recorder_entry_t* entry = next_entry("time", NULL);
223+
rec.time = entry->argl >= 1 ? atoll(entry->args[0]) : 0;
224+
entry_free(entry);
202225
in3_set_func_time(static_time);
203226
}
227+
228+
void recorder_update_cmd(char* file, int* argc, char** argv[]) {
229+
rec.file = file;
230+
rec.f = fopen(file, "r");
231+
recorder_entry_t* entry = next_entry("cmd", NULL);
232+
*argc = entry->argl;
233+
*argv = entry->args;
234+
for (int i = 0; i < entry->argl; i++) {
235+
if (strcmp(entry->args[i], "-fi") == 0) entry->args[i + 1] = file;
236+
}
237+
fclose(rec.f);
238+
rec.f = NULL;
239+
rec.queue = NULL;
240+
}

Diff for: c/src/cmd/in3/recorder.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,6 @@
77
#include <stdlib.h>
88
#include <string.h>
99

10-
void recorder_write_start(in3_t* c, char* file);
10+
void recorder_write_start(in3_t* c, char* file, int argc, char* argv[]);
1111
void recorder_read_start(in3_t* c, char* file);
12+
void recorder_update_cmd(char* file, int* argc, char** argv[]);

Diff for: c/src/core/client/client.h

+3-3
Original file line numberDiff line numberDiff line change
@@ -280,10 +280,10 @@ typedef struct in3_storage_handler {
280280
void* cptr; /**< custom pointer which will be passed to functions */
281281
} in3_storage_handler_t;
282282

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

288288
/** type of the requested signature */
289289
typedef enum {

Diff for: c/src/core/client/client_init.c

+3-3
Original file line numberDiff line numberDiff line change
@@ -383,9 +383,9 @@ in3_ret_t in3_client_add_node(in3_t* c, chain_id_t chain_id, char* url, in3_node
383383
chain->nodelist = chain->nodelist
384384
? _realloc(chain->nodelist, sizeof(in3_node_t) * (chain->nodelist_length + 1), sizeof(in3_node_t) * chain->nodelist_length)
385385
: _calloc(chain->nodelist_length + 1, sizeof(in3_node_t));
386-
chain->weights = chain->weights
387-
? _realloc(chain->weights, sizeof(in3_node_weight_t) * (chain->nodelist_length + 1), sizeof(in3_node_weight_t) * chain->nodelist_length)
388-
: _calloc(chain->nodelist_length + 1, sizeof(in3_node_weight_t));
386+
chain->weights = chain->weights
387+
? _realloc(chain->weights, sizeof(in3_node_weight_t) * (chain->nodelist_length + 1), sizeof(in3_node_weight_t) * chain->nodelist_length)
388+
: _calloc(chain->nodelist_length + 1, sizeof(in3_node_weight_t));
389389
if (!chain->nodelist || !chain->weights) return IN3_ENOMEM;
390390
node = chain->nodelist + chain->nodelist_length;
391391
node->address = b_new(address, 20);

Diff for: c/src/core/client/context.c

+7-5
Original file line numberDiff line numberDiff line change
@@ -82,11 +82,13 @@ char* ctx_get_error_data(in3_ctx_t* ctx) {
8282
}
8383

8484
char* ctx_get_response_data(in3_ctx_t* ctx) {
85-
str_range_t rr = d_to_json(ctx->responses[0]), rin3;
86-
if ((ctx->client->flags & FLAGS_KEEP_IN3) == 0 && (rin3 = d_to_json(d_get(ctx->responses[0], K_IN3))).data) {
87-
while (*rin3.data != ',' && rin3.data > rr.data) rin3.data--;
88-
*rin3.data = '}';
89-
rr.len = rin3.data - rr.data + 1;
85+
str_range_t rr = d_to_json(ctx->responses[0]);
86+
char* start = NULL;
87+
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) {
88+
while (*start != ',' && start > rr.data) start--;
89+
char* res = _strdupn(rr.data, start - rr.data + 1);
90+
res[start - rr.data] = '}';
91+
return res;
9092
}
9193
return _strdupn(rr.data, rr.len);
9294
}

Diff for: c/src/core/client/execute.c

+2-1
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,8 @@ NONULL static in3_ret_t pick_signers(in3_ctx_t* ctx, d_token_t* request) {
107107
return IN3_OK;
108108

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

112113
if (total_sig_cnt) {
113114
node_match_t* signer_nodes = NULL;

Diff for: c/src/core/client/nodelist.c

+3-3
Original file line numberDiff line numberDiff line change
@@ -362,9 +362,9 @@ IN3_EXPORT_TEST bool in3_node_props_match(const in3_node_props_t np_config, cons
362362
}
363363

364364
uint32_t in3_node_calculate_weight(in3_node_weight_t* n, uint32_t capa, uint64_t now) {
365-
const uint32_t avg = (n->response_count > 4 && n->total_response_time)
366-
? (n->total_response_time / n->response_count)
367-
: (10000 / (max(capa, 100) + 100));
365+
const uint32_t avg = (n->response_count > 4 && n->total_response_time)
366+
? (n->total_response_time / n->response_count)
367+
: (10000 / (max(capa, 100) + 100));
368368
const uint32_t blacklist_factor = ((now - n->blacklisted_until) < BLACKLISTWEIGHT)
369369
? ((now - n->blacklisted_until) * 100 / (BLACKLISTWEIGHT))
370370
: 100;

Diff for: c/src/core/util/data.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ d_key_t keyn(const char* c, const size_t len) {
9696
return kn->key;
9797
kn = kn->next;
9898
}
99-
val = __keynames_len;
99+
val = __keynames_len;
100100
#endif
101101
return val;
102102
}

0 commit comments

Comments
 (0)