From 7a931854e403d08217dacfd3ad5b9f878fd405af Mon Sep 17 00:00:00 2001 From: gRoussac Date: Tue, 2 Jul 2024 14:47:58 +0200 Subject: [PATCH 1/5] Remove try_get_named_arg introduced in #4569 As not handling no-value / value but no-value/Some(value) unused yet --- .../contract/src/contract_api/runtime.rs | 31 ------------------- 1 file changed, 31 deletions(-) diff --git a/smart_contracts/contract/src/contract_api/runtime.rs b/smart_contracts/contract/src/contract_api/runtime.rs index 981e1492ee..6e84bb04ab 100644 --- a/smart_contracts/contract/src/contract_api/runtime.rs +++ b/smart_contracts/contract/src/contract_api/runtime.rs @@ -194,37 +194,6 @@ pub fn get_named_arg(name: &str) -> T { bytesrepr::deserialize(arg_bytes).unwrap_or_revert_with(ApiError::InvalidArgument) } -/// Returns given named argument passed to the host for the current module invocation. -/// If the argument is not found, returns `None`. -/// -/// Note that this is only relevant to contracts stored on-chain since a contract deployed directly -/// is not invoked with any arguments. -pub fn try_get_named_arg(name: &str) -> Option { - let arg_size = get_named_arg_size(name)?; - let arg_bytes = if arg_size > 0 { - let res = { - let data_non_null_ptr = contract_api::alloc_bytes(arg_size); - let ret = unsafe { - ext_ffi::casper_get_named_arg( - name.as_bytes().as_ptr(), - name.len(), - data_non_null_ptr.as_ptr(), - arg_size, - ) - }; - let data = - unsafe { Vec::from_raw_parts(data_non_null_ptr.as_ptr(), arg_size, arg_size) }; - api_error::result_from(ret).map(|_| data) - }; - // Assumed to be safe as `get_named_arg_size` checks the argument already - res.unwrap_or_revert() - } else { - // Avoids allocation with 0 bytes and a call to get_named_arg - Vec::new() - }; - bytesrepr::deserialize(arg_bytes).unwrap_or_revert_with(ApiError::InvalidArgument) -} - /// Returns the caller of the current context, i.e. the [`AccountHash`] of the account which made /// the deploy request. pub fn get_caller() -> AccountHash { From 709aa3774281a89c6eb480871b9ef306293095ef Mon Sep 17 00:00:00 2001 From: gRoussac Date: Tue, 2 Jul 2024 16:19:29 +0200 Subject: [PATCH 2/5] Revert + ok --- .../contract/src/contract_api/runtime.rs | 31 +++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/smart_contracts/contract/src/contract_api/runtime.rs b/smart_contracts/contract/src/contract_api/runtime.rs index 6e84bb04ab..ad56d7b14a 100644 --- a/smart_contracts/contract/src/contract_api/runtime.rs +++ b/smart_contracts/contract/src/contract_api/runtime.rs @@ -194,6 +194,37 @@ pub fn get_named_arg(name: &str) -> T { bytesrepr::deserialize(arg_bytes).unwrap_or_revert_with(ApiError::InvalidArgument) } +/// Returns given named argument passed to the host for the current module invocation. +/// If the argument is not found, returns `None`. +/// +/// Note that this is only relevant to contracts stored on-chain since a contract deployed directly +/// is not invoked with any arguments. +pub fn try_get_named_arg(name: &str) -> Option { + let arg_size = get_named_arg_size(name)?; + let arg_bytes = if arg_size > 0 { + let res = { + let data_non_null_ptr = contract_api::alloc_bytes(arg_size); + let ret = unsafe { + ext_ffi::casper_get_named_arg( + name.as_bytes().as_ptr(), + name.len(), + data_non_null_ptr.as_ptr(), + arg_size, + ) + }; + let data = + unsafe { Vec::from_raw_parts(data_non_null_ptr.as_ptr(), arg_size, arg_size) }; + api_error::result_from(ret).map(|_| data) + }; + // Assumed to be safe as `get_named_arg_size` checks the argument already + res.unwrap_or_revert() + } else { + // Avoids allocation with 0 bytes and a call to get_named_arg + Vec::new() + }; + bytesrepr::deserialize(arg_bytes).ok() +} + /// Returns the caller of the current context, i.e. the [`AccountHash`] of the account which made /// the deploy request. pub fn get_caller() -> AccountHash { From 09905a1fc578baa181a560bff0abf5f4c7ac18fc Mon Sep 17 00:00:00 2001 From: gRoussac Date: Tue, 2 Jul 2024 19:45:28 +0200 Subject: [PATCH 3/5] Return Result for try_get_named_arg --- smart_contracts/contract/src/contract_api/runtime.rs | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/smart_contracts/contract/src/contract_api/runtime.rs b/smart_contracts/contract/src/contract_api/runtime.rs index ad56d7b14a..d7ee856448 100644 --- a/smart_contracts/contract/src/contract_api/runtime.rs +++ b/smart_contracts/contract/src/contract_api/runtime.rs @@ -199,8 +199,11 @@ pub fn get_named_arg(name: &str) -> T { /// /// Note that this is only relevant to contracts stored on-chain since a contract deployed directly /// is not invoked with any arguments. -pub fn try_get_named_arg(name: &str) -> Option { - let arg_size = get_named_arg_size(name)?; +pub fn try_get_named_arg(name: &str) -> Result { + let arg_size = match get_named_arg_size(name) { + Some(size) => size, + _ => 0, + }; let arg_bytes = if arg_size > 0 { let res = { let data_non_null_ptr = contract_api::alloc_bytes(arg_size); @@ -222,7 +225,10 @@ pub fn try_get_named_arg(name: &str) -> Option { // Avoids allocation with 0 bytes and a call to get_named_arg Vec::new() }; - bytesrepr::deserialize(arg_bytes).ok() + match bytesrepr::deserialize(arg_bytes) { + Ok(value) => Ok(value), + Err(_) => Err(ApiError::InvalidArgument), + } } /// Returns the caller of the current context, i.e. the [`AccountHash`] of the account which made From 9f35cf3a421063e9df0f48fe8dff947119818463 Mon Sep 17 00:00:00 2001 From: gRoussac Date: Tue, 2 Jul 2024 19:56:02 +0200 Subject: [PATCH 4/5] cosmetic syntax change --- smart_contracts/contract/src/contract_api/runtime.rs | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/smart_contracts/contract/src/contract_api/runtime.rs b/smart_contracts/contract/src/contract_api/runtime.rs index d7ee856448..7d32ef7204 100644 --- a/smart_contracts/contract/src/contract_api/runtime.rs +++ b/smart_contracts/contract/src/contract_api/runtime.rs @@ -200,10 +200,7 @@ pub fn get_named_arg(name: &str) -> T { /// Note that this is only relevant to contracts stored on-chain since a contract deployed directly /// is not invoked with any arguments. pub fn try_get_named_arg(name: &str) -> Result { - let arg_size = match get_named_arg_size(name) { - Some(size) => size, - _ => 0, - }; + let arg_size = get_named_arg_size(name).unwrap_or(0); let arg_bytes = if arg_size > 0 { let res = { let data_non_null_ptr = contract_api::alloc_bytes(arg_size); @@ -225,10 +222,7 @@ pub fn try_get_named_arg(name: &str) -> Result { // Avoids allocation with 0 bytes and a call to get_named_arg Vec::new() }; - match bytesrepr::deserialize(arg_bytes) { - Ok(value) => Ok(value), - Err(_) => Err(ApiError::InvalidArgument), - } + bytesrepr::deserialize(arg_bytes).map_err(|_| ApiError::InvalidArgument) } /// Returns the caller of the current context, i.e. the [`AccountHash`] of the account which made From 94e878572c6b7d0f4714f20cc86ab3e0734d37b7 Mon Sep 17 00:00:00 2001 From: gRoussac Date: Wed, 17 Jul 2024 15:55:35 +0200 Subject: [PATCH 5/5] Revert to Option --- smart_contracts/contract/src/contract_api/runtime.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/smart_contracts/contract/src/contract_api/runtime.rs b/smart_contracts/contract/src/contract_api/runtime.rs index 7d32ef7204..ad56d7b14a 100644 --- a/smart_contracts/contract/src/contract_api/runtime.rs +++ b/smart_contracts/contract/src/contract_api/runtime.rs @@ -199,8 +199,8 @@ pub fn get_named_arg(name: &str) -> T { /// /// Note that this is only relevant to contracts stored on-chain since a contract deployed directly /// is not invoked with any arguments. -pub fn try_get_named_arg(name: &str) -> Result { - let arg_size = get_named_arg_size(name).unwrap_or(0); +pub fn try_get_named_arg(name: &str) -> Option { + let arg_size = get_named_arg_size(name)?; let arg_bytes = if arg_size > 0 { let res = { let data_non_null_ptr = contract_api::alloc_bytes(arg_size); @@ -222,7 +222,7 @@ pub fn try_get_named_arg(name: &str) -> Result { // Avoids allocation with 0 bytes and a call to get_named_arg Vec::new() }; - bytesrepr::deserialize(arg_bytes).map_err(|_| ApiError::InvalidArgument) + bytesrepr::deserialize(arg_bytes).ok() } /// Returns the caller of the current context, i.e. the [`AccountHash`] of the account which made