Skip to content

Commit

Permalink
Merge pull request #4897 from oasisprotocol/kostko/feature/rt-beacon-…
Browse files Browse the repository at this point in the history
…epochstate

runtime: Add epoch_state query and EPOCH_INVALID constant
  • Loading branch information
kostko authored Aug 22, 2022
2 parents e53017b + 2806f47 commit a5f97ea
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 9 deletions.
1 change: 1 addition & 0 deletions .changelog/4897.internal.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
runtime: Add epoch_state query and EPOCH_INVALID constant
10 changes: 10 additions & 0 deletions runtime/src/consensus/beacon.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,12 @@
/// The number of intervals (epochs) since a fixed instant in time/block height (epoch date/height).
pub type EpochTime = u64;

/// An invalid epoch time.
pub const EPOCH_INVALID: EpochTime = 0xffffffffffffffff;

/// The epoch state.
#[derive(Clone, Debug, Default, PartialEq, Eq, Hash, cbor::Encode, cbor::Decode)]
pub struct EpochTimeState {
pub epoch: EpochTime,
pub height: i64,
}
27 changes: 18 additions & 9 deletions runtime/src/consensus/state/beacon.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,14 @@ use io_context::Context;

use crate::{
common::key_format::{KeyFormat, KeyFormatAtom},
consensus::{beacon::EpochTime, state::StateError},
consensus::{
beacon::{EpochTime, EpochTimeState},
state::StateError,
},
key_format,
storage::mkvs::ImmutableMKVS,
};

#[derive(Clone, Debug, Default, PartialEq, Eq, Hash, cbor::Encode, cbor::Decode)]
pub struct EpochTimeState {
pub epoch: EpochTime,
pub height: i64,
}

/// Consensus beacon state wrapper.
pub struct ImmutableState<'a, T: ImmutableMKVS> {
mkvs: &'a T,
Expand All @@ -33,13 +30,18 @@ key_format!(FutureEpochKeyFmt, 0x41, ());
impl<'a, T: ImmutableMKVS> ImmutableState<'a, T> {
/// Returns the current epoch number.
pub fn epoch(&self, ctx: Context) -> Result<EpochTime, StateError> {
self.epoch_state(ctx).map(|es| es.epoch)
}

/// Returns the current epoch state.
pub fn epoch_state(&self, ctx: Context) -> Result<EpochTimeState, StateError> {
match self.mkvs.get(ctx, &CurrentEpochKeyFmt(()).encode()) {
Ok(Some(b)) => {
let state: EpochTimeState =
cbor::from_slice(&b).map_err(|err| StateError::Unavailable(anyhow!(err)))?;
Ok(state.epoch)
Ok(state)
}
Ok(None) => Ok(EpochTime::default()),
Ok(None) => Ok(EpochTimeState::default()),
Err(err) => Err(StateError::Unavailable(anyhow!(err))),
}
}
Expand Down Expand Up @@ -100,6 +102,13 @@ mod test {
.expect("epoch query should work");
assert_eq!(42u64, epoch, "expected epoch should match");

// Test current epoch state.
let epoch_state = beacon_state
.epoch_state(Context::create_child(&ctx))
.expect("epoch state query should work");
assert_eq!(42u64, epoch_state.epoch, "expected epoch should match");
assert_eq!(13i64, epoch_state.height, "expected height should match");

// Test future epoch number.
let epoch = beacon_state
.future_epoch(Context::create_child(&ctx))
Expand Down

0 comments on commit a5f97ea

Please sign in to comment.