Преглед изворни кода

validator/validation,verification: perform validations based on block version

aggstam пре 2 година
родитељ
комит
ade055d3db
2 измењених фајлова са 54 додато и 48 уклоњено
  1. 37 34
      src/validator/validation.rs
  2. 17 14
      src/validator/verification.rs

+ 37 - 34
src/validator/validation.rs

@@ -53,43 +53,46 @@ pub fn validate_block(block: &BlockInfo, previous: &BlockInfo, expected_reward:
         return error
     }
 
-    // Verify slots (4)
-    if block.slots.is_empty() {
-        return error
-    }
+    // Check extra stuff based on block version
+    if block.header.version > 0 {
+        // Verify slots (4)
+        if block.slots.is_empty() {
+            return error
+        }
 
-    // Retrieve previous block last slot
-    let mut previous_slot = previous.slots.last().unwrap();
-
-    // Check if empty slots existed
-    if block.slots.len() > 1 {
-        // All slots exluding the last one must have reward value set to 0.
-        // Slots must already be in correct order (sorted by id).
-        for slot in &block.slots[..block.slots.len() - 1] {
-            validate_slot(
-                slot,
-                previous_slot,
-                &previous_hash,
-                &previous.header.previous,
-                &previous.eta,
-                0,
-            )?;
-            previous_slot = slot;
+        // Retrieve previous block last slot
+        let mut previous_slot = previous.slots.last().unwrap();
+
+        // Check if empty slots existed
+        if block.slots.len() > 1 {
+            // All slots exluding the last one must have reward value set to 0.
+            // Slots must already be in correct order (sorted by id).
+            for slot in &block.slots[..block.slots.len() - 1] {
+                validate_slot(
+                    slot,
+                    previous_slot,
+                    &previous_hash,
+                    &previous.header.previous,
+                    &previous.eta,
+                    0,
+                )?;
+                previous_slot = slot;
+            }
         }
-    }
 
-    validate_slot(
-        block.slots.last().unwrap(),
-        previous_slot,
-        &previous_hash,
-        &previous.header.previous,
-        &previous.eta,
-        expected_reward,
-    )?;
-
-    // Check block slot is the last slot id (5)
-    if block.slots.last().unwrap().id != block.header.slot {
-        return error
+        validate_slot(
+            block.slots.last().unwrap(),
+            previous_slot,
+            &previous_hash,
+            &previous.header.previous,
+            &previous.eta,
+            expected_reward,
+        )?;
+
+        // Check block slot is the last slot id (5)
+        if block.slots.last().unwrap().id != block.header.slot {
+            return error
+        }
     }
 
     Ok(())

+ 17 - 14
src/validator/verification.rs

@@ -56,23 +56,26 @@ pub async fn verify_genesis_block(
         return Err(Error::VerifyingSlotMissmatch())
     }
 
-    // Check genesis slot exist
-    if block.slots.len() != 1 {
-        return Err(Error::BlockIsInvalid(block_hash))
-    }
+    // Check extra stuff based on block version
+    if block.header.version > 0 {
+        // Check genesis slot exist
+        if block.slots.len() != 1 {
+            return Err(Error::BlockIsInvalid(block_hash))
+        }
 
-    // Retrieve genesis slot
-    let genesis_slot = block.slots.last().unwrap();
+        // Retrieve genesis slot
+        let genesis_slot = block.slots.last().unwrap();
 
-    // Genesis block slot total token must correspond to the total
-    // of all genesis transactions public inputs (genesis distribution).
-    if genesis_slot.total_tokens != genesis_txs_total {
-        return Err(Error::SlotIsInvalid(genesis_slot.id))
-    }
+        // Genesis block slot total token must correspond to the total
+        // of all genesis transactions public inputs (genesis distribution).
+        if genesis_slot.total_tokens != genesis_txs_total {
+            return Err(Error::SlotIsInvalid(genesis_slot.id))
+        }
 
-    // Verify there is no reward
-    if genesis_slot.reward != 0 {
-        return Err(Error::SlotIsInvalid(genesis_slot.id))
+        // Verify there is no reward
+        if genesis_slot.reward != 0 {
+            return Err(Error::SlotIsInvalid(genesis_slot.id))
+        }
     }
 
     // Verify transactions vector contains at least one(producers) transaction