lssa/node_core/src/lib.rs

202 lines
5.9 KiB
Rust
Raw Normal View History

2025-08-04 15:09:28 +03:00
use std::sync::{atomic::AtomicU64, Arc};
2024-12-05 13:05:58 +02:00
2025-07-22 15:22:20 +03:00
use common::{
2025-07-24 16:05:27 +03:00
execution_input::PublicNativeTokenSend, transaction::Transaction, ExecutionFailureKind,
2025-07-22 15:22:20 +03:00
};
2024-12-25 09:50:54 +02:00
2025-08-04 15:09:28 +03:00
use accounts::account_core::{address::AccountAddress, Account};
2025-03-16 11:37:23 -04:00
use anyhow::Result;
2025-04-24 15:51:34 +03:00
use chain_storage::NodeChainStore;
2025-08-04 15:09:28 +03:00
use common::transaction::TransactionBody;
2024-12-03 09:32:35 +02:00
use config::NodeConfig;
2024-12-25 09:50:54 +02:00
use log::info;
2025-08-04 15:09:28 +03:00
use sc_core::proofs_circuits::{generate_commitments, pedersen_commitment_vec};
2024-12-22 16:14:52 +02:00
use sequencer_client::{json::SendTxResponse, SequencerClient};
use serde::{Deserialize, Serialize};
2025-04-24 15:51:34 +03:00
use storage::sc_db_utils::DataBlobChangeVariant;
2025-08-04 15:09:28 +03:00
use tokio::sync::RwLock;
2025-01-31 16:50:00 -05:00
use utxo::utxo_core::UTXO;
2025-08-04 15:09:28 +03:00
use zkvm::gas_calculator::GasCalculator;
2024-12-03 09:32:35 +02:00
2024-12-25 09:50:54 +02:00
pub const BLOCK_GEN_DELAY_SECS: u64 = 20;
2025-04-24 15:51:34 +03:00
pub mod chain_storage;
2024-12-03 09:32:35 +02:00
pub mod config;
pub mod sequencer_client;
2025-08-04 15:09:28 +03:00
pub fn vec_u8_to_vec_u64(bytes: Vec<u8>) -> Vec<u64> {
2025-04-09 01:38:02 -04:00
// Pad with zeros to make sure it's a multiple of 8
let mut padded = bytes.clone();
2025-07-18 14:07:06 +03:00
while !padded.len().is_multiple_of(8) {
2025-04-09 01:38:02 -04:00
padded.push(0);
}
padded
.chunks(8)
.map(|chunk| {
let mut array = [0u8; 8];
array.copy_from_slice(chunk);
u64::from_le_bytes(array)
})
.collect()
}
2024-12-22 16:14:52 +02:00
#[derive(Debug, Serialize, Deserialize)]
pub struct MintMoneyPublicTx {
pub acc: AccountAddress,
pub amount: u128,
}
#[derive(Debug, Serialize, Deserialize)]
pub struct SendMoneyShieldedTx {
pub acc_sender: AccountAddress,
pub amount: u128,
}
#[derive(Debug, Serialize, Deserialize)]
pub struct SendMoneyDeshieldedTx {
pub receiver_data: Vec<(u128, AccountAddress)>,
}
2025-01-03 10:44:06 +02:00
#[derive(Debug, Serialize, Deserialize)]
pub struct UTXOPublication {
pub utxos: Vec<UTXO>,
}
2024-12-22 16:14:52 +02:00
#[derive(Debug, Serialize, Deserialize)]
pub enum ActionData {
MintMoneyPublicTx(MintMoneyPublicTx),
SendMoneyShieldedTx(SendMoneyShieldedTx),
SendMoneyDeshieldedTx(SendMoneyDeshieldedTx),
2025-01-03 10:44:06 +02:00
UTXOPublication(UTXOPublication),
2024-12-22 16:14:52 +02:00
}
2024-12-03 09:32:35 +02:00
pub struct NodeCore {
2024-12-25 09:50:54 +02:00
pub storage: Arc<RwLock<NodeChainStore>>,
2024-12-05 13:05:58 +02:00
pub curr_height: Arc<AtomicU64>,
2024-12-03 09:32:35 +02:00
pub node_config: NodeConfig,
2024-12-20 11:02:12 +02:00
pub sequencer_client: Arc<SequencerClient>,
2025-02-28 12:32:54 +02:00
pub gas_calculator: GasCalculator,
2024-12-05 13:05:58 +02:00
}
impl NodeCore {
pub async fn start_from_config_update_chain(config: NodeConfig) -> Result<Self> {
2024-12-20 11:02:12 +02:00
let client = Arc::new(SequencerClient::new(config.clone())?);
2024-12-05 13:05:58 +02:00
let genesis_id = client.get_genesis_id().await?;
2025-05-29 12:00:50 +03:00
info!("Genesis id is {genesis_id:?}");
2024-12-05 13:05:58 +02:00
let genesis_block = client.get_block(genesis_id.genesis_id).await?.block;
2025-08-04 15:09:28 +03:00
let (mut storage, chain_height) = NodeChainStore::new(config.clone(), genesis_block)?;
2025-07-30 14:01:40 +03:00
for acc in config.clone().initial_accounts {
2025-07-23 15:16:53 +03:00
storage.acc_map.insert(acc.address, acc);
}
2024-12-05 13:05:58 +02:00
2024-12-25 09:50:54 +02:00
let wrapped_storage = Arc::new(RwLock::new(storage));
2024-12-05 13:05:58 +02:00
let chain_height_wrapped = Arc::new(AtomicU64::new(chain_height));
Ok(Self {
storage: wrapped_storage,
curr_height: chain_height_wrapped,
node_config: config.clone(),
2024-12-20 11:02:12 +02:00
sequencer_client: client.clone(),
2025-02-28 12:32:54 +02:00
gas_calculator: GasCalculator::from(config.gas_config),
2024-12-05 13:05:58 +02:00
})
}
2024-12-22 16:14:52 +02:00
2025-05-23 09:04:04 +03:00
pub async fn get_roots(&self) -> [[u8; 32]; 2] {
2025-01-24 09:10:42 +02:00
let storage = self.storage.read().await;
[
2025-02-05 12:24:09 +02:00
storage.utxo_commitments_store.get_root().unwrap_or([0; 32]),
storage.pub_tx_store.get_root().unwrap_or([0; 32]),
2025-01-24 09:10:42 +02:00
]
}
2024-12-25 09:50:54 +02:00
pub async fn create_new_account(&mut self) -> AccountAddress {
let account = Account::new();
account.log();
2024-12-25 09:50:54 +02:00
let addr = account.address;
{
let mut write_guard = self.storage.write().await;
write_guard.acc_map.insert(account.address, account);
}
addr
}
pub async fn send_public_native_token_transfer(
&self,
from: AccountAddress,
2025-07-29 16:09:21 -03:00
nonce: u64,
to: AccountAddress,
2025-07-24 16:05:27 +03:00
balance_to_move: u64,
) -> Result<SendTxResponse, ExecutionFailureKind> {
let tx_roots = self.get_roots().await;
let public_context = {
let read_guard = self.storage.read().await;
read_guard.produce_context(from)
};
let (tweak, secret_r, commitment) = pedersen_commitment_vec(
//Will not panic, as public context is serializable
public_context.produce_u64_list_from_context().unwrap(),
);
let sc_addr = hex::encode([0; 32]);
2025-07-24 09:14:38 +03:00
//Native contract does not change its state
let state_changes: Vec<DataBlobChangeVariant> = vec![];
let new_len = 0;
let state_changes = (serde_json::to_value(state_changes).unwrap(), new_len);
2025-07-22 15:22:20 +03:00
let tx: TransactionBody =
sc_core::transaction_payloads_tools::create_public_transaction_payload(
serde_json::to_vec(&PublicNativeTokenSend {
from,
2025-07-29 16:09:21 -03:00
nonce,
to,
2025-07-24 16:05:27 +03:00
balance_to_move,
})
.unwrap(),
commitment,
tweak,
secret_r,
sc_addr,
state_changes,
2025-07-24 09:14:38 +03:00
);
tx.log();
2025-07-22 15:22:20 +03:00
{
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();
let signed_transaction = Transaction::new(tx, key_to_sign_transaction);
Ok(self
.sequencer_client
.send_tx(signed_transaction, tx_roots)
.await?)
} else {
Err(ExecutionFailureKind::AmountMismatchError)
}
}
}
2024-12-03 09:32:35 +02:00
}
2025-01-10 03:00:32 +02:00
pub fn generate_commitments_helper(input_utxos: &[UTXO]) -> Vec<[u8; 32]> {
generate_commitments(input_utxos)
.into_iter()
.map(|comm_raw| comm_raw.try_into().unwrap())
.collect()
}