forked from MystenLabs/sui
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Consensus] add additional block types and store skeleton (MystenLabs…
…#15629) ## Description Add additional block types for I/O and data manipulations. Add skeleton for DagState and Store. I will add more details to them before the PR is merged. ## Test Plan CI --- If your changes are not user-facing and do not break anything, you can skip the following section. Otherwise, please briefly describe what has changed under the Release Notes section. ### Type of Change (Check all that apply) - [ ] protocol change - [ ] user-visible impact - [ ] breaking change for a client SDKs - [ ] breaking change for FNs (FN binary must upgrade) - [ ] breaking change for validators or node operators (must upgrade binaries) - [ ] breaking change for on-chain data layout - [ ] necessitate either a data wipe or data migration ### Release notes
- Loading branch information
Showing
12 changed files
with
221 additions
and
16 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
// Copyright (c) Mysten Labs, Inc. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
use serde::{Deserialize, Serialize}; | ||
|
||
use crate::block::BlockRef; | ||
|
||
/// Specifies one consensus commit. | ||
/// It is stored on disk, so it does not contain blocks which are stored individually. | ||
#[allow(unused)] | ||
#[derive(Deserialize, Serialize)] | ||
pub(crate) struct Commit { | ||
/// Index of the commit. | ||
/// First commit after genesis has an index of 1, then every next commit has an index incremented by 1. | ||
pub index: u64, | ||
/// A reference to the the commit leader. | ||
pub leader: BlockRef, | ||
/// Refs to committed blocks, in the commit order. | ||
pub blocks: Vec<BlockRef>, | ||
} |
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,69 @@ | ||
// Copyright (c) Mysten Labs, Inc. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
use std::{ | ||
collections::{BTreeMap, BTreeSet}, | ||
sync::Arc, | ||
}; | ||
|
||
use crate::{ | ||
block::{BlockAPI as _, BlockRef, Round, VerifiedBlock}, | ||
context::Context, | ||
storage::Store, | ||
}; | ||
|
||
/// Recent rounds of blocks to cached in memory, counted from the last committed leader round. | ||
#[allow(unused)] | ||
const BLOCK_CACHED_ROUNDS: Round = 100; | ||
|
||
/// DagState provides the API to write and read accepted blocks from the DAG. | ||
/// Only uncommited and last committed blocks are cached in memory. | ||
/// The rest of blocks are stored on disk. | ||
/// Refs to cached blocks and additional refs are cached as well, to speed up existence checks. | ||
/// | ||
/// Note: DagState should be wrapped with Arc<RwMutex<_>>, to allow concurrent access from | ||
/// multiple components. | ||
#[allow(unused)] | ||
pub(crate) struct DagState { | ||
context: Arc<Context>, | ||
|
||
// Caches uncommitted blocks, and recent blocks within BLOCK_CACHED_ROUNDS from the | ||
// last committed leader round. | ||
recent_blocks: BTreeMap<BlockRef, VerifiedBlock>, | ||
|
||
// All accepted blocks have their refs cached. Cached refs are never removed for now. | ||
// Each element in the vector contains refs for the authority corresponding to its index. | ||
cached_refs: Vec<BTreeSet<BlockRef>>, | ||
|
||
// Persistent storage for blocks, commits and other consensus data. | ||
store: Arc<dyn Store>, | ||
} | ||
|
||
#[allow(unused)] | ||
impl DagState { | ||
pub(crate) fn new( | ||
context: Arc<Context>, | ||
blocks: Vec<VerifiedBlock>, | ||
store: Arc<dyn Store>, | ||
) -> Self { | ||
let num_authorities = context.committee.size(); | ||
let mut state = Self { | ||
context, | ||
recent_blocks: BTreeMap::new(), | ||
cached_refs: vec![BTreeSet::new(); num_authorities], | ||
store, | ||
}; | ||
|
||
for block in blocks { | ||
state.add_block(block); | ||
} | ||
|
||
state | ||
} | ||
|
||
pub(crate) fn add_block(&mut self, block: VerifiedBlock) { | ||
let block_ref = block.block.reference(); | ||
self.recent_blocks.insert(block_ref, block); | ||
self.cached_refs[block_ref.author].insert(block_ref); | ||
} | ||
} |
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,15 @@ | ||
// Copyright (c) Mysten Labs, Inc. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
use thiserror::Error; | ||
|
||
/// Errors that can occur when processing blocks, reading from storage, or encountering shutdown. | ||
#[allow(unused)] | ||
#[derive(Clone, Debug, Error)] | ||
pub enum ConsensusError { | ||
#[error("Error deserializing block")] | ||
MalformattedBlock, | ||
} | ||
|
||
#[allow(unused)] | ||
pub type ConsensusResult<T> = Result<T, ConsensusError>; |
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,17 @@ | ||
// Copyright (c) Mysten Labs, Inc. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
mod rocksdb; | ||
|
||
use crate::{block::VerifiedBlock, commit::Commit, error::ConsensusResult}; | ||
|
||
/// A common interface for consensus storage. | ||
pub(crate) trait Store { | ||
/// Loads last committed blocks, all uncommitted blocks and last commit from store. | ||
fn recover(&self) -> ConsensusResult<(Vec<VerifiedBlock>, Commit)>; | ||
|
||
/// Writes blocks and consensus commits to store. | ||
fn write(&self, blocks: Vec<VerifiedBlock>, commits: Vec<Commit>) -> ConsensusResult<()>; | ||
|
||
// TODO: add methods to read and scan blocks. | ||
} |
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 @@ | ||
// Copyright (c) Mysten Labs, Inc. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
use crate::{block::VerifiedBlock, commit::Commit, error::ConsensusResult}; | ||
|
||
use super::Store; | ||
|
||
/// Storage implementation using RocksDB. | ||
pub(crate) struct RocksDB {} | ||
|
||
#[allow(unused)] | ||
impl Store for RocksDB { | ||
fn recover(&self) -> ConsensusResult<(Vec<VerifiedBlock>, Commit)> { | ||
unimplemented!() | ||
} | ||
|
||
fn write(&self, blocks: Vec<VerifiedBlock>, commits: Vec<Commit>) -> ConsensusResult<()> { | ||
unimplemented!() | ||
} | ||
} |