Jelajahi Sumber

blockchain/Block: block version to derive from cuttof slot, not hardcoded value

aggstam 2 tahun lalu
induk
melakukan
95434701f4

+ 0 - 3
bin/darkfid2/src/task/miner.rs

@@ -70,9 +70,6 @@ async fn miner_loop(node: &Darkfid, stop_signal: &Receiver<()>) -> Result<()> {
 
         // Mine next block
         let difficulty = module.next_difficulty();
-        // TODO: BlockInfo::default() should be based on block version
-        // TODO: block version should be derived from cuttoff const, not
-        //       hardcoded BLOCK_VERSION
         let mut next_block = BlockInfo::default();
         next_block.header.version = 0;
         next_block.header.previous = last.hash()?;

+ 0 - 3
src/blockchain/block_store.rs

@@ -30,9 +30,6 @@ use crate::{tx::Transaction, Error, Result};
 
 use super::{parse_record, parse_u64_key_record, Header, SledDbOverlayPtr};
 
-/// Block version number
-pub const BLOCK_VERSION: u8 = 1;
-
 /// This struct represents a tuple of the form (`header`, `txs`, `signature`).
 /// The header and transactions are stored as hashes, serving as pointers to the actual data
 /// in the sled database.

+ 3 - 2
src/blockchain/header_store.rs

@@ -17,6 +17,7 @@
  */
 
 use darkfi_sdk::{
+    blockchain::block_version,
     crypto::MerkleTree,
     pasta::{group::ff::Field, pallas},
 };
@@ -27,7 +28,7 @@ use darkfi_serial::{deserialize, serialize, Encodable, SerialDecodable, SerialEn
 
 use crate::{util::time::Timestamp, Error, Result};
 
-use super::{block_store::BLOCK_VERSION, parse_record, SledDbOverlayPtr};
+use super::{parse_record, SledDbOverlayPtr};
 
 /// This struct represents a tuple of the form (version, previous, epoch, height, timestamp, nonce, merkle_root).
 #[derive(Debug, Clone, PartialEq, Eq, SerialEncodable, SerialDecodable)]
@@ -58,7 +59,7 @@ impl Header {
         timestamp: Timestamp,
         nonce: pallas::Base,
     ) -> Self {
-        let version = BLOCK_VERSION;
+        let version = block_version(height);
         let tree = MerkleTree::new(1);
         Self { version, previous, epoch, height, timestamp, nonce, tree }
     }

+ 10 - 0
src/sdk/src/blockchain.rs

@@ -121,6 +121,16 @@ impl Default for Slot {
 // TODO: This values are experimental, should be replaced with the proper ones once defined
 pub const POW_CUTOFF: u64 = 1000000;
 pub const POS_START: u64 = 1000001;
+
+/// Auxiliary function to calculate provided block height(slot) block version.
+/// PoW blocks use version 1, while PoS ones use version 2.
+pub fn block_version(height: u64) -> u8 {
+    match height {
+        0..=POW_CUTOFF => 1,
+        POS_START.. => 2,
+    }
+}
+
 /// Auxiliary function to calculate provided block height(slot) expected reward value.
 /// Genesis slot(0) always returns reward value 0.
 /// We use PoW bootstrap, configured to reduce rewards at fixed height numbers, until a cutoff.