fix: suggestions fix

This commit is contained in:
Pravdyvy 2026-03-17 15:10:12 +02:00
parent 44a11bd3f1
commit 8a8c7f722b
4 changed files with 48 additions and 58 deletions

2
Cargo.lock generated
View File

@ -126,7 +126,6 @@ version = "2.11.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "92589714878ca59a7626ea19734f0e07a6a875197eec751bb5d3f99e64998c63"
dependencies = [
"actix-macros",
"futures-core",
"tokio",
]
@ -3743,7 +3742,6 @@ checksum = "4ee796ad498c8d9a1d68e477df8f754ed784ef875de1414ebdaf169f70a6a784"
name = "indexer_core"
version = "0.1.0"
dependencies = [
"actix-rt",
"anyhow",
"async-stream",
"bedrock_client",

View File

@ -22,5 +22,7 @@ url.workspace = true
logos-blockchain-core.workspace = true
serde_json.workspace = true
async-stream.workspace = true
[dev-dependencies]
tempfile.workspace = true
actix-rt.workspace = true

View File

@ -13,7 +13,7 @@ use tokio::sync::RwLock;
#[derive(Clone)]
pub struct IndexerStore {
dbio: Arc<RocksDBIO>,
final_state: Arc<RwLock<V02State>>,
current_state: Arc<RwLock<V02State>>,
}
impl IndexerStore {
@ -26,11 +26,11 @@ impl IndexerStore {
start_data: Option<(Block, V02State)>,
) -> Result<Self> {
let dbio = RocksDBIO::open_or_create(location, start_data)?;
let final_state = dbio.final_state()?;
let current_state = dbio.final_state()?;
Ok(Self {
dbio: Arc::new(dbio),
final_state: Arc::new(RwLock::new(final_state)),
current_state: Arc::new(RwLock::new(current_state)),
})
}
@ -103,18 +103,17 @@ impl IndexerStore {
Ok(self.dbio.final_state()?)
}
pub async fn get_account_final(&self, account_id: &AccountId) -> Result<Account> {
let account = {
let state_guard = self.final_state.read().await;
state_guard.get_account_by_id(*account_id)
};
Ok(account)
pub async fn account_current_state(&self, account_id: &AccountId) -> Result<Account> {
Ok(self
.current_state
.read()
.await
.get_account_by_id(*account_id))
}
pub async fn put_block(&mut self, mut block: Block, l1_header: HeaderId) -> Result<()> {
{
let mut state_guard = self.final_state.write().await;
let mut state_guard = self.current_state.write().await;
for transaction in &block.body.transactions {
transaction
@ -135,7 +134,7 @@ impl IndexerStore {
#[cfg(test)]
mod tests {
use nssa::AccountId;
use nssa::{AccountId, PublicKey};
use tempfile::tempdir;
use super::*;
@ -144,20 +143,6 @@ mod tests {
common::test_utils::produce_dummy_block(1, None, vec![])
}
fn acc1() -> AccountId {
AccountId::new([
148, 179, 206, 253, 199, 51, 82, 86, 232, 2, 152, 122, 80, 243, 54, 207, 237, 112, 83,
153, 44, 59, 204, 49, 128, 84, 160, 227, 216, 149, 97, 102,
])
}
fn acc2() -> AccountId {
AccountId::new([
30, 145, 107, 3, 207, 73, 192, 230, 160, 63, 238, 207, 18, 69, 54, 216, 103, 244, 92,
94, 124, 248, 42, 16, 141, 19, 119, 18, 14, 226, 140, 204,
])
}
fn acc1_sign_key() -> nssa::PrivateKey {
nssa::PrivateKey::try_new([1; 32]).unwrap()
}
@ -166,35 +151,24 @@ mod tests {
nssa::PrivateKey::try_new([2; 32]).unwrap()
}
fn initial_state() -> V02State {
nssa::V02State::new_with_genesis_accounts(&[(acc1(), 10000), (acc2(), 20000)], &[])
fn acc1() -> AccountId {
AccountId::from(&PublicKey::new_from_private_key(&acc1_sign_key()))
}
fn transfer(amount: u128, nonce: u128, direction: bool) -> NSSATransaction {
let from;
let to;
let sign_key;
if direction {
from = acc1();
to = acc2();
sign_key = acc1_sign_key();
} else {
from = acc2();
to = acc1();
sign_key = acc2_sign_key();
}
common::test_utils::create_transaction_native_token_transfer(
from, nonce, to, amount, sign_key,
)
fn acc2() -> AccountId {
AccountId::from(&PublicKey::new_from_private_key(&acc2_sign_key()))
}
#[test]
fn test_correct_startup() {
let home = tempdir().unwrap();
let storage = IndexerStore::open_db_with_genesis(
tempdir().unwrap().as_ref(),
Some((genesis_block(), initial_state())),
home.as_ref(),
Some((
genesis_block(),
nssa::V02State::new_with_genesis_accounts(&[(acc1(), 10000), (acc2(), 20000)], &[]),
)),
)
.unwrap();
@ -205,18 +179,34 @@ mod tests {
assert_eq!(final_id, 1);
}
#[actix_rt::test]
#[tokio::test]
async fn test_state_transition() {
let home = tempdir().unwrap();
let mut storage = IndexerStore::open_db_with_genesis(
tempdir().unwrap().as_ref(),
Some((genesis_block(), initial_state())),
home.as_ref(),
Some((
genesis_block(),
nssa::V02State::new_with_genesis_accounts(&[(acc1(), 10000), (acc2(), 20000)], &[]),
)),
)
.unwrap();
let mut prev_hash = genesis_block().header.hash;
let from = acc1();
let to = acc2();
let sign_key = acc1_sign_key();
for i in 2..10 {
let tx = transfer(10, i - 2, true);
let tx = common::test_utils::create_transaction_native_token_transfer(
from,
i - 2,
to,
10,
sign_key.clone(),
);
let next_block =
common::test_utils::produce_dummy_block(i as u64, Some(prev_hash), vec![tx]);
prev_hash = next_block.header.hash;
@ -227,8 +217,8 @@ mod tests {
.unwrap();
}
let acc1_val = storage.get_account_final(&acc1()).await.unwrap();
let acc2_val = storage.get_account_final(&acc2()).await.unwrap();
let acc1_val = storage.account_current_state(&acc1()).await.unwrap();
let acc2_val = storage.account_current_state(&acc2()).await.unwrap();
assert_eq!(acc1_val.balance, 9920);
assert_eq!(acc2_val.balance, 20080);

View File

@ -74,7 +74,7 @@ impl indexer_service_rpc::RpcServer for IndexerService {
Ok(self
.indexer
.store
.get_account_final(&account_id.into())
.account_current_state(&account_id.into())
.await
.map_err(db_error)?
.into())