-
Notifications
You must be signed in to change notification settings - Fork 115
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
verify epoch in dispatcher using consensus verifier
- Loading branch information
Showing
10 changed files
with
154 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
runtime: verify epoch in dispatcher using consensus verifier |
20 changes: 20 additions & 0 deletions
20
go/consensus/tendermint/apps/beacon/state/interop/interop.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package interop | ||
|
||
import ( | ||
"context" | ||
|
||
beaconState "github.com/oasisprotocol/oasis-core/go/consensus/tendermint/apps/beacon/state" | ||
"github.com/oasisprotocol/oasis-core/go/storage/mkvs" | ||
) | ||
|
||
// Keep this in sync with tests in runtimes/consensus/state/beacon.rs. | ||
func InitializeTestBeaconState(ctx context.Context, mkvs mkvs.Tree) error { | ||
state := beaconState.NewMutableState(mkvs) | ||
|
||
// Populate beacon. | ||
if err := state.SetEpoch(ctx, 42, 13); err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
//! Beacon state in the consensus layer. | ||
use anyhow::anyhow; | ||
use io_context::Context; | ||
|
||
use crate::{ | ||
common::key_format::{KeyFormat, KeyFormatAtom}, | ||
consensus::{beacon::EpochTime, 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, | ||
} | ||
|
||
impl<'a, T: ImmutableMKVS> ImmutableState<'a, T> { | ||
/// Constructs a new ImmutableMKVS. | ||
pub fn new(mkvs: &'a T) -> ImmutableState<'a, T> { | ||
ImmutableState { mkvs } | ||
} | ||
} | ||
|
||
key_format!(CurrentEpochKeyFmt, 0x40, ()); | ||
|
||
impl<'a, T: ImmutableMKVS> ImmutableState<'a, T> { | ||
/// Returns the current epoch number. | ||
pub fn epoch(&self, ctx: Context) -> Result<EpochTime, 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(None) => Ok(EpochTime::default()), | ||
Err(err) => Err(StateError::Unavailable(anyhow!(err))), | ||
} | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod test { | ||
use std::sync::Arc; | ||
|
||
use crate::{ | ||
common::crypto::hash::Hash, | ||
storage::mkvs::{ | ||
interop::{Fixture, ProtocolServer}, | ||
Root, RootType, Tree, | ||
}, | ||
}; | ||
|
||
use super::*; | ||
|
||
#[test] | ||
fn test_beacon_state_interop() { | ||
// Keep in sync with go/consensus/tendermint/apps/beacon/state/interop/interop.go. | ||
// If mock consensus state changes, update the root hash bellow. | ||
// See protocol server stdout for hash. | ||
|
||
// Setup protocol server with initialized mock consensus state. | ||
let server = ProtocolServer::new(Fixture::ConsensusMock.into()); | ||
let mock_consensus_root = Root { | ||
version: 1, | ||
root_type: RootType::State, | ||
hash: Hash::from("aadd72716ec331c4bc7b8945699cbc608038dd4f3fc2cc3f01fffdad24a47c6f"), | ||
..Default::default() | ||
}; | ||
let mkvs = Tree::builder() | ||
.with_capacity(100_000, 10_000_000) | ||
.with_root(mock_consensus_root) | ||
.build(server.read_sync()); | ||
let beacon_state = ImmutableState::new(&mkvs); | ||
|
||
let ctx = Arc::new(Context::background()); | ||
|
||
// Test current epoch number. | ||
let epoch = beacon_state | ||
.epoch(Context::create_child(&ctx)) | ||
.expect("eapoch query should work"); | ||
assert_eq!(42u64, epoch, "expected epoch should match"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,6 +11,7 @@ use crate::{ | |
types::HostStorageEndpoint, | ||
}; | ||
|
||
pub mod beacon; | ||
pub mod roothash; | ||
pub mod staking; | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters