Skip to content

Commit 53b29da

Browse files
adonagybinier
authored andcommitted
Include the ledger hash in BlockProducerWonSlot (#192)
* feat(block_producer) rework epoch ledger hash representation + add it to BlockProducerWonSlot * refactor(block_producer): remove unnecessary clone
1 parent be09ee4 commit 53b29da

11 files changed

+228
-70
lines changed

Cargo.lock

Lines changed: 78 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

node/native/src/block_producer/vrf_evaluator.rs

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
use mina_signer::Keypair;
22
use node::{
3-
block_producer::{vrf_evaluator::VrfEvaluatorInput, BlockProducerEvent},
3+
block_producer::{
4+
vrf_evaluator::{VrfEvaluationOutputWithHash, VrfEvaluatorInput},
5+
BlockProducerEvent,
6+
},
47
event_source::Event,
58
};
69
use openmina_core::channels::mpsc::{UnboundedReceiver, UnboundedSender};
@@ -15,14 +18,6 @@ pub fn vrf_evaluator(
1518
keypair: Keypair,
1619
) {
1720
while let Some(vrf_evaluator_input) = vrf_evaluation_receiver.blocking_recv() {
18-
// TODO(adonagy): check correctness of epoch bound calculations
19-
// const SLOT_PER_EPOCH: u32 = 7140;
20-
// let epoch_num = vrf_evaluator_input.start_at_slot / SLOT_PER_EPOCH;
21-
// let end = epoch_num * SLOT_PER_EPOCH + SLOT_PER_EPOCH;
22-
23-
// println!("[vrf] evaluating: {} - {}", vrf_evaluator_input.start_at_slot, end);
24-
25-
// let mut batch: Vec<VrfEvaluationOutput> = Vec::new();
2621
let mut vrf_result = VrfEvaluationOutput::SlotLost(vrf_evaluator_input.global_slot);
2722

2823
for (index, account) in vrf_evaluator_input.delegatee_table.iter() {
@@ -42,10 +37,16 @@ pub fn vrf_evaluator(
4237
break;
4338
}
4439
}
40+
let vrf_result_with_hash = VrfEvaluationOutputWithHash::new(
41+
vrf_result,
42+
vrf_evaluator_input.staking_ledger_hash.clone(),
43+
);
4544
// send the result back to the state machine
4645
let _ = event_sender.send(
47-
BlockProducerEvent::VrfEvaluator(BlockProducerVrfEvaluatorEvent::Evaluated(vrf_result))
48-
.into(),
46+
BlockProducerEvent::VrfEvaluator(BlockProducerVrfEvaluatorEvent::Evaluated(
47+
vrf_result_with_hash,
48+
))
49+
.into(),
4950
);
5051
}
5152
}

node/src/block_producer/block_producer_effects.rs

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -90,22 +90,21 @@ pub fn block_producer_effects<S: crate::Service>(
9090

9191
impl BlockProducerBestTipUpdateAction {
9292
pub fn effects<S: redux::Service>(self, _: &ActionMeta, store: &mut Store<S>) {
93+
let best_tip_staking_ledger = self.best_tip.staking_epoch_ledger_hash();
9394
let protocol_state = &self.best_tip.block.header.protocol_state.body;
9495

95-
let vrf_evaluator_epoch = store.state().block_producer.with(None, |this| {
96-
Some(&this.vrf_evaluator.current_epoch_data.ledger)
97-
});
96+
let vrf_evaluator_current_epoch_ledger = store
97+
.state()
98+
.block_producer
99+
.vrf_evaluator()
100+
.and_then(|vrf_evaluator| {
101+
vrf_evaluator
102+
.current_epoch_data
103+
.as_ref()
104+
.map(|epoch_data| &epoch_data.ledger)
105+
});
98106

99-
if vrf_evaluator_epoch.cloned()
100-
!= Some(
101-
protocol_state
102-
.consensus_state
103-
.staking_epoch_data
104-
.ledger
105-
.hash
106-
.to_string(),
107-
)
108-
{
107+
if vrf_evaluator_current_epoch_ledger != Some(best_tip_staking_ledger) {
109108
store.dispatch(BlockProducerVrfEvaluatorEpochDataUpdateAction {
110109
new_epoch_number: protocol_state.consensus_state.epoch_count.as_u32(),
111110
epoch_data: protocol_state.consensus_state.staking_epoch_data.clone(),

node/src/block_producer/mod.rs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ use vrf::VrfWonSlot;
3838

3939
use crate::account::AccountPublicKey;
4040

41+
use self::vrf_evaluator::VrfWonSlotWithHash;
42+
4143
#[derive(Serialize, Deserialize, Debug, PartialEq, Clone)]
4244
pub struct BlockProducerWonSlot {
4345
pub slot_time: redux::Timestamp,
@@ -49,11 +51,19 @@ pub struct BlockProducerWonSlot {
4951
// calculated on spot from `vrf_output`? Maybe with `vrf_output.blake2b()`?
5052
pub vrf_hash: BigInt,
5153
// Staking ledger which was used during vrf evaluation.
52-
// pub staking_ledger_hash: LedgerHash,
54+
pub staking_ledger_hash: LedgerHash,
5355
}
5456

5557
impl BlockProducerWonSlot {
56-
pub fn from_vrf_won_slot(won_slot: VrfWonSlot, genesis_timestamp: redux::Timestamp) -> Self {
58+
pub fn from_vrf_won_slot(
59+
won_slot_with_hash: VrfWonSlotWithHash,
60+
genesis_timestamp: redux::Timestamp,
61+
) -> Self {
62+
let VrfWonSlotWithHash {
63+
won_slot,
64+
staking_ledger_hash,
65+
} = won_slot_with_hash;
66+
5767
let slot_time = Self::calculate_slot_time(genesis_timestamp, won_slot.global_slot);
5868

5969
let winner_pub_key = AccountPublicKey::from(
@@ -78,6 +88,7 @@ impl BlockProducerWonSlot {
7888
global_slot_since_genesis,
7989
vrf_output,
8090
vrf_hash,
91+
staking_ledger_hash,
8192
}
8293
}
8394

node/src/block_producer/vrf_evaluator/block_producer_vrf_evaluator_actions.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ impl redux::EnablingCondition<crate::State> for BlockProducerVrfEvaluatorEvaluat
8787
#[derive(Serialize, Deserialize, Debug, Clone)]
8888
pub struct BlockProducerVrfEvaluatorEvaluationSuccessAction {
8989
pub vrf_output: VrfEvaluationOutput,
90+
pub staking_ledger_hash: LedgerHash,
9091
}
9192

9293
impl redux::EnablingCondition<crate::State> for BlockProducerVrfEvaluatorEvaluationSuccessAction {

node/src/block_producer/vrf_evaluator/block_producer_vrf_evaluator_effects.rs

Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -53,29 +53,32 @@ impl BlockProducerVrfEvaluatorEvaluationSuccessAction {
5353
// TODO(adonagy): Can we get this from somewhere?
5454
const SLOTS_PER_EPOCH: u32 = 7140;
5555
// determine the epoch of the slot
56-
if let Some(current_epoch) = vrf_evaluator_state.current_epoch {
56+
if let (Some(current_epoch), Some(current_epoch_data), Some(next_epoch_data)) = (
57+
&vrf_evaluator_state.current_epoch,
58+
&vrf_evaluator_state.current_epoch_data,
59+
&vrf_evaluator_state.next_epoch_data,
60+
) {
5761
let current_epoch_end = current_epoch * SLOTS_PER_EPOCH + SLOTS_PER_EPOCH - 1;
5862
let next_epoch_end = (current_epoch + 1) * SLOTS_PER_EPOCH + SLOTS_PER_EPOCH - 1;
5963

6064
// slot is in the current epoch
6165
if next_slot <= current_epoch_end {
6266
let vrf_input: VrfEvaluatorInput = VrfEvaluatorInput::new(
63-
vrf_evaluator_state.current_epoch_data.seed.clone(),
64-
vrf_evaluator_state
65-
.current_epoch_data
66-
.delegator_table
67-
.clone(),
67+
current_epoch_data.seed.clone(),
68+
current_epoch_data.delegator_table.clone(),
6869
next_slot,
69-
vrf_evaluator_state.current_epoch_data.total_currency,
70+
current_epoch_data.total_currency,
71+
current_epoch_data.ledger.clone(),
7072
);
7173
store.dispatch(BlockProducerVrfEvaluatorEvaluateVrfAction { vrf_input });
7274
// slot is in the next epoch
7375
} else if next_slot > current_epoch_end && next_slot <= next_epoch_end {
7476
let vrf_input = VrfEvaluatorInput::new(
75-
vrf_evaluator_state.next_epoch_data.seed.clone(),
76-
vrf_evaluator_state.next_epoch_data.delegator_table.clone(),
77+
next_epoch_data.seed.clone(),
78+
next_epoch_data.delegator_table.clone(),
7779
next_slot,
78-
vrf_evaluator_state.next_epoch_data.total_currency,
80+
next_epoch_data.total_currency,
81+
next_epoch_data.ledger.clone(),
7982
);
8083
store.dispatch(BlockProducerVrfEvaluatorEvaluateVrfAction { vrf_input });
8184
}
@@ -113,16 +116,16 @@ impl BlockProducerVrfEvaluatorUpdateProducerAndDelegatesSuccessAction {
113116
let vrf_evaluator_state = store.state().block_producer.vrf_evaluator();
114117

115118
if let Some(vrf_evaluator_state) = vrf_evaluator_state {
116-
let vrf_input: VrfEvaluatorInput = VrfEvaluatorInput::new(
117-
vrf_evaluator_state.current_epoch_data.seed.clone(),
118-
vrf_evaluator_state
119-
.current_epoch_data
120-
.delegator_table
121-
.clone(),
122-
vrf_evaluator_state.current_best_tip_slot + 1,
123-
vrf_evaluator_state.current_epoch_data.total_currency,
124-
);
125-
store.dispatch(BlockProducerVrfEvaluatorEvaluateVrfAction { vrf_input });
119+
if let Some(current_epoch_data) = &vrf_evaluator_state.current_epoch_data {
120+
let vrf_input: VrfEvaluatorInput = VrfEvaluatorInput::new(
121+
current_epoch_data.seed.clone(),
122+
current_epoch_data.delegator_table.clone(),
123+
vrf_evaluator_state.current_best_tip_slot + 1,
124+
current_epoch_data.total_currency,
125+
current_epoch_data.ledger.clone(),
126+
);
127+
store.dispatch(BlockProducerVrfEvaluatorEvaluateVrfAction { vrf_input });
128+
}
126129
}
127130
}
128131
}

node/src/block_producer/vrf_evaluator/block_producer_vrf_evaluator_event.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
use serde::{Deserialize, Serialize};
22
use vrf::VrfEvaluationOutput;
33

4+
use super::VrfEvaluationOutputWithHash;
5+
46
#[derive(derive_more::From, Serialize, Deserialize, Debug, Clone)]
57
pub enum BlockProducerVrfEvaluatorEvent {
6-
Evaluated(VrfEvaluationOutput),
8+
Evaluated(VrfEvaluationOutputWithHash),
79
}
810

911
impl std::fmt::Display for BlockProducerVrfEvaluatorEvent {

0 commit comments

Comments
 (0)