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
use crate::{error::MempoolError, state_replication::TxnManager};
use anyhow::{format_err, Result};
use consensus_types::{block::Block, common::Payload};
use diem_logger::prelude::*;
use diem_mempool::{ConsensusRequest, ConsensusResponse, TransactionSummary};
use diem_metrics::monitor;
use diem_types::transaction::TransactionStatus;
use executor_types::StateComputeResult;
use fail::fail_point;
use futures::channel::{mpsc, oneshot};
use itertools::Itertools;
use std::time::Duration;
use tokio::time::{sleep, timeout};
const NO_TXN_DELAY: u64 = 30;
#[derive(Clone)]
pub struct MempoolProxy {
consensus_to_mempool_sender: mpsc::Sender<ConsensusRequest>,
poll_count: u64,
mempool_executed_txn_timeout_ms: u64,
mempool_txn_pull_timeout_ms: u64,
}
impl MempoolProxy {
pub fn new(
consensus_to_mempool_sender: mpsc::Sender<ConsensusRequest>,
poll_count: u64,
mempool_txn_pull_timeout_ms: u64,
mempool_executed_txn_timeout_ms: u64,
) -> Self {
assert!(
poll_count > 0,
"poll_count = 0 won't pull any txns from mempool"
);
Self {
consensus_to_mempool_sender,
poll_count,
mempool_executed_txn_timeout_ms,
mempool_txn_pull_timeout_ms,
}
}
async fn pull_internal(
&self,
max_size: u64,
exclude_txns: Vec<TransactionSummary>,
) -> Result<Payload, MempoolError> {
let (callback, callback_rcv) = oneshot::channel();
let req = ConsensusRequest::GetBlockRequest(max_size, exclude_txns.clone(), callback);
self.consensus_to_mempool_sender
.clone()
.try_send(req)
.map_err(anyhow::Error::from)?;
match monitor!(
"pull_txn",
timeout(
Duration::from_millis(self.mempool_txn_pull_timeout_ms),
callback_rcv
)
.await
) {
Err(_) => {
Err(anyhow::anyhow!("[consensus] did not receive GetBlockResponse on time").into())
}
Ok(resp) => match resp.map_err(anyhow::Error::from)?? {
ConsensusResponse::GetBlockResponse(txns) => Ok(txns),
_ => Err(
anyhow::anyhow!("[consensus] did not receive expected GetBlockResponse").into(),
),
},
}
}
}
#[async_trait::async_trait]
impl TxnManager for MempoolProxy {
async fn pull_txns(
&self,
max_size: u64,
exclude_payloads: Vec<&Payload>,
) -> Result<Payload, MempoolError> {
fail_point!("consensus::pull_txns", |_| {
Err(anyhow::anyhow!("Injected error in pull_txns").into())
});
let mut exclude_txns = vec![];
for payload in exclude_payloads {
for transaction in payload {
exclude_txns.push(TransactionSummary {
sender: transaction.sender(),
sequence_number: transaction.sequence_number(),
});
}
}
let no_pending_txns = exclude_txns.is_empty();
let mut count = self.poll_count;
let txns = loop {
count -= 1;
let txns = self.pull_internal(max_size, exclude_txns.clone()).await?;
if txns.is_empty() && no_pending_txns && count > 0 {
sleep(Duration::from_millis(NO_TXN_DELAY)).await;
continue;
}
break txns;
};
debug!(
poll_count = self.poll_count - count,
"Pull txn from mempool"
);
Ok(txns)
}
async fn notify(
&self,
block: &Block,
compute_results: &StateComputeResult,
) -> Result<(), MempoolError> {
let mut rejected_txns = vec![];
let txns = match block.payload() {
Some(txns) => txns,
None => return Ok(()),
};
for (txn, status) in txns
.iter()
.zip_eq(compute_results.compute_status().iter().skip(1))
{
if let TransactionStatus::Discard(_) = status {
rejected_txns.push(TransactionSummary {
sender: txn.sender(),
sequence_number: txn.sequence_number(),
});
}
}
if rejected_txns.is_empty() {
return Ok(());
}
let (callback, callback_rcv) = oneshot::channel();
let req = ConsensusRequest::RejectNotification(rejected_txns, callback);
self.consensus_to_mempool_sender
.clone()
.try_send(req)
.map_err(anyhow::Error::from)?;
if let Err(e) = monitor!(
"notify_mempool",
timeout(
Duration::from_millis(self.mempool_executed_txn_timeout_ms),
callback_rcv
)
.await
) {
Err(format_err!("[consensus] txn manager did not receive ACK for commit notification sent to mempool on time: {:?}", e).into())
} else {
Ok(())
}
}
}