ソースを参照

validator: Configurable fee verification, incomplete

It's now only used in add_transactions(), but needs to be in other
places as well.
parazyd 2 年 前
コミット
036afda345

+ 4 - 0
bin/darkfid2/src/main.rs

@@ -234,18 +234,21 @@ async fn realmain(args: Args, ex: Arc<smol::Executor<'static>>) -> Result<()> {
 
     // Initialize validator configuration
     let genesis_txs_total = genesis_txs_total(&genesis_block.txs).await?;
+
     let time_keeper = TimeKeeper::new(
         genesis_block.header.timestamp,
         blockchain_config.epoch_length,
         blockchain_config.slot_time,
         0,
     );
+
     let pow_fixed_difficulty = if let Some(diff) = blockchain_config.pow_fixed_difficulty {
         info!(target: "darkfid", "Node is configured to run with fixed PoW difficulty: {}", diff);
         Some(diff.into())
     } else {
         None
     };
+
     let config = ValidatorConfig::new(
         time_keeper,
         blockchain_config.threshold,
@@ -256,6 +259,7 @@ async fn realmain(args: Args, ex: Arc<smol::Executor<'static>>) -> Result<()> {
         genesis_txs_total,
         vec![],
         blockchain_config.pos_testing_mode,
+        false, // TODO: Make configurable
     );
 
     // Initialize validator

+ 8 - 2
bin/darkfid2/src/tests/harness.rs

@@ -60,9 +60,14 @@ pub struct Harness {
 }
 
 impl Harness {
-    pub async fn new(config: HarnessConfig, ex: &Arc<smol::Executor<'static>>) -> Result<Self> {
+    pub async fn new(
+        config: HarnessConfig,
+        verify_fees: bool,
+        ex: &Arc<smol::Executor<'static>>,
+    ) -> Result<Self> {
         // Use test harness to generate genesis transactions
-        let mut th = TestHarness::new(&["money".to_string(), "consensus".to_string()]).await?;
+        let mut th =
+            TestHarness::new(&["money".to_string(), "consensus".to_string()], verify_fees).await?;
         let (genesis_stake_tx, _) = th.genesis_stake(&Holder::Alice, config.alice_initial)?;
         let (genesis_mint_tx, _) = th.genesis_mint(&Holder::Bob, config.bob_initial)?;
 
@@ -89,6 +94,7 @@ impl Harness {
             genesis_txs_total,
             vec![],
             config.pos_testing_mode,
+            verify_fees,
         );
 
         // Generate validators using pregenerated vks

+ 1 - 1
bin/darkfid2/src/tests/mod.rs

@@ -43,7 +43,7 @@ async fn sync_pos_blocks_real(ex: Arc<Executor<'static>>) -> Result<()> {
         alice_initial: 1000,
         bob_initial: 500,
     };
-    let th = Harness::new(config, &ex).await?;
+    let th = Harness::new(config, false, &ex).await?;
 
     // Retrieve genesis block
     let previous = th.alice.validator.blockchain.last_block()?;

+ 2 - 1
src/contract/consensus/tests/genesis_stake_unstake.rs

@@ -45,7 +45,8 @@ fn consensus_contract_genesis_stake_unstake() -> Result<()> {
         let mut current_slot = 0;
 
         // Initialize harness
-        let mut th = TestHarness::new(&["money".to_string(), "consensus".to_string()]).await?;
+        let mut th =
+            TestHarness::new(&["money".to_string(), "consensus".to_string()], false).await?;
 
         // Now Alice can create a genesis stake transaction to mint
         // some staked coins

+ 2 - 1
src/contract/consensus/tests/stake_unstake.rs

@@ -53,7 +53,8 @@ fn consensus_contract_stake_unstake() -> Result<()> {
         let mut current_slot = 1;
 
         // Initialize harness
-        let mut th = TestHarness::new(&["money".to_string(), "consensus".to_string()]).await?;
+        let mut th =
+            TestHarness::new(&["money".to_string(), "consensus".to_string()], false).await?;
 
         // Now Alice can airdrop some native tokens to herself
         let alice_oc =

+ 1 - 1
src/contract/dao/tests/integration.rs

@@ -50,7 +50,7 @@ fn integration_test() -> Result<()> {
         ];
 
         // Initialize harness
-        let mut th = TestHarness::new(&["money".to_string(), "dao".to_string()]).await?;
+        let mut th = TestHarness::new(&["money".to_string(), "dao".to_string()], false).await?;
 
         // We'll use the ALICE token as the DAO governance token
         let gov_token_id = th.token_id(&Holder::Alice);

+ 2 - 1
src/contract/deployooor/tests/integration.rs

@@ -29,7 +29,8 @@ fn deploy_integration() -> Result<()> {
         let current_slot = 0;
 
         // Initialize harness
-        let mut th = TestHarness::new(&["money".to_string(), "deployooor".to_string()]).await?;
+        let mut th =
+            TestHarness::new(&["money".to_string(), "deployooor".to_string()], false).await?;
 
         // WASM bincode to deploy
         let wasm_bincode = include_bytes!("../../dao/darkfi_dao_contract.wasm");

+ 1 - 1
src/contract/money/tests/genesis_mint.rs

@@ -53,7 +53,7 @@ fn genesis_mint() -> Result<()> {
         let current_slot = 0;
 
         // Initialize harness
-        let mut th = TestHarness::new(&["money".to_string()]).await?;
+        let mut th = TestHarness::new(&["money".to_string()], false).await?;
 
         let mut alice_owncoins = vec![];
         let mut bob_owncoins = vec![];

+ 1 - 1
src/contract/money/tests/integration.rs

@@ -29,7 +29,7 @@ fn money_integration() -> Result<()> {
         const HOLDERS: [Holder; 2] = [Holder::Alice, Holder::Bob];
 
         // Initialize harness
-        let mut th = TestHarness::new(&["money".to_string()]).await?;
+        let mut th = TestHarness::new(&["money".to_string()], true).await?;
 
         // Current verification slot
         let mut verification_slot = 1;

+ 1 - 1
src/contract/money/tests/mint_pay_swap.rs

@@ -54,7 +54,7 @@ fn mint_pay_swap() -> Result<()> {
         let current_slot = 0;
 
         // Initialize harness
-        let mut th = TestHarness::new(&["money".to_string()]).await?;
+        let mut th = TestHarness::new(&["money".to_string()], false).await?;
 
         let mut alice_owncoins = vec![];
         let mut bob_owncoins = vec![];

+ 1 - 1
src/contract/money/tests/pow_reward.rs

@@ -41,7 +41,7 @@ fn pow_reward() -> Result<()> {
         let mut current_height = 0;
 
         // Initialize harness
-        let mut th = TestHarness::new(&["money".to_string()]).await?;
+        let mut th = TestHarness::new(&["money".to_string()], false).await?;
 
         let mut alice_owncoins = vec![];
         let mut bob_owncoins = vec![];

+ 1 - 1
src/contract/money/tests/token_mint.rs

@@ -35,7 +35,7 @@ fn token_mint() -> Result<()> {
         let current_slot = 0;
 
         // Initialize harness
-        let mut th = TestHarness::new(&["money".to_string()]).await?;
+        let mut th = TestHarness::new(&["money".to_string()], false).await?;
 
         info!("[Bob] Building BOB token mint tx");
         let (token_mint_tx, token_mint_params) =

+ 1 - 1
src/contract/money/tests/txs_verification.rs

@@ -47,7 +47,7 @@ fn txs_verification() -> Result<()> {
         let current_slot = 0;
 
         // Initialize harness
-        let mut th = TestHarness::new(&["money".to_string()]).await?;
+        let mut th = TestHarness::new(&["money".to_string()], false).await?;
 
         let mut alice_owncoins = vec![];
         let mut bob_owncoins = vec![];

+ 2 - 2
src/contract/money/tests/verification_bench.rs

@@ -50,7 +50,7 @@ fn alice2alice_random_amounts() -> Result<()> {
         }
 
         // Initialize harness
-        let mut th = TestHarness::new(&["money".to_string()]).await?;
+        let mut th = TestHarness::new(&["money".to_string()], false).await?;
 
         info!(target: "money", "[Faucet] ========================");
         info!(target: "money", "[Faucet] Building Alice's airdrop");
@@ -143,7 +143,7 @@ fn alice2alice_multiplecoins_random_amounts() -> Result<()> {
         }
 
         // Initialize harness
-        let mut th = TestHarness::new(&["money".to_string()]).await?;
+        let mut th = TestHarness::new(&["money".to_string()], false).await?;
 
         // Mint 10 coins
         let mut token_ids = vec![];

+ 13 - 7
src/contract/test-harness/src/lib.rs

@@ -148,6 +148,7 @@ impl Wallet {
         genesis_block: &BlockInfo,
         faucet_pubkeys: &[PublicKey],
         vks: &Vks,
+        verify_fees: bool,
     ) -> Result<Self> {
         let wallet = WalletDb::new(None, None)?;
         let sled_db = sled::Config::new().temporary(true).open()?;
@@ -169,6 +170,7 @@ impl Wallet {
             0,
             faucet_pubkeys.to_vec(),
             false,
+            verify_fees,
         );
         let validator = Validator::new(&sled_db, config).await?;
 
@@ -214,7 +216,7 @@ pub struct TestHarness {
 }
 
 impl TestHarness {
-    pub async fn new(_contracts: &[String]) -> Result<Self> {
+    pub async fn new(_contracts: &[String], verify_fees: bool) -> Result<Self> {
         let mut holders = HashMap::new();
         let mut genesis_block = BlockInfo::default();
         genesis_block.header.timestamp = Timestamp(1689772567);
@@ -236,27 +238,31 @@ impl TestHarness {
 
         let faucet_kp = Keypair::random(&mut rng);
         let faucet_pubkeys = vec![faucet_kp.public];
-        let faucet = Wallet::new(faucet_kp, &genesis_block, &faucet_pubkeys, &vks).await?;
+        let faucet =
+            Wallet::new(faucet_kp, &genesis_block, &faucet_pubkeys, &vks, verify_fees).await?;
         holders.insert(Holder::Faucet, faucet);
 
         let alice_kp = Keypair::random(&mut rng);
-        let alice = Wallet::new(alice_kp, &genesis_block, &faucet_pubkeys, &vks).await?;
+        let alice =
+            Wallet::new(alice_kp, &genesis_block, &faucet_pubkeys, &vks, verify_fees).await?;
         holders.insert(Holder::Alice, alice);
 
         let bob_kp = Keypair::random(&mut rng);
-        let bob = Wallet::new(bob_kp, &genesis_block, &faucet_pubkeys, &vks).await?;
+        let bob = Wallet::new(bob_kp, &genesis_block, &faucet_pubkeys, &vks, verify_fees).await?;
         holders.insert(Holder::Bob, bob);
 
         let charlie_kp = Keypair::random(&mut rng);
-        let charlie = Wallet::new(charlie_kp, &genesis_block, &faucet_pubkeys, &vks).await?;
+        let charlie =
+            Wallet::new(charlie_kp, &genesis_block, &faucet_pubkeys, &vks, verify_fees).await?;
         holders.insert(Holder::Charlie, charlie);
 
         let rachel_kp = Keypair::random(&mut rng);
-        let rachel = Wallet::new(rachel_kp, &genesis_block, &faucet_pubkeys, &vks).await?;
+        let rachel =
+            Wallet::new(rachel_kp, &genesis_block, &faucet_pubkeys, &vks, verify_fees).await?;
         holders.insert(Holder::Rachel, rachel);
 
         let dao_kp = Keypair::random(&mut rng);
-        let dao = Wallet::new(dao_kp, &genesis_block, &faucet_pubkeys, &vks).await?;
+        let dao = Wallet::new(dao_kp, &genesis_block, &faucet_pubkeys, &vks, verify_fees).await?;
         holders.insert(Holder::Dao, dao);
 
         // Build benchmarks map

+ 16 - 4
src/validator/mod.rs

@@ -87,6 +87,8 @@ pub struct ValidatorConfig {
     pub faucet_pubkeys: Vec<PublicKey>,
     /// Flag to enable PoS testing mode
     pub pos_testing_mode: bool,
+    /// Flag to enable tx fee verification
+    pub verify_fees: bool,
 }
 
 impl ValidatorConfig {
@@ -101,6 +103,7 @@ impl ValidatorConfig {
         genesis_txs_total: u64,
         faucet_pubkeys: Vec<PublicKey>,
         pos_testing_mode: bool,
+        verify_fees: bool,
     ) -> Self {
         Self {
             time_keeper,
@@ -112,6 +115,7 @@ impl ValidatorConfig {
             genesis_txs_total,
             faucet_pubkeys,
             pos_testing_mode,
+            verify_fees,
         }
     }
 }
@@ -129,6 +133,8 @@ pub struct Validator {
     pub synced: RwLock<bool>,
     /// Flag to enable PoS testing mode
     pub pos_testing_mode: bool,
+    /// Flag to enable tx fee verification
+    pub verify_fees: bool,
 }
 
 impl Validator {
@@ -172,10 +178,15 @@ impl Validator {
         )?;
 
         // Create the actual state
-        let state =
-            Arc::new(Self { blockchain, consensus, synced: RwLock::new(false), pos_testing_mode });
-        info!(target: "validator::new", "Finished initializing validator");
+        let state = Arc::new(Self {
+            blockchain,
+            consensus,
+            synced: RwLock::new(false),
+            pos_testing_mode,
+            verify_fees: config.verify_fees,
+        });
 
+        info!(target: "validator::new", "Finished initializing validator");
         Ok(state)
     }
 
@@ -471,7 +482,8 @@ impl Validator {
         );
 
         // Verify all transactions and get erroneous ones
-        let erroneous_txs = verify_transactions(&overlay, &time_keeper, txs, false).await?;
+        let erroneous_txs =
+            verify_transactions(&overlay, &time_keeper, txs, self.verify_fees).await?;
 
         let lock = overlay.lock().unwrap();
         let mut overlay = lock.overlay.lock().unwrap();