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

validator: changed block ranking logic

skoupidi пре 2 година
родитељ
комит
5e9892363a
2 измењених фајлова са 13 додато и 12 уклоњено
  1. 6 5
      doc/src/arch/consensus.md
  2. 7 7
      src/validator/utils.rs

+ 6 - 5
doc/src/arch/consensus.md

@@ -73,17 +73,18 @@ that we can produce in advance.
 
 Each block proposal is ranked based on the modulus of the $(n-2)$-block
 proposal's `VRF` proof (attached to the block producer's reward transaction)
-and its `nonce`.
+and the big-integer from the big endian output of its hash.
 
 The rank of the genesis block is 0. The rank of the following 2 blocks is equal
-to the nonce, since there is no $(n-2)$-block producer or `VRF` attached to the
+to their hash output, since there is no $(n-2)$-block producer or `VRF` attached to the
 reward transaction.
 
 For all other blocks, the rank is computed as follows:
 
-1. Grab the `VRF` proof from the reward transaction of the $(n-2)$-block proposal
-2. Obtain a big-integer from the big endian output of the `VRF`
-3. Compute the rank: `vrf.output` % `nonce` (If `nonce` is 0, rank is equal to `vrf.output`)
+1. Obtain a big-integer from the big endian output of the blocks hash
+2. Grab the `VRF` proof from the reward transaction of the $(n-2)$-block proposal
+3. Obtain a big-integer from the big endian output of the `VRF`
+4. Compute the rank: `vrf.output` % `hash_output` (If `hash_output` is 0, rank is equal to `vrf.output`)
 
 To calculate each fork rank, we simply multiply the sum of every block
 proposal's rank in the fork by the fork's length. We use the length

+ 7 - 7
src/validator/utils.rs

@@ -96,9 +96,9 @@ pub async fn deploy_native_contracts(overlay: &BlockchainOverlayPtr) -> Result<(
     Ok(())
 }
 
-/// Compute a block's rank, assuming the its valid.
+/// Compute a block's rank, assuming that its valid.
 /// Genesis block has rank 0.
-/// First 2 blocks rank is equal to their nonce, since their previous
+/// First 2 blocks rank is equal to their hash number, since their previous
 /// previous block producer doesn't exist or have a VRF.
 pub async fn block_rank(block: &BlockInfo, previous_previous: &BlockInfo) -> Result<BigUint> {
     // Genesis block has rank 0
@@ -106,12 +106,12 @@ pub async fn block_rank(block: &BlockInfo, previous_previous: &BlockInfo) -> Res
         return Ok(0u64.into())
     }
 
-    // Grab block nonce
-    let nonce = block.header.nonce;
+    // Grab block hash number
+    let hash_number = BigUint::from_bytes_be(block.hash()?.as_bytes());
 
-    // First 2 blocks have rank equal to their nonce
+    // First 2 blocks have rank equal to their block hash number
     if block.header.height < 3 {
-        return Ok(nonce.into())
+        return Ok(hash_number)
     }
 
     // Extract VRF proof from the previous previous producer transaction
@@ -126,7 +126,7 @@ pub async fn block_rank(block: &BlockInfo, previous_previous: &BlockInfo) -> Res
     let vrf_output = BigUint::from_bytes_be(vrf_proof.hash_output().as_bytes());
 
     // Finally, compute the rank
-    let rank = if nonce != 0 { vrf_output % nonce } else { vrf_output };
+    let rank = if hash_number != 0u8.into() { vrf_output % hash_number } else { vrf_output };
 
     Ok(rank)
 }