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

money/burn: Implement tx-local state and remove token commitment limitation

x 4 месяцев назад
Родитель
Сommit
06528a9c14
1 измененных файлов с 20 добавлено и 19 удалено
  1. 20 19
      src/contract/money/src/entrypoint/burn_v1.rs

+ 20 - 19
src/contract/money/src/entrypoint/burn_v1.rs

@@ -29,7 +29,11 @@ use darkfi_sdk::{
     error::{ContractError, ContractResult},
     msg,
     pasta::pallas,
-    wasm, ContractCall,
+    wasm::{
+        self,
+        db::{db_contains_key, db_contains_key_local, db_lookup, db_lookup_local},
+    },
+    ContractCall,
 };
 use darkfi_serial::{deserialize, serialize, Encodable};
 
@@ -120,18 +124,15 @@ pub(crate) fn money_burn_process_instruction_v1(
 
     // Access the necessary databases where there is information to
     // validate this state transition.
-    let nullifiers_db = wasm::db::db_lookup(cid, MONEY_CONTRACT_NULLIFIERS_TREE)?;
-    let coin_roots_db = wasm::db::db_lookup(cid, MONEY_CONTRACT_COIN_ROOTS_TREE)?;
+    let nullifiers_db = db_lookup(cid, MONEY_CONTRACT_NULLIFIERS_TREE)?;
+    let coin_roots_db = db_lookup(cid, MONEY_CONTRACT_COIN_ROOTS_TREE)?;
+    let coin_roots_db_local = db_lookup_local(cid, MONEY_CONTRACT_COIN_ROOTS_TREE)?;
 
     let hasher = PoseidonFp::new();
     let empty_leaf = pallas::Base::ZERO;
     let smt_store = SmtWasmDbStorage::new(nullifiers_db);
     let smt = SmtWasmFp::new(smt_store, hasher, &EMPTY_NODES_FP);
 
-    // Grab the expected token commitment. All inputs must use the
-    // same token type.
-    let tokcom = params.inputs[0].token_commit;
-
     // ===================================
     // Perform the actual state transition
     // ===================================
@@ -142,12 +143,18 @@ pub(crate) fn money_burn_process_instruction_v1(
     for (i, input) in params.inputs.iter().enumerate() {
         // The Merkle root is used to know whether this is a coin that
         // existed in a previous state.
-        if !wasm::db::db_contains_key(coin_roots_db, &serialize(&input.merkle_root))? {
-            msg!("[BurnV1] Error: Merkle root not found in previous state (input {})", i);
+        if input.tx_local {
+            if !db_contains_key_local(coin_roots_db_local, &serialize(&input.merkle_root))? {
+                msg!("[BurnV1] Error: Merkle root not found in tx-local state (input {})", i);
+                return Err(MoneyError::TransferMerkleRootNotFound.into())
+            }
+        } else if !db_contains_key(coin_roots_db, &serialize(&input.merkle_root))? {
+            msg!("[BurnV1] Error: Merkle root not found in on-chain state (input {})", i);
             return Err(MoneyError::TransferMerkleRootNotFound.into())
         }
 
-        // The nullifiers should not already exist. It is the double-spend protection.
+        // The nullifiers should not already exist.
+        // It is the double-spend protection.
         if new_nullifiers.contains(&input.nullifier) ||
             smt.get_leaf(&input.nullifier.inner()) != empty_leaf
         {
@@ -155,12 +162,6 @@ pub(crate) fn money_burn_process_instruction_v1(
             return Err(MoneyError::DuplicateNullifier.into())
         }
 
-        // Verify the token commitment is the expected one
-        if tokcom != input.token_commit {
-            msg!("[BurnV1] Error: Token commitment mismatch in input {}", i);
-            return Err(MoneyError::TokenMismatch.into())
-        }
-
         new_nullifiers.push(input.nullifier);
     }
 
@@ -176,9 +177,9 @@ pub(crate) fn money_burn_process_update_v1(
     cid: ContractId,
     update: MoneyBurnUpdateV1,
 ) -> ContractResult {
-    let info_db = wasm::db::db_lookup(cid, MONEY_CONTRACT_INFO_TREE)?;
-    let nullifiers_db = wasm::db::db_lookup(cid, MONEY_CONTRACT_NULLIFIERS_TREE)?;
-    let nullifier_roots_db = wasm::db::db_lookup(cid, MONEY_CONTRACT_NULLIFIER_ROOTS_TREE)?;
+    let info_db = db_lookup(cid, MONEY_CONTRACT_INFO_TREE)?;
+    let nullifiers_db = db_lookup(cid, MONEY_CONTRACT_NULLIFIERS_TREE)?;
+    let nullifier_roots_db = db_lookup(cid, MONEY_CONTRACT_NULLIFIER_ROOTS_TREE)?;
 
     msg!("[BurnV1] Adding new nullifiers to the set");
     wasm::merkle::sparse_merkle_insert_batch(