Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

Sassafras consensus - Prototype #1 #11678

Merged
merged 28 commits into from
Jul 21, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
82500c8
First draft of Sassafras components structure
davxy May 24, 2022
d7bef64
Implementation of Sassafras test node
davxy May 24, 2022
59e5b53
Start filling the placeholders
davxy May 24, 2022
e5a8239
Implemented epoch change using internal trigger
davxy May 31, 2022
7a456e2
Epoch randomness computation using blocks VRF accumulator
davxy Jun 1, 2022
11e009a
Verification on block import
davxy Jun 13, 2022
717a16f
Tickets generation procedure
davxy Jun 14, 2022
d86aef1
Publish tickets on-chain using unsigned extrinsic
davxy Jun 16, 2022
33c163d
Introduced first implementation of secondary slot claiming
davxy Jun 22, 2022
b82cccf
Use ticket to claim slot without proof check
davxy Jun 27, 2022
5e9c1f5
Enact epoch tickets on last epoch block
davxy Jun 28, 2022
9865938
Small refactory
davxy Jun 28, 2022
c3660f5
Unify primary and secondary pre-digest
davxy Jun 28, 2022
b50d9f9
Bump Sassafras components version to 0.1.0
davxy Jun 29, 2022
d78f6a2
Use BTreeSet instead of Vec to store next epoch tickets
davxy Jul 1, 2022
2508a5e
Trick to fetch the fist ticket on epoch change
davxy Jul 1, 2022
1e0e092
Fix epoch ticket get api
davxy Jul 2, 2022
8f4b18c
Blocks verification on import
davxy Jul 4, 2022
622b183
Merge branch 'master' into davxy-sassafras-consensus-prototype1
davxy Jul 19, 2022
cfd4c6a
Fix after master merge
davxy Jul 19, 2022
442d901
Restore good old Cargo.lock
davxy Jul 19, 2022
be27264
Fix unused fields warnings
davxy Jul 19, 2022
2a25d82
Try to make the compiler happy
davxy Jul 19, 2022
0e44362
Removed num-bigint dependency
davxy Jul 20, 2022
cd1ef1c
More robust tickets handling
davxy Jul 20, 2022
3d92632
TODOs priority classification
davxy Jul 20, 2022
3d8d2a8
Resolved TODOs with P1
davxy Jul 20, 2022
ccdfd37
Fix github link
davxy Jul 20, 2022
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Implemented epoch change using internal trigger
  • Loading branch information
davxy committed May 31, 2022
commit e5a8239e1a0e46af95f80ef7ffe5cf49de10f00f
2 changes: 2 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions bin/node-sassafras/node/src/chain_spec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,8 +143,7 @@ fn testnet_genesis(
},

