Skip to content

Commit c889ab7

Browse files
committed
refactor: move blocks to historical state in spo state
1 parent 526262d commit c889ab7

File tree

3 files changed

+53
-48
lines changed

3 files changed

+53
-48
lines changed

modules/spo_state/src/historical_spo_state.rs

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use acropolis_common::{
22
queries::governance::VoteRecord, KeyHash, PoolRegistration, PoolUpdateEvent,
33
};
4-
use imbl::HashSet;
4+
use imbl::{HashSet, OrdMap, Vector};
55
use serde::{Deserialize, Serialize};
66

77
use crate::store_config::StoreConfig;
@@ -17,6 +17,10 @@ pub struct HistoricalSPOState {
1717
pub delegators: Option<HashSet<KeyHash>>,
1818
// SPO's votes
1919
pub votes: Option<Vec<VoteRecord>>,
20+
21+
// blocks
22+
// <Epoch Number, Block Heights>
23+
pub blocks: Option<OrdMap<u64, Vector<u64>>>,
2024
}
2125

2226
impl HistoricalSPOState {
@@ -27,6 +31,7 @@ impl HistoricalSPOState {
2731
updates: store_config.store_updates.then(Vec::new),
2832
delegators: store_config.store_delegators.then(HashSet::new),
2933
votes: store_config.store_votes.then(Vec::new),
34+
blocks: store_config.store_blocks.then(OrdMap::new),
3035
}
3136
}
3237

@@ -55,4 +60,21 @@ impl HistoricalSPOState {
5560
pub fn remove_delegator(&mut self, delegator: &KeyHash) -> Option<bool> {
5661
self.delegators.as_mut().and_then(|delegators| Some(delegators.remove(delegator).is_some()))
5762
}
63+
64+
pub fn get_all_blocks(&self) -> Option<Vec<u64>> {
65+
self.blocks.as_ref().map(|blocks| blocks.values().flatten().cloned().collect())
66+
}
67+
68+
pub fn get_blocks_by_epoch(&self, epoch: u64) -> Option<Vec<u64>> {
69+
self.blocks
70+
.as_ref()
71+
.and_then(|blocks| blocks.get(&epoch).map(|blocks| blocks.iter().cloned().collect()))
72+
}
73+
74+
pub fn add_block(&mut self, epoch: u64, block_number: u64) -> Option<()> {
75+
self.blocks.as_mut().and_then(|blocks| {
76+
blocks.entry(epoch).or_insert_with(Vector::new).push_back(block_number);
77+
Some(())
78+
})
79+
}
5880
}

modules/spo_state/src/spo_state.rs

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -635,17 +635,18 @@ impl SPOState {
635635
}
636636

637637
PoolsStateQuery::GetBlocksByPool { pool_id } => {
638-
match state.get_blocks_by_pool(pool_id) {
639-
Ok(blocks) => PoolsStateQueryResponse::BlocksByPool(blocks),
640-
Err(_) => PoolsStateQueryResponse::Error("Blocks are not enabled".into()),
641-
}
638+
state
639+
.is_historical_blocks_enabled()
640+
.then(|| PoolsStateQueryResponse::BlocksByPool(state.get_blocks_by_pool(pool_id).unwrap_or_default()))
641+
.unwrap_or(PoolsStateQueryResponse::Error("Blocks are not enabled".into()))
642642
}
643643

644644
PoolsStateQuery::GetBlocksByPoolAndEpoch { pool_id, epoch } => {
645-
match state.get_blocks_by_pool_and_epoch(pool_id, *epoch) {
646-
Ok(blocks) => PoolsStateQueryResponse::BlocksByPoolAndEpoch(blocks),
647-
Err(_) => PoolsStateQueryResponse::Error("Blocks are not enabled".into()),
648-
}
645+
state
646+
.is_historical_blocks_enabled()
647+
.then(|| PoolsStateQueryResponse::BlocksByPoolAndEpoch(state.get_blocks_by_pool_and_epoch(pool_id, *epoch)
648+
.unwrap_or_default()))
649+
.unwrap_or(PoolsStateQueryResponse::Error("Blocks are not enabled".into()))
649650
}
650651

