blvm_protocol/wire/frame_header.rs
1//! Message framing constants and payload checksum (first 4 bytes of double-SHA256).
2
3use sha2::{Digest, Sha256};
4
5/// Bitcoin P2P message header size (magic + command + length + checksum)
6pub const MESSAGE_HEADER_SIZE: usize = 4 + 12 + 4 + 4;
7
8/// Maximum message payload size (32 MB)
9pub const MAX_MESSAGE_PAYLOAD: usize = 32 * 1024 * 1024;
10
11/// Calculate checksum for message payload (first 4 bytes of double SHA256)
12pub fn calculate_checksum(payload: &[u8]) -> [u8; 4] {
13 let hash1 = Sha256::digest(payload);
14 let hash2 = Sha256::digest(hash1);
15 let mut checksum = [0u8; 4];
16 checksum.copy_from_slice(&hash2[..4]);
17 checksum
18}