Remove appbill config. (#91)

This commit is contained in:
Ravin Perera
2022-01-27 10:40:04 +05:30
committed by GitHub
parent fe3980bf1e
commit 09510a623c
3 changed files with 56 additions and 67 deletions

View File

@@ -96,8 +96,6 @@ function upgrade() {
return 1
fi
appbill_mode=$(jq '.appbill.mode' $contract_config)
appbill_bin_args=$(jq '.appbill.bin_args' $contract_config)
r_user_input_bytes=$(jq '.round_limits.user_input_bytes' $contract_config)
r_user_output_bytes=$(jq '.round_limits.user_output_bytes' $contract_config)
r_npl_output_bytes=$(jq '.round_limits.npl_output_bytes' $contract_config)
@@ -123,7 +121,6 @@ function upgrade() {
consensus: $consensus,\
npl: $npl,\
max_input_ledger_offset: $max_input_ledger_offset,\
appbill: {mode: $appbill_mode, bin_args: $appbill_bin_args},\
round_limits: {user_input_bytes: $r_user_input_bytes,\
user_output_bytes: $r_user_output_bytes,\
npl_output_bytes: $r_npl_output_bytes,\

View File

@@ -108,10 +108,15 @@ struct hp_user_inputs_collection
size_t count;
};
struct hp_pubkey
{
char data[HP_KEY_SIZE + 1]; // +1 for null char.
};
// Represents a user that is connected to HP cluster.
struct hp_user
{
char pubkey[HP_KEY_SIZE + 1]; // +1 for null char.
struct hp_pubkey pubkey;
int outfd;
struct hp_user_inputs_collection inputs;
};
@@ -119,7 +124,8 @@ struct hp_user
// Represents a node that's part of unl.
struct hp_unl_node
{
char pubkey[HP_KEY_SIZE + 1]; // +1 for null char.
struct hp_pubkey pubkey;
uint64_t active_on;
};
struct hp_users_collection
@@ -129,6 +135,12 @@ struct hp_users_collection
int in_fd;
};
struct hp_pubkey_collection
{
struct hp_pubkey *list;
size_t count;
};
struct hp_unl_collection
{
struct hp_unl_node *list;
@@ -136,12 +148,6 @@ struct hp_unl_collection
int npl_fd;
};
struct hp_appbill_config
{
char *mode;
char *bin_args;
};
struct hp_round_limits_config
{
size_t user_input_bytes;
@@ -155,7 +161,7 @@ struct hp_round_limits_config
struct hp_config
{
char *version;
struct hp_unl_collection unl;
struct hp_pubkey_collection unl;
char *bin_path;
char *bin_args;
char *environment;
@@ -164,7 +170,6 @@ struct hp_config
char *consensus;
char *npl;
uint16_t max_input_ledger_offset;
struct hp_appbill_config appbill;
struct hp_round_limits_config round_limits;
};
@@ -202,7 +207,7 @@ struct hp_config *hp_get_config();
int hp_update_config(const struct hp_config *config);
int hp_update_peers(const char *add_peers[], const size_t add_peers_count, const char *remove_peers[], const size_t remove_peers_count);
void hp_set_config_string(char **config_str, const char *value, const size_t value_size);
void hp_set_config_unl(struct hp_config *config, const struct hp_unl_node *new_unl, const size_t new_unl_count);
void hp_set_config_unl(struct hp_config *config, const struct hp_pubkey *new_unl, const size_t new_unl_count);
void hp_free_config(struct hp_config *config);
void __hp_parse_args_json(const struct json_object_s *object);
@@ -493,20 +498,21 @@ int hp_update_config(const struct hp_config *config)
{
for (size_t i = 0; i < config->unl.count; i++)
{
const size_t pubkey_len = strlen(config->unl.list[i].pubkey);
const char *pubkey = config->unl.list[i].data;
const size_t pubkey_len = strlen(pubkey);
if (pubkey_len == 0)
__HP_UPDATE_CONFIG_ERROR("Unl pubkey cannot be empty.");
if (pubkey_len != HP_KEY_SIZE)
__HP_UPDATE_CONFIG_ERROR("Unl pubkey invalid. Invalid length.");
if (config->unl.list[i].pubkey[0] != 'e' || config->unl.list[i].pubkey[1] != 'd')
if (pubkey[0] != 'e' || pubkey[1] != 'd')
__HP_UPDATE_CONFIG_ERROR("Unl pubkey invalid. Invalid format.");
// Checking the validity of hexadecimal portion. (without 'ed').
for (size_t j = 2; j < HP_KEY_SIZE; j++)
{
const char current_char = config->unl.list[i].pubkey[j];
const char current_char = pubkey[j];
if ((current_char < 'A' || current_char > 'F') && (current_char < 'a' || current_char > 'f') && (current_char < '0' || current_char > '9'))
__HP_UPDATE_CONFIG_ERROR("Unl pubkey invalid. Invalid character.");
}
@@ -564,13 +570,13 @@ void hp_set_config_string(char **config_str, const char *value, const size_t val
/**
* Populates the config unl list with the specified values.
* @param config The config struct to populate the unl to.
* @param new_unl Pointer to the new unl node array.
* @param new_unl_count No. of entries in the new unl node array.
* @param new_unl Pointer to array of unl pubkeys.
* @param new_unl_count No. of entries in the new unl pubkey array.
*/
void hp_set_config_unl(struct hp_config *config, const struct hp_unl_node *new_unl, const size_t new_unl_count)
void hp_set_config_unl(struct hp_config *config, const struct hp_pubkey *new_unl, const size_t new_unl_count)
{
const size_t mem_size = sizeof(struct hp_unl_node) * new_unl_count;
config->unl.list = (struct hp_unl_node *)realloc(config->unl.list, mem_size);
const size_t mem_size = sizeof(struct hp_pubkey) * new_unl_count;
config->unl.list = (struct hp_pubkey *)realloc(config->unl.list, mem_size);
memcpy(config->unl.list, new_unl, mem_size);
config->unl.count = new_unl_count;
}
@@ -588,8 +594,6 @@ void hp_free_config(struct hp_config *config)
__HP_FREE(config->environment);
__HP_FREE(config->consensus);
__HP_FREE(config->npl);
__HP_FREE(config->appbill.mode);
__HP_FREE(config->appbill.bin_args);
__HP_FREE(config);
}
@@ -707,7 +711,7 @@ struct hp_config *__hp_read_from_patch_file(const int fd)
*/
int __hp_write_to_patch_file(const int fd, const struct hp_config *config)
{
struct iovec iov_vec[5];
struct iovec iov_vec[4];
// {version: + newline + 4 spaces => 21;
const size_t version_len = 21 + strlen(config->version);
char version_buf[version_len];
@@ -728,7 +732,7 @@ int __hp_write_to_patch_file(const int fd, const struct hp_config *config)
strncpy(unl_buf + pos, "\n ", 9);
pos += 9;
unl_buf[pos++] = '"';
strncpy(unl_buf + pos, config->unl.list[i].pubkey, HP_KEY_SIZE);
strncpy(unl_buf + pos, config->unl.list[i].data, HP_KEY_SIZE);
pos += HP_KEY_SIZE;
unl_buf[pos++] = '"';
}
@@ -759,15 +763,6 @@ int __hp_write_to_patch_file(const int fd, const struct hp_config *config)
iov_vec[2].iov_base = json_buf;
iov_vec[2].iov_len = json_string_len;
// Appbill field valiues.
const char *appbill_json = " \"appbill\": {\n \"mode\": \"%s\",\n \"bin_args\": \"%s\"\n },\n";
const size_t appbill_json_len = 67 + strlen(config->appbill.mode) + strlen(config->appbill.bin_args);
char appbill_buf[appbill_json_len];
sprintf(appbill_buf, appbill_json, config->appbill.mode, config->appbill.bin_args);
iov_vec[3].iov_base = appbill_buf;
iov_vec[3].iov_len = appbill_json_len;
// Round limits field valies.
const char *round_limits_json = " \"round_limits\": {\n"
@@ -791,11 +786,11 @@ int __hp_write_to_patch_file(const int fd, const struct hp_config *config)
sprintf(round_limits_buf, round_limits_json,
user_input_bytes_str, user_output_bytes_str, npl_output_bytes_str,
proc_cpu_seconds_str, proc_mem_bytes_str, proc_ofd_count_str);
iov_vec[4].iov_base = round_limits_buf;
iov_vec[4].iov_len = round_limits_json_len;
iov_vec[3].iov_base = round_limits_buf;
iov_vec[3].iov_len = round_limits_json_len;
if (ftruncate(fd, 0) == -1 || // Clear any previous content in the file.
pwritev(fd, iov_vec, 5, 0) == -1) // Start writing from begining.
pwritev(fd, iov_vec, 4, 0) == -1) // Start writing from begining.
return -1;
return 0;
@@ -825,14 +820,14 @@ void __hp_populate_patch_from_json_object(struct hp_config *config, const struct
const size_t unl_count = unl_array->length;
config->unl.count = unl_count;
config->unl.list = unl_count ? (struct hp_unl_node *)malloc(sizeof(struct hp_unl_node) * unl_count) : NULL;
config->unl.list = unl_count ? (struct hp_pubkey *)malloc(sizeof(struct hp_pubkey) * unl_count) : NULL;
if (unl_count > 0)
{
struct json_array_element_s *unl_elem = unl_array->start;
for (int i = 0; i < unl_count; i++)
{
__HP_ASSIGN_STRING(config->unl.list[i].pubkey, unl_elem);
__HP_ASSIGN_STRING(config->unl.list[i].data, unl_elem);
unl_elem = unl_elem->next;
}
}
@@ -873,23 +868,6 @@ void __hp_populate_patch_from_json_object(struct hp_config *config, const struct
{
__HP_ASSIGN_CHAR_PTR(config->npl, elem);
}
else if (strcmp(k->string, "appbill") == 0)
{
struct json_object_s *object = (struct json_object_s *)elem->value->payload;
struct json_object_element_s *sub_ele = object->start;
do
{
if (strcmp(sub_ele->name->string, "mode") == 0)
{
__HP_ASSIGN_CHAR_PTR(config->appbill.mode, sub_ele);
}
else if (strcmp(sub_ele->name->string, "bin_args") == 0)
{
__HP_ASSIGN_CHAR_PTR(config->appbill.bin_args, sub_ele);
}
sub_ele = sub_ele->next;
} while (sub_ele);
}
else if (strcmp(k->string, "round_limits") == 0)
{
struct json_object_s *object = (struct json_object_s *)elem->value->payload;
@@ -981,7 +959,7 @@ void __hp_parse_args_json(const struct json_object_s *object)
for (int i = 0; i < user_count; i++)
{
struct hp_user *user = &cctx->users.list[i];
memcpy(user->pubkey, user_elem->name->string, HP_KEY_SIZE);
memcpy(user->pubkey.data, user_elem->name->string, HP_KEY_SIZE);
if (user_elem->value->type == json_type_array)
{
@@ -1020,20 +998,38 @@ void __hp_parse_args_json(const struct json_object_s *object)
}
else if (strcmp(k->string, "unl") == 0)
{
if (elem->value->type == json_type_array)
// unl is an object with pubkeys as keys. Each key contains an object with that node statistics.
if (elem->value->type == json_type_object)
{
const struct json_array_s *unl_array = (struct json_array_s *)elem->value->payload;
const size_t unl_count = unl_array->length;
const struct json_object_s *unl_obj = (struct json_object_s *)elem->value->payload;
const size_t unl_count = unl_obj->length;
cctx->unl.count = unl_count;
cctx->unl.list = unl_count ? (struct hp_unl_node *)malloc(sizeof(struct hp_unl_node) * unl_count) : NULL;
if (unl_count > 0)
{
struct json_array_element_s *unl_elem = unl_array->start;
struct json_object_element_s *unl_elem = unl_obj->start;
for (int i = 0; i < unl_count; i++)
{
__HP_ASSIGN_STRING(cctx->unl.list[i].pubkey, unl_elem);
// Each element(key) is named by the pubkey.
strncpy(cctx->unl.list[i].pubkey.data, unl_elem->name->string, unl_elem->name->string_size);
if (unl_elem->value->type == json_type_object)
{
const struct json_object_s *stat_obj = (struct json_object_s *)unl_elem->value->payload;
struct json_object_element_s *stat_elem = stat_obj->start;
do
{
const struct json_string_s *k = stat_elem->name;
if (strcmp(k->string, "active_on") == 0)
{
__HP_ASSIGN_UINT64(cctx->unl.list[i].active_on, stat_elem);
}
stat_elem = stat_elem->next;
} while (stat_elem);
}
unl_elem = unl_elem->next;
}
}

4
dependencies/hp.cfg vendored
View File

@@ -29,10 +29,6 @@
"consensus": "public",
"npl": "public",
"max_input_ledger_offset": 10,
"appbill": {
"mode": "",
"bin_args": ""
},
"round_limits": {
"user_input_bytes": 0,
"user_output_bytes": 0,