From bb7dfe7b6f105be4ffd82e5ea4e97a27289344e4 Mon Sep 17 00:00:00 2001 From: bkolad Date: Wed, 20 Sep 2023 09:21:07 +0200 Subject: [PATCH 1/4] test_config_serialization --- .../examples/sov-value-setter/src/lib.rs | 1 + .../examples/sov-value-setter/src/tests.rs | 11 +++++++++++ 2 files changed, 12 insertions(+) diff --git a/module-system/module-implementations/examples/sov-value-setter/src/lib.rs b/module-system/module-implementations/examples/sov-value-setter/src/lib.rs index 63b3ec422..758813081 100644 --- a/module-system/module-implementations/examples/sov-value-setter/src/lib.rs +++ b/module-system/module-implementations/examples/sov-value-setter/src/lib.rs @@ -15,6 +15,7 @@ pub use query::*; use sov_modules_api::{Error, ModuleInfo, WorkingSet}; /// Initial configuration for sov-value-setter module. +//#[derive(serde::Serialize, serde::Deserialize)] pub struct ValueSetterConfig { /// Admin of the module. pub admin: C::Address, diff --git a/module-system/module-implementations/examples/sov-value-setter/src/tests.rs b/module-system/module-implementations/examples/sov-value-setter/src/tests.rs index 4981f8369..8cdfdfa44 100644 --- a/module-system/module-implementations/examples/sov-value-setter/src/tests.rs +++ b/module-system/module-implementations/examples/sov-value-setter/src/tests.rs @@ -5,6 +5,17 @@ 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 { admin }; + + let config_str = serde_json::to_string(&config).unwrap(); + + println!("config_str {:?}", config_str); +} +*/ #[test] fn test_value_setter() { let tmpdir = tempfile::tempdir().unwrap(); From 693b8b03b41007c8e6fe8d334ca3f6232730f60c Mon Sep 17 00:00:00 2001 From: bkolad Date: Wed, 20 Sep 2023 09:41:58 +0200 Subject: [PATCH 2/4] ValueSetterConfig test --- Cargo.lock | 1 + .../examples/sov-value-setter/Cargo.toml | 2 ++ .../examples/sov-value-setter/src/lib.rs | 2 +- .../examples/sov-value-setter/src/tests.rs | 13 ++++++++----- 4 files changed, 12 insertions(+), 6 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 4e4e103cd..6eae2a27b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -7106,6 +7106,7 @@ dependencies = [ "serde_json", "sov-modules-api", "sov-state", + "sov-value-setter", "tempfile", "thiserror", ] diff --git a/module-system/module-implementations/examples/sov-value-setter/Cargo.toml b/module-system/module-implementations/examples/sov-value-setter/Cargo.toml index 3926cb558..392fbcf4a 100644 --- a/module-system/module-implementations/examples/sov-value-setter/Cargo.toml +++ b/module-system/module-implementations/examples/sov-value-setter/Cargo.toml @@ -12,7 +12,9 @@ readme = "README.md" resolver = "2" publish = false + [dev-dependencies] +sov-value-setter = { path = ".", features = ["native"] } tempfile = { workspace = true } [dependencies] diff --git a/module-system/module-implementations/examples/sov-value-setter/src/lib.rs b/module-system/module-implementations/examples/sov-value-setter/src/lib.rs index 758813081..a66addb13 100644 --- a/module-system/module-implementations/examples/sov-value-setter/src/lib.rs +++ b/module-system/module-implementations/examples/sov-value-setter/src/lib.rs @@ -15,7 +15,7 @@ pub use query::*; use sov_modules_api::{Error, ModuleInfo, WorkingSet}; /// Initial configuration for sov-value-setter module. -//#[derive(serde::Serialize, serde::Deserialize)] +#[derive(serde::Serialize, serde::Deserialize, Debug, PartialEq)] pub struct ValueSetterConfig { /// Admin of the module. pub admin: C::Address, diff --git a/module-system/module-implementations/examples/sov-value-setter/src/tests.rs b/module-system/module-implementations/examples/sov-value-setter/src/tests.rs index 8cdfdfa44..01e107f40 100644 --- a/module-system/module-implementations/examples/sov-value-setter/src/tests.rs +++ b/module-system/module-implementations/examples/sov-value-setter/src/tests.rs @@ -5,17 +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 { admin }; + let config = ValueSetterConfig:: { admin }; - let config_str = serde_json::to_string(&config).unwrap(); + let data = r#" + { + "admin":"sov1qyqszqgpqyqszqgpqyqszqgpqyqszqgpqyqszqgpqyqszqgpqyqs259tk3" + }"#; - println!("config_str {:?}", config_str); + let parsed_config: ValueSetterConfig = serde_json::from_str(data).unwrap(); + assert_eq!(parsed_config, config); } -*/ + #[test] fn test_value_setter() { let tmpdir = tempfile::tempdir().unwrap(); From df81bb8b12533daafd8213252aad2a76860203d5 Mon Sep 17 00:00:00 2001 From: bkolad Date: Wed, 20 Sep 2023 10:14:51 +0200 Subject: [PATCH 3/4] Read VS config frm a file --- examples/demo-stf/src/genesis_config.rs | 12 ++++++------ examples/test-data/genesis/value_setter.json | 3 +++ .../examples/sov-value-setter/src/lib.rs | 5 ++++- 3 files changed, 13 insertions(+), 7 deletions(-) create mode 100644 examples/test-data/genesis/value_setter.json diff --git a/examples/demo-stf/src/genesis_config.rs b/examples/demo-stf/src/genesis_config.rs index 083612202..2d092f314 100644 --- a/examples/demo-stf/src/genesis_config.rs +++ b/examples/demo-stf/src/genesis_config.rs @@ -5,7 +5,7 @@ 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; @@ -38,7 +38,6 @@ pub fn get_genesis_config( initial_sequencer_balance, token_deployer.address.clone(), sequencer_da_address, - &token_deployer.private_key, #[cfg(feature = "experimental")] evm_genesis_addresses, ) @@ -48,7 +47,6 @@ fn create_genesis_config( initial_sequencer_balance: u64, sequencer_address: C::Address, sequencer_da_address: Vec, - value_setter_admin_private_key: &C::PrivateKey, #[cfg(feature = "experimental")] evm_genesis_addresses: Vec, ) -> GenesisConfig { // This will be read from a file: #872 @@ -82,9 +80,11 @@ fn create_genesis_config( }; // This will be read from a file: #872 - let value_setter_config = ValueSetterConfig { - admin: value_setter_admin_private_key.pub_key().to_address(), - }; + let value_setter_data = std::fs::read_to_string("../test-data/genesis/value_setter.json") + .expect("Unable to read file to string"); + + let value_setter_config: ValueSetterConfig = + serde_json::from_str(&value_setter_data).unwrap(); // This will be read from a file: #872 let chain_state_config = ChainStateConfig { diff --git a/examples/test-data/genesis/value_setter.json b/examples/test-data/genesis/value_setter.json new file mode 100644 index 000000000..35c01dd23 --- /dev/null +++ b/examples/test-data/genesis/value_setter.json @@ -0,0 +1,3 @@ +{ + "admin":"sov1l6n2cku82yfqld30lanm2nfw43n2auc8clw7r5u5m6s7p8jrm4zqrr8r94" +} \ No newline at end of file diff --git a/module-system/module-implementations/examples/sov-value-setter/src/lib.rs b/module-system/module-implementations/examples/sov-value-setter/src/lib.rs index a66addb13..a49519240 100644 --- a/module-system/module-implementations/examples/sov-value-setter/src/lib.rs +++ b/module-system/module-implementations/examples/sov-value-setter/src/lib.rs @@ -15,7 +15,10 @@ pub use query::*; use sov_modules_api::{Error, ModuleInfo, WorkingSet}; /// Initial configuration for sov-value-setter module. -#[derive(serde::Serialize, serde::Deserialize, Debug, PartialEq)] +#[cfg_attr( + feature = "native", + derive(serde::Serialize, serde::Deserialize, Debug, PartialEq) +)] pub struct ValueSetterConfig { /// Admin of the module. pub admin: C::Address, From 2263aa14ff3f3a10b16bc37af36ea07fb571a1d1 Mon Sep 17 00:00:00 2001 From: bkolad Date: Wed, 20 Sep 2023 10:27:29 +0200 Subject: [PATCH 4/4] Error handling --- examples/demo-stf/src/genesis_config.rs | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/examples/demo-stf/src/genesis_config.rs b/examples/demo-stf/src/genesis_config.rs index 2d092f314..9a07fb33a 100644 --- a/examples/demo-stf/src/genesis_config.rs +++ b/examples/demo-stf/src/genesis_config.rs @@ -1,3 +1,4 @@ +use anyhow::Context as AnyhowContext; #[cfg(feature = "experimental")] use reth_primitives::Bytes; use sov_chain_state::ChainStateConfig; @@ -41,6 +42,7 @@ pub fn get_genesis_config( #[cfg(feature = "experimental")] evm_genesis_addresses, ) + .expect("Unable to read genesis configuration") } fn create_genesis_config( @@ -48,7 +50,7 @@ fn create_genesis_config( sequencer_address: C::Address, sequencer_da_address: Vec, #[cfg(feature = "experimental")] evm_genesis_addresses: Vec, -) -> GenesisConfig { +) -> anyhow::Result> { // This will be read from a file: #872 let token_config: sov_bank::TokenConfig = sov_bank::TokenConfig { token_name: DEMO_TOKEN_NAME.to_owned(), @@ -79,12 +81,13 @@ fn create_genesis_config( is_preferred_sequencer: true, }; - // This will be read from a file: #872 - let value_setter_data = std::fs::read_to_string("../test-data/genesis/value_setter.json") - .expect("Unable to read file to string"); + // This path will be injected as a parameter: #872 + let value_setter_genesis_path = "../test-data/genesis/value_setter.json"; + 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 = - serde_json::from_str(&value_setter_data).unwrap(); + let value_setter_config: ValueSetterConfig = 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 { @@ -93,7 +96,7 @@ fn create_genesis_config( current_time: Default::default(), }; - GenesisConfig::new( + Ok(GenesisConfig::new( bank_config, sequencer_registry_config, (), @@ -102,7 +105,7 @@ fn create_genesis_config( sov_accounts::AccountConfig { pub_keys: vec![] }, #[cfg(feature = "experimental")] get_evm_config(evm_genesis_addresses), - ) + )) } // TODO: #840