Просмотр исходного кода

consensu/block: added version, consensus/state: fixed epoch 0 participants check, bin/darkfid2: updated config.toml flags, script/consensus_simulation: updated flags

aggstam 4 лет назад
Родитель
Сommit
8d6c696e3a

+ 4 - 4
bin/darkfid2/darkfid_config.toml

@@ -34,10 +34,10 @@
 #consensus_slots = 8
 
 # Seed nodes to connect to for the consensus protocol
-#consensus_seed = []
+#consensus_p2p_seed = []
 
 # Peers to connect to for the consensus protocol
-#consensus_peer = []
+#consensus_p2p_peer = []
 
 # P2P accept address for the syncing protocol
 #sync_p2p_accept = "0.0.0.0:8342"
@@ -49,7 +49,7 @@
 #sync_slots = 8
 
 # Seed nodes to connect to for the syncing protocol
-#sync_seed = []
+#sync_p2p_seed = []
 
 # Peers to connect to for the syncing protocol
-#sync_peer = []
+#sync_p2p_peer = []

+ 4 - 4
script/consensus_simulation.sh

@@ -38,8 +38,8 @@ do
   LOG_TARGETS="!sled,!net" ./darkfid2 \
     -v \
     --consensus \
-    --consensus-seed 127.0.0.1:6000 \
-    --sync-seed 127.0.0.1:6020 \
+    --consensus-p2p-seed 127.0.0.1:6000 \
+    --sync-p2p-seed 127.0.0.1:6020 \
     --consensus-p2p-accept 127.0.0.1:600$i \
     --consensus-p2p-external 127.0.0.1:600$i \
     --database ./tmp/node$i/blockchain \
@@ -69,8 +69,8 @@ bound=$(($nodes-1))
 LOG_TARGETS="!sled,!net" ./darkfid2 \
     -v \
     --consensus \
-    --consensus-seed 127.0.0.1:6000 \
-    --sync-seed 127.0.0.1:6020 \
+    --consensus-p2p-seed 127.0.0.1:6000 \
+    --sync-p2p-seed 127.0.0.1:6020 \
     --consensus-p2p-accept 127.0.0.1:600$bound \
     --consensus-p2p-external 127.0.0.1:600$bound \
     --database ./tmp/node$bound/blockchain \

+ 11 - 5
src/consensus/block.rs

@@ -2,7 +2,7 @@ use std::io;
 
 use log::debug;
 
-use super::{Metadata, StreamletMetadata, Timestamp};
+use super::{Metadata, StreamletMetadata, Timestamp, BLOCK_VERSION};
 use crate::{
     crypto::{address::Address, keypair::PublicKey, schnorr::Signature},
     impl_vec, net,
@@ -11,11 +11,13 @@ use crate::{
     Result,
 };
 
-/// This struct represents a tuple of the form (`st`, `sl`, txs`, `metadata`).
+/// This struct represents a tuple of the form ('v', `st`, `sl`, txs`, `metadata`).
 /// The transactions here are stored as hashes, which serve as pointers to
 /// the actual transaction data in the blockchain database.
 #[derive(Debug, Clone, SerialEncodable, SerialDecodable)]
 pub struct Block {
+    /// Block version
+    pub v: u8,
     /// Previous block hash
     pub st: blake3::Hash,
     /// Slot uid, generated by the beacon
@@ -28,7 +30,8 @@ pub struct Block {
 
 impl Block {
     pub fn new(st: blake3::Hash, sl: u64, txs: Vec<blake3::Hash>, metadata: Metadata) -> Self {
-        Self { st, sl, txs, metadata }
+        let v = *BLOCK_VERSION;
+        Self { v, st, sl, txs, metadata }
     }
 
     /// Generate the genesis block.
@@ -63,6 +66,8 @@ impl net::Message for BlockOrder {
 /// Structure representing full block data.
 #[derive(Debug, Clone, SerialEncodable, SerialDecodable)]
 pub struct BlockInfo {
+    /// Block version
+    pub v: u8,
     /// Previous block hash
     pub st: blake3::Hash,
     /// Slot UID, generated by the beacon
@@ -83,7 +88,8 @@ impl BlockInfo {
         metadata: Metadata,
         sm: StreamletMetadata,
     ) -> Self {
-        Self { st, sl, txs, metadata, sm }
+        let v = *BLOCK_VERSION;
+        Self { v, st, sl, txs, metadata, sm }
     }
 
     /// Calculate the block hash
@@ -96,7 +102,7 @@ impl BlockInfo {
 impl From<BlockInfo> for Block {
     fn from(b: BlockInfo) -> Self {
         let txids = b.txs.iter().map(|x| blake3::hash(&serialize(x))).collect();
-        Self { st: b.st, sl: b.sl, txs: txids, metadata: b.metadata }
+        Self { v: b.v, st: b.st, sl: b.sl, txs: txids, metadata: b.metadata }
     }
 }
 

+ 3 - 0
src/consensus/mod.rs

@@ -35,4 +35,7 @@ lazy_static! {
 
     /// Genesis hash for the testnet chain
     pub static ref TESTNET_GENESIS_HASH_BYTES: blake3::Hash = blake3::hash(b"darkfi_testnet");
+
+    /// Block version number
+    pub static ref BLOCK_VERSION: u8 = 1;
 }

+ 6 - 1
src/consensus/state.rs

@@ -764,7 +764,12 @@ impl ValidatorState {
         }
 
         let previous_epoch = epoch - 1;
-        let previous_from_last_epoch = last_epoch - 1;
+        // This check ensures that when restarting the network, previous
+        // from last epoch is not u64::MAX
+        let previous_from_last_epoch = match last_epoch {
+            0 => 0,
+            _ => last_epoch - 1,
+        };
 
         debug!(
             "refresh_participants(): Node {:?} checking epochs: previous - {:?}, last - {:?}, previous from last - {:?}",