From 309157e0bae5b201e436c5383189cdddb9de1af3 Mon Sep 17 00:00:00 2001 From: zero Date: Tue, 19 Mar 2024 10:15:24 +0100 Subject: [PATCH] runtime/merkle: return early with SUCCESS (but give a warning) if the coins list for changing the tree is empty. We also don't do any gas calc since nothing on disk was modified. --- src/runtime/import/merkle.rs | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/src/runtime/import/merkle.rs b/src/runtime/import/merkle.rs index 3582d817c..d96e01dbf 100644 --- a/src/runtime/import/merkle.rs +++ b/src/runtime/import/merkle.rs @@ -20,7 +20,7 @@ use std::io::Cursor; use darkfi_sdk::crypto::{MerkleNode, MerkleTree}; use darkfi_serial::{serialize, Decodable, Encodable, WriteExt}; -use log::{debug, error}; +use log::{debug, error, warn}; use wasmer::{FunctionEnvMut, WasmPtr}; use super::acl::acl_allow; @@ -131,7 +131,7 @@ pub(crate) fn merkle_add(mut ctx: FunctionEnvMut, ptr: WasmPtr, len: u3 } }; - // This `key` represents the sled database tree name + // This `key` represents the sled key in info where the Merkle tree is let tree_key: Vec = match Decodable::decode(&mut buf_reader) { Ok(v) => v, Err(e) => { @@ -155,6 +155,15 @@ pub(crate) fn merkle_add(mut ctx: FunctionEnvMut, ptr: WasmPtr, len: u3 } }; + // Nothing to do so just return here + if coins.is_empty() { + warn!( + target: "runtime::merkle::merkle_add", + "[WASM] [{}] merkle_add(): Nothing to add! Returning.", cid, + ); + return darkfi_sdk::entrypoint::SUCCESS + } + // Make sure we've read the entire buffer if buf_reader.position() != (len as u64) { error!( @@ -258,11 +267,7 @@ pub(crate) fn merkle_add(mut ctx: FunctionEnvMut, ptr: WasmPtr, len: u3 } // Here we add the Merkle root to our set of roots - // TODO: We should probably make sure that this root isn't in the set for root in new_roots.iter() { - // FIXME: Why were we writing the set size here? - //let root_index: Vec = serialize(&(set_size as u32)); - //assert_eq!(root_index.len(), 4); debug!( target: "runtime::merkle::merkle_add", "[WASM] [{}] merkle_add(): Appending Merkle root to db: {:?}", cid, root,