-
Notifications
You must be signed in to change notification settings - Fork 16
/
relayer.rs
265 lines (247 loc) · 9.72 KB
/
relayer.rs
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
use ckb_network::{
async_trait, bytes::Bytes, extract_peer_id, CKBProtocolContext, CKBProtocolHandler, PeerId,
PeerIndex,
};
use ckb_types::core::{Cycle, TransactionView};
use ckb_types::{packed, prelude::*};
use linked_hash_map::LinkedHashMap;
use log::{debug, trace, warn};
use std::collections::{HashMap, HashSet};
use std::sync::{Arc, RwLock};
use std::time::{Duration, Instant};
use crate::protocols::{Peers, BAD_MESSAGE_BAN_TIME};
const CHECK_PENDING_TXS_TOKEN: u64 = 0;
pub(crate) struct RelayProtocol {
connected_peers: Arc<Peers>,
// Record the peers which have opened the relay protocol, value is used to close the protocol in the inactive period
opened_peers: HashMap<PeerIndex, Option<Instant>>,
// Pending transactions which are waiting for relay
pending_txs: Arc<RwLock<PendingTxs>>,
}
// a simple struct to store the pending transactions in memory with size limit
pub(crate) struct PendingTxs {
txs: LinkedHashMap<packed::Byte32, (packed::Transaction, Cycle, HashSet<PeerId>)>,
updated_at: Instant,
limit: usize,
}
impl PendingTxs {
pub fn new(limit: usize) -> Self {
Self {
txs: LinkedHashMap::new(),
updated_at: Instant::now(),
limit,
}
}
pub fn push(&mut self, tx: TransactionView, cycles: Cycle) {
self.txs
.insert(tx.hash(), (tx.data(), cycles, HashSet::new()));
if self.txs.len() > self.limit {
self.txs.pop_front();
}
self.updated_at = Instant::now();
}
pub fn get(
&self,
hash: &packed::Byte32,
) -> Option<(packed::Transaction, Cycle, HashSet<PeerId>)> {
self.txs.get(hash).cloned()
}
fn fetch_transaction_hashes_for_broadcast(&mut self, peer_id: PeerId) -> Vec<packed::Byte32> {
self.txs
.iter_mut()
.filter_map(|(hash, (_, _, peers))| {
if peers.insert(peer_id.clone()) {
Some(hash.clone())
} else {
None
}
})
.collect()
}
fn is_not_empty_and_updated_at(&self, seconds: u64) -> bool {
!self.txs.is_empty() && self.updated_at.elapsed() < Duration::from_secs(seconds)
}
}
impl RelayProtocol {
pub fn new(pending_txs: Arc<RwLock<PendingTxs>>, connected_peers: Arc<Peers>) -> Self {
Self {
opened_peers: HashMap::new(),
pending_txs,
connected_peers,
}
}
}
#[async_trait]
impl CKBProtocolHandler for RelayProtocol {
async fn init(&mut self, nc: Arc<dyn CKBProtocolContext + Sync>) {
nc.set_notify(Duration::from_secs(2), CHECK_PENDING_TXS_TOKEN)
.await
.expect("set_notify should be ok");
}
async fn connected(
&mut self,
nc: Arc<dyn CKBProtocolContext + Sync>,
peer: PeerIndex,
version: &str,
) {
debug!("RelayProtocol({}).connected peer={}", version, peer);
if self
.pending_txs
.read()
.unwrap()
.is_not_empty_and_updated_at(60)
{
let peer_id = nc
.get_peer(peer)
.and_then(|p| extract_peer_id(&p.connected_addr))
.unwrap();
let tx_hashes = self
.pending_txs
.write()
.unwrap()
.fetch_transaction_hashes_for_broadcast(peer_id);
if !tx_hashes.is_empty() {
let content = packed::RelayTransactionHashes::new_builder()
.tx_hashes(tx_hashes.pack())
.build();
let message = packed::RelayMessage::new_builder().set(content).build();
if let Err(err) = nc.send_message_to(peer, message.as_bytes()) {
warn!(
"RelayProtocol failed to send RelayTransactionHashes message to peer={} since {:?}",
peer, err
);
}
self.opened_peers.insert(peer, Some(Instant::now()));
} else {
self.opened_peers.insert(peer, None);
}
} else {
self.opened_peers.insert(peer, None);
}
}
async fn disconnected(&mut self, _nc: Arc<dyn CKBProtocolContext + Sync>, peer: PeerIndex) {
debug!("RelayProtocol.disconnected peer={}", peer);
self.opened_peers.remove(&peer);
}
async fn received(
&mut self,
nc: Arc<dyn CKBProtocolContext + Sync>,
peer: PeerIndex,
data: Bytes,
) {
let message = match packed::RelayMessageReader::from_compatible_slice(&data) {
Ok(msg) => msg.to_enum(),
_ => {
warn!(
"RelayProtocol.received a malformed message from Peer({})",
peer
);
nc.ban_peer(
peer,
BAD_MESSAGE_BAN_TIME,
String::from("send us a malformed message"),
);
return;
}
};
trace!(
"RelayProtocol.received peer={}, message={}",
peer,
message.item_name()
);
if let packed::RelayMessageUnionReader::GetRelayTransactions(reader) = message {
let pending_txs = self.pending_txs.read().expect("read access should be OK");
let relay_txs: Vec<_> = reader
.tx_hashes()
.iter()
.filter_map(|tx_hash| {
pending_txs
.get(&tx_hash.to_entity())
.map(|(tx, cycles, _)| {
packed::RelayTransaction::new_builder()
.transaction(tx)
.cycles(cycles.pack())
.build()
})
})
.collect();
let content = packed::RelayTransactions::new_builder()
.transactions(relay_txs.pack())
.build();
let msg = packed::RelayMessage::new_builder().set(content).build();
if let Err(err) = nc.send_message_to(peer, msg.as_bytes()) {
warn!(
"RelayProtocol failed to send RelayTransactions message to peer={} since {:?}",
peer, err
);
}
} else {
// ignore other messages
}
}
async fn notify(&mut self, nc: Arc<dyn CKBProtocolContext + Sync>, token: u64) {
match token {
CHECK_PENDING_TXS_TOKEN => {
// we check pending txs every 2 seconds, if the timestamp of the pending txs is updated in the last minute
// and connected relay protocol peers is empty, we try to open the protocol and broadcast the pending txs
if self
.pending_txs
.read()
.unwrap()
.is_not_empty_and_updated_at(60)
&& self.opened_peers.is_empty()
{
let p2p_control = nc.p2p_control().expect("p2p_control should be exist");
for peer in self.connected_peers.get_peers_index() {
if let Err(err) = p2p_control.open_protocol(peer, nc.protocol_id()) {
warn!(
"RelayProtocol failed to open protocol to peer={} since {:?}",
peer, err
);
}
}
} else {
let mut pending_txs = self.pending_txs.write().unwrap();
for (&peer, instant) in self.opened_peers.iter_mut() {
if let Some(peer_id) = nc
.get_peer(peer)
.and_then(|p| extract_peer_id(&p.connected_addr))
{
let tx_hashes =
pending_txs.fetch_transaction_hashes_for_broadcast(peer_id);
if !tx_hashes.is_empty() {
let content = packed::RelayTransactionHashes::new_builder()
.tx_hashes(tx_hashes.pack())
.build();
let message =
packed::RelayMessage::new_builder().set(content).build();
if let Err(err) = nc.send_message_to(peer, message.as_bytes()) {
warn!(
"RelayProtocol failed to send RelayTransactionHashes message to peer={} since {:?}",
peer, err
);
}
instant.replace(Instant::now());
} else if instant
.map(|i| i.elapsed() > Duration::from_secs(60))
.unwrap_or(true)
{
debug!(
"RelayProtocol.notify peer={} is inactive, close the protocol",
peer
);
let _ = nc
.p2p_control()
.expect("p2p_control should be exist")
.close_protocol(peer, nc.protocol_id());
}
}
}
}
}
_ => {
unreachable!()
}
}
}
}