Przeglądaj źródła

darkfid: fully configurable fees verification

skoupidi 2 lat temu
rodzic
commit
80044e306f

+ 9 - 0
bin/darkfid/darkfid_config.toml

@@ -40,6 +40,9 @@ recipient = "5ZHfYpt4mpJcwBNxfEyxLzeFJUEeoePs5NQ5jVEgHrMf"
 # Skip syncing process and start node right away
 skip_sync = true
 
+# Disable transaction's fee verification, used for testing
+skip_fees = false
+
 # Optional sync checkpoint height
 #checkpoint_height = 0
 
@@ -120,6 +123,9 @@ miner = false
 # Skip syncing process and start node right away
 skip_sync = false
 
+# Disable transaction's fee verification, used for testing
+skip_fees = false
+
 # Optional sync checkpoint height
 #checkpoint_height = 0
 
@@ -210,6 +216,9 @@ miner = false
 # Skip syncing process and start node right away
 skip_sync = false
 
+# Disable transaction's fee verification, used for testing
+skip_fees = false
+
 # Optional sync checkpoint height
 #checkpoint_height = 0
 

+ 5 - 1
bin/darkfid/src/main.rs

@@ -138,6 +138,10 @@ pub struct BlockchainNetwork {
     /// Skip syncing process and start node right away
     pub skip_sync: bool,
 
+    #[structopt(long)]
+    /// Disable transaction's fee verification, used for testing
+    pub skip_fees: bool,
+
     #[structopt(long)]
     /// Optional sync checkpoint height
     pub checkpoint_height: Option<u32>,
@@ -243,7 +247,7 @@ async fn realmain(args: Args, ex: Arc<smol::Executor<'static>>) -> Result<()> {
         pow_target: blockchain_config.pow_target,
         pow_fixed_difficulty,
         genesis_block,
-        verify_fees: false, // TODO: Make configurable
+        verify_fees: !blockchain_config.skip_fees,
     };
 
     // Initialize validator

+ 11 - 3
bin/darkfid/src/task/miner.rs

@@ -250,8 +250,15 @@ async fn mine_next_block(
     pk: &ProvingKey,
 ) -> Result<()> {
     // Grab next target and block
-    let (next_target, mut next_block) =
-        generate_next_block(extended_fork, secret, recipient, zkbin, pk).await?;
+    let (next_target, mut next_block) = generate_next_block(
+        extended_fork,
+        secret,
+        recipient,
+        zkbin,
+        pk,
+        node.validator.verify_fees,
+    )
+    .await?;
 
     // Execute request to minerd and parse response
     let target = JsonValue::String(next_target.to_string());
@@ -284,6 +291,7 @@ async fn generate_next_block(
     recipient: &PublicKey,
     zkbin: &ZkBinary,
     pk: &ProvingKey,
+    verify_fees: bool,
 ) -> Result<(BigUint, BlockInfo)> {
     // Grab extended fork next block height
     let last_proposal = extended_fork.last_proposal()?;
@@ -301,7 +309,7 @@ async fn generate_next_block(
 
     // Generate next block proposal
     let target = extended_fork.module.next_mine_target()?;
-    let next_block = extended_fork.generate_unsigned_block(tx).await?;
+    let next_block = extended_fork.generate_unsigned_block(tx, verify_fees).await?;
 
     Ok((target, next_block))
 }

+ 3 - 0
contrib/localnet/darkfid-five-nodes/darkfid0.toml

@@ -37,6 +37,9 @@ recipient = "9vw6WznKk7xEFQwwXhJWMMdjUPi3cXL8NrFKQpKifG1U"
 # Skip syncing process and start node right away
 skip_sync = true
 
+# Disable transaction's fee verification, used for testing
+skip_fees = false
+
 # Optional sync checkpoint height
 #checkpoint_height = 0
 

+ 3 - 0
contrib/localnet/darkfid-five-nodes/darkfid1.toml

@@ -37,6 +37,9 @@ recipient = "9vw6WznKk7xEFQwwXhJWMMdjUPi3cXL8NrFKQpKifG1U"
 # Skip syncing process and start node right away
 skip_sync = false
 
+# Disable transaction's fee verification, used for testing
+skip_fees = false
+
 # Optional sync checkpoint height
 #checkpoint_height = 0
 

+ 3 - 0
contrib/localnet/darkfid-five-nodes/darkfid2.toml

@@ -37,6 +37,9 @@ recipient = "9vw6WznKk7xEFQwwXhJWMMdjUPi3cXL8NrFKQpKifG1U"
 # Skip syncing process and start node right away
 skip_sync = false
 
+# Disable transaction's fee verification, used for testing
+skip_fees = false
+
 # Optional sync checkpoint height
 #checkpoint_height = 0
 

+ 3 - 0
contrib/localnet/darkfid-five-nodes/darkfid3.toml

@@ -37,6 +37,9 @@ recipient = "9vw6WznKk7xEFQwwXhJWMMdjUPi3cXL8NrFKQpKifG1U"
 # Skip syncing process and start node right away
 skip_sync = false
 
+# Disable transaction's fee verification, used for testing
+skip_fees = false
+
 # Optional sync checkpoint height
 #checkpoint_height = 0
 

+ 3 - 0
contrib/localnet/darkfid-five-nodes/darkfid4.toml

@@ -37,6 +37,9 @@ recipient = "9vw6WznKk7xEFQwwXhJWMMdjUPi3cXL8NrFKQpKifG1U"
 # Skip syncing process and start node right away
 skip_sync = false
 
+# Disable transaction's fee verification, used for testing
+skip_fees = false
+
 # Optional sync checkpoint height
 #checkpoint_height = 0
 

+ 3 - 0
contrib/localnet/darkfid-single-node/darkfid.toml

@@ -40,6 +40,9 @@ recipient = "9vw6WznKk7xEFQwwXhJWMMdjUPi3cXL8NrFKQpKifG1U"
 # Skip syncing process and start node right away
 skip_sync = true
 
+# Disable transaction's fee verification, used for testing
+skip_fees = false
+
 # Optional sync checkpoint height
 #checkpoint_height = 0
 

+ 3 - 0
contrib/localnet/darkfid-small/darkfid0.toml

@@ -37,6 +37,9 @@ recipient = "9vw6WznKk7xEFQwwXhJWMMdjUPi3cXL8NrFKQpKifG1U"
 # Skip syncing process and start node right away
 skip_sync = true
 
+# Disable transaction's fee verification, used for testing
+skip_fees = false
+
 # Optional sync checkpoint height
 #checkpoint_height = 0
 

+ 3 - 0
contrib/localnet/darkfid-small/darkfid1.toml

@@ -45,6 +45,9 @@ recipient = "9vw6WznKk7xEFQwwXhJWMMdjUPi3cXL8NrFKQpKifG1U"
 # Skip syncing process and start node right away
 skip_sync = false
 
+# Disable transaction's fee verification, used for testing
+skip_fees = false
+
 # Optional sync checkpoint height
 #checkpoint_height = 0
 

+ 3 - 0
contrib/localnet/darkfid-small/darkfid2.toml

@@ -32,6 +32,9 @@ miner = false
 # Skip syncing process and start node right away
 skip_sync = false
 
+# Disable transaction's fee verification, used for testing
+skip_fees = false
+
 # Optional sync checkpoint height
 #checkpoint_height = 0
 

+ 1 - 1
src/contract/test-harness/src/contract_deploy.rs

@@ -103,7 +103,7 @@ impl TestHarness {
         let wallet = self.holders.get_mut(holder).unwrap();
 
         // Execute the transaction
-        wallet.add_transaction("deploy::deploy", tx, block_height, self.verify_fees).await?;
+        wallet.add_transaction("deploy::deploy", tx, block_height).await?;
 
         if !append {
             return Ok(vec![])

+ 1 - 1
src/contract/test-harness/src/dao_exec.rs

@@ -260,7 +260,7 @@ impl TestHarness {
         let wallet = self.holders.get_mut(holder).unwrap();
 
         // Execute the transaction
-        wallet.add_transaction("dao::exec", tx, block_height, self.verify_fees).await?;
+        wallet.add_transaction("dao::exec", tx, block_height).await?;
 
         if !append {
             return Ok(vec![])

+ 1 - 1
src/contract/test-harness/src/dao_mint.rs

@@ -107,7 +107,7 @@ impl TestHarness {
         let wallet = self.holders.get_mut(holder).unwrap();
 
         // Execute the transaction
-        wallet.add_transaction("dao::mint", tx, block_height, self.verify_fees).await?;
+        wallet.add_transaction("dao::mint", tx, block_height).await?;
 
         if !append {
             return Ok(vec![])

+ 1 - 1
src/contract/test-harness/src/dao_propose.rs

@@ -200,7 +200,7 @@ impl TestHarness {
         let wallet = self.holders.get_mut(holder).unwrap();
 
         // Execute the transaction
-        wallet.add_transaction("dao::propose", tx, block_height, self.verify_fees).await?;
+        wallet.add_transaction("dao::propose", tx, block_height).await?;
 
         wallet.money_null_smt_snapshot = Some(wallet.money_null_smt.clone());
 

+ 1 - 1
src/contract/test-harness/src/dao_vote.rs

@@ -149,7 +149,7 @@ impl TestHarness {
         let wallet = self.holders.get_mut(holder).unwrap();
 
         // Execute the transaction
-        wallet.add_transaction("dao::vote", tx, block_height, self.verify_fees).await?;
+        wallet.add_transaction("dao::vote", tx, block_height).await?;
 
         if !append {
             return Ok(vec![])

+ 3 - 2
src/contract/test-harness/src/lib.rs

@@ -204,13 +204,14 @@ impl Wallet {
         callname: &str,
         tx: Transaction,
         block_height: u32,
-        verify_fees: bool,
     ) -> Result<()> {
         if self.bench_wasm {
             benchmark_wasm_calls(callname, &self.validator, &tx, block_height);
         }
 
-        self.validator.add_transactions(&[tx.clone()], block_height, true, verify_fees).await?;
+        self.validator
+            .add_test_transactions(&[tx.clone()], block_height, true, self.validator.verify_fees)
+            .await?;
 
         // Write the data
         {

+ 3 - 2
src/contract/test-harness/src/money_fee.rs

@@ -169,7 +169,7 @@ impl TestHarness {
             .insert_batch(vec![(nullifier, nullifier)])
             .expect("smt.insert_batch()");
 
-        wallet.add_transaction("money::fee", tx, block_height, self.verify_fees).await?;
+        wallet.add_transaction("money::fee", tx, block_height).await?;
         wallet.money_merkle_tree.append(MerkleNode::from(params.output.coin.inner()));
 
         // Attempt to decrypt the output note to see if this is a coin for the holder
@@ -218,7 +218,8 @@ impl TestHarness {
         // and verification.
         let wallet = self.holders.get(holder).unwrap();
         let mut gas_used = FEE_CALL_GAS;
-        gas_used += wallet.validator.add_transactions(&[tx], block_height, false, false).await?;
+        gas_used +=
+            wallet.validator.add_test_transactions(&[tx], block_height, false, false).await?;
 
         // Knowing the total gas, we can now find an OwnCoin of enough value
         // so that we can create a valid Money::Fee call.

+ 1 - 1
src/contract/test-harness/src/money_genesis_mint.rs

@@ -90,7 +90,7 @@ impl TestHarness {
         let wallet = self.holders.get_mut(holder).unwrap();
 
         // Execute the transaction
-        wallet.add_transaction("money::genesis_mint", tx, block_height, self.verify_fees).await?;
+        wallet.add_transaction("money::genesis_mint", tx, block_height).await?;
 
         if !append {
             return Ok(vec![])

+ 1 - 1
src/contract/test-harness/src/money_otc_swap.rs

@@ -186,7 +186,7 @@ impl TestHarness {
         let wallet = self.holders.get_mut(holder).unwrap();
 
         // Execute the transaction
-        wallet.add_transaction("money::otc_swap", tx, block_height, self.verify_fees).await?;
+        wallet.add_transaction("money::otc_swap", tx, block_height).await?;
 
         let mut found_owncoins = vec![];
 

+ 2 - 2
src/contract/test-harness/src/money_token.rs

@@ -183,7 +183,7 @@ impl TestHarness {
         let wallet = self.holders.get_mut(holder).unwrap();
 
         // Execute the transaction
-        wallet.add_transaction("money::token_mint", tx, block_height, self.verify_fees).await?;
+        wallet.add_transaction("money::token_mint", tx, block_height).await?;
 
         // Iterate over all inputs to mark any spent coins
         if let Some(ref fee_params) = fee_params {
@@ -335,7 +335,7 @@ impl TestHarness {
         let wallet = self.holders.get_mut(holder).unwrap();
 
         // Execute the transaction
-        wallet.add_transaction("money::token_freeze", tx, block_height, self.verify_fees).await?;
+        wallet.add_transaction("money::token_freeze", tx, block_height).await?;
 
         let mut found_owncoins = vec![];
         if let Some(ref fee_params) = fee_params {

+ 1 - 1
src/contract/test-harness/src/money_transfer.rs

@@ -126,7 +126,7 @@ impl TestHarness {
         let wallet = self.holders.get_mut(holder).unwrap();
 
         // Execute the transaction
-        wallet.add_transaction("money::transfer", tx, block_height, self.verify_fees).await?;
+        wallet.add_transaction("money::transfer", tx, block_height).await?;
 
         // Iterate over all inputs to mark any spent coins
         let mut inputs: Vec<Input> = call_params.inputs.to_vec();

+ 12 - 6
src/validator/consensus.rs

@@ -558,7 +558,11 @@ impl Fork {
     }
 
     /// Generate an unsigned block containing all pending transactions.
-    pub async fn generate_unsigned_block(&self, producer_tx: Transaction) -> Result<BlockInfo> {
+    pub async fn generate_unsigned_block(
+        &self,
+        producer_tx: Transaction,
+        verify_fees: bool,
+    ) -> Result<BlockInfo> {
         // Grab forks' last block proposal(previous)
         let previous = self.last_proposal()?;
 
@@ -566,7 +570,8 @@ impl Fork {
         let next_block_height = previous.block.header.height + 1;
 
         // Grab forks' unproposed transactions
-        let mut unproposed_txs = self.unproposed_txs(&self.blockchain, next_block_height).await?;
+        let mut unproposed_txs =
+            self.unproposed_txs(&self.blockchain, next_block_height, verify_fees).await?;
         unproposed_txs.push(producer_tx);
 
         // Generate the new header
@@ -589,8 +594,9 @@ impl Fork {
         &self,
         producer_tx: Transaction,
         secret_key: &SecretKey,
+        verify_fees: bool,
     ) -> Result<Proposal> {
-        let mut block = self.generate_unsigned_block(producer_tx).await?;
+        let mut block = self.generate_unsigned_block(producer_tx, verify_fees).await?;
 
         // Sign block
         block.sign(secret_key);
@@ -663,6 +669,7 @@ impl Fork {
         &self,
         blockchain: &Blockchain,
         verifying_block_height: u32,
+        verify_fees: bool,
     ) -> Result<Vec<Transaction>> {
         // Check if our mempool is not empty
         if self.mempool.is_empty() {
@@ -703,20 +710,19 @@ impl Fork {
 
             // Verify the transaction against current state
             overlay.lock().unwrap().checkpoint();
-            // TODO: shouldn't verify fees be true?
             match verify_transaction(
                 &overlay,
                 verifying_block_height,
                 &unproposed_tx,
                 &mut tree,
                 &mut vks,
-                false,
+                verify_fees,
             )
             .await
             {
                 Ok(gas) => _gas_used += gas,
                 Err(e) => {
-                    debug!(target: "validator::verification::verify_transactions", "Transaction verification failed: {}", e);
+                    debug!(target: "validator::consensus::unproposed_txs", "Transaction verification failed: {}", e);
                     overlay.lock().unwrap().revert_to_checkpoint()?;
                     continue
                 }

+ 7 - 5
src/validator/mod.rs

@@ -163,7 +163,7 @@ impl Validator {
                 next_block_height,
                 &tx_vec,
                 &mut MerkleTree::new(1),
-                false,
+                self.verify_fees,
             )
             .await
             {
@@ -232,7 +232,7 @@ impl Validator {
                     next_block_height,
                     &tx_vec,
                     &mut MerkleTree::new(1),
-                    false,
+                    self.verify_fees,
                 )
                 .await
                 {
@@ -256,7 +256,7 @@ impl Validator {
                 next_block_height,
                 &tx_vec,
                 &mut MerkleTree::new(1),
-                false,
+                self.verify_fees,
             )
             .await
             {
@@ -547,10 +547,12 @@ impl Validator {
     /// Validate a set of [`Transaction`] in sequence and apply them if all are valid.
     /// In case any of the transactions fail, they will be returned to the caller.
     /// The function takes a boolean called `write` which tells it to actually write
-    /// the state transitions to the database.
+    /// the state transitions to the database, and a boolean called `verify_fees` to
+    /// overwrite the nodes configured `verify_fees` flag.
     ///
     /// Returns the total gas used for the given transactions.
-    pub async fn add_transactions(
+    /// Note: this function should only be used in tests.
+    pub async fn add_test_transactions(
         &self,
         txs: &[Transaction],
         verifying_block_height: u32,