use chrono::prelude::{DateTime, Utc};
use crate::core::core::hash::{Hash, Hashed, ZERO_HASH};
use crate::core::core::{Block, BlockHeader, HeaderVersion};
use crate::core::pow::Difficulty;
use crate::core::ser::{self, PMMRIndexHashable, Readable, Reader, Writeable, Writer};
use crate::error::{Error, ErrorKind};
use crate::util::{RwLock, RwLockWriteGuard};
bitflags! {
pub struct Options: u32 {
const NONE = 0b0000_0000;
const SKIP_POW = 0b0000_0001;
const SYNC = 0b0000_0010;
const MINE = 0b0000_0100;
}
}
#[derive(Debug, Clone, Copy, Eq, PartialEq, Deserialize, Serialize)]
pub enum SyncStatus {
Initial,
NoSync,
AwaitingPeers(bool),
HeaderSync {
current_height: u64,
highest_height: u64,
},
TxHashsetDownload(TxHashsetDownloadStats),
TxHashsetSetup,
TxHashsetKernelsValidation {
kernels: u64,
kernels_total: u64,
},
TxHashsetRangeProofsValidation {
rproofs: u64,
rproofs_total: u64,
},
TxHashsetSave,
TxHashsetDone,
BodySync {
current_height: u64,
highest_height: u64,
},
Shutdown,
}
#[derive(Debug, Clone, Copy, Eq, PartialEq, Deserialize, Serialize)]
pub struct TxHashsetDownloadStats {
pub start_time: DateTime<Utc>,
pub prev_update_time: DateTime<Utc>,
pub update_time: DateTime<Utc>,
pub prev_downloaded_size: u64,
pub downloaded_size: u64,
pub total_size: u64,
}
impl Default for TxHashsetDownloadStats {
fn default() -> Self {
TxHashsetDownloadStats {
start_time: Utc::now(),
update_time: Utc::now(),
prev_update_time: Utc::now(),
prev_downloaded_size: 0,
downloaded_size: 0,
total_size: 0,
}
}
}
pub struct SyncState {
current: RwLock<SyncStatus>,
sync_error: RwLock<Option<Error>>,
}
impl SyncState {
pub fn new() -> SyncState {
SyncState {
current: RwLock::new(SyncStatus::Initial),
sync_error: RwLock::new(None),
}
}
pub fn is_syncing(&self) -> bool {
*self.current.read() != SyncStatus::NoSync
}
pub fn status(&self) -> SyncStatus {
*self.current.read()
}
pub fn update(&self, new_status: SyncStatus) -> bool {
let status = self.current.write();
self.update_with_guard(new_status, status)
}
fn update_with_guard(
&self,
new_status: SyncStatus,
mut status: RwLockWriteGuard<SyncStatus>,
) -> bool {
if *status == new_status {
return false;
}
debug!("sync_state: sync_status: {:?} -> {:?}", *status, new_status,);
*status = new_status;
true
}
pub fn update_if<F>(&self, new_status: SyncStatus, f: F) -> bool
where
F: Fn(SyncStatus) -> bool,
{
let status = self.current.write();
if f(*status) {
self.update_with_guard(new_status, status)
} else {
false
}
}
pub fn update_txhashset_download(&self, stats: TxHashsetDownloadStats) {
*self.current.write() = SyncStatus::TxHashsetDownload(stats);
}
pub fn set_sync_error(&self, error: Error) {
*self.sync_error.write() = Some(error);
}
pub fn sync_error(&self) -> Option<String> {
self.sync_error.read().as_ref().map(|e| e.to_string())
}
pub fn clear_sync_error(&self) {
*self.sync_error.write() = None;
}
}
impl TxHashsetWriteStatus for SyncState {
fn on_setup(&self) {
self.update(SyncStatus::TxHashsetSetup);
}
fn on_validation_kernels(&self, kernels: u64, kernels_total: u64) {
self.update(SyncStatus::TxHashsetKernelsValidation {
kernels,
kernels_total,
});
}
fn on_validation_rproofs(&self, rproofs: u64, rproofs_total: u64) {
self.update(SyncStatus::TxHashsetRangeProofsValidation {
rproofs,
rproofs_total,
});
}
fn on_save(&self) {
self.update(SyncStatus::TxHashsetSave);
}
fn on_done(&self) {
self.update(SyncStatus::TxHashsetDone);
}
}
#[derive(Debug)]
pub struct TxHashSetRoots {
pub output_roots: OutputRoots,
pub rproof_root: Hash,
pub kernel_root: Hash,
}
impl TxHashSetRoots {
pub fn output_root(&self, header: &BlockHeader) -> Hash {
self.output_roots.root(header)
}
pub fn validate(&self, header: &BlockHeader) -> Result<(), Error> {
debug!(
"validate roots: {} at {}, {} vs. {} (original: {}, merged: {})",
header.hash(),
header.height,
header.output_root,
self.output_root(header),
self.output_roots.pmmr_root,
self.output_roots.merged_root(header),
);
if header.output_root != self.output_root(header)
|| header.range_proof_root != self.rproof_root
|| header.kernel_root != self.kernel_root
{
Err(ErrorKind::InvalidRoot.into())
} else {
Ok(())
}
}
}
#[derive(Debug)]
pub struct OutputRoots {
pub pmmr_root: Hash,
pub bitmap_root: Hash,
}
impl OutputRoots {
pub fn root(&self, header: &BlockHeader) -> Hash {
if header.version < HeaderVersion(3) {
self.output_root()
} else {
self.merged_root(header)
}
}
fn output_root(&self) -> Hash {
self.pmmr_root
}
fn merged_root(&self, header: &BlockHeader) -> Hash {
(self.pmmr_root, self.bitmap_root).hash_with_index(header.output_mmr_size)
}
}
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct CommitPos {
pub pos: u64,
pub height: u64,
}
impl Readable for CommitPos {
fn read<R: Reader>(reader: &mut R) -> Result<CommitPos, ser::Error> {
let pos = reader.read_u64()?;
let height = reader.read_u64()?;
Ok(CommitPos { pos, height })
}
}
impl Writeable for CommitPos {
fn write<W: Writer>(&self, writer: &mut W) -> Result<(), ser::Error> {
writer.write_u64(self.pos)?;
writer.write_u64(self.height)?;
Ok(())
}
}
#[derive(Serialize, Deserialize, Debug, Clone, Copy, PartialEq)]
pub struct Tip {
pub height: u64,
pub last_block_h: Hash,
pub prev_block_h: Hash,
pub total_difficulty: Difficulty,
}
impl Tip {
pub fn from_header(header: &BlockHeader) -> Tip {
Tip {
height: header.height,
last_block_h: header.hash(),
prev_block_h: header.prev_hash,
total_difficulty: header.total_difficulty(),
}
}
}
impl Hashed for Tip {
fn hash(&self) -> Hash {
self.last_block_h
}
}
impl Default for Tip {
fn default() -> Self {
Tip {
height: 0,
last_block_h: ZERO_HASH,
prev_block_h: ZERO_HASH,
total_difficulty: Difficulty::min(),
}
}
}
impl ser::Writeable for Tip {
fn write<W: ser::Writer>(&self, writer: &mut W) -> Result<(), ser::Error> {
writer.write_u64(self.height)?;
writer.write_fixed_bytes(&self.last_block_h)?;
writer.write_fixed_bytes(&self.prev_block_h)?;
self.total_difficulty.write(writer)
}
}
impl ser::Readable for Tip {
fn read<R: ser::Reader>(reader: &mut R) -> Result<Tip, ser::Error> {
let height = reader.read_u64()?;
let last = Hash::read(reader)?;
let prev = Hash::read(reader)?;
let diff = Difficulty::read(reader)?;
Ok(Tip {
height: height,
last_block_h: last,
prev_block_h: prev,
total_difficulty: diff,
})
}
}
pub trait ChainAdapter {
fn block_accepted(&self, block: &Block, status: BlockStatus, opts: Options);
}
pub trait TxHashsetWriteStatus {
fn on_setup(&self);
fn on_validation_kernels(&self, kernels: u64, kernel_total: u64);
fn on_validation_rproofs(&self, rproofs: u64, rproof_total: u64);
fn on_save(&self);
fn on_done(&self);
}
pub struct NoStatus;
impl TxHashsetWriteStatus for NoStatus {
fn on_setup(&self) {}
fn on_validation_kernels(&self, _ks: u64, _kts: u64) {}
fn on_validation_rproofs(&self, _rs: u64, _rt: u64) {}
fn on_save(&self) {}
fn on_done(&self) {}
}
pub struct NoopAdapter {}
impl ChainAdapter for NoopAdapter {
fn block_accepted(&self, _b: &Block, _status: BlockStatus, _opts: Options) {}
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum BlockStatus {
Next {
prev: Tip,
},
Fork {
prev: Tip,
head: Tip,
fork_point: Tip,
},
Reorg {
prev: Tip,
prev_head: Tip,
fork_point: Tip,
},
}
impl BlockStatus {
pub fn is_next(&self) -> bool {
match *self {
BlockStatus::Next { .. } => true,
_ => false,
}
}
pub fn is_reorg(&self) -> bool {
match *self {
BlockStatus::Reorg { .. } => true,
_ => false,
}
}
}