mirror of
https://github.com/logos-blockchain/lssa.git
synced 2026-05-21 00:59:26 +00:00
fix: restore and dump methods
This commit is contained in:
parent
99640bd70c
commit
c0318de646
@ -1,5 +1,6 @@
|
|||||||
use std::{fs::File, io::BufReader, path::PathBuf, str::FromStr};
|
use std::{fs::File, io::BufReader, path::PathBuf, str::FromStr};
|
||||||
|
|
||||||
|
use accounts::account_core::Account;
|
||||||
use anyhow::{anyhow, Result};
|
use anyhow::{anyhow, Result};
|
||||||
|
|
||||||
use crate::{config::NodeConfig, HOME_DIR_ENV_VAR};
|
use crate::{config::NodeConfig, HOME_DIR_ENV_VAR};
|
||||||
@ -41,3 +42,24 @@ pub fn produce_account_addr_from_hex(hex_str: String) -> Result<[u8; 32]> {
|
|||||||
.try_into()
|
.try_into()
|
||||||
.map_err(|_| anyhow!("Failed conversion to 32 bytes"))
|
.map_err(|_| anyhow!("Failed conversion to 32 bytes"))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
///Fetch list of accounts stored at `NSSA_WALLET_HOME_DIR/curr_accounts.json`
|
||||||
|
///
|
||||||
|
/// If file not present, it is considered as empty list of persistent accounts
|
||||||
|
pub fn fetch_persistent_accounts() -> Result<Vec<Account>> {
|
||||||
|
let home = get_home()?;
|
||||||
|
let accs_path = home.join("curr_accounts.json");
|
||||||
|
|
||||||
|
match File::open(accs_path) {
|
||||||
|
Ok(file) => {
|
||||||
|
let reader = BufReader::new(file);
|
||||||
|
Ok(serde_json::from_reader(reader)?)
|
||||||
|
}
|
||||||
|
Err(err) => match err.kind() {
|
||||||
|
std::io::ErrorKind::NotFound => Ok(vec![]),
|
||||||
|
_ => {
|
||||||
|
anyhow::bail!("IO error {err:#?}");
|
||||||
|
}
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@ -1,4 +1,4 @@
|
|||||||
use std::sync::Arc;
|
use std::{fs::File, io::Write, path::PathBuf, sync::Arc};
|
||||||
|
|
||||||
use common::{
|
use common::{
|
||||||
execution_input::PublicNativeTokenSend,
|
execution_input::PublicNativeTokenSend,
|
||||||
@ -14,11 +14,12 @@ use common::transaction::TransactionBody;
|
|||||||
use config::NodeConfig;
|
use config::NodeConfig;
|
||||||
use log::info;
|
use log::info;
|
||||||
use sc_core::proofs_circuits::pedersen_commitment_vec;
|
use sc_core::proofs_circuits::pedersen_commitment_vec;
|
||||||
use tokio::sync::RwLock;
|
|
||||||
|
|
||||||
use clap::{Parser, Subcommand};
|
use clap::{Parser, Subcommand};
|
||||||
|
|
||||||
use crate::helperfunctions::{fetch_config, produce_account_addr_from_hex};
|
use crate::helperfunctions::{
|
||||||
|
fetch_config, fetch_persistent_accounts, get_home, produce_account_addr_from_hex,
|
||||||
|
};
|
||||||
|
|
||||||
pub const HOME_DIR_ENV_VAR: &str = "NSSA_WALLET_HOME_DIR";
|
pub const HOME_DIR_ENV_VAR: &str = "NSSA_WALLET_HOME_DIR";
|
||||||
pub const BLOCK_GEN_DELAY_SECS: u64 = 20;
|
pub const BLOCK_GEN_DELAY_SECS: u64 = 20;
|
||||||
@ -29,7 +30,7 @@ pub mod helperfunctions;
|
|||||||
pub mod requests_structs;
|
pub mod requests_structs;
|
||||||
|
|
||||||
pub struct NodeCore {
|
pub struct NodeCore {
|
||||||
pub storage: Arc<RwLock<NodeChainStore>>,
|
pub storage: NodeChainStore,
|
||||||
pub node_config: NodeConfig,
|
pub node_config: NodeConfig,
|
||||||
pub sequencer_client: Arc<SequencerClient>,
|
pub sequencer_client: Arc<SequencerClient>,
|
||||||
}
|
}
|
||||||
@ -43,20 +44,26 @@ impl NodeCore {
|
|||||||
storage.acc_map.insert(acc.address, acc);
|
storage.acc_map.insert(acc.address, acc);
|
||||||
}
|
}
|
||||||
|
|
||||||
let wrapped_storage = Arc::new(RwLock::new(storage));
|
//Persistent accounts take precedence for initial accounts
|
||||||
|
let persistent_accounts = fetch_persistent_accounts()?;
|
||||||
|
for acc in persistent_accounts {
|
||||||
|
storage.acc_map.insert(acc.address, acc);
|
||||||
|
}
|
||||||
|
|
||||||
Ok(Self {
|
Ok(Self {
|
||||||
storage: wrapped_storage,
|
storage,
|
||||||
node_config: config.clone(),
|
node_config: config.clone(),
|
||||||
sequencer_client: client.clone(),
|
sequencer_client: client.clone(),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn get_roots(&self) -> [[u8; 32]; 2] {
|
pub async fn get_roots(&self) -> [[u8; 32]; 2] {
|
||||||
let storage = self.storage.read().await;
|
|
||||||
[
|
[
|
||||||
storage.utxo_commitments_store.get_root().unwrap_or([0; 32]),
|
self.storage
|
||||||
storage.pub_tx_store.get_root().unwrap_or([0; 32]),
|
.utxo_commitments_store
|
||||||
|
.get_root()
|
||||||
|
.unwrap_or([0; 32]),
|
||||||
|
self.storage.pub_tx_store.get_root().unwrap_or([0; 32]),
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -66,11 +73,7 @@ impl NodeCore {
|
|||||||
|
|
||||||
let addr = account.address;
|
let addr = account.address;
|
||||||
|
|
||||||
{
|
self.storage.acc_map.insert(account.address, account);
|
||||||
let mut write_guard = self.storage.write().await;
|
|
||||||
|
|
||||||
write_guard.acc_map.insert(account.address, account);
|
|
||||||
}
|
|
||||||
|
|
||||||
addr
|
addr
|
||||||
}
|
}
|
||||||
@ -84,11 +87,7 @@ impl NodeCore {
|
|||||||
) -> Result<SendTxResponse, ExecutionFailureKind> {
|
) -> Result<SendTxResponse, ExecutionFailureKind> {
|
||||||
let tx_roots = self.get_roots().await;
|
let tx_roots = self.get_roots().await;
|
||||||
|
|
||||||
let public_context = {
|
let public_context = self.storage.produce_context(from);
|
||||||
let read_guard = self.storage.read().await;
|
|
||||||
|
|
||||||
read_guard.produce_context(from)
|
|
||||||
};
|
|
||||||
|
|
||||||
let (tweak, secret_r, commitment) = pedersen_commitment_vec(
|
let (tweak, secret_r, commitment) = pedersen_commitment_vec(
|
||||||
//Will not panic, as public context is serializable
|
//Will not panic, as public context is serializable
|
||||||
@ -113,31 +112,60 @@ impl NodeCore {
|
|||||||
);
|
);
|
||||||
tx.log();
|
tx.log();
|
||||||
|
|
||||||
{
|
let account = self.storage.acc_map.get(&from);
|
||||||
let read_guard = self.storage.read().await;
|
|
||||||
|
|
||||||
let account = read_guard.acc_map.get(&from);
|
if let Some(account) = account {
|
||||||
|
let key_to_sign_transaction = account.key_holder.get_pub_account_signing_key();
|
||||||
|
|
||||||
if let Some(account) = account {
|
let signed_transaction = Transaction::new(tx, key_to_sign_transaction);
|
||||||
let key_to_sign_transaction = account.key_holder.get_pub_account_signing_key();
|
|
||||||
|
|
||||||
let signed_transaction = Transaction::new(tx, key_to_sign_transaction);
|
Ok(self
|
||||||
|
.sequencer_client
|
||||||
Ok(self
|
.send_tx(signed_transaction, tx_roots)
|
||||||
.sequencer_client
|
.await?)
|
||||||
.send_tx(signed_transaction, tx_roots)
|
} else {
|
||||||
.await?)
|
Err(ExecutionFailureKind::AmountMismatchError)
|
||||||
} else {
|
|
||||||
Err(ExecutionFailureKind::AmountMismatchError)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
///Dumps all accounts from acc_map at `path`
|
||||||
|
///
|
||||||
|
///Currently storing everything in one file
|
||||||
|
///
|
||||||
|
///ToDo: extend storage
|
||||||
|
pub fn store_present_accounts_at_path(&self, path: PathBuf) -> Result<PathBuf> {
|
||||||
|
let dump_path = path.join("curr_accounts.json");
|
||||||
|
|
||||||
|
let curr_accs: Vec<Account> = self
|
||||||
|
.storage
|
||||||
|
.acc_map
|
||||||
|
.values()
|
||||||
|
.cloned()
|
||||||
|
.collect();
|
||||||
|
let accs_serialized = serde_json::to_vec_pretty(&curr_accs)?;
|
||||||
|
|
||||||
|
let mut acc_file = File::create(&dump_path).unwrap();
|
||||||
|
acc_file.write_all(&accs_serialized).unwrap();
|
||||||
|
|
||||||
|
Ok(dump_path)
|
||||||
|
}
|
||||||
|
|
||||||
|
///Dumps all accounts from acc_map at `NSSA_WALLET_HOME_DIR`
|
||||||
|
///
|
||||||
|
///Currently storing everything in one file
|
||||||
|
///
|
||||||
|
///ToDo: extend storage
|
||||||
|
pub fn store_present_accounts_at_home(&self) -> Result<PathBuf> {
|
||||||
|
let home = get_home()?;
|
||||||
|
self.store_present_accounts_at_path(home)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
///Represents CLI command for a wallet
|
///Represents CLI command for a wallet
|
||||||
#[derive(Subcommand, Debug, Clone)]
|
#[derive(Subcommand, Debug, Clone)]
|
||||||
#[clap(about)]
|
#[clap(about)]
|
||||||
pub enum Command {
|
pub enum Command {
|
||||||
|
///Send native token transfer from `from` to `to` for `amount`
|
||||||
SendNativeTokenTransfer {
|
SendNativeTokenTransfer {
|
||||||
///from - valid 32 byte hex string
|
///from - valid 32 byte hex string
|
||||||
#[arg(long)]
|
#[arg(long)]
|
||||||
@ -149,7 +177,12 @@ pub enum Command {
|
|||||||
#[arg(long)]
|
#[arg(long)]
|
||||||
amount: u64,
|
amount: u64,
|
||||||
},
|
},
|
||||||
DumpAccountsOnDisc,
|
///Dump accounts at destination
|
||||||
|
DumpAccountsOnDisc {
|
||||||
|
///Dump path for accounts
|
||||||
|
#[arg(short, long)]
|
||||||
|
dump_path: PathBuf,
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
///To execute commands, env var NSSA_WALLET_HOME_DIR must be set into directory with config
|
///To execute commands, env var NSSA_WALLET_HOME_DIR must be set into directory with config
|
||||||
@ -178,8 +211,14 @@ pub async fn execute_subcommand(command: Command) -> Result<()> {
|
|||||||
|
|
||||||
info!("Results of tx send is {res:#?}");
|
info!("Results of tx send is {res:#?}");
|
||||||
}
|
}
|
||||||
Command::DumpAccountsOnDisc => {
|
Command::DumpAccountsOnDisc { dump_path } => {
|
||||||
info!("Accounts stored at path");
|
let node_config = fetch_config()?;
|
||||||
|
|
||||||
|
let wallet_core = NodeCore::start_from_config_update_chain(node_config).await?;
|
||||||
|
|
||||||
|
wallet_core.store_present_accounts_at_path(dump_path.clone())?;
|
||||||
|
|
||||||
|
info!("Accounts stored at path {dump_path:#?}");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user