فهرست منبع

Revert "src/consensus: removed fixed genesis timestamp (if sled db doesn't exists it restarts from current timestamp)"

This reverts commit 425d339c43255105a43f7bbe125570db76e138b9.
aggstam 4 سال پیش
والد
کامیت
4ed5fb001b

+ 3 - 2
script/research/nodes-tool/src/main.rs

@@ -311,12 +311,13 @@ impl StateInfo {
 }
 
 fn main() -> Result<()> {
-    let nodes = 1;
+    let nodes = 4;
+    let genesis = 1648383795;
     for i in 0..nodes {
         let path = format!("~/.config/darkfi/validatord_db_{:?}", i);
         let database_path = expand_path(&path).unwrap();
         println!("Export data from sled database: {:?}", database_path);
-        let state = ValidatorState::new(database_path, i).unwrap();
+        let state = ValidatorState::new(database_path, i, genesis).unwrap();
         let info = StateInfo::new(&*state.read().unwrap());
         let info_string = format!("{:#?}", info);
         let path = format!("validatord_state_{:?}", i);

+ 5 - 1
script/research/validatord/src/main.rs

@@ -78,6 +78,9 @@ struct Opt {
     #[structopt(long, default_value = "FOOBAR")]
     /// Password for the created TLS identity
     password: String,
+    #[structopt(long, default_value = "1648383795")]
+    /// Timestamp of the genesis block creation
+    genesis: i64,
     #[structopt(long, default_value = "~/.config/darkfi/validatord_db_0")]
     /// Path to the sled database folder
     database: String,
@@ -195,9 +198,10 @@ async fn start(executor: Arc<Executor<'_>>, opts: &Opt) -> Result<()> {
     };
 
     // State setup
+    let genesis = opts.genesis;
     let database_path = expand_path(&opts.database).unwrap();
     let id = opts.id.clone();
-    let state = ValidatorState::new(database_path, id).unwrap();
+    let state = ValidatorState::new(database_path, id, genesis).unwrap();
 
     // P2P registry setup
     let p2p = net::P2p::new(network_settings).await;

+ 3 - 0
script/research/validatord/validatord_config.toml

@@ -38,6 +38,9 @@ identity = "~/.config/darkfi/validatord_identity.pfx"
 # Password for the created TLS identity. (Unused if serve_tls=false)
 password = "FOOBAR"
 
+# Timestamp of the genesis block creation
+genesis = 1648383795
+
 # Path to the sled database folder 
 database = "~/.config/darkfi/validatord_db_0"
 

+ 3 - 3
src/consensus/block.rs

@@ -37,10 +37,10 @@ impl Block {
     }
 
     /// Generates the genesis block.
-    pub fn genesis_block(genesis: &Timestamp) -> Block {
+    pub fn genesis_block(genesis: i64) -> Block {
         let hash = blake3::Hash::from(EMPTY_HASH_BYTES);
         let metadata = Metadata::new(
-            genesis.clone(),
+            Timestamp(genesis),
             String::from("proof"),
             String::from("r"),
             String::from("s"),
@@ -54,7 +54,7 @@ pub struct BlockStore(sled::Tree);
 
 impl BlockStore {
     /// Opens a new or existing blockstore tree given a sled database.
-    pub fn new(db: &sled::Db, genesis: &Timestamp) -> Result<Self> {
+    pub fn new(db: &sled::Db, genesis: i64) -> Result<Self> {
         let tree = db.open_tree(SLED_BLOCK_TREE)?;
         let store = Self(tree);
         if store.0.is_empty() {

+ 1 - 2
src/consensus/blockchain.rs

@@ -12,7 +12,6 @@ use super::{
     block::{Block, BlockProposal, BlockStore},
     metadata::StreamletMetadataStore,
     tx::TxStore,
-    util::Timestamp,
 };
 
 /// This struct represents the canonical (finalized) blockchain stored in sled database.
@@ -27,7 +26,7 @@ pub struct Blockchain {
 }
 
 impl Blockchain {
-    pub fn new(db: &sled::Db, genesis: &Timestamp) -> Result<Blockchain> {
+    pub fn new(db: &sled::Db, genesis: i64) -> Result<Blockchain> {
         let blocks = BlockStore::new(db, genesis)?;
         let transactions = TxStore::new(db)?;
         let streamlet_metadata = StreamletMetadataStore::new(db)?;

+ 7 - 8
src/consensus/state.rs

@@ -52,15 +52,14 @@ pub struct ConsensusState {
 }
 
 impl ConsensusState {
-    pub fn new(db: &sled::Db, id: u64) -> Result<ConsensusState> {
+    pub fn new(db: &sled::Db, id: u64, genesis: i64) -> Result<ConsensusState> {
         let tree = db.open_tree(SLED_CONSESUS_STATE_TREE)?;
         let consensus = if let Some(found) = tree.get(id.to_ne_bytes())? {
             deserialize(&found).unwrap()
         } else {
-            let genesis = get_current_time();
             let consensus = ConsensusState {
-                genesis: genesis.clone(),
-                last_block: blake3::hash(&serialize(&Block::genesis_block(&genesis))),
+                genesis: Timestamp(genesis),
+                last_block: blake3::hash(&serialize(&Block::genesis_block(genesis))),
                 last_sl: 0,
                 proposals: Vec::new(),
                 orphan_votes: Vec::new(),
@@ -99,15 +98,15 @@ pub struct ValidatorState {
 }
 
 impl ValidatorState {
-    pub fn new(db_path: PathBuf, id: u64) -> Result<ValidatorStatePtr> {
+    pub fn new(db_path: PathBuf, id: u64, genesis: i64) -> Result<ValidatorStatePtr> {
         // TODO: clock sync
         let secret = SecretKey::random(&mut OsRng);
         let db = sled::open(db_path)?;
         let public = PublicKey::from_secret(secret);
-        let consensus = ConsensusState::new(&db, id)?;
-        let blockchain = Blockchain::new(&db, &consensus.genesis)?;
+        let consensus = ConsensusState::new(&db, id, genesis)?;
+        let blockchain = Blockchain::new(&db, genesis)?;
         let unconfirmed_txs = Vec::new();
-        let genesis_block = blake3::hash(&serialize(&Block::genesis_block(&consensus.genesis)));
+        let genesis_block = blake3::hash(&serialize(&Block::genesis_block(genesis)));
         Ok(Arc::new(RwLock::new(ValidatorState {
             id,
             secret,