sassafras: SassafrasConfig {
authorities: vec![], /* TODO-SASS: initial_authorities.iter().map(|x|
* (x.0.clone())).collect(), */
authorities: initial_authorities.iter().map(|x| (x.0.clone(), 0)).collect(),
epoch_config: Some(node_sassafras_runtime::SASSAFRAS_GENESIS_EPOCH_CONFIG),
},
grandpa: GrandpaConfig {
Expand Down
4 changes: 2 additions & 2 deletions bin/node-sassafras/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ parameter_types! {
impl pallet_sassafras::Config for Runtime {
type EpochDuration = EpochDuration;
type ExpectedBlockTime = ExpectedBlockTime;
type EpochChangeTrigger = pallet_sassafras::ExternalTrigger;
type EpochChangeTrigger = pallet_sassafras::SameAuthoritiesForever;
type MaxAuthorities = MaxAuthorities;
}

Expand All @@ -251,7 +251,7 @@ impl pallet_grandpa::Config for Runtime {
impl pallet_timestamp::Config for Runtime {
/// A timestamp: milliseconds since the unix epoch.
type Moment = u64;
type OnTimestampSet = (); // TODO-SASS
type OnTimestampSet = ();
type MinimumPeriod = ConstU64<{ SLOT_DURATION / 2 }>;
type WeightInfo = ();
}
Expand Down
2 changes: 1 addition & 1 deletion client/consensus/sassafras/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ log = "0.4.16"
#num-traits = "0.2.8"
parking_lot = "0.12.0"
#rand = "0.7.2"
#retain_mut = "0.1.4"
retain_mut = "0.1.4"
#schnorrkel = { version = "0.9.1", features = ["preaudit_deprecated"] }
#serde = { version = "1.0.136", features = ["derive"] }
thiserror = "1.0"
Expand Down
53 changes: 43 additions & 10 deletions client/consensus/sassafras/src/aux_schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,18 @@ use scale_codec::{Decode, Encode};
use sc_client_api::backend::AuxStore;
use sc_consensus_epochs::{EpochChangesFor, SharedEpochChanges};
use sp_blockchain::{Error as ClientError, Result as ClientResult};
use sp_consensus_sassafras::SassafrasBlockWeight;
use sp_runtime::traits::Block as BlockT;

use crate::Epoch;

const SASSAFRAS_EPOCH_CHANGES_KEY: &[u8] = b"sassafras_epoch_changes";

/// The aux storage key used to store the block weight of the given block hash.
fn block_weight_key<H: Encode>(block_hash: H) -> Vec<u8> {
(b"sassafras_block_weight", block_hash).encode()
}

fn load_decode<B, T>(backend: &B, key: &[u8]) -> ClientResult<Option<T>>
where
B: AuxStore,
Expand All @@ -43,27 +49,54 @@ where
}
}

/// Update the epoch changes on disk after a change.
pub(crate) fn write_epoch_changes<Block: BlockT, F, R>(
epoch_changes: &EpochChangesFor<Block, Epoch>,
write_aux: F,
) -> R
where
F: FnOnce(&[(&'static [u8], &[u8])]) -> R,
{
epoch_changes.using_encoded(|s| write_aux(&[(SASSAFRAS_EPOCH_CHANGES_KEY, s)]))
}

/// Load or initialize persistent epoch change data from backend.
pub fn load_epoch_changes<B: BlockT, AS: AuxStore>(
pub(crate) fn load_epoch_changes<B: BlockT, AS: AuxStore>(
backend: &AS,
) -> ClientResult<SharedEpochChanges<B, Epoch>> {
let maybe_epoch_changes =
load_decode::<_, EpochChangesFor<B, Epoch>>(backend, SASSAFRAS_EPOCH_CHANGES_KEY)?;

let epoch_changes =
SharedEpochChanges::<B, Epoch>::new(maybe_epoch_changes.unwrap_or_else(|| {
info!(
target: "sassafras",
"🍁 Creating empty Sassafras epoch changes on what appears to be first startup.",
);
EpochChangesFor::<B, Epoch>::default()
}));
let epoch_changes = SharedEpochChanges::<B, Epoch>::new(
maybe_epoch_changes.unwrap_or_else(|| EpochChangesFor::<B, Epoch>::default()),
);

// rebalance the tree after deserialization. this isn't strictly necessary
// Rebalance the tree after deserialization. this isn't strictly necessary
// since the tree is now rebalanced on every update operation. but since the
// tree wasn't rebalanced initially it's useful to temporarily leave it here
// to avoid having to wait until an import for rebalancing.
epoch_changes.shared_data().rebalance();

Ok(epoch_changes)
}

/// Write the cumulative chain-weight of a block ot aux storage.
pub(crate) fn write_block_weight<H: Encode, F, R>(
block_hash: H,
block_weight: SassafrasBlockWeight,
write_aux: F,
) -> R
where
F: FnOnce(&[(Vec<u8>, &[u8])]) -> R,
{
let key = block_weight_key(block_hash);
block_weight.using_encoded(|s| write_aux(&[(key, s)]))
}

/// Load the cumulative chain-weight associated with a block.
pub(crate) fn load_block_weight<H: Encode, B: AuxStore>(
backend: &B,
block_hash: H,
) -> ClientResult<Option<SassafrasBlockWeight>> {
load_decode(backend, block_weight_key(block_hash).as_slice())
}
Loading