@@ -2043,3 +2043,46 @@ void decode_transactions(const unsigned char* block_data, size_t block_len)
2043
2043
printf (" Lock time: %u\n" , lock_time );
2044
2044
}
2045
2045
}
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