Просмотр исходного кода

contract/money: replaced entrypoints code asserts with errors

skoupidi 2 лет назад
Родитель
Сommit
0008360eb2

+ 9 - 1
src/contract/money/src/entrypoint.rs

@@ -20,12 +20,14 @@ use darkfi_sdk::{
     crypto::{pasta_prelude::Field, smt::EMPTY_NODES_FP, ContractId, MerkleNode, MerkleTree},
     dark_tree::DarkLeaf,
     error::ContractResult,
+    msg,
     pasta::pallas,
     wasm, ContractCall,
 };
 use darkfi_serial::{deserialize, serialize, Encodable, WriteExt};
 
 use crate::{
+    error::MoneyError,
     model::{
         MoneyAuthTokenMintUpdateV1, MoneyFeeUpdateV1, MoneyGenesisMintUpdateV1,
         MoneyPoWRewardUpdateV1, MoneyTokenFreezeUpdateV1, MoneyTokenMintUpdateV1,
@@ -130,7 +132,13 @@ fn init_contract(cid: ContractId, _ix: &[u8]) -> ContractResult {
     let mut roots_value_data = Vec::with_capacity(32 + 1);
     tx_hash.encode(&mut roots_value_data)?;
     call_idx.encode(&mut roots_value_data)?;
-    assert_eq!(roots_value_data.len(), 32 + 1);
+    if roots_value_data.len() != 32 + 1 {
+        msg!(
+            "[money::init_contract] Error: Roots value data length is not expected(32 + 1): {}",
+            roots_value_data.len()
+        );
+        return Err(MoneyError::RootsValueDataMismatch.into())
+    }
 
     // Set up a database tree to hold Merkle roots of all coin trees
     // k=root_hash:32, v=(tx_hash:32, call_idx: 1)

+ 7 - 1
src/contract/money/src/entrypoint/auth_token_mint_v1.rs

@@ -42,7 +42,13 @@ pub(crate) fn money_auth_token_mint_get_metadata_v1(
     let self_data = &self_node.data;
     let self_params: MoneyAuthTokenMintParamsV1 = deserialize(&self_data.data[1..])?;
 
-    assert_eq!(self_node.children_indexes.len(), 1);
+    if self_node.children_indexes.len() != 1 {
+        msg!(
+            "[MintV1] Error: Children indexes length is not expected(1): {}",
+            self_node.children_indexes.len()
+        );
+        return Err(MoneyError::ChildrenIndexesLengthMismatch.into())
+    }
     let child_idx = self_node.children_indexes[0];
     let child_node = &calls[child_idx];
     let child_data = &child_node.data;

+ 33 - 28
src/contract/money/src/error.rs

@@ -103,6 +103,12 @@ pub enum MoneyError {
     // TODO: This should catch-all (TransferMerkle../SwapMerkle...)
     #[error("Coin merkle root not found")]
     CoinMerkleRootNotFound,
+
+    #[error("Roots value data length missmatch")]
+    RootsValueDataMismatch,
+
+    #[error("Children indexes length missmatch")]
+    ChildrenIndexesLengthMismatch,
 }
 
 impl From<MoneyError> for ContractError {
@@ -110,34 +116,33 @@ impl From<MoneyError> for ContractError {
         match e {
             MoneyError::TransferMissingInputs => Self::Custom(1),
             MoneyError::TransferMissingOutputs => Self::Custom(2),
-            // 3 was removed
-            MoneyError::TransferClearInputNonNativeToken => Self::Custom(4),
-            MoneyError::TransferClearInputUnauthorised => Self::Custom(5),
-            MoneyError::TransferMerkleRootNotFound => Self::Custom(6),
-            MoneyError::DuplicateNullifier => Self::Custom(7),
-            // 8 was removed
-            // 9 was removed
-            MoneyError::DuplicateCoin => Self::Custom(10),
-            MoneyError::ValueMismatch => Self::Custom(11),
-            MoneyError::TokenMismatch => Self::Custom(12),
-            MoneyError::InvalidNumberOfInputs => Self::Custom(13),
-            MoneyError::InvalidNumberOfOutputs => Self::Custom(14),
-            MoneyError::SpendHookNonZero => Self::Custom(15),
-            MoneyError::SwapMerkleRootNotFound => Self::Custom(16),
-            MoneyError::TokenIdDoesNotDeriveFromMint => Self::Custom(17),
-            MoneyError::TokenMintFrozen => Self::Custom(18),
-            MoneyError::ParentCallFunctionMismatch => Self::Custom(19),
-            MoneyError::ParentCallInputMismatch => Self::Custom(20),
-            MoneyError::ChildCallFunctionMismatch => Self::Custom(21),
-            MoneyError::ChildCallInputMismatch => Self::Custom(22),
-            MoneyError::GenesisCallNonGenesisBlock => Self::Custom(23),
-            MoneyError::MissingNullifier => Self::Custom(24),
-            MoneyError::PoWRewardCallOnGenesisBlock => Self::Custom(25),
-            MoneyError::PoWRewardRetrieveLastBlockHeightError => Self::Custom(26),
-            MoneyError::PoWRewardCallNotOnNextBlockHeight => Self::Custom(27),
-            MoneyError::FeeMissingInputs => Self::Custom(28),
-            MoneyError::InsufficientFee => Self::Custom(29),
-            MoneyError::CoinMerkleRootNotFound => Self::Custom(30),
+            MoneyError::TransferClearInputNonNativeToken => Self::Custom(3),
+            MoneyError::TransferClearInputUnauthorised => Self::Custom(4),
+            MoneyError::TransferMerkleRootNotFound => Self::Custom(5),
+            MoneyError::DuplicateNullifier => Self::Custom(6),
+            MoneyError::DuplicateCoin => Self::Custom(7),
+            MoneyError::ValueMismatch => Self::Custom(8),
+            MoneyError::TokenMismatch => Self::Custom(9),
+            MoneyError::InvalidNumberOfInputs => Self::Custom(10),
+            MoneyError::InvalidNumberOfOutputs => Self::Custom(11),
+            MoneyError::SpendHookNonZero => Self::Custom(12),
+            MoneyError::SwapMerkleRootNotFound => Self::Custom(13),
+            MoneyError::TokenIdDoesNotDeriveFromMint => Self::Custom(14),
+            MoneyError::TokenMintFrozen => Self::Custom(15),
+            MoneyError::ParentCallFunctionMismatch => Self::Custom(16),
+            MoneyError::ParentCallInputMismatch => Self::Custom(17),
+            MoneyError::ChildCallFunctionMismatch => Self::Custom(18),
+            MoneyError::ChildCallInputMismatch => Self::Custom(19),
+            MoneyError::GenesisCallNonGenesisBlock => Self::Custom(20),
+            MoneyError::MissingNullifier => Self::Custom(21),
+            MoneyError::PoWRewardCallOnGenesisBlock => Self::Custom(22),
+            MoneyError::PoWRewardRetrieveLastBlockHeightError => Self::Custom(23),
+            MoneyError::PoWRewardCallNotOnNextBlockHeight => Self::Custom(24),
+            MoneyError::FeeMissingInputs => Self::Custom(25),
+            MoneyError::InsufficientFee => Self::Custom(26),
+            MoneyError::CoinMerkleRootNotFound => Self::Custom(27),
+            MoneyError::RootsValueDataMismatch => Self::Custom(28),
+            MoneyError::ChildrenIndexesLengthMismatch => Self::Custom(29),
         }
     }
 }