Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Minor enhancements to block processing #391

Merged
merged 7 commits into from
Nov 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 8 additions & 12 deletions privatebet/bet.c
Original file line number Diff line number Diff line change
Expand Up @@ -455,19 +455,15 @@ 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();
Expand Down Expand Up @@ -586,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));
}
}
Expand Down
3 changes: 3 additions & 0 deletions privatebet/dealer.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
2 changes: 2 additions & 0 deletions privatebet/err.c
Original file line number Diff line number Diff line change
Expand Up @@ -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...";
}
Expand Down
1 change: 1 addition & 0 deletions privatebet/err.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
6 changes: 3 additions & 3 deletions privatebet/help.c
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ void bet_help_vdxf_command_usage()
"\nExample: \n"
"./bet add_dealer <id_name>\n"
"Note: Before adding dealer ID or name to dealers make sure ID exists\n");

dlg_info("\nCommand: \n"
"list_dealers \n"
"\nDescription: \n"
Expand All @@ -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"
Expand All @@ -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"
Expand Down
4 changes: 4 additions & 0 deletions privatebet/player.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
102 changes: 73 additions & 29 deletions privatebet/vdxf.c
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -507,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) {
Expand All @@ -543,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)
Expand Down Expand Up @@ -796,7 +812,7 @@ bool check_if_enough_funds_avail(char *table_id)
balance = chips_get_balance();
if (balance > min_stake + RESERVE_AMOUNT)
return true;
}
}
return false;
}

Expand Down Expand Up @@ -1153,20 +1169,32 @@ 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);

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"));
Expand All @@ -1190,25 +1218,41 @@ 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));
}
}
}

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;
}
3 changes: 2 additions & 1 deletion privatebet/vdxf.h
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,9 @@ 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
#define RESERVE_AMOUNT ID_UPDATE_ESTIMATE_NO *chips_tx_fee

Expand Down Expand Up @@ -157,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
Loading