فهرست منبع

consensus: genesis timestamp hardcoded and retrieved based on network

aggstam 4 سال پیش
والد
کامیت
d5b9a811a4
4فایلهای تغییر یافته به همراه17 افزوده شده و 21 حذف شده
  1. 5 11
      bin/darkfid2/src/main.rs
  2. 3 6
      script/consensus_simulation.sh
  3. 6 0
      src/consensus/mod.rs
  4. 3 4
      src/consensus/proto/protocol_sync.rs

+ 5 - 11
bin/darkfid2/src/main.rs

@@ -21,8 +21,8 @@ use darkfi::{
         },
         },
         state::ValidatorStatePtr,
         state::ValidatorStatePtr,
         task::{block_sync_task, proposal_task},
         task::{block_sync_task, proposal_task},
-        util::Timestamp,
-        ValidatorState, MAINNET_GENESIS_HASH_BYTES, TESTNET_GENESIS_HASH_BYTES,
+        ValidatorState, MAINNET_GENESIS_HASH_BYTES, MAINNET_GENESIS_TIMESTAMP,
+        TESTNET_GENESIS_HASH_BYTES, TESTNET_GENESIS_TIMESTAMP,
     },
     },
     crypto::{address::Address, keypair::PublicKey, token_list::DrkTokenList},
     crypto::{address::Address, keypair::PublicKey, token_list::DrkTokenList},
     net,
     net,
