Ver Fonte

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

aggstam há 4 anos atrás
pai
commit
425d339c43

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

@@ -311,13 +311,12 @@ impl StateInfo {
 }
 
 fn main() -> Result<()> {
-    let nodes = 4;
-    let genesis = 1648383795;
+    let nodes = 1;
     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, genesis).unwrap();
+        let state = ValidatorState::new(database_path, i).unwrap();
         let info = StateInfo::new(&*state.read().unwrap());
         let info_string = format!("{:#?}", info);
         let path = format!("validatord_state_{:?}", i);

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

@@ -78,9 +78,6 @@ 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,
@@ -198,10 +195,9 @@ 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, genesis).unwrap();
+    let state = ValidatorState::new(database_path, id).unwrap();
 
     // P2P registry setup
     let p2p = net::P2p::new(network_settings).await;

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

@@ -38,9 +38,6 @@ 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: i64) -> Block {
+    pub fn genesis_block(genesis: &Timestamp) -> Block {
         let hash = blake3::Hash::from(EMPTY_HASH_BYTES);
         let metadata = Metadata::new(
-            Timestamp(genesis),
+            genesis.clone(),
             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: i64) -> Result<Self> {
+    pub fn new(db: &sled::Db, genesis: &Timestamp) -> Result<Self> {
         let tree = db.open_tree(SLED_BLOCK_TREE)?;
         let store = Self(tree);
         if store.0.is_empty() {

+ 2 - 1
src/consensus/blockchain.rs

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

+ 8 - 7
src/consensus/state.rs

@@ -52,14 +52,15 @@ pub struct ConsensusState {
 }
 
 impl ConsensusState {
-    pub fn new(db: &sled::Db, id: u64, genesis: i64) -> Result<ConsensusState> {
+    pub fn new(db: &sled::Db, id: u64) -> 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: Timestamp(genesis),
-                last_block: blake3::hash(&serialize(&Block::genesis_block(genesis))),
+                genesis: genesis.clone(),
+                last_block: blake3::hash(&serialize(&Block::genesis_block(&genesis))),
                 last_sl: 0,
                 proposals: Vec::new(),
                 orphan_votes: Vec::new(),
@@ -98,15 +99,15 @@ pub struct ValidatorState {
 }
 
 impl ValidatorState {
-    pub fn new(db_path: PathBuf, id: u64, genesis: i64) -> Result<ValidatorStatePtr> {
+    pub fn new(db_path: PathBuf, id: u64) -> 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, genesis)?;
-        let blockchain = Blockchain::new(&db, genesis)?;
+        let consensus = ConsensusState::new(&db, id)?;
+        let blockchain = Blockchain::new(&db, &consensus.genesis)?;
         let unconfirmed_txs = Vec::new();
-        let genesis_block = blake3::hash(&serialize(&Block::genesis_block(genesis)));
+        let genesis_block = blake3::hash(&serialize(&Block::genesis_block(&consensus.genesis)));
         Ok(Arc::new(RwLock::new(ValidatorState {
             id,
             secret,