Skip to content

Commit

Permalink
staking: move uptime tracking to NV storage (#4046)
Browse files Browse the repository at this point in the history
Close #4030, I think we could also avoid deserializing into a domain
type altogether, maybe other optimizations are possible. This takes care
of the state-breaking part of the change.
  • Loading branch information
erwanor authored Mar 20, 2024
1 parent 7f02156 commit 70a3ed2
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use async_trait::async_trait;
use cnidarium::{StateRead, StateWrite};
use futures::{Future, FutureExt, TryStreamExt};
use penumbra_num::Amount;
use penumbra_proto::{state::future::DomainFuture, StateReadProto, StateWriteProto};
use penumbra_proto::{state::future::DomainFuture, DomainType, StateReadProto, StateWriteProto};
use std::pin::Pin;
use tendermint::PublicKey;
use tracing::instrument;
Expand Down Expand Up @@ -113,7 +113,8 @@ pub trait ValidatorDataRead: StateRead {
&self,
identity_key: &IdentityKey,
) -> DomainFuture<Uptime, Self::GetRawFut> {
self.get(&state_key::validators::uptime::by_id(identity_key))
let key = state_key::validators::uptime::by_id(identity_key);
self.nonverifiable_get(key.as_bytes())
}

// Tendermint validators are referenced to us by their Tendermint consensus key,
Expand Down Expand Up @@ -204,7 +205,12 @@ impl<T: StateRead + ?Sized> ValidatorDataRead for T {}
#[async_trait]
pub(crate) trait ValidatorDataWrite: StateWrite {
fn set_validator_uptime(&mut self, identity_key: &IdentityKey, uptime: Uptime) {
self.put(state_key::validators::uptime::by_id(identity_key), uptime);
self.nonverifiable_put_raw(
state_key::validators::uptime::by_id(identity_key)
.as_bytes()
.to_vec(),
uptime.encode_to_vec(),
);
}

fn set_validator_bonding_state(
Expand Down
18 changes: 18 additions & 0 deletions crates/proto/src/state/read.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,24 @@ pub trait StateReadProto: StateRead + Send + Sync {
}
}

/// Gets a value from the nonverifiable key-value store as a domain type.
///
/// # Returns
///
/// * `Ok(Some(v))` if the value is present and parseable as a domain type `D`;
/// * `Ok(None)` if the value is missing;
/// * `Err(_)` if the value is present but not parseable as a domain type `D`, or if an underlying storage error occurred.
fn nonverifiable_get<D>(&self, key: &[u8]) -> DomainFuture<D, Self::GetRawFut>
where
D: DomainType + std::fmt::Debug,
anyhow::Error: From<<D as TryFrom<D::Proto>>::Error>,
{
DomainFuture {
inner: self.nonverifiable_get_raw(key),
_marker: std::marker::PhantomData,
}
}

/// Gets a value from the verifiable key-value store as a proto type.
///
/// # Returns
Expand Down

0 comments on commit 70a3ed2

Please sign in to comment.