From 10cf1ac5b61f9bba202328c20cf315f5bae96d12 Mon Sep 17 00:00:00 2001 From: sg777 Date: Wed, 15 Nov 2023 10:48:05 +0530 Subject: [PATCH 1/7] Hnadling processing new block --- privatebet/bet.c | 17 +++++++---------- privatebet/dealer.c | 3 +++ 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/privatebet/bet.c b/privatebet/bet.c index 0551ce97..b20c8dc0 100644 --- a/privatebet/bet.c +++ b/privatebet/bet.c @@ -455,19 +455,16 @@ static void bet_start(int argc, char **argv) { int32_t retval = OK; + + if ((strcmp(argv[1], "newblock") == 0) && (argc == 3)) { + process_block(argv[2]); + return; + } + bet_set_unique_id(); if (argc < 2) { bet_command_info(); - exit(0); - } - - if ((strcmp(argv[1], "newblock") == 0) && (argc == 3)) { - dlg_info("A new block arrived"); - if (bet_is_new_block_set()) { - dlg_info("Processing new block"); - process_block(argv[2]); - exit(0); - } + return; } bet_parse_blockchain_config_ini_file(); diff --git a/privatebet/dealer.c b/privatebet/dealer.c index b291c860..07919dd9 100644 --- a/privatebet/dealer.c +++ b/privatebet/dealer.c @@ -265,6 +265,9 @@ int32_t dealer_init(struct table t) { int32_t retval = OK, game_state; + if(is_id_exists(t.dealer_id, 0)) { + //If dealer doesn't exist, then add it to dealers.poker.chips10sec@ + } //Updating the dealer id with t_table_info retval = update_t_info_at_dealer(t); if (retval) { From 9768183cc0e7fd0c582d19660e1d54fda23b7afd Mon Sep 17 00:00:00 2001 From: sg777 Date: Thu, 16 Nov 2023 10:58:28 +0530 Subject: [PATCH 2/7] Skip new block processing in bet if its not configured --- privatebet/vdxf.c | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/privatebet/vdxf.c b/privatebet/vdxf.c index 75516b74..a37704fc 100644 --- a/privatebet/vdxf.c +++ b/privatebet/vdxf.c @@ -1153,6 +1153,11 @@ void process_block(char *blockhash) char verus_addr[1][100] = { CASHIERS_ID }; cJSON *blockjson = NULL, *payin_tx_data = NULL; + if(!bet_is_new_block_set()) { + dlg_info("Flag to process new block info is not set in blockchain_config.ini"); + return; + } + blockjson = cJSON_CreateObject(); blockjson = chips_get_block_from_block_hash(blockhash); From a6e5fd9cea6d74748df69d9ea0ddb7059c3a7583 Mon Sep 17 00:00:00 2001 From: sg777 Date: Thu, 16 Nov 2023 11:00:12 +0530 Subject: [PATCH 3/7] Adding header file --- privatebet/vdxf.c | 1 + 1 file changed, 1 insertion(+) diff --git a/privatebet/vdxf.c b/privatebet/vdxf.c index a37704fc..caa52a9d 100644 --- a/privatebet/vdxf.c +++ b/privatebet/vdxf.c @@ -5,6 +5,7 @@ #include "err.h" #include "game.h" #include "storage.h" +#include "config.h" struct table player_t = { 0 }; char T_PLAYER_KEYS[9][128] = { T_PLAYER1_KEY, T_PLAYER2_KEY, T_PLAYER3_KEY, T_PLAYER4_KEY, T_PLAYER5_KEY, From 0b989d301362a40420e9d3cdafc99989b5c447ec Mon Sep 17 00:00:00 2001 From: sg777 Date: Thu, 16 Nov 2023 06:32:02 +0100 Subject: [PATCH 4/7] Running clang --- privatebet/bet.c | 7 +++---- privatebet/dealer.c | 2 +- privatebet/help.c | 6 +++--- privatebet/vdxf.c | 21 ++++++++++----------- privatebet/vdxf.h | 1 - 5 files changed, 17 insertions(+), 20 deletions(-) diff --git a/privatebet/bet.c b/privatebet/bet.c index b20c8dc0..f42be707 100644 --- a/privatebet/bet.c +++ b/privatebet/bet.c @@ -455,12 +455,11 @@ static void bet_start(int argc, char **argv) { int32_t retval = OK; - if ((strcmp(argv[1], "newblock") == 0) && (argc == 3)) { process_block(argv[2]); return; } - + bet_set_unique_id(); if (argc < 2) { bet_command_info(); @@ -583,13 +582,13 @@ static void bet_start(int argc, char **argv) } else if (strcmp(argv[1], "list_tables") == 0) { list_tables(); } else if ((strcmp(argv[1], "reset_id") == 0) && (argc == 3)) { - if(is_id_exists(argv[2], 0)) + if (is_id_exists(argv[2], 0)) update_cmm(argv[2], NULL); } else { bet_command_info(); } end: - if(retval != OK) { + if (retval != OK) { dlg_info("%s::%d::Exiting with the error ::%s", __func__, __LINE__, bet_err_str(retval)); } } diff --git a/privatebet/dealer.c b/privatebet/dealer.c index 07919dd9..955ecf77 100644 --- a/privatebet/dealer.c +++ b/privatebet/dealer.c @@ -265,7 +265,7 @@ int32_t dealer_init(struct table t) { int32_t retval = OK, game_state; - if(is_id_exists(t.dealer_id, 0)) { + if (is_id_exists(t.dealer_id, 0)) { //If dealer doesn't exist, then add it to dealers.poker.chips10sec@ } //Updating the dealer id with t_table_info diff --git a/privatebet/help.c b/privatebet/help.c index c26bca86..c48ee174 100644 --- a/privatebet/help.c +++ b/privatebet/help.c @@ -213,7 +213,7 @@ void bet_help_vdxf_command_usage() "\nExample: \n" "./bet add_dealer \n" "Note: Before adding dealer ID or name to dealers make sure ID exists\n"); - + dlg_info("\nCommand: \n" "list_dealers \n" "\nDescription: \n" @@ -223,7 +223,7 @@ void bet_help_vdxf_command_usage() "\nExample: \n" "./bet list_dealers\n"); - dlg_info("\nCommand: \n" + dlg_info("\nCommand: \n" "list_tables \n" "\nDescription: \n" "Lists all the tables that are hosted by all the dealers\n" @@ -232,7 +232,7 @@ void bet_help_vdxf_command_usage() "\nExample: \n" "./bet list_tables\n"); - dlg_info("\nCommand: \n" + dlg_info("\nCommand: \n" "reset_id \n" "\nDescription: \n" "Reset the contentmultimap of an ID, meaning set it to NULL\n" diff --git a/privatebet/vdxf.c b/privatebet/vdxf.c index caa52a9d..db23ae9c 100644 --- a/privatebet/vdxf.c +++ b/privatebet/vdxf.c @@ -797,7 +797,7 @@ bool check_if_enough_funds_avail(char *table_id) balance = chips_get_balance(); if (balance > min_stake + RESERVE_AMOUNT) return true; - } + } return false; } @@ -1154,11 +1154,11 @@ void process_block(char *blockhash) char verus_addr[1][100] = { CASHIERS_ID }; cJSON *blockjson = NULL, *payin_tx_data = NULL; - if(!bet_is_new_block_set()) { + if (!bet_is_new_block_set()) { dlg_info("Flag to process new block info is not set in blockchain_config.ini"); return; - } - + } + blockjson = cJSON_CreateObject(); blockjson = chips_get_block_from_block_hash(blockhash); @@ -1196,24 +1196,23 @@ void list_dealers() dealers = cJSON_CreateObject(); dealers = get_cJSON_from_id_key("dealers", DEALERS_KEY); - if(dealers) { + if (dealers) { dlg_info("Available dealers::%s\n", cJSON_Print(dealers)); - } - + } } void list_tables() { cJSON *dealers = NULL, *dealers_arr = NULL; - + dealers = cJSON_CreateObject(); dealers = get_cJSON_from_id_key("dealers", DEALERS_KEY); - + dealers_arr = cJSON_GetObjectItem(dealers, "dealers"); - for(int i=0; i< cJSON_GetArraySize(dealers_arr); i++) { + for (int i = 0; i < cJSON_GetArraySize(dealers_arr); i++) { dlg_info("dealer_id::%s", jstri(dealers_arr, i)); cJSON *table_info = get_cJSON_from_id_key(jstri(dealers_arr, i), T_TABLE_INFO_KEY); - if(table_info) { + if (table_info) { dlg_info("%s", cJSON_Print(table_info)); } } diff --git a/privatebet/vdxf.h b/privatebet/vdxf.h index 272fcc86..e8ae0b56 100644 --- a/privatebet/vdxf.h +++ b/privatebet/vdxf.h @@ -91,7 +91,6 @@ under chips as sg777.chips@ which basically been used to hold the tokens. #define CASHIERS_ID "cashiers.poker.chips10sec@" #define POKER_CHIPS_VDXF_ID "poker.chips10sec@" - #define ID_UPDATE_ESTIMATE_NO 50 #define RESERVE_AMOUNT ID_UPDATE_ESTIMATE_NO *chips_tx_fee From e322ee06f25db6f254d433f4dda9a1aa0584c17f Mon Sep 17 00:00:00 2001 From: sg777 Date: Thu, 16 Nov 2023 17:53:33 +0530 Subject: [PATCH 5/7] Adding more checks to process block --- privatebet/vdxf.c | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/privatebet/vdxf.c b/privatebet/vdxf.c index db23ae9c..c8e13697 100644 --- a/privatebet/vdxf.c +++ b/privatebet/vdxf.c @@ -1162,17 +1162,24 @@ void process_block(char *blockhash) blockjson = cJSON_CreateObject(); blockjson = chips_get_block_from_block_hash(blockhash); - if (blockjson == NULL) - goto end; - + if (blockjson == NULL) { + dlg_error("Failed to get block info from blockhash"); + return; + } blockcount = jint(blockjson, "height"); - if (blockcount <= 0) - goto end; + if (blockcount <= 0) { + dlg_error("Invalid block height, check if the underlying blockchain is syncing right"); + return; + } + dlg_info("received blockhash of block height = %d", blockcount); - dlg_info("received blockhash, found at height = %d", blockcount); + if(!is_id_exists(CASHIERS_ID, 1)) { + dlg_error("Cashiers ID ::%s doesn't exists", CASHIERS_ID); + return; + } + cJSON *argjson = cJSON_CreateObject(); argjson = getaddressutxos(verus_addr, 1); - for (int32_t i = 0; i < cJSON_GetArraySize(argjson); i++) { if (jint(cJSON_GetArrayItem(argjson, i), "height") == blockcount) { dlg_info("tx_id::%s", jstr(cJSON_GetArrayItem(argjson, i), "txid")); From c4ea6856b11b82b812f339da578426f0fbe5c32e Mon Sep 17 00:00:00 2001 From: sg777 Date: Thu, 16 Nov 2023 20:17:59 +0530 Subject: [PATCH 6/7] Adding checks in chosing the table --- privatebet/vdxf.c | 43 +++++++++++++++++++++++++++++-------------- 1 file changed, 29 insertions(+), 14 deletions(-) diff --git a/privatebet/vdxf.c b/privatebet/vdxf.c index c8e13697..5e1b68d9 100644 --- a/privatebet/vdxf.c +++ b/privatebet/vdxf.c @@ -508,31 +508,22 @@ static void copy_table_to_struct_t(cJSON *t_table_info) strcpy(player_t.dealer_id, jstr(t_table_info, "dealer_id")); } -int32_t find_table() +int32_t chose_table() { + int32_t retval = OK; cJSON *t_table_info = NULL, *dealer_ids = NULL; - - if (!is_id_exists("dealers", 0)) { - return ERR_NO_DEALERS_FOUND; - } - /* - * Check if the player wallet has suffiecient funds to join the table - */ - if (!check_if_enough_funds_avail(player_config.table_id)) { - return ERR_CHIPS_INSUFFICIENT_FUNDS; - } - /* - * Check if the configured table meets the preconditions for the player to join the table - */ + t_table_info = cJSON_CreateObject(); retval = check_if_d_t_available(player_config.dealer_id, player_config.table_id, &t_table_info); if (retval == OK) { copy_table_to_struct_t(t_table_info); + dlg_info("Configured Dealer ::%s, Table ::%s are chosen", player_t.dealer_id, player_t.table_id); return retval; } dlg_info("Unable to join preconfigured table ::%s, checking for any other available tables...", bet_err_str(retval)); + dealer_ids = cJSON_CreateArray(); dealer_ids = get_cJSON_from_id_key("dealers", DEALERS_KEY); if (!dealer_ids) { @@ -544,10 +535,34 @@ int32_t find_table() strncpy(player_config.dealer_id, jstri(dealer_ids, i), sizeof(player_config.dealer_id)); strncpy(player_config.table_id, jstr(t_table_info, "table_id"), sizeof(player_config.table_id)); copy_table_to_struct_t(t_table_info); + dlg_info("Available Dealer ::%s, Table ::%s are chosen", player_t.dealer_id, player_t.table_id); return OK; } } return ERR_NO_TABLES_FOUND; + +} +int32_t find_table() +{ + int32_t retval = OK; + cJSON *t_table_info = NULL, *dealer_ids = NULL; + + if (!is_id_exists("dealers", 0)) { + return ERR_NO_DEALERS_FOUND; + } + /* + * Check if the configured table meets the preconditions for the player to join the table + */ + if((retval = chose_table()) != OK) { + return retval; + } + /* + * Check if the player wallet has suffiecient funds to join the table + */ + if (!check_if_enough_funds_avail(player_t.table_id)) { + return ERR_CHIPS_INSUFFICIENT_FUNDS; + } + return retval; } bool is_id_exists(char *id, int16_t full_id) From 5ba78eec47c9baa46b53e813b3887a19b5b13693 Mon Sep 17 00:00:00 2001 From: sg777 Date: Thu, 16 Nov 2023 20:41:22 +0530 Subject: [PATCH 7/7] Making mandatory checks for player join --- privatebet/err.c | 2 ++ privatebet/err.h | 1 + privatebet/player.c | 4 ++++ privatebet/vdxf.c | 17 +++++++++++++++++ privatebet/vdxf.h | 2 ++ 5 files changed, 26 insertions(+) diff --git a/privatebet/err.c b/privatebet/err.c index 061bdf49..29d3e48a 100644 --- a/privatebet/err.c +++ b/privatebet/err.c @@ -172,6 +172,8 @@ const char *bet_err_str(int32_t err_no) return "Error in cashier config args, check if all the necessary args are confiured in verus_cashier.ini"; case ERR_CONFIG_BLOCKCHAIN_ARGS: return "Error in blockchain config args, check if all the necessary args are confiured in blockchain_config.ini"; + case ERR_IDS_NOT_CONFIGURED: + return "The mandatory IDs needed to play poker are not created on CHAIN"; default: return "This error is not handled yet..."; } diff --git a/privatebet/err.h b/privatebet/err.h index 51aa37d7..b9d4abc3 100644 --- a/privatebet/err.h +++ b/privatebet/err.h @@ -120,6 +120,7 @@ All the errors that come across in bet are defined here. The error numbers are a #define ERR_CONFIG_DEALER_ARGS 133 #define ERR_CONFIG_CASHIER_ARGS 134 #define ERR_CONFIG_BLOCKCHAIN_ARGS 135 +#define ERR_IDS_NOT_CONFIGURED 136 // clang-format on const char *bet_err_str(int32_t err_no); diff --git a/privatebet/player.c b/privatebet/player.c index 57bcdca8..aae99323 100644 --- a/privatebet/player.c +++ b/privatebet/player.c @@ -134,6 +134,10 @@ int32_t handle_verus_player() { int32_t retval = OK; + if((retval = check_poker_ready()) != OK) { + return retval; + } + bet_parse_verus_player(); if ((retval = find_table()) != OK) diff --git a/privatebet/vdxf.c b/privatebet/vdxf.c index 5e1b68d9..8448c354 100644 --- a/privatebet/vdxf.c +++ b/privatebet/vdxf.c @@ -1239,3 +1239,20 @@ void list_tables() } } } + +int32_t check_poker_ready() +{ + int32_t retval = OK; + cJSON *dealers = NULL; + + if((!is_id_exists(CASHIERS_ID, 1)) || (!is_id_exists(DEALERS_ID, 1))) { + return ERR_IDS_NOT_CONFIGURED; + } + + dealers = cJSON_CreateObject(); + dealers = get_cJSON_from_id_key("dealers", DEALERS_KEY); + if(!dealers) { + return ERR_NO_DEALERS_FOUND; + } + return retval; +} diff --git a/privatebet/vdxf.h b/privatebet/vdxf.h index e8ae0b56..6c6476cb 100644 --- a/privatebet/vdxf.h +++ b/privatebet/vdxf.h @@ -89,6 +89,7 @@ Any entity in the bet ecosystem can register the identities under chips, like fo under chips as sg777.chips@ which basically been used to hold the tokens. */ #define CASHIERS_ID "cashiers.poker.chips10sec@" +#define DEALERS_ID "dealers.poker.chips10sec@" #define POKER_CHIPS_VDXF_ID "poker.chips10sec@" #define ID_UPDATE_ESTIMATE_NO 50 @@ -156,4 +157,5 @@ int32_t do_payin_tx_checks(char *txid, cJSON *payin_tx_data); void process_block(char *block_hash); void list_dealers(); void list_tables(); +int32_t check_poker_ready(); #endif