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
use crate::{
error::StateSyncError,
state_replication::{StateComputer, StateComputerCommitCallBackType},
test_utils::mock_storage::MockStorage,
};
use anyhow::{format_err, Result};
use consensus_types::{block::Block, common::Payload, executed_block::ExecutedBlock};
use diem_crypto::HashValue;
use diem_infallible::Mutex;
use diem_logger::prelude::*;
use diem_types::ledger_info::LedgerInfoWithSignatures;
use executor_types::{Error, StateComputeResult};
use futures::channel::mpsc;
use std::{collections::HashMap, sync::Arc};
use termion::color::*;
pub struct MockStateComputer {
state_sync_client: mpsc::UnboundedSender<Payload>,
commit_callback: mpsc::UnboundedSender<LedgerInfoWithSignatures>,
consensus_db: Arc<MockStorage>,
block_cache: Mutex<HashMap<HashValue, Payload>>,
}
impl MockStateComputer {
pub fn new(
state_sync_client: mpsc::UnboundedSender<Payload>,
commit_callback: mpsc::UnboundedSender<LedgerInfoWithSignatures>,
consensus_db: Arc<MockStorage>,
) -> Self {
MockStateComputer {
state_sync_client,
commit_callback,
consensus_db,
block_cache: Mutex::new(HashMap::new()),
}
}
}
#[async_trait::async_trait]
impl StateComputer for MockStateComputer {
fn compute(
&self,
block: &Block,
_parent_block_id: HashValue,
) -> Result<StateComputeResult, Error> {
self.block_cache
.lock()
.insert(block.id(), block.payload().unwrap_or(&vec![]).clone());
let result = StateComputeResult::new_dummy();
Ok(result)
}
async fn commit(
&self,
blocks: &[Arc<ExecutedBlock>],
commit: LedgerInfoWithSignatures,
call_back: StateComputerCommitCallBackType,
) -> Result<(), Error> {
self.consensus_db
.commit_to_storage(commit.ledger_info().clone());
let mut txns = vec![];
for block in blocks {
let mut payload = self
.block_cache
.lock()
.remove(&block.id())
.ok_or_else(|| format_err!("Cannot find block"))?;
txns.append(&mut payload);
}
let _ = self.state_sync_client.unbounded_send(txns);
let _ = self.commit_callback.unbounded_send(commit.clone());
call_back(blocks, commit);
Ok(())
}
async fn sync_to(&self, commit: LedgerInfoWithSignatures) -> Result<(), StateSyncError> {
debug!(
"{}Fake sync{} to block id {}",
Fg(Blue),
Fg(Reset),
commit.ledger_info().consensus_block_id()
);
self.consensus_db
.commit_to_storage(commit.ledger_info().clone());
self.commit_callback
.unbounded_send(commit)
.expect("Fail to notify about sync");
Ok(())
}
}
pub struct EmptyStateComputer;
#[async_trait::async_trait]
impl StateComputer for EmptyStateComputer {
fn compute(
&self,
_block: &Block,
_parent_block_id: HashValue,
) -> Result<StateComputeResult, Error> {
Ok(StateComputeResult::new_dummy())
}
async fn commit(
&self,
_blocks: &[Arc<ExecutedBlock>],
_commit: LedgerInfoWithSignatures,
_call_back: StateComputerCommitCallBackType,
) -> Result<(), Error> {
Ok(())
}
async fn sync_to(&self, _commit: LedgerInfoWithSignatures) -> Result<(), StateSyncError> {
Ok(())
}
}
pub struct RandomComputeResultStateComputer {
random_compute_result_root_hash: HashValue,
}
impl RandomComputeResultStateComputer {
pub fn new() -> Self {
Self {
random_compute_result_root_hash: HashValue::random(),
}
}
pub fn get_root_hash(&self) -> HashValue {
self.random_compute_result_root_hash
}
}
#[async_trait::async_trait]
impl StateComputer for RandomComputeResultStateComputer {
fn compute(
&self,
_block: &Block,
_parent_block_id: HashValue,
) -> Result<StateComputeResult, Error> {
Ok(StateComputeResult::new_dummy_with_root_hash(
self.random_compute_result_root_hash,
))
}
async fn commit(
&self,
_blocks: &[Arc<ExecutedBlock>],
_commit: LedgerInfoWithSignatures,
_call_back: StateComputerCommitCallBackType,
) -> Result<(), Error> {
Ok(())
}
async fn sync_to(&self, _commit: LedgerInfoWithSignatures) -> Result<(), StateSyncError> {
Ok(())
}
}