diff --git a/Cargo.lock b/Cargo.lock index 0200d46..8c4a396 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4336,6 +4336,7 @@ dependencies = [ "common", "elliptic-curve", "env_logger", + "hex", "k256", "log", "mempool", diff --git a/sequencer_core/Cargo.toml b/sequencer_core/Cargo.toml index 06df7c5..14c047c 100644 --- a/sequencer_core/Cargo.toml +++ b/sequencer_core/Cargo.toml @@ -4,6 +4,7 @@ version = "0.1.0" edition = "2021" [dependencies] +hex.workspace = true anyhow.workspace = true serde_json.workspace = true env_logger.workspace = true diff --git a/sequencer_core/src/config.rs b/sequencer_core/src/config.rs index 53b18a9..707b8b9 100644 --- a/sequencer_core/src/config.rs +++ b/sequencer_core/src/config.rs @@ -1,6 +1,12 @@ +use serde::{Deserialize, Serialize}; use std::path::PathBuf; -use serde::{Deserialize, Serialize}; +#[derive(Debug, Serialize, Deserialize, Clone)] +pub struct AccountInitialData { + ///Hex encoded AccountAddress + pub addr: String, + pub balance: u64, +} #[derive(Debug, Clone, Serialize, Deserialize)] pub struct SequencerConfig { @@ -18,4 +24,6 @@ pub struct SequencerConfig { pub block_create_timeout_millis: u64, ///Port to listen pub port: u16, + ///List of pairs (account_address, initial_balance) + pub initial_accounts: Vec, } diff --git a/sequencer_core/src/lib.rs b/sequencer_core/src/lib.rs index 1ed603b..2ebff42 100644 --- a/sequencer_core/src/lib.rs +++ b/sequencer_core/src/lib.rs @@ -53,6 +53,7 @@ impl SequencerCore { &config.home, config.genesis_id, config.is_genesis_random, + &config.initial_accounts, ), mempool: MemPool::::default(), chain_height: config.genesis_id, @@ -252,6 +253,8 @@ impl SequencerCore { #[cfg(test)] mod tests { + use crate::config::AccountInitialData; + use super::*; use std::path::PathBuf; @@ -274,6 +277,18 @@ mod tests { max_num_tx_in_block: 10, block_create_timeout_millis: 1000, port: 8080, + initial_accounts: vec![ + AccountInitialData { + addr: "bfd91e6703273a115ad7f099ef32f621243be69369d00ddef5d3a25117d09a8c" + .to_string(), + balance: 10, + }, + AccountInitialData { + addr: "20573479053979b98d2ad09ef31a0750f22c77709bed51c4e64946bd1e376f31" + .to_string(), + balance: 100, + }, + ], } } @@ -322,6 +337,39 @@ mod tests { assert_eq!(sequencer.chain_height, config.genesis_id); assert_eq!(sequencer.sequencer_config.max_num_tx_in_block, 10); assert_eq!(sequencer.sequencer_config.port, 8080); + + let acc1_addr: [u8; 32] = hex::decode( + "bfd91e6703273a115ad7f099ef32f621243be69369d00ddef5d3a25117d09a8c".to_string(), + ) + .unwrap() + .try_into() + .unwrap(); + let acc2_addr: [u8; 32] = hex::decode( + "20573479053979b98d2ad09ef31a0750f22c77709bed51c4e64946bd1e376f31".to_string(), + ) + .unwrap() + .try_into() + .unwrap(); + + assert!(sequencer.store.acc_store.contains_account(&acc1_addr)); + assert!(sequencer.store.acc_store.contains_account(&acc2_addr)); + + assert_eq!( + 10, + sequencer + .store + .acc_store + .get_account_balance(&acc1_addr) + .unwrap() + ); + assert_eq!( + 100, + sequencer + .store + .acc_store + .get_account_balance(&acc2_addr) + .unwrap() + ); } #[test] diff --git a/sequencer_core/src/sequencer_store/mod.rs b/sequencer_core/src/sequencer_store/mod.rs index 322c99d..daffc91 100644 --- a/sequencer_core/src/sequencer_store/mod.rs +++ b/sequencer_core/src/sequencer_store/mod.rs @@ -9,6 +9,8 @@ use common::{ }; use rand::{rngs::OsRng, RngCore}; +use crate::config::AccountInitialData; + pub mod accounts_store; pub mod block_store; @@ -21,8 +23,28 @@ pub struct SequecerChainStore { } impl SequecerChainStore { - pub fn new_with_genesis(home_dir: &Path, genesis_id: u64, is_genesis_random: bool) -> Self { - let acc_store = SequencerAccountsStore::default(); + pub fn new_with_genesis( + home_dir: &Path, + genesis_id: u64, + is_genesis_random: bool, + initial_accounts: &[AccountInitialData], + ) -> Self { + let acc_data_decoded: Vec<([u8; 32], u64)> = initial_accounts + .iter() + .map(|acc_data| { + ( + //ToDo: Handle this error for direct error message + //Failure to produce account address is critical, so error handling is needed only for clarity + hex::decode(acc_data.addr.clone()) + .unwrap() + .try_into() + .unwrap(), + acc_data.balance, + ) + }) + .collect(); + + let acc_store = SequencerAccountsStore::new(&acc_data_decoded); let nullifier_store = HashSet::new(); let utxo_commitments_store = UTXOCommitmentsMerkleTree::new(vec![]); let pub_tx_store = PublicTransactionMerkleTree::new(vec![]); diff --git a/sequencer_runner/configs/debug/sequencer_config.json b/sequencer_runner/configs/debug/sequencer_config.json index 5eccadd..18d1b72 100644 --- a/sequencer_runner/configs/debug/sequencer_config.json +++ b/sequencer_runner/configs/debug/sequencer_config.json @@ -5,5 +5,15 @@ "is_genesis_random": true, "max_num_tx_in_block": 20, "block_create_timeout_millis": 10000, - "port": 3040 + "port": 3040, + "initial_accounts": [ + { + "addr": "bfd91e6703273a115ad7f099ef32f621243be69369d00ddef5d3a25117d09a8c", + "balance": 10 + }, + { + "addr": "20573479053979b98d2ad09ef31a0750f22c77709bed51c4e64946bd1e376f31", + "balance": 100 + } + ] } \ No newline at end of file