Skip to content

Commit 6511204

Browse files
committed
Refactor: Rename variables for clarity in drep_state.rs, simplify decode_key in spo_distribution_store.rs, and add tempfile as a dev dependency
1 parent 4037f85 commit 6511204

File tree

3 files changed

+13
-31
lines changed

3 files changed

+13
-31
lines changed

modules/accounts_state/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ tracing = { workspace = true }
2525
fjall = "2.11.2"
2626
csv = "1.3.1"
2727
itertools = "0.14.0"
28+
29+
[dev-dependencies]
2830
tempfile = "3"
2931

3032
[lib]

modules/accounts_state/src/spo_distribution_store.rs

Lines changed: 4 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use acropolis_common::{PoolId, StakeAddress};
2-
use anyhow::{anyhow, Context, Result};
2+
use anyhow::Result;
33
use fjall::{Config, Keyspace, PartitionCreateOptions};
44
use std::collections::HashMap;
55

@@ -30,27 +30,10 @@ fn encode_epoch_pool_prefix(epoch: u64, pool_id: &PoolId) -> Vec<u8> {
3030
}
3131

3232
fn decode_key(key: &[u8]) -> Result<(u64, PoolId, StakeAddress)> {
33-
let epoch_bytes: [u8; EPOCH_LEN] = key[..EPOCH_LEN]
34-
.try_into()
35-
.map_err(|_| anyhow!("Failed to extract epoch bytes (offset 0-{})", EPOCH_LEN))?;
36-
let epoch = u64::from_be_bytes(epoch_bytes);
37-
38-
let pool_id: PoolId = key[EPOCH_LEN..EPOCH_LEN + POOL_ID_LENGTH].try_into().map_err(|_| {
39-
anyhow!(
40-
"Failed to extract pool ID bytes (offset {}-{})",
41-
EPOCH_LEN,
42-
EPOCH_LEN + POOL_ID_LENGTH
43-
)
44-
})?;
45-
33+
let epoch = u64::from_be_bytes(key[..EPOCH_LEN].try_into()?);
34+
let pool_id = key[EPOCH_LEN..EPOCH_LEN + POOL_ID_LENGTH].try_into()?;
4635
let stake_address_bytes = &key[EPOCH_LEN + POOL_ID_LENGTH..];
47-
let stake_address = StakeAddress::from_binary(stake_address_bytes).with_context(|| {
48-
format!(
49-
"Failed to decode stake address from {} bytes at offset {}",
50-
stake_address_bytes.len(),
51-
EPOCH_LEN + POOL_ID_LENGTH
52-
)
53-
})?;
36+
let stake_address = StakeAddress::from_binary(stake_address_bytes)?;
5437

5538
Ok((epoch, pool_id, stake_address))
5639
}

modules/drep_state/src/state.rs

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -474,12 +474,12 @@ impl State {
474474
context: &Arc<Context<Message>>,
475475
delegators: &[(&StakeAddress, &DRepChoice)],
476476
) -> Result<()> {
477-
let mut stake_address_to_input = HashMap::with_capacity(delegators.len());
477+
let mut stake_address_to_drep = HashMap::with_capacity(delegators.len());
478478
let mut stake_addresses = Vec::with_capacity(delegators.len());
479479

480480
for &(stake_address, drep) in delegators {
481481
stake_addresses.push(stake_address.clone());
482-
stake_address_to_input.insert(stake_address, (stake_address, drep));
482+
stake_address_to_drep.insert(stake_address, drep);
483483
}
484484

485485
let msg = Arc::new(Message::StateQuery(StateQuery::Accounts(
@@ -501,8 +501,8 @@ impl State {
501501
};
502502

503503
for (stake_address, old_drep_opt) in result_map {
504-
let &(delegator, new_drep_choice) = match stake_address_to_input.get(&stake_address) {
505-
Some(pair) => pair,
504+
let new_drep_choice = match stake_address_to_drep.get(&stake_address) {
505+
Some(&drep) => drep,
506506
None => continue,
507507
};
508508

@@ -516,10 +516,7 @@ impl State {
516516
if old_drep_cred != new_drep_cred {
517517
self.update_historical(&old_drep_cred, false, |entry| {
518518
if let Some(delegators) = entry.delegators.as_mut() {
519-
delegators.retain(|s| {
520-
s.get_credential().get_hash()
521-
!= delegator.get_credential().get_hash()
522-
});
519+
delegators.retain(|s| s.get_hash() != stake_address.get_hash());
523520
}
524521
})?;
525522
}
@@ -529,8 +526,8 @@ impl State {
529526
// Add delegator to new DRep
530527
match self.update_historical(&new_drep_cred, true, |entry| {
531528
if let Some(delegators) = entry.delegators.as_mut() {
532-
if !delegators.contains(delegator) {
533-
delegators.push(delegator.clone());
529+
if !delegators.contains(&stake_address) {
530+
delegators.push(stake_address.clone());
534531
}
535532
}
536533
}) {

0 commit comments

Comments
 (0)