verification_bench.rs 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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. use std::{env, str::FromStr};
  19. use darkfi::Result;
  20. use darkfi_contract_test_harness::{init_logger, Holder, TestHarness};
  21. use log::info;
  22. use rand::{prelude::IteratorRandom, Rng};
  23. #[test]
  24. #[ignore]
  25. fn alice2alice_random_amounts() -> Result<()> {
  26. smol::block_on(async {
  27. init_logger();
  28. // Holders this test will use
  29. const HOLDERS: [Holder; 2] = [Holder::Faucet, Holder::Alice];
  30. const ALICE_AIRDROP: u64 = 1000;
  31. // Slot to verify against
  32. let current_slot = 0;
  33. // n transactions to loop
  34. let mut n = 3;
  35. for arg in env::args() {
  36. match usize::from_str(&arg) {
  37. Ok(v) => {
  38. n = v;
  39. break
  40. }
  41. Err(_) => continue,
  42. };
  43. }
  44. // Initialize harness
  45. let mut th = TestHarness::new(&["money".to_string()]).await?;
  46. info!(target: "money", "[Faucet] ========================");
  47. info!(target: "money", "[Faucet] Building Alice's airdrop");
  48. info!(target: "money", "[Faucet] ========================");
  49. let (airdrop_tx, airdrop_params) =
  50. th.airdrop_native(ALICE_AIRDROP, &Holder::Alice, None, None)?;
  51. for holder in &HOLDERS {
  52. info!(target: "money", "[{holder:?}] ==========================");
  53. info!(target: "money", "[{holder:?}] Executing Alice airdrop tx");
  54. info!(target: "money", "[{holder:?}] ==========================");
  55. th.execute_airdrop_native_tx(holder, &airdrop_tx, &airdrop_params, current_slot)
  56. .await?;
  57. }
  58. th.assert_trees(&HOLDERS);
  59. // Gather new owncoins
  60. let mut owncoins = vec![];
  61. let owncoin = th.gather_owncoin(&Holder::Alice, &airdrop_params.outputs[0], None)?;
  62. let token_id = owncoin.note.token_id;
  63. owncoins.push(owncoin);
  64. // Execute transactions loop
  65. for i in 0..n {
  66. info!(target: "money", "[Alice] ===============================================");
  67. info!(target: "money", "[Alice] Building Money::Transfer params for transfer {}", i);
  68. info!(target: "money", "[Alice] Alice coins: {}", owncoins.len());
  69. for (i, c) in owncoins.iter().enumerate() {
  70. info!(target: "money", "[Alice] \t coin {} value: {}", i, c.note.value);
  71. }
  72. let amount = rand::thread_rng().gen_range(1..ALICE_AIRDROP);
  73. info!(target: "money", "[Alice] Sending: {}", amount);
  74. info!(target: "money", "[Alice] ===============================================");
  75. let (tx, params, spent_coins) =
  76. th.transfer(amount, &Holder::Alice, &Holder::Alice, &owncoins, token_id)?;
  77. // Remove the owncoins we've spent
  78. for spent in spent_coins {
  79. owncoins.retain(|x| x != &spent);
  80. }
  81. // Verify transaction
  82. info!(target: "money", "[Faucet] ================================");
  83. info!(target: "money", "[Faucet] Executing Alice2Alice payment tx");
  84. info!(target: "money", "[Faucet] ================================");
  85. th.execute_transfer_tx(&Holder::Faucet, &tx, &params, current_slot, true).await?;
  86. info!(target: "money", "[Alice] ================================");
  87. info!(target: "money", "[Alice] Executing Alice2Alice payment tx");
  88. info!(target: "money", "[Alice] ================================");
  89. th.execute_transfer_tx(&Holder::Alice, &tx, &params, current_slot, false).await?;
  90. // Gather new owncoins
  91. owncoins.append(&mut th.gather_multiple_owncoins(&Holder::Alice, &params.outputs)?);
  92. th.assert_trees(&HOLDERS);
  93. }
  94. // Statistics
  95. th.statistics();
  96. // Thanks for reading
  97. Ok(())
  98. })
  99. }
  100. #[test]
  101. #[ignore]
  102. fn alice2alice_multiplecoins_random_amounts() -> Result<()> {
  103. smol::block_on(async {
  104. init_logger();
  105. // Holders this test will use
  106. const HOLDERS: [Holder; 2] = [Holder::Faucet, Holder::Alice];
  107. // Slot to verify against
  108. let current_slot = 0;
  109. // N blocks to simulate
  110. let mut n = 3;
  111. for arg in env::args() {
  112. match usize::from_str(&arg) {
  113. Ok(v) => {
  114. n = v;
  115. break
  116. }
  117. Err(_) => continue,
  118. };
  119. }
  120. // Initialize harness
  121. let mut th = TestHarness::new(&["money".to_string()]).await?;
  122. // Mint 10 coins
  123. let mut token_ids = vec![];
  124. let mut minted_amounts = vec![];
  125. let mut owncoins = vec![];
  126. for i in 0..10 {
  127. let amount = rand::thread_rng().gen_range(2..1000);
  128. info!(target: "money", "[Faucet] ===================================================");
  129. info!(target: "money", "[Faucet] Building Money::Mint params for Alice's mint for token {} and amount {}", i, amount);
  130. info!(target: "money", "[Faucet] ===================================================");
  131. let (mint_tx, mint_params) =
  132. th.token_mint(amount, &Holder::Alice, &Holder::Alice, None, None)?;
  133. for holder in &HOLDERS {
  134. info!(target: "money", "[{holder:?}] =======================");
  135. info!(target: "money", "[{holder:?}] Executing Alice mint tx");
  136. info!(target: "money", "[{holder:?}] =======================");
  137. th.execute_token_mint_tx(holder, &mint_tx, &mint_params, current_slot).await?;
  138. }
  139. th.assert_trees(&HOLDERS);
  140. // Gather new owncoins
  141. let owncoin = th.gather_owncoin(&Holder::Alice, &mint_params.output, None)?;
  142. let token_id = owncoin.note.token_id;
  143. owncoins.push(vec![owncoin]);
  144. minted_amounts.push(amount);
  145. token_ids.push(token_id);
  146. }
  147. // Simulating N blocks
  148. for b in 0..n {
  149. // Get a random sized sample of owncoins
  150. let sample = (0..10)
  151. .choose_multiple(&mut rand::thread_rng(), rand::thread_rng().gen_range(1..10));
  152. info!(target: "money", "[Alice] =====================================");
  153. info!(target: "money", "[Alice] Generating transactions for block: {}", b);
  154. info!(target: "money", "[Alice] Coins to use: {:?}", sample);
  155. info!(target: "money", "[Alice] =====================================");
  156. // Generate a transaction for each coin
  157. let mut txs = vec![];
  158. let mut txs_params = vec![];
  159. for index in sample {
  160. info!(target: "money", "[Alice] ===============================================");
  161. info!(target: "money", "[Alice] Building Money::Transfer params for coin {}", index);
  162. let mut coins = owncoins[index].clone();
  163. let token_id = token_ids[index];
  164. let mint_amount = minted_amounts[index];
  165. info!(target: "money", "[Alice] Alice coins: {}", coins.len());
  166. for (i, c) in coins.iter().enumerate() {
  167. info!(target: "money", "[Alice] \t coin {} value: {}", i, c.note.value);
  168. }
  169. let amount = rand::thread_rng().gen_range(1..mint_amount);
  170. info!(target: "money", "[Alice] Sending: {}", amount);
  171. info!(target: "money", "[Alice] ===============================================");
  172. let (tx, params, spent_coins) =
  173. th.transfer(amount, &Holder::Alice, &Holder::Alice, &coins, token_id)?;
  174. // Remove the owncoins we've spent
  175. for spent in spent_coins {
  176. coins.retain(|x| x != &spent);
  177. }
  178. // Gather new owncoins
  179. coins.append(&mut th.gather_multiple_owncoins(&Holder::Alice, &params.outputs)?);
  180. // Store transaction and its params
  181. txs.push(tx);
  182. txs_params.push(params);
  183. // Replace coins
  184. owncoins[index] = coins;
  185. }
  186. info!(target: "money", "[Faucet] =================================");
  187. info!(target: "money", "[Faucet] Executing Alice2Alice payment txs");
  188. info!(target: "money", "[Faucet] =================================");
  189. th.execute_multiple_transfer_txs(
  190. &Holder::Faucet,
  191. &txs,
  192. &txs_params,
  193. current_slot,
  194. true,
  195. )
  196. .await?;
  197. info!(target: "money", "[Alice] =================================");
  198. info!(target: "money", "[Alice] Executing Alice2Alice payment txs");
  199. info!(target: "money", "[Alice] =================================");
  200. th.execute_multiple_transfer_txs(
  201. &Holder::Alice,
  202. &txs,
  203. &txs_params,
  204. current_slot,
  205. false,
  206. )
  207. .await?;
  208. th.assert_trees(&HOLDERS);
  209. }
  210. // Statistics
  211. th.statistics();
  212. // Thanks for reading
  213. Ok(())
  214. })
  215. }