@@ -134,10 +134,6 @@ struct Args {
     #[structopt(short, parse(from_occurrences))]
     #[structopt(short, parse(from_occurrences))]
     /// Increase verbosity (-vvv supported)
     /// Increase verbosity (-vvv supported)
     verbose: u8,
     verbose: u8,
-
-    #[structopt(short)]
-    /// Genesis time
-    genesis_time: i64,
 }
 }
 
 
 pub struct Darkfid {
 pub struct Darkfid {
@@ -220,11 +216,9 @@ async fn realmain(args: Args, ex: Arc<Executor<'_>>) -> Result<()> {
     let sled_db = sled::open(&db_path)?;
     let sled_db = sled::open(&db_path)?;
 
 
     // Initialize validator state
     // Initialize validator state
-    // TODO: genesis_ts should be some hardcoded constant
-    let genesis_ts = Timestamp(args.genesis_time);
-    let genesis_data = match args.chain.as_str() {
-        "mainnet" => *MAINNET_GENESIS_HASH_BYTES,
-        "testnet" => *TESTNET_GENESIS_HASH_BYTES,
+    let (genesis_ts, genesis_data) = match args.chain.as_str() {
+        "mainnet" => (*MAINNET_GENESIS_TIMESTAMP, *MAINNET_GENESIS_HASH_BYTES),
+        "testnet" => (*TESTNET_GENESIS_TIMESTAMP, *TESTNET_GENESIS_HASH_BYTES),
         x => {
         x => {
             error!("Unsupported chain `{}`", x);
             error!("Unsupported chain `{}`", x);
             return Err(Error::UnsupportedChain)
             return Err(Error::UnsupportedChain)

+ 3 - 6
script/consensus_simulation.sh

@@ -23,8 +23,7 @@ LOG_TARGETS="!sled,!net" ./darkfid2 \
     --rpc-listen tcp://127.0.0.1:6010 \
     --rpc-listen tcp://127.0.0.1:6010 \
     --sync-p2p-accept 127.0.0.1:6020 \
     --sync-p2p-accept 127.0.0.1:6020 \
     --sync-p2p-external 127.0.0.1:6020 \
     --sync-p2p-external 127.0.0.1:6020 \
-    --wallet-path ./tmp/node0/wallet.db \
-    -g 1650887115 &
+    --wallet-path ./tmp/node0/wallet.db &
     
     
 pids[${#pids[@]}]=$!
 pids[${#pids[@]}]=$!
 
 
@@ -46,8 +45,7 @@ do
     --rpc-listen tcp://127.0.0.1:601$i \
     --rpc-listen tcp://127.0.0.1:601$i \
     --sync-p2p-accept 127.0.0.1:602$i \
     --sync-p2p-accept 127.0.0.1:602$i \
     --sync-p2p-external 127.0.0.1:602$i \
     --sync-p2p-external 127.0.0.1:602$i \
-    --wallet-path ./tmp/node$i/wallet.db \
-    -g 1650887115 &
+    --wallet-path ./tmp/node$i/wallet.db &
   pids[${#pids[@]}]=$!
   pids[${#pids[@]}]=$!
   # waiting for node to setup
   # waiting for node to setup
   sleep 20
   sleep 20
@@ -77,5 +75,4 @@ LOG_TARGETS="!sled,!net" ./darkfid2 \
     --rpc-listen tcp://127.0.0.1:601$bound \
     --rpc-listen tcp://127.0.0.1:601$bound \
     --sync-p2p-accept 127.0.0.1:602$bound \
     --sync-p2p-accept 127.0.0.1:602$bound \
     --sync-p2p-external 127.0.0.1:602$bound \
     --sync-p2p-external 127.0.0.1:602$bound \
-    --wallet-path ./tmp/node$bound/wallet.db \
-    -g 1650887115
+    --wallet-path ./tmp/node$bound/wallet.db

+ 6 - 0
src/consensus/mod.rs

@@ -33,9 +33,15 @@ lazy_static! {
     /// Genesis hash for the mainnet chain
     /// Genesis hash for the mainnet chain
     pub static ref MAINNET_GENESIS_HASH_BYTES: blake3::Hash = blake3::hash(b"darkfi_mainnet");
     pub static ref MAINNET_GENESIS_HASH_BYTES: blake3::Hash = blake3::hash(b"darkfi_mainnet");
 
 
+    /// Genesis timestamp for the mainnet chain
+    pub static ref MAINNET_GENESIS_TIMESTAMP: Timestamp = Timestamp(1650887115);
+
     /// Genesis hash for the testnet chain
     /// Genesis hash for the testnet chain
     pub static ref TESTNET_GENESIS_HASH_BYTES: blake3::Hash = blake3::hash(b"darkfi_testnet");
     pub static ref TESTNET_GENESIS_HASH_BYTES: blake3::Hash = blake3::hash(b"darkfi_testnet");
 
 
+    /// Genesis timestamp for the testnet chain
+    pub static ref TESTNET_GENESIS_TIMESTAMP: Timestamp = Timestamp(1650887115);
+
     /// Block version number
     /// Block version number
     pub static ref BLOCK_VERSION: u8 = 1;
     pub static ref BLOCK_VERSION: u8 = 1;
 }
 }

+ 3 - 4
src/consensus/proto/protocol_sync.rs

@@ -94,7 +94,7 @@ impl ProtocolSync {
             debug!("handle_receive_block(): node runs in consensus mode, skipping...");
             debug!("handle_receive_block(): node runs in consensus mode, skipping...");
             return Ok(())
             return Ok(())
         }
         }
-        
+
         debug!("handle_receive_block() [START]");
         debug!("handle_receive_block() [START]");
         loop {
         loop {
             let info = match self.block_sub.receive().await {
             let info = match self.block_sub.receive().await {
@@ -114,7 +114,7 @@ impl ProtocolSync {
             debug!("handle_receive_block(): Pending lock released");
             debug!("handle_receive_block(): Pending lock released");
 
 
             // Node stores finalized block, if it doesn't exist (checking by slot),
             // Node stores finalized block, if it doesn't exist (checking by slot),
-            // and removes its transactions from the unconfirmed_txs vector.            
+            // and removes its transactions from the unconfirmed_txs vector.
             // Extra validations can be added here.
             // Extra validations can be added here.
             *self.pending.lock().await = true;
             *self.pending.lock().await = true;
             let info_copy = (*info).clone();
             let info_copy = (*info).clone();
@@ -130,8 +130,7 @@ impl ProtocolSync {
 
 
             if !has_block {
             if !has_block {
                 debug!("handle_receive_block(): Starting state transition validation");
                 debug!("handle_receive_block(): Starting state transition validation");
-                let canon_state_clone =
-                    self.state.read().await.state_machine.lock().await.clone();
+                let canon_state_clone = self.state.read().await.state_machine.lock().await.clone();
                 let mem_state = MemoryState::new(canon_state_clone);
                 let mem_state = MemoryState::new(canon_state_clone);
                 let state_updates =
                 let state_updates =
                     match ValidatorState::validate_state_transitions(mem_state, &info.txs) {
                     match ValidatorState::validate_state_transitions(mem_state, &info.txs) {