- nu-vm/engine.rs: execute_block runs all txs in a block against StateDb; TokenTransfer fully applied, other variants return "not implemented" receipt - StateAccessor trait: set_balance/inc_nonce now take &self (RocksDB interior mutability) - src/block_loop.rs: tokio task that produces a block each slot (6s) in dev mode; drains mempool, executes txs, removes successful ones, persists block to RocksDB - StateDb wrapped in Arc<Mutex> — block loop holds write lock per block, RPC holds read lock for nu_getAccount - main.rs: spawns block_loop when --dev --validator flags are set Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
53 lines
1.7 KiB
Rust
53 lines
1.7 KiB
Rust
use crate::{account::AccountState, db::StateDb};
|
|
|
|
/// Read/write access to account state.
|
|
/// `set_balance` and `inc_nonce` take `&self` because StateDb uses RocksDB's
|
|
/// interior mutability — writes do not require exclusive access at the Rust level.
|
|
pub trait StateAccessor {
|
|
fn get_balance(&self, address: &str) -> u64;
|
|
fn get_nonce(&self, address: &str) -> u64;
|
|
fn set_balance(&self, address: &str, balance: u64);
|
|
fn inc_nonce(&self, address: &str);
|
|
}
|
|
|
|
impl StateAccessor for StateDb {
|
|
fn get_balance(&self, address: &str) -> u64 {
|
|
let key = format!("account:{address}");
|
|
self.get::<AccountState>(&key)
|
|
.ok()
|
|
.flatten()
|
|
.map(|a| a.balance)
|
|
.unwrap_or(0)
|
|
}
|
|
|
|
fn get_nonce(&self, address: &str) -> u64 {
|
|
let key = format!("account:{address}");
|
|
self.get::<AccountState>(&key)
|
|
.ok()
|
|
.flatten()
|
|
.map(|a| a.nonce)
|
|
.unwrap_or(0)
|
|
}
|
|
|
|
fn set_balance(&self, address: &str, balance: u64) {
|
|
let key = format!("account:{address}");
|
|
let mut account = self
|
|
.get::<AccountState>(&key)
|
|
.ok()
|
|
.flatten()
|
|
.unwrap_or_else(|| AccountState::new(address.to_string()));
|
|
account.balance = balance;
|
|
let _ = self.put(&key, &account);
|
|
}
|
|
|
|
fn inc_nonce(&self, address: &str) {
|
|
let key = format!("account:{address}");
|
|
let mut account = self
|
|
.get::<AccountState>(&key)
|
|
.ok()
|
|
.flatten()
|
|
.unwrap_or_else(|| AccountState::new(address.to_string()));
|
|
account.nonce += 1;
|
|
let _ = self.put(&key, &account);
|
|
}
|
|
}
|