use crate::core::consensus::HeaderDifficultyInfo;
use crate::core::core::hash::{Hash, Hashed};
use crate::core::core::{Block, BlockHeader, BlockSums};
use crate::core::pow::Difficulty;
use crate::core::ser::{DeserializationMode, ProtocolVersion, Readable, Writeable};
use crate::linked_list::MultiIndex;
use crate::types::{CommitPos, Tip};
use crate::util::secp::pedersen::Commitment;
use croaring::Bitmap;
use aigc_core::ser;
use aigc_store as store;
use aigc_store::{option_to_not_found, to_key, Error};
use std::convert::TryInto;
use std::sync::Arc;
const STORE_SUBPATH: &str = "chain";
const BLOCK_HEADER_PREFIX: u8 = b'h';
const BLOCK_PREFIX: u8 = b'b';
const HEAD_PREFIX: u8 = b'H';
const TAIL_PREFIX: u8 = b'T';
const HEADER_HEAD_PREFIX: u8 = b'G';
const OUTPUT_POS_PREFIX: u8 = b'p';
pub const NRD_KERNEL_LIST_PREFIX: u8 = b'K';
pub const NRD_KERNEL_ENTRY_PREFIX: u8 = b'k';
const BLOCK_SUMS_PREFIX: u8 = b'M';
const BLOCK_SPENT_PREFIX: u8 = b'S';
pub struct ChainStore {
db: store::Store,
}
impl ChainStore {
pub fn new(db_root: &str) -> Result<ChainStore, Error> {
let db = store::Store::new(db_root, None, Some(STORE_SUBPATH), None)?;
Ok(ChainStore { db })
}
pub fn head(&self) -> Result<Tip, Error> {
option_to_not_found(self.db.get_ser(&[HEAD_PREFIX], None), || "HEAD".to_owned())
}
pub fn header_head(&self) -> Result<Tip, Error> {
option_to_not_found(self.db.get_ser(&[HEADER_HEAD_PREFIX], None), || {
"HEADER_HEAD".to_owned()
})
}
pub fn tail(&self) -> Result<Tip, Error> {
option_to_not_found(self.db.get_ser(&[TAIL_PREFIX], None), || "TAIL".to_owned())
}
pub fn head_header(&self) -> Result<BlockHeader, Error> {
self.get_block_header(&self.head()?.last_block_h)
}
pub fn get_block(&self, h: &Hash) -> Result<Block, Error> {
option_to_not_found(self.db.get_ser(&to_key(BLOCK_PREFIX, h), None), || {
format!("BLOCK: {}", h)
})
}
pub fn block_exists(&self, h: &Hash) -> Result<bool, Error> {
self.db.exists(&to_key(BLOCK_PREFIX, h))
}
pub fn get_block_sums(&self, h: &Hash) -> Result<BlockSums, Error> {
option_to_not_found(self.db.get_ser(&to_key(BLOCK_SUMS_PREFIX, h), None), || {
format!("Block sums for block: {}", h)
})
}
pub fn get_previous_header(&self, header: &BlockHeader) -> Result<BlockHeader, Error> {
self.get_block_header(&header.prev_hash)
}
pub fn get_previous_header_skip_proof(
&self,
header: &BlockHeader,
) -> Result<BlockHeader, Error> {
self.get_block_header_skip_proof(&header.prev_hash)
}
pub fn get_block_header(&self, h: &Hash) -> Result<BlockHeader, Error> {
option_to_not_found(
self.db.get_ser(&to_key(BLOCK_HEADER_PREFIX, h), None),
|| format!("BLOCK HEADER: {}", h),
)
}
pub fn get_block_header_skip_proof(&self, h: &Hash) -> Result<BlockHeader, Error> {
option_to_not_found(
self.db.get_ser(
&to_key(BLOCK_HEADER_PREFIX, h),
Some(ser::DeserializationMode::SkipPow),
),
|| format!("BLOCK HEADER: {}", h),
)
}
pub fn get_output_pos(&self, commit: &Commitment) -> Result<u64, Error> {
match self.get_output_pos_height(commit)? {
Some(pos) => Ok(pos.pos - 1),
None => Err(Error::NotFoundErr(format!(
"Output position for: {:?}",
commit
))),
}
}
pub fn get_output_pos_height(&self, commit: &Commitment) -> Result<Option<CommitPos>, Error> {
self.db.get_ser(&to_key(OUTPUT_POS_PREFIX, commit), None)
}
pub fn batch(&self) -> Result<Batch<'_>, Error> {
Ok(Batch {
db: self.db.batch()?,
})
}
}
pub struct Batch<'a> {
pub db: store::Batch<'a>,
}
impl<'a> Batch<'a> {
pub fn head(&self) -> Result<Tip, Error> {
option_to_not_found(self.db.get_ser(&[HEAD_PREFIX], None), || "HEAD".to_owned())
}
pub fn tail(&self) -> Result<Tip, Error> {
option_to_not_found(self.db.get_ser(&[TAIL_PREFIX], None), || "TAIL".to_owned())
}
pub fn header_head(&self) -> Result<Tip, Error> {
option_to_not_found(self.db.get_ser(&[HEADER_HEAD_PREFIX], None), || {
"HEADER_HEAD".to_owned()
})
}
pub fn head_header(&self) -> Result<BlockHeader, Error> {
self.get_block_header(&self.head()?.last_block_h)
}
pub fn save_body_head(&self, t: &Tip) -> Result<(), Error> {
self.db.put_ser(&[HEAD_PREFIX], t)
}
pub fn save_body_tail(&self, t: &Tip) -> Result<(), Error> {
self.db.put_ser(&[TAIL_PREFIX], t)
}
pub fn save_header_head(&self, t: &Tip) -> Result<(), Error> {
self.db.put_ser(&[HEADER_HEAD_PREFIX], t)
}
pub fn get_block(&self, h: &Hash) -> Result<Block, Error> {
option_to_not_found(self.db.get_ser(&to_key(BLOCK_PREFIX, h), None), || {
format!("Block with hash: {}", h)
})
}
pub fn block_exists(&self, h: &Hash) -> Result<bool, Error> {
self.db.exists(&to_key(BLOCK_PREFIX, h))
}
pub fn save_block(&self, b: &Block) -> Result<(), Error> {
debug!(
"save_block: {} at {} ({} -> v{})",
b.header.hash(),
b.header.height,
b.inputs().version_str(),
self.db.protocol_version(),
);
self.db.put_ser(&to_key(BLOCK_PREFIX, b.hash())[..], b)?;
Ok(())
}
pub fn save_spent_index(&self, h: &Hash, spent: &[CommitPos]) -> Result<(), Error> {
self.db
.put_ser(&to_key(BLOCK_SPENT_PREFIX, h)[..], &spent.to_vec())?;
Ok(())
}
pub fn delete(&self, key: &[u8]) -> Result<(), Error> {
self.db.delete(key)
}
pub fn delete_block(&self, bh: &Hash) -> Result<(), Error> {
self.db.delete(&to_key(BLOCK_PREFIX, bh)[..])?;
{
let _ = self.delete_block_sums(bh);
let _ = self.delete_spent_index(bh);
}
Ok(())
}
pub fn save_block_header(&self, header: &BlockHeader) -> Result<(), Error> {
let hash = header.hash();
self.db
.put_ser(&to_key(BLOCK_HEADER_PREFIX, hash)[..], header)?;
Ok(())
}
pub fn save_output_pos_height(&self, commit: &Commitment, pos: CommitPos) -> Result<(), Error> {
self.db
.put_ser(&to_key(OUTPUT_POS_PREFIX, commit)[..], &pos)
}
pub fn delete_output_pos_height(&self, commit: &Commitment) -> Result<(), Error> {
self.db.delete(&to_key(OUTPUT_POS_PREFIX, commit))
}
pub fn is_match_output_pos_key(&self, key: &[u8], commit: &Commitment) -> bool {
let commit_key = to_key(OUTPUT_POS_PREFIX, commit);
commit_key == key
}
pub fn output_pos_iter(&self) -> Result<impl Iterator<Item = (Vec<u8>, CommitPos)>, Error> {
let key = to_key(OUTPUT_POS_PREFIX, "");
let protocol_version = self.db.protocol_version();
self.db.iter(&key, move |k, mut v| {
ser::deserialize(&mut v, protocol_version, DeserializationMode::default())
.map(|pos| (k.to_vec(), pos))
.map_err(From::from)
})
}
pub fn get_output_pos(&self, commit: &Commitment) -> Result<u64, Error> {
match self.get_output_pos_height(commit)? {
Some(pos) => Ok(pos.pos - 1),
None => Err(Error::NotFoundErr(format!(
"Output position for: {:?}",
commit
))),
}
}
pub fn get_output_pos_height(&self, commit: &Commitment) -> Result<Option<CommitPos>, Error> {
self.db.get_ser(&to_key(OUTPUT_POS_PREFIX, commit), None)
}
pub fn get_previous_header(&self, header: &BlockHeader) -> Result<BlockHeader, Error> {
self.get_block_header(&header.prev_hash)
}
pub fn get_previous_header_skip_proof(
&self,
header: &BlockHeader,
) -> Result<BlockHeader, Error> {
self.get_block_header_skip_proof(&header.prev_hash)
}
pub fn get_block_header(&self, h: &Hash) -> Result<BlockHeader, Error> {
option_to_not_found(
self.db.get_ser(&to_key(BLOCK_HEADER_PREFIX, h), None),
|| format!("BLOCK HEADER: {}", h),
)
}
pub fn get_block_header_skip_proof(&self, h: &Hash) -> Result<BlockHeader, Error> {
option_to_not_found(
self.db.get_ser(
&to_key(BLOCK_HEADER_PREFIX, h),
Some(ser::DeserializationMode::SkipPow),
),
|| format!("BLOCK HEADER: {}", h),
)
}
fn delete_spent_index(&self, bh: &Hash) -> Result<(), Error> {
self.db.delete(&to_key(BLOCK_SPENT_PREFIX, bh))
}
pub fn save_block_sums(&self, h: &Hash, sums: BlockSums) -> Result<(), Error> {
self.db.put_ser(&to_key(BLOCK_SUMS_PREFIX, h)[..], &sums)
}
pub fn get_block_sums(&self, h: &Hash) -> Result<BlockSums, Error> {
option_to_not_found(self.db.get_ser(&to_key(BLOCK_SUMS_PREFIX, h), None), || {
format!("Block sums for block: {}", h)
})
}
fn delete_block_sums(&self, bh: &Hash) -> Result<(), Error> {
self.db.delete(&to_key(BLOCK_SUMS_PREFIX, bh))
}
pub fn get_block_input_bitmap(&self, bh: &Hash) -> Result<Bitmap, Error> {
let bitmap = self
.get_spent_index(bh)?
.into_iter()
.map(|x| x.pos.try_into().unwrap())
.collect();
Ok(bitmap)
}
pub fn get_spent_index(&self, bh: &Hash) -> Result<Vec<CommitPos>, Error> {
option_to_not_found(
self.db.get_ser(&to_key(BLOCK_SPENT_PREFIX, bh), None),
|| format!("spent index: {}", bh),
)
}
pub fn commit(self) -> Result<(), Error> {
self.db.commit()
}
pub fn child(&mut self) -> Result<Batch<'_>, Error> {
Ok(Batch {
db: self.db.child()?,
})
}
pub fn blocks_iter(&self) -> Result<impl Iterator<Item = Block>, Error> {
let key = to_key(BLOCK_PREFIX, "");
let protocol_version = self.db.protocol_version();
self.db.iter(&key, move |_, mut v| {
ser::deserialize(&mut v, protocol_version, DeserializationMode::default())
.map_err(From::from)
})
}
pub fn blocks_raw_iter(&self) -> Result<impl Iterator<Item = (Vec<u8>, Vec<u8>)>, Error> {
let key = to_key(BLOCK_PREFIX, "");
self.db.iter(&key, |k, v| Ok((k.to_vec(), v.to_vec())))
}
pub fn protocol_version(&self) -> ProtocolVersion {
self.db.protocol_version()
}
}
pub struct DifficultyIter<'a> {
start: Hash,
store: Option<Arc<ChainStore>>,
batch: Option<Batch<'a>>,
header: Option<BlockHeader>,
prev_header: Option<BlockHeader>,
prev_header_hash: Option<Hash>,
}
impl<'a> DifficultyIter<'a> {
pub fn from<'b>(start: Hash, store: Arc<ChainStore>) -> DifficultyIter<'b> {
DifficultyIter {
start,
store: Some(store),
batch: None,
header: None,
prev_header: None,
prev_header_hash: None,
}
}
pub fn from_batch(start: Hash, batch: Batch<'_>) -> DifficultyIter<'_> {
DifficultyIter {
start,
store: None,
batch: Some(batch),
header: None,
prev_header: None,
prev_header_hash: None,
}
}
}
impl<'a> Iterator for DifficultyIter<'a> {
type Item = HeaderDifficultyInfo;
fn next(&mut self) -> Option<Self::Item> {
let (cur_header, cur_header_hash) = if self.header.is_none() {
if let Some(ref batch) = self.batch {
(
batch.get_block_header_skip_proof(&self.start).ok(),
Some(self.start),
)
} else if let Some(ref store) = self.store {
(
store.get_block_header_skip_proof(&self.start).ok(),
Some(self.start),
)
} else {
(None, None)
}
} else {
(self.prev_header.clone(), self.prev_header_hash)
};
self.header = cur_header;
if let Some(header) = self.header.clone() {
if let Some(ref batch) = self.batch {
self.prev_header = batch.get_previous_header_skip_proof(&header).ok();
} else if let Some(ref store) = self.store {
self.prev_header = store.get_previous_header_skip_proof(&header).ok();
} else {
self.prev_header = None;
}
self.prev_header_hash = Some(header.prev_hash);
let prev_difficulty = self
.prev_header
.clone()
.map_or(Difficulty::zero(), |x| x.total_difficulty());
let difficulty = header.total_difficulty() - prev_difficulty;
let scaling = header.pow.secondary_scaling;
Some(HeaderDifficultyInfo::new(
cur_header_hash,
header.timestamp.timestamp() as u64,
difficulty,
scaling,
header.pow.is_secondary(),
))
} else {
None
}
}
}
pub fn nrd_recent_kernel_index() -> MultiIndex<CommitPos> {
MultiIndex::init(NRD_KERNEL_LIST_PREFIX, NRD_KERNEL_ENTRY_PREFIX)
}
struct BoolFlag(bool);
impl From<BoolFlag> for bool {
fn from(b: BoolFlag) -> Self {
b.0
}
}
impl Readable for BoolFlag {
fn read<R: ser::Reader>(reader: &mut R) -> Result<Self, ser::Error> {
let x = reader.read_u8()?;
Ok(BoolFlag(1 & x == 1))
}
}
impl Writeable for BoolFlag {
fn write<W: ser::Writer>(&self, writer: &mut W) -> Result<(), ser::Error> {
writer.write_u8(self.0.into())?;
Ok(())
}
}