use tari_common_types::{
chain_metadata::ChainMetadata,
epoch::VnEpoch,
types::{BadBlock, CompressedCommitment, CompressedPublicKey, CompressedSignature, FixedHash, HashOutput},
};
use tari_node_components::blocks::{Block, BlockHeader, BlockHeaderAccumulatedData, ChainBlock, ChainHeader};
use tari_sidechain::ShardGroup;
use tari_transaction_components::transaction_components::{TransactionInput, TransactionKernel, TransactionOutput};
use super::{
AccumulatedDataRebuildStatus,
BlockchainCheckRequest,
MinedInfo,
PayrefRebuildStatus,
TemplateRegistrationEntry,
ValidatorNodeRegistrationInfo,
lmdb_db::lmdb_tree_reader::OwnedLmdbTreeReader,
};
use crate::{
blocks::BlockAccumulatedData,
chain_storage::{
ChainStorageError,
DbBasicStats,
DbKey,
DbTotalSizeStats,
DbTransaction,
DbValue,
HorizonData,
HorizonSyncOutputCheckpoint,
InputMinedInfo,
MmrTree,
OutputMinedInfo,
Reorg,
lmdb_db::BlockchainCheckStatus,
},
};
#[allow(clippy::ptr_arg)]
pub trait BlockchainBackend: Send + Sync + 'static {
fn write(&mut self, tx: DbTransaction) -> Result<(), ChainStorageError>;
fn fetch(&self, key: &DbKey) -> Result<Option<DbValue>, ChainStorageError>;
fn contains(&self, key: &DbKey) -> Result<bool, ChainStorageError>;
fn fetch_chain_header_by_height(&self, height: u64) -> Result<ChainHeader, ChainStorageError>;
fn fetch_header_accumulated_data(
&self,
hash: &HashOutput,
) -> Result<Option<BlockHeaderAccumulatedData>, ChainStorageError>;
fn fetch_chain_header_in_all_chains(&self, hash: &HashOutput) -> Result<ChainHeader, ChainStorageError>;
fn fetch_header_containing_kernel_mmr(&self, mmr_position: u64) -> Result<ChainHeader, ChainStorageError>;
fn is_empty(&self) -> Result<bool, ChainStorageError>;
fn fetch_block_accumulated_data(
&self,
header_hash: &HashOutput,
) -> Result<Option<BlockAccumulatedData>, ChainStorageError>;
fn fetch_block_accumulated_data_by_height(
&self,
height: u64,
) -> Result<Option<BlockAccumulatedData>, ChainStorageError>;
fn fetch_kernels_in_block(&self, header_hash: &HashOutput) -> Result<Vec<TransactionKernel>, ChainStorageError>;
fn fetch_bad_blocks(&self) -> Result<Vec<BadBlock>, ChainStorageError>;
fn clear_all_bad_blocks(&mut self) -> Result<(), ChainStorageError>;
fn fetch_kernel_by_excess_sig(
&self,
excess_sig: &CompressedSignature,
) -> Result<Option<(TransactionKernel, HashOutput)>, ChainStorageError>;
fn fetch_outputs_in_block_with_spend_state(
&self,
header_hash: &HashOutput,
spend_status_at_header: Option<&HashOutput>,
) -> Result<Vec<(TransactionOutput, bool)>, ChainStorageError>;
fn fetch_output(&self, output_hash: &HashOutput) -> Result<Option<OutputMinedInfo>, ChainStorageError>;
fn fetch_input(&self, output_hash: &HashOutput) -> Result<Option<InputMinedInfo>, ChainStorageError>;
fn fetch_unspent_output_hash_by_commitment(
&self,
commitment: &CompressedCommitment,
) -> Result<Option<HashOutput>, ChainStorageError>;
fn fetch_mined_info_by_payref(&self, payref: &FixedHash) -> Result<MinedInfo, ChainStorageError>;
fn fetch_mined_info_by_output_hash(&self, output_hash: &HashOutput) -> Result<MinedInfo, ChainStorageError>;
fn fetch_outputs_in_block(&self, header_hash: &HashOutput) -> Result<Vec<TransactionOutput>, ChainStorageError>;
fn fetch_inputs_in_block(&self, header_hash: &HashOutput) -> Result<Vec<TransactionInput>, ChainStorageError>;
fn fetch_mmr_size(&self, tree: MmrTree) -> Result<u64, ChainStorageError>;
fn orphan_count(&self) -> Result<usize, ChainStorageError>;
fn fetch_last_header(&self) -> Result<BlockHeader, ChainStorageError>;
fn clear_all_pending_headers(&self) -> Result<usize, ChainStorageError>;
fn fetch_last_chain_header(&self) -> Result<ChainHeader, ChainStorageError>;
fn fetch_tip_header(&self) -> Result<ChainHeader, ChainStorageError>;
fn fetch_chain_metadata(&self) -> Result<ChainMetadata, ChainStorageError>;
fn fetch_payref_rebuild_status(&self) -> Result<PayrefRebuildStatus, ChainStorageError>;
fn fetch_accumulated_data_rebuild_status(&self) -> Result<AccumulatedDataRebuildStatus, ChainStorageError>;
fn update_blockchain_consistency_check_status(
&self,
request: BlockchainCheckRequest,
) -> Result<BlockchainCheckStatus, ChainStorageError>;
fn update_accumulated_data_check_status(
&self,
request: BlockchainCheckRequest,
) -> Result<BlockchainCheckStatus, ChainStorageError>;
fn fetch_blockchain_consistency_check_status(&self) -> Result<Option<BlockchainCheckStatus>, ChainStorageError>;
fn fetch_accumulated_data_check_status(&self) -> Result<Option<BlockchainCheckStatus>, ChainStorageError>;
fn build_payref_indexes_for_height(
&self,
height: u64,
metadata_at_start: ChainMetadata,
initialize_stats: Option<u64>,
finalize: bool,
) -> Result<PayrefRebuildStatus, ChainStorageError>;
fn update_accumulated_difficulty(
&self,
height: u64,
header_accum_data: BlockHeaderAccumulatedData,
last_chain_header: ChainHeader,
update_meta_data_db: bool,
) -> Result<AccumulatedDataRebuildStatus, ChainStorageError>;
fn utxo_count(&self) -> Result<usize, ChainStorageError>;
fn kernel_count(&self) -> Result<usize, ChainStorageError>;
fn fetch_orphan_chain_tip_by_hash(&self, hash: &HashOutput) -> Result<Option<ChainHeader>, ChainStorageError>;
fn fetch_strongest_orphan_chain_tips(&self) -> Result<Vec<ChainHeader>, ChainStorageError>;
fn fetch_orphan_children_of(&self, hash: HashOutput) -> Result<Vec<Block>, ChainStorageError>;
fn fetch_orphan_chain_block(&self, hash: HashOutput) -> Result<Option<ChainBlock>, ChainStorageError>;
fn delete_oldest_orphans(
&mut self,
horizon_height: u64,
orphan_storage_capacity: usize,
) -> Result<(), ChainStorageError>;
fn fetch_monero_seed_first_seen_height(&self, seed: &[u8]) -> Result<u64, ChainStorageError>;
fn fetch_horizon_data(&self) -> Result<Option<HorizonData>, ChainStorageError>;
fn fetch_horizon_sync_output_checkpoint(&self) -> Result<Option<HorizonSyncOutputCheckpoint>, ChainStorageError>;
fn verify_horizon_sync_output_root(&self, version: u64, expected_root: HashOutput)
-> Result<(), ChainStorageError>;
fn get_stats(&self) -> Result<DbBasicStats, ChainStorageError>;
fn fetch_total_size_stats(&self) -> Result<DbTotalSizeStats, ChainStorageError>;
fn bad_block_exists(&self, block_hash: HashOutput) -> Result<(bool, String), ChainStorageError>;
fn fetch_all_reorgs(&self) -> Result<Vec<Reorg>, ChainStorageError>;
fn fetch_all_active_validator_nodes(
&self,
height: u64,
) -> Result<Vec<ValidatorNodeRegistrationInfo>, ChainStorageError>;
fn fetch_active_validator_nodes(
&self,
sidechain_pk: Option<&CompressedPublicKey>,
height: u64,
) -> Result<Vec<ValidatorNodeRegistrationInfo>, ChainStorageError>;
fn fetch_validators_activating_in_epoch(
&self,
sidechain_pk: Option<&CompressedPublicKey>,
epoch: VnEpoch,
) -> Result<Vec<ValidatorNodeRegistrationInfo>, ChainStorageError>;
fn fetch_validators_exiting_in_epoch(
&self,
sidechain_pk: Option<&CompressedPublicKey>,
epoch: VnEpoch,
) -> Result<Vec<ValidatorNodeRegistrationInfo>, ChainStorageError>;
fn validator_node_exists(
&self,
sidechain_pk: Option<&CompressedPublicKey>,
current_epoch: VnEpoch,
validator_node_pk: &CompressedPublicKey,
) -> Result<bool, ChainStorageError>;
fn validator_node_is_active(
&self,
sidechain_pk: Option<&CompressedPublicKey>,
end_epoch: VnEpoch,
validator_node_pk: &CompressedPublicKey,
) -> Result<bool, ChainStorageError>;
fn validator_node_is_active_for_shard_group(
&self,
sidechain_pk: Option<&CompressedPublicKey>,
epoch: VnEpoch,
validator_node_pk: &CompressedPublicKey,
shard_group: ShardGroup,
) -> Result<bool, ChainStorageError>;
fn validator_nodes_count_for_shard_group(
&self,
sidechain_pk: Option<&CompressedPublicKey>,
end_epoch: VnEpoch,
shard_group: ShardGroup,
) -> Result<usize, ChainStorageError>;
fn get_validator_node(
&self,
sidechain_pk: Option<&CompressedPublicKey>,
public_key: CompressedPublicKey,
) -> Result<Option<ValidatorNodeRegistrationInfo>, ChainStorageError>;
fn fetch_template_registrations(
&self,
start_height: u64,
end_height: u64,
) -> Result<Vec<TemplateRegistrationEntry>, ChainStorageError>;
fn create_smt_reader(&self) -> Result<OwnedLmdbTreeReader<'_>, ChainStorageError>;
fn set_stats_total_height(&self, total: u64);
fn update_stats_progress(&self, current: u64);
fn fetch_all_orphans(&self) -> Result<Vec<ChainHeader>, ChainStorageError>;
}