diff --git a/Cargo.lock b/Cargo.lock index 4561e958..ed3245b7 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -118,22 +118,23 @@ dependencies = [ [[package]] name = "bindgen" -version = "0.47.3" +version = "0.49.2" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ "bitflags 1.0.4 (registry+https://github.com/rust-lang/crates.io-index)", "cexpr 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)", "cfg-if 0.1.9 (registry+https://github.com/rust-lang/crates.io-index)", - "clang-sys 0.26.4 (registry+https://github.com/rust-lang/crates.io-index)", + "clang-sys 0.28.0 (registry+https://github.com/rust-lang/crates.io-index)", "clap 2.33.0 (registry+https://github.com/rust-lang/crates.io-index)", "env_logger 0.6.1 (registry+https://github.com/rust-lang/crates.io-index)", - "hashbrown 0.1.8 (registry+https://github.com/rust-lang/crates.io-index)", + "fxhash 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)", "lazy_static 1.2.0 (registry+https://github.com/rust-lang/crates.io-index)", "log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)", "peeking_take_while 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)", "proc-macro2 0.4.27 (registry+https://github.com/rust-lang/crates.io-index)", "quote 0.6.12 (registry+https://github.com/rust-lang/crates.io-index)", "regex 1.1.5 (registry+https://github.com/rust-lang/crates.io-index)", + "shlex 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)", "which 2.0.1 (registry+https://github.com/rust-lang/crates.io-index)", ] @@ -251,7 +252,6 @@ dependencies = [ "bin-tools 0.1.0", "byteorder 1.3.1 (registry+https://github.com/rust-lang/crates.io-index)", "chrono 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)", - "common 0.1.0", "consensus 0.1.0", "crypto 0.1.0", "elastic-array 0.10.2 (registry+https://github.com/rust-lang/crates.io-index)", @@ -282,7 +282,7 @@ dependencies = [ [[package]] name = "clang-sys" -version = "0.26.4" +version = "0.28.0" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ "glob 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)", @@ -320,15 +320,6 @@ dependencies = [ "cc 1.0.35 (registry+https://github.com/rust-lang/crates.io-index)", ] -[[package]] -name = "common" -version = "0.1.0" -dependencies = [ - "hashbrown 0.1.7 (git+https://github.com/octavonce/hashbrown)", - "lazy_static 1.2.0 (registry+https://github.com/rust-lang/crates.io-index)", - "parking_lot 0.7.1 (registry+https://github.com/rust-lang/crates.io-index)", -] - [[package]] name = "consensus" version = "0.1.0" @@ -756,6 +747,14 @@ name = "futures" version = "0.1.26" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "fxhash" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "byteorder 1.3.1 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "generic-array" version = "0.9.0" @@ -769,6 +768,11 @@ name = "glob" version = "0.2.11" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "glob" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" + [[package]] name = "graphlib" version = "0.3.1" @@ -954,12 +958,12 @@ dependencies = [ [[package]] name = "librocksdb-sys" -version = "5.18.3" +version = "6.1.2" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ - "bindgen 0.47.3 (registry+https://github.com/rust-lang/crates.io-index)", + "bindgen 0.49.2 (registry+https://github.com/rust-lang/crates.io-index)", "cc 1.0.35 (registry+https://github.com/rust-lang/crates.io-index)", - "glob 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)", + "glob 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)", "libc 0.2.55 (registry+https://github.com/rust-lang/crates.io-index)", ] @@ -1156,7 +1160,6 @@ dependencies = [ "byteorder 1.3.1 (registry+https://github.com/rust-lang/crates.io-index)", "chain 0.1.0", "chrono 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)", - "common 0.1.0", "crypto 0.1.0", "env_logger 0.6.1 (registry+https://github.com/rust-lang/crates.io-index)", "futures 0.1.26 (registry+https://github.com/rust-lang/crates.io-index)", @@ -1416,7 +1419,6 @@ dependencies = [ "bin-tools 0.1.0", "byteorder 1.3.1 (registry+https://github.com/rust-lang/crates.io-index)", "cfg-if 0.1.9 (registry+https://github.com/rust-lang/crates.io-index)", - "common 0.1.0", "criterion 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)", "crypto 0.1.0", "elastic-array 0.10.2 (registry+https://github.com/rust-lang/crates.io-index)", @@ -1430,7 +1432,7 @@ dependencies = [ "patricia-trie 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)", "rand 0.6.5 (registry+https://github.com/rust-lang/crates.io-index)", "rlp 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)", - "rocksdb 0.12.2 (registry+https://github.com/rust-lang/crates.io-index)", + "rocksdb 0.12.3 (registry+https://github.com/rust-lang/crates.io-index)", "tempdir 0.3.7 (registry+https://github.com/rust-lang/crates.io-index)", ] @@ -1459,7 +1461,6 @@ dependencies = [ "account 0.1.0", "chain 0.1.0", "clap 2.33.0 (registry+https://github.com/rust-lang/crates.io-index)", - "common 0.1.0", "consensus 0.1.0", "crypto 0.1.0", "dirs 1.0.5 (registry+https://github.com/rust-lang/crates.io-index)", @@ -1480,7 +1481,7 @@ dependencies = [ "parking_lot 0.7.1 (registry+https://github.com/rust-lang/crates.io-index)", "persistence 0.1.0", "purple_vm 0.1.0", - "rocksdb 0.12.2 (registry+https://github.com/rust-lang/crates.io-index)", + "rocksdb 0.12.3 (registry+https://github.com/rust-lang/crates.io-index)", "tokio 0.1.18 (registry+https://github.com/rust-lang/crates.io-index)", "transactions 0.1.0", "unwrap 1.2.1 (registry+https://github.com/rust-lang/crates.io-index)", @@ -1770,11 +1771,11 @@ dependencies = [ [[package]] name = "rocksdb" -version = "0.12.2" +version = "0.12.3" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ "libc 0.2.55 (registry+https://github.com/rust-lang/crates.io-index)", - "librocksdb-sys 5.18.3 (registry+https://github.com/rust-lang/crates.io-index)", + "librocksdb-sys 6.1.2 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] @@ -1951,6 +1952,11 @@ dependencies = [ "fake-simd 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "shlex" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" + [[package]] name = "slab" version = "0.4.2" @@ -2307,7 +2313,7 @@ dependencies = [ "quickcheck 0.7.2 (registry+https://github.com/rust-lang/crates.io-index)", "rand 0.6.5 (registry+https://github.com/rust-lang/crates.io-index)", "rlp 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)", - "rocksdb 0.12.2 (registry+https://github.com/rust-lang/crates.io-index)", + "rocksdb 0.12.3 (registry+https://github.com/rust-lang/crates.io-index)", "rust_decimal 0.9.1 (registry+https://github.com/rust-lang/crates.io-index)", "serde 1.0.91 (registry+https://github.com/rust-lang/crates.io-index)", "serde_derive 1.0.90 (registry+https://github.com/rust-lang/crates.io-index)", @@ -2479,7 +2485,7 @@ dependencies = [ "checksum autocfg 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)" = "a6d640bee2da49f60a4068a7fae53acde8982514ab7bae8b8cea9e88cbcfd799" "checksum backtrace 0.3.15 (registry+https://github.com/rust-lang/crates.io-index)" = "f106c02a3604afcdc0df5d36cc47b44b55917dbaf3d808f71c163a0ddba64637" "checksum backtrace-sys 0.1.28 (registry+https://github.com/rust-lang/crates.io-index)" = "797c830ac25ccc92a7f8a7b9862bde440715531514594a6154e3d4a54dd769b6" -"checksum bindgen 0.47.3 (registry+https://github.com/rust-lang/crates.io-index)" = "df683a55b54b41d5ea8ebfaebb5aa7e6b84e3f3006a78f010dadc9ca88469260" +"checksum bindgen 0.49.2 (registry+https://github.com/rust-lang/crates.io-index)" = "846a1fba6535362a01487ef6b10f0275faa12e5c5d835c5c1c627aabc46ccbd6" "checksum bitflags 1.0.4 (registry+https://github.com/rust-lang/crates.io-index)" = "228047a76f468627ca71776ecdebd732a3423081fcf5125585bcd7c49886ce12" "checksum bitvec 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)" = "cfadef5c4e2c2e64067b9ecc061179f12ac7ec65ba613b1f60f3972bbada1f5b" "checksum blake2-rfc 0.2.18 (registry+https://github.com/rust-lang/crates.io-index)" = "5d6d530bdd2d52966a6d03b7a964add7ae1a288d25214066fd4b600f0f796400" @@ -2495,7 +2501,7 @@ dependencies = [ "checksum cexpr 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)" = "a7fa24eb00d5ffab90eaeaf1092ac85c04c64aaf358ea6f84505b8116d24c6af" "checksum cfg-if 0.1.9 (registry+https://github.com/rust-lang/crates.io-index)" = "b486ce3ccf7ffd79fdeb678eac06a9e6c09fc88d33836340becb8fffe87c5e33" "checksum chrono 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)" = "45912881121cb26fad7c38c17ba7daa18764771836b34fab7d3fbd93ed633878" -"checksum clang-sys 0.26.4 (registry+https://github.com/rust-lang/crates.io-index)" = "6ef0c1bcf2e99c649104bd7a7012d8f8802684400e03db0ec0af48583c6fa0e4" +"checksum clang-sys 0.28.0 (registry+https://github.com/rust-lang/crates.io-index)" = "4227269cec09f5f83ff160be12a1e9b0262dd1aa305302d5ba296c2ebd291055" "checksum clap 2.33.0 (registry+https://github.com/rust-lang/crates.io-index)" = "5067f5bb2d80ef5d68b4c87db81601f0b75bca627bc2ef76b141d7b846a3c6d9" "checksum cloudabi 0.0.3 (registry+https://github.com/rust-lang/crates.io-index)" = "ddfc5b9aa5d4507acaf872de71051dfd0e309860e88966e1051e462a077aac4f" "checksum cmake 0.1.38 (registry+https://github.com/rust-lang/crates.io-index)" = "96210eec534fc3fbfc0452a63769424eaa80205fda6cea98e5b61cb3d97bcec8" @@ -2540,8 +2546,10 @@ dependencies = [ "checksum fuchsia-zircon 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)" = "2e9763c69ebaae630ba35f74888db465e49e259ba1bc0eda7d06f4a067615d82" "checksum fuchsia-zircon-sys 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)" = "3dcaa9ae7725d12cdb85b3ad99a434db70b468c09ded17e012d86b5c1010f7a7" "checksum futures 0.1.26 (registry+https://github.com/rust-lang/crates.io-index)" = "62941eff9507c8177d448bd83a44d9b9760856e184081d8cd79ba9f03dd24981" +"checksum fxhash 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "c31b6d751ae2c7f11320402d34e41349dd1016f8d5d45e48c4312bc8625af50c" "checksum generic-array 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)" = "ef25c5683767570c2bbd7deba372926a55eaae9982d7726ee2a1050239d45b9d" "checksum glob 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)" = "8be18de09a56b60ed0edf84bc9df007e30040691af7acd1c41874faac5895bfb" +"checksum glob 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)" = "9b919933a397b79c37e33b77bb2aa3dc8eb6e165ad809e58ff75bc7db2e34574" "checksum graphlib 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)" = "83f53d0fa782253a8308ee6fe0edd8154a17d70e8479f089073f2665ca1f050e" "checksum hashbrown 0.1.7 (git+https://github.com/octavonce/hashbrown)" = "" "checksum hashbrown 0.1.8 (registry+https://github.com/rust-lang/crates.io-index)" = "3bae29b6653b3412c2e71e9d486db9f9df5d701941d86683005efb9f2d28e3da" @@ -2564,7 +2572,7 @@ dependencies = [ "checksum libc 0.2.55 (registry+https://github.com/rust-lang/crates.io-index)" = "42914d39aad277d9e176efbdad68acb1d5443ab65afe0e0e4f0d49352a950880" "checksum libloading 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)" = "9c3ad660d7cb8c5822cd83d10897b0f1f1526792737a179e73896152f85b88c2" "checksum libmimalloc-sys 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)" = "bde2b342e3e5be92685862146326ad5749f64906bb4b69e744ba57fe1dc8916b" -"checksum librocksdb-sys 5.18.3 (registry+https://github.com/rust-lang/crates.io-index)" = "d19778314deaa7048f2ea7d07b8aa12e1c227acebe975a37eeab6d2f8c74e41b" +"checksum librocksdb-sys 6.1.2 (registry+https://github.com/rust-lang/crates.io-index)" = "a6af56e6599bce586321e8ba8acf8a0a5e97431fd9ab49f9b69f92d93fe642c6" "checksum lock_api 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)" = "62ebf1391f6acad60e5c8b43706dde4582df75c06698ab44511d15016bc2442c" "checksum log 0.3.9 (registry+https://github.com/rust-lang/crates.io-index)" = "e19e8d5c34a3e0e2223db8e060f9e8264aeeb5c5fc64a4ee9965c062211c024b" "checksum log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)" = "c84ec4b527950aa83a329754b01dbe3f58361d1c5efacd1f6d68c494d08a17c6" @@ -2637,7 +2645,7 @@ dependencies = [ "checksum regex-syntax 0.6.6 (registry+https://github.com/rust-lang/crates.io-index)" = "dcfd8681eebe297b81d98498869d4aae052137651ad7b96822f09ceb690d0a96" "checksum remove_dir_all 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)" = "3488ba1b9a2084d38645c4c08276a1752dcbf2c7130d74f1569681ad5d2799c5" "checksum rlp 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)" = "16d1effe9845d54f90e7be8420ee49e5c94623140b97ee4bc6fb5bfddb745720" -"checksum rocksdb 0.12.2 (registry+https://github.com/rust-lang/crates.io-index)" = "d29e12aab379a49bfbca337132440be73d1de6f328d5635641c2b28ac9dfe514" +"checksum rocksdb 0.12.3 (registry+https://github.com/rust-lang/crates.io-index)" = "2e7523c32e26bf2ebc4540645961dafcbd086c652e8ecb563a507f432eb7636d" "checksum rust-base58 0.0.4 (registry+https://github.com/rust-lang/crates.io-index)" = "b313b91fcdc6719ad41fa2dad2b7e810b03833fae4bf911950e15529a5f04439" "checksum rust_decimal 0.9.1 (registry+https://github.com/rust-lang/crates.io-index)" = "460a9f4908697f83b2198dcbf27dc18e9bdfa9cdc5ecfacb2cd3ad1b97501260" "checksum rust_sodium 0.10.2 (git+https://github.com/octavonce/rust_sodium)" = "" @@ -2659,6 +2667,7 @@ dependencies = [ "checksum serde_derive 1.0.90 (registry+https://github.com/rust-lang/crates.io-index)" = "58fc82bec244f168b23d1963b45c8bf5726e9a15a9d146a067f9081aeed2de79" "checksum serde_json 1.0.39 (registry+https://github.com/rust-lang/crates.io-index)" = "5a23aa71d4a4d43fdbfaac00eff68ba8a06a51759a89ac3304323e800c4dd40d" "checksum sha2 0.7.1 (registry+https://github.com/rust-lang/crates.io-index)" = "9eb6be24e4c23a84d7184280d2722f7f2731fcdd4a9d886efbfe4413e4847ea0" +"checksum shlex 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "7fdf1b9db47230893d76faad238fd6097fd6d6a9245cd7a4d90dbd639536bbd2" "checksum slab 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)" = "c111b5bd5695e56cffe5129854aa230b39c93a305372fdbb2668ca2394eea9f8" "checksum smallvec 0.6.9 (registry+https://github.com/rust-lang/crates.io-index)" = "c4488ae950c49d403731982257768f48fada354a5203fe81f9bb6f43ca9002be" "checksum stable_deref_trait 1.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "dba1a27d3efae4351c8051072d619e3ade2820635c3958d826bfea39d59b54c8" diff --git a/Cargo.toml b/Cargo.toml index e05b4603..15abb15a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -9,7 +9,6 @@ members = [ "src/account", "src/causality", "src/chain", - "src/common", "src/consensus", "src/crypto", "src/events", @@ -46,7 +45,6 @@ consensus = { path = "src/consensus" } persistence = { path = "src/persistence" } network = { path = "src/network" } transactions = { path = "src/transactions" } -common = { path = "src/common" } chain = { path = "src/chain" } miner = { path = "src/miner" } mimalloc = "0.1.5" diff --git a/src/chain/Cargo.toml b/src/chain/Cargo.toml index cd23e93c..6b1d1dd3 100644 --- a/src/chain/Cargo.toml +++ b/src/chain/Cargo.toml @@ -17,7 +17,6 @@ patricia-trie = "0.3.0" account = { path = "../account" } crypto = { path = "../crypto" } events = { path = "../events" } -common = { path = "../common" } consensus = { path = "../consensus" } persistence = { path = "../persistence" } bin-tools = { path = "../util/bin-tools/" } diff --git a/src/chain/src/block.rs b/src/chain/src/block.rs index e23efcbd..bdc188ff 100644 --- a/src/chain/src/block.rs +++ b/src/chain/src/block.rs @@ -17,7 +17,6 @@ */ use crate::{ChainErr, EasyBlock, HardBlock, StateBlock}; -use common::Checkpointable; use chrono::prelude::*; use crypto::Hash; use std::boxed::Box; @@ -29,7 +28,7 @@ use std::hash::Hash as HashTrait; /// Generic block interface pub trait Block: Debug + PartialEq + Eq + HashTrait { /// Per tip validation state - type ChainState: Clone + Checkpointable + Debug; + type ChainState: Clone + Debug; /// Size of the block cache. const BLOCK_CACHE_SIZE: usize = 20; diff --git a/src/chain/src/chain.rs b/src/chain/src/chain.rs index 2c17292a..201f36f7 100644 --- a/src/chain/src/chain.rs +++ b/src/chain/src/chain.rs @@ -18,7 +18,6 @@ use crate::block::Block; use crate::orphan_type::OrphanType; -use common::checkpointable::*; use bin_tools::*; use crypto::Hash; use elastic_array::ElasticArray128; @@ -1784,7 +1783,7 @@ mod tests { } impl Block for DummyBlock { - type ChainState = DummyCheckpoint; + type ChainState = (); fn genesis() -> Arc { let genesis = DummyBlock { @@ -1797,8 +1796,8 @@ mod tests { Arc::new(genesis) } - fn genesis_state() -> DummyCheckpoint { - DummyCheckpoint::genesis() + fn genesis_state() -> () { + () } fn parent_hash(&self) -> Option { @@ -1884,7 +1883,7 @@ mod tests { #[test] fn it_rewinds_to_genesis() { let db = test_helpers::init_tempdb(); - let mut hard_chain = Chain::::new(db, DummyCheckpoint::genesis(), true); + let mut hard_chain = Chain::::new(db, (), true); let mut A = DummyBlock::new(Some(Hash::NULL), crate::random_socket_addr(), 1); let A = Arc::new(A); @@ -1954,7 +1953,7 @@ mod tests { #[test] fn stages_append_test1() { let db = test_helpers::init_tempdb(); - let mut hard_chain = Chain::::new(db, DummyCheckpoint::genesis(), true); + let mut hard_chain = Chain::::new(db, (), true); let mut A = DummyBlock::new(Some(Hash::NULL), crate::random_socket_addr(), 1); let A = Arc::new(A); @@ -2096,7 +2095,7 @@ mod tests { #[test] fn stages_append_test2() { let db = test_helpers::init_tempdb(); - let mut hard_chain = Chain::::new(db, DummyCheckpoint::genesis(), true); + let mut hard_chain = Chain::::new(db, (), true); let mut A = DummyBlock::new(Some(Hash::NULL), crate::random_socket_addr(), 1); let A = Arc::new(A); @@ -2294,7 +2293,7 @@ mod tests { /// of appended blocks. fn stages_append_test3() { let db = test_helpers::init_tempdb(); - let mut hard_chain = Chain::::new(db, DummyCheckpoint::genesis(), true); + let mut hard_chain = Chain::::new(db, (), true); let mut A = DummyBlock::new(Some(Hash::NULL), crate::random_socket_addr(), 1); let A = Arc::new(A); @@ -3967,7 +3966,7 @@ mod tests { /// tip instead of G at commit hash `d0ad0bd6a7422f6308b96a34a6f7725662c8b7d4`. fn stages_append_test4() { let db = test_helpers::init_tempdb(); - let mut hard_chain = Chain::::new(db, DummyCheckpoint::genesis(), true); + let mut hard_chain = Chain::::new(db, (), true); let mut A = DummyBlock::new(Some(Hash::NULL), crate::random_socket_addr(), 1); let A = Arc::new(A); @@ -6713,7 +6712,7 @@ mod tests { /// G, C', C'', E', C, B', F'', E'', B, A, D', F, D''', E, D, D'', fn stages_append_test5() { let db = test_helpers::init_tempdb(); - let mut chain = Chain::::new(db, DummyCheckpoint::genesis(), true); + let mut chain = Chain::::new(db, (), true); let mut A = DummyBlock::new(Some(Hash::NULL), crate::random_socket_addr(), 1); let A = Arc::new(A); @@ -6817,7 +6816,7 @@ mod tests { /// E', E'', D'', C, D''', F, D, B', C'', E, F'', G, A, C', D', B fn stages_append_test6() { let db = test_helpers::init_tempdb(); - let mut chain = Chain::::new(db, DummyCheckpoint::genesis(), true); + let mut chain = Chain::::new(db, (), true); let mut A = DummyBlock::new(Some(Hash::NULL), crate::random_socket_addr(), 1); let A = Arc::new(A); @@ -6916,7 +6915,7 @@ mod tests { /// E, D''', D', A, B, F'', E'', C, F, C'', D'', G, C', E', D, B' fn stages_append_test7() { let db = test_helpers::init_tempdb(); - let mut chain = Chain::::new(db, DummyCheckpoint::genesis(), true); + let mut chain = Chain::::new(db, (), true); let mut A = DummyBlock::new(Some(Hash::NULL), crate::random_socket_addr(), 1); let A = Arc::new(A); @@ -7015,7 +7014,7 @@ mod tests { /// E, D'', D, A, C'', F'', G, E'', C, B, C', D''', E', F, B', D', fn stages_append_test8() { let db = test_helpers::init_tempdb(); - let mut chain = Chain::::new(db, DummyCheckpoint::genesis(), true); + let mut chain = Chain::::new(db, (), true); let mut A = DummyBlock::new(Some(Hash::NULL), crate::random_socket_addr(), 1); let A = Arc::new(A); @@ -7259,7 +7258,7 @@ mod tests { /// the height of the chain must be that of `G` which is 7. fn append_stress_test() -> bool { let db = test_helpers::init_tempdb(); - let mut hard_chain = Chain::::new(db, DummyCheckpoint::genesis(), true); + let mut hard_chain = Chain::::new(db, (), true); let mut A = DummyBlock::new(Some(Hash::NULL), crate::random_socket_addr(), 1); let A = Arc::new(A); @@ -7392,7 +7391,7 @@ mod tests { fn it_rewinds_correctly1() -> bool { let db = test_helpers::init_tempdb(); - let mut hard_chain = Chain::::new(db, DummyCheckpoint::genesis(), true); + let mut hard_chain = Chain::::new(db, (), true); let mut A = DummyBlock::new(Some(Hash::NULL), crate::random_socket_addr(), 1); let A = Arc::new(A); @@ -7467,7 +7466,7 @@ mod tests { fn it_rewinds_correctly2() -> bool { let db = test_helpers::init_tempdb(); - let mut hard_chain = Chain::::new(db, DummyCheckpoint::genesis(), true); + let mut hard_chain = Chain::::new(db, (), true); let mut A = DummyBlock::new(Some(Hash::NULL), crate::random_socket_addr(), 1); let A = Arc::new(A); diff --git a/src/chain/src/easy_chain/block.rs b/src/chain/src/easy_chain/block.rs index d27dbb09..4e45863c 100644 --- a/src/chain/src/easy_chain/block.rs +++ b/src/chain/src/easy_chain/block.rs @@ -18,7 +18,6 @@ use crate::block::Block; use crate::chain::ChainErr; -use common::checkpointable::*; use bin_tools::*; use account::NormalAddress; use crypto::PublicKey; @@ -95,14 +94,14 @@ impl HashTrait for EasyBlock { } impl Block for EasyBlock { - type ChainState = DummyCheckpoint; + type ChainState = (); fn genesis() -> Arc { GENESIS_RC.clone() } - fn genesis_state() -> DummyCheckpoint { - DummyCheckpoint::genesis() + fn genesis_state() -> () { + () } fn height(&self) -> u64 { diff --git a/src/chain/src/hard_chain/block.rs b/src/chain/src/hard_chain/block.rs index 896805a3..d29c16cd 100644 --- a/src/chain/src/hard_chain/block.rs +++ b/src/chain/src/hard_chain/block.rs @@ -19,7 +19,6 @@ use crate::block::Block; use crate::chain::ChainErr; use crate::easy_chain::block::EasyBlock; -use common::checkpointable::*; use account::NormalAddress; use crypto::PublicKey; use bin_tools::*; @@ -105,14 +104,14 @@ impl HashTrait for HardBlock { } impl Block for HardBlock { - type ChainState = DummyCheckpoint; + type ChainState = (); fn genesis() -> Arc { GENESIS_RC.clone() } - fn genesis_state() -> DummyCheckpoint { - DummyCheckpoint::genesis() + fn genesis_state() -> () { + () } fn height(&self) -> u64 { diff --git a/src/chain/src/lib.rs b/src/chain/src/lib.rs index 4c9c8b23..00160074 100644 --- a/src/chain/src/lib.rs +++ b/src/chain/src/lib.rs @@ -34,7 +34,6 @@ pub use hard_chain::chain::*; pub use state_chain::block::*; pub use state_chain::chain::*; pub use state_chain::state::*; -pub use common::checkpointable::*; #[cfg(test)] use std::net::{SocketAddr, IpAddr, Ipv4Addr}; diff --git a/src/chain/src/state_chain/state.rs b/src/chain/src/state_chain/state.rs index 75b3c9df..b4c6d833 100644 --- a/src/chain/src/state_chain/state.rs +++ b/src/chain/src/state_chain/state.rs @@ -16,8 +16,7 @@ along with the Purple Library. If not, see . */ -use persistence::{PersistentDb, STATE_REGISTRY}; -use common::{StorageLocation, Checkpointable}; +use persistence::PersistentDb; use consensus::PoolState; /// Wrapper over the `StateChain` associated chain state. @@ -49,96 +48,4 @@ impl ChainState { pub fn make_canonical(&mut self) -> Result<(), ()> { self.db.make_canonical() } -} - -impl Checkpointable for ChainState { - 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> { - #[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<(), ()> { - #[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 { - let database = { - #[cfg(not(feature = "test"))] - { - let registry = STATE_REGISTRY.lock(); - registry.load_from_disk(id)? - } - - #[cfg(feature = "test")] - { - STATE_REGISTRY.with(|registry| { - let registry = registry.lock(); - registry.load_from_disk(id) - })? - } - }; - - Ok(ChainState::new(database)) - } - - fn storage_location(&self) -> StorageLocation { - if self.db.memory_db.is_empty() { - StorageLocation::Disk - } else { - StorageLocation::Memory - } - } - - fn make_canonical(old_state: &Self, mut new_state: Self) -> Self { - assert!(old_state.is_canonical()); - - if new_state.is_canonical() { - // Just flush changes if both states are canonical - new_state.db.flush(); - new_state - } else { - new_state.make_canonical().unwrap(); - new_state.db.flush(); - new_state - } - } } \ No newline at end of file diff --git a/src/common/Cargo.toml b/src/common/Cargo.toml deleted file mode 100644 index e0b7b937..00000000 --- a/src/common/Cargo.toml +++ /dev/null @@ -1,10 +0,0 @@ -[package] -name = "common" -version = "0.1.0" -authors = ["Octavian Oncescu "] -edition = "2018" - -[dependencies] -parking_lot = "0.7.1" -lazy_static = "1.2.0" -hashbrown = { git = "https://github.com/octavonce/hashbrown", features = ["serde", "rayon"] } diff --git a/src/common/src/checkpointable.rs b/src/common/src/checkpointable.rs deleted file mode 100644 index 3f4ecae5..00000000 --- a/src/common/src/checkpointable.rs +++ /dev/null @@ -1,170 +0,0 @@ -/* - Copyright 2018 The Purple Library Authors - This file is part of the Purple Library. - - The Purple Library is free software: you can redistribute it and/or modify - it under the terms of the GNU General Public License as published by - the Free Software Foundation, either version 3 of the License, or - (at your option) any later version. - - The Purple Library is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - - You should have received a copy of the GNU General Public License - along with the Purple Library. If not, see . -*/ - -use lazy_static::*; -use hashbrown::HashMap; -use parking_lot::Mutex; -use std::fmt::Debug; -use std::sync::Arc; -use std::sync::atomic::{Ordering, AtomicUsize}; - -#[derive(Clone, Copy, Debug, PartialEq)] -pub enum StorageLocation { - Disk, - Memory -} - -/// Trait for any state that can be checkpointed -/// and that can be reloaded from checkpoints. -pub trait Checkpointable: Sized + Debug + Clone { - /// Creates a checkpoint of the current state and - /// returns the checkpoint id associated with it. - 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>; - - /// Deletes the checkpoint with the given id - fn delete_checkpoint(id: u64) -> Result<(), ()>; - - /// Reloads the state from the disk checkpoint with the given id. - fn load_from_disk(id: u64) -> Result; - - /// Returns the storage location of the checkpoint. - fn storage_location(&self) -> StorageLocation; - - /// Receives the old canonical state and a checkpoint state. This - /// function is responsible for doing any additional modifications - /// in order for the new state to become canonical. - /// - /// Returns the final canonical state. - fn make_canonical(old_state: &Self, new_state: Self) -> Self; -} - -lazy_static! { - static ref CHECKPOINT_ID: AtomicUsize = AtomicUsize::new(1); - static ref BACKEND_ID: AtomicUsize = AtomicUsize::new(0); - static ref DUMMY_BACKEND: Mutex>> = Mutex::new(HashMap::new()); -} - -#[derive(Debug)] -/// Placeholder checkpoint type -pub struct DummyCheckpoint { - id: Arc>, - backend_id: u64, - location: Arc>, - height: Arc>, -} - -impl Clone for DummyCheckpoint { - fn clone(&self) -> Self { - let id = self.id.lock(); - let location = self.location.lock(); - let height = self.height.lock(); - - DummyCheckpoint { - id: Arc::new(Mutex::new(id.clone())), - backend_id: self.backend_id, - location: Arc::new(Mutex::new(location.clone())), - height: Arc::new(Mutex::new(height.clone())), - } - } -} - -impl DummyCheckpoint { - pub fn new(location: StorageLocation, height: u64, backend_id: u64) -> DummyCheckpoint { - let id = CHECKPOINT_ID.fetch_add(1, Ordering::Relaxed) as u64; - DummyCheckpoint { - location: Arc::new(Mutex::new(location)), - height: Arc::new(Mutex::new(height)), - id: Arc::new(Mutex::new(id)), - backend_id, - } - } - - pub fn genesis() -> DummyCheckpoint { - DummyCheckpoint::new(StorageLocation::Disk, 0, BACKEND_ID.fetch_add(1, Ordering::Relaxed) as u64) - } - - pub fn increment(&mut self) { - let mut height = self.height.lock(); - let mut location = self.location.lock(); - - *height += 1; - *location = StorageLocation::Memory; - } - - pub fn height(&self) -> u64 { - let height = self.height.lock(); - height.clone() - } - - pub fn clear_checkpoints() { - let mut db = DUMMY_BACKEND.lock(); - db.clear(); - } -} - -impl Checkpointable for DummyCheckpoint { - 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; - *location = StorageLocation::Disk; - - let mut db = DUMMY_BACKEND.lock(); - let mut db = db.get_mut(&self.backend_id).unwrap(); - db.insert(id.clone(), self.clone()); - - id.clone() - } - - fn fetch_existing_checkpoints() -> Option> { - None - } - - fn delete_checkpoint(id: u64) -> Result<(), ()> { - let mut db = DUMMY_BACKEND.lock(); - let mut db = db.get_mut(&id).unwrap(); - - if let Some(_) = db.remove(&id) { - Ok(()) - } else { - Err(()) - } - } - - fn load_from_disk(id: u64) -> Result { - let db = DUMMY_BACKEND.lock(); - let db = db.get(&id).unwrap(); - - if let Some(result) = db.get(&id) { - Ok(result.clone()) - } else { - Err(()) - } - } - - fn storage_location(&self) -> StorageLocation { - let location = self.location.lock(); - location.clone() - } - - fn make_canonical(_old_state: &Self, new_state: Self) -> Self { new_state } -} \ No newline at end of file diff --git a/src/common/src/lib.rs b/src/common/src/lib.rs deleted file mode 100644 index 8346881b..00000000 --- a/src/common/src/lib.rs +++ /dev/null @@ -1,21 +0,0 @@ -/* - Copyright 2018 The Purple Library Authors - This file is part of the Purple Library. - - The Purple Library is free software: you can redistribute it and/or modify - it under the terms of the GNU General Public License as published by - the Free Software Foundation, either version 3 of the License, or - (at your option) any later version. - - The Purple Library is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - - You should have received a copy of the GNU General Public License - along with the Purple Library. If not, see . -*/ - -pub mod checkpointable; - -pub use checkpointable::*; \ No newline at end of file diff --git a/src/network/Cargo.toml b/src/network/Cargo.toml index ef2f7230..c62ab343 100644 --- a/src/network/Cargo.toml +++ b/src/network/Cargo.toml @@ -28,5 +28,4 @@ hashbrown = { git = "https://github.com/octavonce/hashbrown", features = ["rayon [dev-dependencies] tempdir = "0.3.7" -common = { path = "../common" } test-helpers = { path = "../util/test-helpers" } \ No newline at end of file diff --git a/src/network/src/lib.rs b/src/network/src/lib.rs index a5f0e37d..8805cb09 100644 --- a/src/network/src/lib.rs +++ b/src/network/src/lib.rs @@ -23,9 +23,6 @@ extern crate quickcheck; #[macro_use] extern crate log; -#[cfg(test)] -extern crate common as other_common; - #[cfg(test)] extern crate tempdir; @@ -75,9 +72,6 @@ use tempdir::TempDir; #[cfg(test)] use std::thread; -#[cfg(test)] -use other_common::checkpointable::*; - #[cfg(test)] use crypto::NodeId; diff --git a/src/persistence/Cargo.toml b/src/persistence/Cargo.toml index a29a721e..2356f50f 100644 --- a/src/persistence/Cargo.toml +++ b/src/persistence/Cargo.toml @@ -11,7 +11,6 @@ elastic-array = "0.10.0" rlp = "0.3.0" parking_lot = "0.7.1" rocksdb = "0.12.2" -common = { path = "../common" } num_cpus = "1.10.1" lazy_static = "1.2.0" hashbrown = { git = "https://github.com/octavonce/hashbrown", features = ["serde", "rayon"] } diff --git a/src/persistence/src/init.rs b/src/persistence/src/init.rs deleted file mode 100644 index 7bf16b60..00000000 --- a/src/persistence/src/init.rs +++ /dev/null @@ -1,115 +0,0 @@ -/* - Copyright 2018 The Purple Library Authors - This file is part of the Purple Library. - - The Purple Library is free software: you can redistribute it and/or modify - it under the terms of the GNU General Public License as published by - the Free Software Foundation, either version 3 of the License, or - (at your option) any later version. - - The Purple Library is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - - You should have received a copy of the GNU General Public License - along with the Purple Library. If not, see . -*/ - -use crate::persistent_db::PersistentDb; -use std::sync::Arc; -use std::path::PathBuf; -use std::sync::atomic::{AtomicBool, Ordering}; - -// Use thread_local on `cfg(test)`. -cfg_if! { - if #[cfg(feature = "test")] { - use std::cell::RefCell; - - thread_local! { - static INITIALIZED: RefCell = RefCell::new(false); - - /// Directory containing checkpoints - pub(crate) static WORKING_DIR: RefCell> = RefCell::new(None); - - /// Internal database - pub(crate) static REGISTRY_DB: RefCell> = RefCell::new(None); - } - } else { - static INITIALIZED: AtomicBool = AtomicBool::new(false); - - /// Directory containing checkpoints - pub(crate) static mut WORKING_DIR: Option = None; - - /// Internal database - pub(crate) static mut REGISTRY_DB: Option = None; - } -} - -/// This function must be called in `fn main()` at the beginning -/// in order to initialize database paths. Note that this function -/// **IS NOT** thread-safe. -#[cfg(not(feature = "test"))] -pub fn init(working_dir: PathBuf) { - if INITIALIZED.load(Ordering::Relaxed) { - panic!("Already called init! This function can only be called once!"); - } - - unsafe { - let registry_db_path = working_dir.join("registry_db"); - let registry_db = crate::open_database_no_checks(®istry_db_path); - let registry_db = PersistentDb::new_without_checks(Arc::new(registry_db), None); - - // Set registry db - REGISTRY_DB = Some(registry_db); - - // Set working dir - WORKING_DIR = Some(working_dir); - } - - // Flag as initialized - INITIALIZED.store(true, Ordering::Relaxed); -} - -#[cfg(feature = "test")] -pub fn init(working_dir: PathBuf) { - if !is_initialized() { - // Set initialized flag - INITIALIZED.with(|initialized| { - let mut initialized = initialized.borrow_mut(); - *initialized = true; - }); - } else { - panic!("Already called init! This function can only be called once!"); - } - - let registry_db_path = working_dir.join("registry_db"); - let registry_wal_path = working_dir.join("registry_db_wal"); - let registry_db = crate::open_database_no_checks(®istry_db_path, ®istry_wal_path); - let registry_db = PersistentDb::new_without_checks(Arc::new(registry_db), None); - - // Set registry db - REGISTRY_DB.with(|db_ref| { - *db_ref.borrow_mut() = Some(registry_db); - }); - - // Set working dir - WORKING_DIR.with(|dir_ref| { - *dir_ref.borrow_mut() = Some(working_dir); - }); -} - -/// Returns `true` if the persistence module is initialized -#[cfg(not(feature = "test"))] -pub fn is_initialized() -> bool { - unsafe { - INITIALIZED.load(Ordering::Relaxed) && WORKING_DIR.is_some() && REGISTRY_DB.is_some() - } -} - -#[cfg(feature = "test")] -pub fn is_initialized() -> bool { - INITIALIZED.with(|initialized| { - initialized.borrow().clone() - }) -} \ No newline at end of file diff --git a/src/persistence/src/lib.rs b/src/persistence/src/lib.rs index de949b71..e6eb1853 100644 --- a/src/persistence/src/lib.rs +++ b/src/persistence/src/lib.rs @@ -27,11 +27,7 @@ use std::path::{Path, PathBuf}; use rocksdb::DB; pub fn open_database(path: &PathBuf, wal_path: &Path) -> DB { - DB::open(&crate::db_options(wal_path), path.to_str().unwrap()).unwrap() -} - -pub(crate) fn open_database_no_checks(path: &PathBuf, wal_path: &Path) -> DB { - DB::open(&crate::db_options_no_checks(wal_path), path.to_str().unwrap()).unwrap() + DB::open(&crate::db_options(wal_path), path.to_str().unwrap()).unwrap() } #[cfg(test)] @@ -42,7 +38,6 @@ extern crate log; #[macro_use] extern crate lazy_static; -extern crate common; extern crate crypto; extern crate elastic_array; extern crate hashbrown; @@ -56,11 +51,7 @@ extern crate rocksdb; pub use hasher::*; pub use node_codec::*; pub use persistent_db::*; -pub use state_registry::*; -pub use init::*; mod hasher; mod node_codec; -mod persistent_db; -mod state_registry; -mod init; \ No newline at end of file +mod persistent_db; \ No newline at end of file diff --git a/src/persistence/src/persistent_db.rs b/src/persistence/src/persistent_db.rs index 6c46e671..e41c1c52 100644 --- a/src/persistence/src/persistent_db.rs +++ b/src/persistence/src/persistent_db.rs @@ -15,10 +15,6 @@ You should have received a copy of the GNU General Public License along with the Purple Library. If not, see . */ - -use crate::init::*; -use crate::state_registry::STATE_REGISTRY; -use common::{Checkpointable, StorageLocation}; use crypto::Hash; use elastic_array::ElasticArray128; use hashbrown::HashMap; @@ -36,34 +32,6 @@ pub fn cf_options() -> Options { } pub fn db_options(wal_dir: &Path) -> Options { - if !is_initialized() { - panic!("Persistence module not initialized! Call `persistence::init()` before using anything"); - } - - let mut opts = Options::default(); - opts.set_wal_dir(wal_dir); - opts.increase_parallelism(num_cpus::get() as i32); - opts.create_if_missing(true); - opts.create_missing_column_families(true); - opts.set_max_open_files(10000); - opts.set_use_fsync(false); - opts.set_bytes_per_sync(8388608); - opts.optimize_for_point_lookup(1024); - opts.set_table_cache_num_shard_bits(6); - opts.set_max_write_buffer_number(32); - opts.set_write_buffer_size(536870912); - opts.set_target_file_size_base(1073741824); - opts.set_min_write_buffer_number_to_merge(4); - opts.set_level_zero_stop_writes_trigger(2000); - opts.set_level_zero_slowdown_writes_trigger(0); - opts.set_compaction_style(DBCompactionStyle::Universal); - opts.set_max_background_compactions(4); - opts.set_max_background_flushes(4); - opts.set_disable_auto_compactions(true); - opts -} - -pub(crate) fn db_options_no_checks(wal_dir: &Path) -> Options { let mut opts = Options::default(); opts.set_wal_dir(wal_dir); opts.increase_parallelism(num_cpus::get() as i32); @@ -114,19 +82,6 @@ impl PersistentDb { pub const ROOT_HASH_KEY: &'static [u8] = b"root_hash"; pub fn new(db_ref: Arc, cf_name: Option<&'static str>) -> PersistentDb { - if !is_initialized() { - panic!("Persistence module not initialized! Call `persistence::init()` before using anything"); - } - - PersistentDb { - db_ref: Some(db_ref), - cf_name, - db_type: None, - memory_db: HashMap::new(), - } - } - - pub(crate) fn new_without_checks(db_ref: Arc, cf_name: Option<&'static str>) -> PersistentDb { PersistentDb { db_ref: Some(db_ref), cf_name, @@ -380,7 +335,6 @@ mod tests { let dir = TempDir::new("purple_test").unwrap(); let path = dir.path().join("database"); let path = path.to_str().unwrap(); - init(dir.path().to_path_buf()); let db = DB::open_default(path).unwrap(); let db_ref = Arc::new(db); let mut persistent_db = PersistentDb::new(db_ref, None); @@ -396,7 +350,6 @@ mod tests { let dir = TempDir::new("purple_test").unwrap(); let path = dir.path().join("database"); let path = path.to_str().unwrap(); - init(dir.path().to_path_buf()); let db = DB::open_default(path).unwrap(); let db_ref = Arc::new(db); let mut persistent_db = PersistentDb::new(db_ref, None); @@ -413,7 +366,6 @@ mod tests { let dir = TempDir::new("purple_test").unwrap(); let path = dir.path().join("database"); let path = path.to_str().unwrap(); - init(dir.path().to_path_buf()); let db = DB::open_default(path).unwrap(); let db_ref = Arc::new(db); let mut persistent_db = PersistentDb::new(db_ref, None); @@ -429,7 +381,6 @@ mod tests { let dir = TempDir::new("purple_test").unwrap(); let path = dir.path().join("database"); let path = path.to_str().unwrap(); - init(dir.path().to_path_buf()); let db = DB::open_default(path).unwrap(); let db_ref = Arc::new(db); let mut persistent_db = PersistentDb::new(db_ref, None); @@ -450,7 +401,6 @@ mod tests { let dir = TempDir::new("purple_test").unwrap(); let path = dir.path().join("database"); let path = path.to_str().unwrap(); - init(dir.path().to_path_buf()); let db = DB::open_default(path).unwrap(); let db_ref = Arc::new(db); let mut persistent_db = PersistentDb::new(db_ref, None); @@ -471,7 +421,6 @@ mod tests { let dir = TempDir::new("purple_test").unwrap(); let path = dir.path().join("database"); let path = path.to_str().unwrap(); - init(dir.path().to_path_buf()); let db = DB::open_default(path).unwrap(); let db_ref = Arc::new(db); let mut persistent_db = PersistentDb::new(db_ref, None); @@ -495,7 +444,6 @@ mod tests { let dir = TempDir::new("purple_test").unwrap(); let path = dir.path().join("database"); let path = path.to_str().unwrap(); - init(dir.path().to_path_buf()); let db = DB::open_default(path).unwrap(); let db_ref = Arc::new(db); let mut persistent_db = PersistentDb::new(db_ref, None); @@ -513,7 +461,6 @@ mod tests { let dir = TempDir::new("purple_test").unwrap(); let path = dir.path().join("database"); let path = path.to_str().unwrap(); - init(dir.path().to_path_buf()); let db = DB::open_default(path).unwrap(); let db_ref = Arc::new(db); let mut persistent_db = PersistentDb::new(db_ref, None); diff --git a/src/persistence/src/state_registry.rs b/src/persistence/src/state_registry.rs deleted file mode 100644 index 8db9ba77..00000000 --- a/src/persistence/src/state_registry.rs +++ /dev/null @@ -1,206 +0,0 @@ -/* - Copyright 2018 The Purple Library Authors - This file is part of the Purple Library. - - The Purple Library is free software: you can redistribute it and/or modify - it under the terms of the GNU General Public License as published by - the Free Software Foundation, either version 3 of the License, or - (at your option) any later version. - - The Purple Library is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - - You should have received a copy of the GNU General Public License - along with the Purple Library. If not, see . -*/ - -use crate::init::*; -use crate::persistent_db::PersistentDb; -use std::sync::Arc; -use std::path::PathBuf; -use byteorder::{BigEndian, ReadBytesExt}; -use rocksdb::DB; -use rocksdb::checkpoint::Checkpoint; -use parking_lot::Mutex; -use std::fs; -use std::io::Cursor; -use rlp::RlpStream; - -#[cfg(not(feature = "test"))] -lazy_static! { - pub static ref STATE_REGISTRY: Mutex = { - if !is_initialized() { - panic!("Persistence module not initialized! Call `persistence::init()` before using anything"); - } - - let working_dir = unsafe { - WORKING_DIR.clone().unwrap() - }; - - Mutex::new(StateRegistry::new(working_dir)) - }; -} - -#[cfg(feature = "test")] -thread_local! { - pub static STATE_REGISTRY: Mutex = { - if !is_initialized() { - panic!("Persistence module not initialized! Call `persistence::init()` before using anything"); - } - - let working_dir = WORKING_DIR.with(|working_dir| working_dir.borrow().clone().unwrap()); - Mutex::new(StateRegistry::new(working_dir)) - }; -} - -/// Registry for ephemeral state db handles. -#[derive(Debug)] -pub struct StateRegistry { - /// The id of the latest snapshot - latest_id: u64, - - /// The internal registry database - inner_db: PersistentDb, - - /// The directory that contains all databases - working_dir: PathBuf, -} - -impl StateRegistry { - const LATEST_ID_KEY: &'static [u8] = b"latest_id"; - const IDS_AND_HEIGHTS_KEY: &'static [u8] = b"ids_and_heights"; - - pub fn new(working_dir: PathBuf) -> Self { - #[cfg(not(feature = "test"))] - let inner_db = unsafe { REGISTRY_DB.as_ref().unwrap().clone() }; - - #[cfg(feature = "test")] - let inner_db = REGISTRY_DB.with(|registry_db| registry_db.borrow().clone().unwrap()); - - let latest_id = if let Some(latest_id) = inner_db.db_ref.as_ref().unwrap().get(Self::LATEST_ID_KEY).unwrap() { - decode_be_u64!(latest_id).unwrap() - } else { - inner_db.db_ref.as_ref().unwrap().put(Self::LATEST_ID_KEY, &[0, 0, 0, 0, 0, 0, 0, 0]).unwrap(); - 0 - }; - - // TODO: When initializing the state registry, - // delete all checkpoints which are not listed - // in the ids and heights entry. - - StateRegistry { - latest_id, - inner_db, - working_dir, - } - } - - /// Returns the listed checkpoints ids and their corresponding heights - pub fn retrieve_ids_and_heights(&self) -> Option> { - let ids_and_heights = self.inner_db.retrieve(Self::IDS_AND_HEIGHTS_KEY)?; - let ids_and_heights = rlp::decode_list::>(&ids_and_heights); - - if ids_and_heights.is_empty() { - return None; - } - - let ids_and_heights = ids_and_heights - .iter() - .map(|encoded| { - let mut cursor = Cursor::new(encoded); - let id = cursor.read_u64::().unwrap(); - let height = cursor.read_u64::().unwrap(); - - (id, height) - }) - .collect(); - - Some(ids_and_heights) - } - - // TODO: Make this async - pub fn delete_checkpoint(&mut self, id: u64) -> Result<(), ()> { - let path = self.working_dir.join(&format!("{}", id)); - let exists = fs::metadata(&path).is_ok(); - - if exists { - let ids_and_heights = self - .retrieve_ids_and_heights() - .unwrap() - .iter() - // Remove entry with given id - .filter(|(i, _)| *i != id) - .cloned() - .collect(); - - let entries = Self::encode_ids_and_heights(ids_and_heights); - - // Update database entries - self.inner_db.put(Self::IDS_AND_HEIGHTS_KEY, &entries); - - // Delete checkpoint dir - fs::remove_dir_all(&path).unwrap(); - Ok(()) - } else { - Err(()) - } - } - - // TODO: Maybe return a `Result` instead of panicking. - pub fn checkpoint(&mut self, db_ref: Arc, height: u64) -> u64 { - let latest_id = if let Some(latest_id) = self.inner_db.retrieve(Self::LATEST_ID_KEY) { - decode_be_u64!(latest_id).unwrap() - } else { - unreachable!(); - }; - - let next_id = latest_id + 1; - let checkpoint = Checkpoint::new(db_ref.as_ref()).unwrap(); - let checkpoint_path = self.working_dir.join(&format!("{}", next_id)); - checkpoint.create_checkpoint(checkpoint_path).unwrap(); - - let ids_and_heights = self.retrieve_ids_and_heights(); - - // Update ids and heights entry - let entries: Vec = if let Some(mut ids_and_heights) = ids_and_heights { - ids_and_heights.push((next_id, height)); - Self::encode_ids_and_heights(ids_and_heights) - } else { - // Create ids and heights entry if it doesn't exist - Self::encode_ids_and_heights(vec![(next_id, height)]) - }; - - // Write entries to db - self.inner_db.put(Self::IDS_AND_HEIGHTS_KEY, &entries); - - // Write next id - self.inner_db.put(Self::LATEST_ID_KEY, &encode_be_u64!(next_id)); - self.latest_id = next_id; - - next_id - } - - pub fn load_from_disk(&self, id: u64) -> Result { - unimplemented!(); - } - - fn encode_ids_and_heights(ids_and_heights: Vec<(u64, u64)>) -> Vec { - let mut rlp = RlpStream::new_list(ids_and_heights.len()); - - // Encode entries - for (id, height) in ids_and_heights { - let mut buf = Vec::with_capacity(16); - let encoded_id = encode_be_u64!(id); - let encoded_height = encode_be_u64!(height); - - buf.extend_from_slice(&encoded_id); - buf.extend_from_slice(&encoded_height); - - rlp.append(&buf); - } - - rlp.out() - } -} \ No newline at end of file diff --git a/src/purple/main.rs b/src/purple/main.rs index 59ed7f64..4588886d 100644 --- a/src/purple/main.rs +++ b/src/purple/main.rs @@ -25,7 +25,6 @@ extern crate jsonrpc_macros; extern crate chain; extern crate clap; -extern crate common; extern crate crypto; extern crate dirs; extern crate elastic_array; @@ -44,7 +43,6 @@ extern crate tokio; use chain::*; use clap::{App, Arg}; -use common::checkpointable::DummyCheckpoint; use crypto::{Identity, NodeId, SecretKey as Sk}; use elastic_array::ElasticArray128; use futures::future::ok;