Skip to content

Commit

Permalink
De-anonymizing BlobTransactionTrait in STF (#500)
Browse files Browse the repository at this point in the history
* De-anonymizing blobtrait
* Consistency with import
  • Loading branch information
citizen-stig authored Jul 18, 2023
1 parent c38d821 commit 843c79d
Show file tree
Hide file tree
Showing 18 changed files with 111 additions and 80 deletions.
4 changes: 3 additions & 1 deletion examples/demo-prover/host/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use demo_stf::runner_config::{from_toml_path, Config as RunnerConfig};
use jupiter::da_service::{CelestiaService, DaServiceConfig};
use jupiter::types::NamespaceId;
use jupiter::verifier::RollupParams;
use jupiter::BlobWithSender;
use methods::{ROLLUP_ELF, ROLLUP_ID};
use risc0_adapter::host::Risc0Host;
use serde::Deserialize;
Expand Down Expand Up @@ -55,7 +56,8 @@ async fn main() -> Result<(), anyhow::Error> {

let sequencer_private_key = DefaultPrivateKey::generate();

let mut demo_runner = NativeAppRunner::<Risc0Host>::new(rollup_config.runner.clone());
let mut demo_runner =
NativeAppRunner::<Risc0Host, BlobWithSender>::new(rollup_config.runner.clone());
let is_storage_empty = demo_runner.get_storage().is_empty();
let demo = demo_runner.inner_mut();

Expand Down
3 changes: 2 additions & 1 deletion examples/demo-prover/methods/guest/src/bin/rollup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,10 @@ pub fn main() {
env::write(&"txs read\n");

// Step 2: Apply blobs
let mut demo_runner = <ZkAppRunner<Risc0Guest> as StateTransitionRunner<
let mut demo_runner = <ZkAppRunner<Risc0Guest, BlobWithSender> as StateTransitionRunner<
ZkConfig,
Risc0Guest,
BlobWithSender,
>>::new(prev_state_root_hash);
let demo = demo_runner.inner_mut();

Expand Down
6 changes: 4 additions & 2 deletions examples/demo-rollup/benches/rollup_bench.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,13 @@ use criterion::{criterion_group, criterion_main, Criterion};
use demo_stf::app::NativeAppRunner;
use demo_stf::genesis_config::create_demo_genesis_config;
use demo_stf::runner_config::from_toml_path;
use jupiter::verifier::address::CelestiaAddress;
use risc0_adapter::host::Risc0Verifier;
use sov_db::ledger_db::{LedgerDB, SlotCommit};
use sov_demo_rollup::config::RollupConfig;
use sov_demo_rollup::rng_xfers::RngDaService;
use sov_modules_api::default_signature::private_key::DefaultPrivateKey;
use sov_rollup_interface::mocks::{TestBlock, TestBlockHeader, TestHash};
use sov_rollup_interface::mocks::{TestBlob, TestBlock, TestBlockHeader, TestHash};
use sov_rollup_interface::services::da::DaService;
use sov_rollup_interface::services::stf_runner::StateTransitionRunner;
use sov_rollup_interface::stf::StateTransitionFunction;
Expand Down Expand Up @@ -42,7 +43,8 @@ fn rollup_bench(_bench: &mut Criterion) {

let da_service = Arc::new(RngDaService::new());

let mut demo_runner = NativeAppRunner::<Risc0Verifier>::new(rollup_config.runner);
let mut demo_runner =
NativeAppRunner::<Risc0Verifier, TestBlob<CelestiaAddress>>::new(rollup_config.runner);

let demo = demo_runner.inner_mut();
let sequencer_private_key = DefaultPrivateKey::generate();
Expand Down
7 changes: 5 additions & 2 deletions examples/demo-rollup/benches/rollup_coarse_measure.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,22 @@ use const_rollup_config::SEQUENCER_DA_ADDRESS;
use demo_stf::app::NativeAppRunner;
use demo_stf::genesis_config::create_demo_genesis_config;
use demo_stf::runner_config::from_toml_path;
use jupiter::verifier::address::CelestiaAddress;
use prometheus::{Histogram, HistogramOpts, Registry};
use risc0_adapter::host::Risc0Verifier;
use sov_db::ledger_db::{LedgerDB, SlotCommit};
use sov_demo_rollup::config::RollupConfig;
use sov_demo_rollup::rng_xfers::RngDaService;
use sov_modules_api::default_signature::private_key::DefaultPrivateKey;
use sov_rollup_interface::mocks::{TestBlock, TestBlockHeader, TestHash};
use sov_rollup_interface::mocks::{TestBlob, TestBlock, TestBlockHeader, TestHash};
use sov_rollup_interface::services::da::DaService;
use sov_rollup_interface::services::stf_runner::StateTransitionRunner;
use sov_rollup_interface::stf::StateTransitionFunction;
use tempfile::TempDir;

#[macro_use]
extern crate prettytable;

use prettytable::Table;

fn print_times(
Expand Down Expand Up @@ -113,7 +115,8 @@ async fn main() -> Result<(), anyhow::Error> {

let da_service = Arc::new(RngDaService::new());

let mut demo_runner = NativeAppRunner::<Risc0Verifier>::new(rollup_config.runner);
let mut demo_runner =
NativeAppRunner::<Risc0Verifier, TestBlob<CelestiaAddress>>::new(rollup_config.runner);

let demo = demo_runner.inner_mut();
let sequencer_private_key = DefaultPrivateKey::generate();
Expand Down
4 changes: 3 additions & 1 deletion examples/demo-rollup/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ use jsonrpsee::core::server::rpc_module::Methods;
use jupiter::da_service::CelestiaService;
use jupiter::types::NamespaceId;
use jupiter::verifier::{CelestiaVerifier, ChainValidityCondition, RollupParams};
use jupiter::BlobWithSender;
use risc0_adapter::host::Risc0Verifier;
use sov_db::ledger_db::{LedgerDB, SlotCommit};
#[cfg(feature = "experimental")]
Expand Down Expand Up @@ -145,7 +146,8 @@ async fn main() -> Result<(), anyhow::Error> {

// Our state transition function implements the StateTransitionRunner interface,
// so we use that to initialize the STF
let mut demo_runner = NativeAppRunner::<Risc0Verifier>::new(rollup_config.runner.clone());
let mut demo_runner =
NativeAppRunner::<Risc0Verifier, BlobWithSender>::new(rollup_config.runner.clone());

// Our state transition also implements the RpcRunner interface,
// so we use that to initialize the RPC server.
Expand Down
4 changes: 2 additions & 2 deletions examples/demo-simple-stf/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ pub enum ApplyBlobResult {
Success,
}

impl<VM: Zkvm> StateTransitionFunction<VM> for CheckHashPreimageStf {
impl<Vm: Zkvm, B: BlobTransactionTrait> StateTransitionFunction<Vm, B> for CheckHashPreimageStf {
// Since our rollup is stateless, we don't need to consider the StateRoot.
type StateRoot = ();

Expand Down Expand Up @@ -49,7 +49,7 @@ impl<VM: Zkvm> StateTransitionFunction<VM> for CheckHashPreimageStf {
// The core logic of our rollup.
fn apply_blob(
&mut self,
blob: &mut impl BlobTransactionTrait,
blob: &mut B,
_misbehavior_hint: Option<Self::MisbehaviorProof>,
) -> BatchReceipt<Self::BatchReceiptContents, Self::TxReceiptContents> {
let blob_data = blob.data_mut();
Expand Down
12 changes: 8 additions & 4 deletions examples/demo-simple-stf/tests/stf_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,15 @@ fn test_stf() {
let mut test_blob = TestBlob::<DaAddress>::new(preimage, address, [0; 32]);
let stf = &mut CheckHashPreimageStf {};

StateTransitionFunction::<MockZkvm>::init_chain(stf, ());
StateTransitionFunction::<MockZkvm>::begin_slot(stf, ());
StateTransitionFunction::<MockZkvm, TestBlob<DaAddress>>::init_chain(stf, ());
StateTransitionFunction::<MockZkvm, TestBlob<DaAddress>>::begin_slot(stf, ());

let receipt = StateTransitionFunction::<MockZkvm>::apply_blob(stf, &mut test_blob, None);
let receipt = StateTransitionFunction::<MockZkvm, TestBlob<DaAddress>>::apply_blob(
stf,
&mut test_blob,
None,
);
assert_eq!(receipt.inner, ApplyBlobResult::Success);

StateTransitionFunction::<MockZkvm>::end_slot(stf);
StateTransitionFunction::<MockZkvm, TestBlob<DaAddress>>::end_slot(stf);
}
27 changes: 16 additions & 11 deletions examples/demo-stf/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use sov_modules_api::RpcRunner;
use sov_modules_api::Spec;
pub use sov_modules_stf_template::Batch;
use sov_modules_stf_template::{AppTemplate, SequencerOutcome, TxEffect};
use sov_rollup_interface::da::BlobTransactionTrait;
use sov_rollup_interface::services::stf_runner::StateTransitionRunner;
#[cfg(feature = "native")]
use sov_rollup_interface::stf::ProverConfig;
Expand All @@ -24,27 +25,29 @@ use crate::batch_builder::FiFoStrictBatchBuilder;
use crate::runner_config::Config;
use crate::runtime::Runtime;

pub struct DemoAppRunner<C: Context, Vm: Zkvm> {
pub stf: DemoApp<C, Vm>,
pub struct DemoAppRunner<C: Context, Vm: Zkvm, B: BlobTransactionTrait> {
pub stf: DemoApp<C, Vm, B>,
pub batch_builder: Option<FiFoStrictBatchBuilder<Runtime<C>, C>>,
}

pub type ZkAppRunner<Vm> = DemoAppRunner<ZkDefaultContext, Vm>;
pub type ZkAppRunner<Vm, B> = DemoAppRunner<ZkDefaultContext, Vm, B>;

#[cfg(feature = "native")]
pub type NativeAppRunner<Vm> = DemoAppRunner<DefaultContext, Vm>;
pub type NativeAppRunner<Vm, B> = DemoAppRunner<DefaultContext, Vm, B>;

pub type DemoApp<C, Vm> = AppTemplate<C, Runtime<C>, Vm>;
pub type DemoApp<C, Vm, B> = AppTemplate<C, Runtime<C>, Vm, B>;

/// Batch receipt type used by the demo app. We export this type so that it's easily accessible to the full node.
pub type DemoBatchReceipt = SequencerOutcome;
/// Tx receipt type used by the demo app. We export this type so that it's easily accessible to the full node.
pub type DemoTxReceipt = TxEffect;

#[cfg(feature = "native")]
impl<Vm: Zkvm> StateTransitionRunner<ProverConfig, Vm> for DemoAppRunner<DefaultContext, Vm> {
impl<Vm: Zkvm, B: BlobTransactionTrait> StateTransitionRunner<ProverConfig, Vm, B>
for DemoAppRunner<DefaultContext, Vm, B>
{
type RuntimeConfig = Config;
type Inner = DemoApp<DefaultContext, Vm>;
type Inner = DemoApp<DefaultContext, Vm, B>;
type BatchBuilder = FiFoStrictBatchBuilder<Runtime<DefaultContext>, DefaultContext>;

fn new(runtime_config: Self::RuntimeConfig) -> Self {
Expand Down Expand Up @@ -77,14 +80,16 @@ impl<Vm: Zkvm> StateTransitionRunner<ProverConfig, Vm> for DemoAppRunner<Default
}
}

impl<Vm: Zkvm> StateTransitionRunner<ZkConfig, Vm> for DemoAppRunner<ZkDefaultContext, Vm> {
impl<Vm: Zkvm, B: BlobTransactionTrait> StateTransitionRunner<ZkConfig, Vm, B>
for DemoAppRunner<ZkDefaultContext, Vm, B>
{
type RuntimeConfig = [u8; 32];
type Inner = DemoApp<ZkDefaultContext, Vm>;
type Inner = DemoApp<ZkDefaultContext, Vm, B>;
type BatchBuilder = FiFoStrictBatchBuilder<Runtime<ZkDefaultContext>, ZkDefaultContext>;

fn new(runtime_config: Self::RuntimeConfig) -> Self {
let storage = ZkStorage::with_config(runtime_config).expect("Failed to open zk storage");
let app: AppTemplate<ZkDefaultContext, Runtime<ZkDefaultContext>, Vm> =
let app: AppTemplate<ZkDefaultContext, Runtime<ZkDefaultContext>, Vm, B> =
AppTemplate::new(storage.clone(), Runtime::default());

let batch_size_bytes = 1024 * 100; // 100 KB
Expand Down Expand Up @@ -114,7 +119,7 @@ impl<Vm: Zkvm> StateTransitionRunner<ZkConfig, Vm> for DemoAppRunner<ZkDefaultCo
}

#[cfg(feature = "native")]
impl<Vm: Zkvm> RpcRunner for DemoAppRunner<DefaultContext, Vm> {
impl<Vm: Zkvm, B: BlobTransactionTrait> RpcRunner for DemoAppRunner<DefaultContext, Vm, B> {
type Context = DefaultContext;
fn get_storage(&self) -> <Self::Context as Spec>::Storage {
self.inner().current_storage.clone()
Expand Down
20 changes: 12 additions & 8 deletions examples/demo-stf/src/sov-cli/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -401,7 +401,7 @@ mod test {
// Test helpers
struct TestDemo {
config: GenesisConfig<C>,
demo: DemoApp<C, MockZkvm>,
demo: DemoApp<C, MockZkvm, TestBlob>,
}

impl TestDemo {
Expand All @@ -421,7 +421,7 @@ mod test {

Self {
config: genesis_config,
demo: DemoAppRunner::<DefaultContext, MockZkvm>::new(runner_config).stf,
demo: DemoAppRunner::<DefaultContext, MockZkvm, TestBlob>::new(runner_config).stf,
}
}
}
Expand Down Expand Up @@ -476,11 +476,15 @@ mod test {
}
}

fn execute_txs(demo: &mut DemoApp<C, MockZkvm>, config: GenesisConfig<C>, txs: Vec<RawTx>) {
StateTransitionFunction::<MockZkvm>::init_chain(demo, config);
StateTransitionFunction::<MockZkvm>::begin_slot(demo, Default::default());
fn execute_txs(
demo: &mut DemoApp<C, MockZkvm, TestBlob>,
config: GenesisConfig<C>,
txs: Vec<RawTx>,
) {
StateTransitionFunction::<MockZkvm, TestBlob>::init_chain(demo, config);
StateTransitionFunction::<MockZkvm, TestBlob>::begin_slot(demo, Default::default());

let apply_blob_outcome = StateTransitionFunction::<MockZkvm>::apply_blob(
let apply_blob_outcome = StateTransitionFunction::<MockZkvm, TestBlob>::apply_blob(
demo,
&mut new_test_blob(Batch { txs }, &DEMO_SEQUENCER_DA_ADDRESS),
None,
Expand All @@ -491,11 +495,11 @@ mod test {
apply_blob_outcome,
"Sequencer execution should have succeeded but failed",
);
StateTransitionFunction::<MockZkvm>::end_slot(demo);
StateTransitionFunction::<MockZkvm, TestBlob>::end_slot(demo);
}

fn get_balance(
demo: &mut DemoApp<DefaultContext, MockZkvm>,
demo: &mut DemoApp<DefaultContext, MockZkvm, TestBlob>,
token_deployer_address: &Address,
user_address: Address,
) -> Option<u64> {
Expand Down
2 changes: 1 addition & 1 deletion examples/demo-stf/src/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ pub type TestBlob = sov_rollup_interface::mocks::TestBlob<Address>;

pub fn create_new_demo(
path: impl AsRef<Path>,
) -> DemoApp<DefaultContext, sov_rollup_interface::mocks::MockZkvm> {
) -> DemoApp<DefaultContext, sov_rollup_interface::mocks::MockZkvm, TestBlob> {
let runtime = Runtime::default();
let storage = ProverStorage::with_path(path).unwrap();
AppTemplate::new(storage, runtime)
Expand Down
36 changes: 21 additions & 15 deletions examples/demo-stf/src/tests/stf_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ pub mod test {
use crate::genesis_config::{create_demo_config, DEMO_SEQUENCER_DA_ADDRESS, LOCKED_AMOUNT};
use crate::runtime::Runtime;
use crate::tests::data_generation::simulate_da;
use crate::tests::{create_new_demo, has_tx_events, new_test_blob, C};
use crate::tests::{create_new_demo, has_tx_events, new_test_blob, TestBlob, C};

#[test]
fn test_demo_values_in_db() {
Expand All @@ -27,12 +27,15 @@ pub mod test {
{
let mut demo = create_new_demo(path);

StateTransitionFunction::<MockZkvm>::init_chain(&mut demo, config);
StateTransitionFunction::<MockZkvm>::begin_slot(&mut demo, Default::default());
StateTransitionFunction::<MockZkvm, TestBlob>::init_chain(&mut demo, config);
StateTransitionFunction::<MockZkvm, TestBlob>::begin_slot(
&mut demo,
Default::default(),
);

let txs = simulate_da(value_setter_admin_private_key, election_admin_private_key);

let apply_blob_outcome = StateTransitionFunction::<MockZkvm>::apply_blob(
let apply_blob_outcome = StateTransitionFunction::<MockZkvm, TestBlob>::apply_blob(
&mut demo,
&mut new_test_blob(Batch { txs }, &DEMO_SEQUENCER_DA_ADDRESS),
None,
Expand All @@ -46,7 +49,7 @@ pub mod test {

assert!(has_tx_events(&apply_blob_outcome),);

StateTransitionFunction::<MockZkvm>::end_slot(&mut demo);
StateTransitionFunction::<MockZkvm, TestBlob>::end_slot(&mut demo);
}

// Generate a new storage instance after dumping data to the db.
Expand Down Expand Up @@ -85,12 +88,12 @@ pub mod test {
&election_admin_private_key,
);

StateTransitionFunction::<MockZkvm>::init_chain(&mut demo, config);
StateTransitionFunction::<MockZkvm>::begin_slot(&mut demo, Default::default());
StateTransitionFunction::<MockZkvm, TestBlob>::init_chain(&mut demo, config);
StateTransitionFunction::<MockZkvm, TestBlob>::begin_slot(&mut demo, Default::default());

let txs = simulate_da(value_setter_admin_private_key, election_admin_private_key);

let apply_blob_outcome = StateTransitionFunction::<MockZkvm>::apply_blob(
let apply_blob_outcome = StateTransitionFunction::<MockZkvm, TestBlob>::apply_blob(
&mut demo,
&mut new_test_blob(Batch { txs }, &DEMO_SEQUENCER_DA_ADDRESS),
None,
Expand All @@ -104,7 +107,7 @@ pub mod test {

assert!(has_tx_events(&apply_blob_outcome),);

StateTransitionFunction::<MockZkvm>::end_slot(&mut demo);
StateTransitionFunction::<MockZkvm, TestBlob>::end_slot(&mut demo);

let runtime = &mut Runtime::<DefaultContext>::default();
let mut working_set = WorkingSet::new(demo.current_storage.clone());
Expand Down Expand Up @@ -140,12 +143,15 @@ pub mod test {
{
let mut demo = create_new_demo(path);

StateTransitionFunction::<MockZkvm>::init_chain(&mut demo, config);
StateTransitionFunction::<MockZkvm>::begin_slot(&mut demo, Default::default());
StateTransitionFunction::<MockZkvm, TestBlob>::init_chain(&mut demo, config);
StateTransitionFunction::<MockZkvm, TestBlob>::begin_slot(
&mut demo,
Default::default(),
);

let txs = simulate_da(value_setter_admin_private_key, election_admin_private_key);

let apply_blob_outcome = StateTransitionFunction::<MockZkvm>::apply_blob(
let apply_blob_outcome = StateTransitionFunction::<MockZkvm, TestBlob>::apply_blob(
&mut demo,
&mut new_test_blob(Batch { txs }, &DEMO_SEQUENCER_DA_ADDRESS),
None,
Expand Down Expand Up @@ -193,13 +199,13 @@ pub mod test {

let mut demo = create_new_demo(path);

StateTransitionFunction::<MockZkvm>::init_chain(&mut demo, config);
StateTransitionFunction::<MockZkvm>::begin_slot(&mut demo, Default::default());
StateTransitionFunction::<MockZkvm, TestBlob>::init_chain(&mut demo, config);
StateTransitionFunction::<MockZkvm, TestBlob>::begin_slot(&mut demo, Default::default());

let txs = simulate_da(value_setter_admin_private_key, election_admin_private_key);

let some_sequencer: [u8; 32] = [121; 32];
let apply_blob_outcome = StateTransitionFunction::<MockZkvm>::apply_blob(
let apply_blob_outcome = StateTransitionFunction::<MockZkvm, TestBlob>::apply_blob(
&mut demo,
&mut new_test_blob(Batch { txs }, &some_sequencer),
None,
Expand Down
Loading

0 comments on commit 843c79d

Please sign in to comment.