فهرست منبع

contract/money/client: replaced asserts with error returnes and added log targets

skoupidi 2 سال پیش
والد
کامیت
5c126999a1

+ 1 - 1
src/contract/money/src/client/auth_token_freeze_v1.rs

@@ -45,7 +45,7 @@ pub struct AuthTokenFreezeCallBuilder {
 
 impl AuthTokenFreezeCallBuilder {
     pub fn build(&self) -> Result<AuthTokenFreezeCallDebris> {
-        debug!("Building Money::AuthTokenFreezeV1 contract call");
+        debug!(target: "contract::money::client::auth_token_freeze", "Building Money::AuthTokenFreezeV1 contract call");
 
         // For the AuthTokenFreeze call, we just need to produce a valid signature,
         // and enforce the correct derivation inside ZK.

+ 1 - 1
src/contract/money/src/client/auth_token_mint_v1.rs

@@ -51,7 +51,7 @@ pub struct AuthTokenMintCallBuilder {
 
 impl AuthTokenMintCallBuilder {
     pub fn build(&self) -> Result<AuthTokenMintCallDebris> {
-        debug!("Building Money::AuthTokenMintV1 contract call");
+        debug!(target: "contract::money::client::auth_token_mint", "Building Money::AuthTokenMintV1 contract call");
 
         // Create the proof
         let prover_witnesses = vec![

+ 7 - 5
src/contract/money/src/client/genesis_mint_v1.rs

@@ -19,13 +19,13 @@
 use darkfi::{
     zk::{Proof, ProvingKey},
     zkas::ZkBinary,
-    Result,
+    ClientFailed, Result,
 };
 use darkfi_sdk::{
     crypto::{note::AeadEncryptedNote, pasta_prelude::*, Blind, FuncId, Keypair, PublicKey},
     pasta::pallas,
 };
-use log::{debug, info};
+use log::debug;
 use rand::rngs::OsRng;
 
 use crate::{
@@ -77,8 +77,10 @@ pub struct GenesisMintCallBuilder {
 
 impl GenesisMintCallBuilder {
     pub fn build(&self) -> Result<GenesisMintCallDebris> {
-        debug!("Building Money::MintV1 contract call");
-        assert!(self.amount != 0);
+        debug!(target: "contract::money::client::genesis_mint", "Building Money::MintV1 contract call");
+        if self.amount == 0 {
+            return Err(ClientFailed::InvalidAmount(self.amount).into())
+        }
 
         // In this call, we will build one clear input and one anonymous output.
         // Only DARK_TOKEN_ID can be minted on genesis block.
@@ -115,7 +117,7 @@ impl GenesisMintCallBuilder {
 
         let coin_blind = Blind::random(&mut OsRng);
 
-        info!("Creating token mint proof for output");
+        debug!(target: "contract::money::client::genesis_mint", "Creating token mint proof for output");
         let (proof, public_inputs) = create_transfer_mint_proof(
             &self.mint_zkbin,
             &self.mint_pk,

+ 3 - 3
src/contract/money/src/client/pow_reward_v1.rs

@@ -26,7 +26,7 @@ use darkfi_sdk::{
     crypto::{note::AeadEncryptedNote, pasta_prelude::*, Blind, FuncId, PublicKey, SecretKey},
     pasta::pallas,
 };
-use log::{debug, info};
+use log::debug;
 use rand::rngs::OsRng;
 
 use crate::{
@@ -83,7 +83,7 @@ pub struct PoWRewardCallBuilder {
 
 impl PoWRewardCallBuilder {
     fn _build(&self, value: u64) -> Result<PoWRewardCallDebris> {
-        debug!("Building Money::MintV1 contract call");
+        debug!(target: "contract::money::client::pow_reward", "Building Money::PowRewardV1 contract call");
 
         // In this call, we will build one clear input and one anonymous output.
         // Only DARK_TOKEN_ID can be minted as PoW reward.
@@ -116,7 +116,7 @@ impl PoWRewardCallBuilder {
 
         let coin_blind = Blind::random(&mut OsRng);
 
-        info!("Creating token mint proof for output");
+        debug!(target: "contract::money::client::pow_reward", "Creating token mint proof for output");
         let (proof, public_inputs) = create_transfer_mint_proof(
             &self.mint_zkbin,
             &self.mint_pk,

+ 23 - 8
src/contract/money/src/client/swap_v1.rs

@@ -31,7 +31,7 @@ use darkfi_sdk::{
     pasta::pallas,
 };
 use darkfi_serial::serialize;
-use log::debug;
+use log::{debug, error};
 use rand::rngs::OsRng;
 
 use crate::{
@@ -94,11 +94,26 @@ pub struct SwapCallBuilder {
 
 impl SwapCallBuilder {
     pub fn build(&self) -> Result<SwapCallDebris> {
-        debug!("Building half of Money::OtcSwapV1 contract call");
-        assert!(self.value_send != 0);
-        assert!(self.value_recv != 0);
-        assert!(self.token_id_send.inner() != pallas::Base::zero());
-        assert!(self.token_id_recv.inner() != pallas::Base::zero());
+        debug!(target: "contract::money::client::swap", "Building half of Money::OtcSwapV1 contract call");
+        if self.value_send == 0 {
+            error!(target: "contract::money::client::swap", "Error: Value send is 0");
+            return Err(ClientFailed::InvalidAmount(self.value_send).into())
+        }
+
+        if self.value_recv == 0 {
+            error!(target: "contract::money::client::swap", "Error: Value receive is 0");
+            return Err(ClientFailed::InvalidAmount(self.value_recv).into())
+        }
+
+        if self.token_id_send.inner() == pallas::Base::ZERO {
+            error!(target: "contract::money::client::swap", "Error: Token send is ZERO");
+            return Err(ClientFailed::InvalidTokenId(self.token_id_send.to_string()).into())
+        }
+
+        if self.token_id_recv.inner() == pallas::Base::ZERO {
+            error!(target: "contract::money::client::swap", "Error: Token receive is ZERO");
+            return Err(ClientFailed::InvalidTokenId(self.token_id_recv.to_string()).into())
+        }
 
         if self.coin.note.value != self.value_send {
             return Err(ClientFailed::InvalidAmount(self.coin.note.value).into())
@@ -130,7 +145,7 @@ impl SwapCallBuilder {
         let signature_secret = SecretKey::random(&mut OsRng);
 
         let mut proofs = vec![];
-        debug!("Creating burn proof for input");
+        debug!(target: "contract::money::client::swap", "Creating burn proof for input");
         let (proof, public_inputs) = create_transfer_burn_proof(
             &self.burn_zkbin,
             &self.burn_pk,
@@ -154,7 +169,7 @@ impl SwapCallBuilder {
         // For the output, we create a new coin blind
         let coin_blind = Blind::random(&mut OsRng);
 
-        debug!("Creating mint proof for output");
+        debug!(target: "contract::money::client::swap", "Creating mint proof for output");
         let (proof, public_inputs) = create_transfer_mint_proof(
             &self.mint_zkbin,
             &self.mint_pk,

+ 1 - 1
src/contract/money/src/client/token_mint_v1.rs

@@ -45,7 +45,7 @@ pub struct TokenMintCallBuilder {
 
 impl TokenMintCallBuilder {
     pub fn build(&self) -> Result<TokenMintCallDebris> {
-        debug!("Building Money::TokenMintV1 contract call");
+        debug!(target: "contract::money::client::token_mint", "Building Money::TokenMintV1 contract call");
         let (public_x, public_y) = self.coin_attrs.public_key.xy();
 
         let prover_witnesses = vec![

+ 17 - 7
src/contract/money/src/client/transfer_v1/builder.rs

@@ -15,10 +15,11 @@
  * 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::{
     zk::{Proof, ProvingKey},
     zkas::ZkBinary,
-    Result,
+    ClientFailed, Result,
 };
 use darkfi_sdk::{
     crypto::{
@@ -33,6 +34,7 @@ use rand::rngs::OsRng;
 use super::proof::{create_transfer_burn_proof, create_transfer_mint_proof};
 use crate::{
     client::{compute_remainder_blind, MoneyNote, OwnCoin, TokenId},
+    error::MoneyError,
     model::{CoinAttributes, Input, MoneyTransferParamsV1, Output},
 };
 
@@ -74,8 +76,12 @@ pub type TransferCallOutput = CoinAttributes;
 
 impl TransferCallBuilder {
     pub fn build(self) -> Result<(MoneyTransferParamsV1, TransferCallSecrets)> {
-        debug!("Building Money::TransferV1 contract call");
-        assert!(self.clear_inputs.len() + self.inputs.len() > 0);
+        debug!(target: "contract::money::client::transfer::build", "Building Money::TransferV1 contract call");
+        if self.clear_inputs.is_empty() && self.inputs.is_empty() {
+            return Err(
+                ClientFailed::VerifyError(MoneyError::TransferMissingInputs.to_string()).into()
+            )
+        }
 
         let mut params = MoneyTransferParamsV1 { inputs: vec![], outputs: vec![] };
         let mut signature_secrets = vec![];
@@ -85,7 +91,7 @@ impl TransferCallBuilder {
         let mut input_blinds = vec![];
         let mut output_blinds = vec![];
 
-        debug!("Building anonymous inputs");
+        debug!(target: "contract::money::client::transfer::build", "Building anonymous inputs");
         for (i, input) in self.inputs.iter().enumerate() {
             let value_blind = Blind::random(&mut OsRng);
             input_blinds.push(value_blind);
@@ -93,7 +99,7 @@ impl TransferCallBuilder {
             let signature_secret = SecretKey::random(&mut OsRng);
             signature_secrets.push(signature_secret);
 
-            debug!("Creating transfer burn proof for input {}", i);
+            debug!(target: "contract::money::client::transfer::build", "Creating transfer burn proof for input {}", i);
             let (proof, public_inputs) = create_transfer_burn_proof(
                 &self.burn_zkbin,
                 &self.burn_pk,
@@ -116,7 +122,11 @@ impl TransferCallBuilder {
         }
 
         // This value_blind calc assumes there will always be at least a single output
-        assert!(!self.outputs.is_empty());
+        if self.outputs.is_empty() {
+            return Err(
+                ClientFailed::VerifyError(MoneyError::TransferMissingOutputs.to_string()).into()
+            )
+        }
 
         let mut output_notes = vec![];
 
@@ -129,7 +139,7 @@ impl TransferCallBuilder {
 
             output_blinds.push(value_blind);
 
-            debug!("Creating transfer mint proof for output {}", i);
+            debug!(target: "contract::money::client::transfer::build", "Creating transfer mint proof for output {}", i);
             let (proof, public_inputs) = create_transfer_mint_proof(
                 &self.mint_zkbin,
                 &self.mint_pk,

+ 22 - 8
src/contract/money/src/client/transfer_v1/mod.rs

@@ -15,6 +15,7 @@
  * 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::{zk::ProvingKey, zkas::ZkBinary, ClientFailed, Result};
 use darkfi_sdk::{
     crypto::{pasta_prelude::*, Blind, FuncId, Keypair, MerkleTree, PublicKey},
@@ -25,6 +26,7 @@ use rand::rngs::OsRng;
 
 use crate::{
     client::OwnCoin,
+    error::MoneyError,
     model::{MoneyTransferParamsV1, TokenId},
 };
 
@@ -53,7 +55,7 @@ pub fn select_coins(coins: Vec<OwnCoin>, min_value: u64) -> Result<(Vec<OwnCoin>
     }
 
     if total_value < min_value {
-        error!("Not enough value to build tx inputs");
+        error!(target: "contract::money::client::transfer::select_coins", "Not enough value to build tx inputs");
         return Err(ClientFailed::NotEnoughValue(total_value).into())
     }
 
@@ -93,15 +95,25 @@ pub fn make_transfer_call(
     burn_zkbin: ZkBinary,
     burn_pk: ProvingKey,
 ) -> Result<(MoneyTransferParamsV1, TransferCallSecrets, Vec<OwnCoin>)> {
-    debug!("Building Money::TransferV1 contract call");
-    assert_ne!(value, 0);
-    assert_ne!(token_id.inner(), pallas::Base::ZERO);
-    assert!(!coins.is_empty());
+    debug!(target: "contract::money::client::transfer", "Building Money::TransferV1 contract call");
+    if value == 0 {
+        return Err(ClientFailed::InvalidAmount(value).into())
+    }
+
+    if token_id.inner() == pallas::Base::ZERO {
+        return Err(ClientFailed::InvalidTokenId(token_id.to_string()).into())
+    }
+
+    if coins.is_empty() {
+        return Err(ClientFailed::VerifyError(MoneyError::TransferMissingInputs.to_string()).into())
+    }
 
     // Ensure the coins given to us are all of the same token ID.
     // The money contract base transfer doesn't allow conversions.
     for coin in &coins {
-        assert_eq!(token_id, coin.note.token_id);
+        if coin.note.token_id != token_id {
+            return Err(ClientFailed::InvalidTokenId(coin.note.token_id.to_string()).into())
+        }
     }
 
     let mut inputs = vec![];
@@ -118,7 +130,6 @@ pub fn make_transfer_call(
 
         inputs.push(input);
     }
-    debug!("Selected inputs");
 
     outputs.push(TransferCallOutput {
         public_key: recipient,
@@ -140,7 +151,10 @@ pub fn make_transfer_call(
         });
     }
 
-    assert!(!inputs.is_empty());
+    if inputs.is_empty() {
+        error!(target: "contract::money::client::transfer", "Error: No inputs selected");
+        return Err(ClientFailed::VerifyError(MoneyError::TransferMissingInputs.to_string()).into())
+    }
 
     let xfer_builder = TransferCallBuilder {
         clear_inputs: vec![],

+ 1 - 1
src/contract/money/src/client/transfer_v1/proof.rs

@@ -175,7 +175,7 @@ pub fn create_transfer_mint_proof(
         user_data,
         blind: coin_blind,
     };
-    debug!("Created coin: {:?}", coin);
+    debug!(target: "contract::money::client::transfer::proof", "Created coin: {:?}", coin);
     let coin = coin.to_coin();
 
     let public_inputs = TransferMintRevealed { coin, value_commit, token_commit };