Skip to content
Permalink
Browse files

Hooked checkpoints back-end to StateChain

  • Loading branch information...
octavonce committed Jul 15, 2019
1 parent ba5d361 commit f990505e60e2448394d2d7385963a67c894828bc
@@ -64,4 +64,5 @@ build: false

test_script:
- git submodule update --init --recursive
- cargo test --verbose --all %cargoflags%
- cargo test --verbose --all %cargoflags%
- cargo build --verbose %cargoflags%
@@ -12,4 +12,5 @@ matrix:
fast_finish: true

script:
- cargo test --verbose --all --features miner-cpu
- cargo test --verbose --all --features miner-cpu
- cargo build --verbose --features miner-cpu

Some generated files are not rendered by default. Learn more.

@@ -5,6 +5,21 @@ license = "GPL-3.0"
publish = false

[workspace]
members = [
"src/account",
"src/causality",
"src/chain",
"src/common",
"src/consensus",
"src/crypto",
"src/events",
"src/jump",
"src/miner",
"src/network",
"src/persistence",
"src/purple_vm",
"src/transactions",
]

[dependencies]
clap = "2.32.0"
@@ -36,6 +51,14 @@ chain = { path = "src/chain" }
miner = { path = "src/miner" }
mimalloc = "0.1.5"

[dev-dependencies.chain]
path = "src/chain"
features = ["test"]

[dev-dependencies.persistence]
path = "src/persistence"
features = ["test"]

[[bin]]
path = "src/purple/main.rs"
name = "purple"
@@ -47,4 +70,5 @@ lto = true
default = []
miner-cpu-avx = ["miner/cpu", "miner/avx"]
miner-cpu = ["miner/cpu"]
miner-gpu = ["miner/gpu"]
miner-gpu = ["miner/gpu"]

@@ -26,4 +26,7 @@ quickcheck = "0.7.2"
hashbrown = { git = "https://github.com/octavonce/hashbrown", features = ["serde", "rayon"] }

[dev-dependencies]
test-helpers = { path = "../util/test-helpers" }
test-helpers = { path = "../util/test-helpers" }

[features]
test = []
@@ -232,6 +232,19 @@ impl<B: Block> Chain<B> {
None => None
};

// Load heights and checkpoint ids
let disk_heights_checkpoints = if let Some(ids_and_heights) = B::ChainState::fetch_existing_checkpoints() {
let mut hm = HashMap::with_capacity(B::MAX_CHECKPOINTS);

for (id, height) in ids_and_heights {
hm.insert(height, id);
}

hm
} else {
HashMap::with_capacity(B::MAX_CHECKPOINTS)
};

let height = height;

