use std::marker::PhantomData;
use log::*;
use tari_common_types::types::{CompressedCommitment, PrivateKey};
use tari_crypto::commitment::HomomorphicCommitmentFactory;
use tari_transaction_components::{MicroMinotari, crypto_factories::CryptoFactories};
use crate::{
chain_storage::BlockchainBackend,
consensus::BaseNodeConsensusManager,
validation::{FinalHorizonStateValidation, ValidationError},
};
const LOG_TARGET: &str = "c::bn::state_machine_service::states::horizon_state_sync::chain_balance";
pub struct ChainBalanceValidator<B> {
rules: BaseNodeConsensusManager,
factories: CryptoFactories,
_phantom: PhantomData<B>,
}
impl<B: BlockchainBackend> ChainBalanceValidator<B> {
pub fn new(rules: BaseNodeConsensusManager, factories: CryptoFactories) -> Self {
Self {
rules,
factories,
_phantom: Default::default(),
}
}
}
impl<B: BlockchainBackend> FinalHorizonStateValidation<B> for ChainBalanceValidator<B> {
fn validate(
&self,
backend: &B,
height: u64,
total_utxo_sum: &CompressedCommitment,
total_kernel_sum: &CompressedCommitment,
total_burned_sum: &CompressedCommitment,
) -> Result<(), ValidationError> {
let emission_h = self.get_emission_commitment_at(height);
let total_offset = self.fetch_total_offset_commitment(height, backend)?;
debug!(
target: LOG_TARGET,
"Emission:{emission_h:?}. Offset:{total_offset:?}, total kernel: {total_kernel_sum:?}, height: {height}, total_utxo: {total_utxo_sum:?}, total_burned: {total_burned_sum:?}"
);
let input =
&(&emission_h.to_commitment()? + &total_kernel_sum.to_commitment()?) + &total_offset.to_commitment()?;
if (&total_utxo_sum.to_commitment()? + &total_burned_sum.to_commitment()?) != input {
return Err(ValidationError::ChainBalanceValidationFailed(height));
}
Ok(())
}
}
impl<B: BlockchainBackend> ChainBalanceValidator<B> {
fn fetch_total_offset_commitment(&self, height: u64, backend: &B) -> Result<CompressedCommitment, ValidationError> {
let chain_header = backend.fetch_chain_header_by_height(height)?;
let offset = &chain_header.accumulated_data().total_kernel_offset;
Ok(CompressedCommitment::from_commitment(
self.factories.commitment.commit(offset, &0u64.into()),
))
}
fn get_emission_commitment_at(&self, height: u64) -> CompressedCommitment {
let total_supply = self.rules.get_total_emission_at(height);
debug!(
target: LOG_TARGET,
"Expected emission at height {height} is {total_supply}"
);
self.commit_value(total_supply)
}
#[inline]
fn commit_value(&self, v: MicroMinotari) -> CompressedCommitment {
CompressedCommitment::from_commitment(self.factories.commitment.commit_value(&PrivateKey::default(), v.into()))
}
}