blvm-protocol 0.1.4

Bitcoin Commons BLVM: Bitcoin protocol abstraction layer for multiple variants and evolution
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
//! Wire Format Tests
//!
//! Tests for Bitcoin P2P wire format serialization and deserialization.
//! Tests wire format module functions: calculate_checksum, serialize_message, deserialize_message.

use blvm_protocol::network::{NetworkAddress, NetworkMessage, VersionMessage};

// Note: wire module is not currently exported, but we can test via network module
// These tests verify wire format functionality when the module is enabled

#[test]
fn test_checksum_calculation() {
    // Test checksum calculation directly
    // Since wire module isn't exported, we test via message serialization
    // This will be fully testable once wire module is exported

    // For now, verify that version messages can be processed
    let version = VersionMessage {
        version: 70001,
        services: 1,
        timestamp: 1234567890,
        addr_recv: NetworkAddress {
            services: 1,
            ip: [0u8; 16],
            port: 8333,
        },
        addr_from: NetworkAddress {
            services: 1,
            ip: [0u8; 16],
            port: 8333,
        },
        nonce: 12345,
        user_agent: "test/1.0".to_string(),
        start_height: 0,
        relay: true,
    };

    let message = NetworkMessage::Version(version);

    // Verify message can be created
    assert!(matches!(message, NetworkMessage::Version(_)));
}

#[test]
fn test_wire_format_constants() {
    // Test that wire format constants are accessible
    // MESSAGE_HEADER_SIZE = 4 + 12 + 4 + 4 = 24 bytes
    // MAX_MESSAGE_PAYLOAD = 32 MB

    // These constants would be testable if wire module was exported
    // For now, we verify the expected structure
    let expected_header_size = 24; // 4 (magic) + 12 (command) + 4 (length) + 4 (checksum)
    let expected_max_payload = 32 * 1024 * 1024; // 32 MB

    // Verify constants match expected Bitcoin protocol values
    assert_eq!(expected_header_size, 24);
    assert_eq!(expected_max_payload, 33_554_432);
}

#[test]
fn test_message_size_limits() {
    // Test that message size limits are enforced
    // MAX_MESSAGE_PAYLOAD should be 32 MB

    let max_payload = 32 * 1024 * 1024;
    assert_eq!(max_payload, 33_554_432);

    // Verify this matches Bitcoin protocol limits
    assert!(max_payload > 0);
}

#[test]
fn test_magic_bytes_validation() {
    // Test that magic bytes are validated
    // Mainnet magic: 0xf9beb4d9
    // Testnet magic: 0x0b110907
    // Regtest magic: 0xfabfb5da

    let mainnet_magic = [0xf9, 0xbe, 0xb4, 0xd9];
    let testnet_magic = [0x0b, 0x11, 0x09, 0x07];
    let regtest_magic = [0xfa, 0xbf, 0xb5, 0xda];

    // Verify magic bytes are different for each network
    assert_ne!(mainnet_magic, testnet_magic);
    assert_ne!(mainnet_magic, regtest_magic);
    assert_ne!(testnet_magic, regtest_magic);
}

#[test]
fn test_command_encoding() {
    // Test that command names are properly encoded
    // Commands are 12-byte ASCII strings, null-padded

    let commands = vec![
        "version", "verack", "addr", "inv", "getdata", "ping", "pong",
    ];

    for cmd in commands {
        // Verify command length fits in 12 bytes
        assert!(cmd.len() <= 12, "Command '{}' exceeds 12 bytes", cmd);

        // Verify command is ASCII
        assert!(cmd.is_ascii(), "Command '{}' is not ASCII", cmd);
    }
}

#[test]
fn test_payload_length_encoding() {
    // Test that payload length is encoded as little-endian u32
    // This is critical for wire format correctness

    let test_lengths = vec![0u32, 1, 100, 1000, 10000, u32::MAX];

    for length in test_lengths {
        let encoded = length.to_le_bytes();
        let decoded = u32::from_le_bytes(encoded);

        assert_eq!(
            length, decoded,
            "Payload length encoding failed for {}",
            length
        );
    }
}

#[test]
fn test_checksum_algorithm() {
    // Test checksum calculation algorithm
    // Checksum = first 4 bytes of double SHA256(payload)

    use sha2::{Digest, Sha256};

    let test_payload = b"test payload";
    let hash1 = Sha256::digest(test_payload);
    let hash2 = Sha256::digest(hash1);
    let checksum: [u8; 4] = hash2[..4].try_into().unwrap();

    // Verify checksum is 4 bytes
    assert_eq!(checksum.len(), 4);

    // Verify checksum is deterministic
    let hash1_2 = Sha256::digest(test_payload);
    let hash2_2 = Sha256::digest(hash1_2);
    let checksum2: [u8; 4] = hash2_2[..4].try_into().unwrap();
    assert_eq!(checksum, checksum2);
}