Chain {
@@ -245,7 +258,7 @@ impl<B: Block> Chain<B> {
disconnected_tips_mapping: HashMap::with_capacity(B::MAX_ORPHANS),
valid_tips: HashSet::with_capacity(B::MAX_ORPHANS),
valid_tips_states: HashMap::with_capacity(B::MAX_ORPHANS),
disk_heights_checkpoints: HashMap::with_capacity(B::MAX_CHECKPOINTS),
disk_heights_checkpoints,
last_checkpoint_height,
earliest_checkpoint_height,
max_orphan_height: None,
@@ -580,7 +593,7 @@ impl<B: Block> Chain<B> {
}

self.last_checkpoint_height = Some(height);
let checkpoint_id = new_tip_state.checkpoint();
let checkpoint_id = new_tip_state.checkpoint(height);

// Store checkpoint id
self.disk_heights_checkpoints.insert(height, checkpoint_id);
@@ -710,7 +723,7 @@ impl<B: Block> Chain<B> {
}

self.last_checkpoint_height = Some(height);
let checkpoint_id = state.checkpoint();
let checkpoint_id = state.checkpoint(height);

// Store checkpoint id
self.disk_heights_checkpoints.insert(height, checkpoint_id);
@@ -1201,7 +1214,7 @@ impl<B: Block> Chain<B> {
}

self.last_checkpoint_height = Some(height);
let checkpoint_id = new_tip_state.checkpoint();
let checkpoint_id = new_tip_state.checkpoint(height);

// Store checkpoint id
self.disk_heights_checkpoints.insert(height, checkpoint_id);
@@ -31,6 +31,8 @@ pub struct ChainState {
}

impl ChainState {
const POOL_STATE_KEY: &'static [u8] = b"pool_state";

pub fn new(db: PersistentDb) -> ChainState {
ChainState {
db,
@@ -40,12 +42,52 @@ impl ChainState {
}

impl Checkpointable for ChainState {
fn checkpoint(&self) -> u64 {
unimplemented!();
fn checkpoint(&self, height: u64) -> u64 {
#[cfg(not(feature = "test"))]
{
let mut registry = STATE_REGISTRY.lock();
registry.checkpoint(self.db.db_ref.clone().unwrap(), height)
}

#[cfg(feature = "test")]
{
STATE_REGISTRY.with(|registry| {
let mut registry = registry.lock();
registry.checkpoint(self.db.db_ref.clone().unwrap(), height)
})
}
}

fn fetch_existing_checkpoints() -> Option<Vec<(u64, u64)>> {
#[cfg(not(feature = "test"))]
{
let registry = STATE_REGISTRY.lock();
registry.retrieve_ids_and_heights()
}

#[cfg(feature = "test")]
{
STATE_REGISTRY.with(|registry| {
let mut registry = registry.lock();
registry.retrieve_ids_and_heights()
})
}
}

fn delete_checkpoint(id: u64) -> Result<(), ()> {
unimplemented!();
#[cfg(not(feature = "test"))]
{
let mut registry = STATE_REGISTRY.lock();
registry.delete_checkpoint(id)
}

#[cfg(feature = "test")]
{
STATE_REGISTRY.with(|registry| {
let mut registry = registry.lock();
registry.delete_checkpoint(id)
})
}
}

fn load_from_disk(id: u64) -> Result<ChainState, ()> {
@@ -34,7 +34,11 @@ pub enum StorageLocation {
pub trait Checkpointable: Sized + Debug + Clone {
/// Creates a checkpoint of the current state and
/// returns the checkpoint id associated with it.
fn checkpoint(&self) -> u64;
fn checkpoint(&self, height: u64) -> u64;

/// Returns a vector containing existing checkpoint ids and
/// their corresponding height if there are any listed.
fn fetch_existing_checkpoints() -> Option<Vec<(u64, u64)>>;

/// Deletes the checkpoint with the given id
fn delete_checkpoint(id: u64) -> Result<(), ()>;
@@ -111,7 +115,7 @@ impl DummyCheckpoint {
}

impl Checkpointable for DummyCheckpoint {
fn checkpoint(&self) -> u64 {
fn checkpoint(&self, _height: u64) -> u64 {
let mut id = self.id.lock();
let mut location = self.location.lock();
*id = CHECKPOINT_ID.fetch_add(1, Ordering::Relaxed) as u64;
@@ -124,6 +128,10 @@ impl Checkpointable for DummyCheckpoint {
id.clone()
}

fn fetch_existing_checkpoints() -> Option<Vec<(u64, u64)>> {
None
}

fn delete_checkpoint(id: u64) -> Result<(), ()> {
let mut db = DUMMY_BACKEND.lock();
let mut db = db.get_mut(&id).unwrap();
@@ -27,5 +27,6 @@ quickcheck = "0.7.2"
hashbrown = { git = "https://github.com/octavonce/hashbrown", features = ["rayon", "serde"] }

[dev-dependencies]
tempdir = "0.3.7"
common = { path = "../common" }
test-helpers = { path = "../util/test-helpers" }

0 comments on commit f990505

Please sign in to comment.
You can’t perform that action at this time.