use std::ops::RangeInclusive;
use miden_node_tracing::miden_instrument;
use miden_node_utils::limiter::MAX_RESPONSE_PAYLOAD_BYTES;
use miden_protocol::account::AccountId;
use miden_protocol::block::{BlockHeader, BlockNumber, BlockSignatures};
use miden_protocol::crypto::merkle::mmr::{Forest, MmrDelta, MmrProof};
use super::StateView;
use crate::COMPONENT;
use crate::db::queries::{
AccountStorageMapValuesPaged,
AccountVaultAssets,
NoteSyncMulti,
NullifiersByPrefix,
StorageMapValuesPage,
TransactionsRecords,
};
use crate::db::{
AccountVaultValue,
NoteSyncUpdate,
NullifierInfo,
default_storage_map_entries_limit,
};
use crate::errors::{DatabaseError, NoteSyncError, StateSyncError};
impl StateView {
pub async fn sync_transactions(
&self,
account_ids: Vec<AccountId>,
block_range: RangeInclusive<BlockNumber>,
) -> Result<(BlockNumber, Vec<crate::db::TransactionRecord>), DatabaseError> {
let block_range = self.scope_range(block_range)?;
let query = TransactionsRecords::new(account_ids, block_range.scoped_end());
let page = self.db.page(query, block_range.start()).await?;
Ok((page.last_block_included(block_range.end()), page.items))
}
#[miden_instrument(
level = "debug",
target = COMPONENT,
err,
)]
pub async fn sync_chain_mmr(
&self,
block_range: RangeInclusive<BlockNumber>,
) -> Result<(MmrDelta, BlockHeader, BlockSignatures), StateSyncError> {
let block_range = self.scope_range(block_range)?;
let block_from = block_range.start();
let block_to = block_range.end();
let (block_header, signatures) = self
.db
.select_block_header_and_signatures_by_block_num(block_range.scoped_end())
.await?
.expect("the range-end header should exist in the database");
if block_from == block_to {
return Ok((
MmrDelta {
forest: Forest::new(block_from.as_usize()).expect("block index fits in u32"),
data: vec![],
},
block_header,
signatures,
));
}
let from_forest = (block_from + 1).as_usize();
let to_forest = block_to.as_usize();
let mmr_delta = self
.blockchain()
.as_mmr()
.get_delta(
Forest::new(from_forest).expect("from_forest fits in u32"),
Forest::new(to_forest).expect("to_forest fits in u32"),
)
.map_err(StateSyncError::FailedToBuildMmrDelta)?;
Ok((mmr_delta, block_header, signatures))
}
#[miden_instrument(
level = "debug",
target = COMPONENT,
err,
)]
pub async fn sync_notes(
&self,
note_tags: Vec<u32>,
block_range: RangeInclusive<BlockNumber>,
) -> Result<(Vec<(NoteSyncUpdate, MmrProof)>, BlockNumber), NoteSyncError> {
let block_range = self.scope_range(block_range)?;
let block_end = block_range.end();
let mmr_checkpoint = block_end + 1;
let query =
NoteSyncMulti::new(note_tags, block_range.scoped_end(), MAX_RESPONSE_PAYLOAD_BYTES);
let page = self.db.page(query, block_range.start()).await?;
let last_block_included = page.last_block_included(block_end);
let mut results = Vec::new();
for note_sync in page.items {
let mmr_proof =
self.blockchain().open_at(note_sync.block_header.block_num(), mmr_checkpoint)?;
results.push((note_sync, mmr_proof));
}
Ok((results, last_block_included))
}
pub async fn sync_nullifiers(
&self,
prefix_len: u32,
nullifier_prefixes: Vec<u32>,
block_range: RangeInclusive<BlockNumber>,
) -> Result<(Vec<NullifierInfo>, BlockNumber), DatabaseError> {
let block_range = self.scope_range(block_range)?;
assert_eq!(prefix_len, 16, "Only 16-bit prefixes are supported");
let nullifier_prefixes =
nullifier_prefixes.into_iter().map(|prefix| prefix as u16).collect::<Vec<_>>();
let query =
NullifiersByPrefix::new(prefix_len as u8, nullifier_prefixes, block_range.scoped_end());
let page = self.db.page(query, block_range.start()).await?;
let last_block_included = page.last_block_included(block_range.end());
Ok((page.items, last_block_included))
}
pub async fn sync_account_vault(
&self,
account_id: AccountId,
block_range: RangeInclusive<BlockNumber>,
) -> Result<(BlockNumber, Vec<AccountVaultValue>), DatabaseError> {
let block_range = self.scope_retained_range(block_range)?;
let query = AccountVaultAssets::new(account_id, block_range.scoped_end());
let page = self.db.page(query, block_range.start()).await?;
Ok((page.last_block_included(block_range.end()), page.items))
}
pub async fn sync_account_storage_maps(
&self,
account_id: AccountId,
block_range: RangeInclusive<BlockNumber>,
) -> Result<StorageMapValuesPage, DatabaseError> {
let block_range = self.scope_retained_range(block_range)?;
let query = AccountStorageMapValuesPaged::new(
account_id,
block_range.scoped_end(),
default_storage_map_entries_limit(),
);
let page = self.db.page(query, block_range.start()).await?;
Ok(StorageMapValuesPage {
last_block_included: page.last_block_included(block_range.end()),
values: page.items,
})
}
}
#[cfg(test)]
mod tests {
use miden_node_utils::fee::{test_fee_params, test_protocol_config};
use miden_protocol::block::{BlockBody, SignedBlock, ValidatorConfig};
use miden_protocol::crypto::merkle::mmr::Mmr;
use miden_protocol::testing::account_id::ACCOUNT_ID_PUBLIC_FUNGIBLE_FAUCET;
use miden_protocol::testing::random_secret_key::random_secret_key;
use miden_protocol::transaction::OrderedTransactionHeaders;
use super::*;
use crate::GenesisState;
use crate::db::HISTORICAL_BLOCK_RETENTION;
use crate::errors::RangeBelowRetention;
use crate::state::{BlockWriter, State};
async fn apply_empty_blocks(state: &State, writer: &mut BlockWriter, count: u32) {
let view = state.view();
let mut mmr = Mmr::new();
let mut parent = None;
for height in 0..=view.tip().as_u32() {
let (header, _) = view.get_block_header(Some(height.into()), false).await.unwrap();
let header = header.expect("block header should exist");
mmr.add(header.commitment()).unwrap();
parent = Some(header);
}
drop(view);
let mut parent = parent.expect("chain should have a parent block");
for _ in 0..count {
let body = BlockBody::new(
Vec::new(),
Vec::new(),
Vec::new(),
OrderedTransactionHeaders::new_unchecked(Vec::new()),
)
.unwrap();
let header = BlockHeader::new(
parent.commitment(),
parent.block_num().child(),
mmr.peaks().hash_peaks(),
parent.account_root(),
parent.nullifier_root(),
body.compute_block_note_tree().root(),
body.transaction_commitment(),
parent.validator_config().clone(),
parent.fee_parameters().clone(),
parent.protocol_config_commitment(),
None,
parent.timestamp() + 1,
);
let block = SignedBlock::new_unchecked(
header.clone(),
body,
BlockSignatures::new(Vec::new()).unwrap(),
);
writer.apply_block(block, None).await.expect("empty block should apply");
mmr.add(header.commitment()).unwrap();
parent = header;
}
}
async fn bootstrap_store(path: &std::path::Path) {
let signer = random_secret_key();
let genesis_block = GenesisState::new(
vec![],
test_fee_params(),
1,
ValidatorConfig::new(vec![signer.public_key()], 1)
.expect("validator config should be valid"),
test_protocol_config(),
)
.into_block()
.expect("genesis block should be created");
State::bootstrap(genesis_block, path).await.expect("store should bootstrap");
}
#[tokio::test(flavor = "multi_thread")]
async fn account_syncs_reject_ranges_that_end_below_the_retention_window() {
let data_directory = tempfile::tempdir().expect("tempdir should be created");
bootstrap_store(data_directory.path()).await;
let (state, mut writer, _proof_writer) = State::for_tests(data_directory.path()).await;
apply_empty_blocks(&state, &mut writer, HISTORICAL_BLOCK_RETENTION + 10).await;
let view = state.view();
let tip = *view.tip();
let oldest_retained = BlockNumber::from(tip.as_u32() - HISTORICAL_BLOCK_RETENTION);
let below_window = BlockNumber::from(oldest_retained.as_u32() - 1);
let account_id = AccountId::try_from(ACCOUNT_ID_PUBLIC_FUNGIBLE_FAUCET).unwrap();
let vault = view.sync_account_vault(account_id, BlockNumber::GENESIS..=below_window).await;
assert!(
matches!(
vault,
Err(DatabaseError::RangeBelowRetention(RangeBelowRetention {
oldest_retained: oldest,
block_to,
})) if oldest == oldest_retained && block_to == below_window
),
"vault sync below the retention window must fail, got {vault:?}"
);
let storage = view
.sync_account_storage_maps(account_id, BlockNumber::GENESIS..=below_window)
.await;
assert!(
matches!(
storage,
Err(DatabaseError::RangeBelowRetention(RangeBelowRetention {
oldest_retained: oldest,
block_to,
})) if oldest == oldest_retained && block_to == below_window
),
"storage map sync below the retention window must fail, got {storage:?}"
);
let (last_block, updates) = view
.sync_account_vault(account_id, BlockNumber::GENESIS..=oldest_retained)
.await
.expect("vault sync at the retention boundary should succeed");
assert_eq!(last_block, oldest_retained);
assert!(updates.is_empty());
view.sync_account_storage_maps(account_id, BlockNumber::GENESIS..=oldest_retained)
.await
.expect("storage map sync at the retention boundary should succeed");
}
}