dao_vote.rs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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::{
  20. tx::{ContractCallLeaf, Transaction, TransactionBuilder},
  21. Result,
  22. };
  23. use darkfi_dao_contract::{
  24. client::{DaoVoteCall, DaoVoteInput},
  25. model::{Dao, DaoProposal, DaoProposalBulla, DaoVoteParams},
  26. DaoFunction, DAO_CONTRACT_ZKAS_DAO_VOTE_BURN_NS, DAO_CONTRACT_ZKAS_DAO_VOTE_MAIN_NS,
  27. };
  28. use darkfi_money_contract::client::OwnCoin;
  29. use darkfi_sdk::{
  30. crypto::{pasta_prelude::Field, Keypair, SecretKey, DAO_CONTRACT_ID},
  31. pasta::pallas,
  32. ContractCall,
  33. };
  34. use darkfi_serial::{serialize, Encodable};
  35. use rand::rngs::OsRng;
  36. use super::{Holder, TestHarness, TxAction};
  37. impl TestHarness {
  38. pub fn dao_vote(
  39. &mut self,
  40. voter: &Holder,
  41. dao_kp: &Keypair,
  42. vote_option: bool,
  43. dao: &Dao,
  44. proposal: &DaoProposal,
  45. proposal_bulla: &DaoProposalBulla,
  46. ) -> Result<(Transaction, DaoVoteParams)> {
  47. let wallet = self.holders.get(voter).unwrap();
  48. let (dao_vote_burn_pk, dao_vote_burn_zkbin) =
  49. self.proving_keys.get(&DAO_CONTRACT_ZKAS_DAO_VOTE_BURN_NS.to_string()).unwrap();
  50. let (dao_vote_main_pk, dao_vote_main_zkbin) =
  51. self.proving_keys.get(&DAO_CONTRACT_ZKAS_DAO_VOTE_MAIN_NS.to_string()).unwrap();
  52. let tx_action_benchmark = self.tx_action_benchmarks.get_mut(&TxAction::DaoVote).unwrap();
  53. let timer = Instant::now();
  54. let (_proposal_leaf_pos, money_merkle_tree) =
  55. wallet.dao_prop_leafs.get(proposal_bulla).unwrap();
  56. let vote_owncoin: OwnCoin = wallet
  57. .unspent_money_coins
  58. .iter()
  59. .find(|x| x.note.token_id == dao.gov_token_id)
  60. .unwrap()
  61. .clone();
  62. let signature_secret = SecretKey::random(&mut OsRng);
  63. let input = DaoVoteInput {
  64. secret: wallet.keypair.secret,
  65. note: vote_owncoin.note.clone(),
  66. leaf_position: vote_owncoin.leaf_position,
  67. merkle_path: money_merkle_tree.witness(vote_owncoin.leaf_position, 0).unwrap(),
  68. signature_secret,
  69. };
  70. let call = DaoVoteCall {
  71. inputs: vec![input],
  72. vote_option,
  73. yes_vote_blind: pallas::Scalar::random(&mut OsRng),
  74. vote_keypair: *dao_kp,
  75. proposal: proposal.clone(),
  76. dao: dao.clone(),
  77. };
  78. let (params, proofs) = call.make(
  79. dao_vote_burn_zkbin,
  80. dao_vote_burn_pk,
  81. dao_vote_main_zkbin,
  82. dao_vote_main_pk,
  83. )?;
  84. let mut data = vec![DaoFunction::Vote as u8];
  85. params.encode(&mut data)?;
  86. let call = ContractCall { contract_id: *DAO_CONTRACT_ID, data };
  87. let mut tx_builder = TransactionBuilder::new(ContractCallLeaf { call, proofs }, vec![]);
  88. let mut tx = tx_builder.build()?;
  89. let sigs = tx.create_sigs(&mut OsRng, &[signature_secret])?;
  90. tx.signatures = vec![sigs];
  91. tx_action_benchmark.creation_times.push(timer.elapsed());
  92. // Calculate transaction sizes
  93. let encoded: Vec<u8> = serialize(&tx);
  94. let size = std::mem::size_of_val(&*encoded);
  95. tx_action_benchmark.sizes.push(size);
  96. let base58 = bs58::encode(&encoded).into_string();
  97. let size = std::mem::size_of_val(&*base58);
  98. tx_action_benchmark.broadcasted_sizes.push(size);
  99. Ok((tx, params))
  100. }
  101. pub async fn execute_dao_vote_tx(
  102. &mut self,
  103. holder: &Holder,
  104. tx: &Transaction,
  105. _params: &DaoVoteParams,
  106. slot: u64,
  107. ) -> Result<()> {
  108. let wallet = self.holders.get_mut(holder).unwrap();
  109. let tx_action_benchmark = self.tx_action_benchmarks.get_mut(&TxAction::DaoVote).unwrap();
  110. let timer = Instant::now();
  111. wallet.validator.add_transactions(&[tx.clone()], slot, true).await?;
  112. tx_action_benchmark.verify_times.push(timer.elapsed());
  113. Ok(())
  114. }
  115. }