Procházet zdrojové kódy

contract/money: Port genesis-mint test

parazyd před 2 roky
rodič
revize
23abd8c526
1 změnil soubory, kde provedl 13 přidání a 118 odebrání
  1. 13 118
      src/contract/money/tests/genesis_mint.rs

+ 13 - 118
src/contract/money/tests/genesis_mint.rs

@@ -26,8 +26,7 @@
 //! with detection of erroneous transactions.
 
 use darkfi::Result;
-use darkfi_contract_test_harness::{init_logger, Holder, TestHarness, TxAction};
-use darkfi_money_contract::model::DARK_TOKEN_ID;
+use darkfi_contract_test_harness::{init_logger, Holder, TestHarness};
 use log::info;
 
 #[test]
@@ -36,34 +35,25 @@ fn genesis_mint() -> Result<()> {
         init_logger();
 
         // Holders this test will use
-        const HOLDERS: [Holder; 3] = [Holder::Faucet, Holder::Alice, Holder::Bob];
+        const HOLDERS: [Holder; 2] = [Holder::Alice, Holder::Bob];
 
         // Some numbers we want to assert
         const ALICE_INITIAL: u64 = 100;
         const BOB_INITIAL: u64 = 200;
 
-        // Alice = 50 DARK
-        // Bob = 250 DARK
-        const ALICE_SEND: u64 = ALICE_INITIAL - 50;
-        // Alice = 230 DARK
-        // Bob = 50
-        const BOB_SEND: u64 = BOB_INITIAL - 20;
-
         // Block height to verify against
         let current_block_height = 0;
 
         // Initialize harness
-        let mut th = TestHarness::new(&["money".to_string()], false).await?;
-
-        let mut alice_owncoins = vec![];
-        let mut bob_owncoins = vec![];
+        let mut th = TestHarness::new(&HOLDERS, false).await?;
 
         info!(target: "money", "[Alice] ========================");
         info!(target: "money", "[Alice] Building genesis mint tx");
         info!(target: "money", "[Alice] ========================");
         let (genesis_mint_tx, genesis_mint_params) =
-            th.genesis_mint(&Holder::Alice, ALICE_INITIAL)?;
+            th.genesis_mint(&Holder::Alice, ALICE_INITIAL).await?;
 
+        /* FIXME:
         // We are going to use alice genesis mint transaction to
         // test some malicious cases.
         info!(target: "money", "[Malicious] ==================================");
@@ -89,6 +79,7 @@ fn genesis_mint() -> Result<()> {
             1,
         )
         .await?;
+        */
 
         for holder in &HOLDERS {
             info!(target: "money", "[{holder:?}] ================================");
@@ -96,24 +87,21 @@ fn genesis_mint() -> Result<()> {
             info!(target: "money", "[{holder:?}] ================================");
             th.execute_genesis_mint_tx(
                 holder,
-                &genesis_mint_tx,
+                genesis_mint_tx.clone(),
                 &genesis_mint_params,
                 current_block_height,
+                true,
             )
             .await?;
         }
 
         th.assert_trees(&HOLDERS);
 
-        // Alice gathers her new owncoin
-        let alice_oc =
-            th.gather_owncoin_from_output(&Holder::Alice, &genesis_mint_params.output, None)?;
-        alice_owncoins.push(alice_oc);
-
         info!(target: "money", "[Bob] ========================");
         info!(target: "money", "[Bob] Building genesis mint tx");
         info!(target: "money", "[Bob] ========================");
-        let (genesis_mint_tx, genesis_mint_params) = th.genesis_mint(&Holder::Bob, BOB_INITIAL)?;
+        let (genesis_mint_tx, genesis_mint_params) =
+            th.genesis_mint(&Holder::Bob, BOB_INITIAL).await?;
 
         for holder in &HOLDERS {
             info!(target: "money", "[{holder:?}] =============================");
@@ -121,43 +109,9 @@ fn genesis_mint() -> Result<()> {
             info!(target: "money", "[{holder:?}] =============================");
             th.execute_genesis_mint_tx(
                 holder,
-                &genesis_mint_tx,
+                genesis_mint_tx.clone(),
                 &genesis_mint_params,
                 current_block_height,
-            )
-            .await?;
-        }
-
-        th.assert_trees(&HOLDERS);
-
-        // Bob gathers his new owncoin
-        let bob_oc =
-            th.gather_owncoin_from_output(&Holder::Bob, &genesis_mint_params.output, None)?;
-        bob_owncoins.push(bob_oc);
-
-        // Now Alice can send a little bit of funds to Bob
-        info!(target: "money", "[Alice] ====================================================");
-        info!(target: "money", "[Alice] Building Money::Transfer params for a payment to Bob");
-        info!(target: "money", "[Alice] ====================================================");
-        let (transfer_tx, transfer_params, spent_coins) =
-            th.transfer(ALICE_SEND, &Holder::Alice, &Holder::Bob, &alice_owncoins, *DARK_TOKEN_ID)?;
-
-        // Validating transfer params
-        assert!(transfer_params.inputs.len() == 1);
-        assert!(transfer_params.outputs.len() == 2);
-        assert!(spent_coins.len() == 1);
-        alice_owncoins.retain(|x| x != &spent_coins[0]);
-        assert!(alice_owncoins.is_empty());
-
-        for holder in &HOLDERS {
-            info!(target: "money", "[{holder:?}] ==============================");
-            info!(target: "money", "[{holder:?}] Executing Alice2Bob payment tx");
-            info!(target: "money", "[{holder:?}] ==============================");
-            th.execute_transfer_tx(
-                holder,
-                &transfer_tx,
-                &transfer_params,
-                current_block_height,
                 true,
             )
             .await?;
@@ -165,70 +119,11 @@ fn genesis_mint() -> Result<()> {
 
         th.assert_trees(&HOLDERS);
 
-        // Bob should have his old OwnCoin, and this new one.
-        let bob_oc =
-            th.gather_owncoin_from_output(&Holder::Bob, &transfer_params.outputs[0], None)?;
-        bob_owncoins.push(bob_oc);
-
-        // Alice should now have one OwnCoin with the change from the above transaction.
-        let alice_oc =
-            th.gather_owncoin_from_output(&Holder::Alice, &transfer_params.outputs[1], None)?;
-        alice_owncoins.push(alice_oc);
-
+        let alice_owncoins = &th.holders.get(&Holder::Alice).unwrap().unspent_money_coins;
+        let bob_owncoins = &th.holders.get(&Holder::Bob).unwrap().unspent_money_coins;
         assert!(alice_owncoins.len() == 1);
-        assert!(bob_owncoins.len() == 2);
-
-        // Bob can send a little bit to Alice as well
-        info!(target: "money", "[Bob] ======================================================");
-        info!(target: "money", "[Bob] Building Money::Transfer params for a payment to Alice");
-        info!(target: "money", "[Bob] ======================================================");
-        let (transfer_tx, transfer_params, spent_coins) =
-            th.transfer(BOB_SEND, &Holder::Bob, &Holder::Alice, &bob_owncoins, *DARK_TOKEN_ID)?;
-
-        // Validating transfer params
-        assert!(transfer_params.inputs.len() == 1);
-        assert!(transfer_params.outputs.len() == 2);
-        assert!(spent_coins.len() == 1);
-        bob_owncoins.retain(|x| x != &spent_coins[0]);
         assert!(bob_owncoins.len() == 1);
 
-        for holder in &HOLDERS {
-            info!(target: "money", "[{holder:?}] ==============================");
-            info!(target: "money", "[{holder:?}] Executing Bob2Alice payment tx");
-            info!(target: "money", "[{holder:?}] ==============================");
-            th.execute_transfer_tx(
-                holder,
-                &transfer_tx,
-                &transfer_params,
-                current_block_height,
-                true,
-            )
-            .await?;
-        }
-
-        th.assert_trees(&HOLDERS);
-
-        // Alice should now have two OwnCoins
-        let alice_oc =
-            th.gather_owncoin_from_output(&Holder::Alice, &transfer_params.outputs[0], None)?;
-        alice_owncoins.push(alice_oc);
-
-        // Bob should have two with the change from the above tx
-        let bob_oc =
-            th.gather_owncoin_from_output(&Holder::Bob, &transfer_params.outputs[1], None)?;
-        bob_owncoins.push(bob_oc);
-
-        // Validating transaction outcomes
-        assert!(alice_owncoins.len() == 2);
-        assert!(bob_owncoins.len() == 2);
-        assert!(alice_owncoins[0].note.value == ALICE_INITIAL - ALICE_SEND);
-        assert!(alice_owncoins[1].note.value == BOB_SEND);
-        assert!(bob_owncoins[0].note.value == ALICE_SEND);
-        assert!(bob_owncoins[1].note.value == BOB_INITIAL - BOB_SEND);
-
-        // Statistics
-        th.statistics();
-
         // Thanks for reading
         Ok(())
     })