#[test]
fn test_empty_payload() {
    // Test that empty payloads are handled correctly
    // Some messages (verack, getaddr, mempool, sendheaders) have empty payloads

    let empty_payload: Vec<u8> = vec![];

    // Empty payload should be valid
    assert_eq!(empty_payload.len(), 0);

    // Checksum of empty payload should be calculable
    use sha2::{Digest, Sha256};
    let hash1 = Sha256::digest(&empty_payload);
    let hash2 = Sha256::digest(hash1);
    let checksum = &hash2[..4];
    assert_eq!(checksum.len(), 4);
}

#[test]
fn test_large_payload_boundary() {
    // Test payload size boundary conditions
    // MAX_MESSAGE_PAYLOAD = 32 MB

    let max_payload = 32 * 1024 * 1024;
    let over_limit = max_payload + 1;

    // Verify boundary values
    assert_eq!(max_payload, 33_554_432);
    assert!(over_limit > max_payload);
}

#[test]
fn test_message_header_structure() {
    // Test that message header has correct structure
    // Format: [magic:4][command:12][length:4][checksum:4]

    let header_size = 4 + 12 + 4 + 4; // 24 bytes
    assert_eq!(header_size, 24);

    // Verify each field size
    let magic_size = 4;
    let command_size = 12;
    let length_size = 4;
    let checksum_size = 4;

    assert_eq!(
        magic_size + command_size + length_size + checksum_size,
        header_size
    );
}

#[test]
fn test_version_message_serialization_roundtrip() {
    // Test that version messages can be serialized and deserialized correctly
    // This ensures compatibility with Bitcoin Core wire format

    use blvm_protocol::wire::{deserialize_version, serialize_version};

    let original = VersionMessage {
        version: 70015,
        services: 0x0000000000000001, // NODE_NETWORK
        timestamp: 1234567890,
        addr_recv: NetworkAddress {
            services: 0x0000000000000001,
            ip: [
                0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0x7f, 0x00,
                0x00, 0x01,
            ], // IPv4: 127.0.0.1
            port: 8333,
        },
        addr_from: NetworkAddress {
            services: 0x0000000000000001,
            ip: [
                0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0x7f, 0x00,
                0x00, 0x01,
            ], // IPv4: 127.0.0.1
            port: 8333,
        },
        nonce: 0x1234567890abcdef,
        user_agent: "/BitcoinCommons:0.1.0/".to_string(),
        start_height: 100000,
        relay: true,
    };

    // Serialize
    let serialized = serialize_version(&original).expect("Serialization should succeed");

    // Deserialize
    let deserialized = deserialize_version(&serialized).expect("Deserialization should succeed");

    // Verify all fields match
    assert_eq!(original.version, deserialized.version);
    assert_eq!(original.services, deserialized.services);
    assert_eq!(original.timestamp, deserialized.timestamp);
    assert_eq!(original.addr_recv.services, deserialized.addr_recv.services);
    assert_eq!(original.addr_recv.ip, deserialized.addr_recv.ip);
    assert_eq!(original.addr_recv.port, deserialized.addr_recv.port);
    assert_eq!(original.addr_from.services, deserialized.addr_from.services);
    assert_eq!(original.addr_from.ip, deserialized.addr_from.ip);
    assert_eq!(original.addr_from.port, deserialized.addr_from.port);
    assert_eq!(original.nonce, deserialized.nonce);
    assert_eq!(original.user_agent, deserialized.user_agent);
    assert_eq!(original.start_height, deserialized.start_height);
    assert_eq!(original.relay, deserialized.relay);
}

