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

blockchain/header: derive block version using sdk block_version fn

skoupidi 2 лет назад
Родитель
Сommit
ccc3a8e3a7

+ 1 - 7
bin/darkfid/src/tests/harness.rs

@@ -214,13 +214,7 @@ impl Harness {
 
         // Generate header
         let height = slots.last().unwrap().id;
-        let header = Header::new(
-            previous_hash,
-            self.alice.validator.consensus.time_keeper.slot_epoch(height),
-            height,
-            timestamp,
-            previous.header.nonce,
-        );
+        let header = Header::new(previous_hash, height, timestamp, previous.header.nonce);
 
         // Generate the block
         let mut block = BlockInfo::new_empty(header, slots);

+ 2 - 3
src/blockchain/header_store.rs

@@ -17,7 +17,7 @@
  */
 
 use darkfi_sdk::{
-    blockchain::block_version,
+    blockchain::{block_epoch, block_version},
     crypto::MerkleTree,
     pasta::{group::ff::Field, pallas},
 };
@@ -54,12 +54,12 @@ pub struct Header {
 impl Header {
     pub fn new(
         previous: blake3::Hash,
-        epoch: u64,
         height: u64,
         timestamp: Timestamp,
         nonce: pallas::Base,
     ) -> Self {
         let version = block_version(height);
+        let epoch = block_epoch(height);
         let tree = MerkleTree::new(1);
         Self { version, previous, epoch, height, timestamp, nonce, tree }
     }
@@ -86,7 +86,6 @@ impl Default for Header {
         Header::new(
             blake3::hash(b"Let there be dark!"),
             0,
-            0,
             Timestamp::current_time(),
             pallas::Base::ZERO,
         )

+ 4 - 7
src/sdk/src/blockchain.rs

@@ -122,13 +122,10 @@ impl Default for Slot {
 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 block version.
+/// Currently, a single version(1) exists.
+pub fn block_version(_height: u64) -> u8 {
+    1
 }
 
 /// Auxiliary function to calculate provided block height epoch.

+ 0 - 1
src/validator/consensus.rs

@@ -164,7 +164,6 @@ impl Consensus {
         // TODO: verify if header timestamp should be blockchain or system timestamp
         let header = Header::new(
             previous.block.hash()?,
-            time_keeper.slot_epoch(slot.id),
             slot.id,
             Timestamp::current_time(),
             slot.last_nonce,

+ 35 - 23
src/validator/validation.rs

@@ -17,7 +17,7 @@
  */
 
 use darkfi_sdk::{
-    blockchain::{block_version, expected_reward, Slot},
+    blockchain::{block_epoch, block_version, expected_reward, Slot},
     pasta::{group::ff::Field, pallas},
 };
 use num_bigint::BigUint;
@@ -47,12 +47,13 @@ pub fn validate_block(
 
 /// A PoW block is considered valid when the following rules apply:
 ///     1. Block version is equal to 1
-///     2. Parent hash is equal to the hash of the previous block
-///     3. Block height increments previous block height by 1
-///     4. Timestamp is valid based on PoWModule validation
-///     5. Block hash is valid based on PoWModule validation
-///     6. Slots vector contains a single valid slot
-///     7. Block height is the same as the slots vector last slot id
+///     2. Block epoch corresponds to the one for its height
+///     3. Parent hash is equal to the hash of the previous block
+///     4. Block height increments previous block height by 1
+///     5. Timestamp is valid based on PoWModule validation
+///     6. Block hash is valid based on PoWModule validation
+///     7. Slots vector contains a single valid slot
+///     8. Block height is the same as the slots vector last slot id
 /// Additional validity rules can be applied.
 pub fn validate_pow_block(
     block: &BlockInfo,
@@ -67,26 +68,31 @@ pub fn validate_pow_block(
         return error
     }
 
-    // Check previous hash (2)
+    // Check block epoch (2)
+    if block.header.epoch != block_epoch(block.header.height) {
+        return error
+    }
+
+    // Check previous hash (3)
     let previous_hash = previous.hash()?;
     if block.header.previous != previous_hash {
         return error
     }
 
-    // Check heights are incremental (3)
+    // Check heights are incremental (4)
     if block.header.height != previous.header.height + 1 {
         return error
     }
 
-    // Check timestamp validity (4)
+    // Check timestamp validity (5)
     if !module.verify_timestamp_by_median(block.header.timestamp.0) {
         return error
     }
 
-    // Check block hash corresponds to next one (5)
+    // Check block hash corresponds to next one (6)
     module.verify_block_hash(block)?;
 
-    // Verify slots vector contains single slot (6)
+    // Verify slots vector contains single slot (7)
     if block.slots.len() != 1 {
         return error
     }
@@ -105,7 +111,7 @@ pub fn validate_pow_block(
         expected_reward,
     )?;
 
-    // Check block height is the last slot id (7)
+    // Check block height is the last slot id (8)
     if last_slot.id != block.header.height {
         return error
     }
@@ -187,11 +193,12 @@ pub fn validate_pow_slot(
 
 /// A PoS block is considered valid when the following rules apply:
 ///     1. Block version is equal to 2
-///     2. Parent hash is equal to the hash of the previous block
-///     3. Timestamp increments previous block timestamp
-///     4. Slot increments previous block slot
-///     5. Slots vector is not empty and all its slots are valid
-///     6. Slot is the same as the slots vector last slot id
+///     2. Block epoch corresponds to the one for its height
+///     3. Parent hash is equal to the hash of the previous block
+///     4. Timestamp increments previous block timestamp
+///     5. Slot increments previous block slot
+///     6. Slots vector is not empty and all its slots are valid
+///     7. Slot is the same as the slots vector last slot id
 /// Additional validity rules can be applied.
 pub fn validate_pos_block(
     block: &BlockInfo,
@@ -205,23 +212,28 @@ pub fn validate_pos_block(
         return error
     }
 
-    // Check previous hash (2)
+    // Check block epoch (2)
+    if block.header.epoch != block_epoch(block.header.height) {
+        return error
+    }
+
+    // Check previous hash (3)
     let previous_hash = previous.hash()?;
     if block.header.previous != previous_hash {
         return error
     }
 
-    // Check timestamps are incremental (3)
+    // Check timestamps are incremental (4)
     if block.header.timestamp <= previous.header.timestamp {
         return error
     }
 
-    // Check heights are incremental (4)
+    // Check heights are incremental (5)
     if block.header.height <= previous.header.height {
         return error
     }
 
-    // Verify slots (5)
+    // Verify slots (6)
     if block.slots.is_empty() {
         return error
     }
@@ -257,7 +269,7 @@ pub fn validate_pos_block(
         expected_reward,
     )?;
 
-    // Check block height is the last slot id (6)
+    // Check block height is the last slot id (7)
     if last_slot.id != block.header.height {
         return error
     }

+ 1 - 2
tests/blockchain.rs

@@ -95,8 +95,7 @@ impl Harness {
         timestamp.add(1);
 
         // Generate header
-        let header =
-            Header::new(previous_hash, previous.header.epoch, id, timestamp, previous.header.nonce);
+        let header = Header::new(previous_hash, id, timestamp, previous.header.nonce);
 
         // Generate the block
         let mut block = BlockInfo::new_empty(header, vec![slot]);