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
//! Message bridge between blvm-consensus and transport layer
//!
//! Provides conversion between blvm-consensus NetworkMessage types
//! and the transport layer's message format.
use crate::network::protocol_adapter::ProtocolAdapter;
use crate::network::transport::TransportType;
use anyhow::Result;
use blvm_protocol::network::{NetworkMessage as ConsensusNetworkMessage, NetworkResponse};
use tracing::debug;
/// Message bridge for connecting blvm-consensus message processing
/// with the transport layer
pub struct MessageBridge;
impl MessageBridge {
/// Convert blvm-consensus NetworkMessage to transport wire format
pub fn to_transport_message(
msg: &ConsensusNetworkMessage,
transport: TransportType,
) -> Result<Vec<u8>> {
debug!(
"Converting consensus message to transport format: {:?}",
transport
);
ProtocolAdapter::serialize_message(msg, transport)
}
/// Convert transport wire format to blvm-consensus NetworkMessage
pub fn from_transport_message(
data: &[u8],
transport: TransportType,
) -> Result<ConsensusNetworkMessage> {
debug!(
"Converting transport message to consensus format: {:?}",
transport
);
ProtocolAdapter::deserialize_message(data, transport)
}
/// Process a blvm-consensus NetworkResponse and extract messages to send
///
/// NetworkResponse can indicate sending one or multiple messages,
/// or other actions (Ok, Reject).
pub fn extract_send_messages(
response: &NetworkResponse,
transport: TransportType,
) -> Result<Vec<Vec<u8>>> {
match response {
NetworkResponse::Ok => {
Ok(Vec::new()) // No messages to send
}
NetworkResponse::SendMessage(msg) => {
let wire = Self::to_transport_message(msg, transport)?;
Ok(vec![wire])
}
NetworkResponse::SendMessages(msgs) => {
let mut wires = Vec::new();
for msg in msgs {
let wire = Self::to_transport_message(msg, transport)?;
wires.push(wire);
}
Ok(wires)
}
NetworkResponse::Reject(reason) => {
debug!("Message rejected: {}", reason);
Ok(Vec::new()) // Rejection doesn't send a message
}
}
}
/// Process incoming transport message and generate response
///
/// Takes transport bytes, converts to protocol message, processes it,
/// and returns wire format messages to send back (if any).
///
/// NOTE: This is a future integration point. To fully implement, this would:
/// 1. Take `&BitcoinProtocolEngine`, `&mut PeerState`, and `Option<&dyn ChainStateAccess>`
/// 2. Call `blvm_protocol::network::process_network_message()` with these parameters
/// 3. Convert the `NetworkResponse` to wire format messages using `extract_send_messages()`
///
/// For now, this method only handles message conversion, not processing.
pub fn process_incoming_message(data: &[u8], transport: TransportType) -> Result<Vec<Vec<u8>>> {
// Convert to protocol message
let protocol_msg = Self::from_transport_message(data, transport)?;
// Note: Protocol layer processing is handled in NetworkManager::handle_incoming_wire_tcp
// This function provides message extraction for transport layer
// This requires:
// - BitcoinProtocolEngine instance
// - PeerState management
// - ChainStateAccess implementation
// - UTXO set and height for block/tx validation
debug!("Converted incoming protocol message: {:?}", protocol_msg);
// Return empty for now - actual processing would be done by network handlers
// that have access to protocol engine and chain state
Ok(Vec::new())
}
}