integration.rs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /* This file is part of DarkFi (https://dark.fi)
  2. *
  3. * Copyright (C) 2020-2023 Dyne.org foundation
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU Affero General Public License as
  7. * published by the Free Software Foundation, either version 3 of the
  8. * License, or (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU Affero General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Affero General Public License
  16. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  17. */
  18. //! Integration test for functionalities of the money smart contract:
  19. //!
  20. //! * Airdrops of the native token from the faucet
  21. //! * Arbitrary token minting
  22. //! * Transfers/Payments
  23. //! * Atomic swaps
  24. //! * Token mint freezing
  25. //!
  26. //! With this test we want to confirm the money contract state transitions
  27. //! work between multiple parties and are able to be verified.
  28. //!
  29. //! TODO: Malicious cases
  30. use darkfi::Result;
  31. use darkfi_contract_test_harness::{init_logger, Holder, TestHarness};
  32. use log::info;
  33. #[async_std::test]
  34. async fn money_integration() -> Result<()> {
  35. init_logger();
  36. // Holders this test will use
  37. const HOLDERS: [Holder; 3] = [Holder::Faucet, Holder::Alice, Holder::Bob];
  38. // Some numbers we want to assert
  39. const ALICE_NATIVE_AIRDROP: u64 = 10000000000; // 100 DRK
  40. const BOB_SUPPLY: u64 = 2000000000; // 10 BOB
  41. // Slot to verify against
  42. let current_slot = 0;
  43. // Initialize harness
  44. let mut th = TestHarness::new(&["money".to_string()]).await?;
  45. info!("[Faucet] Building Alice airdrop tx");
  46. let (airdrop_tx, airdrop_params) = th.airdrop_native(ALICE_NATIVE_AIRDROP, Holder::Alice)?;
  47. info!("[Faucet] Executing Alice airdrop tx");
  48. th.execute_airdrop_native_tx(Holder::Faucet, &airdrop_tx, &airdrop_params, current_slot)
  49. .await?;
  50. info!("[Alice] Executing Alice airdrop tx");
  51. th.execute_airdrop_native_tx(Holder::Alice, &airdrop_tx, &airdrop_params, current_slot).await?;
  52. info!("[Bob] Executing Alice airdrop tx");
  53. th.execute_airdrop_native_tx(Holder::Bob, &airdrop_tx, &airdrop_params, current_slot).await?;
  54. th.assert_trees(&HOLDERS);
  55. // Alice gathers her new coin
  56. th.gather_owncoin(Holder::Alice, airdrop_params.outputs[0].clone(), None)?;
  57. info!("[Bob] Building BOB token mint tx");
  58. let (token_mint_tx, token_mint_params) = th.token_mint(BOB_SUPPLY, Holder::Bob, Holder::Bob)?;
  59. info!("[Faucet] Executing BOB token mint tx");
  60. th.execute_token_mint_tx(Holder::Faucet, &token_mint_tx, &token_mint_params, current_slot)
  61. .await?;
  62. info!("[Alice] Executing BOB token mint tx");
  63. th.execute_token_mint_tx(Holder::Alice, &token_mint_tx, &token_mint_params, current_slot)
  64. .await?;
  65. info!("[Bob] Executing BOB token mint tx");
  66. th.execute_token_mint_tx(Holder::Bob, &token_mint_tx, &token_mint_params, current_slot).await?;
  67. th.assert_trees(&HOLDERS);
  68. // Bob gathers his new coin
  69. th.gather_owncoin(Holder::Bob, token_mint_params.output.clone(), None)?;
  70. info!("[Bob] Building BOB token freeze tx");
  71. let (token_frz_tx, token_frz_params) = th.token_freeze(Holder::Bob)?;
  72. info!("[Faucet] Executing BOB token freeze tx");
  73. th.execute_token_freeze_tx(Holder::Faucet, &token_frz_tx, &token_frz_params, current_slot)
  74. .await?;
  75. info!("[Alice] Executing BOB token freeze tx");
  76. th.execute_token_freeze_tx(Holder::Alice, &token_frz_tx, &token_frz_params, current_slot)
  77. .await?;
  78. info!("[Bob] Executing BOB token freeze tx");
  79. th.execute_token_freeze_tx(Holder::Bob, &token_frz_tx, &token_frz_params, current_slot).await?;
  80. th.assert_trees(&HOLDERS);
  81. // Statistics
  82. th.statistics();
  83. // Thanks for reading
  84. Ok(())
  85. }