use std::sync::Arc;
use tenzro_storage::{
account_store::AccountStoreImpl,
block_store::BlockStoreImpl,
kv::MemoryStore,
merkle::MerklePatriciaTrie,
traits::{AccountStore, BlockStore},
};
use tenzro_types::{
Account, Address, Block, BlockHeader, BlockHeight, ConsensusProof, Hash,
Nonce,
};
use tenzro_types::block::ConsensusAlgorithm;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
println!("Tenzro Storage - Basic Example\n");
let kv_store = Arc::new(MemoryStore::new());
println!("=== Block Storage ===");
demo_block_storage(kv_store.clone()).await?;
println!("\n=== Account Storage ===");
demo_account_storage(kv_store.clone()).await?;
println!("\n=== Merkle Patricia Trie ===");
demo_merkle_trie()?;
Ok(())
}
async fn demo_block_storage(kv_store: Arc<MemoryStore>) -> Result<(), Box<dyn std::error::Error>> {
let mut block_store = BlockStoreImpl::new(kv_store)?;
let header = BlockHeader::new(
BlockHeight::new(1),
Hash::zero(),
Hash::zero(),
Hash::zero(),
Address::zero(),
ConsensusProof::new(ConsensusAlgorithm::PoS, vec![]),
);
let block = Block::new(header, vec![]);
println!("Storing block at height {}...", block.height());
block_store.put_block(&block).await?;
let retrieved = block_store.get_block(&block.hash()).await?;
println!("Retrieved block: {:?}", retrieved.is_some());
let latest = block_store.latest_block().await?;
println!("Latest block height: {:?}", latest.map(|b| b.height()));
Ok(())
}
async fn demo_account_storage(
kv_store: Arc<MemoryStore>,
) -> Result<(), Box<dyn std::error::Error>> {
let mut account_store = AccountStoreImpl::new(kv_store);
let address = Address::new([1u8; 32]);
let mut account = Account::new(address);
account.balance = 1000;
account.nonce = Nonce::new(0);
println!("Storing account with address {}...", address);
account_store.put_account(&account).await?;
let retrieved = account_store.get_account(&address).await?;
println!("Retrieved account balance: {}", retrieved.unwrap().balance);
let balance = account_store.get_balance(&address).await?;
println!("Account balance: {}", balance);
account_store.update_nonce(&address, Nonce::new(1)).await?;
let nonce = account_store.get_nonce(&address).await?;
println!("Updated nonce: {}", nonce);
Ok(())
}
fn demo_merkle_trie() -> Result<(), Box<dyn std::error::Error>> {
let mut trie = MerklePatriciaTrie::new();
println!("Inserting key-value pairs...");
trie.insert(b"account1", b"balance:1000")?;
trie.insert(b"account2", b"balance:2000")?;
trie.insert(b"account3", b"balance:3000")?;
let state_root = trie.commit()?;
println!("State root: {}", state_root);
let value = trie.get(b"account1")?;
println!(
"Value for 'account1': {}",
String::from_utf8_lossy(value.as_ref().unwrap())
);
let proof = trie.generate_proof(b"account1")?;
let is_valid =
MerklePatriciaTrie::verify_proof(&proof, state_root, Some(b"balance:1000"))?;
println!("Proof verification: {}", is_valid);
println!("Deleting 'account2'...");
trie.delete(b"account2")?;
let new_root = trie.commit()?;
println!("New state root: {}", new_root);
println!("State root changed: {}", state_root != new_root);
Ok(())
}