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
use crate::{
error::StateSyncError,
state_replication::{StateComputer, StateComputerCommitCallBackType},
};
use anyhow::Result;
use consensus_notifications::ConsensusNotificationSender;
use consensus_types::{block::Block, executed_block::ExecutedBlock};
use diem_crypto::HashValue;
use diem_logger::prelude::*;
use diem_metrics::monitor;
use diem_types::ledger_info::LedgerInfoWithSignatures;
use execution_correctness::ExecutionCorrectness;
use executor_types::{Error as ExecutionError, StateComputeResult};
use fail::fail_point;
use std::{boxed::Box, sync::Arc};
pub struct ExecutionProxy {
execution_correctness_client: Box<dyn ExecutionCorrectness + Send + Sync>,
state_sync_notifier: Box<dyn ConsensusNotificationSender>,
}
impl ExecutionProxy {
pub fn new(
execution_correctness_client: Box<dyn ExecutionCorrectness + Send + Sync>,
state_sync_notifier: Box<dyn ConsensusNotificationSender>,
) -> Self {
Self {
execution_correctness_client,
state_sync_notifier,
}
}
}
#[async_trait::async_trait]
impl StateComputer for ExecutionProxy {
fn compute(
&self,
block: &Block,
parent_block_id: HashValue,
) -> Result<StateComputeResult, ExecutionError> {
fail_point!("consensus::compute", |_| {
Err(ExecutionError::InternalError {
error: "Injected error in compute".into(),
})
});
debug!(
block_id = block.id(),
parent_id = block.parent_id(),
"Executing block",
);
monitor!(
"execute_block",
self.execution_correctness_client
.execute_block(block.clone(), parent_block_id)
)
}
async fn commit(
&self,
blocks: &[Arc<ExecutedBlock>],
finality_proof: LedgerInfoWithSignatures,
callback: StateComputerCommitCallBackType,
) -> Result<(), ExecutionError> {
let mut block_ids = Vec::new();
let mut txns = Vec::new();
let mut reconfig_events = Vec::new();
for block in blocks {
block_ids.push(block.id());
txns.extend(block.transactions_to_commit());
reconfig_events.extend(block.reconfig_event());
}
monitor!(
"commit_block",
self.execution_correctness_client
.commit_blocks(block_ids, finality_proof.clone())?
);
if let Err(e) = monitor!(
"notify_state_sync",
self.state_sync_notifier
.notify_new_commit(txns, reconfig_events)
.await
) {
error!(error = ?e, "Failed to notify state synchronizer");
}
callback(blocks, finality_proof);
Ok(())
}
async fn sync_to(&self, target: LedgerInfoWithSignatures) -> Result<(), StateSyncError> {
fail_point!("consensus::sync_to", |_| {
Err(anyhow::anyhow!("Injected error in sync_to").into())
});
let res = monitor!(
"sync_to",
self.state_sync_notifier.sync_to_target(target).await
);
self.execution_correctness_client.reset()?;
res.map_err(|error| {
let anyhow_error: anyhow::Error = error.into();
anyhow_error.into()
})
}
}