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
use crate::state_replication::StateComputerCommitCallBackType;
use consensus_types::{common::Author, executed_block::ExecutedBlock};
use diem_crypto::ed25519::Ed25519Signature;
use diem_types::{
account_address::AccountAddress,
block_info::BlockInfo,
ledger_info::{LedgerInfo, LedgerInfoWithSignatures},
validator_verifier::ValidatorVerifier,
};
#[allow(deprecated)]
use itertools::zip;
use std::collections::BTreeMap;
pub struct OrderedBufferItem {
pub pending_votes: BTreeMap<AccountAddress, Ed25519Signature>,
pub callback: StateComputerCommitCallBackType,
pub ordered_blocks: Vec<ExecutedBlock>,
pub ordered_proof: LedgerInfoWithSignatures,
}
pub struct ExecutedBufferItem {
pub executed_blocks: Vec<ExecutedBlock>,
pub pending_votes: BTreeMap<AccountAddress, Ed25519Signature>,
pub callback: StateComputerCommitCallBackType,
pub commit_info: BlockInfo,
pub ordered_proof: LedgerInfoWithSignatures,
}
pub struct SignedBufferItem {
pub executed_blocks: Vec<ExecutedBlock>,
pub commit_proof: LedgerInfoWithSignatures,
pub callback: StateComputerCommitCallBackType,
}
pub struct AggregatedBufferItem {
pub executed_blocks: Vec<ExecutedBlock>,
pub aggregated_proof: LedgerInfoWithSignatures,
pub callback: StateComputerCommitCallBackType,
}
pub enum BufferItem {
Ordered(Box<OrderedBufferItem>),
Executed(Box<ExecutedBufferItem>),
Signed(Box<SignedBufferItem>),
Aggregated(Box<AggregatedBufferItem>),
}
impl BufferItem {
pub fn new_ordered(
ordered_blocks: Vec<ExecutedBlock>,
ordered_proof: LedgerInfoWithSignatures,
callback: StateComputerCommitCallBackType,
) -> Self {
Self::Ordered(Box::new(OrderedBufferItem {
pending_votes: BTreeMap::<AccountAddress, Ed25519Signature>::new(),
callback,
ordered_blocks,
ordered_proof,
}))
}
#[allow(deprecated)]
pub fn advance_to_executed(self, executed_blocks: Vec<ExecutedBlock>) -> Self {
match self {
Self::Ordered(ordered_item_box) => {
let ordered_item = *ordered_item_box;
assert_eq!(ordered_item.ordered_blocks.len(), executed_blocks.len());
for (b1, b2) in zip(ordered_item.ordered_blocks.iter(), executed_blocks.iter()) {
assert_eq!(b1.id(), b2.id());
}
let commit_info = executed_blocks.last().unwrap().block_info();
Self::Executed(Box::new(ExecutedBufferItem {
executed_blocks,
pending_votes: ordered_item.pending_votes,
callback: ordered_item.callback,
commit_info,
ordered_proof: ordered_item.ordered_proof,
}))
}
_ => {
panic!("Only ordered blocks can advance to executed blocks.")
}
}
}
pub fn advance_to_signed(
self,
author: Author,
signature: Ed25519Signature,
verifier: &ValidatorVerifier,
) -> Self {
match self {
Self::Executed(executed_item_box) => {
let executed_item = *executed_item_box;
let mut valid_sigs = BTreeMap::<AccountAddress, Ed25519Signature>::new();
valid_sigs.insert(author, signature);
let commit_ledger_info = LedgerInfo::new(
executed_item.commit_info,
executed_item
.ordered_proof
.ledger_info()
.consensus_data_hash(),
);
for (author, sig) in executed_item.pending_votes.iter() {
if verifier.verify(*author, &commit_ledger_info, sig).is_ok() {
valid_sigs.insert(*author, sig.clone());
}
}
let commit_ledger_info_with_sigs =
LedgerInfoWithSignatures::new(commit_ledger_info, valid_sigs);
Self::Signed(Box::new(SignedBufferItem {
executed_blocks: executed_item.executed_blocks,
callback: executed_item.callback,
commit_proof: commit_ledger_info_with_sigs,
}))
}
_ => {
panic!("Only executed buffer items can advance to signed blocks.")
}
}
}
pub fn try_advance_to_aggregated(self, validator: &ValidatorVerifier) -> Self {
match self {
Self::Signed(signed_item_box) => {
let signed_item = *signed_item_box;
if signed_item
.commit_proof
.check_voting_power(validator)
.is_ok()
{
Self::Aggregated(Box::new(AggregatedBufferItem {
executed_blocks: signed_item.executed_blocks,
aggregated_proof: signed_item.commit_proof,
callback: signed_item.callback,
}))
} else {
Self::Signed(Box::new(signed_item))
}
}
_ => {
panic!("Only signed buffer items can advance to aggregated blocks.")
}
}
}
pub fn get_blocks(&self) -> &Vec<ExecutedBlock> {
match self {
Self::Ordered(ordered) => &ordered.ordered_blocks,
Self::Executed(executed) => &executed.executed_blocks,
Self::Signed(signed) => &signed.executed_blocks,
Self::Aggregated(aggregated) => &aggregated.executed_blocks,
}
}
pub fn get_commit_info(&self) -> &BlockInfo {
match self {
Self::Ordered(_) => {
panic!("Ordered buffer item does not contain commit info");
}
Self::Executed(executed) => &executed.commit_info,
Self::Signed(signed) => signed.commit_proof.ledger_info().commit_info(),
Self::Aggregated(aggregated) => aggregated.aggregated_proof.ledger_info().commit_info(),
}
}
}