Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ValueSetter module: read genesis config from a file. #901

Merged
merged 4 commits into from
Sep 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
1 change: 1 addition & 0 deletions Cargo.lock

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

23 changes: 13 additions & 10 deletions examples/demo-stf/src/genesis_config.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
use anyhow::Context as AnyhowContext;
#[cfg(feature = "experimental")]
use reth_primitives::Bytes;
use sov_chain_state::ChainStateConfig;
use sov_cli::wallet_state::PrivateKeyAndAddress;
#[cfg(feature = "experimental")]
use sov_evm::{AccountData, EvmConfig, SpecId};
pub use sov_modules_api::default_context::DefaultContext;
use sov_modules_api::{Context, PrivateKey, PublicKey};
use sov_modules_api::Context;
use sov_rollup_interface::da::DaSpec;
pub use sov_state::config::Config as StorageConfig;
use sov_value_setter::ValueSetterConfig;
Expand Down Expand Up @@ -38,19 +39,18 @@ pub fn get_genesis_config<C: Context, Da: DaSpec>(
initial_sequencer_balance,
token_deployer.address.clone(),
sequencer_da_address,
&token_deployer.private_key,
#[cfg(feature = "experimental")]
evm_genesis_addresses,
)
.expect("Unable to read genesis configuration")
}

fn create_genesis_config<C: Context, Da: DaSpec>(
initial_sequencer_balance: u64,
sequencer_address: C::Address,
sequencer_da_address: Vec<u8>,
value_setter_admin_private_key: &C::PrivateKey,
#[cfg(feature = "experimental")] evm_genesis_addresses: Vec<reth_primitives::Address>,
) -> GenesisConfig<C, Da> {
) -> anyhow::Result<GenesisConfig<C, Da>> {
// This will be read from a file: #872
let token_config: sov_bank::TokenConfig<C> = sov_bank::TokenConfig {
token_name: DEMO_TOKEN_NAME.to_owned(),
Expand Down Expand Up @@ -81,10 +81,13 @@ fn create_genesis_config<C: Context, Da: DaSpec>(
is_preferred_sequencer: true,
};

// This will be read from a file: #872
let value_setter_config = ValueSetterConfig {
admin: value_setter_admin_private_key.pub_key().to_address(),
};
// This path will be injected as a parameter: #872
let value_setter_genesis_path = "../test-data/genesis/value_setter.json";
bkolad marked this conversation as resolved.
Show resolved Hide resolved
let value_setter_data = std::fs::read_to_string(value_setter_genesis_path)
.with_context(|| format!("Failed to read genesis from {}", value_setter_genesis_path))?;

let value_setter_config: ValueSetterConfig<C> = serde_json::from_str(&value_setter_data)
.with_context(|| format!("Failed to parse genesis from {}", value_setter_genesis_path))?;

// This will be read from a file: #872
let chain_state_config = ChainStateConfig {
Expand All @@ -93,7 +96,7 @@ fn create_genesis_config<C: Context, Da: DaSpec>(
current_time: Default::default(),
};

GenesisConfig::new(
Ok(GenesisConfig::new(
bank_config,
sequencer_registry_config,
(),
Expand All @@ -102,7 +105,7 @@ fn create_genesis_config<C: Context, Da: DaSpec>(
sov_accounts::AccountConfig { pub_keys: vec![] },
#[cfg(feature = "experimental")]
get_evm_config(evm_genesis_addresses),
)
))
}

// TODO: #840
Expand Down
3 changes: 3 additions & 0 deletions examples/test-data/genesis/value_setter.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"admin":"sov1l6n2cku82yfqld30lanm2nfw43n2auc8clw7r5u5m6s7p8jrm4zqrr8r94"
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ readme = "README.md"
resolver = "2"
publish = false


[dev-dependencies]
sov-value-setter = { path = ".", features = ["native"] }
tempfile = { workspace = true }

[dependencies]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ pub use query::*;
use sov_modules_api::{Error, ModuleInfo, WorkingSet};

/// Initial configuration for sov-value-setter module.
#[cfg_attr(
feature = "native",
derive(serde::Serialize, serde::Deserialize, Debug, PartialEq)
)]
pub struct ValueSetterConfig<C: sov_modules_api::Context> {
/// Admin of the module.
pub admin: C::Address,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,20 @@ use sov_state::{ProverStorage, ZkStorage};
use super::ValueSetter;
use crate::{call, query, ValueSetterConfig};

#[test]
fn test_config_serialization() {
let admin = Address::from([1; 32]);
let config = ValueSetterConfig::<DefaultContext> { admin };

let data = r#"
{
"admin":"sov1qyqszqgpqyqszqgpqyqszqgpqyqszqgpqyqszqgpqyqszqgpqyqs259tk3"
}"#;

let parsed_config: ValueSetterConfig<DefaultContext> = serde_json::from_str(data).unwrap();
assert_eq!(parsed_config, config);
}

#[test]
fn test_value_setter() {
let tmpdir = tempfile::tempdir().unwrap();
Expand Down