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

Commit 262e11e

Browse files
committed
Add send_tx
1 parent 9df4cd7 commit 262e11e

File tree

2 files changed

+56
-0
lines changed

2 files changed

+56
-0
lines changed

bitlab/include/peer_connection.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,4 +171,17 @@ void send_getdata_and_wait(int idx, const unsigned char* hashes, size_t hash_cou
171171
*/
172172
void send_inv_and_wait(int idx, const unsigned char* inv_data, size_t inv_count);
173173

174+
/**
175+
* @brief Sends a 'tx' message to the specified node.
176+
*
177+
* This function sends a 'tx' message to the node identified by the given index
178+
* with the provided transaction data. It constructs the message with the appropriate
179+
* Bitcoin protocol header and sends it over the network socket associated with the node.
180+
*
181+
* @param idx The index of the node in the nodes array.
182+
* @param tx_data A pointer to the transaction data in hexadecimal format.
183+
* @param tx_size The size of the transaction data in bytes.
184+
*/
185+
void send_tx(int idx, const unsigned char* tx_data, size_t tx_size);
186+
174187
#endif // __PEER_CONNECTION_H

bitlab/src/peer_connection.c

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2043,3 +2043,46 @@ void decode_transactions(const unsigned char* block_data, size_t block_len)
20432043
printf(" Lock time: %u\n", lock_time);
20442044
}
20452045
}
2046+
2047+
void send_tx(int idx, const unsigned char* tx_data, size_t tx_size)
2048+
{
2049+
if (idx < 0 || idx >= MAX_NODES || !nodes[idx].is_connected)
2050+
{
2051+
fprintf(stderr, "[Error] Invalid node index or node not connected.\n");
2052+
return;
2053+
}
2054+
2055+
Node* node = &nodes[idx];
2056+
char log_filename[256];
2057+
snprintf(log_filename, sizeof(log_filename), "peer_connection_%s.log", node->ip_address);
2058+
2059+
if (!tx_data || tx_size == 0)
2060+
{
2061+
fprintf(stderr, "[Error] Invalid transaction data.\n");
2062+
return;
2063+
}
2064+
2065+
// Build the 'tx' message with the appropriate header
2066+
unsigned char tx_msg[sizeof(bitcoin_msg_header) + tx_size];
2067+
2068+
// Build the message header
2069+
bitcoin_msg_header* header = (bitcoin_msg_header*)tx_msg;
2070+
header->magic = htole32(BITCOIN_MAINNET_MAGIC);
2071+
strncpy(header->command, "tx", 12);
2072+
header->length = htole32(tx_size);
2073+
compute_checksum(tx_data, tx_size, header->checksum);
2074+
2075+
// Copy the transaction data
2076+
memcpy(tx_msg + sizeof(bitcoin_msg_header), tx_data, tx_size);
2077+
2078+
// Send the 'tx' message
2079+
ssize_t bytes_sent = send(node->socket_fd, tx_msg, sizeof(bitcoin_msg_header) + tx_size, 0);
2080+
if (bytes_sent < 0)
2081+
{
2082+
log_message(LOG_INFO, log_filename, __FILE__,
2083+
"[Error] Failed to send 'tx' message: %s", strerror(errno));
2084+
return;
2085+
}
2086+
2087+
log_message(LOG_INFO, log_filename, __FILE__, "Sent 'tx' message (%zu bytes).", tx_size);
2088+
}

0 commit comments

Comments
 (0)