Skip to content

Commit

Permalink
Simplify generics
Browse files Browse the repository at this point in the history
  • Loading branch information
citizen-stig committed Aug 25, 2023
1 parent b1df21b commit ebef2a9
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 30 deletions.
8 changes: 5 additions & 3 deletions examples/demo-stf/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,10 @@ use crate::runtime::Runtime;

#[cfg(feature = "native")]
pub struct App<Vm: Zkvm, DA: DaSpec> {
pub stf: AppTemplate<DefaultContext, DA, Vm, Runtime<DefaultContext, DA>>,
pub batch_builder: Option<FiFoStrictBatchBuilder<Runtime<DefaultContext, DA>, DefaultContext>>,
pub stf: AppTemplate<DefaultContext, DA, Vm, Runtime<DefaultContext, DA::ValidityCondition>>,
pub batch_builder: Option<
FiFoStrictBatchBuilder<Runtime<DefaultContext, DA::ValidityCondition>, DefaultContext>,
>,
}

#[cfg(feature = "native")]
Expand Down Expand Up @@ -51,7 +53,7 @@ impl<Vm: Zkvm, DA: DaSpec> App<Vm, DA> {

pub fn create_zk_app_template<Vm: Zkvm, DA: DaSpec>(
runtime_config: [u8; 32],
) -> AppTemplate<ZkDefaultContext, DA, Vm, Runtime<ZkDefaultContext, DA>> {
) -> AppTemplate<ZkDefaultContext, DA, Vm, Runtime<ZkDefaultContext, DA::ValidityCondition>> {
let storage = ZkStorage::with_config(runtime_config).expect("Failed to open zk storage");
AppTemplate::new(storage, Runtime::default())
}
12 changes: 6 additions & 6 deletions examples/demo-stf/src/genesis_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ pub use sov_modules_api::default_context::DefaultContext;
use sov_modules_api::default_signature::private_key::DefaultPrivateKey;
use sov_modules_api::utils::generate_address;
use sov_modules_api::{Context, PrivateKey, PublicKey};
use sov_rollup_interface::da::DaSpec;
use sov_rollup_interface::zk::ValidityCondition;
pub use sov_state::config::Config as StorageConfig;
use sov_value_setter::ValueSetterConfig;

Expand All @@ -18,13 +18,13 @@ pub const LOCKED_AMOUNT: u64 = 50;
pub const DEMO_SEQ_PUB_KEY_STR: &str = "seq_pub_key";
pub const DEMO_TOKEN_NAME: &str = "sov-demo-token";

pub fn create_demo_genesis_config<C: Context, DA: DaSpec>(
pub fn create_demo_genesis_config<C: Context, Cond: ValidityCondition>(
initial_sequencer_balance: u64,
sequencer_address: C::Address,
sequencer_da_address: Vec<u8>,
value_setter_admin_private_key: &DefaultPrivateKey,
election_admin_private_key: &DefaultPrivateKey,
) -> GenesisConfig<C, DA> {
) -> GenesisConfig<C, Cond> {
let token_config: sov_bank::TokenConfig<C> = sov_bank::TokenConfig {
token_name: DEMO_TOKEN_NAME.to_owned(),
address_and_balances: vec![(sequencer_address.clone(), initial_sequencer_balance)],
Expand Down Expand Up @@ -94,12 +94,12 @@ pub fn create_demo_genesis_config<C: Context, DA: DaSpec>(
)
}

pub fn create_demo_config<DA: DaSpec>(
pub fn create_demo_config<Cond: ValidityCondition>(
initial_sequencer_balance: u64,
value_setter_admin_private_key: &DefaultPrivateKey,
election_admin_private_key: &DefaultPrivateKey,
) -> GenesisConfig<DefaultContext, DA> {
create_demo_genesis_config::<DefaultContext, DA>(
) -> GenesisConfig<DefaultContext, Cond> {
create_demo_genesis_config::<DefaultContext, Cond>(
initial_sequencer_balance,
generate_address::<DefaultContext>(DEMO_SEQ_PUB_KEY_STR),
DEMO_SEQUENCER_DA_ADDRESS.to_vec(),
Expand Down
19 changes: 10 additions & 9 deletions examples/demo-stf/src/hooks_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@ use sov_modules_api::hooks::{ApplyBlobHooks, TxHooks};
use sov_modules_api::transaction::Transaction;
use sov_modules_api::{Context, Spec};
use sov_modules_stf_template::SequencerOutcome;
use sov_rollup_interface::da::{BlobReaderTrait, DaSpec};
use sov_rollup_interface::da::BlobReaderTrait;
use sov_rollup_interface::zk::ValidityCondition;
use sov_sequencer_registry::SequencerRegistry;
use sov_state::WorkingSet;
use tracing::info;

use crate::runtime::Runtime;

impl<C: Context, DA: DaSpec> TxHooks for Runtime<C, DA> {
impl<C: Context, Cond: ValidityCondition> TxHooks for Runtime<C, Cond> {
type Context = C;

fn pre_dispatch_tx_hook(
Expand All @@ -29,15 +30,15 @@ impl<C: Context, DA: DaSpec> TxHooks for Runtime<C, DA> {
}
}

impl<C: Context, DA: DaSpec> ApplyBlobHooks<DA::BlobTransaction> for Runtime<C, DA> {
impl<C: Context, Cond: ValidityCondition, B: BlobReaderTrait> ApplyBlobHooks<B>
for Runtime<C, Cond>
{
type Context = C;
type BlobResult = SequencerOutcome<
<<DA as sov_rollup_interface::da::DaSpec>::BlobTransaction as BlobReaderTrait>::Address,
>;
type BlobResult = SequencerOutcome<B::Address>;

fn begin_blob_hook(
&self,
blob: &mut DA::BlobTransaction,
blob: &mut B,
working_set: &mut WorkingSet<<Self::Context as Spec>::Storage>,
) -> anyhow::Result<()> {
self.sequencer_registry.begin_blob_hook(blob, working_set)
Expand All @@ -51,7 +52,7 @@ impl<C: Context, DA: DaSpec> ApplyBlobHooks<DA::BlobTransaction> for Runtime<C,
match result {
SequencerOutcome::Rewarded(_reward) => {
// TODO: Process reward here or above.
<SequencerRegistry<C> as ApplyBlobHooks<DA::BlobTransaction>>::end_blob_hook(
<SequencerRegistry<C> as ApplyBlobHooks<B>>::end_blob_hook(
&self.sequencer_registry,
sov_sequencer_registry::SequencerOutcome::Completed,
working_set,
Expand All @@ -63,7 +64,7 @@ impl<C: Context, DA: DaSpec> ApplyBlobHooks<DA::BlobTransaction> for Runtime<C,
sequencer_da_address,
} => {
info!("Sequencer {} slashed: {:?}", sequencer_da_address, reason);
<SequencerRegistry<C> as ApplyBlobHooks<DA::BlobTransaction>>::end_blob_hook(
<SequencerRegistry<C> as ApplyBlobHooks<B>>::end_blob_hook(
&self.sequencer_registry,
sov_sequencer_registry::SequencerOutcome::Slashed {
sequencer: sequencer_da_address.as_ref().to_vec(),
Expand Down
22 changes: 12 additions & 10 deletions examples/demo-stf/src/runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ use sov_modules_api::macros::DefaultRuntime;
#[cfg(feature = "native")]
use sov_modules_api::macros::{expose_rpc, CliWallet};
use sov_modules_api::{Context, DispatchCall, Genesis, MessageCodec, Spec};
use sov_rollup_interface::da::{BlobReaderTrait, DaSpec};
use sov_rollup_interface::da::BlobReaderTrait;
use sov_rollup_interface::zk::ValidityCondition;
#[cfg(feature = "native")]
use sov_sequencer_registry::{SequencerRegistryRpcImpl, SequencerRegistryRpcServer};
use sov_state::WorkingSet;
Expand Down Expand Up @@ -70,6 +71,7 @@ pub mod query {
///
/// Similar mechanism works for queries with the difference that queries are submitted by users directly to the rollup node
/// instead of going through the DA layer.
///
#[cfg(not(feature = "experimental"))]
#[cfg_attr(feature = "native", derive(CliWallet), expose_rpc(DefaultContext))]
Expand All @@ -79,13 +81,13 @@ pub mod query {
feature = "native",
serialization(serde::Serialize, serde::Deserialize)
)]
pub struct Runtime<C: Context, DA: DaSpec> {
pub struct Runtime<C: Context, Cond: ValidityCondition> {
pub bank: sov_bank::Bank<C>,
pub sequencer_registry: sov_sequencer_registry::SequencerRegistry<C>,
#[cfg_attr(feature = "native", cli_skip)]
pub blob_storage: sov_blob_storage::BlobStorage<C>,
#[cfg_attr(feature = "native", cli_skip)]
pub chain_state: sov_chain_state::ChainState<C, DA::ValidityCondition>,
pub chain_state: sov_chain_state::ChainState<C, Cond>,
pub election: sov_election::Election<C>,
pub value_setter: sov_value_setter::ValueSetter<C>,
pub accounts: sov_accounts::Accounts<C>,
Expand All @@ -99,21 +101,21 @@ pub struct Runtime<C: Context, DA: DaSpec> {
feature = "native",
serialization(serde::Serialize, serde::Deserialize)
)]
pub struct Runtime<C: Context, DA: DaSpec> {
pub struct Runtime<C: Context, Cond: ValidityCondition> {
pub bank: sov_bank::Bank<C>,
pub sequencer_registry: sov_sequencer_registry::SequencerRegistry<C>,
#[cfg_attr(feature = "native", cli_skip)]
pub blob_storage: sov_blob_storage::BlobStorage<C>,
#[cfg_attr(feature = "native", cli_skip)]
pub chain_state: sov_chain_state::ChainState<C, DA::ValidityCondition>,
pub chain_state: sov_chain_state::ChainState<C, Cond>,
pub election: sov_election::Election<C>,
pub value_setter: sov_value_setter::ValueSetter<C>,
pub accounts: sov_accounts::Accounts<C>,
#[cfg_attr(feature = "native", cli_skip)]
pub evm: sov_evm::Evm<C>,
}

impl<C: Context, DA: DaSpec> SlotHooks<DA::ValidityCondition> for Runtime<C, DA> {
impl<C: Context, Cond: ValidityCondition> SlotHooks<Cond> for Runtime<C, Cond> {
type Context = C;

fn begin_slot_hook(
Expand All @@ -130,15 +132,15 @@ impl<C: Context, DA: DaSpec> SlotHooks<DA::ValidityCondition> for Runtime<C, DA>
}
}

impl<C, DA> sov_modules_stf_template::Runtime<C, DA::ValidityCondition, DA::BlobTransaction>
for Runtime<C, DA>
impl<C, Cond: ValidityCondition, B> sov_modules_stf_template::Runtime<C, Cond, B>
for Runtime<C, Cond>
where
C: Context,
DA: DaSpec,
B: BlobReaderTrait,
{
}

impl<C: Context, DA: DaSpec> BlobSelector for Runtime<C, DA> {
impl<C: Context, Cond: ValidityCondition> BlobSelector for Runtime<C, Cond> {
type Context = C;

fn get_blobs_for_this_slot<'a, I, B>(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,8 +141,8 @@ fn test_simple_value_setter_with_chain_state() {
assert_eq!(stored_root, init_root_hash.0, "Root hashes don't match");

// Check the slot height
let new_height_storage = chain_state_ref.get_slot_height(&mut working_set);
assert_eq!(new_height_storage, 2, "The new height did not update");
let new_height_storage = chain_state_ref.get_slot_height(&mut working_set).unwrap();
assert_eq!(2, new_height_storage, "The new height did not update");

// Check the tx in progress
let new_tx_in_progress: TransitionInProgress<MockValidityCond> = chain_state_ref
Expand Down

0 comments on commit ebef2a9

Please sign in to comment.