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

Commit 5fbff4d

Browse files
authored
Merge pull request #10 from milosz275/blockchain
Blockchain
2 parents 12aee51 + e0ff2f6 commit 5fbff4d

File tree

9 files changed

+1307
-132
lines changed

9 files changed

+1307
-132
lines changed

.gitignore

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ CMakeCache.txt
4949
*.log
5050
logs/
5151
logs
52+
history/
53+
history
5254

5355
# VSCode
5456
*.vc.*
@@ -58,3 +60,9 @@ logs
5860

5961
# BitLab history
6062
.*history.txt
63+
64+
# Testing
65+
*.test
66+
67+
# Blockchain
68+
*.dat

.vscode/settings.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
"getdata",
1212
"getheaders",
1313
"getip",
14+
"htole",
1415
"ipver",
1516
"libreadline",
1617
"lineptr",
@@ -45,7 +46,8 @@
4546
"bit": "c",
4647
"compare": "c",
4748
"limits": "c",
48-
"new": "c"
49+
"new": "c",
50+
"errno.h": "c"
4951
},
5052
"C_Cpp.formatting": "vcFormat",
5153
"C_Cpp.autoAddFileAssociations": true,
@@ -57,4 +59,4 @@
5759
"editor.formatOnType": true,
5860
"editor.formatOnSave": true
5961
}
60-
}
62+
}

Doxyfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -937,6 +937,7 @@ INPUT_ENCODING = UTF-8
937937
# *.vhdl, *.ucf, *.qsf and *.ice.
938938

939939
FILE_PATTERNS = *.c \
940+
*.cpp \
940941
*.h \
941942
*.md
942943

