Skip to content

Commit b93b2ff

Browse files
committed
fix: Resolve merge conflicts
Signed-off-by: William Hankins <[email protected]>
2 parents bbbfbfb + c6ce9dd commit b93b2ff

File tree

31 files changed

+1610
-682
lines changed

31 files changed

+1610
-682
lines changed

Cargo.lock

Lines changed: 6 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

common/src/messages.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#![allow(dead_code)]
55

66
use crate::ledger_state::SPOState;
7+
use crate::queries::parameters::{ParametersStateQuery, ParametersStateQueryResponse};
78
use crate::queries::{
89
accounts::{AccountsStateQuery, AccountsStateQueryResponse},
910
addresses::{AddressStateQuery, AddressStateQueryResponse},
@@ -168,6 +169,15 @@ pub struct SPOStakeDistributionMessage {
168169
pub spos: Vec<(KeyHash, DelegatedStake)>,
169170
}
170171

172+
#[derive(Debug, Default, Clone, serde::Serialize, serde::Deserialize)]
173+
pub struct SPORewardsMessage {
174+
/// Epoch which has ended
175+
pub epoch: u64,
176+
177+
/// SPO rewards by operator ID (total rewards before distribution, pool operator's rewards)
178+
pub spos: Vec<(KeyHash, SPORewards)>,
179+
}
180+
171181
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
172182
pub struct ProtocolParamsMessage {
173183
pub params: ProtocolParams,
@@ -224,6 +234,7 @@ pub enum CardanoMessage {
224234
// Stake distribution info
225235
DRepStakeDistribution(DRepStakeDistributionMessage), // Info about drep stake
226236
SPOStakeDistribution(SPOStakeDistributionMessage), // SPO delegation distribution (SPDD)
237+
SPORewards(SPORewardsMessage), // SPO rewards distribution (SPRD)
227238
StakeAddressDeltas(StakeAddressDeltasMessage), // Stake part of address deltas
228239
}
229240

@@ -315,6 +326,7 @@ pub enum StateQuery {
315326
Pools(PoolsStateQuery),
316327
Scripts(ScriptsStateQuery),
317328
Transactions(TransactionsStateQuery),
329+
Parameters(ParametersStateQuery),
318330
}
319331

320332
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
@@ -332,4 +344,5 @@ pub enum StateQueryResponse {
332344
Pools(PoolsStateQueryResponse),
333345
Scripts(ScriptsStateQueryResponse),
334346
Transactions(TransactionsStateQueryResponse),
347+
Parameters(ParametersStateQueryResponse),
335348
}

common/src/queries/epochs.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
use crate::{messages::EpochActivityMessage, KeyHash, ProtocolParams};
22

3+
pub const DEFAULT_EPOCHS_QUERY_TOPIC: (&str, &str) =
4+
("epochs-state-query-topic", "cardano.query.epochs");
5+
36
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
47
pub enum EpochsStateQuery {
58
GetLatestEpoch,

common/src/queries/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ pub mod ledger;
1212
pub mod mempool;
1313
pub mod metadata;
1414
pub mod network;
15+
pub mod parameters;
1516
pub mod pools;
1617
pub mod scripts;
1718
pub mod transactions;

common/src/queries/parameters.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
use crate::ProtocolParams;
2+
3+
pub const DEFAULT_PARAMETERS_QUERY_TOPIC: (&str, &str) =
4+
("parameters-state-query-topic", "cardano.query.parameters");
5+
6+
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
7+
pub enum ParametersStateQuery {
8+
GetLatestParameters,
9+
}
10+
11+
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
12+
pub enum ParametersStateQueryResponse {
13+
LatestParameters(LatestParameters),
14+
15+
NotFound,
16+
Error(String),
17+
}
18+
19+
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
20+
pub struct LatestParameters {
21+
pub parameters: ProtocolParams,
22+
}

common/src/queries/pools.rs

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
use crate::PoolRegistration;
1+
use crate::{KeyHash, PoolEpochState, PoolRegistration, PoolRetirement};
2+
3+
pub const DEFAULT_POOLS_QUERY_TOPIC: (&str, &str) =
4+
("pools-state-query-topic", "cardano.query.pools");
25

36
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
47
pub enum PoolsStateQuery {
@@ -62,19 +65,24 @@ pub enum PoolsStateQueryResponse {
6265

6366
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
6467
pub struct PoolsList {
65-
pub pool_operators: Vec<Vec<u8>>,
68+
pub pool_operators: Vec<KeyHash>,
6669
}
6770

6871
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
6972
pub struct PoolsListWithInfo {
70-
pub pools: Vec<(Vec<u8>, PoolRegistration)>,
73+
pub pools: Vec<(KeyHash, PoolRegistration)>,
7174
}
7275

7376
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
74-
pub struct PoolsRetiredList {}
77+
pub struct PoolsRetiredList {
78+
pub retired_pools: Vec<PoolRetirement>,
79+
}
7580

7681
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
77-
pub struct PoolsRetiringList {}
82+
pub struct PoolsRetiringList {
83+
// pool id, retiring epoch
84+
pub retiring_pools: Vec<PoolRetirement>,
85+
}
7886

7987
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
8088
pub struct PoolsActiveStakes {
@@ -94,7 +102,9 @@ pub struct PoolsTotalBlocksMinted {
94102
pub struct PoolInfo {}
95103

96104
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
97-
pub struct PoolHistory {}
105+
pub struct PoolHistory {
106+
pub history: Vec<PoolEpochState>,
107+
}
98108

99109
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
100110
pub struct PoolMetadata {}

common/src/state_history.rs

Lines changed: 54 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,20 @@
22
//! Keeps per-block state for rollbacks or per-epoch state for historical lookups
33
//! Use imbl collections in the state to avoid memory explosion!
44
5-
use crate::params::SECURITY_PARAMETER_K;
65
use std::collections::VecDeque;
7-
86
use tracing::info;
97

10-
pub enum HistoryKind {
11-
BlockState, // Used for rollbacks, bounded at k
12-
EpochState, // Used for historical lookups, unbounded
8+
use crate::params::SECURITY_PARAMETER_K;
9+
10+
pub enum StateHistoryStore {
11+
Bounded(u64), // Used for rollbacks, bounded at k
12+
Unbounded, // Used for historical lookups, unbounded
13+
}
14+
15+
impl StateHistoryStore {
16+
pub fn default_block_store() -> Self {
17+
Self::Bounded(SECURITY_PARAMETER_K)
18+
}
1319
}
1420

1521
struct HistoryEntry<S> {
@@ -25,17 +31,16 @@ pub struct StateHistory<S> {
2531
/// Module name
2632
module: String,
2733

28-
// Block or Epoch based history
29-
kind: HistoryKind,
34+
store: StateHistoryStore,
3035
}
3136

3237
impl<S: Clone + Default> StateHistory<S> {
3338
/// Construct
34-
pub fn new(module: &str, kind: HistoryKind) -> Self {
39+
pub fn new(module: &str, store: StateHistoryStore) -> Self {
3540
Self {
3641
history: VecDeque::new(),
3742
module: module.to_string(),
38-
kind: kind,
43+
store,
3944
}
4045
}
4146

@@ -50,38 +55,25 @@ impl<S: Clone + Default> StateHistory<S> {
5055
self.history.back().map(|entry| entry.state.clone()).unwrap_or_default()
5156
}
5257

58+
/// Get all the states references in the history
59+
pub fn values(&self) -> Vec<&S> {
60+
self.history.iter().map(|entry| &entry.state).collect()
61+
}
62+
5363
/// Get the previous state for the given block, handling rollbacks if required
5464
/// State returned is cloned ready for modification - call commit() when done
5565
pub fn get_rolled_back_state(&mut self, index: u64) -> S {
56-
match self.kind {
57-
HistoryKind::BlockState => {
58-
while let Some(entry) = self.history.back() {
59-
if entry.index >= index {
60-
info!(
61-
"{} rolling back state to {} removing block {}",
62-
self.module, index, entry.index
63-
);
64-
self.history.pop_back();
65-
} else {
66-
break;
67-
}
68-
}
69-
}
70-
HistoryKind::EpochState => {
71-
while let Some(entry) = self.history.back() {
72-
if entry.index >= index {
73-
info!(
74-
"{} rolling back epoch state to {} removing epoch {}",
75-
self.module, index, entry.index
76-
);
77-
self.history.pop_back();
78-
} else {
79-
break;
80-
}
81-
}
66+
while let Some(entry) = self.history.back() {
67+
if entry.index >= index {
68+
info!(
69+
"{} rolling back state to {} removing block {}",
70+
self.module, index, entry.index
71+
);
72+
self.history.pop_back();
73+
} else {
74+
break;
8275
}
8376
}
84-
8577
self.get_current_state()
8678
}
8779

@@ -90,20 +82,36 @@ impl<S: Clone + Default> StateHistory<S> {
9082
self.history.iter().find(|entry| entry.index == index).map(|entry| &entry.state)
9183
}
9284

85+
/// Return a reference to the state at the given block number, if it exists
86+
pub fn get_by_index_reverse(&self, index: u64) -> Option<&S> {
87+
self.history.iter().rev().find(|entry| entry.index == index).map(|entry| &entry.state)
88+
}
89+
90+
/// Get state history's size
9391
pub fn len(&self) -> usize {
9492
self.history.len()
9593
}
9694

95+
/// Commit new state without checking the block number
96+
/// TODO: enhance block number logic to commit state without check (for bootstrapping)
97+
pub fn commit_forced(&mut self, state: S) {
98+
self.history.push_back(HistoryEntry { index: 0, state });
99+
}
100+
97101
/// Commit the new state
98102
pub fn commit(&mut self, index: u64, state: S) {
99-
match self.kind {
100-
HistoryKind::BlockState => {
101-
while self.history.len() >= SECURITY_PARAMETER_K as usize {
102-
self.history.pop_front();
103+
match self.store {
104+
StateHistoryStore::Bounded(k) => {
105+
while let Some(entry) = self.history.front() {
106+
if (index - entry.index) > k {
107+
self.history.pop_front();
108+
} else {
109+
break;
110+
}
103111
}
104112
self.history.push_back(HistoryEntry { index, state });
105113
}
106-
HistoryKind::EpochState => {
114+
StateHistoryStore::Unbounded => {
107115
self.history.push_back(HistoryEntry { index, state });
108116
}
109117
}
@@ -122,4 +130,9 @@ impl<S: Clone> StateHistory<S> {
122130
init()
123131
}
124132
}
133+
134+
/// Clear the history
135+
pub fn clear(&mut self) {
136+
self.history.clear();
137+
}
125138
}

common/src/types.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -495,6 +495,18 @@ pub struct PoolRetirement {
495495
pub epoch: u64,
496496
}
497497

498+
/// Pool Epoch History Data
499+
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
500+
pub struct PoolEpochState {
501+
pub epoch: u64,
502+
pub blocks_minted: u64,
503+
pub active_stake: u64,
504+
pub active_size: RationalNumber,
505+
pub delegators_count: u64,
506+
pub pool_reward: u64,
507+
pub spo_reward: u64,
508+
}
509+
498510
/// Stake delegation data
499511
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
500512
pub struct StakeDelegation {
@@ -511,10 +523,23 @@ pub struct DelegatedStake {
511523
/// Active stake - UTXO values only (used for reward calcs)
512524
pub active: Lovelace,
513525

526+
/// Active delegators count - delegators making active stakes (used for pool history)
527+
pub active_delegators_count: u64,
528+
514529
/// Total 'live' stake - UTXO values and rewards (used for VRF)
515530
pub live: Lovelace,
516531
}
517532

533+
/// SPO rewards data (for SPORewardsMessage)
534+
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
535+
pub struct SPORewards {
536+
/// Total rewards before distribution
537+
pub total_rewards: Lovelace,
538+
539+
/// Pool operator's rewards
540+
pub operator_rewards: Lovelace,
541+
}
542+
518543
/// Genesis key delegation
519544
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
520545
pub struct GenesisKeyDelegation {

0 commit comments

Comments
 (0)