#[test]
fn test_version_message_wire_format_compatibility() {
    // Test that version message wire format matches Bitcoin Core expectations
    // This ensures compatibility with other Bitcoin nodes

    use blvm_protocol::wire::serialize_version;

    let version = VersionMessage {
        version: 70015,
        services: 0x0000000000000001, // NODE_NETWORK
        timestamp: 1234567890,
        addr_recv: NetworkAddress {
            services: 0x0000000000000001,
            ip: [
                0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0x7f, 0x00,
                0x00, 0x01,
            ], // IPv4: 127.0.0.1
            port: 8333,
        },
        addr_from: NetworkAddress {
            services: 0x0000000000000001,
            ip: [
                0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0x7f, 0x00,
                0x00, 0x01,
            ], // IPv4: 127.0.0.1
            port: 8333,
        },
        nonce: 0x1234567890abcdef,
        user_agent: "/BitcoinCommons:0.1.0/".to_string(),
        start_height: 0,
        relay: true,
    };

    let serialized = serialize_version(&version).expect("Serialization should succeed");

    // Verify minimum expected size
    // version (4) + services (8) + timestamp (8) + addr_recv (26) + addr_from (26)
    // + nonce (8) + user_agent varint + user_agent bytes + start_height (4) + relay (1)
    let min_size = 4 + 8 + 8 + 26 + 26 + 8 + 1 + 22 + 4 + 1; // Minimum with short user_agent
    assert!(
        serialized.len() >= min_size,
        "Serialized version message too short"
    );

    // Verify structure: version should be first 4 bytes (little-endian i32)
    let version_bytes = [serialized[0], serialized[1], serialized[2], serialized[3]];
    let decoded_version = i32::from_le_bytes(version_bytes) as u32;
    assert_eq!(decoded_version, 70015);

    // Verify services (next 8 bytes, little-endian u64)
    let services_bytes = [
        serialized[4],
        serialized[5],
        serialized[6],
        serialized[7],
        serialized[8],
        serialized[9],
        serialized[10],
        serialized[11],
    ];
    let decoded_services = u64::from_le_bytes(services_bytes);
    assert_eq!(decoded_services, 0x0000000000000001);

    // Verify NetworkAddress format: services (8) + ip (16) + port (2, big-endian)
    // addr_recv starts at byte 20 (after version(4) + services(8) + timestamp(8))
    let addr_recv_port_start = 20 + 8 + 16; // services + ip
    let port_bytes = [
        serialized[addr_recv_port_start],
        serialized[addr_recv_port_start + 1],
    ];
    let decoded_port = u16::from_be_bytes(port_bytes);
    assert_eq!(decoded_port, 8333, "Port should be big-endian");
}

#[test]
fn test_version_message_user_agent_variants() {
    // Test version messages with different user agent lengths
    // User agent uses CompactSize (varint) encoding

    use blvm_protocol::wire::{deserialize_version, serialize_version};

    let test_cases = vec![
        "".to_string(),                       // Empty
        "/BitcoinCommons:0.1.0/".to_string(), // Short
        "/BitcoinCommons:0.1.0/".repeat(10),  // Long
    ];

    for user_agent in test_cases {
        let version = VersionMessage {
            version: 70015,
            services: 0x0000000000000001,
            timestamp: 1234567890,
            addr_recv: NetworkAddress {
                services: 0,
                ip: [0u8; 16],
                port: 0,
            },
            addr_from: NetworkAddress {
                services: 0,
                ip: [0u8; 16],
                port: 0,
            },
            nonce: 0,
            user_agent: user_agent.clone(),
            start_height: 0,
            relay: false,
        };

        let serialized = serialize_version(&version).expect("Serialization should succeed");
        let deserialized =
            deserialize_version(&serialized).expect("Deserialization should succeed");

        assert_eq!(
            deserialized.user_agent, user_agent,
            "User agent should match after round-trip"
        );
    }
}

#[test]
fn test_version_message_network_address_encoding() {
    // Test that NetworkAddress is encoded correctly (services LE, ip, port BE)

    use blvm_protocol::wire::serialize_version;

    let version = VersionMessage {
        version: 70015,
        services: 0,
        timestamp: 0,
        addr_recv: NetworkAddress {
            services: 0x0102030405060708,
            ip: [
                0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0x0a, 0x00,
                0x00, 0x01,
            ], // IPv4: 10.0.0.1
            port: 0x1234, // Port 4660
        },
        addr_from: NetworkAddress {
            services: 0,
            ip: [0u8; 16],
            port: 0,
        },
        nonce: 0,
        user_agent: "".to_string(),
        start_height: 0,
        relay: false,
    };

    let serialized = serialize_version(&version).expect("Serialization should succeed");

    // addr_recv starts at byte 20 (version(4) + services(8) + timestamp(8))
    // Verify services (little-endian)
    let services_start = 20;
    let services_bytes = [
        serialized[services_start],
        serialized[services_start + 1],
        serialized[services_start + 2],
        serialized[services_start + 3],
        serialized[services_start + 4],
        serialized[services_start + 5],
        serialized[services_start + 6],
        serialized[services_start + 7],
    ];
    let decoded_services = u64::from_le_bytes(services_bytes);
    assert_eq!(decoded_services, 0x0102030405060708);

    // Verify port (big-endian, at offset 20 + 8 + 16 = 44)
    let port_start = 20 + 8 + 16;
    let port_bytes = [serialized[port_start], serialized[port_start + 1]];
    let decoded_port = u16::from_be_bytes(port_bytes);
    assert_eq!(decoded_port, 0x1234);
}