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

validator: producer tx on first instead of last position in block txs vector

aggstam 2 лет назад
Родитель
Сommit
a4ba0b1f6b
3 измененных файлов с 14 добавлено и 20 удалено
  1. 0 4
      bin/darkfid2/src/tests/harness.rs
  2. 6 7
      src/validator/utils.rs
  3. 8 9
      src/validator/verification.rs

+ 0 - 4
bin/darkfid2/src/tests/harness.rs

@@ -69,13 +69,9 @@ impl Harness {
         // Generate default genesis block
         // Generate default genesis block
         let mut genesis_block = BlockInfo::default();
         let mut genesis_block = BlockInfo::default();
 
 
-        // Retrieve genesis producer transaction
-        let producer_tx = genesis_block.txs.pop().unwrap();
-
         // Append genesis transactions and calculate their total
         // Append genesis transactions and calculate their total
         genesis_block.txs.push(genesis_stake_tx);
         genesis_block.txs.push(genesis_stake_tx);
         genesis_block.txs.push(genesis_mint_tx);
         genesis_block.txs.push(genesis_mint_tx);
-        genesis_block.txs.push(producer_tx);
         let genesis_txs_total = genesis_txs_total(&genesis_block.txs).await?;
         let genesis_txs_total = genesis_txs_total(&genesis_block.txs).await?;
         genesis_block.slots[0].total_tokens = genesis_txs_total;
         genesis_block.slots[0].total_tokens = genesis_txs_total;
 
 

+ 6 - 7
src/validator/utils.rs

@@ -177,7 +177,7 @@ pub fn median(mut v: Vec<u64>) -> u64 {
 /// Auxiliary function to calculate the total amount of minted tokens in provided
 /// Auxiliary function to calculate the total amount of minted tokens in provided
 /// genesis transactions set. This includes both staked and normal tokens.
 /// genesis transactions set. This includes both staked and normal tokens.
 /// If a non-genesis transaction is found, execution fails.
 /// If a non-genesis transaction is found, execution fails.
-/// Set must also include the genesis transaction(empty) at last position.
+/// Set must also include the genesis transaction(empty) at first position.
 pub async fn genesis_txs_total(txs: &[Transaction]) -> Result<u64> {
 pub async fn genesis_txs_total(txs: &[Transaction]) -> Result<u64> {
     let mut total = 0;
     let mut total = 0;
 
 
@@ -185,8 +185,12 @@ pub async fn genesis_txs_total(txs: &[Transaction]) -> Result<u64> {
         return Ok(total)
         return Ok(total)
     }
     }
 
 
+    if txs[0] != Transaction::default() {
+        return Err(TxVerifyFailed::ErroneousTxs(vec![txs[0].clone()]).into())
+    }
+
     // Iterate transactions, exluding producer(last) one
     // Iterate transactions, exluding producer(last) one
-    for tx in &txs[..txs.len() - 1] {
+    for tx in &txs[1..] {
         // Transaction must contain a single Consensus::GenesisStake (0x00)
         // Transaction must contain a single Consensus::GenesisStake (0x00)
         // or Money::GenesisMint (0x01) call
         // or Money::GenesisMint (0x01) call
         if tx.calls.len() != 1 {
         if tx.calls.len() != 1 {
@@ -215,11 +219,6 @@ pub async fn genesis_txs_total(txs: &[Transaction]) -> Result<u64> {
         total += value;
         total += value;
     }
     }
 
 
-    let tx = txs.last().unwrap();
-    if tx != &Transaction::default() {
-        return Err(TxVerifyFailed::ErroneousTxs(vec![tx.clone()]).into())
-    }
-
     Ok(total)
     Ok(total)
 }
 }
 
 

+ 8 - 9
src/validator/verification.rs

@@ -97,14 +97,13 @@ pub async fn verify_genesis_block(
     overlay.lock().unwrap().slots.insert(&[genesis_slot.clone()])?;
     overlay.lock().unwrap().slots.insert(&[genesis_slot.clone()])?;
 
 
     // Genesis transaction must be the Transaction::default() one(empty)
     // Genesis transaction must be the Transaction::default() one(empty)
-    let tx = block.txs.last().unwrap();
-    if tx != &Transaction::default() {
+    if block.txs[0] != Transaction::default() {
         error!(target: "validator::verification::verify_genesis_block", "Genesis proposal transaction is not default one");
         error!(target: "validator::verification::verify_genesis_block", "Genesis proposal transaction is not default one");
-        return Err(TxVerifyFailed::ErroneousTxs(vec![tx.clone()]).into())
+        return Err(TxVerifyFailed::ErroneousTxs(vec![block.txs[0].clone()]).into())
     }
     }
 
 
-    // Verify transactions, exluding producer(last) one
-    let txs = &block.txs[..block.txs.len() - 1];
+    // Verify transactions, exluding producer(first) one
+    let txs = &block.txs[1..];
     let erroneous_txs = verify_transactions(overlay, time_keeper, txs, false).await?;
     let erroneous_txs = verify_transactions(overlay, time_keeper, txs, false).await?;
     if !erroneous_txs.is_empty() {
     if !erroneous_txs.is_empty() {
         warn!(target: "validator::verification::verify_genesis_block", "Erroneous transactions found in set");
         warn!(target: "validator::verification::verify_genesis_block", "Erroneous transactions found in set");
@@ -163,14 +162,14 @@ pub async fn verify_block(
     // Verify proposal transaction.
     // Verify proposal transaction.
     // For PoS blocks(version 2) verify if not in PoS testing mode.
     // For PoS blocks(version 2) verify if not in PoS testing mode.
     if block.header.version != 2 || !pos_testing_mode {
     if block.header.version != 2 || !pos_testing_mode {
-        let tx = block.txs.last().unwrap();
         let public_key =
         let public_key =
-            verify_producer_transaction(overlay, time_keeper, tx, block.header.version).await?;
+            verify_producer_transaction(overlay, time_keeper, &block.txs[0], block.header.version)
+                .await?;
         verify_producer_signature(block, &public_key)?;
         verify_producer_signature(block, &public_key)?;
     }
     }
 
 
-    // Verify transactions, exluding producer(last) one
-    let txs = &block.txs[..block.txs.len() - 1];
+    // Verify transactions, exluding producer(first) one
+    let txs = &block.txs[1..];
     let erroneous_txs = verify_transactions(overlay, time_keeper, txs, false).await?;
     let erroneous_txs = verify_transactions(overlay, time_keeper, txs, false).await?;
     if !erroneous_txs.is_empty() {
     if !erroneous_txs.is_empty() {
         warn!(target: "validator::verification::verify_block", "Erroneous transactions found in set");
         warn!(target: "validator::verification::verify_block", "Erroneous transactions found in set");