Skip to content

Commit e955748

Browse files
committed
refactor: move total_blocks_minted from epochs_state to spo_state
1 parent 21817cd commit e955748

File tree

7 files changed

+210
-177
lines changed

7 files changed

+210
-177
lines changed

common/src/queries/epochs.rs

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,7 @@ pub enum EpochsStateQuery {
1313
GetEpochStakeDistributionByPool { epoch_number: u64 },
1414
GetEpochBlockDistribution { epoch_number: u64 },
1515
GetEpochBlockDistributionByPool { epoch_number: u64 },
16-
17-
// Pools related queries
18-
GetTotalBlocksMintedByPools { vrf_key_hashes: Vec<KeyHash> },
19-
GetBlocksMintedInfoByPool { vrf_key_hash: KeyHash },
16+
GetBlocksMintedByPool { vrf_key_hash: KeyHash },
2017
}
2118

2219
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
@@ -29,10 +26,7 @@ pub enum EpochsStateQueryResponse {
2926
EpochStakeDistributionByPool(EpochStakeDistributionByPool),
3027
EpochBlockDistribution(EpochBlockDistribution),
3128
EpochBlockDistributionByPool(EpochBlockDistributionByPool),
32-
33-
// Pools related responses
34-
TotalBlocksMintedByPools(Vec<u64>),
35-
BlocksMintedInfoByPool(BlocksMintedInfoByPool),
29+
BlocksMintedByPool(u64),
3630

3731
NotFound,
3832
Error(String),
@@ -70,9 +64,3 @@ pub struct EpochBlockDistribution {}
7064

7165
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
7266
pub struct EpochBlockDistributionByPool {}
73-
74-
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
75-
pub struct BlocksMintedInfoByPool {
76-
pub total_blocks_minted: u64,
77-
pub epoch_blocks_minted: u64,
78-
}

common/src/queries/pools.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ pub enum PoolsStateQuery {
2020
pools_operators: Vec<KeyHash>,
2121
epoch: u64,
2222
},
23+
GetPoolsTotalBlocksMinted {
24+
pools_operators: Vec<KeyHash>,
25+
},
2326
GetPoolInfo {
2427
pool_id: KeyHash,
2528
},
@@ -35,7 +38,10 @@ pub enum PoolsStateQuery {
3538
GetPoolDelegators {
3639
pool_id: KeyHash,
3740
},
38-
GetPoolBlocks {
41+
GetPoolTotalBlocksMinted {
42+
pool_id: KeyHash,
43+
},
44+
GetPoolBlockHashes {
3945
pool_id: KeyHash,
4046
},
4147
GetPoolUpdates {
@@ -54,12 +60,14 @@ pub enum PoolsStateQueryResponse {
5460
PoolsRetiringList(Vec<PoolRetirement>),
5561
PoolActiveStakeInfo(PoolActiveStakeInfo),
5662
PoolsActiveStakes(Vec<u64>),
63+
PoolsTotalBlocksMinted(Vec<u64>),
5764
PoolInfo(PoolRegistration),
5865
PoolHistory(Vec<PoolEpochState>),
5966
PoolMetadata(PoolMetadata),
6067
PoolRelays(Vec<Relay>),
6168
PoolDelegators(PoolDelegators),
62-
PoolBlocks(Vec<BlockHash>),
69+
PoolTotalBlocksMinted(u64),
70+
PoolBlockHashes(Vec<BlockHash>),
6371
PoolUpdates(Vec<PoolUpdateEvent>),
6472
PoolVotes(Vec<VoteRecord>),
6573
NotFound,

modules/epochs_state/src/epochs_state.rs

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
use acropolis_common::{
55
messages::{CardanoMessage, Message, StateQuery, StateQueryResponse},
66
queries::epochs::{
7-
BlocksMintedInfoByPool, EpochInfo, EpochsStateQuery, EpochsStateQueryResponse, LatestEpoch,
7+
EpochInfo, EpochsStateQuery, EpochsStateQueryResponse, LatestEpoch,
88
DEFAULT_EPOCHS_QUERY_TOPIC,
99
},
1010
state_history::{StateHistory, StateHistoryStore},
@@ -227,21 +227,12 @@ impl EpochsState {
227227
}
228228
}
229229

230-
EpochsStateQuery::GetTotalBlocksMintedByPools { vrf_key_hashes } => {
231-
EpochsStateQueryResponse::TotalBlocksMintedByPools(
232-
state.get_total_blocks_minted_by_pools(vrf_key_hashes),
230+
EpochsStateQuery::GetBlocksMintedByPool { vrf_key_hash } => {
231+
EpochsStateQueryResponse::BlocksMintedByPool(
232+
state.get_blocks_minted_by_pool(vrf_key_hash),
233233
)
234234
}
235235

236-
EpochsStateQuery::GetBlocksMintedInfoByPool { vrf_key_hash } => {
237-
let (total_blocks_minted, epoch_blocks_minted) =
238-
state.get_blocks_minted_data_by_pool(vrf_key_hash);
239-
EpochsStateQueryResponse::BlocksMintedInfoByPool(BlocksMintedInfoByPool {
240-
total_blocks_minted,
241-
epoch_blocks_minted,
242-
})
243-
}
244-
245236
_ => EpochsStateQueryResponse::Error(format!(
246237
"Unimplemented query variant: {:?}",
247238
query

modules/epochs_state/src/state.rs

Lines changed: 10 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,6 @@ pub struct State {
2020

2121
// fees seen this epoch
2222
epoch_fees: u64,
23-
24-
// Total blocks minted till block number
25-
// Keyed by vrf_key_hash
26-
total_blocks_minted: HashMap<KeyHash, u64>,
2723
}
2824

2925
impl State {
@@ -35,7 +31,6 @@ impl State {
3531
blocks_minted: HashMap::new(),
3632
epoch_blocks: 0,
3733
epoch_fees: 0,
38-
total_blocks_minted: HashMap::new(),
3934
}
4035
}
4136

@@ -46,7 +41,6 @@ impl State {
4641
let vrf_key_hash = keyhash(vrf_vkey);
4742
// Count one on this hash
4843
*(self.blocks_minted.entry(vrf_key_hash.clone()).or_insert(0)) += 1;
49-
*(self.total_blocks_minted.entry(vrf_key_hash.clone()).or_insert(0)) += 1;
5044
}
5145
}
5246

@@ -87,19 +81,8 @@ impl State {
8781
}
8882
}
8983

90-
/// Get epoch's total blocks minted for each vrf key hash till current block number
91-
pub fn get_total_blocks_minted_by_pools(&self, vrf_key_hashes: &Vec<KeyHash>) -> Vec<u64> {
92-
vrf_key_hashes
93-
.iter()
94-
.map(|key_hash| self.total_blocks_minted.get(key_hash).map(|v| *v as u64).unwrap_or(0))
95-
.collect()
96-
}
97-
98-
pub fn get_blocks_minted_data_by_pool(&self, vrf_key_hash: &KeyHash) -> (u64, u64) {
99-
(
100-
self.total_blocks_minted.get(vrf_key_hash).map(|v| *v as u64).unwrap_or(0),
101-
self.blocks_minted.get(vrf_key_hash).map(|v| *v as u64).unwrap_or(0),
102-
)
84+
pub fn get_blocks_minted_by_pool(&self, vrf_key_hash: &KeyHash) -> u64 {
85+
self.blocks_minted.get(vrf_key_hash).map(|v| *v as u64).unwrap_or(0)
10386
}
10487
}
10588

@@ -166,7 +149,6 @@ mod tests {
166149
assert_eq!(state.epoch_blocks, 2);
167150
assert_eq!(state.blocks_minted.len(), 1);
168151
assert_eq!(state.blocks_minted.get(&keyhash(vrf)), Some(&2));
169-
assert_eq!(state.total_blocks_minted.get(&keyhash(vrf)), Some(&2));
170152
}
171153

172154
#[test]
@@ -190,9 +172,8 @@ mod tests {
190172
Some(2)
191173
);
192174

193-
let blocks_minted_data = state.get_blocks_minted_data_by_pool(&keyhash(b"vrf_2"));
194-
assert_eq!(blocks_minted_data.0, 2);
195-
assert_eq!(blocks_minted_data.1, 2);
175+
let blocks_minted = state.get_blocks_minted_by_pool(&keyhash(b"vrf_2"));
176+
assert_eq!(blocks_minted, 2);
196177
}
197178

198179
#[test]
@@ -231,9 +212,8 @@ mod tests {
231212
assert_eq!(state.epoch_fees, 0);
232213
assert!(state.blocks_minted.is_empty());
233214

234-
let blocks_minted_data = state.get_blocks_minted_data_by_pool(&keyhash(b"vrf_1"));
235-
assert_eq!(blocks_minted_data.0, 1);
236-
assert_eq!(blocks_minted_data.1, 0);
215+
let blocks_minted = state.get_blocks_minted_by_pool(&keyhash(b"vrf_1"));
216+
assert_eq!(blocks_minted, 0);
237217
}
238218

239219
#[tokio::test]
@@ -252,35 +232,15 @@ mod tests {
252232
block.number += 1;
253233
state.handle_mint(&block, Some(b"vrf_1"));
254234
state.handle_fees(&block, 123);
235+
assert_eq!(state.get_blocks_minted_by_pool(&keyhash(b"vrf_1")), 2);
255236
history.lock().await.commit(block.number, state);
256237

257-
let mut state = history.lock().await.get_current_state();
258-
block = make_block(2);
259-
let _ = state.end_epoch(&block);
260-
state.handle_mint(&block, Some(b"vrf_1"));
261-
state.handle_fees(&block, 123);
262-
history.lock().await.commit(block.number, state);
263-
264-
let state = history.lock().await.get_current_state();
265-
assert_eq!(state.epoch_blocks, 1);
266-
assert_eq!(state.epoch_fees, 123);
267-
assert_eq!(
268-
3,
269-
state.get_total_blocks_minted_by_pools(&vec![keyhash(b"vrf_1")])[0]
270-
);
271-
272-
// roll back of epoch 2
273-
block = make_rolled_back_block(2);
238+
block = make_rolled_back_block(0);
274239
let mut state = history.lock().await.get_rolled_back_state(block.number);
275-
let _ = state.end_epoch(&block);
276240
state.handle_mint(&block, Some(b"vrf_2"));
277241
state.handle_fees(&block, 123);
242+
assert_eq!(state.get_blocks_minted_by_pool(&keyhash(b"vrf_1")), 0);
243+
assert_eq!(state.get_blocks_minted_by_pool(&keyhash(b"vrf_2")), 1);
278244
history.lock().await.commit(block.number, state);
279-
280-
let state = history.lock().await.get_current_state();
281-
assert_eq!(
282-
2,
283-
state.get_total_blocks_minted_by_pools(&vec![keyhash(b"vrf_1")])[0]
284-
);
285245
}
286246
}

0 commit comments

Comments
 (0)