README.md

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ network's inner workings using the Bitcoin protocol.
3333
- [Usage](#usage)
3434
- [Config directory](#config-directory)
3535
- [Features](#features)
36-
- [Acknowledgements](#acknowledgements)
3736
- [License](#license)
3837

3938
## Installation
@@ -109,12 +108,11 @@ the following files:
109108
- `blocks.dat`: Block list file
110109
- `txs.dat`: Transaction list file -->
111110

112-
Please feel free to link logs to your working directory:
111+
Please feel free to link logs and history to your current working directory:
113112

114113
```bash
115-
mkdir -p logs
116-
ln -s ~/.bitlab/logs/bitlab.log logs/bitlab.log
117-
ln -s ~/.bitlab/history/cli_history.txt logs/cli_history.txt
114+
ln -s ~/.bitlab/logs ./logs
115+
ln -s ~/.bitlab/history ./history
118116
```
119117

120118
> [!NOTE]
@@ -157,8 +155,7 @@ Stay connected with active peers and monitor peer availability:
157155
- **Network Alerts:**
158156
- Send `alert` messages to notify peers of important network events.
159157
- `alert` has been deprecated because of security risks so it won't be implemented
160-
in this
161-
app. [documentation thread](https://bitcoin.org/en/alert/2016-11-01-alert-retirement#reasons-for-retirement)
158+
in this app. [documentation thread](https://bitcoin.org/en/alert/2016-11-01-alert-retirement#reasons-for-retirement)
162159
163160
### 4. Error Handling & Diagnostics
164161
@@ -191,10 +188,6 @@ Efficiently share and request blocks and transactions with peers:
191188
- `block` message: Send or advertise a specific block.
192189
- `headers` message: Share up to 2,000 block headers for faster synchronization.
193190
194-
## Acknowledgements
195-
196-
- [Thread-safe logging](https://github.com/milosz275/secure-chat/blob/main/common/include/log.h)
197-
198191
## License
199192
200193
This project is licensed under the MIT License - see

bitlab/include/cli.h

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ int cli_info(char** args);
113113
/**
114114
* Discovers Bitcoin peers.
115115
*
116-
* @param args The arguments passed to the function should be empty.
116+
* @param args The arguments passed to the function should match those specified in 'help peerdiscovery'.
117117
* @return The exit code.
118118
*/
119119
int cli_peer_discovery(char** args);
@@ -129,7 +129,7 @@ int cli_clear(char** args);
129129
/**
130130
* Prints CLI command help.
131131
*
132-
* @param args The arguments passed to this function should be empty.
132+
* @param args The arguments passed to this function can be empty to get full program help or argument should specify one comand to get help for.
133133
* @return The exit code.
134134
*/
135135
int cli_help(char** args);
@@ -177,6 +177,25 @@ int cli_getaddr(char** args);
177177
*/
178178
int cli_disconnect(char** args);
179179

180+
/**
181+
* @brief Sends a 'getheaders' message to specified peer.
182+
*
183+
* @param args The index of the peer.
184+
* @return The exit code.
185+
*/
186+
int cli_getheaders(char** args);
187+
188+
/**
189+
* @brief Sends a 'getblocks' message to specified peer.
190+
*
191+
* @param args The index of the peer.
192+
* @return The exit code.
193+
*/
194+
int cli_getblocks(char** args);
195+
196+
int cli_inv(char** args);
197+
198+
180199
//// LINE HANDLING FUNCTIONS ////
181200

182201
/**

bitlab/include/peer_connection.h

Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,66 @@
11
#ifndef __PEER_CONNECTION_H
22
#define __PEER_CONNECTION_H
33

4+
#include <stdint.h>
5+
#include <pthread.h>
6+
7+
// Maximum number of peers to track
8+
#define MAX_NODES 100
9+
10+
// Bitcoin mainnet magic bytes:
11+
#define BITCOIN_MAINNET_MAGIC 0xD9B4BEF9
12+
13+
// Default Bitcoin mainnet port:
14+
#define BITCOIN_MAINNET_PORT 8333
15+
16+
#define htole16(x) ((uint16_t)((((x) & 0xFF) << 8) | (((x) >> 8) & 0xFF)))
17+
#define htole32(x) ((uint32_t)((((x) & 0xFF) << 24) | (((x) >> 8) & 0xFF00) | (((x) >> 16) & 0xFF) | (((x) >> 24) & 0xFF000000)))
18+
#define htole64(x) ((uint64_t)((((x) & 0xFF) << 56) | (((x) >> 8) & 0xFF00) | (((x) >> 16) & 0xFF0000) | (((x) >> 24) & 0xFF000000) | (((x) >> 32) & 0xFF00000000) | (((x) >> 40) & 0xFF0000000000) | (((x) >> 48) & 0xFF000000000000) | (((x) >> 56) & 0xFF00000000000000)))
19+
20+
#define MAX_LOCATOR_COUNT 10
21+
#define GENESIS_BLOCK_HASH "0000000000000000000000000000000000000000000000000000000000000000"
22+
#define HEADERS_FILE "headers.dat"
23+
#define MAX_HEADERS_COUNT 2000
24+
25+
// Structure for Bitcoin P2P message header (24 bytes).
26+
// For reference: https://en.bitcoin.it/wiki/Protocol_documentation#Message_structure
27+
#pragma pack(push, 1)
28+
typedef struct
29+
{
30+
unsigned int magic; // Magic value indicating message origin network
31+
char command[12]; // ASCII command (null-padded)
32+
unsigned int length; // Payload size (little-endian)
33+
unsigned char checksum[4]; // First 4 bytes of double SHA-256 of the payload
34+
} bitcoin_msg_header;
35+
#pragma pack(pop)
36+
37+
/**
38+
* @brief The structure to store information about a connected peer.
39+
*
40+
* @param ip_address The IP address of the peer.
41+
* @param port The port of the peer.
42+
* @param socket_fd The socket file descriptor for communication.
43+
* @param thread The thread assigned to this connection.
44+
* @param is_connected The connection status.
45+
* @param operation_in_progress The operation status.
46+
* @param compact_blocks Does peer want to use compact blocks.
47+
* @param fee_rate Min fee rate in sat/kB of transaction that peer allows.
48+
*/
49+
typedef struct
50+
{
51+
char ip_address[64];
52+
uint16_t port;
53+
int socket_fd;
54+
pthread_t thread;
55+
int is_connected;
56+
int operation_in_progress;
57+
uint64_t compact_blocks;
58+
uint64_t fee_rate;
59+
} Node;
60+
61+
// global array of nodes
62+
extern Node nodes[MAX_NODES];
63+
464
/**
565
* @brief Lists all connected nodes and their details.
666
*
@@ -9,6 +69,14 @@
969
*/
1070
void list_connected_nodes();
1171

72+
/**
73+
* @brief Get the index of the node with the given IP address.
74+
*
75+
* @param ip_address The IP address of the node.
76+
* @return The index of the node in the global array.
77+
*/
78+
int get_idx(const char* ip_address);
79+
1280
/**
1381
* @brief Sends a 'getaddr' message to the peer and waits for a response.
1482
*
@@ -39,4 +107,68 @@ int connect_to_peer(const char* ip_addr);
39107
*/
40108
void disconnect(int node_id);
41109

110+
111+
unsigned char* load_blocks_from_file(const char* filename, size_t* payload_len);
112+
113+
/**
114+
* @brief Sends a 'getheaders' message to the peer and waits for a response.
115+
*
116+
* This function sends a 'getheaders' message to the peer identified by the given index
117+
* and waits for a response. It is used to request a list of known peers from the connected peer.
118+
*
119+
* @param idx The index of the peer in the nodes array.
120+
*/
121+
void send_getheaders_and_wait(int idx);
122+
123+
/**
124+
* @brief Sends a 'headers' message to the peer.
125+
*
126+
* This function sends a 'headers' message to the peer identified by the given index.
127+
* It retrieves the block headers from the local storage starting from the specified
128+
* start hash up to the stop hash or the maximum number of headers allowed.
129+
*
130+
* @param idx The index of the peer in the nodes array.
131+
* @param start_hash The hash of the first block header to send.
132+
* @param stop_hash The hash of the last block header to send.
133+
*/
134+
void send_headers(int idx, const unsigned char* start_hash, const unsigned char* stop_hash);
135+
136+
/**
137+
* @brief Sends a 'getblocks' message to the peer and waits for a response.
138+
*
139+
* This function sends a 'getblocks' message to the peer identified by the given index
140+
* and waits for a response. It is used to request a list of blocks from the connected peer.
141+
* The response is processed and the blocks are saved to a file.
142+
*
143+
* @param idx The index of the peer in the nodes array.
144+
*/
145+
void send_getblocks_and_wait(int idx);
146+
147+
/**
148+
* @brief Sends a 'getdata' message to the peer and waits for a response.
149+
*
150+
* This function sends a 'getdata' message to the peer identified by the given index
151+
* and waits for a response. It is used to request specific blocks or transactions
152+
* from the connected peer based on the provided hashes. The response is saved to a file
153+
* and logged to the Bitlab logs.
154+
*
155+
* @param idx The index of the peer in the nodes array.
156+
* @param hashes An array of hashes representing the blocks or transactions to request.
157+
* @param hash_count The number of hashes in the array.
158+
*/
159+
void send_getdata_and_wait(int idx, const unsigned char* hashes, size_t hash_count);
160+
161+
/**
162+
* @brief Sends an 'inv' message to the peer and waits for a response.
163+
*
164+
* This function sends an 'inv' message to the peer identified by the given index
165+
* and waits for a response. It is used to advertise the knowledge of one or more objects
166+
* (blocks or transactions). The inventory data is provided as input to the function.
167+
*
168+
* @param idx The index of the peer in the nodes array.
169+
* @param inv_data An array of inventory vectors (type + hash).
170+
* @param inv_count The number of inventory vectors in the array.
171+
*/
172+
void send_inv_and_wait(int idx, const unsigned char* inv_data, size_t inv_count);
173+
42174
#endif // __PEER_CONNECTION_H

0 commit comments

Comments
 (0)