Skip to content

Commit

Permalink
Add docs for dmo-stf. (#998)
Browse files Browse the repository at this point in the history
* Make app a private module

* Create ZkApp

* fix configure_prover

* Docs for wallet cli

* Remove read_private_key

* Cleanup

* Move create_zk_app_template back

* demo-std refactoring

* fix lint

* Remove experimrntal runtime

* fix expose_rpc macro

* Add docs

* cleanup

* fix lint

* fix ci

* Fix Cargo.toml

* fix create_genesis_config

* Fix Readme

* update doc
  • Loading branch information
bkolad authored Oct 9, 2023
1 parent 367585e commit 1c3f77f
Show file tree
Hide file tree
Showing 18 changed files with 180 additions and 176 deletions.
2 changes: 1 addition & 1 deletion examples/demo-prover/benches/prover_bench.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ use std::sync::{Arc, Mutex};

use anyhow::Context;
use const_rollup_config::{ROLLUP_NAMESPACE_RAW, SEQUENCER_DA_ADDRESS};
use demo_stf::app::App;
use demo_stf::genesis_config::{get_genesis_config, GenesisPaths};
use demo_stf::App;
use log4rs::config::{Appender, Config, Root};
use methods::ROLLUP_ELF;
use regex::Regex;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@
#![no_main]

use const_rollup_config::ROLLUP_NAMESPACE_RAW;
use demo_stf::app::create_zk_app_template;
use demo_stf::{create_zk_app_template, AppVerifier};
use sov_celestia_adapter::types::NamespaceId;
use sov_celestia_adapter::verifier::CelestiaVerifier;
use demo_stf::AppVerifier;
use sov_risc0_adapter::guest::Risc0Guest;

// The rollup stores its data in the namespace b"sov-test" on Celestia
Expand Down
5 changes: 2 additions & 3 deletions examples/demo-prover/methods/guest-mock/src/bin/mock_da.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#![no_main]

use demo_stf::app::create_zk_app_template;
use demo_stf::AppVerifier;
use demo_stf::{create_zk_app_template, AppVerifier};
use sov_risc0_adapter::guest::Risc0Guest;

use sov_rollup_interface::mocks::MockDaVerifier;
Expand All @@ -13,7 +12,7 @@ pub fn main() {

let mut stf_verifier =
AppVerifier::new(create_zk_app_template::<Risc0Guest, _>(), MockDaVerifier {});

stf_verifier
.run_block(guest)
.expect("Prover must be honest");
Expand Down
2 changes: 1 addition & 1 deletion examples/demo-rollup/benches/rollup_bench.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ use std::time::Duration;

use anyhow::Context;
use criterion::{criterion_group, criterion_main, Criterion};
use demo_stf::app::App;
use demo_stf::genesis_config::{get_genesis_config, GenesisPaths};
use demo_stf::App;
use rng_xfers::{RngDaService, RngDaSpec};
use sov_db::ledger_db::{LedgerDB, SlotCommit};
use sov_risc0_adapter::host::Risc0Verifier;
Expand Down
2 changes: 1 addition & 1 deletion examples/demo-rollup/benches/rollup_coarse_measure.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ use std::sync::Arc;
use std::time::{Duration, Instant};

use anyhow::Context;
use demo_stf::app::App;
use demo_stf::genesis_config::{get_genesis_config, GenesisPaths};
use demo_stf::App;
use prometheus::{Histogram, HistogramOpts, Registry};
use rng_xfers::{RngDaService, RngDaSpec};
use sov_db::ledger_db::{LedgerDB, SlotCommit};
Expand Down
2 changes: 1 addition & 1 deletion examples/demo-rollup/src/register_rpc.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! Full-Node specific RPC methods.

use anyhow::Context;
use demo_stf::app::App;
use demo_stf::App;
use sov_celestia_adapter::verifier::address::CelestiaAddress;
use sov_db::ledger_db::LedgerDB;
#[cfg(feature = "experimental")]
Expand Down
16 changes: 8 additions & 8 deletions examples/demo-rollup/src/rollup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,9 @@ use std::str::FromStr;

use anyhow::Context;
use const_rollup_config::SEQUENCER_DA_ADDRESS;
#[cfg(feature = "experimental")]
use demo_stf::app::DefaultPrivateKey;
use demo_stf::app::{create_zk_app_template, App, DefaultContext};
use demo_stf::genesis_config::{get_genesis_config, GenesisPaths, StorageConfig};
use demo_stf::runtime::{get_rpc_methods, GenesisConfig, Runtime};
use demo_stf::AppVerifier;
use demo_stf::{create_zk_app_template, App, AppVerifier};
#[cfg(feature = "experimental")]
use secp256k1::SecretKey;
use sov_celestia_adapter::verifier::address::CelestiaAddress;
Expand All @@ -20,7 +17,9 @@ use sov_cli::wallet_state::PrivateKeyAndAddress;
use sov_db::ledger_db::LedgerDB;
#[cfg(feature = "experimental")]
use sov_ethereum::experimental::EthRpcConfig;
use sov_modules_api::default_context::ZkDefaultContext;
use sov_modules_api::default_context::{DefaultContext, ZkDefaultContext};
#[cfg(feature = "experimental")]
use sov_modules_api::default_signature::private_key::DefaultPrivateKey;
use sov_modules_stf_template::AppTemplate;
use sov_rollup_interface::mocks::{
MockAddress, MockDaConfig, MockDaService, MOCK_SEQUENCER_DA_ADDRESS,
Expand Down Expand Up @@ -69,9 +68,10 @@ pub fn configure_prover<Vm: ZkvmHost, Da: DaService>(
da_verifier: Da::Verifier,
) -> Prover<ZkStf<Da::Spec, Vm::Guest>, Da, Vm> {
let config = match cfg {
DemoProverConfig::Simulate => {
ProofGenConfig::Simulate(AppVerifier::new(create_zk_app_template(), da_verifier))
}
DemoProverConfig::Simulate => ProofGenConfig::Simulate(AppVerifier::new(
create_zk_app_template::<Vm::Guest, _>(),
da_verifier,
)),
DemoProverConfig::Execute => ProofGenConfig::Execute,
DemoProverConfig::Prove => ProofGenConfig::Prover,
};
Expand Down
2 changes: 1 addition & 1 deletion examples/demo-rollup/src/sov-cli/main.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#[tokio::main]
async fn main() -> Result<(), anyhow::Error> {
demo_stf::cli::run::<
demo_stf::cli::run_wallet::<
<sov_celestia_adapter::CelestiaService as sov_rollup_interface::services::da::DaService>::Spec,
>()
.await
Expand Down
2 changes: 1 addition & 1 deletion examples/demo-rollup/tests/bank/mod.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
use std::net::SocketAddr;

use borsh::BorshSerialize;
use demo_stf::app::DefaultPrivateKey;
use demo_stf::genesis_config::GenesisPaths;
use demo_stf::runtime::RuntimeCall;
use jsonrpsee::core::client::{Subscription, SubscriptionClientT};
use jsonrpsee::rpc_params;
use methods::MOCK_DA_ELF;
use sov_demo_rollup::DemoProverConfig;
use sov_modules_api::default_context::DefaultContext;
use sov_modules_api::default_signature::private_key::DefaultPrivateKey;
use sov_modules_api::transaction::Transaction;
use sov_modules_api::{PrivateKey, Spec};
use sov_risc0_adapter::host::Risc0Host;
Expand Down
16 changes: 14 additions & 2 deletions examples/demo-stf/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,15 +60,27 @@ The final piece of the puzzle is your app's runtime. A runtime is just a list of
module to your app, just add an additional field to the runtime.

```rust
use sov_modules_api::{Genesis, DispatchCall, MessageCodec, Context};
use sov_modules_api::macros::expose_rpc;
use sov_sequencer::Sequencer;
use sov_rollup_interface::da::DaSpec;
#[cfg(feature = "native")]
use sov_accounts::{AccountsRpcImpl, AccountsRpcServer};
#[cfg(feature = "native")]
use sov_bank::{BankRpcImpl, BankRpcServer};
#[cfg(feature = "native")]
use sov_sequencer_registry::{SequencerRegistryRpcImpl, SequencerRegistryRpcServer};


#[cfg_attr(
feature = "native",
expose_rpc(DefaultContext)
)]
#[derive(Genesis, DispatchCall, MessageCodec)]
#[serialization(borsh::BorshDeserialize, borsh::BorshSerialize)]
pub struct MyRuntime<C: Context> {
pub struct MyRuntime<C: Context, Da: DaSpec> {
#[allow(unused)]
sequencer: sov_sequencer_registry::Sequencer<C>,
sequencer: sov_sequencer_registry::SequencerRegistry<C, Da>,

#[allow(unused)]
bank: sov_bank::Bank<C>,
Expand Down
56 changes: 0 additions & 56 deletions examples/demo-stf/src/app.rs

This file was deleted.

10 changes: 6 additions & 4 deletions examples/demo-stf/src/cli.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//! Generates wallet cli for the runtime.
use sov_cli::wallet_state::WalletState;
use sov_cli::workflows::keys::KeyWorkflow;
use sov_cli::workflows::rpc::RpcWorkflows;
Expand All @@ -14,7 +15,7 @@ type Ctx = DefaultContext;

#[derive(clap::Subcommand)]
#[command(author, version, about, long_about = None)]
pub enum Workflows<Da: DaSpec> {
enum Workflows<Da: DaSpec> {
#[clap(subcommand)]
Transactions(
TransactionWorkflow<
Expand All @@ -30,12 +31,13 @@ pub enum Workflows<Da: DaSpec> {

#[derive(clap::Parser)]
#[command(author, version, about, long_about = None)]
pub struct App<Da: DaSpec> {
struct CliApp<Da: DaSpec> {
#[clap(subcommand)]
workflow: Workflows<Da>,
}

pub async fn run<Da: DaSpec + serde::Serialize + serde::de::DeserializeOwned>(
/// Runs wallet cli.
pub async fn run_wallet<Da: DaSpec + serde::Serialize + serde::de::DeserializeOwned>(
) -> Result<(), anyhow::Error> {
let app_dir = wallet_dir()?;
std::fs::create_dir_all(app_dir.as_ref())?;
Expand All @@ -44,7 +46,7 @@ pub async fn run<Da: DaSpec + serde::Serialize + serde::de::DeserializeOwned>(
let mut wallet_state: WalletState<RuntimeCall<Ctx, Da>, Ctx> =
WalletState::load(&wallet_state_path)?;

let invocation = App::<Da>::parse();
let invocation = CliApp::<Da>::parse();

match invocation.workflow {
Workflows::Transactions(tx) => tx
Expand Down
39 changes: 12 additions & 27 deletions examples/demo-stf/src/genesis_config.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
//! While the `GenesisConfig` type for `Rollup` is generated from the underlying runtime through a macro,
//! specific module configurations are obtained from files. This code is responsible for the logic
//! that transforms module genesis data into Rollup genesis data.

use std::convert::AsRef;
use std::path::Path;

Expand All @@ -6,7 +10,6 @@ use serde::de::DeserializeOwned;
use sov_accounts::AccountConfig;
use sov_bank::BankConfig;
use sov_chain_state::ChainStateConfig;
use sov_cli::wallet_state::PrivateKeyAndAddress;
#[cfg(feature = "experimental")]
use sov_evm::EvmConfig;
pub use sov_modules_api::default_context::DefaultContext;
Expand All @@ -20,18 +23,22 @@ use sov_value_setter::ValueSetterConfig;
/// Creates config for a rollup with some default settings, the config is used in demos and tests.
use crate::runtime::GenesisConfig;

pub const LOCKED_AMOUNT: u64 = 50;
pub const DEMO_TOKEN_NAME: &str = "sov-demo-token";

/// Paths pointing to genesis files.
pub struct GenesisPaths<P: AsRef<Path>> {
/// Bank genesis path.
pub bank_genesis_path: P,
/// Sequencer Registry genesis path.
pub sequencer_genesis_path: P,
/// Value Setter genesis path.
pub value_setter_genesis_path: P,
/// Accounts genesis path.
pub accounts_genesis_path: P,
/// Chain State genesis path.
pub chain_state_genesis_path: P,
/// Nft genesis path.
pub nft_path: P,
#[cfg(feature = "experimental")]
/// EVM genesis path.
pub evm_genesis_path: P,
}

Expand Down Expand Up @@ -108,9 +115,9 @@ fn create_genesis_config<C: Context, Da: DaSpec, P: AsRef<Path>>(
chain_state_config,
value_setter_config,
accounts_config,
nft_config,
#[cfg(feature = "experimental")]
evm_config,
nft_config,
))
}

Expand Down Expand Up @@ -141,25 +148,3 @@ fn get_evm_config<P: AsRef<Path>>(

Ok(config)
}

pub fn read_private_key<C: Context>() -> PrivateKeyAndAddress<C> {
// TODO fix the hardcoded path: #872
let token_deployer_data =
std::fs::read_to_string("../test-data/keys/token_deployer_private_key.json")
.expect("Unable to read file to string");

let token_deployer: PrivateKeyAndAddress<C> = serde_json::from_str(&token_deployer_data)
.unwrap_or_else(|_| {
panic!(
"Unable to convert data {} to PrivateKeyAndAddress",
&token_deployer_data
)
});

assert!(
token_deployer.is_matching_to_default(),
"Inconsistent key data"
);

token_deployer
}
Loading

0 comments on commit 1c3f77f

Please sign in to comment.