Skip to content
This repository was archived by the owner on Apr 15, 2025. It is now read-only.

Commit 9df4cd7

Browse files
committed
Add tx command
1 parent 7c5379b commit 9df4cd7

File tree

2 files changed

+73
-0
lines changed

2 files changed

+73
-0
lines changed

bitlab/include/cli.h

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,10 +193,30 @@ int cli_getheaders(char** args);
193193
*/
194194
int cli_getblocks(char** args);
195195

196+
/**
197+
* @brief Sends a 'inv' message to specified peer.
198+
*
199+
* @param args The index of the peer.
200+
* @return The exit code.
201+
*/
196202
int cli_inv(char** args);
197203

204+
/**
205+
* @brief Sends a 'getdata' message to specified peer.
206+
*
207+
* @param args The index of the peer.
208+
* @return The exit code.
209+
*/
198210
int cli_getdata(char** args);
199211

212+
/**
213+
* @brief Sends a 'tx' message to specified peer.
214+
*
215+
* @param args The index of the peer.
216+
* @return The exit code.
217+
*/
218+
int cli_tx(char** args);
219+
200220

201221
//// LINE HANDLING FUNCTIONS ////
202222

bitlab/src/cli.c

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,13 @@ static cli_command cli_commands[] =
159159
.cli_command_detailed_desc = " * getdata - Sends 'getdata' to a idx-th node with given block hashes. Retrieves transaction data from returned 'blocks' message.",
160160
.cli_command_usage = "getdata [idx of node] [block hash 1] ..."
161161
},
162+
{
163+
.cli_command = &cli_tx,
164+
.cli_command_name = "tx",
165+
.cli_command_brief_desc = "Sends a transaction to a specified node.",
166+
.cli_command_detailed_desc = " * tx - Sends a 'tx' message to the specified node with the provided transaction data.",
167+
.cli_command_usage = "tx [idx of node] [transaction data in hex]"
168+
},
162169
}; // do not add NULLs at the end
163170

164171
void print_help()
@@ -1026,6 +1033,52 @@ int cli_inv(char** args)
10261033
return 0;
10271034
}
10281035

1036+
int cli_tx(char** args)
1037+
{
1038+
pthread_mutex_lock(&cli_mutex);
1039+
if (args[0] == NULL || args[1] == NULL)
1040+
{
1041+
log_message(LOG_WARN, BITLAB_LOG, __FILE__,
1042+
"No arguments provided for tx command");
1043+
print_usage("tx");
1044+
pthread_mutex_unlock(&cli_mutex);
1045+
return 1;
1046+
}
1047+
1048+
int idx = atoi(args[0]);
1049+
if (idx < 0 || idx >= MAX_NODES)
1050+
{
1051+
log_message(LOG_WARN, BITLAB_LOG, __FILE__,
1052+
"Invalid node index for tx command");
1053+
print_usage("tx");
1054+
pthread_mutex_unlock(&cli_mutex);
1055+
return 1;
1056+
}
1057+
1058+
// Parse the transaction data from the arguments
1059+
size_t tx_size = strlen(args[1]) / 2;
1060+
unsigned char* tx_data = malloc(tx_size);
1061+
if (tx_data == NULL)
1062+
{
1063+
log_message(LOG_ERROR, BITLAB_LOG, __FILE__,
1064+
"Failed to allocate memory for transaction data");
1065+
pthread_mutex_unlock(&cli_mutex);
1066+
return 1;
1067+
}
1068+
1069+
for (size_t i = 0; i < tx_size; i++)
1070+
{
1071+
sscanf(&args[1][i * 2], "%2hhx", &tx_data[i]);
1072+
}
1073+
1074+
guarded_print_line("Sending transaction to %d with size %zu", idx, tx_size);
1075+
send_tx(idx, tx_data, tx_size);
1076+
1077+
free(tx_data);
1078+
pthread_mutex_unlock(&cli_mutex);
1079+
return 0;
1080+
}
1081+
10291082
int cli_list(char** args)
10301083
{
10311084
pthread_mutex_lock(&cli_mutex);

0 commit comments

Comments
 (0)