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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
use crate::{
account_address::AccountAddress,
block_info::{BlockInfo, Round},
epoch_state::EpochState,
on_chain_config::ValidatorSet,
transaction::Version,
validator_verifier::{ValidatorVerifier, VerifyError},
};
use diem_crypto::{ed25519::Ed25519Signature, hash::HashValue};
use diem_crypto_derive::{BCSCryptoHash, CryptoHasher};
#[cfg(any(test, feature = "fuzzing"))]
use proptest_derive::Arbitrary;
use serde::{Deserialize, Serialize};
use std::{
collections::BTreeMap,
fmt::{Display, Formatter},
ops::{Deref, DerefMut},
};
#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize, CryptoHasher, BCSCryptoHash)]
#[cfg_attr(any(test, feature = "fuzzing"), derive(Arbitrary))]
pub struct LedgerInfo {
commit_info: BlockInfo,
consensus_data_hash: HashValue,
}
impl Display for LedgerInfo {
fn fmt(&self, f: &mut Formatter) -> std::fmt::Result {
write!(f, "LedgerInfo: [commit_info: {}]", self.commit_info())
}
}
impl LedgerInfo {
pub fn new(commit_info: BlockInfo, consensus_data_hash: HashValue) -> Self {
Self {
commit_info,
consensus_data_hash,
}
}
pub fn genesis(genesis_state_root_hash: HashValue, validator_set: ValidatorSet) -> Self {
Self::new(
BlockInfo::genesis(genesis_state_root_hash, validator_set),
HashValue::zero(),
)
}
#[cfg(any(test, feature = "fuzzing"))]
pub fn mock_genesis(validator_set: Option<ValidatorSet>) -> Self {
Self::new(BlockInfo::mock_genesis(validator_set), HashValue::zero())
}
pub fn commit_info(&self) -> &BlockInfo {
&self.commit_info
}
pub fn epoch(&self) -> u64 {
self.commit_info.epoch()
}
pub fn next_block_epoch(&self) -> u64 {
self.commit_info.next_block_epoch()
}
pub fn round(&self) -> Round {
self.commit_info.round()
}
pub fn consensus_block_id(&self) -> HashValue {
self.commit_info.id()
}
pub fn transaction_accumulator_hash(&self) -> HashValue {
self.commit_info.executed_state_id()
}
pub fn version(&self) -> Version {
self.commit_info.version()
}
pub fn timestamp_usecs(&self) -> u64 {
self.commit_info.timestamp_usecs()
}
pub fn next_epoch_state(&self) -> Option<&EpochState> {
self.commit_info.next_epoch_state()
}
pub fn ends_epoch(&self) -> bool {
self.next_epoch_state().is_some()
}
pub fn consensus_data_hash(&self) -> HashValue {
self.consensus_data_hash
}
pub fn set_consensus_data_hash(&mut self, consensus_data_hash: HashValue) {
self.consensus_data_hash = consensus_data_hash;
}
}
#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
pub enum LedgerInfoWithSignatures {
V0(LedgerInfoWithV0),
}
impl Display for LedgerInfoWithSignatures {
fn fmt(&self, f: &mut Formatter) -> std::fmt::Result {
match self {
LedgerInfoWithSignatures::V0(ledger) => write!(f, "{}", ledger),
}
}
}
impl LedgerInfoWithSignatures {
pub fn new(
ledger_info: LedgerInfo,
signatures: BTreeMap<AccountAddress, Ed25519Signature>,
) -> Self {
LedgerInfoWithSignatures::V0(LedgerInfoWithV0::new(ledger_info, signatures))
}
pub fn genesis(genesis_state_root_hash: HashValue, validator_set: ValidatorSet) -> Self {
LedgerInfoWithSignatures::V0(LedgerInfoWithV0::genesis(
genesis_state_root_hash,
validator_set,
))
}
}
impl Deref for LedgerInfoWithSignatures {
type Target = LedgerInfoWithV0;
fn deref(&self) -> &LedgerInfoWithV0 {
match &self {
LedgerInfoWithSignatures::V0(ledger) => ledger,
}
}
}
impl DerefMut for LedgerInfoWithSignatures {
fn deref_mut(&mut self) -> &mut LedgerInfoWithV0 {
match self {
LedgerInfoWithSignatures::V0(ref mut ledger) => ledger,
}
}
}
#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
pub struct LedgerInfoWithV0 {
ledger_info: LedgerInfo,
signatures: BTreeMap<AccountAddress, Ed25519Signature>,
}
impl Display for LedgerInfoWithV0 {
fn fmt(&self, f: &mut Formatter) -> std::fmt::Result {
write!(f, "{}", self.ledger_info)
}
}
impl LedgerInfoWithV0 {
pub fn new(
ledger_info: LedgerInfo,
signatures: BTreeMap<AccountAddress, Ed25519Signature>,
) -> Self {
LedgerInfoWithV0 {
ledger_info,
signatures,
}
}
pub fn genesis(genesis_state_root_hash: HashValue, validator_set: ValidatorSet) -> Self {
Self::new(
LedgerInfo::genesis(genesis_state_root_hash, validator_set),
BTreeMap::new(),
)
}
pub fn ledger_info(&self) -> &LedgerInfo {
&self.ledger_info
}
pub fn commit_info(&self) -> &BlockInfo {
self.ledger_info.commit_info()
}
pub fn add_signature(&mut self, validator: AccountAddress, signature: Ed25519Signature) {
self.signatures.entry(validator).or_insert(signature);
}
pub fn remove_signature(&mut self, validator: AccountAddress) {
self.signatures.remove(&validator);
}
pub fn signatures(&self) -> &BTreeMap<AccountAddress, Ed25519Signature> {
&self.signatures
}
pub fn verify_signatures(
&self,
validator: &ValidatorVerifier,
) -> ::std::result::Result<(), VerifyError> {
validator.batch_verify_aggregated_signatures(self.ledger_info(), self.signatures())
}
pub fn check_voting_power(
&self,
validator: &ValidatorVerifier,
) -> ::std::result::Result<(), VerifyError> {
validator.check_voting_power(self.signatures.keys())
}
}
#[cfg(any(test, feature = "fuzzing"))]
use ::proptest::prelude::*;
#[cfg(any(test, feature = "fuzzing"))]
impl Arbitrary for LedgerInfoWithV0 {
type Parameters = ();
type Strategy = BoxedStrategy<Self>;
fn arbitrary_with(_args: Self::Parameters) -> Self::Strategy {
let dummy_signature = Ed25519Signature::dummy_signature();
(
proptest::arbitrary::any::<LedgerInfo>(),
proptest::collection::vec(proptest::arbitrary::any::<AccountAddress>(), 0..100),
)
.prop_map(move |(ledger_info, addresses)| {
let mut signatures = BTreeMap::new();
for address in addresses {
let signature = dummy_signature.clone();
signatures.insert(address, signature);
}
Self {
ledger_info,
signatures,
}
})
.boxed()
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::validator_signer::ValidatorSigner;
#[test]
fn test_signatures_hash() {
let ledger_info = LedgerInfo::new(BlockInfo::empty(), HashValue::random());
const NUM_SIGNERS: u8 = 7;
let validator_signers: Vec<ValidatorSigner> = (0..NUM_SIGNERS)
.map(|i| ValidatorSigner::random([i; 32]))
.collect();
let mut author_to_signature_map = BTreeMap::new();
for validator in validator_signers.iter() {
author_to_signature_map.insert(validator.author(), validator.sign(&ledger_info));
}
let ledger_info_with_signatures =
LedgerInfoWithV0::new(ledger_info.clone(), author_to_signature_map);
let mut author_to_signature_map = BTreeMap::new();
for validator in validator_signers.iter().rev() {
author_to_signature_map.insert(validator.author(), validator.sign(&ledger_info));
}
let ledger_info_with_signatures_reversed =
LedgerInfoWithV0::new(ledger_info, author_to_signature_map);
let ledger_info_with_signatures_bytes =
bcs::to_bytes(&ledger_info_with_signatures).expect("block serialization failed");
let ledger_info_with_signatures_reversed_bytes =
bcs::to_bytes(&ledger_info_with_signatures_reversed)
.expect("block serialization failed");
assert_eq!(
ledger_info_with_signatures_bytes,
ledger_info_with_signatures_reversed_bytes
);
}
}