-
Notifications
You must be signed in to change notification settings - Fork 236
/
mod.rs
1032 lines (963 loc) · 38.9 KB
/
mod.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
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
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
mod block_proposal_process;
mod block_transactions_process;
mod block_transactions_verifier;
mod block_uncles_verifier;
mod compact_block_process;
mod compact_block_verifier;
mod get_block_proposal_process;
mod get_block_transactions_process;
mod get_transactions_process;
#[cfg(test)]
pub(crate) mod tests;
mod transaction_hashes_process;
mod transactions_process;
use self::block_proposal_process::BlockProposalProcess;
use self::block_transactions_process::BlockTransactionsProcess;
pub(crate) use self::compact_block_process::CompactBlockProcess;
use self::get_block_proposal_process::GetBlockProposalProcess;
use self::get_block_transactions_process::GetBlockTransactionsProcess;
use self::get_transactions_process::GetTransactionsProcess;
use self::transaction_hashes_process::TransactionHashesProcess;
use self::transactions_process::TransactionsProcess;
use crate::types::{post_sync_process, ActiveChain, SyncShared};
use crate::utils::{metric_ckb_message_bytes, send_message_to, MetricDirection};
use crate::{Status, StatusCode};
use ckb_chain::VerifyResult;
use ckb_chain::{ChainController, RemoteBlock};
use ckb_constant::sync::BAD_MESSAGE_BAN_TIME;
use ckb_error::is_internal_db_error;
use ckb_logger::{
debug, debug_target, error, error_target, info_target, trace_target, warn_target,
};
use ckb_network::{
async_trait, bytes::Bytes, tokio, CKBProtocolContext, CKBProtocolHandler, PeerIndex,
SupportProtocols, TargetSession,
};
use ckb_shared::block_status::BlockStatus;
use ckb_shared::Shared;
use ckb_systemtime::unix_time_as_millis;
use ckb_tx_pool::service::TxVerificationResult;
use ckb_types::BlockNumberAndHash;
use ckb_types::{
core::{self, BlockView},
packed::{self, Byte32, ProposalShortId},
prelude::*,
};
use ckb_util::Mutex;
use itertools::Itertools;
use std::collections::{HashMap, HashSet};
use std::sync::Arc;
use std::time::{Duration, Instant};
pub const TX_PROPOSAL_TOKEN: u64 = 0;
pub const ASK_FOR_TXS_TOKEN: u64 = 1;
pub const TX_HASHES_TOKEN: u64 = 2;
pub const MAX_RELAY_PEERS: usize = 128;
pub const MAX_RELAY_TXS_NUM_PER_BATCH: usize = 32767;
pub const MAX_RELAY_TXS_BYTES_PER_BATCH: usize = 1024 * 1024;
type RateLimiter<T> = governor::RateLimiter<
T,
governor::state::keyed::DefaultKeyedStateStore<T>,
governor::clock::DefaultClock,
>;
#[derive(Debug, Eq, PartialEq)]
pub enum ReconstructionResult {
Block(BlockView),
Missing(Vec<usize>, Vec<usize>),
Collided,
Error(Status),
}
/// Relayer protocol handle
pub struct Relayer {
chain: ChainController,
pub(crate) shared: Arc<SyncShared>,
rate_limiter: Arc<Mutex<RateLimiter<(PeerIndex, u32)>>>,
v3: bool,
}
impl Relayer {
/// Init relay protocol handle
///
/// This is a runtime relay protocol shared state, and any relay messages will be processed and forwarded by it
pub fn new(chain: ChainController, shared: Arc<SyncShared>) -> Self {
// setup a rate limiter keyed by peer and message type that lets through 30 requests per second
// current max rps is 10 (ASK_FOR_TXS_TOKEN / TX_PROPOSAL_TOKEN), 30 is a flexible hard cap with buffer
let quota = governor::Quota::per_second(std::num::NonZeroU32::new(30).unwrap());
let rate_limiter = Arc::new(Mutex::new(RateLimiter::keyed(quota)));
Relayer {
chain,
shared,
rate_limiter,
v3: false,
}
}
/// Set relay to v3
pub fn v3(mut self) -> Self {
self.v3 = true;
self
}
/// Get shared state
pub fn shared(&self) -> &Arc<SyncShared> {
&self.shared
}
fn try_process(
&mut self,
nc: Arc<dyn CKBProtocolContext + Sync>,
peer: PeerIndex,
message: packed::RelayMessageUnionReader<'_>,
) -> Status {
// CompactBlock will be verified by POW, it's OK to skip rate limit checking.
let should_check_rate =
!matches!(message, packed::RelayMessageUnionReader::CompactBlock(_));
if should_check_rate
&& self
.rate_limiter
.lock()
.check_key(&(peer, message.item_id()))
.is_err()
{
return StatusCode::TooManyRequests.with_context(message.item_name());
}
match message {
packed::RelayMessageUnionReader::CompactBlock(reader) => {
CompactBlockProcess::new(reader, self, nc, peer).execute()
}
packed::RelayMessageUnionReader::RelayTransactions(reader) => {
// after ckb2023, v2 doesn't work with relay tx
// before ckb2023, v3 doesn't work with relay tx
match RelaySwitch::new(&nc, self.v3) {
RelaySwitch::Ckb2023RelayV2 | RelaySwitch::Ckb2021RelayV3 => {
return Status::ignored()
}
RelaySwitch::Ckb2023RelayV3 | RelaySwitch::Ckb2021RelayV2 => (),
}
if reader.check_data() {
TransactionsProcess::new(reader, self, nc, peer).execute()
} else {
StatusCode::ProtocolMessageIsMalformed
.with_context("RelayTransactions is invalid")
}
}
packed::RelayMessageUnionReader::RelayTransactionHashes(reader) => {
// after ckb2023, v2 doesn't work with relay tx
// before ckb2023, v3 doesn't work with relay tx
match RelaySwitch::new(&nc, self.v3) {
RelaySwitch::Ckb2023RelayV2 | RelaySwitch::Ckb2021RelayV3 => {
return Status::ignored()
}
RelaySwitch::Ckb2023RelayV3 | RelaySwitch::Ckb2021RelayV2 => (),
}
TransactionHashesProcess::new(reader, self, peer).execute()
}
packed::RelayMessageUnionReader::GetRelayTransactions(reader) => {
// after ckb2023, v2 doesn't work with relay tx
// before ckb2023, v3 doesn't work with relay tx
match RelaySwitch::new(&nc, self.v3) {
RelaySwitch::Ckb2023RelayV2 | RelaySwitch::Ckb2021RelayV3 => {
return Status::ignored()
}
RelaySwitch::Ckb2023RelayV3 | RelaySwitch::Ckb2021RelayV2 => (),
}
GetTransactionsProcess::new(reader, self, nc, peer).execute()
}
packed::RelayMessageUnionReader::GetBlockTransactions(reader) => {
GetBlockTransactionsProcess::new(reader, self, nc, peer).execute()
}
packed::RelayMessageUnionReader::BlockTransactions(reader) => {
if reader.check_data() {
BlockTransactionsProcess::new(reader, self, nc, peer).execute()
} else {
StatusCode::ProtocolMessageIsMalformed
.with_context("BlockTransactions is invalid")
}
}
packed::RelayMessageUnionReader::GetBlockProposal(reader) => {
GetBlockProposalProcess::new(reader, self, nc, peer).execute()
}
packed::RelayMessageUnionReader::BlockProposal(reader) => {
BlockProposalProcess::new(reader, self).execute()
}
}
}
fn process(
&mut self,
nc: Arc<dyn CKBProtocolContext + Sync>,
peer: PeerIndex,
message: packed::RelayMessageUnionReader<'_>,
) {
let item_name = message.item_name();
let item_bytes = message.as_slice().len() as u64;
let status = self.try_process(Arc::clone(&nc), peer, message);
metric_ckb_message_bytes(
MetricDirection::In,
&SupportProtocols::RelayV2.name(),
message.item_name(),
Some(status.code()),
item_bytes,
);
if let Some(ban_time) = status.should_ban() {
error_target!(
crate::LOG_TARGET_RELAY,
"receive {} from {}, ban {:?} for {}",
item_name,
peer,
ban_time,
status
);
nc.ban_peer(peer, ban_time, status.to_string());
} else if status.should_warn() {
warn_target!(
crate::LOG_TARGET_RELAY,
"receive {} from {}, {}",
item_name,
peer,
status
);
} else if !status.is_ok() {
debug_target!(
crate::LOG_TARGET_RELAY,
"receive {} from {}, {}",
item_name,
peer,
status
);
}
}
/// Request the transaction corresponding to the proposal id from the specified node
pub fn request_proposal_txs(
&self,
nc: &dyn CKBProtocolContext,
peer: PeerIndex,
block_hash_and_number: BlockNumberAndHash,
proposals: Vec<packed::ProposalShortId>,
) {
let tx_pool = self.shared.shared().tx_pool_controller();
let fresh_proposals: Vec<ProposalShortId> = match tx_pool.fresh_proposals_filter(proposals)
{
Err(err) => {
debug_target!(
crate::LOG_TARGET_RELAY,
"tx_pool fresh_proposals_filter error: {:?}",
err,
);
return;
}
Ok(fresh_proposals) => fresh_proposals.into_iter().unique().collect(),
};
let to_ask_proposals: Vec<ProposalShortId> = self
.shared()
.state()
.insert_inflight_proposals(fresh_proposals.clone(), block_hash_and_number.number)
.into_iter()
.zip(fresh_proposals)
.filter_map(|(firstly_in, id)| if firstly_in { Some(id) } else { None })
.collect();
if !to_ask_proposals.is_empty() {
let content = packed::GetBlockProposal::new_builder()
.block_hash(block_hash_and_number.hash)
.proposals(to_ask_proposals.clone().pack())
.build();
let message = packed::RelayMessage::new_builder().set(content).build();
if !send_message_to(nc, peer, &message).is_ok() {
self.shared()
.state()
.remove_inflight_proposals(&to_ask_proposals);
}
}
}
/// Accept a new block from network
#[allow(clippy::needless_collect)]
pub fn accept_block(
&self,
nc: Arc<dyn CKBProtocolContext + Sync>,
peer_id: PeerIndex,
block: core::BlockView,
msg_name: &str,
) {
if self
.shared()
.active_chain()
.contains_block_status(&block.hash(), BlockStatus::BLOCK_STORED)
{
return;
}
let block = Arc::new(block);
let verify_callback = {
let nc: Arc<dyn CKBProtocolContext + Sync> = Arc::clone(&nc);
let block = Arc::clone(&block);
let shared = Arc::clone(self.shared());
let msg_name = msg_name.to_owned();
Box::new(move |result: VerifyResult| match result {
Ok(verified) => {
if !verified {
debug!(
"block {}-{} has verified already, won't build compact block and broadcast it",
block.number(),
block.hash()
);
return;
}
build_and_broadcast_compact_block(nc.as_ref(), shared.shared(), peer_id, block);
}
Err(err) => {
error!(
"verify block {}-{} failed: {:?}, won't build compact block and broadcast it",
block.number(),
block.hash(),
err
);
let is_internal_db_error = is_internal_db_error(&err);
if is_internal_db_error {
return;
}
// punish the malicious peer
post_sync_process(
nc.as_ref(),
peer_id,
&msg_name,
StatusCode::BlockIsInvalid.with_context(format!(
"block {} is invalid, reason: {}",
block.hash(),
err
)),
);
}
})
};
let remote_block = RemoteBlock {
block,
verify_callback,
};
self.shared.accept_remote_block(&self.chain, remote_block);
}
/// Reorganize the full block according to the compact block/txs/uncles
// nodes should attempt to reconstruct the full block by taking the prefilledtxn transactions
// from the original CompactBlock message and placing them in the marked positions,
// then for each short transaction ID from the original compact_block message, in order,
// find the corresponding transaction either from the BlockTransactions message or
// from other sources and place it in the first available position in the block
// then once the block has been reconstructed, it shall be processed as normal,
// keeping in mind that short_ids are expected to occasionally collide,
// and that nodes must not be penalized for such collisions, wherever they appear.
pub fn reconstruct_block(
&self,
active_chain: &ActiveChain,
compact_block: &packed::CompactBlock,
received_transactions: Vec<core::TransactionView>,
uncles_index: &[u32],
received_uncles: &[core::UncleBlockView],
) -> ReconstructionResult {
let block_txs_len = received_transactions.len();
let compact_block_hash = compact_block.calc_header_hash();
debug_target!(
crate::LOG_TARGET_RELAY,
"start block reconstruction, block hash: {}, received transactions len: {}",
compact_block_hash,
block_txs_len,
);
let mut short_ids_set: HashSet<ProposalShortId> =
compact_block.short_ids().into_iter().collect();
let mut txs_map: HashMap<ProposalShortId, core::TransactionView> = received_transactions
.into_iter()
.filter_map(|tx| {
let short_id = tx.proposal_short_id();
if short_ids_set.remove(&short_id) {
Some((short_id, tx))
} else {
None
}
})
.collect();
if !short_ids_set.is_empty() {
let tx_pool = self.shared.shared().tx_pool_controller();
let fetch_txs = tx_pool.fetch_txs(short_ids_set);
if let Err(e) = fetch_txs {
return ReconstructionResult::Error(StatusCode::TxPool.with_context(e));
}
txs_map.extend(fetch_txs.unwrap());
}
let txs_len = compact_block.txs_len();
let mut block_transactions: Vec<Option<core::TransactionView>> =
Vec::with_capacity(txs_len);
let short_ids_iter = &mut compact_block.short_ids().into_iter();
// fill transactions gap
compact_block
.prefilled_transactions()
.into_iter()
.for_each(|pt| {
let index: usize = pt.index().unpack();
let gap = index - block_transactions.len();
if gap > 0 {
short_ids_iter
.take(gap)
.for_each(|short_id| block_transactions.push(txs_map.remove(&short_id)));
}
block_transactions.push(Some(pt.transaction().into_view()));
});
// append remain transactions
short_ids_iter.for_each(|short_id| block_transactions.push(txs_map.remove(&short_id)));
let missing = block_transactions.iter().any(Option::is_none);
let mut missing_uncles = Vec::with_capacity(compact_block.uncles().len());
let mut uncles = Vec::with_capacity(compact_block.uncles().len());
let mut position = 0;
for (i, uncle_hash) in compact_block.uncles().into_iter().enumerate() {
if uncles_index.contains(&(i as u32)) {
uncles.push(
received_uncles
.get(position)
.expect("have checked the indexes")
.clone()
.data(),
);
position += 1;
continue;
};
let status = active_chain.get_block_status(&uncle_hash);
match status {
BlockStatus::UNKNOWN | BlockStatus::HEADER_VALID => missing_uncles.push(i),
BlockStatus::BLOCK_STORED | BlockStatus::BLOCK_VALID => {
if let Some(uncle) = active_chain.get_block(&uncle_hash) {
uncles.push(uncle.as_uncle().data());
} else {
debug_target!(
crate::LOG_TARGET_RELAY,
"reconstruct_block could not find {:#?} uncle block: {:#?}",
status,
uncle_hash,
);
missing_uncles.push(i);
}
}
BlockStatus::BLOCK_RECEIVED => {
if let Some(uncle) = self
.chain
.get_orphan_block(self.shared().store(), &uncle_hash)
{
uncles.push(uncle.as_uncle().data());
} else {
debug_target!(
crate::LOG_TARGET_RELAY,
"reconstruct_block could not find {:#?} uncle block: {:#?}",
status,
uncle_hash,
);
missing_uncles.push(i);
}
}
BlockStatus::BLOCK_INVALID => {
return ReconstructionResult::Error(
StatusCode::CompactBlockHasInvalidUncle.with_context(uncle_hash),
)
}
_ => missing_uncles.push(i),
}
}
if !missing && missing_uncles.is_empty() {
let txs = block_transactions
.into_iter()
.collect::<Option<Vec<_>>>()
.expect("missing checked, should not fail");
let block = if let Some(extension) = compact_block.extension() {
packed::BlockV1::new_builder()
.header(compact_block.header())
.uncles(uncles.pack())
.transactions(txs.into_iter().map(|tx| tx.data()).pack())
.proposals(compact_block.proposals())
.extension(extension)
.build()
.as_v0()
} else {
packed::Block::new_builder()
.header(compact_block.header())
.uncles(uncles.pack())
.transactions(txs.into_iter().map(|tx| tx.data()).pack())
.proposals(compact_block.proposals())
.build()
}
.into_view();
debug_target!(
crate::LOG_TARGET_RELAY,
"finish block reconstruction, block hash: {}",
compact_block.calc_header_hash(),
);
let compact_block_tx_root = compact_block.header().raw().transactions_root();
let reconstruct_block_tx_root = block.transactions_root();
if compact_block_tx_root != reconstruct_block_tx_root {
if compact_block.short_ids().is_empty()
|| compact_block.short_ids().len() == block_txs_len
{
return ReconstructionResult::Error(
StatusCode::CompactBlockHasUnmatchedTransactionRootWithReconstructedBlock
.with_context(format!(
"Compact_block_tx_root({}) != reconstruct_block_tx_root({})",
compact_block.header().raw().transactions_root(),
block.transactions_root(),
)),
);
} else {
if let Some(metrics) = ckb_metrics::handle() {
metrics.ckb_relay_transaction_short_id_collide.inc();
}
return ReconstructionResult::Collided;
}
}
ReconstructionResult::Block(block)
} else {
let missing_indexes: Vec<usize> = block_transactions
.iter()
.enumerate()
.filter_map(|(i, t)| if t.is_none() { Some(i) } else { None })
.collect();
debug_target!(
crate::LOG_TARGET_RELAY,
"block reconstruction failed, block hash: {}, missing: {}, total: {}",
compact_block.calc_header_hash(),
missing_indexes.len(),
compact_block.short_ids().len(),
);
ReconstructionResult::Missing(missing_indexes, missing_uncles)
}
}
fn prune_tx_proposal_request(&self, nc: &dyn CKBProtocolContext) {
let get_block_proposals = self.shared().state().drain_get_block_proposals();
let tx_pool = self.shared.shared().tx_pool_controller();
let fetch_txs = tx_pool.fetch_txs(
get_block_proposals
.iter()
.map(|kv_pair| kv_pair.key().clone())
.collect(),
);
if let Err(err) = fetch_txs {
debug_target!(
crate::LOG_TARGET_RELAY,
"relayer prune_tx_proposal_request internal error: {:?}",
err,
);
return;
}
let txs = fetch_txs.unwrap();
let mut peer_txs = HashMap::new();
for (id, peer_indices) in get_block_proposals.into_iter() {
if let Some(tx) = txs.get(&id) {
for peer_index in peer_indices {
let tx_set = peer_txs.entry(peer_index).or_insert_with(Vec::new);
tx_set.push(tx.clone());
}
}
}
let send_block_proposals =
|nc: &dyn CKBProtocolContext, peer_index: PeerIndex, txs: Vec<packed::Transaction>| {
let content = packed::BlockProposal::new_builder()
.transactions(txs.into_iter().pack())
.build();
let message = packed::RelayMessage::new_builder().set(content).build();
let status = send_message_to(nc, peer_index, &message);
if !status.is_ok() {
ckb_logger::error!(
"send RelayBlockProposal to {}, status: {:?}",
peer_index,
status
);
}
};
let mut relay_bytes = 0;
let mut relay_proposals = Vec::new();
for (peer_index, txs) in peer_txs {
for tx in txs {
let data = tx.data();
let tx_size = data.total_size();
if relay_bytes + tx_size > MAX_RELAY_TXS_BYTES_PER_BATCH {
send_block_proposals(nc, peer_index, std::mem::take(&mut relay_proposals));
relay_bytes = tx_size;
} else {
relay_bytes += tx_size;
}
relay_proposals.push(data);
}
if !relay_proposals.is_empty() {
send_block_proposals(nc, peer_index, std::mem::take(&mut relay_proposals));
relay_bytes = 0;
}
}
}
/// Ask for relay transaction by hash from all peers
pub fn ask_for_txs(&self, nc: &dyn CKBProtocolContext) {
for (peer, mut tx_hashes) in self.shared().state().pop_ask_for_txs() {
if !tx_hashes.is_empty() {
debug_target!(
crate::LOG_TARGET_RELAY,
"Send get transaction ({} hashes) to {}",
tx_hashes.len(),
peer,
);
tx_hashes.truncate(MAX_RELAY_TXS_NUM_PER_BATCH);
let content = packed::GetRelayTransactions::new_builder()
.tx_hashes(tx_hashes.pack())
.build();
let message = packed::RelayMessage::new_builder().set(content).build();
let status = send_message_to(nc, peer, &message);
if !status.is_ok() {
ckb_logger::error!(
"interrupted request for transactions, status: {:?}",
status
);
}
}
}
}
/// Send bulk of tx hashes to selected peers
pub fn send_bulk_of_tx_hashes(&self, nc: &dyn CKBProtocolContext) {
const BUFFER_SIZE: usize = 42;
let connected_peers = nc.connected_peers();
if connected_peers.is_empty() {
return;
}
let ckb2023 = nc.ckb2023();
let tx_verify_results = self
.shared
.state()
.take_relay_tx_verify_results(MAX_RELAY_TXS_NUM_PER_BATCH);
let mut selected: HashMap<PeerIndex, Vec<Byte32>> = HashMap::default();
{
for tx_verify_result in tx_verify_results {
match tx_verify_result {
TxVerificationResult::Ok {
original_peer,
with_vm_2023,
tx_hash,
} => {
// must all fork or all no-fork
if ckb2023 != with_vm_2023 {
continue;
}
for target in &connected_peers {
match original_peer {
Some(peer) => {
// broadcast tx hash to all connected peers except original peer
if peer != *target {
let hashes = selected
.entry(*target)
.or_insert_with(|| Vec::with_capacity(BUFFER_SIZE));
hashes.push(tx_hash.clone());
}
}
None => {
// since this tx is submitted through local rpc, it is assumed to be a new tx for all connected peers
let hashes = selected
.entry(*target)
.or_insert_with(|| Vec::with_capacity(BUFFER_SIZE));
hashes.push(tx_hash.clone());
self.shared.state().mark_as_known_tx(tx_hash.clone());
}
}
}
}
TxVerificationResult::Reject { tx_hash } => {
self.shared.state().remove_from_known_txs(&tx_hash);
}
TxVerificationResult::UnknownParents { peer, parents } => {
let tx_hashes: Vec<_> = {
let mut tx_filter = self.shared.state().tx_filter();
tx_filter.remove_expired();
parents
.into_iter()
.filter(|tx_hash| !tx_filter.contains(tx_hash))
.collect()
};
self.shared.state().add_ask_for_txs(peer, tx_hashes);
}
}
}
}
for (peer, hashes) in selected {
let content = packed::RelayTransactionHashes::new_builder()
.tx_hashes(hashes.pack())
.build();
let message = packed::RelayMessage::new_builder().set(content).build();
if let Err(err) = nc.filter_broadcast(TargetSession::Single(peer), message.as_bytes()) {
debug_target!(
crate::LOG_TARGET_RELAY,
"relayer send TransactionHashes error: {:?}",
err,
);
}
}
}
}
fn build_and_broadcast_compact_block(
nc: &dyn CKBProtocolContext,
shared: &Shared,
peer: PeerIndex,
block: Arc<BlockView>,
) {
debug_target!(
crate::LOG_TARGET_RELAY,
"[block_relay] relayer accept_block {} {}",
block.header().hash(),
unix_time_as_millis()
);
let block_hash = block.hash();
shared.remove_header_view(&block_hash);
let cb = packed::CompactBlock::build_from_block(&block, &HashSet::new());
let message = packed::RelayMessage::new_builder().set(cb).build();
let selected_peers: Vec<PeerIndex> = nc
.connected_peers()
.into_iter()
.filter(|target_peer| peer != *target_peer)
.take(MAX_RELAY_PEERS)
.collect();
if let Err(err) = nc.quick_filter_broadcast(
TargetSession::Multi(Box::new(selected_peers.into_iter())),
message.as_bytes(),
) {
debug_target!(
crate::LOG_TARGET_RELAY,
"relayer send block when accept block error: {:?}",
err,
);
}
if let Some(p2p_control) = nc.p2p_control() {
let snapshot = shared.snapshot();
let parent_chain_root = {
let mmr = snapshot.chain_root_mmr(block.header().number() - 1);
match mmr.get_root() {
Ok(root) => root,
Err(err) => {
error_target!(
crate::LOG_TARGET_RELAY,
"Generate last state to light client failed: {:?}",
err
);
return;
}
}
};
let tip_header = packed::VerifiableHeader::new_builder()
.header(block.header().data())
.uncles_hash(block.calc_uncles_hash())
.extension(Pack::pack(&block.extension()))
.parent_chain_root(parent_chain_root)
.build();
let light_client_message = {
let content = packed::SendLastState::new_builder()
.last_header(tip_header)
.build();
packed::LightClientMessage::new_builder()
.set(content)
.build()
};
let light_client_peers: HashSet<PeerIndex> = nc
.connected_peers()
.into_iter()
.filter_map(|index| nc.get_peer(index).map(|peer| (index, peer)))
.filter(|(_id, peer)| peer.if_lightclient_subscribed)
.map(|(id, _)| id)
.collect();
if let Err(err) = p2p_control.filter_broadcast(
TargetSession::Filter(Box::new(move |id| light_client_peers.contains(id))),
SupportProtocols::LightClient.protocol_id(),
light_client_message.as_bytes(),
) {
debug_target!(
crate::LOG_TARGET_RELAY,
"relayer send last state to light client when accept block, error: {:?}",
err,
);
}
}
}
#[async_trait]
impl CKBProtocolHandler for Relayer {
async fn init(&mut self, nc: Arc<dyn CKBProtocolContext + Sync>) {
nc.set_notify(Duration::from_millis(100), TX_PROPOSAL_TOKEN)
.await
.expect("set_notify at init is ok");
nc.set_notify(Duration::from_millis(100), ASK_FOR_TXS_TOKEN)
.await
.expect("set_notify at init is ok");
nc.set_notify(Duration::from_millis(300), TX_HASHES_TOKEN)
.await
.expect("set_notify at init is ok");
}
async fn received(
&mut self,
nc: Arc<dyn CKBProtocolContext + Sync>,
peer_index: PeerIndex,
data: Bytes,
) {
// If self is in the IBD state, don't process any relayer message.
if self.shared.active_chain().is_initial_block_download() {
return;
}
let msg = match packed::RelayMessageReader::from_compatible_slice(&data) {
Ok(msg) => {
let item = msg.to_enum();
if let packed::RelayMessageUnionReader::CompactBlock(ref reader) = item {
if reader.count_extra_fields() > 1 {
info_target!(
crate::LOG_TARGET_RELAY,
"Peer {} sends us a malformed message: \
too many fields in CompactBlock",
peer_index
);
nc.ban_peer(
peer_index,
BAD_MESSAGE_BAN_TIME,
String::from(
"send us a malformed message: \
too many fields in CompactBlock",
),
);
return;
} else {
item
}
} else {
match packed::RelayMessageReader::from_slice(&data) {
Ok(msg) => msg.to_enum(),
_ => {
info_target!(
crate::LOG_TARGET_RELAY,
"Peer {} sends us a malformed message: \
too many fields",
peer_index
);
nc.ban_peer(
peer_index,
BAD_MESSAGE_BAN_TIME,
String::from(
"send us a malformed message \
too many fields",
),
);
return;
}
}
}
}
_ => {
info_target!(
crate::LOG_TARGET_RELAY,
"Peer {} sends us a malformed message",
peer_index
);
nc.ban_peer(
peer_index,
BAD_MESSAGE_BAN_TIME,
String::from("send us a malformed message"),
);
return;
}
};
debug_target!(
crate::LOG_TARGET_RELAY,
"received msg {} from {}",
msg.item_name(),
peer_index
);
#[cfg(feature = "with_sentry")]
{
let sentry_hub = sentry::Hub::current();
let _scope_guard = sentry_hub.push_scope();
sentry_hub.configure_scope(|scope| {
scope.set_tag("p2p.protocol", "relayer");
scope.set_tag("p2p.message", msg.item_name());
});
}
let start_time = Instant::now();
tokio::task::block_in_place(|| self.process(nc, peer_index, msg));
debug_target!(
crate::LOG_TARGET_RELAY,
"process message={}, peer={}, cost={:?}",
msg.item_name(),
peer_index,
Instant::now().saturating_duration_since(start_time),
);
}
async fn connected(
&mut self,
_nc: Arc<dyn CKBProtocolContext + Sync>,
peer_index: PeerIndex,
version: &str,
) {
self.shared().state().peers().relay_connected(peer_index);
info_target!(
crate::LOG_TARGET_RELAY,
"RelayProtocol({}).connected peer={}",
version,
peer_index
);
}
async fn disconnected(
&mut self,
_nc: Arc<dyn CKBProtocolContext + Sync>,
peer_index: PeerIndex,
) {
info_target!(
crate::LOG_TARGET_RELAY,
"RelayProtocol.disconnected peer={}",
peer_index
);
// Retains all keys in the rate limiter that were used recently enough.
self.rate_limiter.lock().retain_recent();
}
async fn notify(&mut self, nc: Arc<dyn CKBProtocolContext + Sync>, token: u64) {
// If self is in the IBD state, don't trigger any relayer notify.
if self.shared.active_chain().is_initial_block_download() {
return;
}
match RelaySwitch::new(&nc, self.v3) {
RelaySwitch::Ckb2021RelayV3 => return,
RelaySwitch::Ckb2023RelayV2 => {
if nc.remove_notify(TX_PROPOSAL_TOKEN).await.is_err() {
trace_target!(crate::LOG_TARGET_RELAY, "remove v2 relay notify fail");
}
if nc.remove_notify(ASK_FOR_TXS_TOKEN).await.is_err() {
trace_target!(crate::LOG_TARGET_RELAY, "remove v2 relay notify fail");
}
if nc.remove_notify(TX_HASHES_TOKEN).await.is_err() {
trace_target!(crate::LOG_TARGET_RELAY, "remove v2 relay notify fail");
}
for kv_pair in self.shared().state().peers().state.iter() {
let (peer, state) = kv_pair.pair();
if !state.peer_flags.is_2023edition {
let _ignore = nc.disconnect(*peer, "Evict low-version clients ");
}
}
return;
}
RelaySwitch::Ckb2023RelayV3 | RelaySwitch::Ckb2021RelayV2 => (),
}
let start_time = Instant::now();
trace_target!(crate::LOG_TARGET_RELAY, "start notify token={}", token);
match token {
TX_PROPOSAL_TOKEN => {
tokio::task::block_in_place(|| self.prune_tx_proposal_request(nc.as_ref()))