mirror of
https://github.com/logos-blockchain/lssa.git
synced 2026-01-22 07:03:07 +00:00
feat: initial accounts configuration
This commit is contained in:
parent
5e9c6a0677
commit
a43a32fc55
1
Cargo.lock
generated
1
Cargo.lock
generated
@ -4336,6 +4336,7 @@ dependencies = [
|
||||
"common",
|
||||
"elliptic-curve",
|
||||
"env_logger",
|
||||
"hex",
|
||||
"k256",
|
||||
"log",
|
||||
"mempool",
|
||||
|
||||
@ -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
|
||||
|
||||
@ -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<AccountInitialData>,
|
||||
}
|
||||
|
||||
@ -53,6 +53,7 @@ impl SequencerCore {
|
||||
&config.home,
|
||||
config.genesis_id,
|
||||
config.is_genesis_random,
|
||||
&config.initial_accounts,
|
||||
),
|
||||
mempool: MemPool::<TransactionMempool>::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]
|
||||
|
||||
@ -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![]);
|
||||
|
||||
@ -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
|
||||
}
|
||||
]
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user