Skip to content

Commit 4caab30

Browse files
committed
Refactor: Simplify encode_key in spo_distribution_store.rs by removing Result return type and add back documentation for SPDDStore methods and fields
1 parent 981ff15 commit 4caab30

File tree

2 files changed

+19
-13
lines changed

2 files changed

+19
-13
lines changed

modules/accounts_state/src/spo_distribution_store.rs

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,18 @@ const STAKE_ADDRESS_LEN: usize = 29; // 1 byte header + 28 bytes hash
88
const EPOCH_LEN: usize = 8;
99
const TOTAL_KEY_LEN: usize = EPOCH_LEN + POOL_ID_LENGTH + STAKE_ADDRESS_LEN;
1010

11+
// Batch size balances commit overhead vs memory usage
12+
// ~720KB per batch (72 bytes × 10,000)
13+
// ~130 commits for typical epoch (~1.3M delegations)
1114
const BATCH_SIZE: usize = 10_000;
1215

13-
fn encode_key(epoch: u64, pool_id: &PoolId, stake_address: &StakeAddress) -> Result<Vec<u8>> {
16+
fn encode_key(epoch: u64, pool_id: &PoolId, stake_address: &StakeAddress) -> Vec<u8> {
1417
let mut key = Vec::with_capacity(TOTAL_KEY_LEN);
1518
key.extend_from_slice(&epoch.to_be_bytes());
1619
key.extend_from_slice(pool_id.as_ref());
1720
key.extend_from_slice(&stake_address.to_binary());
1821

19-
Ok(key)
22+
key
2023
}
2124

2225
fn encode_epoch_pool_prefix(epoch: u64, pool_id: &PoolId) -> Vec<u8> {
@@ -44,14 +47,22 @@ fn decode_key(key: &[u8]) -> Result<(u64, PoolId, StakeAddress)> {
4447
Ok((epoch, pool_id, stake_address))
4548
}
4649

50+
/// Encode epoch completion marker key
4751
fn encode_epoch_marker(epoch: u64) -> Vec<u8> {
4852
epoch.to_be_bytes().to_vec()
4953
}
5054

5155
pub struct SPDDStore {
5256
keyspace: Keyspace,
57+
/// Partition for all SPDD data
58+
/// Key format: epoch(8 bytes) + pool_id + stake_key
59+
/// Value: amount(8 bytes)
5360
spdd: fjall::PartitionHandle,
61+
/// Partition for epoch completion markers
62+
/// Key format: epoch(8 bytes)
63+
/// Value: "complete"
5464
epoch_markers: fjall::PartitionHandle,
65+
/// Maximum number of epochs to retain (None = unlimited)
5566
retention_epochs: u64,
5667
}
5768

@@ -101,7 +112,7 @@ impl SPDDStore {
101112
&mut self,
102113
epoch: u64,
103114
spdd_state: HashMap<PoolId, Vec<(StakeAddress, u64)>>,
104-
) -> Result<()> {
115+
) -> fjall::Result<()> {
105116
if self.is_epoch_complete(epoch)? {
106117
return Ok(());
107118
}
@@ -111,7 +122,7 @@ impl SPDDStore {
111122
let mut count = 0;
112123
for (pool_id, delegations) in spdd_state {
113124
for (stake_address, amount) in delegations {
114-
let key = encode_key(epoch, &pool_id, &stake_address)?;
125+
let key = encode_key(epoch, &pool_id, &stake_address);
115126
let value = amount.to_be_bytes();
116127
batch.insert(&self.spdd, key, value);
117128

@@ -127,6 +138,7 @@ impl SPDDStore {
127138
batch.commit()?;
128139
}
129140

141+
// Mark epoch as complete (single key operation)
130142
let marker_key = encode_epoch_marker(epoch);
131143
self.epoch_markers.insert(marker_key, b"complete")?;
132144

@@ -139,6 +151,7 @@ impl SPDDStore {
139151
}
140152

141153
pub fn remove_epoch_data(&self, epoch: u64) -> fjall::Result<u64> {
154+
// Remove epoch marker first - if process fails midway, epoch will be marked incomplete
142155
let marker_key = encode_epoch_marker(epoch);
143156
self.epoch_markers.remove(marker_key)?;
144157

modules/accounts_state/src/state.rs

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use acropolis_common::{
1515
protocol_params::ProtocolParams,
1616
stake_addresses::{StakeAddressMap, StakeAddressState},
1717
BlockInfo, DRepChoice, DRepCredential, DelegatedStake, InstantaneousRewardSource,
18-
InstantaneousRewardTarget, KeyHash, Lovelace, MoveInstantaneousReward, PoolId,
18+
InstantaneousRewardTarget, Lovelace, MoveInstantaneousReward, PoolId,
1919
PoolLiveStakeInfo, PoolRegistration, Pot, SPORewards, StakeAddress, StakeRewardDelta,
2020
TxCertificate,
2121
};
@@ -962,14 +962,7 @@ impl State {
962962
mod tests {
963963
use super::*;
964964
use acropolis_common::crypto::{keyhash_224, keyhash_256};
965-
use acropolis_common::{
966-
protocol_params::ConwayParams, rational_number::RationalNumber, Anchor, Committee,
967-
Constitution, CostModel, DRepVotingThresholds, NetworkId, PoolVotingThresholds, Pot,
968-
PotDelta, Ratio, Registration, StakeAddress, StakeAddressDelta, StakeAndVoteDelegation,
969-
StakeCredential, StakeRegistrationAndStakeAndVoteDelegation,
970-
StakeRegistrationAndVoteDelegation, TxCertificateWithPos, TxIdentifier, VoteDelegation,
971-
VrfKeyHash, Withdrawal,
972-
};
965+
use acropolis_common::{protocol_params::ConwayParams, rational_number::RationalNumber, Anchor, Committee, Constitution, CostModel, DRepVotingThresholds, KeyHash, NetworkId, PoolVotingThresholds, Pot, PotDelta, Ratio, Registration, StakeAddress, StakeAddressDelta, StakeAndVoteDelegation, StakeCredential, StakeRegistrationAndStakeAndVoteDelegation, StakeRegistrationAndVoteDelegation, TxCertificateWithPos, TxIdentifier, VoteDelegation, VrfKeyHash, Withdrawal};
973966

974967
// Helper to create a StakeAddress from a byte slice
975968
fn create_address(hash: &[u8]) -> StakeAddress {

0 commit comments

Comments
 (0)