Skip to content

Commit ac49a74

Browse files
adonagybinier
authored andcommitted
Vrf evaluator cleanup (#201)
* fix(block_producer) validate delegator table and requested slots with ledger hash * fix(block_producer): Correct VrfEvaluator event formatting * fix(block_producer): Use correct type for pub keys + remove duplication * fix(block_producer): Use Arc with DelegatorTable
1 parent db91ccb commit ac49a74

12 files changed

+132
-73
lines changed

node/native/src/block_producer/vrf_evaluator.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ pub fn vrf_evaluator(
2020
while let Some(vrf_evaluator_input) = vrf_evaluation_receiver.blocking_recv() {
2121
let mut vrf_result = VrfEvaluationOutput::SlotLost(vrf_evaluator_input.global_slot);
2222

23-
for (index, account) in vrf_evaluator_input.delegatee_table.iter() {
23+
for (index, account) in vrf_evaluator_input.delegator_table.iter() {
2424
let vrf_input = VrfEvaluationInput::new(
2525
keypair.clone(),
2626
vrf_evaluator_input.epoch_seed.clone(),

node/src/block_producer/block_producer_effects.rs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,6 @@ pub fn block_producer_effects<S: crate::Service>(
3232
BlockProducerVrfEvaluatorAction::EvaluateVrf(action) => {
3333
action.effects(&meta, store);
3434
}
35-
// BlockProducerVrfEvaluatorAction::EvaluationPending(action) => {
36-
// action.effects(&meta, store);
37-
// },
3835
BlockProducerVrfEvaluatorAction::EvaluationSuccess(action) => {
3936
let has_won_slot =
4037
matches!(action.vrf_output, vrf::VrfEvaluationOutput::SlotWon(_));

node/src/block_producer/block_producer_state.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,12 @@ impl BlockProducerState {
153153
pub fn vrf_evaluator(&self) -> Option<&BlockProducerVrfEvaluatorState> {
154154
self.with(None, |this| Some(&this.vrf_evaluator))
155155
}
156+
157+
pub fn vrf_evaluator_with_config(
158+
&self,
159+
) -> Option<(&BlockProducerVrfEvaluatorState, &BlockProducerConfig)> {
160+
self.with(None, |this| Some((&this.vrf_evaluator, &this.config)))
161+
}
156162
}
157163

158164
impl BlockProducerCurrentState {

node/src/block_producer/vrf_evaluator/block_producer_vrf_evaluator_actions.rs

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use std::collections::BTreeMap;
2+
use std::sync::Arc;
23

34
use crate::account::AccountPublicKey;
45
use crate::block_producer::{vrf_evaluator::BlockProducerVrfEvaluatorStatus, BlockProducerAction};
@@ -10,7 +11,7 @@ use mina_p2p_messages::v2::{
1011
use serde::{Deserialize, Serialize};
1112
use vrf::VrfEvaluationOutput;
1213

13-
use super::VrfEvaluatorInput;
14+
use super::{DelegatorTable, VrfEvaluatorInput};
1415

1516
pub type BlockProducerVrfEvaluatorActionWithMeta =
1617
redux::ActionWithMeta<BlockProducerVrfEvaluatorAction>;
@@ -32,7 +33,7 @@ pub enum BlockProducerVrfEvaluatorAction {
3233
pub struct BlockProducerVrfEvaluatorUpdateProducerAndDelegatesAction {
3334
pub current_epoch_ledger_hash: LedgerHash,
3435
pub next_epoch_ledger_hash: LedgerHash,
35-
pub producer: String,
36+
pub producer: AccountPublicKey,
3637
}
3738

3839
impl redux::EnablingCondition<crate::State>
@@ -50,8 +51,9 @@ impl redux::EnablingCondition<crate::State>
5051

5152
#[derive(Serialize, Deserialize, Debug, Clone)]
5253
pub struct BlockProducerVrfEvaluatorUpdateProducerAndDelegatesSuccessAction {
53-
pub current_epoch_producer_and_delegators: BTreeMap<AccountIndex, (AccountPublicKey, u64)>,
54-
pub next_epoch_producer_and_delegators: BTreeMap<AccountIndex, (AccountPublicKey, u64)>,
54+
pub current_epoch_producer_and_delegators: Arc<DelegatorTable>,
55+
pub next_epoch_producer_and_delegators: Arc<DelegatorTable>,
56+
pub staking_ledger_hash: LedgerHash,
5557
}
5658

5759
impl redux::EnablingCondition<crate::State>
@@ -62,7 +64,11 @@ impl redux::EnablingCondition<crate::State>
6264
matches!(
6365
this.vrf_evaluator.status,
6466
BlockProducerVrfEvaluatorStatus::DataPending { .. }
65-
)
67+
) && this
68+
.vrf_evaluator
69+
.current_epoch_data
70+
.as_ref()
71+
.is_some_and(|epoch_data| epoch_data.ledger == self.staking_ledger_hash)
6672
})
6773
}
6874
}
@@ -93,10 +99,9 @@ pub struct BlockProducerVrfEvaluatorEvaluationSuccessAction {
9399
impl redux::EnablingCondition<crate::State> for BlockProducerVrfEvaluatorEvaluationSuccessAction {
94100
fn is_enabled(&self, state: &crate::State) -> bool {
95101
state.block_producer.with(false, |this| {
96-
matches!(
97-
this.vrf_evaluator.status,
98-
BlockProducerVrfEvaluatorStatus::SlotsRequested { .. }
99-
)
102+
this.vrf_evaluator
103+
.status
104+
.matches_requsted_slot(self.vrf_output.global_slot(), &self.staking_ledger_hash)
100105
})
101106
}
102107
}

node/src/block_producer/vrf_evaluator/block_producer_vrf_evaluator_effects.rs

Lines changed: 12 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -14,21 +14,13 @@ use super::{
1414

1515
impl BlockProducerVrfEvaluatorEpochDataUpdateAction {
1616
pub fn effects<S: Service>(self, _: &ActionMeta, store: &mut Store<S>) {
17-
// TODO(adonagy): once block producer is enabled
18-
// if let Some(config) = store.state().block_producer.config() {
19-
// store.dispatch(BlockProducerVrfEvaluatorUpdateProducerAndDelegatesAction {
20-
// current_epoch_ledger_hash: self.epoch_data.ledger.hash,
21-
// next_epoch_ledger_hash: self.next_epoch_data.ledger.hash,
22-
// producer: config.pub_key.to_string(),
23-
// });
24-
// }
25-
26-
// let vrf_evaluator_state = store.state().block_producer.vrf_evaluator();
27-
if let Some(vrf_evaluator_state) = store.state().block_producer.vrf_evaluator() {
17+
let vrf_evaluator_state_with_config =
18+
store.state().block_producer.vrf_evaluator_with_config();
19+
if let Some((vrf_evaluator_state, config)) = vrf_evaluator_state_with_config {
2820
store.dispatch(BlockProducerVrfEvaluatorUpdateProducerAndDelegatesAction {
2921
current_epoch_ledger_hash: self.epoch_data.ledger.hash,
3022
next_epoch_ledger_hash: self.next_epoch_data.ledger.hash,
31-
producer: vrf_evaluator_state.producer_pub_key.to_string(),
23+
producer: config.pub_key.clone().into(),
3224
});
3325
}
3426
}
@@ -40,10 +32,6 @@ impl BlockProducerVrfEvaluatorEvaluateVrfAction {
4032
}
4133
}
4234

43-
// impl BlockProducerVrfEvaluatorEvaluationPendingAction {
44-
// pub fn effects<S: Service>(self, _: &ActionMeta, store: &mut Store<S>) {}
45-
// }
46-
4735
impl BlockProducerVrfEvaluatorEvaluationSuccessAction {
4836
pub fn effects<S: Service>(self, _: &ActionMeta, store: &mut Store<S>) {
4937
let vrf_evaluator_state = store.state().block_producer.vrf_evaluator();
@@ -89,23 +77,19 @@ impl BlockProducerVrfEvaluatorEvaluationSuccessAction {
8977

9078
impl BlockProducerVrfEvaluatorUpdateProducerAndDelegatesAction {
9179
pub fn effects<S: Service>(self, _: &ActionMeta, store: &mut Store<S>) {
92-
let current_epoch_producer_and_delegators: std::collections::BTreeMap<
93-
ledger::AccountIndex,
94-
(AccountPublicKey, u64),
95-
> = store
96-
.service
97-
.get_producer_and_delegates(self.current_epoch_ledger_hash, self.producer.clone());
98-
let next_epoch_producer_and_delegators: std::collections::BTreeMap<
99-
ledger::AccountIndex,
100-
(AccountPublicKey, u64),
101-
> = store
80+
let current_epoch_producer_and_delegators = store.service.get_producer_and_delegates(
81+
self.current_epoch_ledger_hash.clone(),
82+
self.producer.clone(),
83+
);
84+
let next_epoch_producer_and_delegators = store
10285
.service
10386
.get_producer_and_delegates(self.next_epoch_ledger_hash, self.producer.clone());
10487

10588
store.dispatch(
10689
BlockProducerVrfEvaluatorUpdateProducerAndDelegatesSuccessAction {
107-
current_epoch_producer_and_delegators,
108-
next_epoch_producer_and_delegators,
90+
current_epoch_producer_and_delegators: current_epoch_producer_and_delegators.into(),
91+
next_epoch_producer_and_delegators: next_epoch_producer_and_delegators.into(),
92+
staking_ledger_hash: self.current_epoch_ledger_hash,
10993
},
11094
);
11195
}

node/src/block_producer/vrf_evaluator/block_producer_vrf_evaluator_event.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ impl std::fmt::Display for BlockProducerVrfEvaluatorEvent {
1313
write!(f, "VrfEvaluator, ")?;
1414
match self {
1515
Self::Evaluated(vrf_output) => {
16-
write!(f, "Evaluated, {:?}", vrf_output)
16+
write!(f, "Evaluated, {}", vrf_output)
1717
}
1818
}
1919
}

node/src/block_producer/vrf_evaluator/block_producer_vrf_evaluator_reducer.rs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,12 @@ impl BlockProducerVrfEvaluatorState {
2121
));
2222
self.current_epoch = Some(action.new_epoch_number);
2323
}
24-
BlockProducerVrfEvaluatorAction::EvaluateVrf(_) => {
25-
// self.status = BlockProducerVrfEvaluatorStatus::Pending(action.vrf_input.global_slot);
26-
self.status = BlockProducerVrfEvaluatorStatus::SlotsRequested { time: meta.time() };
24+
BlockProducerVrfEvaluatorAction::EvaluateVrf(action) => {
25+
self.status = BlockProducerVrfEvaluatorStatus::SlotsRequested {
26+
time: meta.time(),
27+
global_slot: action.vrf_input.global_slot,
28+
staking_ledger_hash: action.vrf_input.staking_ledger_hash.clone(),
29+
};
2730
}
2831
// BlockProducerVrfEvaluatorAction::EvaluationPending(_) => todo!(),
2932
BlockProducerVrfEvaluatorAction::EvaluationSuccess(action) => {
@@ -40,7 +43,11 @@ impl BlockProducerVrfEvaluatorState {
4043
}
4144
vrf::VrfEvaluationOutput::SlotLost(global_slot) => *global_slot,
4245
};
43-
self.status = BlockProducerVrfEvaluatorStatus::SlotsReceived { time: meta.time() };
46+
self.status = BlockProducerVrfEvaluatorStatus::SlotsReceived {
47+
time: meta.time(),
48+
global_slot: global_slot_evaluated,
49+
staking_ledger_hash: action.staking_ledger_hash.clone(),
50+
};
4451
self.latest_evaluated_slot = global_slot_evaluated;
4552
}
4653
BlockProducerVrfEvaluatorAction::UpdateProducerAndDelegates(_) => {

node/src/block_producer/vrf_evaluator/block_producer_vrf_evaluator_service.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use mina_p2p_messages::v2::LedgerHash;
55

66
use crate::account::AccountPublicKey;
77

8-
use super::VrfEvaluatorInput;
8+
use super::{DelegatorTable, VrfEvaluatorInput};
99

1010
pub trait BlockProducerVrfEvaluatorService: redux::Service {
1111
fn evaluate(&mut self, data: VrfEvaluatorInput);
@@ -15,6 +15,6 @@ pub trait BlockProducerVrfEvaluatorLedgerService: redux::Service {
1515
fn get_producer_and_delegates(
1616
&mut self,
1717
ledger_hash: LedgerHash,
18-
producer: String,
19-
) -> BTreeMap<AccountIndex, (AccountPublicKey, u64)>;
18+
producer: AccountPublicKey,
19+
) -> DelegatorTable;
2020
}

node/src/block_producer/vrf_evaluator/block_producer_vrf_evaluator_state.rs

Lines changed: 48 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use std::collections::BTreeMap;
2+
use std::sync::Arc;
23

34
use ledger::AccountIndex;
45
use mina_p2p_messages::v2::LedgerHash;
@@ -8,7 +9,7 @@ use vrf::VrfWonSlot;
89
use crate::account::AccountPublicKey;
910
use crate::BlockProducerConfig;
1011

11-
use super::VrfWonSlotWithHash;
12+
use super::{DelegatorTable, VrfWonSlotWithHash};
1213

1314
// TODO(adonagy): consodilate types, make more clear
1415
// pub type AccountAddressAndBalance = (String, u64);
@@ -19,7 +20,6 @@ pub struct BlockProducerVrfEvaluatorState {
1920
pub won_slots: BTreeMap<u32, VrfWonSlotWithHash>,
2021
pub current_epoch_data: Option<EpochData>,
2122
pub next_epoch_data: Option<EpochData>,
22-
pub producer_pub_key: String,
2323
// TODO(adonagy): move to block producer state probably
2424
pub current_epoch: Option<u32>,
2525
pub current_best_tip_slot: u32,
@@ -30,13 +30,11 @@ pub struct BlockProducerVrfEvaluatorState {
3030

3131
impl BlockProducerVrfEvaluatorState {
3232
pub fn new(now: redux::Timestamp, config: BlockProducerConfig) -> Self {
33-
let producer_pub_key = config.pub_key.to_string();
3433
Self {
3534
status: BlockProducerVrfEvaluatorStatus::Idle { time: now },
3635
won_slots: Default::default(),
3736
current_epoch_data: Default::default(),
3837
next_epoch_data: Default::default(),
39-
producer_pub_key,
4038
current_epoch: None,
4139
current_best_tip_slot: Default::default(),
4240
latest_evaluated_slot: Default::default(),
@@ -50,7 +48,7 @@ impl BlockProducerVrfEvaluatorState {
5048
pub struct EpochData {
5149
pub seed: String,
5250
pub ledger: LedgerHash,
53-
pub delegator_table: BTreeMap<AccountIndex, (AccountPublicKey, u64)>,
51+
pub delegator_table: Arc<DelegatorTable>,
5452
pub total_currency: u64,
5553
}
5654

@@ -67,11 +65,49 @@ impl EpochData {
6765

6866
#[derive(Serialize, Deserialize, Debug, Clone)]
6967
pub enum BlockProducerVrfEvaluatorStatus {
70-
Idle { time: redux::Timestamp },
71-
EpochChanged { time: redux::Timestamp },
72-
DataPending { time: redux::Timestamp },
73-
DataSuccess { time: redux::Timestamp },
74-
DataFail { time: redux::Timestamp },
75-
SlotsRequested { time: redux::Timestamp },
76-
SlotsReceived { time: redux::Timestamp },
68+
Idle {
69+
time: redux::Timestamp,
70+
},
71+
EpochChanged {
72+
time: redux::Timestamp,
73+
},
74+
DataPending {
75+
time: redux::Timestamp,
76+
},
77+
DataSuccess {
78+
time: redux::Timestamp,
79+
},
80+
DataFail {
81+
time: redux::Timestamp,
82+
},
83+
SlotsRequested {
84+
time: redux::Timestamp,
85+
global_slot: u32,
86+
staking_ledger_hash: LedgerHash,
87+
},
88+
SlotsReceived {
89+
time: redux::Timestamp,
90+
global_slot: u32,
91+
staking_ledger_hash: LedgerHash,
92+
},
93+
}
94+
95+
impl BlockProducerVrfEvaluatorStatus {
96+
pub fn matches_requsted_slot(
97+
&self,
98+
expected_global_slot: u32,
99+
expected_staking_ledger_hash: &LedgerHash,
100+
) -> bool {
101+
match self {
102+
Self::SlotsRequested {
103+
global_slot,
104+
staking_ledger_hash,
105+
..
106+
} => {
107+
&expected_global_slot == global_slot
108+
&& expected_staking_ledger_hash == staking_ledger_hash
109+
}
110+
_ => false,
111+
}
112+
}
77113
}

node/src/block_producer/vrf_evaluator/mod.rs

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use crate::account::AccountPublicKey;
33
use ledger::AccountIndex;
44
use mina_p2p_messages::v2::LedgerHash;
55
use std::collections::BTreeMap;
6+
use std::sync::Arc;
67
use vrf::{VrfEvaluationOutput, VrfWonSlot};
78

89
pub use block_producer_vrf_evaluator_state::*;
@@ -23,10 +24,12 @@ mod block_producer_vrf_evaluator_service;
2324
pub use block_producer_vrf_evaluator_service::*;
2425
use serde::{Deserialize, Serialize};
2526

27+
pub type DelegatorTable = BTreeMap<AccountIndex, (AccountPublicKey, u64)>;
28+
2629
#[derive(Debug, Deserialize, Clone, Serialize)]
2730
pub struct VrfEvaluatorInput {
2831
pub epoch_seed: String,
29-
pub delegatee_table: BTreeMap<AccountIndex, (AccountPublicKey, u64)>,
32+
pub delegator_table: Arc<DelegatorTable>,
3033
pub global_slot: u32,
3134
pub total_currency: u64,
3235
pub staking_ledger_hash: LedgerHash,
@@ -53,6 +56,20 @@ pub struct VrfEvaluationOutputWithHash {
5356
pub staking_ledger_hash: LedgerHash,
5457
}
5558

59+
impl std::fmt::Display for VrfEvaluationOutputWithHash {
60+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
61+
write!(f, "{} ", self.staking_ledger_hash.to_string())?;
62+
match &self.evaluation_result {
63+
VrfEvaluationOutput::SlotWon(won_slot) => {
64+
write!(f, "SlotWon {}", won_slot.global_slot)
65+
}
66+
VrfEvaluationOutput::SlotLost(global_slot) => {
67+
write!(f, "SlotLost {}", global_slot)
68+
}
69+
}
70+
}
71+
}
72+
5673
impl VrfEvaluationOutputWithHash {
5774
pub fn new(evaluation_result: VrfEvaluationOutput, staking_ledger_hash: LedgerHash) -> Self {
5875
Self {
@@ -65,14 +82,14 @@ impl VrfEvaluationOutputWithHash {
6582
impl VrfEvaluatorInput {
6683
pub fn new(
6784
epoch_seed: String,
68-
delegatee_table: BTreeMap<AccountIndex, (AccountPublicKey, u64)>,
85+
delegator_table: Arc<DelegatorTable>,
6986
global_slot: u32,
7087
total_currency: u64,
7188
staking_ledger_hash: LedgerHash,
7289
) -> Self {
7390
Self {
7491
epoch_seed,
75-
delegatee_table,
92+
delegator_table,
7693
global_slot,
7794
total_currency,
7895
staking_ledger_hash,

0 commit comments

Comments
 (0)