forked from aptos-labs/aptos-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
leader_reputation_test.rs
220 lines (212 loc) · 6.66 KB
/
leader_reputation_test.rs
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
// Copyright (c) Aptos
// SPDX-License-Identifier: Apache-2.0
use crate::liveness::{
leader_reputation::{
ActiveInactiveHeuristic, LeaderReputation, MetadataBackend, ReputationHeuristic,
},
proposer_election::{next, ProposerElection},
};
use aptos_types::{block_metadata::NewBlockEvent, validator_signer::ValidatorSigner};
use consensus_types::{
block::{block_test_utils::certificate_for_genesis, Block},
common::{Author, Round},
};
use itertools::Itertools;
struct MockHistory {
window_size: usize,
data: Vec<NewBlockEvent>,
}
impl MockHistory {
fn new(window_size: usize, data: Vec<NewBlockEvent>) -> Self {
Self { window_size, data }
}
}
impl MetadataBackend for MockHistory {
fn get_block_metadata(&self, _target_round: Round) -> Vec<NewBlockEvent> {
let start = if self.data.len() > self.window_size {
self.data.len() - self.window_size
} else {
0
};
self.data[start..].to_vec()
}
}
fn create_block(epoch: u64, proposer: Author, voters: Vec<bool>) -> NewBlockEvent {
NewBlockEvent::new(epoch, 0, voters, proposer, 0)
}
#[test]
fn test_simple_heuristic() {
let active_weight = 9;
let inactive_weight = 1;
let mut proposers = vec![];
let mut signers = vec![];
for i in 0..8 {
let signer = ValidatorSigner::random([i; 32]);
proposers.push(signer.author());
signers.push(signer);
}
let heuristic = ActiveInactiveHeuristic::new(proposers[0], active_weight, inactive_weight);
// 1. Window size not enough
let weights = heuristic.get_weights(0, &proposers, &[]);
assert_eq!(weights.len(), proposers.len());
for w in weights {
assert_eq!(w, inactive_weight);
}
// 2. Sliding window with [proposer 0, voters 1, 2], [proposer 0, voters 3]
let weights = heuristic.get_weights(
0,
&proposers,
&[
create_block(
0,
proposers[0],
vec![false, true, true, false, false, false, false, false],
),
create_block(
0,
proposers[0],
vec![false, false, false, true, false, false, false, false],
),
],
);
assert_eq!(weights.len(), proposers.len());
for (i, w) in weights.iter().enumerate() {
let expected = if i < 4 {
active_weight
} else {
inactive_weight
};
assert_eq!(*w, expected);
}
}
#[test]
fn test_epoch_change() {
let active_weight = 9;
let inactive_weight = 1;
let mut proposers = vec![];
let mut signers = vec![];
for i in 0..8 {
let signer = ValidatorSigner::random([i; 32]);
proposers.push(signer.author());
signers.push(signer);
}
let heuristic = ActiveInactiveHeuristic::new(proposers[0], active_weight, inactive_weight);
// History with [proposer 0, voters 1, 2], [proposer 0, voters 3] in current epoch
let weights = heuristic.get_weights(
2,
&proposers,
&[
create_block(
2,
proposers[0],
vec![false, true, true, false, false, false, false, false],
),
create_block(
2,
proposers[0],
vec![false, false, false, true, false, false, false, false],
),
create_block(
1,
proposers[0],
vec![false, true, true, true, true, true, true, true],
),
create_block(
0,
proposers[0],
vec![false, true, true, true, true, true, true, true],
),
],
);
assert_eq!(weights.len(), proposers.len());
for (i, w) in weights.iter().enumerate() {
let expected = if i < 4 {
active_weight
} else {
inactive_weight
};
assert_eq!(*w, expected);
}
}
#[test]
fn test_api() {
let active_weight = 9;
let inactive_weight = 1;
let (proposers, signers): (Vec<_>, Vec<_>) = (0..5)
.map(|i| ValidatorSigner::random([i; 32]))
.sorted_by(|a, b| Ord::cmp(&a.author(), &b.author()))
.map(|signer| (signer.author(), signer))
.unzip();
let history = vec![
create_block(0, proposers[0], vec![false, true, true, false, false]),
create_block(0, proposers[0], vec![false, false, false, true, false]),
];
let leader_reputation = LeaderReputation::new(
0,
proposers.clone(),
Box::new(MockHistory::new(1, history)),
Box::new(ActiveInactiveHeuristic::new(
proposers[0],
active_weight,
inactive_weight,
)),
4,
);
let round = 42u64;
// first metadata is ignored because of window size 1
let expected_weights = vec![
active_weight,
inactive_weight,
inactive_weight,
active_weight,
inactive_weight,
];
let sum = expected_weights.iter().fold(0, |mut s, w| {
s += *w;
s
});
let mut state = round.to_le_bytes().to_vec();
let chosen_weight = next(&mut state) % sum;
let mut expected_index = 0usize;
let mut accu = 0u64;
for (i, w) in expected_weights.iter().enumerate() {
accu += *w;
if accu >= chosen_weight {
expected_index = i;
}
}
let unexpected_index = (expected_index + 1) % proposers.len();
let proposer_election: Box<dyn ProposerElection> = Box::new(leader_reputation);
let output = proposer_election.get_valid_proposer(round);
assert_eq!(output, proposers[expected_index]);
assert!(proposer_election.is_valid_proposer(proposers[expected_index], 42));
assert!(!proposer_election.is_valid_proposer(proposers[unexpected_index], 42));
let good_proposal = Block::new_proposal(
vec![],
round,
1,
certificate_for_genesis(),
&signers[expected_index],
);
assert!(proposer_election.is_valid_proposal(&good_proposal));
let bad_proposal = Block::new_proposal(
vec![],
round,
1,
certificate_for_genesis(),
&signers[unexpected_index],
);
assert!(!proposer_election.is_valid_proposal(&bad_proposal));
let bad_proposal_2 = Block::new_proposal(
vec![],
round,
2,
certificate_for_genesis(),
&signers[expected_index],
);
assert_ne!(good_proposal.id(), bad_proposal_2.id());
// another proposal from the valid proposer should fail
assert!(!proposer_election.is_valid_proposal(&bad_proposal_2));
// good proposal still passes
assert!(proposer_election.is_valid_proposal(&good_proposal));
}