Browse Source

cleanup DAO user API

x 3 years ago
parent
commit
be037aeba9

+ 6 - 6
src/contract/dao/src/dao_client/exec.rs

@@ -30,12 +30,12 @@ use darkfi::{
     Result,
 };
 
-use super::{propose::Proposal, Dao};
+use super::{DaoInfo, ProposalInfo};
 use crate::dao_model::DaoExecParams;
 
-pub struct Builder {
-    pub proposal: Proposal,
-    pub dao: Dao,
+pub struct ExecCall {
+    pub proposal: ProposalInfo,
+    pub dao: DaoInfo,
     pub yes_votes_value: u64,
     pub all_votes_value: u64,
     pub yes_votes_blind: pallas::Scalar,
@@ -50,8 +50,8 @@ pub struct Builder {
     pub signature_secret: SecretKey,
 }
 
-impl Builder {
-    pub fn build(
+impl ExecCall {
+    pub fn make(
         self,
         exec_zkbin: &ZkBinary,
         exec_pk: &ProvingKey,

+ 2 - 2
src/contract/dao/src/dao_client/mint.rs

@@ -28,7 +28,7 @@ use rand::rngs::OsRng;
 use crate::dao_model::DaoMintParams;
 
 #[derive(Clone)]
-pub struct Dao {
+pub struct DaoInfo {
     pub proposer_limit: u64,
     pub quorum: u64,
     pub approval_ratio_quot: u64,
@@ -39,7 +39,7 @@ pub struct Dao {
 }
 
 pub fn make_mint_call(
-    dao: &Dao,
+    dao: &DaoInfo,
     dao_mint_zkbin: &ZkBinary,
     dao_mint_pk: &ProvingKey,
 ) -> Result<(DaoMintParams, Vec<Proof>)> {

+ 16 - 2
src/contract/dao/src/dao_client/mod.rs

@@ -1,9 +1,23 @@
 pub mod mint;
-pub use mint::{make_mint_call, Dao};
+pub use mint::{make_mint_call, DaoInfo};
 
+/// Provides core structs for DAO::propose()
+///
+/// * `ProposalInfo` is the main info about the proposal.
+/// * `ProposeStakeInput` are the staking inputs used to meet the `proposer_limit` threshold.
+/// * `ProposeCall` is what creates the call data used on chain.
+/// * `ProposeNote` is the secret shared info transmitted between DAO members.
 pub mod propose;
-pub use propose::{Proposal, ProposalStakeInput, ProposeCall};
+pub use propose::{ProposalInfo, ProposeCall, ProposeNote, ProposeStakeInput};
 
+/// Provides core structs for DAO::vote()
+///
+/// * `VoteInfo` is the main info about the vote.
+/// * `VoteStakeInput` are the staking inputs used in actual voting.
+/// * `VoteCall` is what creates the call data used on chain.
+/// * `VoteNote` is the secret shared info transmitted between DAO members.
 pub mod vote;
+pub use vote::{VoteCall, VoteInfo, VoteInput, VoteNote};
 
 pub mod exec;
+pub use exec::ExecCall;

+ 17 - 17
src/contract/dao/src/dao_client/propose.rs

@@ -34,14 +34,23 @@ use crate::{
     note,
 };
 
-use super::Dao;
+use super::DaoInfo;
+
+#[derive(SerialEncodable, SerialDecodable, Clone)]
+pub struct ProposalInfo {
+    pub dest: PublicKey,
+    pub amount: u64,
+    pub serial: pallas::Base,
+    pub token_id: TokenId,
+    pub blind: pallas::Base,
+}
 
 #[derive(SerialEncodable, SerialDecodable)]
-pub struct Note {
-    pub proposal: Proposal,
+pub struct ProposeNote {
+    pub proposal: ProposalInfo,
 }
 
-pub struct ProposalStakeInput {
+pub struct ProposeStakeInput {
     pub secret: SecretKey,
     //pub note: money::transfer::wallet::Note,
     pub note: darkfi_money_contract::client::Note,
@@ -50,19 +59,10 @@ pub struct ProposalStakeInput {
     pub signature_secret: SecretKey,
 }
 
-#[derive(SerialEncodable, SerialDecodable, Clone)]
-pub struct Proposal {
-    pub dest: PublicKey,
-    pub amount: u64,
-    pub serial: pallas::Base,
-    pub token_id: TokenId,
-    pub blind: pallas::Base,
-}
-
 pub struct ProposeCall {
-    pub inputs: Vec<ProposalStakeInput>,
-    pub proposal: Proposal,
-    pub dao: Dao,
+    pub inputs: Vec<ProposeStakeInput>,
+    pub proposal: ProposalInfo,
+    pub dao: DaoInfo,
     pub dao_leaf_position: MerklePosition,
     pub dao_merkle_path: Vec<MerkleNode>,
     pub dao_merkle_root: MerkleNode,
@@ -247,7 +247,7 @@ impl ProposeCall {
             .expect("DAO::propose() proving error!");
         proofs.push(main_proof);
 
-        let note = Note { proposal: self.proposal };
+        let note = ProposeNote { proposal: self.proposal };
         let enc_note = note::encrypt(&note, &self.dao.public_key).unwrap();
         let params = DaoProposeParams {
             dao_merkle_root: self.dao_merkle_root,

+ 15 - 15
src/contract/dao/src/dao_client/vote.rs

@@ -30,26 +30,26 @@ use darkfi::{
     Result,
 };
 
-use super::{propose::Proposal, Dao};
+use super::{DaoInfo, ProposalInfo};
 use crate::{
-    dao_model::{DaoVoteParams, VoteInput},
+    dao_model::{DaoVoteParams, VoteCallInput},
     note,
 };
 
 #[derive(SerialEncodable, SerialDecodable)]
-pub struct Note {
-    pub vote: Vote,
+pub struct VoteNote {
+    pub vote: VoteInfo,
     pub vote_value: u64,
     pub vote_value_blind: pallas::Scalar,
 }
 
 #[derive(SerialEncodable, SerialDecodable)]
-pub struct Vote {
+pub struct VoteInfo {
     pub vote_option: bool,
     pub vote_option_blind: pallas::Scalar,
 }
 
-pub struct BuilderInput {
+pub struct VoteInput {
     pub secret: SecretKey,
     //pub note: money::transfer::wallet::Note,
     pub note: darkfi_money_contract::client::Note,
@@ -60,16 +60,16 @@ pub struct BuilderInput {
 
 // TODO: should be token locking voting?
 // Inside ZKproof, check proposal is correct.
-pub struct Builder {
-    pub inputs: Vec<BuilderInput>,
-    pub vote: Vote,
+pub struct VoteCall {
+    pub inputs: Vec<VoteInput>,
+    pub vote: VoteInfo,
     pub vote_keypair: Keypair,
-    pub proposal: Proposal,
-    pub dao: Dao,
+    pub proposal: ProposalInfo,
+    pub dao: DaoInfo,
 }
 
-impl Builder {
-    pub fn build(
+impl VoteCall {
+    pub fn make(
         self,
         burn_zkbin: &ZkBinary,
         burn_pk: &ProvingKey,
@@ -168,7 +168,7 @@ impl Builder {
                 .expect("DAO::vote() proving error!");
             proofs.push(input_proof);
 
-            let input = VoteInput {
+            let input = VoteCallInput {
                 nullifier: Nullifier::from(nullifier),
                 vote_commit,
                 merkle_root,
@@ -267,7 +267,7 @@ impl Builder {
             .expect("DAO::vote() proving error!");
         proofs.push(main_proof);
 
-        let note = Note { vote: self.vote, vote_value, vote_value_blind };
+        let note = VoteNote { vote: self.vote, vote_value, vote_value_blind };
         let enc_note = note::encrypt(&note, &self.vote_keypair.public).unwrap();
 
         let params = DaoVoteParams {

+ 2 - 2
src/contract/dao/src/dao_model.rs

@@ -94,7 +94,7 @@ pub struct DaoProposeUpdate {
 }
 
 #[derive(SerialEncodable, SerialDecodable)]
-pub struct VoteInput {
+pub struct VoteCallInput {
     pub nullifier: Nullifier,
     pub vote_commit: pallas::Point,
     pub merkle_root: MerkleNode,
@@ -108,7 +108,7 @@ pub struct DaoVoteParams {
     pub yes_vote_commit: pallas::Point,
     pub ciphertext: Vec<u8>,
     pub ephem_public: PublicKey,
-    pub inputs: Vec<VoteInput>,
+    pub inputs: Vec<VoteCallInput>,
 }
 
 #[derive(SerialEncodable, SerialDecodable)]

+ 9 - 9
src/contract/dao/src/money_client.rs

@@ -32,19 +32,19 @@ use darkfi_money_contract::{
     state::{ClearInput, Input, MoneyTransferParams, Output},
 };
 
-pub struct Builder {
-    pub clear_inputs: Vec<BuilderClearInputInfo>,
-    pub inputs: Vec<BuilderInputInfo>,
-    pub outputs: Vec<BuilderOutputInfo>,
+pub struct TransferCall {
+    pub clear_inputs: Vec<TransferClearInput>,
+    pub inputs: Vec<TransferInput>,
+    pub outputs: Vec<TransferOutput>,
 }
 
-pub struct BuilderClearInputInfo {
+pub struct TransferClearInput {
     pub value: u64,
     pub token_id: TokenId,
     pub signature_secret: SecretKey,
 }
 
-pub struct BuilderInputInfo {
+pub struct TransferInput {
     pub leaf_position: MerklePosition,
     pub merkle_path: Vec<MerkleNode>,
     pub secret: SecretKey,
@@ -54,7 +54,7 @@ pub struct BuilderInputInfo {
     pub signature_secret: SecretKey,
 }
 
-pub struct BuilderOutputInfo {
+pub struct TransferOutput {
     pub value: u64,
     pub token_id: TokenId,
     pub public: PublicKey,
@@ -64,7 +64,7 @@ pub struct BuilderOutputInfo {
     pub user_data: pallas::Base,
 }
 
-impl Builder {
+impl TransferCall {
     fn compute_remainder_blind(
         clear_inputs: &[ClearInput],
         input_blinds: &[ValueBlind],
@@ -87,7 +87,7 @@ impl Builder {
         total
     }
 
-    pub fn build(
+    pub fn make(
         self,
         mint_zkbin: &ZkBinary,
         mint_pk: &ProvingKey,

+ 37 - 43
src/contract/dao/tests/integration.rs

@@ -28,13 +28,7 @@ use darkfi_serial::{Decodable, Encodable};
 use log::debug;
 use rand::rngs::OsRng;
 
-use darkfi_dao_contract::{
-    dao_client,
-    dao_client::{exec as dao_exec_client, propose as dao_propose_client, vote as dao_vote_client},
-    money_client, note,
-    wallet_cache::WalletCache,
-    DaoFunction,
-};
+use darkfi_dao_contract::{dao_client, money_client, note, wallet_cache::WalletCache, DaoFunction};
 
 use darkfi_money_contract::{client::EncryptedNote, state::MoneyTransferParams, MoneyFunction};
 
@@ -67,7 +61,7 @@ async fn integration_test() -> Result<()> {
     let gdrk_token_id = TokenId::from(pallas::Base::random(&mut OsRng));
 
     // DAO parameters
-    let dao = dao_client::Dao {
+    let dao = dao_client::DaoInfo {
         proposer_limit: 110,
         quorum: 110,
         approval_ratio_base: 2,
@@ -135,14 +129,14 @@ async fn integration_test() -> Result<()> {
     // In out case, it's the bulla for the DAO
     let user_data = dao_bulla.inner();
 
-    let builder = money_client::Builder {
-        clear_inputs: vec![money_client::BuilderClearInputInfo {
+    let call = money_client::TransferCall {
+        clear_inputs: vec![money_client::TransferClearInput {
             value: xdrk_supply,
             token_id: xdrk_token_id,
             signature_secret: dao_th.faucet_kp.secret,
         }],
         inputs: vec![],
-        outputs: vec![money_client::BuilderOutputInfo {
+        outputs: vec![money_client::TransferOutput {
             value: xdrk_supply,
             token_id: xdrk_token_id,
             public: dao_th.dao_kp.public,
@@ -152,7 +146,7 @@ async fn integration_test() -> Result<()> {
             user_data,
         }],
     };
-    let (params, proofs) = builder.build(
+    let (params, proofs) = call.make(
         &dao_th.money_mint_zkbin,
         &dao_th.money_mint_pk,
         &dao_th.money_burn_zkbin,
@@ -231,7 +225,7 @@ async fn integration_test() -> Result<()> {
     let spend_hook = pallas::Base::from(0);
     let user_data = pallas::Base::from(0);
 
-    let output1 = money_client::BuilderOutputInfo {
+    let output1 = money_client::TransferOutput {
         value: 400000,
         token_id: gdrk_token_id,
         public: dao_th.alice_kp.public,
@@ -241,7 +235,7 @@ async fn integration_test() -> Result<()> {
         user_data,
     };
 
-    let output2 = money_client::BuilderOutputInfo {
+    let output2 = money_client::TransferOutput {
         value: 400000,
         token_id: gdrk_token_id,
         public: dao_th.bob_kp.public,
@@ -251,7 +245,7 @@ async fn integration_test() -> Result<()> {
         user_data,
     };
 
-    let output3 = money_client::BuilderOutputInfo {
+    let output3 = money_client::TransferOutput {
         value: 200000,
         token_id: gdrk_token_id,
         public: dao_th.charlie_kp.public,
@@ -263,8 +257,8 @@ async fn integration_test() -> Result<()> {
 
     assert!(2 * 400000 + 200000 == gdrk_supply);
 
-    let builder = money_client::Builder {
-        clear_inputs: vec![money_client::BuilderClearInputInfo {
+    let call = money_client::TransferCall {
+        clear_inputs: vec![money_client::TransferClearInput {
             value: gdrk_supply,
             token_id: gdrk_token_id,
             // This might be different for various tokens but lets reuse it here
@@ -273,7 +267,7 @@ async fn integration_test() -> Result<()> {
         inputs: vec![],
         outputs: vec![output1, output2, output3],
     };
-    let (params, proofs) = builder.build(
+    let (params, proofs) = call.make(
         &dao_th.money_mint_zkbin,
         &dao_th.money_mint_pk,
         &dao_th.money_burn_zkbin,
@@ -378,7 +372,7 @@ async fn integration_test() -> Result<()> {
     // TODO: is it possible for an invalid transfer() to be constructed on exec()?
     //       need to look into this
     let signature_secret = SecretKey::random(&mut OsRng);
-    let input = dao_client::ProposalStakeInput {
+    let input = dao_client::ProposeStakeInput {
         secret: dao_th.alice_kp.secret,
         note: gov_recv[0].note.clone(),
         leaf_position: money_leaf_position,
@@ -393,7 +387,7 @@ async fn integration_test() -> Result<()> {
         (merkle_path, root)
     };
 
-    let proposal = dao_client::Proposal {
+    let proposal = dao_client::ProposalInfo {
         dest: receiver_keypair.public,
         amount: 1000,
         serial: pallas::Base::random(&mut OsRng),
@@ -437,7 +431,7 @@ async fn integration_test() -> Result<()> {
             ciphertext: params.ciphertext,
             ephem_public: params.ephem_public,
         };
-        let note: dao_propose_client::Note = enc_note.decrypt(&dao_th.dao_kp.secret).unwrap();
+        let note: dao_client::ProposeNote = enc_note.decrypt(&dao_th.dao_kp.secret).unwrap();
 
         // TODO: check it belongs to DAO bulla
 
@@ -491,7 +485,7 @@ async fn integration_test() -> Result<()> {
     };
 
     let signature_secret = SecretKey::random(&mut OsRng);
-    let input = dao_vote_client::BuilderInput {
+    let input = dao_client::VoteInput {
         secret: dao_th.alice_kp.secret,
         note: gov_recv[0].note.clone(),
         leaf_position: money_leaf_position,
@@ -506,9 +500,9 @@ async fn integration_test() -> Result<()> {
     // For the demo MVP, you can just use the dao_keypair secret
     let vote_keypair_1 = Keypair::random(&mut OsRng);
 
-    let builder = dao_vote_client::Builder {
+    let call = dao_client::VoteCall {
         inputs: vec![input],
-        vote: dao_vote_client::Vote {
+        vote: dao_client::VoteInfo {
             vote_option,
             vote_option_blind: pallas::Scalar::random(&mut OsRng),
         },
@@ -516,7 +510,7 @@ async fn integration_test() -> Result<()> {
         proposal: proposal.clone(),
         dao: dao.clone(),
     };
-    let (params, proofs) = builder.build(
+    let (params, proofs) = call.make(
         &dao_th.dao_vote_burn_zkbin,
         &dao_th.dao_vote_burn_pk,
         &dao_th.dao_vote_main_zkbin,
@@ -543,7 +537,7 @@ async fn integration_test() -> Result<()> {
             ciphertext: params.ciphertext,
             ephem_public: params.ephem_public,
         };
-        let note: dao_vote_client::Note = enc_note.decrypt(&vote_keypair_1.secret).unwrap();
+        let note: dao_client::VoteNote = enc_note.decrypt(&vote_keypair_1.secret).unwrap();
         note
     };
     debug!(target: "dao", "User 1 voted!");
@@ -561,7 +555,7 @@ async fn integration_test() -> Result<()> {
     };
 
     let signature_secret = SecretKey::random(&mut OsRng);
-    let input = dao_vote_client::BuilderInput {
+    let input = dao_client::VoteInput {
         //secret: gov_keypair_2.secret,
         secret: dao_th.bob_kp.secret,
         note: gov_recv[1].note.clone(),
@@ -576,9 +570,9 @@ async fn integration_test() -> Result<()> {
     // We create a new keypair to encrypt the vote.
     let vote_keypair_2 = Keypair::random(&mut OsRng);
 
-    let builder = dao_vote_client::Builder {
+    let call = dao_client::VoteCall {
         inputs: vec![input],
-        vote: dao_vote_client::Vote {
+        vote: dao_client::VoteInfo {
             vote_option,
             vote_option_blind: pallas::Scalar::random(&mut OsRng),
         },
@@ -586,7 +580,7 @@ async fn integration_test() -> Result<()> {
         proposal: proposal.clone(),
         dao: dao.clone(),
     };
-    let (params, proofs) = builder.build(
+    let (params, proofs) = call.make(
         &dao_th.dao_vote_burn_zkbin,
         &dao_th.dao_vote_burn_pk,
         &dao_th.dao_vote_main_zkbin,
@@ -610,7 +604,7 @@ async fn integration_test() -> Result<()> {
             ciphertext: params.ciphertext,
             ephem_public: params.ephem_public,
         };
-        let note: dao_vote_client::Note = enc_note.decrypt(&vote_keypair_2.secret).unwrap();
+        let note: dao_client::VoteNote = enc_note.decrypt(&vote_keypair_2.secret).unwrap();
         note
     };
     debug!(target: "dao", "User 2 voted!");
@@ -628,7 +622,7 @@ async fn integration_test() -> Result<()> {
     };
 
     let signature_secret = SecretKey::random(&mut OsRng);
-    let input = dao_vote_client::BuilderInput {
+    let input = dao_client::VoteInput {
         //secret: gov_keypair_3.secret,
         secret: dao_th.charlie_kp.secret,
         note: gov_recv[2].note.clone(),
@@ -643,9 +637,9 @@ async fn integration_test() -> Result<()> {
     // We create a new keypair to encrypt the vote.
     let vote_keypair_3 = Keypair::random(&mut OsRng);
 
-    let builder = dao_vote_client::Builder {
+    let call = dao_client::VoteCall {
         inputs: vec![input],
-        vote: dao_vote_client::Vote {
+        vote: dao_client::VoteInfo {
             vote_option,
             vote_option_blind: pallas::Scalar::random(&mut OsRng),
         },
@@ -653,7 +647,7 @@ async fn integration_test() -> Result<()> {
         proposal: proposal.clone(),
         dao: dao.clone(),
     };
-    let (params, proofs) = builder.build(
+    let (params, proofs) = call.make(
         &dao_th.dao_vote_burn_zkbin,
         &dao_th.dao_vote_burn_pk,
         &dao_th.dao_vote_main_zkbin,
@@ -680,7 +674,7 @@ async fn integration_test() -> Result<()> {
             ciphertext: params.ciphertext,
             ephem_public: params.ephem_public,
         };
-        let note: dao_vote_client::Note = enc_note.decrypt(&vote_keypair_3.secret).unwrap();
+        let note: dao_client::VoteNote = enc_note.decrypt(&vote_keypair_3.secret).unwrap();
         note
     };
     debug!(target: "dao", "User 3 voted!");
@@ -777,9 +771,9 @@ async fn integration_test() -> Result<()> {
     // In out case, it's the bulla for the DAO
     let user_data = dao_bulla.inner();
 
-    let builder = money_client::Builder {
+    let xfer_call = money_client::TransferCall {
         clear_inputs: vec![],
-        inputs: vec![money_client::BuilderInputInfo {
+        inputs: vec![money_client::TransferInput {
             leaf_position: treasury_leaf_position,
             merkle_path: treasury_merkle_path,
             secret: dao_th.dao_kp.secret,
@@ -790,7 +784,7 @@ async fn integration_test() -> Result<()> {
         }],
         outputs: vec![
             // Sending money
-            money_client::BuilderOutputInfo {
+            money_client::TransferOutput {
                 value: 1000,
                 token_id: xdrk_token_id,
                 //public: user_keypair.public,
@@ -801,7 +795,7 @@ async fn integration_test() -> Result<()> {
                 user_data: pallas::Base::from(0),
             },
             // Change back to DAO
-            money_client::BuilderOutputInfo {
+            money_client::TransferOutput {
                 value: xdrk_supply - 1000,
                 token_id: xdrk_token_id,
                 public: dao_th.dao_kp.public,
@@ -812,7 +806,7 @@ async fn integration_test() -> Result<()> {
             },
         ],
     };
-    let (xfer_params, xfer_proofs) = builder.build(
+    let (xfer_params, xfer_proofs) = xfer_call.make(
         &dao_th.money_mint_zkbin,
         &dao_th.money_mint_pk,
         &dao_th.money_burn_zkbin,
@@ -823,7 +817,7 @@ async fn integration_test() -> Result<()> {
     xfer_params.encode(&mut data)?;
     let xfer_call = ContractCall { contract_id: *MONEY_CONTRACT_ID, data };
 
-    let builder = dao_exec_client::Builder {
+    let call = dao_client::ExecCall {
         proposal,
         dao,
         yes_votes_value,
@@ -839,7 +833,7 @@ async fn integration_test() -> Result<()> {
         hook_dao_exec: spend_hook,
         signature_secret: exec_signature_secret,
     };
-    let (exec_params, exec_proofs) = builder.build(&dao_th.dao_exec_zkbin, &dao_th.dao_exec_pk)?;
+    let (exec_params, exec_proofs) = call.make(&dao_th.dao_exec_zkbin, &dao_th.dao_exec_pk)?;
 
     let mut data = vec![DaoFunction::Exec as u8];
     exec_params.encode(&mut data)?;