dao_mint.rs 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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::time::Instant;
  19. use darkfi::{tx::Transaction, Result};
  20. use darkfi_dao_contract::{
  21. client, client::DaoInfo, model::DaoMintParams, DaoFunction, DAO_CONTRACT_ZKAS_DAO_MINT_NS,
  22. };
  23. use darkfi_sdk::{
  24. crypto::{Keypair, MerkleNode, DAO_CONTRACT_ID},
  25. ContractCall,
  26. };
  27. use darkfi_serial::{serialize, Encodable};
  28. use rand::rngs::OsRng;
  29. use super::{Holder, TestHarness, TxAction};
  30. impl TestHarness {
  31. pub fn dao_mint(
  32. &mut self,
  33. dao_info: &DaoInfo,
  34. dao_kp: &Keypair,
  35. ) -> Result<(Transaction, DaoMintParams)> {
  36. // We assume DAO Holder exists at least
  37. let wallet = self.holders.get(&Holder::Dao).unwrap();
  38. let (dao_mint_pk, dao_mint_zkbin) =
  39. wallet.proving_keys.get(&DAO_CONTRACT_ZKAS_DAO_MINT_NS.to_string()).unwrap();
  40. let tx_action_benchmark = self.tx_action_benchmarks.get_mut(&TxAction::DaoMint).unwrap();
  41. let timer = Instant::now();
  42. let (params, proofs) =
  43. client::make_mint_call(dao_info, &dao_kp.secret, dao_mint_zkbin, dao_mint_pk)?;
  44. let mut data = vec![DaoFunction::Mint as u8];
  45. params.encode(&mut data)?;
  46. let calls = vec![ContractCall { contract_id: *DAO_CONTRACT_ID, data }];
  47. let proofs = vec![proofs];
  48. let mut tx = Transaction { calls, proofs, signatures: vec![] };
  49. let sigs = tx.create_sigs(&mut OsRng, &[dao_kp.secret])?;
  50. tx.signatures = vec![sigs];
  51. tx_action_benchmark.creation_times.push(timer.elapsed());
  52. // Calculate transaction sizes
  53. let encoded: Vec<u8> = serialize(&tx);
  54. let size = std::mem::size_of_val(&*encoded);
  55. tx_action_benchmark.sizes.push(size);
  56. let base58 = bs58::encode(&encoded).into_string();
  57. let size = std::mem::size_of_val(&*base58);
  58. tx_action_benchmark.broadcasted_sizes.push(size);
  59. Ok((tx, params))
  60. }
  61. pub async fn execute_dao_mint_tx(
  62. &mut self,
  63. holder: &Holder,
  64. tx: &Transaction,
  65. params: &DaoMintParams,
  66. slot: u64,
  67. ) -> Result<()> {
  68. let wallet = self.holders.get_mut(holder).unwrap();
  69. let tx_action_benchmark = self.tx_action_benchmarks.get_mut(&TxAction::DaoMint).unwrap();
  70. let timer = Instant::now();
  71. wallet.validator.read().await.add_transactions(&[tx.clone()], slot, true).await?;
  72. wallet.dao_merkle_tree.append(MerkleNode::from(params.dao_bulla.inner()));
  73. let leaf_pos = wallet.dao_merkle_tree.mark().unwrap();
  74. wallet.dao_leafs.insert(params.dao_bulla, leaf_pos);
  75. tx_action_benchmark.verify_times.push(timer.elapsed());
  76. Ok(())
  77. }
  78. }