-
Notifications
You must be signed in to change notification settings - Fork 219
/
get_block_transactions_process.rs
78 lines (70 loc) · 2.4 KB
/
get_block_transactions_process.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
use crate::relayer::Relayer;
use ckb_logger::debug_target;
use ckb_network::{CKBProtocolContext, PeerIndex};
use ckb_types::{packed, prelude::*};
use failure::Error as FailureError;
use std::sync::Arc;
pub struct GetBlockTransactionsProcess<'a> {
message: packed::GetBlockTransactionsReader<'a>,
relayer: &'a Relayer,
nc: Arc<dyn CKBProtocolContext>,
peer: PeerIndex,
}
impl<'a> GetBlockTransactionsProcess<'a> {
pub fn new(
message: packed::GetBlockTransactionsReader<'a>,
relayer: &'a Relayer,
nc: Arc<dyn CKBProtocolContext>,
peer: PeerIndex,
) -> Self {
GetBlockTransactionsProcess {
message,
nc,
relayer,
peer,
}
}
pub fn execute(self) -> Result<(), FailureError> {
let snapshot = self.relayer.shared.snapshot();
let block_hash = self.message.block_hash().to_entity();
debug_target!(
crate::LOG_TARGET_RELAY,
"get_block_transactions {}",
block_hash
);
if let Some(block) = snapshot.get_block(&block_hash) {
let transactions = self
.message
.indexes()
.iter()
.filter_map(|i| {
block
.transactions()
.get(Unpack::<u32>::unpack(&i) as usize)
.cloned()
})
.collect::<Vec<_>>();
let uncles = self
.message
.uncle_indexes()
.iter()
.filter_map(|i| block.uncles().get(Unpack::<u32>::unpack(&i) as usize))
.collect::<Vec<_>>();
let content = packed::BlockTransactions::new_builder()
.block_hash(block_hash)
.transactions(transactions.into_iter().map(|tx| tx.data()).pack())
.uncles(uncles.into_iter().map(|uncle| uncle.data()).pack())
.build();
let message = packed::RelayMessage::new_builder().set(content).build();
let data = message.as_slice().into();
if let Err(err) = self.nc.send_message_to(self.peer, data) {
debug_target!(
crate::LOG_TARGET_RELAY,
"relayer send BlockTransactions error: {:?}",
err
);
}
}
Ok(())
}
}