Skip to content

Commit 7bf3fb3

Browse files
committed
Address review comments.
Put block rewards into a constant, remove duplicated switch block detection from era supervisor, and rename `relative_height` to `height`.
1 parent 558a504 commit 7bf3fb3

File tree

5 files changed

+25
-19
lines changed

5 files changed

+25
-19
lines changed

node/src/components/consensus/consensus_protocol.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,8 @@ pub(crate) enum ConsensusProtocolResult<I, C: ConsensusValueT, VID> {
5353
new_equivocators: Vec<VID>,
5454
rewards: BTreeMap<VID, u64>,
5555
timestamp: Timestamp,
56-
relative_height: u64,
56+
height: u64,
57+
switch_block: bool,
5758
},
5859
/// Request validation of the consensus value, contained in a message received from the given
5960
/// node.

node/src/components/consensus/era_supervisor.rs

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,10 @@ use crate::{
3939
utils::WithDir,
4040
};
4141

42+
// We use one trillion as a block reward unit because it's large enough to allow precise
43+
// fractions, and small enough for many block rewards to fit into a u64.
44+
const BLOCK_REWARD: u64 = 1_000_000_000_000;
45+
4246
#[derive(Debug, Clone, Copy, Hash, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
4347
pub struct EraId(pub(crate) u64);
4448

@@ -58,8 +62,6 @@ impl EraId {
5862
pub(crate) struct Era<I> {
5963
/// The consensus protocol instance.
6064
consensus: Box<dyn ConsensusProtocol<I, ProtoBlock, PublicKey>>,
61-
/// The timestamp of the last block of the previous era.
62-
start_time: Timestamp,
6365
/// The height of this era's first block.
6466
start_height: u64,
6567
}
@@ -163,16 +165,13 @@ where
163165
let ftt = validators.total_weight()
164166
* u64::from(self.highway_config.finality_threshold_percent)
165167
/ 100;
166-
// We use one trillion as a block reward unit because it's large enough to allow precise
167-
// fractions, and small enough for many block rewards to fit into a u64.
168-
let block_reward = 1_000_000_000_000;
169168
// The number of rounds after which a block reward is paid out.
170169
// TODO: Make this configurable?
171170
let reward_delay = 8;
172171
let params = Params::new(
173172
0, // TODO: get a proper seed.
174-
block_reward,
175-
block_reward / 5, // TODO: Make reduced block reward configurable?
173+
BLOCK_REWARD,
174+
BLOCK_REWARD / 5, // TODO: Make reduced block reward configurable?
176175
reward_delay,
177176
self.highway_config.minimum_round_exponent,
178177
self.highway_config.minimum_era_height,
@@ -190,22 +189,23 @@ where
190189

191190
let era = Era {
192191
consensus: Box::new(highway),
193-
start_time,
194192
start_height,
195193
};
196194
let _ = self.active_eras.insert(era_id, era);
197195

198196
results
199197
}
200198

199+
#[allow(clippy::too_many_arguments)]
201200
fn handle_finalized_block(
202201
&mut self,
203202
era_id: EraId,
204203
proto_block: ProtoBlock,
205204
new_equivocators: Vec<PublicKey>,
206205
rewards: BTreeMap<PublicKey, u64>,
207206
timestamp: Timestamp,
208-
relative_height: u64,
207+
height: u64,
208+
switch_block: bool,
209209
) -> (
210210
Vec<ConsensusProtocolResult<I, ProtoBlock, PublicKey>>,
211211
FinalizedBlock,
@@ -221,18 +221,14 @@ where
221221
if !rewards.is_empty() {
222222
system_transactions.push(SystemTransaction::Rewards(rewards));
223223
};
224-
let start_height = self.active_eras[&era_id].start_height;
225-
let start_time = self.active_eras[&era_id].start_time;
226-
let switch_block = relative_height + 1 >= self.highway_config.minimum_era_height
227-
&& timestamp >= start_time + self.highway_config.era_duration;
228224
// Request execution of the finalized block.
229225
let fb = FinalizedBlock::new(
230226
proto_block,
231227
timestamp,
232228
system_transactions,
233229
switch_block,
234230
era_id,
235-
start_height + relative_height,
231+
self.active_eras[&era_id].start_height + height,
236232
);
237233
let results = if fb.switch_block() {
238234
self.current_era_mut().consensus.deactivate_validator();
@@ -457,7 +453,8 @@ where
457453
new_equivocators,
458454
rewards,
459455
timestamp,
460-
relative_height,
456+
height,
457+
switch_block,
461458
} => {
462459
// Announce the finalized proto block.
463460
let mut effects = self
@@ -470,7 +467,8 @@ where
470467
new_equivocators,
471468
rewards,
472469
timestamp,
473-
relative_height,
470+
height,
471+
switch_block,
474472
);
475473
effects.extend(
476474
results

node/src/components/consensus/highway_core/finality_detector.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ pub(crate) enum FinalityOutcome<C: Context> {
3131
timestamp: Timestamp,
3232
/// The relative height in this instance of the protocol.
3333
height: u64,
34+
/// Whether this is a terminal block, i.e. the last one to be finalized.
35+
terminal: bool,
3436
},
3537
/// The fault tolerance threshold has been exceeded: The number of observed equivocations
3638
/// invalidates this finality detector's results.
@@ -82,6 +84,7 @@ impl<C: Context> FinalityDetector<C> {
8284
rewards: rewards_iter.map(|(vidx, r)| (to_id(vidx), *r)).collect(),
8385
timestamp: state.vote(bhash).timestamp,
8486
height: block.height,
87+
terminal: state.is_terminal_block(bhash),
8588
}
8689
}
8790

node/src/components/consensus/highway_core/highway_testing.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -479,6 +479,7 @@ where
479479
rewards,
480480
timestamp,
481481
height,
482+
terminal,
482483
} => {
483484
if !new_equivocators.is_empty() {
484485
warn!("New equivocators detected: {:?}", new_equivocators);
@@ -488,7 +489,8 @@ where
488489
warn!("Rewards are not verified yet: {:?}", rewards);
489490
}
490491
trace!(
491-
"Consensus value finalized: {:?}, height: {:?}",
492+
"{}consensus value finalized: {:?}, height: {:?}",
493+
if terminal { "last " } else { "" },
492494
value,
493495
height
494496
);

node/src/components/consensus/protocols/highway.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,13 +131,15 @@ impl<I: NodeIdT, C: Context> HighwayProtocol<I, C> {
131131
rewards,
132132
timestamp,
133133
height,
134+
terminal,
134135
} => {
135136
results.push(ConsensusProtocolResult::FinalizedBlock {
136137
value,
137138
new_equivocators,
138139
rewards,
139140
timestamp,
140-
relative_height: height,
141+
height,
142+
switch_block: terminal,
141143
});
142144
}
143145
}

0 commit comments

Comments
 (0)