Explorar el Código

contract/deployooor: Update API to use DarkLeaf for contract calls

parazyd hace 2 años
padre
commit
92557d2cec

+ 18 - 32
src/contract/deployooor/src/entrypoint.rs

@@ -18,9 +18,9 @@
 
 use darkfi_sdk::{
     crypto::ContractId,
+    dark_tree::DarkLeaf,
     db::{db_init, db_lookup, db_set, zkas_db_set},
-    error::{ContractError, ContractResult},
-    msg,
+    error::ContractResult,
     util::set_return_data,
     ContractCall,
 };
@@ -77,45 +77,31 @@ fn init_contract(cid: ContractId, _ix: &[u8]) -> ContractResult {
 /// for verifying signatures and zk proofs. The payload given here are all the
 /// contract calls in the transaction.
 fn get_metadata(cid: ContractId, ix: &[u8]) -> ContractResult {
-    let (call_idx, calls): (u32, Vec<ContractCall>) = deserialize(ix)?;
-    if call_idx >= calls.len() as u32 {
-        msg!("Error: call_idx >= calls.len()");
-        return Err(ContractError::Internal)
-    }
+    let (call_idx, calls): (u32, Vec<DarkLeaf<ContractCall>>) = deserialize(ix)?;
+    let self_ = &calls[call_idx as usize].data;
+    let func = DeployFunction::try_from(self_.data[0])?;
 
-    match DeployFunction::try_from(calls[call_idx as usize].data[0])? {
-        DeployFunction::DeployV1 => {
-            let metadata = deploy_get_metadata_v1(cid, call_idx, calls)?;
-            Ok(set_return_data(&metadata)?)
-        }
+    let metadata = match func {
+        DeployFunction::DeployV1 => deploy_get_metadata_v1(cid, call_idx, calls)?,
+        DeployFunction::LockV1 => lock_get_metadata_v1(cid, call_idx, calls)?,
+    };
 
-        DeployFunction::LockV1 => {
-            let metadata = lock_get_metadata_v1(cid, call_idx, calls)?;
-            Ok(set_return_data(&metadata)?)
-        }
-    }
+    set_return_data(&metadata)
 }
 
 /// This function verifies a state transition and produces a state update
 /// if everything is successful.
 fn process_instruction(cid: ContractId, ix: &[u8]) -> ContractResult {
-    let (call_idx, calls): (u32, Vec<ContractCall>) = deserialize(ix)?;
-    if call_idx >= calls.len() as u32 {
-        msg!("Error: call_idx >= calls.len()");
-        return Err(ContractError::Internal)
-    }
+    let (call_idx, calls): (u32, Vec<DarkLeaf<ContractCall>>) = deserialize(ix)?;
+    let self_ = &calls[call_idx as usize].data;
+    let func = DeployFunction::try_from(self_.data[0])?;
 
-    match DeployFunction::try_from(calls[call_idx as usize].data[0])? {
-        DeployFunction::DeployV1 => {
-            let update_data = deploy_process_instruction_v1(cid, call_idx, calls)?;
-            Ok(set_return_data(&update_data)?)
-        }
+    let update_data = match func {
+        DeployFunction::DeployV1 => deploy_process_instruction_v1(cid, call_idx, calls)?,
+        DeployFunction::LockV1 => lock_process_instruction_v1(cid, call_idx, calls)?,
+    };
 
-        DeployFunction::LockV1 => {
-            let update_data = lock_process_instruction_v1(cid, call_idx, calls)?;
-            Ok(set_return_data(&update_data)?)
-        }
-    }
+    set_return_data(&update_data)
 }
 
 /// This function attempts to write a given state update provided the previous

+ 5 - 4
src/contract/deployooor/src/entrypoint/deploy_v1.rs

@@ -18,6 +18,7 @@
 
 use darkfi_sdk::{
     crypto::{ContractId, PublicKey},
+    dark_tree::DarkLeaf,
     db::{db_get, db_lookup, db_set},
     deploy::DeployParamsV1,
     error::{ContractError, ContractResult},
@@ -40,10 +41,10 @@ use crate::{
 pub(crate) fn deploy_get_metadata_v1(
     _cid: ContractId,
     call_idx: u32,
-    calls: Vec<ContractCall>,
+    calls: Vec<DarkLeaf<ContractCall>>,
 ) -> Result<Vec<u8>, ContractError> {
     let self_ = &calls[call_idx as usize];
-    let params: DeployParamsV1 = deserialize(&self_.data[1..])?;
+    let params: DeployParamsV1 = deserialize(&self_.data.data[1..])?;
 
     // Public inputs for the ZK proofs we have to verify
     let mut zk_public_inputs: Vec<(String, Vec<pallas::Base>)> = vec![];
@@ -72,10 +73,10 @@ pub(crate) fn deploy_get_metadata_v1(
 pub(crate) fn deploy_process_instruction_v1(
     cid: ContractId,
     call_idx: u32,
-    calls: Vec<ContractCall>,
+    calls: Vec<DarkLeaf<ContractCall>>,
 ) -> Result<Vec<u8>, ContractError> {
     let self_ = &calls[call_idx as usize];
-    let params: DeployParamsV1 = deserialize(&self_.data[1..])?;
+    let params: DeployParamsV1 = deserialize(&self_.data.data[1..])?;
 
     // In this function, we have to check that the contract isn't locked.
     let lock_db = db_lookup(cid, DEPLOY_CONTRACT_LOCK_TREE)?;

+ 5 - 4
src/contract/deployooor/src/entrypoint/lock_v1.rs

@@ -18,6 +18,7 @@
 
 use darkfi_sdk::{
     crypto::{ContractId, PublicKey},
+    dark_tree::DarkLeaf,
     db::{db_contains_key, db_get, db_lookup, db_set},
     error::{ContractError, ContractResult},
     msg,
@@ -36,10 +37,10 @@ use crate::{
 pub(crate) fn lock_get_metadata_v1(
     _cid: ContractId,
     call_idx: u32,
-    calls: Vec<ContractCall>,
+    calls: Vec<DarkLeaf<ContractCall>>,
 ) -> Result<Vec<u8>, ContractError> {
     let self_ = &calls[call_idx as usize];
-    let params: LockParamsV1 = deserialize(&self_.data[1..])?;
+    let params: LockParamsV1 = deserialize(&self_.data.data[1..])?;
 
     // Public inputs for the ZK proofs we have to verify
     let mut zk_public_inputs: Vec<(String, Vec<pallas::Base>)> = vec![];
@@ -68,10 +69,10 @@ pub(crate) fn lock_get_metadata_v1(
 pub(crate) fn lock_process_instruction_v1(
     cid: ContractId,
     call_idx: u32,
-    calls: Vec<ContractCall>,
+    calls: Vec<DarkLeaf<ContractCall>>,
 ) -> Result<Vec<u8>, ContractError> {
     let self_ = &calls[call_idx as usize];
-    let params: LockParamsV1 = deserialize(&self_.data[1..])?;
+    let params: LockParamsV1 = deserialize(&self_.data.data[1..])?;
 
     // In this function, we check that the contract exists, and that it isn't
     // already locked.