651652
PoolsStateQuery::GetPoolUpdates { pool_id } => {

modules/spo_state/src/state.rs

Lines changed: 21 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use acropolis_common::{
1515
VotingProcedures,
1616
};
1717
use anyhow::Result;
18-
use imbl::{HashMap, OrdMap, Vector};
18+
use imbl::HashMap;
1919
use std::sync::{Arc, Mutex};
2020
use tracing::{debug, error, info};
2121

@@ -41,10 +41,6 @@ pub struct State {
4141
// Keyed by pool_id
4242
total_blocks_minted: HashMap<KeyHash, u64>,
4343

44-
/// block heights (numbers) keyed pool id and epoch
45-
/// <Pool Id, <Epoch Number, Block Height>>
46-
blocks: Option<HashMap<KeyHash, OrdMap<u64, Vector<u64>>>>,
47-
4844
/// historical spo state
4945
/// keyed by pool operator id
5046
historical_spos: Option<HashMap<KeyHash, HistoricalSPOState>>,
@@ -68,11 +64,6 @@ impl State {
6864
} else {
6965
None
7066
},
71-
blocks: if config.store_blocks {
72-
Some(HashMap::new())
73-
} else {
74-
None
75-
},
7667
stake_addresses: if config.store_stake_addresses {
7768
Some(Arc::new(Mutex::new(StakeAddressMap::new())))
7869
} else {
@@ -97,7 +88,7 @@ impl State {
9788
self.store_config.store_votes
9889
}
9990

100-
pub fn is_blocks_enabled(&self) -> bool {
91+
pub fn is_historical_blocks_enabled(&self) -> bool {
10192
self.store_config.store_blocks
10293
}
10394

@@ -125,7 +116,6 @@ impl From<SPOState> for State {
125116
vrf_key_hash_to_pool_id_map,
126117
total_blocks_minted: HashMap::new(),
127118
historical_spos: None,
128-
blocks: None,
129119
stake_addresses: None,
130120
}
131121
}
@@ -222,28 +212,21 @@ impl State {
222212

223213
/// Get Blocks by Pool
224214
/// Return Vector of block heights
225-
/// Return Err when store_blocks not enabled
226-
pub fn get_blocks_by_pool(&self, pool_id: &KeyHash) -> Result<Vec<u64>> {
227-
let Some(blocks) = self.blocks.as_ref() else {
228-
return Err(anyhow::anyhow!("Blocks are not enabled"));
215+
/// Return None when store_blocks not enabled
216+
pub fn get_blocks_by_pool(&self, pool_id: &KeyHash) -> Option<Vec<u64>> {
217+
let Some(historical_spos) = self.historical_spos.as_ref() else {
218+
return None;
229219
};
230-
Ok(blocks
231-
.get(pool_id)
232-
.map(|epochs| epochs.values().flatten().cloned().collect())
233-
.unwrap_or_default())
220+
historical_spos.get(pool_id).and_then(|s| s.get_all_blocks())
234221
}
235222

236223
/// Get Blocks by Pool and Epoch
237-
/// Return Err when store_blocks not enabled
238-
pub fn get_blocks_by_pool_and_epoch(&self, pool_id: &KeyHash, epoch: u64) -> Result<Vec<u64>> {
239-
let Some(blocks) = self.blocks.as_ref() else {
240-
return Err(anyhow::anyhow!("Blocks are not enabled"));
224+
/// Return None when store_blocks not enabled
225+
pub fn get_blocks_by_pool_and_epoch(&self, pool_id: &KeyHash, epoch: u64) -> Option<Vec<u64>> {
226+
let Some(historical_spos) = self.historical_spos.as_ref() else {
227+
return None;
241228
};
242-
Ok(blocks
243-
.get(pool_id)
244-
.map(|epochs| epochs.get(&epoch).map(|blocks| blocks.iter().cloned().collect()))
245-
.flatten()
246-
.unwrap_or_default())
229+
historical_spos.get(pool_id).and_then(|s| s.get_blocks_by_epoch(epoch))
247230
}
248231

249232
/// Get Pool Updates
@@ -299,14 +282,13 @@ impl State {
299282

300283
*(self.total_blocks_minted.entry(pool_id.clone()).or_insert(0)) += 1;
301284
// if store_blocks is enabled
302-
if let Some(blocks) = self.blocks.as_mut() {
303-
blocks
304-
.entry(pool_id)
305-
.or_insert_with(OrdMap::new)
306-
.entry(block_info.epoch)
307-
.or_insert_with(Vector::new)
308-
.push_back(block_info.number);
309-
};
285+
if self.is_historical_blocks_enabled() {
286+
if let Some(historical_spos) = self.historical_spos.as_mut() {
287+
if let Some(historical_spo) = historical_spos.get_mut(&pool_id) {
288+
historical_spo.add_block(block_info.epoch, block_info.number);
289+
}
290+
}
291+
}
310292
true
311293
}
312294

@@ -1080,9 +1062,9 @@ mod tests {
10801062
}
10811063

10821064
#[test]
1083-
fn get_blocks_returns_err_when_blocks_not_enabled() {
1065+
fn get_blocks_returns_none_when_blocks_not_enabled() {
10841066
let state = State::default();
1085-
assert!(state.get_blocks_by_pool(&vec![0]).is_err());
1067+
assert!(state.get_blocks_by_pool(&vec![0]).is_none());
10861068
}
10871069

10881070
#[test]

0 commit comments

Comments
 (0)