From 6dcf89074a1a9180b5a47726989eb48bf2cea2df Mon Sep 17 00:00:00 2001 From: schwartz-concordium <132270889+schwartz-concordium@users.noreply.github.com> Date: Wed, 24 Jan 2024 16:11:49 +0100 Subject: [PATCH 01/26] Update rust code --- rust-bindings/Cargo.lock | 1 + rust-bindings/Cargo.toml | 3 +- rust-bindings/src/lib.rs | 99 +++++++++++++++++++++++++++++++--------- 3 files changed, 80 insertions(+), 23 deletions(-) diff --git a/rust-bindings/Cargo.lock b/rust-bindings/Cargo.lock index e60e2d84..e1a9a41c 100644 --- a/rust-bindings/Cargo.lock +++ b/rust-bindings/Cargo.lock @@ -524,6 +524,7 @@ dependencies = [ "hex", "serde", "serde_json", + "thiserror", ] [[package]] diff --git a/rust-bindings/Cargo.toml b/rust-bindings/Cargo.toml index 16528ce3..88c35a48 100644 --- a/rust-bindings/Cargo.toml +++ b/rust-bindings/Cargo.toml @@ -11,6 +11,7 @@ name="rust_bindings" crate-type = ["cdylib"] [dependencies] +thiserror = "1.0" anyhow="1.0" serde_json = "1.0" serde = { version = "1.0" } @@ -20,4 +21,4 @@ concordium-contracts-common = { version = "8.1.1", features = ["derive-serde"], hex = "0.4" [profile.release] -rpath = true \ No newline at end of file +rpath = true diff --git a/rust-bindings/src/lib.rs b/rust-bindings/src/lib.rs index ce1c534e..277a62f7 100644 --- a/rust-bindings/src/lib.rs +++ b/rust-bindings/src/lib.rs @@ -1,9 +1,9 @@ -use anyhow::{anyhow, Result}; +use anyhow::Result; use concordium_contracts_common::{ - schema::{Type, VersionedModuleSchema}, - Cursor, + schema::{Type, VersionedModuleSchema, impls::VersionedSchemaError}, schema_json::ToJsonError, Cursor }; use serde_json::to_vec; +use thiserror::Error; use std::{ffi::CStr, os::raw::c_char}; pub type JsonString = String; @@ -49,7 +49,7 @@ pub unsafe extern "C" fn schema_display( schema_size: i32, schema_version: FFIByteOption, callback: ResultCallback, -) -> bool { +) -> u32 { let schema = std::slice::from_raw_parts(schema_ptr, schema_size as usize); assign_result(callback, || { schema_display_aux(schema, schema_version.into_option()) @@ -88,7 +88,7 @@ pub unsafe extern "C" fn get_receive_contract_parameter( value_ptr: *const u8, value_size: i32, callback: ResultCallback, -) -> bool { +) -> u32 { assign_result(callback, || { let schema = std::slice::from_raw_parts(schema_ptr, schema_size as usize); let contract_name_str = get_str_from_pointer(contract_name)?; @@ -132,7 +132,7 @@ pub unsafe extern "C" fn get_event_contract( value_ptr: *const u8, value_size: i32, callback: ResultCallback, -) -> bool { +) -> u32 { assign_result(callback, || { let schema = std::slice::from_raw_parts(schema_ptr, schema_size as usize); let contract_name_str = get_str_from_pointer(contract_name)?; @@ -158,20 +158,20 @@ pub unsafe extern "C" fn get_event_contract( /// # Returns /// /// A boolean, that indicates whether the computation was successful or not. -fn assign_result Result>>(callback: ResultCallback, f: F) -> bool { +fn assign_result Result, FFIError>>(callback: ResultCallback, f: F) -> u32 { match f() { Ok(output) => { let out_lenght = output.len() as i32; let ptr = output.as_ptr(); callback(ptr, out_lenght); - true + 0 } Err(e) => { let error = format!("{}", e).into_bytes(); let error_length = error.len() as i32; let ptr = error.as_ptr(); callback(ptr, error_length); - false + e.to_int() } } } @@ -182,45 +182,100 @@ pub fn get_receive_contract_parameter_aux( contract_name: &str, entrypoint: &str, value: &[u8], -) -> Result> { +) -> Result, FFIError> { let module_schema = VersionedModuleSchema::new(schema, &schema_version)?; let parameter_type = module_schema.get_receive_param_schema(contract_name, entrypoint)?; - let deserialized = deserialize_type_value(value, ¶meter_type, true)?; + let deserialized = deserialize_type_value(value, ¶meter_type)?; Ok(deserialized) } -fn schema_display_aux(schema: &[u8], schema_version: Option) -> Result> { +fn schema_display_aux(schema: &[u8], schema_version: Option) -> Result, FFIError> { let display = VersionedModuleSchema::new(schema, &schema_version)?; Ok(display.to_string().into_bytes()) } +#[derive(Error, Debug)] +pub enum FFIError { + + #[error("{}", 0)] + JsonError(String), + #[error("error when using serde")] + SerdeJsonError, + #[error("encountered string which wasn't utf8 encoded")] + Utf8Error, + #[error(transparent)] + VersionedSchemaError(#[from]VersionedSchemaError) +} + +impl FFIError { + fn to_int(&self) -> u32 { + match self { + FFIError::JsonError(_) => 1, + FFIError::SerdeJsonError => 2, + FFIError::Utf8Error => 3, + FFIError::VersionedSchemaError(schema_error) => match schema_error { + VersionedSchemaError::ParseError => 4, + VersionedSchemaError::MissingSchemaVersion => 5, + VersionedSchemaError::InvalidSchemaVersion => 6, + VersionedSchemaError::NoContractInModule => 7, + VersionedSchemaError::NoReceiveInContract => 8, + VersionedSchemaError::NoInitInContract => 9, + VersionedSchemaError::NoParamsInReceive => 10, + VersionedSchemaError::NoParamsInInit => 11, + VersionedSchemaError::NoErrorInReceive => 12, + VersionedSchemaError::NoErrorInInit => 13, + VersionedSchemaError::ErrorNotSupported => 14, + VersionedSchemaError::NoReturnValueInReceive => 15, + VersionedSchemaError::ReturnValueNotSupported => 16, + VersionedSchemaError::NoEventInContract => 17, + VersionedSchemaError::EventNotSupported => 18, + }, + } + } +} + +impl From for FFIError { + fn from(_: std::str::Utf8Error) -> Self { + FFIError::Utf8Error + } +} + +impl From for FFIError { + fn from(_: serde_json::Error) -> Self { + FFIError::SerdeJsonError + } +} + +impl From for FFIError { + fn from(value: ToJsonError) -> Self { + FFIError::JsonError(value.display(true)) + } +} + fn get_event_contract_aux( schema: &[u8], schema_version: Option, contract_name: &str, value: &[u8], -) -> Result> { +) -> Result, FFIError> { let module_schema = VersionedModuleSchema::new(schema, &schema_version)?; let parameter_type = module_schema.get_event_schema(contract_name)?; - let deserialized = deserialize_type_value(value, ¶meter_type, true)?; + let deserialized = deserialize_type_value(value, ¶meter_type)?; Ok(deserialized) } fn deserialize_type_value( value: &[u8], - value_type: &Type, - verbose_error_message: bool, -) -> Result> { + value_type: &Type +) -> Result, FFIError> { let mut cursor = Cursor::new(value); - match value_type.to_json(&mut cursor) { - Ok(v) => Ok(to_vec(&v)?), - Err(e) => Err(anyhow!("{}", e.display(verbose_error_message))), - } + let v = value_type.to_json(&mut cursor)?; + Ok(to_vec(&v)?) } /// The provided raw pointer [`c_char`] must be a [`std::ffi::CString`]. /// The content of the pointer [`c_char`] must not be mutated for the duration /// of lifetime 'a. -fn get_str_from_pointer<'a>(input: *const c_char) -> Result<&'a str> { +fn get_str_from_pointer<'a>(input: *const c_char) -> Result<&'a str, FFIError> { let c_str: &CStr = unsafe { CStr::from_ptr(input) }; Ok(c_str.to_str()?) } From 7be92ca597eef952c823d1f82289961aa456f752 Mon Sep 17 00:00:00 2001 From: schwartz-concordium <132270889+schwartz-concordium@users.noreply.github.com> Date: Fri, 26 Jan 2024 09:27:32 +0100 Subject: [PATCH 02/26] Added enum --- concordium-base | 2 +- rust-bindings/src/lib.rs | 5 ++-- src/Exceptions/InteropBindingException.cs | 17 +++++++----- src/Interop/InteropBinding.cs | 18 ++++++------ src/Interop/Result.cs | 34 +++++++++++++++++++++++ 5 files changed, 57 insertions(+), 19 deletions(-) create mode 100644 src/Interop/Result.cs diff --git a/concordium-base b/concordium-base index cfe43b79..872e8590 160000 --- a/concordium-base +++ b/concordium-base @@ -1 +1 @@ -Subproject commit cfe43b798b369d7da2a949b4a200ad1848da0455 +Subproject commit 872e8590b3de8391a854a5f6282ac49e2005a520 diff --git a/rust-bindings/src/lib.rs b/rust-bindings/src/lib.rs index 277a62f7..006ae8c0 100644 --- a/rust-bindings/src/lib.rs +++ b/rust-bindings/src/lib.rs @@ -1,6 +1,6 @@ use anyhow::Result; use concordium_contracts_common::{ - schema::{Type, VersionedModuleSchema, impls::VersionedSchemaError}, schema_json::ToJsonError, Cursor + schema::{Type, VersionedModuleSchema, VersionedSchemaError}, schema_json::ToJsonError, Cursor }; use serde_json::to_vec; use thiserror::Error; @@ -195,6 +195,7 @@ fn schema_display_aux(schema: &[u8], schema_version: Option) -> Result u32 { + fn to_int(&self) -> u8 { match self { FFIError::JsonError(_) => 1, FFIError::SerdeJsonError => 2, diff --git a/src/Exceptions/InteropBindingException.cs b/src/Exceptions/InteropBindingException.cs index ab27186d..310d287b 100644 --- a/src/Exceptions/InteropBindingException.cs +++ b/src/Exceptions/InteropBindingException.cs @@ -1,20 +1,23 @@ using System.Text; +using Concordium.Sdk.Interop; namespace Concordium.Sdk.Exceptions; /// /// Thrown when a interop call failed with possible error as message. /// -internal sealed class InteropBindingException : Exception +public sealed class InteropBindingException : Exception { private const string EmptyErrorMessage = "Empty error message returned"; + /// + /// Type of error + /// + public Result Result { get; } - internal static InteropBindingException Create(byte[]? message) => - message != null ? new InteropBindingException(Encoding.UTF8.GetString(message)) : Empty(); + internal static InteropBindingException Create(Result result, byte[]? message) => + message != null ? new InteropBindingException(result, Encoding.UTF8.GetString(message)) : Empty(result); - private InteropBindingException(string message) : base(message) - { } + private InteropBindingException(Result result, string message) : base(message) => this.Result = result; - private static InteropBindingException Empty() => new(EmptyErrorMessage); + private static InteropBindingException Empty(Result result) => new(result, EmptyErrorMessage); } - diff --git a/src/Interop/InteropBinding.cs b/src/Interop/InteropBinding.cs index 49006135..b0a3a810 100644 --- a/src/Interop/InteropBinding.cs +++ b/src/Interop/InteropBinding.cs @@ -15,14 +15,14 @@ internal static class InteropBinding private const string DllName = "rust_bindings"; [DllImport(DllName, CallingConvention = CallingConvention.Cdecl, EntryPoint = "schema_display")] - private static extern bool SchemaDisplay( + private static extern Result SchemaDisplay( [MarshalAs(UnmanagedType.LPArray)] byte[] schema, int schema_size, FfiByteOption schema_version, [MarshalAs(UnmanagedType.FunctionPtr)] SetResultCallback callback); [DllImport(DllName, CallingConvention = CallingConvention.Cdecl, EntryPoint = "get_receive_contract_parameter")] - private static extern bool GetReceiveContractParameter( + private static extern Result GetReceiveContractParameter( [MarshalAs(UnmanagedType.LPArray)] byte[] schema, int schema_size, FfiByteOption schema_version, [MarshalAs(UnmanagedType.LPUTF8Str)] string contract_name, @@ -32,7 +32,7 @@ private static extern bool GetReceiveContractParameter( [MarshalAs(UnmanagedType.FunctionPtr)] SetResultCallback callback); [DllImport(DllName, CallingConvention = CallingConvention.Cdecl, EntryPoint = "get_event_contract")] - private static extern bool GetEventContract( + private static extern Result GetEventContract( [MarshalAs(UnmanagedType.LPArray)] byte[] schema, int schema_size, FfiByteOption schema_version, @@ -63,12 +63,12 @@ internal static Utf8Json SchemaDisplay(VersionedModuleSchema schema) Marshal.Copy(ptr, result, 0, size); }); - if (schemaDisplay && result != null) + if (!schemaDisplay.IsError() && result != null) { return new Utf8Json(result); } - var interopException = InteropBindingException.Create(result); + var interopException = InteropBindingException.Create(schemaDisplay, result); throw interopException; } @@ -97,12 +97,12 @@ internal static Utf8Json GetReceiveContractParameter(VersionedModuleSchema schem Marshal.Copy(ptr, result, 0, size); }); - if (receiveContractParameter && result != null) + if (!receiveContractParameter.IsError() && result != null) { return new Utf8Json(result); } - var interopException = InteropBindingException.Create(result); + var interopException = InteropBindingException.Create(receiveContractParameter, result); throw interopException; } @@ -127,12 +127,12 @@ internal static Utf8Json GetEventContract(VersionedModuleSchema schema, Contract Marshal.Copy(ptr, result, 0, size); }); - if (schemaDisplay && result != null) + if (!schemaDisplay.IsError() && result != null) { return new Utf8Json(result); } - var interopException = InteropBindingException.Create(result); + var interopException = InteropBindingException.Create(schemaDisplay, result); throw interopException; } diff --git a/src/Interop/Result.cs b/src/Interop/Result.cs new file mode 100644 index 00000000..fe0d00df --- /dev/null +++ b/src/Interop/Result.cs @@ -0,0 +1,34 @@ +namespace Concordium.Sdk.Interop; + +/// +/// Result type which on errors hold error type information. +/// +public enum Result +{ + /// + /// No error + /// + NoError = 0, + JsonError, + SerdeJsonError, + Utf8Error, + VersionedSchemaErrorParseError, + VersionedSchemaErrorMissingSchemaVersion, + VersionedSchemaErrorNoContractInModule, + VersionedSchemaErrorNoReceiveInContract, + VersionedSchemaErrorNoInitInContract, + VersionedSchemaErrorNoParamsInReceive, + VersionedSchemaErrorNoParamsInInit, + VersionedSchemaErrorNoErrorInReceive, + VersionedSchemaErrorNoErrorInInit, + VersionedSchemaErrorErrorNotSupported, + VersionedSchemaErrorNoReturnValueInReceive, + VersionedSchemaErrorReturnValueNotSupported, + VersionedSchemaErrorNoEventInContract, + VersionedSchemaErrorEventNotSupported, +} + +internal static class ErrorExtensions +{ + internal static bool IsError(this Result result) => result != Result.NoError; +} From b8ad709523ce3b4f964a34cf9f4f82d6423b4002 Mon Sep 17 00:00:00 2001 From: schwartz-concordium <132270889+schwartz-concordium@users.noreply.github.com> Date: Fri, 26 Jan 2024 10:48:43 +0100 Subject: [PATCH 03/26] bump --- rust-bindings/Cargo.lock | 40 ++++++++++++++++++++++------------------ rust-bindings/Cargo.toml | 2 +- rust-bindings/src/lib.rs | 8 ++++---- 3 files changed, 27 insertions(+), 23 deletions(-) diff --git a/rust-bindings/Cargo.lock b/rust-bindings/Cargo.lock index e1a9a41c..1efcef23 100644 --- a/rust-bindings/Cargo.lock +++ b/rust-bindings/Cargo.lock @@ -66,9 +66,9 @@ dependencies = [ [[package]] name = "block-buffer" -version = "0.9.0" +version = "0.10.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4152116fd6e9dadb291ae18fc1ec3575ed6d84c29642d97890f4b4a3417297e4" +checksum = "3078c7629b62d3f0439517fa394996acacc5cbc91c5a20d8c658e77abd503a71" dependencies = [ "generic-array", ] @@ -120,11 +120,12 @@ dependencies = [ [[package]] name = "bs58" -version = "0.4.0" +version = "0.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "771fe0050b883fcc3ea2359b1a96bcfbc090b7116eae7c3c512c7a083fdf23d3" +checksum = "f5353f36341f7451062466f0b755b96ac3a9547e4d7f6b70d603fc721a7d7896" dependencies = [ "sha2", + "tinyvec", ] [[package]] @@ -192,7 +193,7 @@ dependencies = [ [[package]] name = "concordium-contracts-common" -version = "8.1.1" +version = "9.0.0" dependencies = [ "base64", "bs58", @@ -235,12 +236,23 @@ dependencies = [ ] [[package]] -name = "digest" -version = "0.9.0" +name = "crypto-common" +version = "0.1.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d3dd60d1080a57a05ab032377049e0591415d2b31afd7028356dbf3cc6dcb066" +checksum = "1bfb12502f3fc46cca1bb51ac28df9d618d813cdc3d2f25b9fe775a34af26bb3" dependencies = [ "generic-array", + "typenum", +] + +[[package]] +name = "digest" +version = "0.10.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9ed9a281f7bc9b7576e61468ba615a66a5c8cfdff42420a70aa82701a3b1e292" +dependencies = [ + "block-buffer", + "crypto-common", ] [[package]] @@ -383,12 +395,6 @@ version = "1.18.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "dd8b5dd2ae5ed71462c540258bedcb51965123ad7e7ccf4b9a8cafaa4a63576d" -[[package]] -name = "opaque-debug" -version = "0.3.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "624a8340c38c1b80fd549087862da4ba43e08858af025b236e509b6649fc13d5" - [[package]] name = "ppv-lite86" version = "0.2.17" @@ -588,15 +594,13 @@ dependencies = [ [[package]] name = "sha2" -version = "0.9.9" +version = "0.10.8" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4d58a1e1bf39749807d89cf2d98ac2dfa0ff1cb3faa38fbb64dd88ac8013d800" +checksum = "793db75ad2bcafc3ffa7c68b215fee268f537982cd901d132f89c6343f3a3dc8" dependencies = [ - "block-buffer", "cfg-if", "cpufeatures", "digest", - "opaque-debug", ] [[package]] diff --git a/rust-bindings/Cargo.toml b/rust-bindings/Cargo.toml index 88c35a48..4c445843 100644 --- a/rust-bindings/Cargo.toml +++ b/rust-bindings/Cargo.toml @@ -15,7 +15,7 @@ thiserror = "1.0" anyhow="1.0" serde_json = "1.0" serde = { version = "1.0" } -concordium-contracts-common = { version = "8.1.1", features = ["derive-serde"], path = "../concordium-base/smart-contracts/contracts-common/concordium-contracts-common" } +concordium-contracts-common = { version = "9.0.0", features = ["derive-serde"], path = "../concordium-base/smart-contracts/contracts-common/concordium-contracts-common" } [dev-dependencies] hex = "0.4" diff --git a/rust-bindings/src/lib.rs b/rust-bindings/src/lib.rs index 006ae8c0..1f7a6ee5 100644 --- a/rust-bindings/src/lib.rs +++ b/rust-bindings/src/lib.rs @@ -49,7 +49,7 @@ pub unsafe extern "C" fn schema_display( schema_size: i32, schema_version: FFIByteOption, callback: ResultCallback, -) -> u32 { +) -> u8 { let schema = std::slice::from_raw_parts(schema_ptr, schema_size as usize); assign_result(callback, || { schema_display_aux(schema, schema_version.into_option()) @@ -88,7 +88,7 @@ pub unsafe extern "C" fn get_receive_contract_parameter( value_ptr: *const u8, value_size: i32, callback: ResultCallback, -) -> u32 { +) -> u8 { assign_result(callback, || { let schema = std::slice::from_raw_parts(schema_ptr, schema_size as usize); let contract_name_str = get_str_from_pointer(contract_name)?; @@ -132,7 +132,7 @@ pub unsafe extern "C" fn get_event_contract( value_ptr: *const u8, value_size: i32, callback: ResultCallback, -) -> u32 { +) -> u8 { assign_result(callback, || { let schema = std::slice::from_raw_parts(schema_ptr, schema_size as usize); let contract_name_str = get_str_from_pointer(contract_name)?; @@ -158,7 +158,7 @@ pub unsafe extern "C" fn get_event_contract( /// # Returns /// /// A boolean, that indicates whether the computation was successful or not. -fn assign_result Result, FFIError>>(callback: ResultCallback, f: F) -> u32 { +fn assign_result Result, FFIError>>(callback: ResultCallback, f: F) -> u8 { match f() { Ok(output) => { let out_lenght = output.len() as i32; From f6fb722a1753a24fdcfad95697cec111f02813bb Mon Sep 17 00:00:00 2001 From: schwartz-concordium <132270889+schwartz-concordium@users.noreply.github.com> Date: Fri, 26 Jan 2024 10:51:31 +0100 Subject: [PATCH 04/26] Update lib.rs --- rust-bindings/src/lib.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/rust-bindings/src/lib.rs b/rust-bindings/src/lib.rs index 1f7a6ee5..d352307a 100644 --- a/rust-bindings/src/lib.rs +++ b/rust-bindings/src/lib.rs @@ -195,7 +195,6 @@ fn schema_display_aux(schema: &[u8], schema_version: Option) -> Result Date: Fri, 26 Jan 2024 10:54:25 +0100 Subject: [PATCH 05/26] fix format --- rust-bindings/src/lib.rs | 25 +++++++++---------------- 1 file changed, 9 insertions(+), 16 deletions(-) diff --git a/rust-bindings/src/lib.rs b/rust-bindings/src/lib.rs index d352307a..80299502 100644 --- a/rust-bindings/src/lib.rs +++ b/rust-bindings/src/lib.rs @@ -1,10 +1,12 @@ use anyhow::Result; use concordium_contracts_common::{ - schema::{Type, VersionedModuleSchema, VersionedSchemaError}, schema_json::ToJsonError, Cursor + schema::{Type, VersionedModuleSchema, VersionedSchemaError}, + schema_json::ToJsonError, + Cursor }; use serde_json::to_vec; -use thiserror::Error; use std::{ffi::CStr, os::raw::c_char}; +use thiserror::Error; pub type JsonString = String; @@ -204,7 +206,7 @@ pub enum FFIError { #[error("encountered string which wasn't utf8 encoded")] Utf8Error, #[error(transparent)] - VersionedSchemaError(#[from]VersionedSchemaError) + VersionedSchemaError(#[from]VersionedSchemaError), } impl FFIError { @@ -235,21 +237,15 @@ impl FFIError { } impl From for FFIError { - fn from(_: std::str::Utf8Error) -> Self { - FFIError::Utf8Error - } + fn from(_: std::str::Utf8Error) -> Self { FFIError::Utf8Error } } impl From for FFIError { - fn from(_: serde_json::Error) -> Self { - FFIError::SerdeJsonError - } + fn from(_: serde_json::Error) -> Self { FFIError::SerdeJsonError } } impl From for FFIError { - fn from(value: ToJsonError) -> Self { - FFIError::JsonError(value.display(true)) - } + fn from(value: ToJsonError) -> Self { FFIError::JsonError(value.display(true)) } } fn get_event_contract_aux( @@ -264,10 +260,7 @@ fn get_event_contract_aux( Ok(deserialized) } -fn deserialize_type_value( - value: &[u8], - value_type: &Type -) -> Result, FFIError> { +fn deserialize_type_value(value: &[u8], value_type: &Type) -> Result, FFIError> { let mut cursor = Cursor::new(value); let v = value_type.to_json(&mut cursor)?; Ok(to_vec(&v)?) From 702e52a88c1c3de0f6de2c317c68b8540a5b0365 Mon Sep 17 00:00:00 2001 From: schwartz-concordium <132270889+schwartz-concordium@users.noreply.github.com> Date: Fri, 26 Jan 2024 10:56:13 +0100 Subject: [PATCH 06/26] Update lib.rs --- rust-bindings/src/lib.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/rust-bindings/src/lib.rs b/rust-bindings/src/lib.rs index 80299502..065fa7bd 100644 --- a/rust-bindings/src/lib.rs +++ b/rust-bindings/src/lib.rs @@ -2,7 +2,7 @@ use anyhow::Result; use concordium_contracts_common::{ schema::{Type, VersionedModuleSchema, VersionedSchemaError}, schema_json::ToJsonError, - Cursor + Cursor, }; use serde_json::to_vec; use std::{ffi::CStr, os::raw::c_char}; @@ -206,7 +206,7 @@ pub enum FFIError { #[error("encountered string which wasn't utf8 encoded")] Utf8Error, #[error(transparent)] - VersionedSchemaError(#[from]VersionedSchemaError), + VersionedSchemaError(#[from] VersionedSchemaError), } impl FFIError { From 9712402dd0e8de7f0c025cc25257d3895df3e44f Mon Sep 17 00:00:00 2001 From: schwartz-concordium <132270889+schwartz-concordium@users.noreply.github.com> Date: Fri, 26 Jan 2024 10:57:55 +0100 Subject: [PATCH 07/26] Update lib.rs --- rust-bindings/src/lib.rs | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/rust-bindings/src/lib.rs b/rust-bindings/src/lib.rs index 065fa7bd..3556c15a 100644 --- a/rust-bindings/src/lib.rs +++ b/rust-bindings/src/lib.rs @@ -1,7 +1,7 @@ use anyhow::Result; use concordium_contracts_common::{ - schema::{Type, VersionedModuleSchema, VersionedSchemaError}, - schema_json::ToJsonError, + schema::{Type, VersionedModuleSchema, VersionedSchemaError}, + schema_json::ToJsonError, Cursor, }; use serde_json::to_vec; @@ -198,7 +198,6 @@ fn schema_display_aux(schema: &[u8], schema_version: Option) -> Result Date: Fri, 26 Jan 2024 11:32:29 +0100 Subject: [PATCH 08/26] added documentation --- src/Interop/Result.cs | 91 +++++++++++++++++++++++++++++++++++-------- 1 file changed, 74 insertions(+), 17 deletions(-) diff --git a/src/Interop/Result.cs b/src/Interop/Result.cs index fe0d00df..f5f9d8a8 100644 --- a/src/Interop/Result.cs +++ b/src/Interop/Result.cs @@ -9,23 +9,80 @@ public enum Result /// No error /// NoError = 0, - JsonError, - SerdeJsonError, - Utf8Error, - VersionedSchemaErrorParseError, - VersionedSchemaErrorMissingSchemaVersion, - VersionedSchemaErrorNoContractInModule, - VersionedSchemaErrorNoReceiveInContract, - VersionedSchemaErrorNoInitInContract, - VersionedSchemaErrorNoParamsInReceive, - VersionedSchemaErrorNoParamsInInit, - VersionedSchemaErrorNoErrorInReceive, - VersionedSchemaErrorNoErrorInInit, - VersionedSchemaErrorErrorNotSupported, - VersionedSchemaErrorNoReturnValueInReceive, - VersionedSchemaErrorReturnValueNotSupported, - VersionedSchemaErrorNoEventInContract, - VersionedSchemaErrorEventNotSupported, + /// + /// Represents errors occurring while deserializing to the schema JSON format. + /// + JsonError = 1, + /// + /// This type represents all possible errors that can occur when serializing or + /// deserializing JSON data. + /// + SerdeJsonError = 2, + /// + /// Errors which can occur when attempting to interpret a sequence of bytes + /// as a string. + /// + Utf8Error = 3, + /// + /// Versioned Schema Error - Parse error + /// + VersionedSchemaErrorParseError = 4, + /// + /// Versioned Schema Error - Missing Schema Version + /// + VersionedSchemaErrorMissingSchemaVersion = 5, + /// + /// Versioned Schema Error - Invalid Schema Version + /// + VersionedSchemaErrorInvalidSchemaVersion = 6, + /// + /// Versioned Schema Error - No Contract In Module + /// + VersionedSchemaErrorNoContractInModule = 7, + /// + /// Versioned Schema Error - Receive function schema not found in contract schema + /// + VersionedSchemaErrorNoReceiveInContract = 8, + /// + /// Versioned Schema Error - Init function schema not found in contract schema + /// + VersionedSchemaErrorNoInitInContract = 9, + /// + /// Versioned Schema Error - Receive function schema does not contain a parameter schema + /// + VersionedSchemaErrorNoParamsInReceive = 10, + /// + /// Versioned Schema Error - Init function schema does not contain a parameter schema + /// + VersionedSchemaErrorNoParamsInInit = 11, + /// + /// Versioned Schema Error - Receive function schema not found in contract schema + /// + VersionedSchemaErrorNoErrorInReceive = 12, + /// + /// Versioned Schema Error - Init function schema does not contain an error schema + /// + VersionedSchemaErrorNoErrorInInit = 13, + /// + /// Versioned Schema Error - Errors not supported for this module version + /// + VersionedSchemaErrorErrorNotSupported = 14, + /// + /// Versioned Schema Error - Receive function schema has no return value schema + /// + VersionedSchemaErrorNoReturnValueInReceive = 15, + /// + /// Versioned Schema Error - Return values not supported for this module version + /// + VersionedSchemaErrorReturnValueNotSupported = 16, + /// + /// Versioned Schema Error - Event schema not found in contract schema + /// + VersionedSchemaErrorNoEventInContract = 17, + /// + /// Versioned Schema Error - Events not supported for this module version + /// + VersionedSchemaErrorEventNotSupported = 18, } internal static class ErrorExtensions From 446936be43c9a781a70f402fe2dadf8453685612 Mon Sep 17 00:00:00 2001 From: schwartz-concordium <132270889+schwartz-concordium@users.noreply.github.com> Date: Fri, 26 Jan 2024 11:34:13 +0100 Subject: [PATCH 09/26] Update ProtocolVersion.cs --- src/Types/ProtocolVersion.cs | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/Types/ProtocolVersion.cs b/src/Types/ProtocolVersion.cs index ab5b51a5..ca768737 100644 --- a/src/Types/ProtocolVersion.cs +++ b/src/Types/ProtocolVersion.cs @@ -34,7 +34,12 @@ public enum ProtocolVersion P5, /// Protocol `P6` is a future protocol update that will introduce a new /// consensus. - P6 + P6, + /// + /// Protocol `P7` modifies hashing to better support light clients, and + /// implements tokenomics changes. + /// + P7, } @@ -60,6 +65,7 @@ internal static ProtocolVersion Into(this Grpc.V2.ProtocolVersion protocolVersio Grpc.V2.ProtocolVersion._4 => ProtocolVersion.P4, Grpc.V2.ProtocolVersion._5 => ProtocolVersion.P5, Grpc.V2.ProtocolVersion._6 => ProtocolVersion.P6, + Grpc.V2.ProtocolVersion._7 => ProtocolVersion.P7, _ => throw new MissingEnumException(protocolVersion) }; } From c01a00b69b81f2320e5b159b9cef3df673fd4e04 Mon Sep 17 00:00:00 2001 From: schwartz-concordium <132270889+schwartz-concordium@users.noreply.github.com> Date: Fri, 26 Jan 2024 11:47:41 +0100 Subject: [PATCH 10/26] added test for exceptions --- .../UnitTests/Interop/InteropBindingTests.cs | 38 +++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/tests/UnitTests/Interop/InteropBindingTests.cs b/tests/UnitTests/Interop/InteropBindingTests.cs index bf08e6fc..3e74e151 100644 --- a/tests/UnitTests/Interop/InteropBindingTests.cs +++ b/tests/UnitTests/Interop/InteropBindingTests.cs @@ -1,6 +1,7 @@ using System; using System.IO; using System.Threading.Tasks; +using Concordium.Sdk.Exceptions; using Concordium.Sdk.Interop; using Concordium.Sdk.Types; using FluentAssertions; @@ -44,6 +45,22 @@ await Verifier.Verify(message.ToString()) .UseDirectory("__snapshots__"); } + [Fact] + public async Task GivenBadSchema_WhenSchemaDisplay_ThenThrowExceptionWithParseError() + { + // Arrange + var schema = Convert.FromHexString((await File.ReadAllTextAsync("./Data/cis2-nft-schema")).Trim()); + var versionedModuleSchema = new VersionedModuleSchema(schema[..^3], ModuleSchemaVersion.V1); + + // Act + + var action = () => InteropBinding.SchemaDisplay(versionedModuleSchema); + + // Assert + action.Should().Throw() + .Where(e => e.Result == Result.VersionedSchemaErrorParseError); + } + [Fact] public async Task WhenDisplayReceiveParam_ThenReturnParams() { @@ -66,6 +83,27 @@ await Verifier.Verify(message.ToString()) .UseDirectory("__snapshots__"); } + [Fact] + public async Task GivenBadReceiveParam_WhenDisplayReceiveParam_ThenThrowException() + { + // Arrange + var schema = (await File.ReadAllTextAsync("./Data/cis2_wCCD_sub")).Trim(); + const string contractName = "cis2_wCCD"; + const string entrypoint = "wrap"; + var value = Convert.FromHexString("005f8b99a3ea8089002291fd646554848b00e7a0cd934e5bad6e6e93a4d4f4dc790000"); + var versionedModuleSchema = new VersionedModuleSchema(Convert.FromHexString(schema), ModuleSchemaVersion.Undefined); + var parameter = new Parameter(value[..^3]); + var contractIdentifier = new ContractIdentifier(contractName); + var entryPoint = new EntryPoint(entrypoint); + + // Act + var action = () => InteropBinding.GetReceiveContractParameter(versionedModuleSchema, contractIdentifier, entryPoint, parameter); + + // Assert + action.Should().Throw() + .Where(e => e.Result == Result.NoError); + } + [Fact] public async Task WhenDisplayEvent_ThenReturnEvent() { From 02d9129eaf14fd81ecb6b1d706d4c4caa0d2ea84 Mon Sep 17 00:00:00 2001 From: schwartz-concordium <132270889+schwartz-concordium@users.noreply.github.com> Date: Fri, 26 Jan 2024 11:59:45 +0100 Subject: [PATCH 11/26] Added test for errors --- rust-bindings/src/lib.rs | 2 +- tests/UnitTests/Interop/InteropBindingTests.cs | 8 ++++++-- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/rust-bindings/src/lib.rs b/rust-bindings/src/lib.rs index 3556c15a..d788a683 100644 --- a/rust-bindings/src/lib.rs +++ b/rust-bindings/src/lib.rs @@ -198,7 +198,7 @@ fn schema_display_aux(schema: &[u8], schema_version: Option) -> Result() - .Where(e => e.Result == Result.VersionedSchemaErrorParseError); + .Where(e => + e.Result == Result.VersionedSchemaErrorParseError && + e.Message.Equals("Parse error", StringComparison.Ordinal)); } [Fact] @@ -101,7 +103,9 @@ public async Task GivenBadReceiveParam_WhenDisplayReceiveParam_ThenThrowExceptio // Assert action.Should().Throw() - .Where(e => e.Result == Result.NoError); + .Where(e => + e.Result == Result.JsonError && + e.Message.StartsWith("Failed to deserialize AccountAddress due to: Could not parse AccountAddress")); } [Fact] From 095d2739862de48d829d319c13803ced8ef5df1b Mon Sep 17 00:00:00 2001 From: schwartz-concordium <132270889+schwartz-concordium@users.noreply.github.com> Date: Fri, 26 Jan 2024 13:05:30 +0100 Subject: [PATCH 12/26] Update InteropBindingTests.cs --- .../UnitTests/Interop/InteropBindingTests.cs | 133 +++++++++++++++++- 1 file changed, 131 insertions(+), 2 deletions(-) diff --git a/tests/UnitTests/Interop/InteropBindingTests.cs b/tests/UnitTests/Interop/InteropBindingTests.cs index 82db4fb4..6d3f93aa 100644 --- a/tests/UnitTests/Interop/InteropBindingTests.cs +++ b/tests/UnitTests/Interop/InteropBindingTests.cs @@ -50,7 +50,7 @@ public async Task GivenBadSchema_WhenSchemaDisplay_ThenThrowExceptionWithParseEr { // Arrange var schema = Convert.FromHexString((await File.ReadAllTextAsync("./Data/cis2-nft-schema")).Trim()); - var versionedModuleSchema = new VersionedModuleSchema(schema[..^3], ModuleSchemaVersion.V1); + var versionedModuleSchema = new VersionedModuleSchema(schema[..^3], ModuleSchemaVersion.V1); // Bad schema // Act @@ -94,7 +94,7 @@ public async Task GivenBadReceiveParam_WhenDisplayReceiveParam_ThenThrowExceptio const string entrypoint = "wrap"; var value = Convert.FromHexString("005f8b99a3ea8089002291fd646554848b00e7a0cd934e5bad6e6e93a4d4f4dc790000"); var versionedModuleSchema = new VersionedModuleSchema(Convert.FromHexString(schema), ModuleSchemaVersion.Undefined); - var parameter = new Parameter(value[..^3]); + var parameter = new Parameter(value[..^3]); // Bad parameter var contractIdentifier = new ContractIdentifier(contractName); var entryPoint = new EntryPoint(entrypoint); @@ -108,6 +108,72 @@ public async Task GivenBadReceiveParam_WhenDisplayReceiveParam_ThenThrowExceptio e.Message.StartsWith("Failed to deserialize AccountAddress due to: Could not parse AccountAddress")); } + [Fact] + public async Task GivenBadContractIdentifier_WhenDisplayReceiveParam_ThenThrowException() + { + // Arrange + var schema = (await File.ReadAllTextAsync("./Data/cis2_wCCD_sub")).Trim(); + const string contractName = "cis2_wCCD"; + const string entrypoint = "wrap"; + var versionedModuleSchema = new VersionedModuleSchema(Convert.FromHexString(schema), ModuleSchemaVersion.Undefined); + var parameter = new Parameter(Convert.FromHexString("005f8b99a3ea8089002291fd646554848b00e7a0cd934e5bad6e6e93a4d4f4dc790000")); + var contractIdentifier = new ContractIdentifier(contractName[..^3]); // Bad contract identifier + var entryPoint = new EntryPoint(entrypoint); + + // Act + var action = () => InteropBinding.GetReceiveContractParameter(versionedModuleSchema, contractIdentifier, entryPoint, parameter); + + // Assert + action.Should().Throw() + .Where(e => + e.Result == Result.VersionedSchemaErrorNoContractInModule && + e.Message.Equals("Unable to find contract schema in module schema", StringComparison.Ordinal)); + } + + [Fact] + public async Task GivenBadEntrypoint_WhenDisplayReceiveParam_ThenThrowException() + { + // Arrange + var schema = (await File.ReadAllTextAsync("./Data/cis2_wCCD_sub")).Trim(); + const string contractName = "cis2_wCCD"; + const string entrypoint = "wrap"; + var versionedModuleSchema = new VersionedModuleSchema(Convert.FromHexString(schema), ModuleSchemaVersion.Undefined); + var parameter = new Parameter(Convert.FromHexString("005f8b99a3ea8089002291fd646554848b00e7a0cd934e5bad6e6e93a4d4f4dc790000")); + var contractIdentifier = new ContractIdentifier(contractName); + var entryPoint = new EntryPoint(entrypoint[..^2]); // Bad entrypoint + + // Act + var action = () => InteropBinding.GetReceiveContractParameter(versionedModuleSchema, contractIdentifier, entryPoint, parameter); + + // Assert + action.Should().Throw() + .Where(e => + e.Result == Result.VersionedSchemaErrorNoReceiveInContract && + e.Message.Equals("Receive function schema not found in contract schema", StringComparison.Ordinal)); + } + + [Fact] + public async Task GivenBadSchema_WhenDisplayReceiveParam_ThenThrowException() + { + // Arrange + var schema = Convert.FromHexString((await File.ReadAllTextAsync("./Data/cis2_wCCD_sub")).Trim()); + const string contractName = "cis2_wCCD"; + const string entrypoint = "wrap"; + var versionedModuleSchema = new VersionedModuleSchema(schema[30..], ModuleSchemaVersion.Undefined); // Bad schema + var parameter = new Parameter(Convert.FromHexString("005f8b99a3ea8089002291fd646554848b00e7a0cd934e5bad6e6e93a4d4f4dc790000")); + var contractIdentifier = new ContractIdentifier(contractName); + var entryPoint = new EntryPoint(entrypoint[..^2]); + + // Act + var action = () => InteropBinding.GetReceiveContractParameter(versionedModuleSchema, contractIdentifier, entryPoint, parameter); + + // Assert + action.Should().Throw() + .Where(e => + e.Result == Result.VersionedSchemaErrorMissingSchemaVersion && + e.Message.Equals("Missing Schema Version", StringComparison.Ordinal)); + } + [Fact] public async Task WhenDisplayEvent_ThenReturnEvent() { @@ -128,6 +194,69 @@ await Verifier.Verify(message.ToString()) .UseDirectory("__snapshots__"); } + [Fact] + public async Task GivenBadSchema_WhenDisplayEvent_ThenThrowException() + { + // Arrange + var schema = Convert.FromHexString((await File.ReadAllTextAsync("./Data/cis2_wCCD_sub")).Trim()); + const string contractName = "cis2_wCCD"; + const string value = "fe00c0843d005f8b99a3ea8089002291fd646554848b00e7a0cd934e5bad6e6e93a4d4f4dc79"; + var versionedModuleSchema = new VersionedModuleSchema(schema[..^3], ModuleSchemaVersion.Undefined); // Bad schema + var contractIdentifier = new ContractIdentifier(contractName); + var contractEvent = new ContractEvent(Convert.FromHexString(value)); + + // Act + var action = () => InteropBinding.GetEventContract(versionedModuleSchema, contractIdentifier, contractEvent); + + // Assert + action.Should().Throw() + .Where(e => + e.Result == Result.VersionedSchemaErrorMissingSchemaVersion && + e.Message.Equals("Missing Schema Version", StringComparison.Ordinal)); + } + + [Fact] + public async Task GivenBadContractIdentifier_WhenDisplayEvent_ThenThrowException() + { + // Arrange + var schema = (await File.ReadAllTextAsync("./Data/cis2_wCCD_sub")).Trim(); + const string contractName = "cis2_wCCD"; + const string value = "fe00c0843d005f8b99a3ea8089002291fd646554848b00e7a0cd934e5bad6e6e93a4d4f4dc79"; + var versionedModuleSchema = new VersionedModuleSchema(Convert.FromHexString(schema), ModuleSchemaVersion.Undefined); + var contractIdentifier = new ContractIdentifier(contractName[..^3]); // Bad contract identifier + var contractEvent = new ContractEvent(Convert.FromHexString(value)); + + // Act + var action = () => InteropBinding.GetEventContract(versionedModuleSchema, contractIdentifier, contractEvent); + + // Assert + action.Should().Throw() + .Where(e => + e.Result == Result.VersionedSchemaErrorNoContractInModule && + e.Message.Equals("Unable to find contract schema in module schema", StringComparison.Ordinal)); + } + + [Fact] + public async Task GivenBadContractEvent_WhenDisplayEvent_ThenThrowException() + { + // Arrange + var schema = (await File.ReadAllTextAsync("./Data/cis2_wCCD_sub")).Trim(); + const string contractName = "cis2_wCCD"; + const string value = "fe00c0843d005f8b99a3ea8089002291fd646554848b00e7a0cd934e5bad6e6e93a4d4f4dc79"; + var versionedModuleSchema = new VersionedModuleSchema(Convert.FromHexString(schema), ModuleSchemaVersion.Undefined); + var contractIdentifier = new ContractIdentifier(contractName); + var contractEvent = new ContractEvent(Convert.FromHexString(value)[..^3]); // Bad contract event + + // Act + var action = () => InteropBinding.GetEventContract(versionedModuleSchema, contractIdentifier, contractEvent); + + // Assert + action.Should().Throw() + .Where(e => + e.Result == Result.JsonError && + e.Message.StartsWith("Failed to deserialize AccountAddress due to: Could not parse")); + } + [Theory] [InlineData(ModuleSchemaVersion.V0, (byte)0)] [InlineData(ModuleSchemaVersion.V2, (byte)2)] From 9de5bba80fd0fb17267696aa22552ccc08385142 Mon Sep 17 00:00:00 2001 From: schwartz-concordium <132270889+schwartz-concordium@users.noreply.github.com> Date: Fri, 26 Jan 2024 13:11:13 +0100 Subject: [PATCH 13/26] Update InteropBindingTests.cs --- tests/UnitTests/Interop/InteropBindingTests.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/UnitTests/Interop/InteropBindingTests.cs b/tests/UnitTests/Interop/InteropBindingTests.cs index 6d3f93aa..1f96dfaa 100644 --- a/tests/UnitTests/Interop/InteropBindingTests.cs +++ b/tests/UnitTests/Interop/InteropBindingTests.cs @@ -105,7 +105,7 @@ public async Task GivenBadReceiveParam_WhenDisplayReceiveParam_ThenThrowExceptio action.Should().Throw() .Where(e => e.Result == Result.JsonError && - e.Message.StartsWith("Failed to deserialize AccountAddress due to: Could not parse AccountAddress")); + e.Message.StartsWith("Failed to deserialize AccountAddress due to: Could not parse AccountAddress", StringComparison.Ordinal)); } [Fact] From eb7ce2d42f480a1a82e368fc1666181a41c1aff1 Mon Sep 17 00:00:00 2001 From: schwartz-concordium <132270889+schwartz-concordium@users.noreply.github.com> Date: Fri, 26 Jan 2024 13:26:41 +0100 Subject: [PATCH 14/26] Update InteropBindingTests.cs --- tests/UnitTests/Interop/InteropBindingTests.cs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/tests/UnitTests/Interop/InteropBindingTests.cs b/tests/UnitTests/Interop/InteropBindingTests.cs index 1f96dfaa..5914ba47 100644 --- a/tests/UnitTests/Interop/InteropBindingTests.cs +++ b/tests/UnitTests/Interop/InteropBindingTests.cs @@ -104,8 +104,9 @@ public async Task GivenBadReceiveParam_WhenDisplayReceiveParam_ThenThrowExceptio // Assert action.Should().Throw() .Where(e => - e.Result == Result.JsonError && - e.Message.StartsWith("Failed to deserialize AccountAddress due to: Could not parse AccountAddress", StringComparison.Ordinal)); + e.Result == Result.JsonError + // && e.Message.StartsWith("Failed to deserialize AccountAddress due to: Could not parse AccountAddress") + ); } [Fact] From 8e2df142fe22258584df8277bd96e4fd4f00f995 Mon Sep 17 00:00:00 2001 From: schwartz-concordium <132270889+schwartz-concordium@users.noreply.github.com> Date: Fri, 26 Jan 2024 13:29:55 +0100 Subject: [PATCH 15/26] Update InteropBindingTests.cs --- tests/UnitTests/Interop/InteropBindingTests.cs | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/tests/UnitTests/Interop/InteropBindingTests.cs b/tests/UnitTests/Interop/InteropBindingTests.cs index 5914ba47..f8f1be00 100644 --- a/tests/UnitTests/Interop/InteropBindingTests.cs +++ b/tests/UnitTests/Interop/InteropBindingTests.cs @@ -104,9 +104,8 @@ public async Task GivenBadReceiveParam_WhenDisplayReceiveParam_ThenThrowExceptio // Assert action.Should().Throw() .Where(e => - e.Result == Result.JsonError - // && e.Message.StartsWith("Failed to deserialize AccountAddress due to: Could not parse AccountAddress") - ); + e.Result == Result.JsonError && + e.Message.StartsWith("Failed to deserialize AccountAddress due to: Could not parse AccountAddress", StringComparison.InvariantCulture)); } [Fact] From e34789e09c5cc22cccc664767ed9d8de0198f3a2 Mon Sep 17 00:00:00 2001 From: schwartz-concordium <132270889+schwartz-concordium@users.noreply.github.com> Date: Fri, 26 Jan 2024 13:34:26 +0100 Subject: [PATCH 16/26] try see output --- tests/UnitTests/Interop/InteropBindingTests.cs | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/tests/UnitTests/Interop/InteropBindingTests.cs b/tests/UnitTests/Interop/InteropBindingTests.cs index f8f1be00..19dced69 100644 --- a/tests/UnitTests/Interop/InteropBindingTests.cs +++ b/tests/UnitTests/Interop/InteropBindingTests.cs @@ -7,12 +7,17 @@ using FluentAssertions; using VerifyXunit; using Xunit; +using Xunit.Abstractions; namespace Concordium.Sdk.Tests.UnitTests.Interop; [UsesVerify] public class InteropBindingTests { + private readonly ITestOutputHelper _outputHelper; + + public InteropBindingTests(ITestOutputHelper outputHelper) => this._outputHelper = outputHelper; + [Fact] public async Task GivenSchemaVersion_WhenSchemaDisplay_ThenReturnSchema() { @@ -98,6 +103,15 @@ public async Task GivenBadReceiveParam_WhenDisplayReceiveParam_ThenThrowExceptio var contractIdentifier = new ContractIdentifier(contractName); var entryPoint = new EntryPoint(entrypoint); + try + { + var receiveContractParameter = InteropBinding.GetReceiveContractParameter(versionedModuleSchema, contractIdentifier, entryPoint, parameter); + } + catch (InteropBindingException e) + { + this._outputHelper.WriteLine($"{e.Result}"); + } + // Act var action = () => InteropBinding.GetReceiveContractParameter(versionedModuleSchema, contractIdentifier, entryPoint, parameter); From c1b18e25f22f29dcb17b0985b86c9ca7dddac630 Mon Sep 17 00:00:00 2001 From: schwartz-concordium <132270889+schwartz-concordium@users.noreply.github.com> Date: Fri, 26 Jan 2024 14:41:59 +0100 Subject: [PATCH 17/26] Update InteropBindingTests.cs --- tests/UnitTests/Interop/InteropBindingTests.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/UnitTests/Interop/InteropBindingTests.cs b/tests/UnitTests/Interop/InteropBindingTests.cs index 19dced69..50636ea1 100644 --- a/tests/UnitTests/Interop/InteropBindingTests.cs +++ b/tests/UnitTests/Interop/InteropBindingTests.cs @@ -109,7 +109,7 @@ public async Task GivenBadReceiveParam_WhenDisplayReceiveParam_ThenThrowExceptio } catch (InteropBindingException e) { - this._outputHelper.WriteLine($"{e.Result}"); + this._outputHelper.WriteLine($"This is some result: {e.Result}"); } // Act From ee7563cff6ea195237f9c26828479b235922e2b5 Mon Sep 17 00:00:00 2001 From: schwartz-concordium <132270889+schwartz-concordium@users.noreply.github.com> Date: Fri, 26 Jan 2024 15:09:01 +0100 Subject: [PATCH 18/26] change to map error --- src/Exceptions/InteropBindingException.cs | 10 +-- src/Interop/InteropBinding.cs | 22 +++--- src/Interop/{Result.cs => InteropError.cs} | 76 +++++++++++++++++-- .../UnitTests/Interop/InteropBindingTests.cs | 18 ++--- 4 files changed, 94 insertions(+), 32 deletions(-) rename src/Interop/{Result.cs => InteropError.cs} (54%) diff --git a/src/Exceptions/InteropBindingException.cs b/src/Exceptions/InteropBindingException.cs index 310d287b..9d4a7945 100644 --- a/src/Exceptions/InteropBindingException.cs +++ b/src/Exceptions/InteropBindingException.cs @@ -12,12 +12,12 @@ public sealed class InteropBindingException : Exception /// /// Type of error /// - public Result Result { get; } + public InteropError InteropError { get; } - internal static InteropBindingException Create(Result result, byte[]? message) => - message != null ? new InteropBindingException(result, Encoding.UTF8.GetString(message)) : Empty(result); + internal static InteropBindingException Create(InteropError interopError, byte[]? message) => + message != null ? new InteropBindingException(interopError, Encoding.UTF8.GetString(message)) : Empty(interopError); - private InteropBindingException(Result result, string message) : base(message) => this.Result = result; + private InteropBindingException(InteropError interopError, string message) : base(message) => this.InteropError = interopError; - private static InteropBindingException Empty(Result result) => new(result, EmptyErrorMessage); + private static InteropBindingException Empty(InteropError interopError) => new(interopError, EmptyErrorMessage); } diff --git a/src/Interop/InteropBinding.cs b/src/Interop/InteropBinding.cs index b0a3a810..1142e237 100644 --- a/src/Interop/InteropBinding.cs +++ b/src/Interop/InteropBinding.cs @@ -15,14 +15,14 @@ internal static class InteropBinding private const string DllName = "rust_bindings"; [DllImport(DllName, CallingConvention = CallingConvention.Cdecl, EntryPoint = "schema_display")] - private static extern Result SchemaDisplay( + private static extern byte SchemaDisplay( [MarshalAs(UnmanagedType.LPArray)] byte[] schema, int schema_size, FfiByteOption schema_version, [MarshalAs(UnmanagedType.FunctionPtr)] SetResultCallback callback); [DllImport(DllName, CallingConvention = CallingConvention.Cdecl, EntryPoint = "get_receive_contract_parameter")] - private static extern Result GetReceiveContractParameter( + private static extern byte GetReceiveContractParameter( [MarshalAs(UnmanagedType.LPArray)] byte[] schema, int schema_size, FfiByteOption schema_version, [MarshalAs(UnmanagedType.LPUTF8Str)] string contract_name, @@ -32,7 +32,7 @@ private static extern Result GetReceiveContractParameter( [MarshalAs(UnmanagedType.FunctionPtr)] SetResultCallback callback); [DllImport(DllName, CallingConvention = CallingConvention.Cdecl, EntryPoint = "get_event_contract")] - private static extern Result GetEventContract( + private static extern byte GetEventContract( [MarshalAs(UnmanagedType.LPArray)] byte[] schema, int schema_size, FfiByteOption schema_version, @@ -54,7 +54,7 @@ private static extern Result GetEventContract( internal static Utf8Json SchemaDisplay(VersionedModuleSchema schema) { var ffiOption = FfiByteOption.Create(schema.Version); - byte[]? result = null; + var result = Array.Empty(); var schemaDisplay = SchemaDisplay(schema.Schema, schema.Schema.Length, ffiOption, (ptr, size) => @@ -63,12 +63,12 @@ internal static Utf8Json SchemaDisplay(VersionedModuleSchema schema) Marshal.Copy(ptr, result, 0, size); }); - if (!schemaDisplay.IsError() && result != null) + if (!InteropErrorExtensions.TryMapError(schemaDisplay, out var error)) { return new Utf8Json(result); } - var interopException = InteropBindingException.Create(schemaDisplay, result); + var interopException = InteropBindingException.Create(error!.Value, result); throw interopException; } @@ -86,7 +86,7 @@ internal static Utf8Json GetReceiveContractParameter(VersionedModuleSchema schem { var ffiOption = FfiByteOption.Create(schema.Version); - byte[]? result = null; + var result = Array.Empty(); var receiveContractParameter = GetReceiveContractParameter(schema.Schema, schema.Schema.Length, ffiOption, @@ -97,12 +97,12 @@ internal static Utf8Json GetReceiveContractParameter(VersionedModuleSchema schem Marshal.Copy(ptr, result, 0, size); }); - if (!receiveContractParameter.IsError() && result != null) + if (!InteropErrorExtensions.TryMapError(receiveContractParameter, out var error)) { return new Utf8Json(result); } - var interopException = InteropBindingException.Create(receiveContractParameter, result); + var interopException = InteropBindingException.Create(error!.Value, result); throw interopException; } @@ -127,12 +127,12 @@ internal static Utf8Json GetEventContract(VersionedModuleSchema schema, Contract Marshal.Copy(ptr, result, 0, size); }); - if (!schemaDisplay.IsError() && result != null) + if (!InteropErrorExtensions.TryMapError(schemaDisplay, out var error)) { return new Utf8Json(result); } - var interopException = InteropBindingException.Create(schemaDisplay, result); + var interopException = InteropBindingException.Create(error!.Value, result); throw interopException; } diff --git a/src/Interop/Result.cs b/src/Interop/InteropError.cs similarity index 54% rename from src/Interop/Result.cs rename to src/Interop/InteropError.cs index f5f9d8a8..8567ec60 100644 --- a/src/Interop/Result.cs +++ b/src/Interop/InteropError.cs @@ -3,12 +3,8 @@ namespace Concordium.Sdk.Interop; /// /// Result type which on errors hold error type information. /// -public enum Result +public enum InteropError { - /// - /// No error - /// - NoError = 0, /// /// Represents errors occurring while deserializing to the schema JSON format. /// @@ -85,7 +81,73 @@ public enum Result VersionedSchemaErrorEventNotSupported = 18, } -internal static class ErrorExtensions +internal static class InteropErrorExtensions { - internal static bool IsError(this Result result) => result != Result.NoError; + internal static bool TryMapError(byte result, out InteropError? error) + { + error = null; + switch (result) + { + case 0: + break; + case 1: + error = InteropError.JsonError; + break; + case 2: + error = InteropError.SerdeJsonError; + break; + case 3: + error = InteropError.Utf8Error; + break; + case 4: + error = InteropError.VersionedSchemaErrorParseError; + break; + case 5: + error = InteropError.VersionedSchemaErrorMissingSchemaVersion; + break; + case 6: + error = InteropError.VersionedSchemaErrorInvalidSchemaVersion; + break; + case 7: + error = InteropError.VersionedSchemaErrorNoContractInModule; + break; + case 8: + error = InteropError.VersionedSchemaErrorNoReceiveInContract; + break; + case 9: + error = InteropError.VersionedSchemaErrorNoInitInContract; + break; + case 10: + error = InteropError.VersionedSchemaErrorNoParamsInReceive; + break; + case 11: + error = InteropError.VersionedSchemaErrorNoParamsInInit; + break; + case 12: + error = InteropError.VersionedSchemaErrorNoErrorInReceive; + break; + case 13: + error = InteropError.VersionedSchemaErrorNoErrorInInit; + break; + case 14: + error = InteropError.VersionedSchemaErrorErrorNotSupported; + break; + case 15: + error = InteropError.VersionedSchemaErrorNoReturnValueInReceive; + break; + case 16: + error = InteropError.VersionedSchemaErrorReturnValueNotSupported; + break; + case 17: + error = InteropError.VersionedSchemaErrorNoEventInContract; + break; + case 18: + error = InteropError.VersionedSchemaErrorEventNotSupported; + break; + default: + throw new ArgumentOutOfRangeException(nameof(result), result, null); + } + + return error != null; + } } diff --git a/tests/UnitTests/Interop/InteropBindingTests.cs b/tests/UnitTests/Interop/InteropBindingTests.cs index 50636ea1..44850463 100644 --- a/tests/UnitTests/Interop/InteropBindingTests.cs +++ b/tests/UnitTests/Interop/InteropBindingTests.cs @@ -64,7 +64,7 @@ public async Task GivenBadSchema_WhenSchemaDisplay_ThenThrowExceptionWithParseEr // Assert action.Should().Throw() .Where(e => - e.Result == Result.VersionedSchemaErrorParseError && + e.InteropError == InteropError.VersionedSchemaErrorParseError && e.Message.Equals("Parse error", StringComparison.Ordinal)); } @@ -109,7 +109,7 @@ public async Task GivenBadReceiveParam_WhenDisplayReceiveParam_ThenThrowExceptio } catch (InteropBindingException e) { - this._outputHelper.WriteLine($"This is some result: {e.Result}"); + this._outputHelper.WriteLine($"This is some result: {e.InteropError}"); } // Act @@ -118,7 +118,7 @@ public async Task GivenBadReceiveParam_WhenDisplayReceiveParam_ThenThrowExceptio // Assert action.Should().Throw() .Where(e => - e.Result == Result.JsonError && + e.InteropError == InteropError.JsonError && e.Message.StartsWith("Failed to deserialize AccountAddress due to: Could not parse AccountAddress", StringComparison.InvariantCulture)); } @@ -140,7 +140,7 @@ public async Task GivenBadContractIdentifier_WhenDisplayReceiveParam_ThenThrowEx // Assert action.Should().Throw() .Where(e => - e.Result == Result.VersionedSchemaErrorNoContractInModule && + e.InteropError == InteropError.VersionedSchemaErrorNoContractInModule && e.Message.Equals("Unable to find contract schema in module schema", StringComparison.Ordinal)); } @@ -162,7 +162,7 @@ public async Task GivenBadEntrypoint_WhenDisplayReceiveParam_ThenThrowException( // Assert action.Should().Throw() .Where(e => - e.Result == Result.VersionedSchemaErrorNoReceiveInContract && + e.InteropError == InteropError.VersionedSchemaErrorNoReceiveInContract && e.Message.Equals("Receive function schema not found in contract schema", StringComparison.Ordinal)); } @@ -184,7 +184,7 @@ public async Task GivenBadSchema_WhenDisplayReceiveParam_ThenThrowException() // Assert action.Should().Throw() .Where(e => - e.Result == Result.VersionedSchemaErrorMissingSchemaVersion && + e.InteropError == InteropError.VersionedSchemaErrorMissingSchemaVersion && e.Message.Equals("Missing Schema Version", StringComparison.Ordinal)); } @@ -225,7 +225,7 @@ public async Task GivenBadSchema_WhenDisplayEvent_ThenThrowException() // Assert action.Should().Throw() .Where(e => - e.Result == Result.VersionedSchemaErrorMissingSchemaVersion && + e.InteropError == InteropError.VersionedSchemaErrorMissingSchemaVersion && e.Message.Equals("Missing Schema Version", StringComparison.Ordinal)); } @@ -246,7 +246,7 @@ public async Task GivenBadContractIdentifier_WhenDisplayEvent_ThenThrowException // Assert action.Should().Throw() .Where(e => - e.Result == Result.VersionedSchemaErrorNoContractInModule && + e.InteropError == InteropError.VersionedSchemaErrorNoContractInModule && e.Message.Equals("Unable to find contract schema in module schema", StringComparison.Ordinal)); } @@ -267,7 +267,7 @@ public async Task GivenBadContractEvent_WhenDisplayEvent_ThenThrowException() // Assert action.Should().Throw() .Where(e => - e.Result == Result.JsonError && + e.InteropError == InteropError.JsonError && e.Message.StartsWith("Failed to deserialize AccountAddress due to: Could not parse")); } From 4941b8e5ee4fc689494b3e309e4820665429b29d Mon Sep 17 00:00:00 2001 From: schwartz-concordium <132270889+schwartz-concordium@users.noreply.github.com> Date: Fri, 26 Jan 2024 15:19:18 +0100 Subject: [PATCH 19/26] Revert "change to map error" This reverts commit ee7563cff6ea195237f9c26828479b235922e2b5. --- src/Exceptions/InteropBindingException.cs | 10 +-- src/Interop/InteropBinding.cs | 22 +++--- src/Interop/{InteropError.cs => Result.cs} | 76 ++----------------- .../UnitTests/Interop/InteropBindingTests.cs | 18 ++--- 4 files changed, 32 insertions(+), 94 deletions(-) rename src/Interop/{InteropError.cs => Result.cs} (54%) diff --git a/src/Exceptions/InteropBindingException.cs b/src/Exceptions/InteropBindingException.cs index 9d4a7945..310d287b 100644 --- a/src/Exceptions/InteropBindingException.cs +++ b/src/Exceptions/InteropBindingException.cs @@ -12,12 +12,12 @@ public sealed class InteropBindingException : Exception /// /// Type of error /// - public InteropError InteropError { get; } + public Result Result { get; } - internal static InteropBindingException Create(InteropError interopError, byte[]? message) => - message != null ? new InteropBindingException(interopError, Encoding.UTF8.GetString(message)) : Empty(interopError); + internal static InteropBindingException Create(Result result, byte[]? message) => + message != null ? new InteropBindingException(result, Encoding.UTF8.GetString(message)) : Empty(result); - private InteropBindingException(InteropError interopError, string message) : base(message) => this.InteropError = interopError; + private InteropBindingException(Result result, string message) : base(message) => this.Result = result; - private static InteropBindingException Empty(InteropError interopError) => new(interopError, EmptyErrorMessage); + private static InteropBindingException Empty(Result result) => new(result, EmptyErrorMessage); } diff --git a/src/Interop/InteropBinding.cs b/src/Interop/InteropBinding.cs index 1142e237..b0a3a810 100644 --- a/src/Interop/InteropBinding.cs +++ b/src/Interop/InteropBinding.cs @@ -15,14 +15,14 @@ internal static class InteropBinding private const string DllName = "rust_bindings"; [DllImport(DllName, CallingConvention = CallingConvention.Cdecl, EntryPoint = "schema_display")] - private static extern byte SchemaDisplay( + private static extern Result SchemaDisplay( [MarshalAs(UnmanagedType.LPArray)] byte[] schema, int schema_size, FfiByteOption schema_version, [MarshalAs(UnmanagedType.FunctionPtr)] SetResultCallback callback); [DllImport(DllName, CallingConvention = CallingConvention.Cdecl, EntryPoint = "get_receive_contract_parameter")] - private static extern byte GetReceiveContractParameter( + private static extern Result GetReceiveContractParameter( [MarshalAs(UnmanagedType.LPArray)] byte[] schema, int schema_size, FfiByteOption schema_version, [MarshalAs(UnmanagedType.LPUTF8Str)] string contract_name, @@ -32,7 +32,7 @@ private static extern byte GetReceiveContractParameter( [MarshalAs(UnmanagedType.FunctionPtr)] SetResultCallback callback); [DllImport(DllName, CallingConvention = CallingConvention.Cdecl, EntryPoint = "get_event_contract")] - private static extern byte GetEventContract( + private static extern Result GetEventContract( [MarshalAs(UnmanagedType.LPArray)] byte[] schema, int schema_size, FfiByteOption schema_version, @@ -54,7 +54,7 @@ private static extern byte GetEventContract( internal static Utf8Json SchemaDisplay(VersionedModuleSchema schema) { var ffiOption = FfiByteOption.Create(schema.Version); - var result = Array.Empty(); + byte[]? result = null; var schemaDisplay = SchemaDisplay(schema.Schema, schema.Schema.Length, ffiOption, (ptr, size) => @@ -63,12 +63,12 @@ internal static Utf8Json SchemaDisplay(VersionedModuleSchema schema) Marshal.Copy(ptr, result, 0, size); }); - if (!InteropErrorExtensions.TryMapError(schemaDisplay, out var error)) + if (!schemaDisplay.IsError() && result != null) { return new Utf8Json(result); } - var interopException = InteropBindingException.Create(error!.Value, result); + var interopException = InteropBindingException.Create(schemaDisplay, result); throw interopException; } @@ -86,7 +86,7 @@ internal static Utf8Json GetReceiveContractParameter(VersionedModuleSchema schem { var ffiOption = FfiByteOption.Create(schema.Version); - var result = Array.Empty(); + byte[]? result = null; var receiveContractParameter = GetReceiveContractParameter(schema.Schema, schema.Schema.Length, ffiOption, @@ -97,12 +97,12 @@ internal static Utf8Json GetReceiveContractParameter(VersionedModuleSchema schem Marshal.Copy(ptr, result, 0, size); }); - if (!InteropErrorExtensions.TryMapError(receiveContractParameter, out var error)) + if (!receiveContractParameter.IsError() && result != null) { return new Utf8Json(result); } - var interopException = InteropBindingException.Create(error!.Value, result); + var interopException = InteropBindingException.Create(receiveContractParameter, result); throw interopException; } @@ -127,12 +127,12 @@ internal static Utf8Json GetEventContract(VersionedModuleSchema schema, Contract Marshal.Copy(ptr, result, 0, size); }); - if (!InteropErrorExtensions.TryMapError(schemaDisplay, out var error)) + if (!schemaDisplay.IsError() && result != null) { return new Utf8Json(result); } - var interopException = InteropBindingException.Create(error!.Value, result); + var interopException = InteropBindingException.Create(schemaDisplay, result); throw interopException; } diff --git a/src/Interop/InteropError.cs b/src/Interop/Result.cs similarity index 54% rename from src/Interop/InteropError.cs rename to src/Interop/Result.cs index 8567ec60..f5f9d8a8 100644 --- a/src/Interop/InteropError.cs +++ b/src/Interop/Result.cs @@ -3,8 +3,12 @@ namespace Concordium.Sdk.Interop; /// /// Result type which on errors hold error type information. /// -public enum InteropError +public enum Result { + /// + /// No error + /// + NoError = 0, /// /// Represents errors occurring while deserializing to the schema JSON format. /// @@ -81,73 +85,7 @@ public enum InteropError VersionedSchemaErrorEventNotSupported = 18, } -internal static class InteropErrorExtensions +internal static class ErrorExtensions { - internal static bool TryMapError(byte result, out InteropError? error) - { - error = null; - switch (result) - { - case 0: - break; - case 1: - error = InteropError.JsonError; - break; - case 2: - error = InteropError.SerdeJsonError; - break; - case 3: - error = InteropError.Utf8Error; - break; - case 4: - error = InteropError.VersionedSchemaErrorParseError; - break; - case 5: - error = InteropError.VersionedSchemaErrorMissingSchemaVersion; - break; - case 6: - error = InteropError.VersionedSchemaErrorInvalidSchemaVersion; - break; - case 7: - error = InteropError.VersionedSchemaErrorNoContractInModule; - break; - case 8: - error = InteropError.VersionedSchemaErrorNoReceiveInContract; - break; - case 9: - error = InteropError.VersionedSchemaErrorNoInitInContract; - break; - case 10: - error = InteropError.VersionedSchemaErrorNoParamsInReceive; - break; - case 11: - error = InteropError.VersionedSchemaErrorNoParamsInInit; - break; - case 12: - error = InteropError.VersionedSchemaErrorNoErrorInReceive; - break; - case 13: - error = InteropError.VersionedSchemaErrorNoErrorInInit; - break; - case 14: - error = InteropError.VersionedSchemaErrorErrorNotSupported; - break; - case 15: - error = InteropError.VersionedSchemaErrorNoReturnValueInReceive; - break; - case 16: - error = InteropError.VersionedSchemaErrorReturnValueNotSupported; - break; - case 17: - error = InteropError.VersionedSchemaErrorNoEventInContract; - break; - case 18: - error = InteropError.VersionedSchemaErrorEventNotSupported; - break; - default: - throw new ArgumentOutOfRangeException(nameof(result), result, null); - } - - return error != null; - } + internal static bool IsError(this Result result) => result != Result.NoError; } diff --git a/tests/UnitTests/Interop/InteropBindingTests.cs b/tests/UnitTests/Interop/InteropBindingTests.cs index 44850463..50636ea1 100644 --- a/tests/UnitTests/Interop/InteropBindingTests.cs +++ b/tests/UnitTests/Interop/InteropBindingTests.cs @@ -64,7 +64,7 @@ public async Task GivenBadSchema_WhenSchemaDisplay_ThenThrowExceptionWithParseEr // Assert action.Should().Throw() .Where(e => - e.InteropError == InteropError.VersionedSchemaErrorParseError && + e.Result == Result.VersionedSchemaErrorParseError && e.Message.Equals("Parse error", StringComparison.Ordinal)); } @@ -109,7 +109,7 @@ public async Task GivenBadReceiveParam_WhenDisplayReceiveParam_ThenThrowExceptio } catch (InteropBindingException e) { - this._outputHelper.WriteLine($"This is some result: {e.InteropError}"); + this._outputHelper.WriteLine($"This is some result: {e.Result}"); } // Act @@ -118,7 +118,7 @@ public async Task GivenBadReceiveParam_WhenDisplayReceiveParam_ThenThrowExceptio // Assert action.Should().Throw() .Where(e => - e.InteropError == InteropError.JsonError && + e.Result == Result.JsonError && e.Message.StartsWith("Failed to deserialize AccountAddress due to: Could not parse AccountAddress", StringComparison.InvariantCulture)); } @@ -140,7 +140,7 @@ public async Task GivenBadContractIdentifier_WhenDisplayReceiveParam_ThenThrowEx // Assert action.Should().Throw() .Where(e => - e.InteropError == InteropError.VersionedSchemaErrorNoContractInModule && + e.Result == Result.VersionedSchemaErrorNoContractInModule && e.Message.Equals("Unable to find contract schema in module schema", StringComparison.Ordinal)); } @@ -162,7 +162,7 @@ public async Task GivenBadEntrypoint_WhenDisplayReceiveParam_ThenThrowException( // Assert action.Should().Throw() .Where(e => - e.InteropError == InteropError.VersionedSchemaErrorNoReceiveInContract && + e.Result == Result.VersionedSchemaErrorNoReceiveInContract && e.Message.Equals("Receive function schema not found in contract schema", StringComparison.Ordinal)); } @@ -184,7 +184,7 @@ public async Task GivenBadSchema_WhenDisplayReceiveParam_ThenThrowException() // Assert action.Should().Throw() .Where(e => - e.InteropError == InteropError.VersionedSchemaErrorMissingSchemaVersion && + e.Result == Result.VersionedSchemaErrorMissingSchemaVersion && e.Message.Equals("Missing Schema Version", StringComparison.Ordinal)); } @@ -225,7 +225,7 @@ public async Task GivenBadSchema_WhenDisplayEvent_ThenThrowException() // Assert action.Should().Throw() .Where(e => - e.InteropError == InteropError.VersionedSchemaErrorMissingSchemaVersion && + e.Result == Result.VersionedSchemaErrorMissingSchemaVersion && e.Message.Equals("Missing Schema Version", StringComparison.Ordinal)); } @@ -246,7 +246,7 @@ public async Task GivenBadContractIdentifier_WhenDisplayEvent_ThenThrowException // Assert action.Should().Throw() .Where(e => - e.InteropError == InteropError.VersionedSchemaErrorNoContractInModule && + e.Result == Result.VersionedSchemaErrorNoContractInModule && e.Message.Equals("Unable to find contract schema in module schema", StringComparison.Ordinal)); } @@ -267,7 +267,7 @@ public async Task GivenBadContractEvent_WhenDisplayEvent_ThenThrowException() // Assert action.Should().Throw() .Where(e => - e.InteropError == InteropError.JsonError && + e.Result == Result.JsonError && e.Message.StartsWith("Failed to deserialize AccountAddress due to: Could not parse")); } From d95bfc8467d5513b64377d4b6b57a58804876cbd Mon Sep 17 00:00:00 2001 From: schwartz-concordium <132270889+schwartz-concordium@users.noreply.github.com> Date: Fri, 26 Jan 2024 15:20:14 +0100 Subject: [PATCH 20/26] Update lib.rs --- rust-bindings/src/lib.rs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/rust-bindings/src/lib.rs b/rust-bindings/src/lib.rs index d788a683..45ee29f0 100644 --- a/rust-bindings/src/lib.rs +++ b/rust-bindings/src/lib.rs @@ -51,7 +51,7 @@ pub unsafe extern "C" fn schema_display( schema_size: i32, schema_version: FFIByteOption, callback: ResultCallback, -) -> u8 { +) -> u16 { let schema = std::slice::from_raw_parts(schema_ptr, schema_size as usize); assign_result(callback, || { schema_display_aux(schema, schema_version.into_option()) @@ -90,7 +90,7 @@ pub unsafe extern "C" fn get_receive_contract_parameter( value_ptr: *const u8, value_size: i32, callback: ResultCallback, -) -> u8 { +) -> u16 { assign_result(callback, || { let schema = std::slice::from_raw_parts(schema_ptr, schema_size as usize); let contract_name_str = get_str_from_pointer(contract_name)?; @@ -134,7 +134,7 @@ pub unsafe extern "C" fn get_event_contract( value_ptr: *const u8, value_size: i32, callback: ResultCallback, -) -> u8 { +) -> u16 { assign_result(callback, || { let schema = std::slice::from_raw_parts(schema_ptr, schema_size as usize); let contract_name_str = get_str_from_pointer(contract_name)?; @@ -160,7 +160,7 @@ pub unsafe extern "C" fn get_event_contract( /// # Returns /// /// A boolean, that indicates whether the computation was successful or not. -fn assign_result Result, FFIError>>(callback: ResultCallback, f: F) -> u8 { +fn assign_result Result, FFIError>>(callback: ResultCallback, f: F) -> u16 { match f() { Ok(output) => { let out_lenght = output.len() as i32; @@ -209,7 +209,7 @@ pub enum FFIError { } impl FFIError { - fn to_int(&self) -> u8 { + fn to_int(&self) -> u16 { match self { FFIError::JsonError(_) => 1, FFIError::SerdeJsonError => 2, From 45a245b3cc058c2dd6e589f3fbc27af586743a94 Mon Sep 17 00:00:00 2001 From: schwartz-concordium <132270889+schwartz-concordium@users.noreply.github.com> Date: Fri, 26 Jan 2024 15:20:34 +0100 Subject: [PATCH 21/26] Update InteropBindingTests.cs --- tests/UnitTests/Interop/InteropBindingTests.cs | 9 --------- 1 file changed, 9 deletions(-) diff --git a/tests/UnitTests/Interop/InteropBindingTests.cs b/tests/UnitTests/Interop/InteropBindingTests.cs index 50636ea1..bd646267 100644 --- a/tests/UnitTests/Interop/InteropBindingTests.cs +++ b/tests/UnitTests/Interop/InteropBindingTests.cs @@ -103,15 +103,6 @@ public async Task GivenBadReceiveParam_WhenDisplayReceiveParam_ThenThrowExceptio var contractIdentifier = new ContractIdentifier(contractName); var entryPoint = new EntryPoint(entrypoint); - try - { - var receiveContractParameter = InteropBinding.GetReceiveContractParameter(versionedModuleSchema, contractIdentifier, entryPoint, parameter); - } - catch (InteropBindingException e) - { - this._outputHelper.WriteLine($"This is some result: {e.Result}"); - } - // Act var action = () => InteropBinding.GetReceiveContractParameter(versionedModuleSchema, contractIdentifier, entryPoint, parameter); From bbec4685678b4905d71888118eeab9e80ce056dc Mon Sep 17 00:00:00 2001 From: schwartz-concordium <132270889+schwartz-concordium@users.noreply.github.com> Date: Fri, 26 Jan 2024 15:21:42 +0100 Subject: [PATCH 22/26] Update InteropBindingTests.cs --- tests/UnitTests/Interop/InteropBindingTests.cs | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/UnitTests/Interop/InteropBindingTests.cs b/tests/UnitTests/Interop/InteropBindingTests.cs index bd646267..291fd9cf 100644 --- a/tests/UnitTests/Interop/InteropBindingTests.cs +++ b/tests/UnitTests/Interop/InteropBindingTests.cs @@ -58,7 +58,6 @@ public async Task GivenBadSchema_WhenSchemaDisplay_ThenThrowExceptionWithParseEr var versionedModuleSchema = new VersionedModuleSchema(schema[..^3], ModuleSchemaVersion.V1); // Bad schema // Act - var action = () => InteropBinding.SchemaDisplay(versionedModuleSchema); // Assert From 339b12d7276648015e58783332124b3fac906f52 Mon Sep 17 00:00:00 2001 From: schwartz-concordium <132270889+schwartz-concordium@users.noreply.github.com> Date: Fri, 26 Jan 2024 15:25:55 +0100 Subject: [PATCH 23/26] Update InteropBindingTests.cs --- tests/UnitTests/Interop/InteropBindingTests.cs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/UnitTests/Interop/InteropBindingTests.cs b/tests/UnitTests/Interop/InteropBindingTests.cs index 291fd9cf..d9a2a2b3 100644 --- a/tests/UnitTests/Interop/InteropBindingTests.cs +++ b/tests/UnitTests/Interop/InteropBindingTests.cs @@ -210,7 +210,7 @@ public async Task GivenBadSchema_WhenDisplayEvent_ThenThrowException() var contractEvent = new ContractEvent(Convert.FromHexString(value)); // Act - var action = () => InteropBinding.GetEventContract(versionedModuleSchema, contractIdentifier, contractEvent); + var action = () => InteropBinding.GetEventContract(versionedModuleSchema, contractIdentifier, contractEvent); // Assert action.Should().Throw() @@ -231,7 +231,7 @@ public async Task GivenBadContractIdentifier_WhenDisplayEvent_ThenThrowException var contractEvent = new ContractEvent(Convert.FromHexString(value)); // Act - var action = () => InteropBinding.GetEventContract(versionedModuleSchema, contractIdentifier, contractEvent); + var action = () => InteropBinding.GetEventContract(versionedModuleSchema, contractIdentifier, contractEvent); // Assert action.Should().Throw() @@ -252,7 +252,7 @@ public async Task GivenBadContractEvent_WhenDisplayEvent_ThenThrowException() var contractEvent = new ContractEvent(Convert.FromHexString(value)[..^3]); // Bad contract event // Act - var action = () => InteropBinding.GetEventContract(versionedModuleSchema, contractIdentifier, contractEvent); + var action = () => InteropBinding.GetEventContract(versionedModuleSchema, contractIdentifier, contractEvent); // Assert action.Should().Throw() From 5d2bb624757f98d577003c846c7b01d48b6ea288 Mon Sep 17 00:00:00 2001 From: schwartz-concordium <132270889+schwartz-concordium@users.noreply.github.com> Date: Fri, 26 Jan 2024 15:46:15 +0100 Subject: [PATCH 24/26] Update InteropBindingTests.cs --- tests/UnitTests/Interop/InteropBindingTests.cs | 5 ----- 1 file changed, 5 deletions(-) diff --git a/tests/UnitTests/Interop/InteropBindingTests.cs b/tests/UnitTests/Interop/InteropBindingTests.cs index d9a2a2b3..d96a3d17 100644 --- a/tests/UnitTests/Interop/InteropBindingTests.cs +++ b/tests/UnitTests/Interop/InteropBindingTests.cs @@ -7,17 +7,12 @@ using FluentAssertions; using VerifyXunit; using Xunit; -using Xunit.Abstractions; namespace Concordium.Sdk.Tests.UnitTests.Interop; [UsesVerify] public class InteropBindingTests { - private readonly ITestOutputHelper _outputHelper; - - public InteropBindingTests(ITestOutputHelper outputHelper) => this._outputHelper = outputHelper; - [Fact] public async Task GivenSchemaVersion_WhenSchemaDisplay_ThenReturnSchema() { From 2e5125fde31e78c9eaf8a18564f6a4a7682e6a4f Mon Sep 17 00:00:00 2001 From: schwartz-concordium <132270889+schwartz-concordium@users.noreply.github.com> Date: Tue, 30 Jan 2024 09:54:09 +0100 Subject: [PATCH 25/26] update from comments --- rust-bindings/src/lib.rs | 2 ++ src/Exceptions/InteropBindingException.cs | 23 ------------- src/Exceptions/SchemaJsonException.cs | 23 +++++++++++++ src/Interop/InteropBinding.cs | 12 +++---- .../{Result.cs => SchemaJsonResult.cs} | 4 +-- src/Types/ContractEvent.cs | 2 +- src/Types/ContractInitializedEvent.cs | 2 +- src/Types/ContractTraceElement.cs | 8 ++--- src/Types/RejectReason.cs | 2 +- src/Types/VersionedModuleSchema.cs | 2 +- .../UnitTests/Interop/InteropBindingTests.cs | 32 +++++++++---------- 11 files changed, 57 insertions(+), 55 deletions(-) delete mode 100644 src/Exceptions/InteropBindingException.cs create mode 100644 src/Exceptions/SchemaJsonException.cs rename src/Interop/{Result.cs => SchemaJsonResult.cs} (95%) diff --git a/rust-bindings/src/lib.rs b/rust-bindings/src/lib.rs index 45ee29f0..016c0ace 100644 --- a/rust-bindings/src/lib.rs +++ b/rust-bindings/src/lib.rs @@ -209,6 +209,8 @@ pub enum FFIError { } impl FFIError { + /// The enumeration starts a 1 since return value 0 indicating a successfull + /// FFI call. fn to_int(&self) -> u16 { match self { FFIError::JsonError(_) => 1, diff --git a/src/Exceptions/InteropBindingException.cs b/src/Exceptions/InteropBindingException.cs deleted file mode 100644 index 310d287b..00000000 --- a/src/Exceptions/InteropBindingException.cs +++ /dev/null @@ -1,23 +0,0 @@ -using System.Text; -using Concordium.Sdk.Interop; - -namespace Concordium.Sdk.Exceptions; - -/// -/// Thrown when a interop call failed with possible error as message. -/// -public sealed class InteropBindingException : Exception -{ - private const string EmptyErrorMessage = "Empty error message returned"; - /// - /// Type of error - /// - public Result Result { get; } - - internal static InteropBindingException Create(Result result, byte[]? message) => - message != null ? new InteropBindingException(result, Encoding.UTF8.GetString(message)) : Empty(result); - - private InteropBindingException(Result result, string message) : base(message) => this.Result = result; - - private static InteropBindingException Empty(Result result) => new(result, EmptyErrorMessage); -} diff --git a/src/Exceptions/SchemaJsonException.cs b/src/Exceptions/SchemaJsonException.cs new file mode 100644 index 00000000..5c51cf71 --- /dev/null +++ b/src/Exceptions/SchemaJsonException.cs @@ -0,0 +1,23 @@ +using System.Text; +using Concordium.Sdk.Interop; + +namespace Concordium.Sdk.Exceptions; + +/// +/// Thrown when a interop call failed with possible error as message. +/// +public sealed class SchemaJsonException : Exception +{ + private const string EmptyErrorMessage = "Empty error message returned"; + /// + /// Type of error + /// + public SchemaJsonResult SchemaJsonResult { get; } + + internal static SchemaJsonException Create(SchemaJsonResult schemaJsonResult, byte[]? message) => + message != null ? new SchemaJsonException(schemaJsonResult, Encoding.UTF8.GetString(message)) : Empty(schemaJsonResult); + + private SchemaJsonException(SchemaJsonResult schemaJsonResult, string message) : base(message) => this.SchemaJsonResult = schemaJsonResult; + + private static SchemaJsonException Empty(SchemaJsonResult schemaJsonResult) => new(schemaJsonResult, EmptyErrorMessage); +} diff --git a/src/Interop/InteropBinding.cs b/src/Interop/InteropBinding.cs index b0a3a810..54f02d39 100644 --- a/src/Interop/InteropBinding.cs +++ b/src/Interop/InteropBinding.cs @@ -15,14 +15,14 @@ internal static class InteropBinding private const string DllName = "rust_bindings"; [DllImport(DllName, CallingConvention = CallingConvention.Cdecl, EntryPoint = "schema_display")] - private static extern Result SchemaDisplay( + private static extern SchemaJsonResult SchemaDisplay( [MarshalAs(UnmanagedType.LPArray)] byte[] schema, int schema_size, FfiByteOption schema_version, [MarshalAs(UnmanagedType.FunctionPtr)] SetResultCallback callback); [DllImport(DllName, CallingConvention = CallingConvention.Cdecl, EntryPoint = "get_receive_contract_parameter")] - private static extern Result GetReceiveContractParameter( + private static extern SchemaJsonResult GetReceiveContractParameter( [MarshalAs(UnmanagedType.LPArray)] byte[] schema, int schema_size, FfiByteOption schema_version, [MarshalAs(UnmanagedType.LPUTF8Str)] string contract_name, @@ -32,7 +32,7 @@ private static extern Result GetReceiveContractParameter( [MarshalAs(UnmanagedType.FunctionPtr)] SetResultCallback callback); [DllImport(DllName, CallingConvention = CallingConvention.Cdecl, EntryPoint = "get_event_contract")] - private static extern Result GetEventContract( + private static extern SchemaJsonResult GetEventContract( [MarshalAs(UnmanagedType.LPArray)] byte[] schema, int schema_size, FfiByteOption schema_version, @@ -68,7 +68,7 @@ internal static Utf8Json SchemaDisplay(VersionedModuleSchema schema) return new Utf8Json(result); } - var interopException = InteropBindingException.Create(schemaDisplay, result); + var interopException = SchemaJsonException.Create(schemaDisplay, result); throw interopException; } @@ -102,7 +102,7 @@ internal static Utf8Json GetReceiveContractParameter(VersionedModuleSchema schem return new Utf8Json(result); } - var interopException = InteropBindingException.Create(receiveContractParameter, result); + var interopException = SchemaJsonException.Create(receiveContractParameter, result); throw interopException; } @@ -132,7 +132,7 @@ internal static Utf8Json GetEventContract(VersionedModuleSchema schema, Contract return new Utf8Json(result); } - var interopException = InteropBindingException.Create(schemaDisplay, result); + var interopException = SchemaJsonException.Create(schemaDisplay, result); throw interopException; } diff --git a/src/Interop/Result.cs b/src/Interop/SchemaJsonResult.cs similarity index 95% rename from src/Interop/Result.cs rename to src/Interop/SchemaJsonResult.cs index f5f9d8a8..1a399b6d 100644 --- a/src/Interop/Result.cs +++ b/src/Interop/SchemaJsonResult.cs @@ -3,7 +3,7 @@ namespace Concordium.Sdk.Interop; /// /// Result type which on errors hold error type information. /// -public enum Result +public enum SchemaJsonResult { /// /// No error @@ -87,5 +87,5 @@ public enum Result internal static class ErrorExtensions { - internal static bool IsError(this Result result) => result != Result.NoError; + internal static bool IsError(this SchemaJsonResult schemaJsonResult) => schemaJsonResult != SchemaJsonResult.NoError; } diff --git a/src/Types/ContractEvent.cs b/src/Types/ContractEvent.cs index d75002c2..dfdac385 100644 --- a/src/Types/ContractEvent.cs +++ b/src/Types/ContractEvent.cs @@ -19,7 +19,7 @@ public sealed record ContractEvent(byte[] Bytes) /// Module schema in hexadecimal. /// Contract name. /// deserialized as json uft8 encoded. - /// Thrown when event wasn't able to be deserialized from schema. + /// Thrown when event wasn't able to be deserialized from schema. public Utf8Json GetDeserializeEvent( VersionedModuleSchema schema, ContractIdentifier contractName diff --git a/src/Types/ContractInitializedEvent.cs b/src/Types/ContractInitializedEvent.cs index c9531c49..8bce0a6b 100644 --- a/src/Types/ContractInitializedEvent.cs +++ b/src/Types/ContractInitializedEvent.cs @@ -45,7 +45,7 @@ internal static ContractInitializedEvent From(Grpc.V2.ContractInitializedEvent i /// /// Module schema in hexadecimal. /// List of deserialized json uft8 encoded events. Possible null if this was returned from deserialization. - /// Thrown if an event wasn't able to be deserialized from schema. + /// Thrown if an event wasn't able to be deserialized from schema. public IList GetDeserializedEvents(VersionedModuleSchema schema) { var deserialized = new List(this.Events.Count); diff --git a/src/Types/ContractTraceElement.cs b/src/Types/ContractTraceElement.cs index f99df036..730d2c5d 100644 --- a/src/Types/ContractTraceElement.cs +++ b/src/Types/ContractTraceElement.cs @@ -79,7 +79,7 @@ public sealed record Updated( /// /// Versioned module schema. /// deserialized as json uft8 encoded. - /// Thrown when message wasn't able to be deserialized form schema. + /// Thrown when message wasn't able to be deserialized form schema. public Utf8Json GetDeserializeMessage(VersionedModuleSchema schema) => GetDeserializeMessage(schema, this.ReceiveName.GetContractName(), this.ReceiveName.GetEntrypoint(), this.Message); @@ -91,7 +91,7 @@ public Utf8Json GetDeserializeMessage(VersionedModuleSchema schema) => /// Entrypoint on contract. /// Message to entrypoint. /// deserialized as json uft8 encoded. - /// Thrown when message wasn't able to be deserialized from schema. + /// Thrown when message wasn't able to be deserialized from schema. public static Utf8Json GetDeserializeMessage( VersionedModuleSchema schema, ContractIdentifier contractIdentifier, @@ -105,7 +105,7 @@ Parameter message /// /// Module schema. /// List of deserialized json uft8 encoded events. Possible null if this was returned from deserialization. - /// Thrown if an event wasn't able to be deserialized from schema. + /// Thrown if an event wasn't able to be deserialized from schema. public IList GetDeserializedEvents(VersionedModuleSchema schema) { var deserialized = new List(this.Events.Count); @@ -141,7 +141,7 @@ public sealed record Interrupted(ContractAddress Address, IList E /// Module schema. /// Contract name. /// List of deserialized json uft8 encoded events. Possible null if this was returned from deserialization. - /// Thrown if an event wasn't able to be deserialized from schema. + /// Thrown if an event wasn't able to be deserialized from schema. public IList GetDeserializedEvents(VersionedModuleSchema schema, ContractIdentifier contractName) { var deserialized = new List(this.Events.Count); diff --git a/src/Types/RejectReason.cs b/src/Types/RejectReason.cs index f709dbd8..837bc7d2 100644 --- a/src/Types/RejectReason.cs +++ b/src/Types/RejectReason.cs @@ -194,7 +194,7 @@ public sealed record RejectedReceive(int RejectReason, ContractAddress ContractA /// /// Versioned module schema. /// deserialized as json uft8 encoded. - /// Thrown when message wasn't able to be deserialized using the schema. + /// Thrown when message wasn't able to be deserialized using the schema. public Utf8Json GetDeserializeMessage(VersionedModuleSchema schema) => Updated.GetDeserializeMessage(schema, this.ReceiveName.GetContractName(), this.ReceiveName.GetEntrypoint(), this.Parameter); } diff --git a/src/Types/VersionedModuleSchema.cs b/src/Types/VersionedModuleSchema.cs index 40a4d18b..767cd8cf 100644 --- a/src/Types/VersionedModuleSchema.cs +++ b/src/Types/VersionedModuleSchema.cs @@ -21,6 +21,6 @@ public sealed record VersionedModuleSchema(byte[] Schema, ModuleSchemaVersion Ve /// Deserialize schema. /// /// Schema as json uft8 encoded. - /// Thrown when schema wasn't able to be deserialized. + /// Thrown when schema wasn't able to be deserialized. public Utf8Json GetDeserializedSchema() => InteropBinding.SchemaDisplay(this); }; diff --git a/tests/UnitTests/Interop/InteropBindingTests.cs b/tests/UnitTests/Interop/InteropBindingTests.cs index d96a3d17..492b719e 100644 --- a/tests/UnitTests/Interop/InteropBindingTests.cs +++ b/tests/UnitTests/Interop/InteropBindingTests.cs @@ -56,9 +56,9 @@ public async Task GivenBadSchema_WhenSchemaDisplay_ThenThrowExceptionWithParseEr var action = () => InteropBinding.SchemaDisplay(versionedModuleSchema); // Assert - action.Should().Throw() + action.Should().Throw() .Where(e => - e.Result == Result.VersionedSchemaErrorParseError && + e.SchemaJsonResult == SchemaJsonResult.VersionedSchemaErrorParseError && e.Message.Equals("Parse error", StringComparison.Ordinal)); } @@ -101,9 +101,9 @@ public async Task GivenBadReceiveParam_WhenDisplayReceiveParam_ThenThrowExceptio var action = () => InteropBinding.GetReceiveContractParameter(versionedModuleSchema, contractIdentifier, entryPoint, parameter); // Assert - action.Should().Throw() + action.Should().Throw() .Where(e => - e.Result == Result.JsonError && + e.SchemaJsonResult == SchemaJsonResult.JsonError && e.Message.StartsWith("Failed to deserialize AccountAddress due to: Could not parse AccountAddress", StringComparison.InvariantCulture)); } @@ -123,9 +123,9 @@ public async Task GivenBadContractIdentifier_WhenDisplayReceiveParam_ThenThrowEx var action = () => InteropBinding.GetReceiveContractParameter(versionedModuleSchema, contractIdentifier, entryPoint, parameter); // Assert - action.Should().Throw() + action.Should().Throw() .Where(e => - e.Result == Result.VersionedSchemaErrorNoContractInModule && + e.SchemaJsonResult == SchemaJsonResult.VersionedSchemaErrorNoContractInModule && e.Message.Equals("Unable to find contract schema in module schema", StringComparison.Ordinal)); } @@ -145,9 +145,9 @@ public async Task GivenBadEntrypoint_WhenDisplayReceiveParam_ThenThrowException( var action = () => InteropBinding.GetReceiveContractParameter(versionedModuleSchema, contractIdentifier, entryPoint, parameter); // Assert - action.Should().Throw() + action.Should().Throw() .Where(e => - e.Result == Result.VersionedSchemaErrorNoReceiveInContract && + e.SchemaJsonResult == SchemaJsonResult.VersionedSchemaErrorNoReceiveInContract && e.Message.Equals("Receive function schema not found in contract schema", StringComparison.Ordinal)); } @@ -167,9 +167,9 @@ public async Task GivenBadSchema_WhenDisplayReceiveParam_ThenThrowException() var action = () => InteropBinding.GetReceiveContractParameter(versionedModuleSchema, contractIdentifier, entryPoint, parameter); // Assert - action.Should().Throw() + action.Should().Throw() .Where(e => - e.Result == Result.VersionedSchemaErrorMissingSchemaVersion && + e.SchemaJsonResult == SchemaJsonResult.VersionedSchemaErrorMissingSchemaVersion && e.Message.Equals("Missing Schema Version", StringComparison.Ordinal)); } @@ -208,9 +208,9 @@ public async Task GivenBadSchema_WhenDisplayEvent_ThenThrowException() var action = () => InteropBinding.GetEventContract(versionedModuleSchema, contractIdentifier, contractEvent); // Assert - action.Should().Throw() + action.Should().Throw() .Where(e => - e.Result == Result.VersionedSchemaErrorMissingSchemaVersion && + e.SchemaJsonResult == SchemaJsonResult.VersionedSchemaErrorMissingSchemaVersion && e.Message.Equals("Missing Schema Version", StringComparison.Ordinal)); } @@ -229,9 +229,9 @@ public async Task GivenBadContractIdentifier_WhenDisplayEvent_ThenThrowException var action = () => InteropBinding.GetEventContract(versionedModuleSchema, contractIdentifier, contractEvent); // Assert - action.Should().Throw() + action.Should().Throw() .Where(e => - e.Result == Result.VersionedSchemaErrorNoContractInModule && + e.SchemaJsonResult == SchemaJsonResult.VersionedSchemaErrorNoContractInModule && e.Message.Equals("Unable to find contract schema in module schema", StringComparison.Ordinal)); } @@ -250,9 +250,9 @@ public async Task GivenBadContractEvent_WhenDisplayEvent_ThenThrowException() var action = () => InteropBinding.GetEventContract(versionedModuleSchema, contractIdentifier, contractEvent); // Assert - action.Should().Throw() + action.Should().Throw() .Where(e => - e.Result == Result.JsonError && + e.SchemaJsonResult == SchemaJsonResult.JsonError && e.Message.StartsWith("Failed to deserialize AccountAddress due to: Could not parse")); } From b65b160e3c3b17962bd1ba445e68603feac7e53c Mon Sep 17 00:00:00 2001 From: schwartz-concordium <132270889+schwartz-concordium@users.noreply.github.com> Date: Tue, 30 Jan 2024 10:01:19 +0100 Subject: [PATCH 26/26] bump release and update changelog --- CHANGELOG.md | 4 ++++ src/Concordium.Sdk.csproj | 2 +- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 43069d77..46a7f63a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ ## Unreleased changes +## 4.3.1 +- Added + - Enhanced error handling for FFI calls and exposing detailed exceptions to the client, providing information about potential errors. + ## 4.3.0 - Bugfix - Switched the GitHub runners from using 'ubuntu-latest' to 'ubuntu-20.04' to ensure compatibility with the default .NET 6 Docker image for the SDK. diff --git a/src/Concordium.Sdk.csproj b/src/Concordium.Sdk.csproj index 7076476c..f975b568 100644 --- a/src/Concordium.Sdk.csproj +++ b/src/Concordium.Sdk.csproj @@ -15,7 +15,7 @@ concordium;concordium-net-sdk;blockchain;sdk; Concordium ConcordiumNetSdk - 4.3.0 + 4.3.1 icon.png README.md MPL-2.0