Переглянути джерело

contract/consensus: rebased and updated

aggstam 3 роки тому
батько
коміт
fe98576ba7

+ 32 - 0
src/contract/consensus/src/entrypoint.rs

@@ -16,6 +16,7 @@
  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
  */
 
+<<<<<<< HEAD
 use darkfi_money_contract::{
     model::{ConsensusStakeUpdateV1, ConsensusUnstakeUpdateV1},
     CONSENSUS_CONTRACT_COINS_TREE, CONSENSUS_CONTRACT_COIN_MERKLE_TREE,
@@ -33,6 +34,17 @@ use darkfi_sdk::{
 use darkfi_serial::{deserialize, serialize, Encodable, WriteExt};
 
 use crate::{model::ConsensusRewardUpdateV1, ConsensusFunction};
+=======
+use darkfi_sdk::{
+    crypto::ContractId,
+    db::{db_init, db_lookup, set_return_data, SMART_CONTRACT_ZKAS_DB_NAME},
+    error::{ContractError, ContractResult},
+    msg, ContractCall,
+};
+use darkfi_serial::deserialize;
+
+use crate::{model::ConsensusStakeUpdateV1, ConsensusFunction};
+>>>>>>> 2b38600b8a (contract/consensus: laid foundation)
 
 /// `Consensus::Stake` functions
 mod stake_v1;
@@ -41,6 +53,7 @@ use stake_v1::{
     consensus_stake_process_update_v1,
 };
 
+<<<<<<< HEAD
 /// `Consensus::ProposalBurn` functions
 mod proposal_burn_v1;
 use proposal_burn_v1::{
@@ -69,6 +82,8 @@ use unstake_v1::{
     consensus_unstake_process_update_v1,
 };
 
+=======
+>>>>>>> 2b38600b8a (contract/consensus: laid foundation)
 darkfi_sdk::define_contract!(
     init: init_contract,
     exec: process_instruction,
@@ -86,11 +101,16 @@ fn init_contract(cid: ContractId, _ix: &[u8]) -> ContractResult {
     // The lookups can be done by `contract_id+_zkas+namespace`.
     // TODO: For the zkas tree, external host checks should be done to ensure
     //       that the bincode is actually valid and not arbitrary.
+<<<<<<< HEAD
     let zkas_db = match db_lookup(cid, SMART_CONTRACT_ZKAS_DB_NAME) {
+=======
+    let _zkas_db = match db_lookup(cid, SMART_CONTRACT_ZKAS_DB_NAME) {
+>>>>>>> 2b38600b8a (contract/consensus: laid foundation)
         Ok(v) => v,
         Err(_) => db_init(cid, SMART_CONTRACT_ZKAS_DB_NAME)?,
     };
 
+<<<<<<< HEAD
     let money_mint_v1_bincode = include_bytes!("../../money/proof/mint_v1.zk.bin");
     let money_burn_v1_bincode = include_bytes!("../../money/proof/burn_v1.zk.bin");
     let reward_v1_bincode = include_bytes!("../proof/reward_v1.zk.bin");
@@ -140,6 +160,9 @@ fn init_contract(cid: ContractId, _ix: &[u8]) -> ContractResult {
         &serialize(&CONSENSUS_CONTRACT_DB_VERSION),
         &serialize(&env!("CARGO_PKG_VERSION")),
     )?;
+=======
+    // TODO: implement
+>>>>>>> 2b38600b8a (contract/consensus: laid foundation)
 
     Ok(())
 }
@@ -163,6 +186,7 @@ fn get_metadata(cid: ContractId, ix: &[u8]) -> ContractResult {
             let metadata = consensus_stake_get_metadata_v1(cid, call_idx, calls)?;
             Ok(set_return_data(&metadata)?)
         }
+<<<<<<< HEAD
         ConsensusFunction::ProposalBurnV1 => {
             let metadata = consensus_proposal_burn_get_metadata_v1(cid, call_idx, calls)?;
             Ok(set_return_data(&metadata)?)
@@ -179,6 +203,8 @@ fn get_metadata(cid: ContractId, ix: &[u8]) -> ContractResult {
             let metadata = consensus_unstake_get_metadata_v1(cid, call_idx, calls)?;
             Ok(set_return_data(&metadata)?)
         }
+=======
+>>>>>>> 2b38600b8a (contract/consensus: laid foundation)
     }
 }
 
@@ -202,6 +228,7 @@ fn process_instruction(cid: ContractId, ix: &[u8]) -> ContractResult {
             let update_data = consensus_stake_process_instruction_v1(cid, call_idx, calls)?;
             Ok(set_return_data(&update_data)?)
         }
+<<<<<<< HEAD
         ConsensusFunction::ProposalBurnV1 => {
             let update_data = consensus_proposal_burn_process_instruction_v1(cid, call_idx, calls)?;
             Ok(set_return_data(&update_data)?)
@@ -219,6 +246,8 @@ fn process_instruction(cid: ContractId, ix: &[u8]) -> ContractResult {
             let update_data = consensus_unstake_process_instruction_v1(cid, call_idx, calls)?;
             Ok(set_return_data(&update_data)?)
         }
+=======
+>>>>>>> 2b38600b8a (contract/consensus: laid foundation)
     }
 }
 
@@ -232,6 +261,7 @@ fn process_update(cid: ContractId, update_data: &[u8]) -> ContractResult {
             let update: ConsensusStakeUpdateV1 = deserialize(&update_data[1..])?;
             Ok(consensus_stake_process_update_v1(cid, update)?)
         }
+<<<<<<< HEAD
         ConsensusFunction::ProposalBurnV1 => {
             let update: ConsensusUnstakeUpdateV1 = deserialize(&update_data[1..])?;
             Ok(consensus_proposal_burn_process_update_v1(cid, update)?)
@@ -248,5 +278,7 @@ fn process_update(cid: ContractId, update_data: &[u8]) -> ContractResult {
             let update: ConsensusUnstakeUpdateV1 = deserialize(&update_data[1..])?;
             Ok(consensus_unstake_process_update_v1(cid, update)?)
         }
+=======
+>>>>>>> 2b38600b8a (contract/consensus: laid foundation)
     }
 }

+ 33 - 0
src/contract/consensus/src/error.rs

@@ -0,0 +1,33 @@
+/* This file is part of DarkFi (https://dark.fi)
+ *
+ * Copyright (C) 2020-2023 Dyne.org foundation
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as
+ * published by the Free Software Foundation, either version 3 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program.  If not, see <https://www.gnu.org/licenses/>.
+ */
+
+use darkfi_sdk::error::ContractError;
+
+#[derive(Debug, Clone, thiserror::Error)]
+pub enum ConsensusError {
+    #[error("Error")]
+    SomeError,
+}
+
+impl From<ConsensusError> for ContractError {
+    fn from(e: ConsensusError) -> Self {
+        match e {
+            ConsensusError::SomeError => Self::Custom(1),
+        }
+    }
+}

+ 44 - 0
src/contract/consensus/tests/harness.rs

@@ -30,9 +30,13 @@ use darkfi::{
 };
 use darkfi_money_contract::client::OwnCoin;
 use darkfi_sdk::{
+<<<<<<< HEAD
     crypto::{
         Keypair, MerkleTree, PublicKey, CONSENSUS_CONTRACT_ID, DARK_TOKEN_ID, MONEY_CONTRACT_ID,
     },
+=======
+    crypto::{Keypair, MerkleTree, PublicKey, DARK_TOKEN_ID, MONEY_CONTRACT_ID},
+>>>>>>> 2b38600b8a (contract/consensus: laid foundation)
     db::SMART_CONTRACT_ZKAS_DB_NAME,
     pasta::pallas,
     ContractCall,
@@ -43,8 +47,13 @@ use rand::rngs::OsRng;
 
 use darkfi_money_contract::{
     client::transfer_v1::TransferCallBuilder, model::MoneyTransferParamsV1, MoneyFunction,
+<<<<<<< HEAD
     CONSENSUS_CONTRACT_ZKAS_REWARD_NS_V1, MONEY_CONTRACT_ZKAS_BURN_NS_V1,
     MONEY_CONTRACT_ZKAS_MINT_NS_V1,
+=======
+    MONEY_CONTRACT_ZKAS_BURN_NS_V1, MONEY_CONTRACT_ZKAS_MINT_NS_V1,
+    MONEY_CONTRACT_ZKAS_TOKEN_FRZ_NS_V1, MONEY_CONTRACT_ZKAS_TOKEN_MINT_NS_V1,
+>>>>>>> 2b38600b8a (contract/consensus: laid foundation)
 };
 
 pub fn init_logger() {
@@ -69,7 +78,10 @@ pub struct Wallet {
     pub keypair: Keypair,
     pub state: ValidatorStatePtr,
     pub merkle_tree: MerkleTree,
+<<<<<<< HEAD
     pub consensus_merkle_tree: MerkleTree,
+=======
+>>>>>>> 2b38600b8a (contract/consensus: laid foundation)
     pub wallet: WalletPtr,
     pub coins: Vec<OwnCoin>,
     pub spent_coins: Vec<OwnCoin>,
@@ -94,18 +106,30 @@ impl Wallet {
         .await?;
 
         let merkle_tree = MerkleTree::new(100);
+<<<<<<< HEAD
         let consensus_merkle_tree = MerkleTree::new(100);
+=======
+>>>>>>> 2b38600b8a (contract/consensus: laid foundation)
 
         let coins = vec![];
         let spent_coins = vec![];
 
+<<<<<<< HEAD
         Ok(Self { keypair, state, merkle_tree, consensus_merkle_tree, wallet, coins, spent_coins })
+=======
+        Ok(Self { keypair, state, merkle_tree, wallet, coins, spent_coins })
+>>>>>>> 2b38600b8a (contract/consensus: laid foundation)
     }
 }
 
 pub struct ConsensusTestHarness {
     pub faucet: Wallet,
     pub alice: Wallet,
+<<<<<<< HEAD
+=======
+    pub bob: Wallet,
+    pub charlie: Wallet,
+>>>>>>> 2b38600b8a (contract/consensus: laid foundation)
     pub proving_keys: HashMap<&'static str, (ProvingKey, ZkBinary)>,
 }
 
@@ -118,10 +142,23 @@ impl ConsensusTestHarness {
         let alice_kp = Keypair::random(&mut OsRng);
         let alice = Wallet::new(alice_kp, &faucet_pubkeys).await?;
 
+<<<<<<< HEAD
         // Get the zkas circuits and build proving keys
         let mut proving_keys = HashMap::new();
         let alice_sled = alice.state.read().await.blockchain.sled_db.clone();
         let mut db_handle = alice.state.read().await.blockchain.contracts.lookup(
+=======
+        let bob_kp = Keypair::random(&mut OsRng);
+        let bob = Wallet::new(bob_kp, &faucet_pubkeys).await?;
+
+        let charlie_kp = Keypair::random(&mut OsRng);
+        let charlie = Wallet::new(charlie_kp, &faucet_pubkeys).await?;
+
+        // Get the zkas circuits and build proving keys
+        let mut proving_keys = HashMap::new();
+        let alice_sled = alice.state.read().await.blockchain.sled_db.clone();
+        let db_handle = alice.state.read().await.blockchain.contracts.lookup(
+>>>>>>> 2b38600b8a (contract/consensus: laid foundation)
             &alice_sled,
             &MONEY_CONTRACT_ID,
             SMART_CONTRACT_ZKAS_DB_NAME,
@@ -140,6 +177,7 @@ impl ConsensusTestHarness {
 
         mkpk!(MONEY_CONTRACT_ZKAS_MINT_NS_V1);
         mkpk!(MONEY_CONTRACT_ZKAS_BURN_NS_V1);
+<<<<<<< HEAD
 
         db_handle = alice.state.read().await.blockchain.contracts.lookup(
             &alice_sled,
@@ -151,6 +189,12 @@ impl ConsensusTestHarness {
         mkpk!(CONSENSUS_CONTRACT_ZKAS_REWARD_NS_V1);
 
         Ok(Self { faucet, alice, proving_keys })
+=======
+        mkpk!(MONEY_CONTRACT_ZKAS_TOKEN_MINT_NS_V1);
+        mkpk!(MONEY_CONTRACT_ZKAS_TOKEN_FRZ_NS_V1);
+
+        Ok(Self { faucet, alice, bob, charlie, proving_keys })
+>>>>>>> 2b38600b8a (contract/consensus: laid foundation)
     }
 
     pub fn airdrop_native(