Skip to content

Commit

Permalink
Multiple sequencers registration and exit (Sovereign-Labs#410)
Browse files Browse the repository at this point in the history
  • Loading branch information
citizen-stig authored Jun 27, 2023
1 parent 524dd2a commit 3cda7d1
Show file tree
Hide file tree
Showing 20 changed files with 794 additions and 335 deletions.
16 changes: 15 additions & 1 deletion examples/demo-stf/src/genesis_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ pub fn create_demo_genesis_config<C: Context>(
};

GenesisConfig::new(
sequencer_config,
bank_config,
sequencer_config,
election_config,
value_setter_config,
sov_accounts::AccountConfig { pub_keys: vec![] },
Expand All @@ -67,3 +67,17 @@ pub fn generate_address<C: Context>(key: &str) -> <C as Spec>::Address {
let hash = <C as Spec>::Hasher::hash(key.as_bytes());
<C as Spec>::Address::from(hash)
}

pub fn create_demo_config(
initial_sequencer_balance: u64,
value_setter_admin_private_key: &DefaultPrivateKey,
election_admin_private_key: &DefaultPrivateKey,
) -> GenesisConfig<DefaultContext> {
create_demo_genesis_config::<DefaultContext>(
initial_sequencer_balance,
generate_address::<DefaultContext>(DEMO_SEQ_PUB_KEY_STR),
DEMO_SEQUENCER_DA_ADDRESS.to_vec(),
value_setter_admin_private_key,
election_admin_private_key,
)
}
26 changes: 22 additions & 4 deletions examples/demo-stf/src/hooks_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use sov_modules_api::{
use sov_modules_stf_template::SequencerOutcome;
use sov_rollup_interface::da::BlobTransactionTrait;
use sov_state::WorkingSet;
use tracing::info;

impl<C: Context> TxHooks for Runtime<C> {
type Context = C;
Expand Down Expand Up @@ -37,7 +38,7 @@ impl<C: Context> ApplyBlobHooks for Runtime<C> {
blob: &mut impl BlobTransactionTrait,
working_set: &mut WorkingSet<<Self::Context as Spec>::Storage>,
) -> anyhow::Result<()> {
self.sequencer.begin_blob_hook(blob, working_set)
self.sequencer_registry.begin_blob_hook(blob, working_set)
}

fn end_blob_hook(
Expand All @@ -46,9 +47,26 @@ impl<C: Context> ApplyBlobHooks for Runtime<C> {
working_set: &mut WorkingSet<<Self::Context as Spec>::Storage>,
) -> anyhow::Result<()> {
match result {
SequencerOutcome::Rewarded(reward) => self.sequencer.end_blob_hook(reward, working_set),
SequencerOutcome::Ignored => self.sequencer.end_blob_hook(0, working_set),
SequencerOutcome::Slashed(_) => Ok(()),
SequencerOutcome::Rewarded(_reward) => {
// TODO: Process reward here or above.
self.sequencer_registry.end_blob_hook(
sov_sequencer_registry::SequencerOutcome::Completed,
working_set,
)
}
SequencerOutcome::Ignored => Ok(()),
SequencerOutcome::Slashed {
reason,
sequencer_da_address,
} => {
info!("Sequencer slashed: {:?}", reason);
self.sequencer_registry.end_blob_hook(
sov_sequencer_registry::SequencerOutcome::Slashed {
sequencer: sequencer_da_address,
},
working_set,
)
}
}
}
}
4 changes: 2 additions & 2 deletions examples/demo-stf/src/runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use sov_bank::query::{BankRpcImpl, BankRpcServer};
#[cfg(feature = "native")]
use sov_election::query::{ElectionRpcImpl, ElectionRpcServer};
#[cfg(feature = "native")]
use sov_sequencer_registry::query::{SequencerRpcImpl, SequencerRpcServer};
use sov_sequencer_registry::query::{SequencerRegistryRpcImpl, SequencerRegistryRpcServer};
#[cfg(feature = "native")]
use sov_value_setter::query::{ValueSetterRpcImpl, ValueSetterRpcServer};

Expand Down Expand Up @@ -59,8 +59,8 @@ use sov_value_setter::query::{ValueSetterRpcImpl, ValueSetterRpcServer};
#[derive(Genesis, DispatchCall, MessageCodec, DefaultRuntime)]
#[serialization(borsh::BorshDeserialize, borsh::BorshSerialize)]
pub struct Runtime<C: Context> {
pub sequencer: sov_sequencer_registry::Sequencer<C>,
pub bank: sov_bank::Bank<C>,
pub sequencer_registry: sov_sequencer_registry::SequencerRegistry<C>,
pub election: sov_election::Election<C>,
pub value_setter: sov_value_setter::ValueSetter<C>,
pub accounts: sov_accounts::Accounts<C>,
Expand Down
35 changes: 9 additions & 26 deletions examples/demo-stf/src/sov-cli/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -258,10 +258,7 @@ pub fn main() {
mod test {
use super::*;
use demo_stf::app::{DemoApp, DemoAppRunner};
use demo_stf::genesis_config::{
create_demo_genesis_config, generate_address, DEMO_SEQUENCER_DA_ADDRESS,
DEMO_SEQ_PUB_KEY_STR, LOCKED_AMOUNT,
};
use demo_stf::genesis_config::{create_demo_config, DEMO_SEQUENCER_DA_ADDRESS, LOCKED_AMOUNT};
use demo_stf::runner_config::Config;
use demo_stf::runtime::GenesisConfig;
use sov_modules_api::Address;
Expand All @@ -271,6 +268,14 @@ mod test {
use sov_rollup_interface::{mocks::MockZkvm, stf::StateTransitionFunction};
use sov_state::WorkingSet;

type TestBlob = sov_rollup_interface::mocks::TestBlob<Address>;

fn new_test_blob(batch: Batch, address: &[u8]) -> TestBlob {
let address = Address::try_from(address).unwrap();
let data = batch.try_to_vec().unwrap();
TestBlob::new(data, address, [0; 32])
}

#[test]
fn test_sov_cli() {
// Tempdir is created here, so it will be deleted only after test is finished.
Expand Down Expand Up @@ -411,26 +416,4 @@ mod test {
fn create_token_address(token_deployer_address: &Address) -> Address {
sov_bank::create_token_address::<C>("sov-test-token", token_deployer_address.as_ref(), 11)
}

pub type TestBlob = sov_rollup_interface::mocks::TestBlob<Address>;

pub fn new_test_blob(batch: Batch, address: &[u8]) -> TestBlob {
let address = Address::try_from(address).unwrap();
let data = batch.try_to_vec().unwrap();
TestBlob::new(data, address, [0; 32])
}

pub fn create_demo_config(
initial_sequencer_balance: u64,
value_setter_admin_private_key: &DefaultPrivateKey,
election_admin_private_key: &DefaultPrivateKey,
) -> GenesisConfig<DefaultContext> {
create_demo_genesis_config::<DefaultContext>(
initial_sequencer_balance,
generate_address::<DefaultContext>(DEMO_SEQ_PUB_KEY_STR),
DEMO_SEQUENCER_DA_ADDRESS.to_vec(),
value_setter_admin_private_key,
election_admin_private_key,
)
}
}
40 changes: 8 additions & 32 deletions examples/demo-stf/src/tests/mod.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,11 @@
use borsh::BorshSerialize;
use sov_modules_api::{
default_context::DefaultContext, default_signature::private_key::DefaultPrivateKey, Address,
};
use sov_modules_api::{default_context::DefaultContext, Address};
use sov_modules_stf_template::{AppTemplate, Batch, SequencerOutcome, TxEffect};
use sov_rollup_interface::stf::BatchReceipt;
use sov_state::ProverStorage;
use std::path::Path;

use crate::{
app::DemoApp,
genesis_config::{
create_demo_genesis_config, generate_address, DEMO_SEQUENCER_DA_ADDRESS,
DEMO_SEQ_PUB_KEY_STR,
},
runtime::{GenesisConfig, Runtime},
};
use crate::{app::DemoApp, runtime::Runtime};

mod data_generation;
mod stf_tests;
Expand All @@ -23,13 +14,6 @@ pub(crate) type C = DefaultContext;

pub type TestBlob = sov_rollup_interface::mocks::TestBlob<Address>;

pub fn new_test_blob(batch: Batch, address: &[u8]) -> TestBlob {
let address = Address::try_from(address).unwrap();
let data = batch.try_to_vec().unwrap();

TestBlob::new(data, address, [0; 32])
}

pub fn create_new_demo(
path: impl AsRef<Path>,
) -> DemoApp<DefaultContext, sov_rollup_interface::mocks::MockZkvm> {
Expand All @@ -38,20 +22,6 @@ pub fn create_new_demo(
AppTemplate::new(storage, runtime)
}

pub fn create_demo_config(
initial_sequencer_balance: u64,
value_setter_admin_private_key: &DefaultPrivateKey,
election_admin_private_key: &DefaultPrivateKey,
) -> GenesisConfig<DefaultContext> {
create_demo_genesis_config::<DefaultContext>(
initial_sequencer_balance,
generate_address::<DefaultContext>(DEMO_SEQ_PUB_KEY_STR),
DEMO_SEQUENCER_DA_ADDRESS.to_vec(),
value_setter_admin_private_key,
election_admin_private_key,
)
}

pub fn has_tx_events(apply_blob_outcome: &BatchReceipt<SequencerOutcome, TxEffect>) -> bool {
let events = apply_blob_outcome
.tx_receipts
Expand All @@ -60,3 +30,9 @@ pub fn has_tx_events(apply_blob_outcome: &BatchReceipt<SequencerOutcome, TxEffec

events.peekable().peek().is_some()
}

pub fn new_test_blob(batch: Batch, address: &[u8]) -> TestBlob {
let address = Address::try_from(address).unwrap();
let data = batch.try_to_vec().unwrap();
TestBlob::new(data, address, [0; 32])
}
16 changes: 8 additions & 8 deletions examples/demo-stf/src/tests/stf_tests.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
#[cfg(test)]
pub mod test {
use crate::genesis_config::create_demo_config;
use crate::tests::new_test_blob;
use crate::{
genesis_config::{DEMO_SEQUENCER_DA_ADDRESS, LOCKED_AMOUNT},
runtime::Runtime,
tests::{
create_demo_config, create_new_demo, data_generation::simulate_da, has_tx_events,
new_test_blob, C,
},
tests::{create_new_demo, data_generation::simulate_da, has_tx_events, C},
};
use sov_modules_api::{
default_context::DefaultContext, default_signature::private_key::DefaultPrivateKey,
Expand Down Expand Up @@ -178,15 +177,15 @@ pub mod test {
}

#[test]
fn test_sequencer_insufficient_funds() {
fn test_sequencer_unknown_sequencer() {
let tempdir = tempfile::tempdir().unwrap();
let path = tempdir.path();

let value_setter_admin_private_key = DefaultPrivateKey::generate();
let election_admin_private_key = DefaultPrivateKey::generate();

let config = create_demo_config(
LOCKED_AMOUNT - 1,
LOCKED_AMOUNT + 1,
&value_setter_admin_private_key,
&election_admin_private_key,
);
Expand All @@ -198,15 +197,16 @@ pub mod test {

let txs = simulate_da(value_setter_admin_private_key, election_admin_private_key);

let some_sequencer: [u8; 32] = [101; 32];
let apply_blob_outcome = StateTransitionFunction::<MockZkvm>::apply_blob(
&mut demo,
&mut new_test_blob(Batch { txs }, &DEMO_SEQUENCER_DA_ADDRESS),
&mut new_test_blob(Batch { txs }, &some_sequencer),
None,
);

assert!(
matches!(apply_blob_outcome.inner, SequencerOutcome::Ignored),
"Batch should have been skipped due to insufficient funds"
"Batch should have been skipped due to unknown sequencer"
);

// Assert that there are no events
Expand Down
Loading

0 comments on commit 3cda7d1

Please sign in to comment.