Skip to content

Commit

Permalink
Read bank genesis from a file. (#961)
Browse files Browse the repository at this point in the history
* Read bank genesis from a file

* fix lint

* add read_json_file

* make lint fix

* fix lint
  • Loading branch information
bkolad authored Oct 2, 2023
1 parent 4f4e27e commit 86d1f00
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 29 deletions.
47 changes: 21 additions & 26 deletions examples/demo-stf/src/genesis_config.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
use std::convert::AsRef;
use std::path::Path;

use anyhow::Context as AnyhowContext;
#[cfg(feature = "experimental")]
use reth_primitives::Bytes;
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")]
Expand Down Expand Up @@ -32,12 +37,9 @@ pub fn get_genesis_config<C: Context, Da: DaSpec>(
sequencer_da_address: Da::Address,
#[cfg(feature = "experimental")] evm_genesis_addresses: Vec<reth_primitives::Address>,
) -> GenesisConfig<C, Da> {
// This will be read from a file: #872
let initial_sequencer_balance = 100000000;
let token_deployer: PrivateKeyAndAddress<C> = read_private_key();

create_genesis_config(
initial_sequencer_balance,
token_deployer.address.clone(),
sequencer_da_address,
#[cfg(feature = "experimental")]
Expand All @@ -47,24 +49,13 @@ pub fn get_genesis_config<C: Context, Da: DaSpec>(
}

fn create_genesis_config<C: Context, Da: DaSpec>(
initial_sequencer_balance: u64,
sequencer_address: C::Address,
sequencer_da_address: Da::Address,
#[cfg(feature = "experimental")] evm_genesis_addresses: Vec<reth_primitives::Address>,
) -> 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(),
address_and_balances: vec![(sequencer_address.clone(), initial_sequencer_balance)],
authorized_minters: vec![sequencer_address.clone()],
salt: 0,
};

// This will be read from a file: #872
let bank_config = sov_bank::BankConfig {
tokens: vec![token_config],
};

// This path will be injected as a parameter: #872
let bank_genesis_path = "../test-data/genesis/bank.json";
let bank_config: BankConfig<C> = read_json_file(bank_genesis_path)?;
// This will be read from a file: #872
let token_address = sov_bank::get_genesis_token_address::<C>(
&bank_config.tokens[0].token_name,
Expand All @@ -84,17 +75,10 @@ fn create_genesis_config<C: Context, Da: DaSpec>(

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

let accounts_genesis_path = "../test-data/genesis/accounts.json";
let accounts_data = std::fs::read_to_string(accounts_genesis_path)
.with_context(|| format!("Failed to read genesis from {}", accounts_genesis_path))?;

let accounts_config: AccountConfig<C> = serde_json::from_str(&accounts_data)
.with_context(|| format!("Failed to parse genesis from {}", accounts_genesis_path))?;
let accounts_config: AccountConfig<C> = read_json_file(accounts_genesis_path)?;

let nft_config = sov_nft_module::NonFungibleTokenConfig {};
// This will be read from a file: #872
Expand All @@ -117,6 +101,17 @@ fn create_genesis_config<C: Context, Da: DaSpec>(
))
}

fn read_json_file<T: DeserializeOwned, P: AsRef<Path>>(path: P) -> anyhow::Result<T> {
let path_str = path.as_ref().display();

let data = std::fs::read_to_string(&path)
.with_context(|| format!("Failed to read genesis from {}", path_str))?;
let config: T = serde_json::from_str(&data)
.with_context(|| format!("Failed to parse genesis from {}", path_str))?;

Ok(config)
}

// TODO: #840
#[cfg(feature = "experimental")]
fn get_evm_config(genesis_addresses: Vec<reth_primitives::Address>) -> EvmConfig {
Expand Down
10 changes: 10 additions & 0 deletions examples/test-data/genesis/bank.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"tokens":[
{
"token_name":"sov-demo-token",
"address_and_balances":[["sov1l6n2cku82yfqld30lanm2nfw43n2auc8clw7r5u5m6s7p8jrm4zqrr8r94",100000000]],
"authorized_minters":["sov1l6n2cku82yfqld30lanm2nfw43n2auc8clw7r5u5m6s7p8jrm4zqrr8r94"]
,"salt":0
}
]
}
10 changes: 7 additions & 3 deletions module-system/module-implementations/sov-bank/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@ mod genesis;
mod query;
#[cfg(feature = "native")]
pub use query::*;
#[cfg(test)]
mod tests;
mod token;
/// Util functions for bank
pub mod utils;

/// Specifies the call methods using in that module.
pub use call::CallMessage;
use serde::de::DeserializeOwned;
use serde::{Deserialize, Serialize};
use sov_modules_api::{CallResponse, Error, GasUnit, ModuleInfo, WorkingSet};
use token::Token;
Expand All @@ -22,7 +24,8 @@ pub use utils::{get_genesis_token_address, get_token_address};

/// [`TokenConfig`] specifies a configuration used when generating a token for the bank
/// module.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[derive(Debug, Clone, Serialize, Deserialize, Eq, PartialEq)]
#[serde(bound = "C::Address: Serialize + DeserializeOwned")]
pub struct TokenConfig<C: sov_modules_api::Context> {
/// The name of the token.
pub token_name: String,
Expand All @@ -35,7 +38,8 @@ pub struct TokenConfig<C: sov_modules_api::Context> {
}

/// Initial configuration for sov-bank module.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[derive(Debug, Clone, Serialize, Deserialize, Eq, PartialEq)]
#[serde(bound = "C::Address: Serialize + DeserializeOwned")]
pub struct BankConfig<C: sov_modules_api::Context> {
/// A list of configurations for the initial tokens.
pub tokens: Vec<TokenConfig<C>>,
Expand Down
39 changes: 39 additions & 0 deletions module-system/module-implementations/sov-bank/src/tests.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
use std::str::FromStr;

use sov_modules_api::default_context::DefaultContext;
use sov_modules_api::{AddressBech32, Spec};

use crate::{BankConfig, TokenConfig};

#[test]
fn test_config_serialization() {
let address: <DefaultContext as Spec>::Address =
AddressBech32::from_str("sov1l6n2cku82yfqld30lanm2nfw43n2auc8clw7r5u5m6s7p8jrm4zqrr8r94")
.unwrap()
.into();

let config = BankConfig::<DefaultContext> {
tokens: vec![TokenConfig {
token_name: "sov-demo-token".to_owned(),
address_and_balances: vec![(address, 100000000)],
authorized_minters: vec![address],
salt: 0,
}],
};

let data = r#"
{
"tokens":[
{
"token_name":"sov-demo-token",
"address_and_balances":[["sov1l6n2cku82yfqld30lanm2nfw43n2auc8clw7r5u5m6s7p8jrm4zqrr8r94",100000000]],
"authorized_minters":["sov1l6n2cku82yfqld30lanm2nfw43n2auc8clw7r5u5m6s7p8jrm4zqrr8r94"]
,"salt":0
}
]
}"#;

let parsed_config: BankConfig<DefaultContext> = serde_json::from_str(data).unwrap();

assert_eq!(config, parsed_config)
}

0 comments on commit 86d1f00

Please sign in to comment.