use chrono::prelude::{DateTime, Utc};
use std::sync::Arc;
use crate::core::core::hash::{Hash, Hashed, ZERO_HASH};
use crate::core::core::{Block, BlockHeader};
use crate::core::pow::Difficulty;
use crate::core::ser;
use crate::error::Error;
use crate::util::RwLock;
bitflags! {
pub struct Options: u32 {
const NONE = 0b00000000;
const SKIP_POW = 0b00000001;
const SYNC = 0b00000010;
const MINE = 0b00000100;
}
}
#[derive(Debug, Clone, Copy, Eq, PartialEq, Deserialize, Serialize)]
#[allow(missing_docs)]
pub enum SyncStatus {
Initial,
NoSync,
AwaitingPeers(bool),
HeaderSync {
current_height: u64,
highest_height: u64,
},
TxHashsetDownload {
start_time: DateTime<Utc>,
prev_update_time: DateTime<Utc>,
update_time: DateTime<Utc>,
prev_downloaded_size: u64,
downloaded_size: u64,
total_size: u64,
},
TxHashsetSetup,
TxHashsetValidation {
kernels: u64,
kernel_total: u64,
rproofs: u64,
rproof_total: u64,
},
TxHashsetSave,
TxHashsetDone,
BodySync {
current_height: u64,
highest_height: u64,
},
Shutdown,
}
pub struct SyncState {
current: RwLock<SyncStatus>,
sync_error: Arc<RwLock<Option<Error>>>,
}
impl SyncState {
pub fn new() -> SyncState {
SyncState {
current: RwLock::new(SyncStatus::Initial),
sync_error: Arc::new(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) {
if self.status() == new_status {
return;
}
let mut status = self.current.write();
debug!("sync_state: sync_status: {:?} -> {:?}", *status, new_status,);
*status = new_status;
}
pub fn update_txhashset_download(&self, new_status: SyncStatus) -> bool {
if let SyncStatus::TxHashsetDownload { .. } = new_status {
let mut status = self.current.write();
*status = new_status;
true
} else {
false
}
}
pub fn set_sync_error(&self, error: Error) {
*self.sync_error.write() = Some(error);
}
pub fn sync_error(&self) -> Arc<RwLock<Option<Error>>> {
Arc::clone(&self.sync_error)
}
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(&self, vkernels: u64, vkernel_total: u64, vrproofs: u64, vrproof_total: u64) {
let mut status = self.current.write();
match *status {
SyncStatus::TxHashsetValidation {
kernels,
kernel_total,
rproofs,
rproof_total,
} => {
let ks = if vkernels > 0 { vkernels } else { kernels };
let kt = if vkernel_total > 0 {
vkernel_total
} else {
kernel_total
};
let rps = if vrproofs > 0 { vrproofs } else { rproofs };
let rpt = if vrproof_total > 0 {
vrproof_total
} else {
rproof_total
};
*status = SyncStatus::TxHashsetValidation {
kernels: ks,
kernel_total: kt,
rproofs: rps,
rproof_total: rpt,
};
}
_ => {
*status = SyncStatus::TxHashsetValidation {
kernels: 0,
kernel_total: 0,
rproofs: 0,
rproof_total: 0,
}
}
}
}
fn on_save(&self) {
self.update(SyncStatus::TxHashsetSave);
}
fn on_done(&self) {
self.update(SyncStatus::TxHashsetDone);
}
}
#[derive(Debug, PartialEq)]
pub struct TxHashSetRoots {
pub output_root: Hash,
pub rproof_root: Hash,
pub kernel_root: Hash,
}
#[derive(Debug)]
pub struct OutputMMRPosition {
pub output_mmr_hash: Hash,
pub position: u64,
pub height: u64,
}
#[derive(Serialize, Deserialize, Debug, Clone, 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(reader: &mut dyn ser::Reader) -> 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(&self, kernels: u64, kernel_total: u64, 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(&self, _ks: u64, _kts: u64, _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, PartialEq)]
pub enum BlockStatus {
Next,
Fork,
Reorg(u64),
}