407 lines
13 KiB
Rust
Raw Normal View History

use std::fmt::Display;
2024-11-25 07:26:16 +02:00
use anyhow::Result;
2025-04-16 16:17:53 +03:00
use common::{
block::{Block, HashableBlockData},
merkle_tree_public::TreeHashType,
nullifier::UTXONullifier,
transaction::{TransactionBody, TxKind},
utxo_commitment::UTXOCommitment,
};
2025-04-16 16:17:53 +03:00
use config::SequencerConfig;
use mempool::MemPool;
use sequencer_store::{accounts_store::AccountPublicData, SequecerChainStore};
use serde::{Deserialize, Serialize};
2024-11-25 07:26:16 +02:00
use transaction_mempool::TransactionMempool;
pub mod config;
2025-04-16 16:17:53 +03:00
pub mod sequencer_store;
2024-10-10 14:09:31 +03:00
pub mod transaction_mempool;
2024-11-25 07:26:16 +02:00
pub struct SequencerCore {
pub store: SequecerChainStore,
pub mempool: MemPool<TransactionMempool>,
pub sequencer_config: SequencerConfig,
pub chain_height: u64,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum TransactionMalformationErrorKind {
PublicTransactionChangedPrivateData { tx: TreeHashType },
PrivateTransactionChangedPublicData { tx: TreeHashType },
TxHashAlreadyPresentInTree { tx: TreeHashType },
NullifierAlreadyPresentInTree { tx: TreeHashType },
UTXOCommitmentAlreadyPresentInTree { tx: TreeHashType },
2025-01-24 09:10:42 +02:00
MempoolFullForRound { tx: TreeHashType },
ChainStateFurtherThanTransactionState { tx: TreeHashType },
FailedToInsert { tx: TreeHashType, details: String },
}
impl Display for TransactionMalformationErrorKind {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{self:#?}")
}
}
impl std::error::Error for TransactionMalformationErrorKind {}
2024-11-25 07:26:16 +02:00
impl SequencerCore {
pub fn start_from_config(config: SequencerConfig) -> Self {
Self {
store: SequecerChainStore::new_with_genesis(
&config.home,
config.genesis_id,
config.is_genesis_random,
),
mempool: MemPool::<TransactionMempool>::default(),
chain_height: config.genesis_id,
sequencer_config: config,
}
}
2025-05-23 09:04:04 +03:00
pub fn get_tree_roots(&self) -> [[u8; 32]; 2] {
2025-01-24 09:10:42 +02:00
[
2025-01-31 10:02:09 +02:00
self.store
.utxo_commitments_store
.get_root()
.unwrap_or([0; 32]),
2025-01-31 09:54:19 +02:00
self.store.pub_tx_store.get_root().unwrap_or([0; 32]),
2025-01-24 09:10:42 +02:00
]
}
pub fn transaction_pre_check(
&mut self,
tx: &TransactionBody,
2025-05-23 09:04:04 +03:00
tx_roots: [[u8; 32]; 2],
) -> Result<(), TransactionMalformationErrorKind> {
let TransactionBody {
tx_kind,
ref execution_input,
ref execution_output,
ref utxo_commitments_created_hashes,
ref nullifier_created_hashes,
..
} = tx;
let tx_hash = tx.hash();
2025-01-24 09:10:42 +02:00
let mempool_size = self.mempool.len();
if mempool_size >= self.sequencer_config.max_num_tx_in_block {
return Err(TransactionMalformationErrorKind::MempoolFullForRound { tx: tx_hash });
2025-01-24 09:10:42 +02:00
}
let curr_sequencer_roots = self.get_tree_roots();
if tx_roots != curr_sequencer_roots {
return Err(
TransactionMalformationErrorKind::ChainStateFurtherThanTransactionState {
tx: tx_hash,
2025-01-24 09:10:42 +02:00
},
);
}
//Sanity check
match tx_kind {
TxKind::Public => {
if !utxo_commitments_created_hashes.is_empty()
|| !nullifier_created_hashes.is_empty()
{
//Public transactions can not make private operations.
return Err(
TransactionMalformationErrorKind::PublicTransactionChangedPrivateData {
tx: tx_hash,
},
);
}
}
TxKind::Private => {
if !execution_input.is_empty() || !execution_output.is_empty() {
//Not entirely necessary, but useful simplification for a future.
//This way only shielded and deshielded transactions can be used for interaction
//between public and private state.
return Err(
TransactionMalformationErrorKind::PrivateTransactionChangedPublicData {
tx: tx_hash,
},
);
}
}
_ => {}
};
//Tree checks
let tx_tree_check = self.store.pub_tx_store.get_tx(tx_hash).is_some();
let nullifier_tree_check = nullifier_created_hashes
.iter()
.map(|nullifier_hash| {
2025-05-23 09:07:53 +03:00
self.store.nullifier_store.contains(&UTXONullifier {
utxo_hash: *nullifier_hash,
})
})
.any(|check| check);
let utxo_commitments_check = utxo_commitments_created_hashes
.iter()
.map(|utxo_commitment_hash| {
self.store
.utxo_commitments_store
.get_tx(*utxo_commitment_hash)
.is_some()
})
.any(|check| check);
if tx_tree_check {
2025-07-10 12:16:05 -03:00
return Err(
TransactionMalformationErrorKind::TxHashAlreadyPresentInTree { tx: tx.hash() },
);
}
if nullifier_tree_check {
return Err(
TransactionMalformationErrorKind::NullifierAlreadyPresentInTree { tx: tx.hash() },
);
}
if utxo_commitments_check {
return Err(
2025-07-10 12:16:05 -03:00
TransactionMalformationErrorKind::UTXOCommitmentAlreadyPresentInTree {
tx: tx.hash(),
},
);
}
Ok(())
}
pub fn push_tx_into_mempool_pre_check(
&mut self,
item: TransactionMempool,
2025-05-23 09:04:04 +03:00
tx_roots: [[u8; 32]; 2],
) -> Result<(), TransactionMalformationErrorKind> {
2025-01-24 09:10:42 +02:00
self.transaction_pre_check(&item.tx, tx_roots)?;
self.mempool.push_item(item);
Ok(())
}
fn execute_check_transaction_on_state(
&mut self,
tx: TransactionMempool,
) -> Result<(), TransactionMalformationErrorKind> {
let TransactionBody {
ref utxo_commitments_created_hashes,
ref nullifier_created_hashes,
..
} = tx.tx;
for utxo_comm in utxo_commitments_created_hashes {
self.store
.utxo_commitments_store
.add_tx(UTXOCommitment { hash: *utxo_comm });
}
2025-05-22 00:13:17 +03:00
for nullifier in nullifier_created_hashes.iter() {
2025-05-23 09:07:53 +03:00
self.store.nullifier_store.insert(UTXONullifier {
utxo_hash: *nullifier,
});
}
self.store.pub_tx_store.add_tx(tx.tx);
Ok(())
}
pub fn register_account(&mut self, acc_data: AccountPublicData) {
self.store
.acc_store
.accounts
.insert(acc_data.address, acc_data);
}
///Produces new block from transactions in mempool
pub fn produce_new_block_with_mempool_transactions(&mut self) -> Result<u64> {
let new_block_height = self.chain_height + 1;
2024-11-25 07:26:16 +02:00
let transactions = self
.mempool
.pop_size(self.sequencer_config.max_num_tx_in_block);
2025-05-23 09:04:04 +03:00
for tx in &transactions {
self.execute_check_transaction_on_state(tx.clone())?;
}
let prev_block_hash = self
.store
.block_store
.get_block_at_id(self.chain_height)?
.hash;
2024-11-25 07:26:16 +02:00
let hashable_data = HashableBlockData {
block_id: new_block_height,
prev_block_id: self.chain_height,
2024-11-25 07:26:16 +02:00
transactions: transactions.into_iter().map(|tx_mem| tx_mem.tx).collect(),
data: vec![],
prev_block_hash,
2024-11-25 07:26:16 +02:00
};
let block = Block::produce_block_from_hashable_data(hashable_data);
self.store.block_store.put_block_at_id(block)?;
self.chain_height += 1;
Ok(self.chain_height - 1)
2024-11-25 07:26:16 +02:00
}
}
2025-01-27 13:42:11 +01:00
#[cfg(test)]
mod tests {
use super::*;
2025-04-18 08:15:29 -04:00
use std::path::PathBuf;
2025-01-27 13:42:11 +01:00
use common::transaction::{TransactionBody, TxKind};
2025-01-27 13:42:11 +01:00
use rand::Rng;
2025-04-09 02:20:27 -04:00
use secp256k1_zkp::Tweak;
2025-01-27 13:42:11 +01:00
use transaction_mempool::TransactionMempool;
fn setup_sequencer_config() -> SequencerConfig {
let mut rng = rand::thread_rng();
let random_u8: u8 = rng.gen();
let path_str = format!("/tmp/sequencer_{:?}", random_u8);
SequencerConfig {
home: PathBuf::from(path_str),
override_rust_log: Some("info".to_string()),
genesis_id: 1,
is_genesis_random: false,
max_num_tx_in_block: 10,
block_create_timeout_millis: 1000,
port: 8080,
}
}
2025-01-27 13:42:27 +01:00
fn create_dummy_transaction(
nullifier_created_hashes: Vec<[u8; 32]>,
utxo_commitments_spent_hashes: Vec<[u8; 32]>,
utxo_commitments_created_hashes: Vec<[u8; 32]>,
) -> TransactionBody {
2025-04-09 02:03:01 -04:00
let mut rng = rand::thread_rng();
TransactionBody {
2025-01-27 13:42:27 +01:00
tx_kind: TxKind::Private,
execution_input: vec![],
execution_output: vec![],
utxo_commitments_spent_hashes,
utxo_commitments_created_hashes,
nullifier_created_hashes,
execution_proof_private: "dummy_proof".to_string(),
encoded_data: vec![],
ephemeral_pub_key: vec![10, 11, 12],
2025-04-09 01:29:35 -04:00
commitment: vec![],
tweak: Tweak::new(&mut rng),
secret_r: [0; 32],
2025-04-24 15:51:34 +03:00
sc_addr: "sc_addr".to_string(),
state_changes: (serde_json::Value::Null, 0),
2025-01-27 13:42:27 +01:00
}
}
fn common_setup(sequencer: &mut SequencerCore) {
let tx = create_dummy_transaction(vec![[9; 32]], vec![[7; 32]], vec![[8; 32]]);
2025-01-27 13:42:43 +01:00
let tx_mempool = TransactionMempool { tx };
sequencer.mempool.push_item(tx_mempool);
sequencer.produce_new_block_with_mempool_transactions();
}
2025-01-27 13:42:11 +01:00
2025-01-27 13:43:17 +01:00
#[test]
fn test_start_from_config() {
let config = setup_sequencer_config();
let sequencer = SequencerCore::start_from_config(config.clone());
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);
}
2025-01-27 13:43:45 +01:00
#[test]
fn test_get_tree_roots() {
let config = setup_sequencer_config();
let mut sequencer = SequencerCore::start_from_config(config);
common_setup(&mut sequencer);
let roots = sequencer.get_tree_roots();
2025-05-23 09:10:34 +03:00
assert_eq!(roots.len(), 2); // Should return two roots
2025-01-27 13:43:45 +01:00
}
2025-01-27 13:44:07 +01:00
#[test]
fn test_transaction_pre_check_pass() {
let config = setup_sequencer_config();
let mut sequencer = SequencerCore::start_from_config(config);
common_setup(&mut sequencer);
let tx = create_dummy_transaction(vec![[91; 32]], vec![[71; 32]], vec![[81; 32]]);
2025-01-27 13:44:07 +01:00
let tx_roots = sequencer.get_tree_roots();
let result = sequencer.transaction_pre_check(&tx, tx_roots);
assert!(result.is_ok());
}
#[test]
fn test_transaction_pre_check_fail_mempool_full() {
let config = SequencerConfig {
max_num_tx_in_block: 1,
..setup_sequencer_config()
};
let mut sequencer = SequencerCore::start_from_config(config);
common_setup(&mut sequencer);
let tx = create_dummy_transaction(vec![[92; 32]], vec![[72; 32]], vec![[82; 32]]);
let tx_roots = sequencer.get_tree_roots();
// Fill the mempool
let dummy_tx = TransactionMempool { tx: tx.clone() };
sequencer.mempool.push_item(dummy_tx);
let result = sequencer.transaction_pre_check(&tx, tx_roots);
assert!(matches!(
result,
Err(TransactionMalformationErrorKind::MempoolFullForRound { .. })
));
}
#[test]
fn test_push_tx_into_mempool_pre_check() {
let config = setup_sequencer_config();
let mut sequencer = SequencerCore::start_from_config(config);
common_setup(&mut sequencer);
let tx = create_dummy_transaction(vec![[93; 32]], vec![[73; 32]], vec![[83; 32]]);
let tx_roots = sequencer.get_tree_roots();
let tx_mempool = TransactionMempool { tx };
let result = sequencer.push_tx_into_mempool_pre_check(tx_mempool.clone(), tx_roots);
assert!(result.is_ok());
assert_eq!(sequencer.mempool.len(), 1);
}
2025-01-27 13:43:17 +01:00
#[test]
fn test_produce_new_block_with_mempool_transactions() {
let config = setup_sequencer_config();
let mut sequencer = SequencerCore::start_from_config(config);
let tx = create_dummy_transaction(vec![[94; 32]], vec![[7; 32]], vec![[8; 32]]);
let tx_mempool = TransactionMempool { tx };
sequencer.mempool.push_item(tx_mempool);
let block_id = sequencer.produce_new_block_with_mempool_transactions();
assert!(block_id.is_ok());
assert_eq!(block_id.unwrap(), 1);
}
2025-01-27 13:51:27 +01:00
}