#![allow(unused_imports)]
use std::ops::Deref;
use zebra_chain::{amount::NonNegative, block::Block, sprout, value_balance::ValueBalance};
use crate::service::finalized_state::{
disk_db::{DiskDb, DiskWriteBatch, WriteDisk},
ZebraDb,
};
impl Deref for ZebraDb {
type Target = DiskDb;
fn deref(&self) -> &Self::Target {
self.db()
}
}
impl ZebraDb {
pub fn db(&self) -> &DiskDb {
&self.db
}
#[cfg(any(test, feature = "proptest-impl"))]
pub fn set_finalized_value_pool(&self, fake_value_pool: ValueBalance<NonNegative>) {
let mut batch = DiskWriteBatch::new();
let value_pool_cf = self.db().cf_handle("tip_chain_value_pool").unwrap();
batch.zs_insert(&value_pool_cf, (), fake_value_pool);
self.db().write(batch).unwrap();
}
#[cfg(any(test, feature = "proptest-impl"))]
pub fn populate_with_anchors(&self, block: &Block) {
let mut batch = DiskWriteBatch::new();
let sprout_anchors = self.db().cf_handle("sprout_anchors").unwrap();
let sapling_anchors = self.db().cf_handle("sapling_anchors").unwrap();
let orchard_anchors = self.db().cf_handle("orchard_anchors").unwrap();
let sprout_tree = sprout::tree::NoteCommitmentTree::default();
sprout_tree.root();
for transaction in block.transactions.iter() {
for joinsplit in transaction.sprout_groth16_joinsplits() {
batch.zs_insert(&sprout_anchors, joinsplit.anchor, sprout_tree.clone());
}
for anchor in transaction.sapling_anchors() {
batch.zs_insert(&sapling_anchors, anchor, ());
}
if let Some(orchard_shielded_data) = transaction.orchard_shielded_data() {
batch.zs_insert(&orchard_anchors, orchard_shielded_data.shared_anchor, ());
}
}
self.db().write(batch).unwrap();
}
}