Skip to content

Commit 33f8dc0

Browse files
authored
Merge pull request #122 from input-output-hk/whankinsiv/drep-state-expansion
feat: Expanded drep_state with historical state and rollback support
2 parents ba31e4f + 2b2f21f commit 33f8dc0

File tree

15 files changed

+974
-190
lines changed

15 files changed

+974
-190
lines changed

common/src/messages.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ pub struct GovernanceProceduresMessage {
129129
pub proposal_procedures: Vec<ProposalProcedure>,
130130

131131
/// Voting
132-
pub voting_procedures: Vec<(DataHash, VotingProcedures)>,
132+
pub voting_procedures: Vec<([u8; 32], VotingProcedures)>,
133133

134134
/// Alonzo-compatible (from Shelley) and Babbage updates
135135
pub alonzo_babbage_updates: Vec<AlonzoBabbageUpdateProposal>,

common/src/queries/accounts.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
1+
use std::collections::HashMap;
2+
13
use crate::{DRepChoice, KeyHash};
24

5+
pub const DEFAULT_ACCOUNTS_QUERY_TOPIC: (&str, &str) =
6+
("accounts-state-query-topic", "cardano.query.accounts");
7+
38
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
49
pub enum AccountsStateQuery {
510
GetAccountInfo { stake_key: Vec<u8> },
@@ -16,6 +21,9 @@ pub enum AccountsStateQuery {
1621

1722
// Pools related queries
1823
GetPoolsLiveStakes { pools_operators: Vec<Vec<u8>> },
24+
25+
// Dreps related queries
26+
GetAccountsDrepDelegationsMap { stake_keys: Vec<Vec<u8>> },
1927
}
2028

2129
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
@@ -35,6 +43,9 @@ pub enum AccountsStateQueryResponse {
3543
// Pools related responses
3644
PoolsLiveStakes(PoolsLiveStakes),
3745

46+
// DReps related responses
47+
AccountsDrepDelegationsMap(HashMap<Vec<u8>, Option<DRepChoice>>),
48+
3849
NotFound,
3950
Error(String),
4051
}

common/src/queries/mod.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
use crate::messages::Message;
2+
use caryatid_sdk::Context;
3+
use std::sync::Arc;
4+
15
pub mod accounts;
26
pub mod addresses;
37
pub mod assets;
@@ -12,3 +16,7 @@ pub mod pools;
1216
pub mod scripts;
1317
pub mod transactions;
1418
pub mod utils;
19+
20+
pub fn get_query_topic(context: Arc<Context<Message>>, topic: (&str, &str)) -> String {
21+
context.config.get_string(topic.0).unwrap_or_else(|_| topic.1.to_string())
22+
}

common/src/state_history.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,3 +89,17 @@ impl<S: Clone + Default> StateHistory<S> {
8989
});
9090
}
9191
}
92+
93+
/// Helper that lets callers initialize the first state with custom config.
94+
impl<S: Clone> StateHistory<S> {
95+
pub fn get_or_init_with<F>(&mut self, init: F) -> S
96+
where
97+
F: FnOnce() -> S,
98+
{
99+
if let Some(current) = self.history.back() {
100+
current.state.clone()
101+
} else {
102+
init()
103+
}
104+
}
105+
}

common/src/types.rs

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -658,6 +658,13 @@ pub struct DRepRegistration {
658658
pub anchor: Option<Anchor>,
659659
}
660660

661+
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
662+
pub struct DRepRegistrationWithPos {
663+
pub reg: DRepRegistration,
664+
pub tx_hash: [u8; 32],
665+
pub cert_index: u64,
666+
}
667+
661668
/// DRep Deregistration = unreg_drep_cert
662669
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
663670
pub struct DRepDeregistration {
@@ -668,6 +675,13 @@ pub struct DRepDeregistration {
668675
pub refund: Lovelace,
669676
}
670677

678+
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
679+
pub struct DRepDeregistrationWithPos {
680+
pub reg: DRepDeregistration,
681+
pub tx_hash: [u8; 32],
682+
pub cert_index: u64,
683+
}
684+
671685
/// DRep Update = update_drep_cert
672686
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
673687
pub struct DRepUpdate {
@@ -678,6 +692,13 @@ pub struct DRepUpdate {
678692
pub anchor: Option<Anchor>,
679693
}
680694

695+
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
696+
pub struct DRepUpdateWithPos {
697+
pub reg: DRepUpdate,
698+
pub tx_hash: [u8; 32],
699+
pub cert_index: u64,
700+
}
701+
681702
pub type CommitteeCredential = Credential;
682703

683704
/// Authorise a committee hot credential
@@ -1209,6 +1230,7 @@ pub enum Vote {
12091230
pub struct VotingProcedure {
12101231
pub vote: Vote,
12111232
pub anchor: Option<Anchor>,
1233+
pub vote_index: u32,
12121234
}
12131235

12141236
#[serde_as]
@@ -1375,13 +1397,13 @@ pub enum TxCertificate {
13751397
ResignCommitteeCold(ResignCommitteeCold),
13761398

13771399
/// DRep registration
1378-
DRepRegistration(DRepRegistration),
1400+
DRepRegistration(DRepRegistrationWithPos),
13791401

13801402
/// DRep deregistration
1381-
DRepDeregistration(DRepDeregistration),
1403+
DRepDeregistration(DRepDeregistrationWithPos),
13821404

13831405
/// DRep update
1384-
DRepUpdate(DRepUpdate),
1406+
DRepUpdate(DRepUpdateWithPos),
13851407
}
13861408

13871409
#[cfg(test)]
@@ -1440,6 +1462,7 @@ mod tests {
14401462
VotingProcedure {
14411463
anchor: None,
14421464
vote: Vote::Abstain,
1465+
vote_index: 0,
14431466
},
14441467
);
14451468
voting.votes.insert(

modules/accounts_state/src/accounts_state.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -439,6 +439,15 @@ impl AccountsState {
439439
})
440440
}
441441

442+
AccountsStateQuery::GetAccountsDrepDelegationsMap { stake_keys } => match state
443+
.get_drep_delegations_map(stake_keys)
444+
{
445+
Some(map) => AccountsStateQueryResponse::AccountsDrepDelegationsMap(map),
446+
None => AccountsStateQueryResponse::Error(
447+
"Error retrieving DRep delegations map".to_string(),
448+
),
449+
},
450+
442451
_ => AccountsStateQueryResponse::Error(format!(
443452
"Unimplemented query variant: {:?}",
444453
query

modules/accounts_state/src/state.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,24 @@ impl State {
133133
.collect()
134134
}
135135

136+
/// Map stake_keys to their delegated DRep
137+
pub fn get_drep_delegations_map(
138+
&self,
139+
stake_keys: &[Vec<u8>],
140+
) -> Option<HashMap<Vec<u8>, Option<DRepChoice>>> {
141+
let accounts = self.stake_addresses.lock().ok()?; // If lock fails, return None
142+
143+
let mut map = HashMap::new();
144+
145+
for stake_key in stake_keys {
146+
let account = accounts.get(stake_key)?;
147+
let maybe_drep = account.delegated_drep.clone();
148+
map.insert(stake_key.clone(), maybe_drep);
149+
}
150+
151+
Some(map)
152+
}
153+
136154
/// Log statistics
137155
fn log_stats(&self) {
138156
info!(num_stake_addresses = self.stake_addresses.lock().unwrap().keys().len(),);

0 commit comments

Comments
 (0)