fix: cli added

This commit is contained in:
Oleksandr Pravdyvyi
2025-08-06 14:56:58 +03:00
parent 7be870369c
commit c5097ab879
10 changed files with 243 additions and 211 deletions
+1
View File
@@ -21,6 +21,7 @@ tempfile.workspace = true
risc0-zkvm = { git = "https://github.com/risc0/risc0.git", branch = "release-2.3" }
hex.workspace = true
actix-rt.workspace = true
clap.workspace = true
[dependencies.sc_core]
path = "../sc_core"
-27
View File
@@ -89,7 +89,6 @@ impl NodeChainStore {
#[cfg(test)]
mod tests {
use super::*;
use crate::config::GasConfig;
use accounts::account_core::Account;
use std::path::PathBuf;
use tempfile::tempdir;
@@ -268,42 +267,16 @@ mod tests {
initial_accounts
}
// fn create_genesis_block() -> Block {
// Block {
// block_id: 0,
// prev_block_id: 0,
// prev_block_hash: [0; 32],
// hash: [1; 32],
// transactions: vec![],
// data: Data::default(),
// }
// }
fn create_sample_node_config(home: PathBuf) -> NodeConfig {
NodeConfig {
home,
override_rust_log: None,
sequencer_addr: "http://127.0.0.1".to_string(),
seq_poll_timeout_secs: 1,
port: 8000,
gas_config: create_sample_gas_config(),
shapshot_frequency_in_blocks: 1,
initial_accounts: create_initial_accounts(),
}
}
fn create_sample_gas_config() -> GasConfig {
GasConfig {
gas_fee_per_byte_deploy: 0,
gas_fee_per_input_buffer_runtime: 0,
gas_fee_per_byte_runtime: 0,
gas_cost_runtime: 0,
gas_cost_deploy: 0,
gas_limit_deploy: 0,
gas_limit_runtime: 0,
}
}
#[test]
fn test_new_initializes_correctly() {
let temp_dir = tempdir().unwrap();
-18
View File
@@ -4,11 +4,6 @@ use accounts::account_core::Account;
use serde::{Deserialize, Serialize};
use zkvm::gas_calculator::GasCalculator;
use anyhow::Result;
use std::fs::File;
use std::io::BufReader;
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct GasConfig {
/// Gas spent per deploying one byte of data
@@ -51,19 +46,6 @@ pub struct NodeConfig {
pub sequencer_addr: String,
///Sequencer polling duration for new blocks in seconds
pub seq_poll_timeout_secs: u64,
///Port to listen
pub port: u16,
///Gas config
pub gas_config: GasConfig,
///Frequency of snapshots
pub shapshot_frequency_in_blocks: u64,
///Initial accounts for wallet
pub initial_accounts: Vec<Account>,
}
pub fn from_file(config_home: PathBuf) -> Result<NodeConfig> {
let file = File::open(config_home)?;
let reader = BufReader::new(file);
Ok(serde_json::from_reader(reader)?)
}
+70 -5
View File
@@ -1,22 +1,25 @@
use std::sync::Arc;
use std::{fs::File, io::BufReader, path::PathBuf, str::FromStr, sync::Arc};
use common::{
execution_input::PublicNativeTokenSend, transaction::Transaction, ExecutionFailureKind,
};
use accounts::account_core::{address::AccountAddress, Account};
use anyhow::Result;
use anyhow::{anyhow, Result};
use chain_storage::NodeChainStore;
use common::transaction::TransactionBody;
use config::NodeConfig;
use log::info;
use sc_core::proofs_circuits::{generate_commitments, pedersen_commitment_vec};
use sequencer_client::{json::SendTxResponse, SequencerClient};
use serde::{Deserialize, Serialize};
use storage::sc_db_utils::DataBlobChangeVariant;
use tokio::sync::RwLock;
use utxo::utxo_core::UTXO;
use zkvm::gas_calculator::GasCalculator;
use clap::{Parser, Subcommand};
pub const HOME_DIR_ENV_VAR: &str = "HOME_DIR";
pub const BLOCK_GEN_DELAY_SECS: u64 = 20;
pub mod chain_storage;
@@ -74,7 +77,6 @@ pub struct NodeCore {
pub storage: Arc<RwLock<NodeChainStore>>,
pub node_config: NodeConfig,
pub sequencer_client: Arc<SequencerClient>,
pub gas_calculator: GasCalculator,
}
impl NodeCore {
@@ -92,7 +94,6 @@ impl NodeCore {
storage: wrapped_storage,
node_config: config.clone(),
sequencer_client: client.clone(),
gas_calculator: GasCalculator::from(config.gas_config),
})
}
@@ -190,3 +191,67 @@ pub fn generate_commitments_helper(input_utxos: &[UTXO]) -> Vec<[u8; 32]> {
.map(|comm_raw| comm_raw.try_into().unwrap())
.collect()
}
///Represents CLI command for a wallet
#[derive(Subcommand, Debug, Clone)]
pub enum Command {
SendNativeTokenTransfer {
#[arg(long)]
from: String,
#[arg(long)]
to: String,
#[arg(long)]
amount: u64,
},
}
#[derive(Parser, Debug)]
#[clap(version)]
pub struct Args {
/// Wallet command
#[command(subcommand)]
pub command: Command,
}
pub fn get_home() -> Result<PathBuf> {
Ok(PathBuf::from_str(&std::env::var(HOME_DIR_ENV_VAR)?)?)
}
pub fn fetch_config() -> Result<NodeConfig> {
let config_home = get_home()?;
let file = File::open(config_home.join("node_config.json"))?;
let reader = BufReader::new(file);
Ok(serde_json::from_reader(reader)?)
}
//ToDo: Replace with structures in future
pub fn produce_account_addr_from_hex(hex_str: String) -> Result<[u8; 32]> {
hex::decode(hex_str)?
.try_into()
.map_err(|_| anyhow!("Failed conversion to 32 bytes"))
}
pub async fn execute_subcommand(command: Command) -> Result<()> {
//env_logger::init();
match command {
Command::SendNativeTokenTransfer { from, to, amount } => {
let node_config = fetch_config()?;
let from = produce_account_addr_from_hex(from)?;
let to = produce_account_addr_from_hex(to)?;
let wallet_core = NodeCore::start_from_config_update_chain(node_config).await?;
//ToDo: Nonce management
let res = wallet_core
.send_public_native_token_transfer(from, 0, to, amount)
.await?;
info!("Results of tx send is {res:#?}");
}
}
Ok(())
}
+22
View File
@@ -0,0 +1,22 @@
use anyhow::Result;
use clap::Parser;
use node_core::{execute_subcommand, Args};
use tokio::runtime::Builder;
pub const NUM_THREADS: usize = 2;
fn main() -> Result<()> {
let runtime = Builder::new_multi_thread()
.worker_threads(NUM_THREADS)
.enable_all()
.build()
.unwrap();
let args = Args::parse();
runtime.block_on(async move {
execute_subcommand(args.command).await.unwrap();
});
Ok(())
}