Skip to content

Commit f68b48c

Browse files
committed
Merge branch 'main' of github.com:input-output-hk/acropolis into shd/abstain-votes-conway
2 parents f30c92f + f1b991b commit f68b48c

30 files changed

+422
-363
lines changed

.github/workflows/run-tests-on-push-to-main.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,11 @@ jobs:
3838
--package acropolis_module_drep_state \
3939
--package acropolis_module_epochs_state \
4040
--package acropolis_module_genesis_bootstrapper \
41+
--package acropolis_module_governance_state \
42+
--package acropolis_module_historical_accounts_state \
4143
--package acropolis_module_mithril_snapshot_fetcher \
44+
--package acropolis_module_parameters_state \
45+
--package acropolis_module_rest_blockfrost \
4246
--package acropolis_module_snapshot_bootstrapper \
4347
--package acropolis_module_spdd_state \
4448
--package acropolis_module_spo_state \

common/src/queries/accounts.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ pub enum AccountsStateQuery {
1818
GetAccountRegistrationHistory { account: StakeAddress },
1919
GetAccountDelegationHistory { account: StakeAddress },
2020
GetAccountMIRHistory { account: StakeAddress },
21-
GetAccountWithdrawalHistory { stake_key: Vec<u8> },
21+
GetAccountWithdrawalHistory { account: StakeAddress },
2222
GetAccountAssociatedAddresses { stake_key: Vec<u8> },
2323
GetAccountAssets { stake_key: Vec<u8> },
2424
GetAccountAssetsTotals { stake_key: Vec<u8> },
@@ -52,7 +52,7 @@ pub enum AccountsStateQueryResponse {
5252
AccountRegistrationHistory(Vec<RegistrationUpdate>),
5353
AccountDelegationHistory(Vec<DelegationUpdate>),
5454
AccountMIRHistory(Vec<AccountWithdrawal>),
55-
AccountWithdrawalHistory(AccountWithdrawalHistory),
55+
AccountWithdrawalHistory(Vec<AccountWithdrawal>),
5656
AccountAssociatedAddresses(AccountAssociatedAddresses),
5757
AccountAssets(AccountAssets),
5858
AccountAssetsTotals(AccountAssetsTotals),

common/src/stake_addresses.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -867,6 +867,8 @@ mod tests {
867867
}
868868

869869
mod withdrawal_tests {
870+
use crate::TxIdentifier;
871+
870872
use super::*;
871873

872874
#[test]
@@ -880,6 +882,7 @@ mod tests {
880882
let withdrawal = Withdrawal {
881883
address: stake_address.clone(),
882884
value: 40,
885+
tx_identifier: TxIdentifier::default(),
883886
};
884887
stake_addresses.process_withdrawal(&withdrawal);
885888

@@ -898,6 +901,7 @@ mod tests {
898901
let withdrawal = Withdrawal {
899902
address: stake_address.clone(),
900903
value: 24,
904+
tx_identifier: TxIdentifier::default(),
901905
};
902906
stake_addresses.process_withdrawal(&withdrawal);
903907
assert_eq!(stake_addresses.get(&stake_address).unwrap().rewards, 12);
@@ -906,6 +910,7 @@ mod tests {
906910
let withdrawal = Withdrawal {
907911
address: stake_address.clone(),
908912
value: 2,
913+
tx_identifier: TxIdentifier::default(),
909914
};
910915
stake_addresses.process_withdrawal(&withdrawal);
911916
assert_eq!(stake_addresses.get(&stake_address).unwrap().rewards, 10);
@@ -922,6 +927,7 @@ mod tests {
922927
let withdrawal = Withdrawal {
923928
address: stake_address.clone(),
924929
value: 0,
930+
tx_identifier: TxIdentifier::default(),
925931
};
926932

927933
stake_addresses.process_withdrawal(&withdrawal);
@@ -936,6 +942,7 @@ mod tests {
936942
let withdrawal = Withdrawal {
937943
address: stake_address.clone(),
938944
value: 10,
945+
tx_identifier: TxIdentifier::default(),
939946
};
940947

941948
// Should log error but not panic

common/src/types.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -610,6 +610,9 @@ pub struct Withdrawal {
610610

611611
/// Value to withdraw
612612
pub value: Lovelace,
613+
614+
// Identifier of withdrawal tx
615+
pub tx_identifier: TxIdentifier,
613616
}
614617

615618
/// Treasury pot account

modules/accounts_state/src/rewards.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use std::sync::Arc;
1414
use tracing::{debug, info, warn};
1515

1616
/// Type of reward being given
17-
#[derive(Debug, Clone)]
17+
#[derive(Debug, Clone, PartialEq)]
1818
pub enum RewardType {
1919
Leader,
2020
Member,
@@ -34,7 +34,7 @@ pub struct RewardDetail {
3434
}
3535

3636
/// Result of a rewards calculation
37-
#[derive(Debug, Default)]
37+
#[derive(Debug, Default, Clone)]
3838
pub struct RewardsResult {
3939
/// Epoch these rewards were earned in (when blocks produced)
4040
pub epoch: u64,

modules/accounts_state/src/state.rs

Lines changed: 40 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//! Acropolis AccountsState: State storage
22
use crate::monetary::calculate_monetary_change;
3-
use crate::rewards::{calculate_rewards, RewardsResult};
3+
use crate::rewards::{calculate_rewards, RewardType, RewardsResult};
44
use crate::snapshot::Snapshot;
55
use crate::verifier::Verifier;
66
use acropolis_common::queries::accounts::OptimalPoolSizing;
@@ -116,9 +116,6 @@ pub struct State {
116116
/// Pool refunds to apply next epoch (list of reward accounts to refund to)
117117
pool_refunds: Vec<StakeAddress>,
118118

119-
/// Stake address refunds to apply next epoch
120-
stake_refunds: Vec<(StakeAddress, Lovelace)>,
121-
122119
/// MIRs to pay next epoch
123120
mirs: Vec<MoveInstantaneousReward>,
124121

@@ -324,7 +321,6 @@ impl State {
324321

325322
// Pay the refunds after snapshot, so they don't appear in active_stake
326323
reward_deltas.extend(self.pay_pool_refunds());
327-
reward_deltas.extend(self.pay_stake_refunds());
328324

329325
// Verify pots state
330326
verifier.verify_pots(epoch, &self.pots);
@@ -496,33 +492,6 @@ impl State {
496492
reward_deltas
497493
}
498494

499-
/// Pay stake address refunds
500-
fn pay_stake_refunds(&mut self) -> Vec<StakeRewardDelta> {
501-
let mut reward_deltas = Vec::<StakeRewardDelta>::new();
502-
503-
let refunds = take(&mut self.stake_refunds);
504-
if !refunds.is_empty() {
505-
info!(
506-
"{} deregistered stake addresses, total refunds {}",
507-
refunds.len(),
508-
refunds.iter().map(|(_, n)| n).sum::<Lovelace>()
509-
);
510-
}
511-
512-
// Send them their deposits back
513-
for (stake_address, deposit) in refunds {
514-
let mut stake_addresses = self.stake_addresses.lock().unwrap();
515-
reward_deltas.push(StakeRewardDelta {
516-
stake_address: stake_address.clone(), // Extract hash for delta
517-
delta: deposit as i64,
518-
});
519-
stake_addresses.add_to_reward(&stake_address, deposit);
520-
self.pots.deposits -= deposit;
521-
}
522-
523-
reward_deltas
524-
}
525-
526495
/// Pay MIRs
527496
fn pay_mirs(&mut self) -> Vec<StakeRewardDelta> {
528497
let mut reward_deltas = Vec::<StakeRewardDelta>::new();
@@ -657,36 +626,52 @@ impl State {
657626

658627
// If rewards have been calculated, save the results
659628
if let Some(task) = task.take() {
660-
if let Ok(Ok(reward_result)) = task.await {
661-
// Collect rewards to stake addresses reward deltas
662-
for rewards in reward_result.rewards.values() {
663-
reward_deltas.extend(
664-
rewards
665-
.iter()
666-
.map(|reward| StakeRewardDelta {
667-
stake_address: reward.account.clone(),
668-
delta: reward.amount as i64,
669-
})
670-
.collect::<Vec<_>>(),
671-
);
672-
}
673-
674-
// Verify them
675-
verifier.verify_rewards(reward_result.epoch, &reward_result);
676-
629+
if let Ok(Ok(rewards_result)) = task.await {
677630
// Pay the rewards
678631
let mut stake_addresses = self.stake_addresses.lock().unwrap();
679-
for (_, rewards) in reward_result.rewards {
632+
let mut filtered_rewards_result = rewards_result.clone();
633+
for (spo, rewards) in rewards_result.rewards {
680634
for reward in rewards {
681-
stake_addresses.add_to_reward(&reward.account, reward.amount);
635+
if stake_addresses.is_registered(&reward.account) {
636+
stake_addresses.add_to_reward(&reward.account, reward.amount);
637+
reward_deltas.push(StakeRewardDelta {
638+
stake_address: reward.account.clone(),
639+
delta: reward.amount as i64,
640+
});
641+
} else {
642+
warn!(
643+
"Reward account {} deregistered - paying reward {} to treasury",
644+
reward.account, reward.amount
645+
);
646+
self.pots.treasury += reward.amount;
647+
648+
// Remove from filtered version for comparison and result
649+
if let Some(rewards) = filtered_rewards_result.rewards.get_mut(&spo) {
650+
rewards.retain(|r| r.account != reward.account);
651+
}
652+
653+
if let Some((_, spor)) = filtered_rewards_result
654+
.spo_rewards
655+
.iter_mut()
656+
.find(|(fspo, _)| *fspo == spo)
657+
{
658+
spor.total_rewards -= reward.amount;
659+
if reward.rtype == RewardType::Leader {
660+
spor.operator_rewards -= reward.amount;
661+
}
662+
}
663+
}
682664
}
683665
}
684666

667+
// Verify them
668+
verifier.verify_rewards(&filtered_rewards_result);
669+
685670
// save SPO rewards
686-
spo_rewards = reward_result.spo_rewards.into_iter().collect();
671+
spo_rewards = filtered_rewards_result.spo_rewards.clone();
687672

688673
// Adjust the reserves for next time with amount actually paid
689-
self.pots.reserves -= reward_result.total_paid;
674+
self.pots.reserves -= rewards_result.total_paid;
690675
}
691676
};
692677

@@ -841,8 +826,7 @@ impl State {
841826
}
842827
};
843828

844-
// Schedule refund
845-
self.stake_refunds.push((stake_address.clone(), deposit));
829+
self.pots.deposits -= deposit;
846830

847831
// Add to registration changes
848832
self.current_epoch_registration_changes.lock().unwrap().push(RegistrationChange {
@@ -1314,6 +1298,7 @@ mod tests {
13141298
withdrawals: vec![Withdrawal {
13151299
address: stake_address.clone(),
13161300
value: 39,
1301+
tx_identifier: TxIdentifier::default(),
13171302
}],
13181303
};
13191304

modules/accounts_state/src/verifier.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,8 @@ impl Verifier {
119119
}
120120

121121
/// Verify rewards, logging any errors
122-
pub fn verify_rewards(&self, epoch: u64, rewards: &RewardsResult) {
122+
pub fn verify_rewards(&self, rewards: &RewardsResult) {
123+
let epoch = rewards.epoch;
123124
if let Some(template) = &self.rewards_file_template {
124125
let path = template.replace("{}", &epoch.to_string());
125126

modules/governance_state/src/alonzo_babbage_voting.rs

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,15 @@ use acropolis_common::{
55
use anyhow::{bail, Result};
66
use std::collections::{HashMap, HashSet};
77

8+
// (vote epoch, vote slot, proposal)
9+
type VoteData = (u64, u64, Box<ProtocolParamUpdate>);
10+
811
#[derive(Default)]
912
pub struct AlonzoBabbageVoting {
1013
/// map "enact epoch" (proposal enacts at this epoch end) to voting
11-
/// "voting": map voter (genesis key) => (vote epoch, vote slot, proposal)
14+
/// "voting": map voter (genesis key) => votedata
1215
/// "vote epoch/slot" --- moment, when the vote was cast for the proposal
13-
proposals: HashMap<u64, HashMap<GenesisKeyhash, (u64, u64, Box<ProtocolParamUpdate>)>>,
16+
proposals: HashMap<u64, HashMap<GenesisKeyhash, VoteData>>,
1417
slots_per_epoch: u32,
1518
update_quorum: u32,
1619
}
@@ -31,7 +34,7 @@ impl AlonzoBabbageVoting {
3134
pub fn process_update_proposals(
3235
&mut self,
3336
block_info: &BlockInfo,
34-
updates: &Vec<AlonzoBabbageUpdateProposal>,
37+
updates: &[AlonzoBabbageUpdateProposal],
3538
) -> Result<()> {
3639
if updates.is_empty() {
3740
return Ok(());
@@ -46,7 +49,7 @@ impl AlonzoBabbageVoting {
4649
}
4750

4851
for pp in updates.iter() {
49-
let entry = self.proposals.entry(pp.enactment_epoch + 1).or_insert(HashMap::new());
52+
let entry = self.proposals.entry(pp.enactment_epoch + 1).or_default();
5053
for (k, p) in &pp.proposals {
5154
// A new proposal for key k always replaces the old one
5255
entry.insert(k.clone(), (block_info.epoch, block_info.slot, p.clone()));
@@ -81,7 +84,8 @@ impl AlonzoBabbageVoting {
8184

8285
let votes: Vec<_> = proposals
8386
.iter()
84-
.filter_map(|(k, v)| (v == parameter_update).then(|| k.clone()))
87+
.filter(|&(_, v)| v == parameter_update)
88+
.map(|(k, _)| k.clone())
8589
.collect();
8690

8791
for v in &votes {
@@ -220,7 +224,7 @@ mod tests {
220224
fn extract_mainnet_parameter<T: Clone>(
221225
f: impl Fn(&ProtocolParamUpdate) -> Option<T>,
222226
) -> Result<Vec<(u64, T)>> {
223-
extract_parameter(5, 432_000, &MAINNET_PROPOSALS_JSON, f)
227+
extract_parameter(5, 432_000, MAINNET_PROPOSALS_JSON, f)
224228
}
225229

226230
const DECENTRALISATION: [(u64, f32); 39] = [
@@ -282,9 +286,9 @@ mod tests {
282286
let dcu = extract_mainnet_parameter(|p| p.decentralisation_constant)?;
283287

284288
assert_eq!(DECENTRALISATION.len(), dcu.len());
285-
for i in 0..dcu.len() {
286-
let rat = rational_number_from_f32(DECENTRALISATION[i].1)?;
287-
assert_eq!((DECENTRALISATION[i].0, rat), *dcu.get(i).unwrap());
289+
for (decent, param) in DECENTRALISATION.iter().zip(dcu) {
290+
let rat = rational_number_from_f32(decent.1)?;
291+
assert_eq!((decent.0, rat), param);
288292
}
289293

290294
Ok(())
@@ -319,7 +323,7 @@ mod tests {
319323
fn extract_sanchonet_parameter<T: Clone>(
320324
f: impl Fn(&ProtocolParamUpdate) -> Option<T>,
321325
) -> Result<Vec<(u64, T)>> {
322-
extract_parameter(3, 86_400, &SANCHONET_PROPOSALS_JSON, f)
326+
extract_parameter(3, 86_400, SANCHONET_PROPOSALS_JSON, f)
323327
}
324328

325329
const SANCHONET_PROTOCOL_VERSION: [(u64, (u64, u64)); 3] =

0 commit comments

Comments
 (0)