From 69c27567fbd725c6add2af657c9f46e6b87b799b Mon Sep 17 00:00:00 2001 From: Graydon Hoare Date: Sat, 13 May 2023 22:37:01 -0700 Subject: [PATCH] Adapt to error reform in XDR and env --- Cargo.lock | 36 ++------- Cargo.toml | 8 +- soroban-ledger-snapshot/Cargo.toml | 3 +- soroban-ledger-snapshot/src/lib.rs | 9 ++- soroban-sdk-macros/src/derive_enum.rs | 16 ++-- soroban-sdk-macros/src/derive_enum_int.rs | 10 +-- .../src/derive_error_enum_int.rs | 44 +++++------ soroban-sdk-macros/src/derive_struct.rs | 16 ++-- soroban-sdk-macros/src/derive_struct_tuple.rs | 16 ++-- soroban-sdk-macros/src/map_type.rs | 2 +- soroban-sdk-macros/src/syn_ext.rs | 6 +- soroban-sdk/Cargo.toml | 4 +- soroban-sdk/src/address.rs | 4 +- soroban-sdk/src/bytes.rs | 8 +- soroban-sdk/src/env.rs | 79 +++++-------------- soroban-sdk/src/events.rs | 4 +- soroban-sdk/src/lib.rs | 4 +- soroban-sdk/src/logging.rs | 64 +++++++-------- soroban-sdk/src/map.rs | 12 +-- soroban-sdk/src/num.rs | 4 +- soroban-sdk/src/string.rs | 4 +- soroban-sdk/src/symbol.rs | 6 +- soroban-sdk/src/tests/contract_assert.rs | 14 +--- soroban-sdk/src/tests/contract_invoke.rs | 13 +-- .../src/tests/contractfile_with_sha256.rs | 2 +- .../src/tests/contractimport_with_sha256.rs | 2 +- soroban-sdk/src/testutils.rs | 4 +- soroban-sdk/src/vec.rs | 10 +-- soroban-spec/src/gen/json/types.rs | 4 +- soroban-spec/src/gen/rust/types.rs | 2 +- tests/auth/src/lib.rs | 8 +- tests/errors/src/lib.rs | 11 +-- tests/logging/src/lib.rs | 31 ++++---- 33 files changed, 195 insertions(+), 265 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index cec3332e7..d8a1c7e35 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -302,12 +302,6 @@ version = "1.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9ea835d29036a4087793836fa931b08837ad5e957da9e23886b29586fb9b6650" -[[package]] -name = "dyn-fmt" -version = "0.3.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c2a0836c9bd73a9d3ca55b0effc5b1eedf96dd13ef994389bcac6d4d33c46188" - [[package]] name = "ed25519" version = "1.5.3" @@ -782,7 +776,7 @@ checksum = "74233d3b3b2f6d4b006dc19dee745e73e2a6bfb6f93607cd3b02bd5b00797d7c" [[package]] name = "soroban-env-common" version = "0.0.16" -source = "git+https://github.com/stellar/rs-soroban-env?rev=0c0cae6fa22b751e7fd95d7ce7556ac6d7b7010e#0c0cae6fa22b751e7fd95d7ce7556ac6d7b7010e" +source = "git+https://github.com/stellar/rs-soroban-env?rev=3762edfe52aafc6c0935619211d76d2d05fe87e6#3762edfe52aafc6c0935619211d76d2d05fe87e6" dependencies = [ "arbitrary", "crate-git-revision", @@ -797,7 +791,7 @@ dependencies = [ [[package]] name = "soroban-env-guest" version = "0.0.16" -source = "git+https://github.com/stellar/rs-soroban-env?rev=0c0cae6fa22b751e7fd95d7ce7556ac6d7b7010e#0c0cae6fa22b751e7fd95d7ce7556ac6d7b7010e" +source = "git+https://github.com/stellar/rs-soroban-env?rev=3762edfe52aafc6c0935619211d76d2d05fe87e6#3762edfe52aafc6c0935619211d76d2d05fe87e6" dependencies = [ "soroban-env-common", "static_assertions", @@ -806,11 +800,10 @@ dependencies = [ [[package]] name = "soroban-env-host" version = "0.0.16" -source = "git+https://github.com/stellar/rs-soroban-env?rev=0c0cae6fa22b751e7fd95d7ce7556ac6d7b7010e#0c0cae6fa22b751e7fd95d7ce7556ac6d7b7010e" +source = "git+https://github.com/stellar/rs-soroban-env?rev=3762edfe52aafc6c0935619211d76d2d05fe87e6#3762edfe52aafc6c0935619211d76d2d05fe87e6" dependencies = [ "backtrace", "curve25519-dalek", - "dyn-fmt", "ed25519-dalek", "hex", "log", @@ -822,13 +815,12 @@ dependencies = [ "soroban-native-sdk-macros", "soroban-wasmi", "static_assertions", - "tinyvec", ] [[package]] name = "soroban-env-macros" version = "0.0.16" -source = "git+https://github.com/stellar/rs-soroban-env?rev=0c0cae6fa22b751e7fd95d7ce7556ac6d7b7010e#0c0cae6fa22b751e7fd95d7ce7556ac6d7b7010e" +source = "git+https://github.com/stellar/rs-soroban-env?rev=3762edfe52aafc6c0935619211d76d2d05fe87e6#3762edfe52aafc6c0935619211d76d2d05fe87e6" dependencies = [ "itertools", "proc-macro2", @@ -847,6 +839,7 @@ dependencies = [ "pretty_assertions", "serde", "serde_json", + "soroban-env-common", "soroban-env-host", "thiserror", ] @@ -854,7 +847,7 @@ dependencies = [ [[package]] name = "soroban-native-sdk-macros" version = "0.0.16" -source = "git+https://github.com/stellar/rs-soroban-env?rev=0c0cae6fa22b751e7fd95d7ce7556ac6d7b7010e#0c0cae6fa22b751e7fd95d7ce7556ac6d7b7010e" +source = "git+https://github.com/stellar/rs-soroban-env?rev=3762edfe52aafc6c0935619211d76d2d05fe87e6#3762edfe52aafc6c0935619211d76d2d05fe87e6" dependencies = [ "itertools", "proc-macro2", @@ -968,7 +961,7 @@ dependencies = [ [[package]] name = "stellar-xdr" version = "0.0.16" -source = "git+https://github.com/stellar/rs-stellar-xdr?rev=53e1a9cf2335aff29305c72deb6f075e78915dad#53e1a9cf2335aff29305c72deb6f075e78915dad" +source = "git+https://github.com/stellar/rs-stellar-xdr?rev=b283ec0bcb791610013107b9eab7d7ff07a65db1#b283ec0bcb791610013107b9eab7d7ff07a65db1" dependencies = [ "arbitrary", "base64 0.13.1", @@ -1166,21 +1159,6 @@ dependencies = [ "time-core", ] -[[package]] -name = "tinyvec" -version = "1.6.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "87cc5ceb3875bb20c2890005a4e226a4651264a5c75edb2421b52861a0a0cb50" -dependencies = [ - "tinyvec_macros", -] - -[[package]] -name = "tinyvec_macros" -version = "0.1.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1f3ccbac311fea05f86f61904b462b55fb3df8837a366dfc601a0161d0532f20" - [[package]] name = "typenum" version = "1.16.0" diff --git a/Cargo.toml b/Cargo.toml index 08c96a649..a5a350b54 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -36,17 +36,17 @@ soroban-token-sdk = { version = "0.8.4", path = "soroban-token-sdk" } [workspace.dependencies.soroban-env-common] version = "0.0.16" git = "https://github.com/stellar/rs-soroban-env" -rev = "0c0cae6fa22b751e7fd95d7ce7556ac6d7b7010e" +rev = "3762edfe52aafc6c0935619211d76d2d05fe87e6" [workspace.dependencies.soroban-env-guest] version = "0.0.16" git = "https://github.com/stellar/rs-soroban-env" -rev = "0c0cae6fa22b751e7fd95d7ce7556ac6d7b7010e" +rev = "3762edfe52aafc6c0935619211d76d2d05fe87e6" [workspace.dependencies.soroban-env-host] version = "0.0.16" git = "https://github.com/stellar/rs-soroban-env" -rev = "0c0cae6fa22b751e7fd95d7ce7556ac6d7b7010e" +rev = "3762edfe52aafc6c0935619211d76d2d05fe87e6" [workspace.dependencies.stellar-strkey] version = "0.0.7" @@ -55,7 +55,7 @@ git = "https://github.com/stellar/rs-stellar-strkey" [workspace.dependencies.stellar-xdr] version = "0.0.16" git = "https://github.com/stellar/rs-stellar-xdr" -rev = "53e1a9cf2335aff29305c72deb6f075e78915dad" +rev = "b283ec0bcb791610013107b9eab7d7ff07a65db1" default-features = false #[patch."https://github.com/stellar/rs-soroban-env"] diff --git a/soroban-ledger-snapshot/Cargo.toml b/soroban-ledger-snapshot/Cargo.toml index db322cba5..5d3d3d9ca 100644 --- a/soroban-ledger-snapshot/Cargo.toml +++ b/soroban-ledger-snapshot/Cargo.toml @@ -11,7 +11,8 @@ edition = "2021" rust-version = "1.69" [dependencies] -soroban-env-host = { workspace = true, features = ["serde"] } +soroban-env-host = { workspace = true } +soroban-env-common = {workspace = true, features = ["serde"]} serde = { version = "1.0.0", features = ["derive"] } serde_json = "1.0.0" thiserror = "1.0" diff --git a/soroban-ledger-snapshot/src/lib.rs b/soroban-ledger-snapshot/src/lib.rs index 0bea685e1..bfcdbeb41 100644 --- a/soroban-ledger-snapshot/src/lib.rs +++ b/soroban-ledger-snapshot/src/lib.rs @@ -5,9 +5,10 @@ use std::{ rc::Rc, }; +use soroban_env_common::xdr::{ScErrorCode, ScErrorType}; use soroban_env_host::{ storage::SnapshotSource, - xdr::{LedgerEntry, LedgerKey, ScHostStorageErrorCode, ScStatus}, + xdr::{LedgerEntry, LedgerKey, ScError}, Host, HostError, LedgerInfo, }; @@ -165,9 +166,11 @@ impl SnapshotSource for &LedgerSnapshot { fn get(&self, key: &Rc) -> Result, HostError> { match self.ledger_entries.iter().find(|(k, _)| &**k == &**key) { Some((_, v)) => Ok(Rc::new(*v.clone())), - None => { - Err(ScStatus::HostStorageError(ScHostStorageErrorCode::AccessToUnknownEntry).into()) + None => Err(ScError { + type_: ScErrorType::Storage, + code: ScErrorCode::MissingValue, } + .into()), } } fn has(&self, key: &Rc) -> Result { diff --git a/soroban-sdk-macros/src/derive_enum.rs b/soroban-sdk-macros/src/derive_enum.rs index 1e0b1c640..6596d454e 100644 --- a/soroban-sdk-macros/src/derive_enum.rs +++ b/soroban-sdk-macros/src/derive_enum.rs @@ -167,7 +167,7 @@ pub fn derive_type_enum( impl #path::TryFromVal<#path::Env, #path::RawVal> for #enum_ident { type Error = #path::ConversionError; #[inline(always)] - fn try_from_val(env: &#path::Env, val: &#path::RawVal) -> Result { + fn try_from_val(env: &#path::Env, val: &#path::RawVal) -> Result { use #path::{EnvBase,TryIntoVal,TryFromVal}; const CASES: &'static [&'static str] = &[#(#case_name_str_lits),*]; let vec: #path::Vec<#path::RawVal> = val.try_into_val(env)?; @@ -183,7 +183,7 @@ pub fn derive_type_enum( impl #path::TryFromVal<#path::Env, #enum_ident> for #path::RawVal { type Error = #path::ConversionError; #[inline(always)] - fn try_from_val(env: &#path::Env, val: &#enum_ident) -> Result { + fn try_from_val(env: &#path::Env, val: &#enum_ident) -> Result { use #path::{TryIntoVal,TryFromVal}; match val { #(#try_intos,)* @@ -195,7 +195,7 @@ pub fn derive_type_enum( impl #path::TryFromVal<#path::Env, #path::xdr::ScVec> for #enum_ident { type Error = #path::xdr::Error; #[inline(always)] - fn try_from_val(env: &#path::Env, val: &#path::xdr::ScVec) -> Result { + fn try_from_val(env: &#path::Env, val: &#path::xdr::ScVec) -> Result { use #path::xdr::Validate; use #path::TryIntoVal; @@ -215,7 +215,7 @@ pub fn derive_type_enum( impl #path::TryFromVal<#path::Env, #path::xdr::ScVal> for #enum_ident { type Error = #path::xdr::Error; #[inline(always)] - fn try_from_val(env: &#path::Env, val: &#path::xdr::ScVal) -> Result { + fn try_from_val(env: &#path::Env, val: &#path::xdr::ScVal) -> Result { if let #path::xdr::ScVal::Vec(Some(vec)) = val { <_ as #path::TryFromVal<_, _>>::try_from_val(env, vec) } else { @@ -228,7 +228,7 @@ pub fn derive_type_enum( impl TryFrom<&#enum_ident> for #path::xdr::ScVec { type Error = #path::xdr::Error; #[inline(always)] - fn try_from(val: &#enum_ident) -> Result { + fn try_from(val: &#enum_ident) -> Result { extern crate alloc; Ok(match val { #(#into_xdrs,)* @@ -240,7 +240,7 @@ pub fn derive_type_enum( impl TryFrom<#enum_ident> for #path::xdr::ScVec { type Error = #path::xdr::Error; #[inline(always)] - fn try_from(val: #enum_ident) -> Result { + fn try_from(val: #enum_ident) -> Result { (&val).try_into() } } @@ -249,7 +249,7 @@ pub fn derive_type_enum( impl TryFrom<&#enum_ident> for #path::xdr::ScVal { type Error = #path::xdr::Error; #[inline(always)] - fn try_from(val: &#enum_ident) -> Result { + fn try_from(val: &#enum_ident) -> Result { Ok(#path::xdr::ScVal::Vec(Some(val.try_into()?))) } } @@ -258,7 +258,7 @@ pub fn derive_type_enum( impl TryFrom<#enum_ident> for #path::xdr::ScVal { type Error = #path::xdr::Error; #[inline(always)] - fn try_from(val: #enum_ident) -> Result { + fn try_from(val: #enum_ident) -> Result { (&val).try_into() } } diff --git a/soroban-sdk-macros/src/derive_enum_int.rs b/soroban-sdk-macros/src/derive_enum_int.rs index 871254d5c..c1de223d5 100644 --- a/soroban-sdk-macros/src/derive_enum_int.rs +++ b/soroban-sdk-macros/src/derive_enum_int.rs @@ -96,7 +96,7 @@ pub fn derive_type_enum_int( impl #path::TryFromVal<#path::Env, #path::RawVal> for #enum_ident { type Error = #path::ConversionError; #[inline(always)] - fn try_from_val(env: &#path::Env, val: &#path::RawVal) -> Result { + fn try_from_val(env: &#path::Env, val: &#path::RawVal) -> Result { use #path::TryIntoVal; let discriminant: u32 = val.try_into_val(env)?; Ok(match discriminant { @@ -109,7 +109,7 @@ pub fn derive_type_enum_int( impl #path::TryFromVal<#path::Env, #enum_ident> for #path::RawVal { type Error = #path::ConversionError; #[inline(always)] - fn try_from_val(env: &#path::Env, val: &#enum_ident) -> Result { + fn try_from_val(env: &#path::Env, val: &#enum_ident) -> Result { Ok(match val { #(#try_intos,)* }) @@ -120,7 +120,7 @@ pub fn derive_type_enum_int( impl #path::TryFromVal<#path::Env, #path::xdr::ScVal> for #enum_ident { type Error = #path::xdr::Error; #[inline(always)] - fn try_from_val(env: &#path::Env, val: &#path::xdr::ScVal) -> Result { + fn try_from_val(env: &#path::Env, val: &#path::xdr::ScVal) -> Result { if let #path::xdr::ScVal::U32(discriminant) = val { Ok(match *discriminant { #(#try_froms,)* @@ -136,7 +136,7 @@ pub fn derive_type_enum_int( impl TryInto<#path::xdr::ScVal> for &#enum_ident { type Error = #path::xdr::Error; #[inline(always)] - fn try_into(self) -> Result<#path::xdr::ScVal, Self::Error> { + fn try_into(self) -> Result<#path::xdr::ScVal, #path::xdr::Error> { Ok((*self as u32).into()) } } @@ -145,7 +145,7 @@ pub fn derive_type_enum_int( impl TryInto<#path::xdr::ScVal> for #enum_ident { type Error = #path::xdr::Error; #[inline(always)] - fn try_into(self) -> Result<#path::xdr::ScVal, Self::Error> { + fn try_into(self) -> Result<#path::xdr::ScVal, #path::xdr::Error> { Ok((self as u32).into()) } } diff --git a/soroban-sdk-macros/src/derive_error_enum_int.rs b/soroban-sdk-macros/src/derive_error_enum_int.rs index 8c2e203a3..e7d3a2fc1 100644 --- a/soroban-sdk-macros/src/derive_error_enum_int.rs +++ b/soroban-sdk-macros/src/derive_error_enum_int.rs @@ -49,7 +49,7 @@ pub fn derive_type_error_enum_int( }; let try_from = quote! { #discriminant => Self::#ident }; let into = - quote! { #enum_ident::#ident => #path::Status::from_contract_error(#discriminant) }; + quote! { #enum_ident::#ident => #path::Error::from_contract_error(#discriminant) }; (spec_case, try_from, into) }) .multiunzip(); @@ -90,42 +90,42 @@ pub fn derive_type_error_enum_int( quote! { #spec_gen - impl TryFrom<#path::Status> for #enum_ident { - type Error = #path::Status; + impl TryFrom<#path::Error> for #enum_ident { + type Error = #path::Error; #[inline(always)] - fn try_from(status: #path::Status) -> Result { - if status.is_type(#path::xdr::ScStatusType::ContractError) { - let discriminant = status.get_code(); + fn try_from(error: #path::Error) -> Result { + if error.is_type(#path::xdr::ScErrorType::Contract) { + let discriminant = error.get_code(); Ok(match discriminant { #(#try_froms,)* - _ => return Err(status), + _ => return Err(error), }) } else { - Err(status) + Err(error) } } } - impl TryFrom<&#path::Status> for #enum_ident { - type Error = #path::Status; + impl TryFrom<&#path::Error> for #enum_ident { + type Error = #path::Error; #[inline(always)] - fn try_from(status: &#path::Status) -> Result { - <_ as TryFrom<#path::Status>>::try_from(*status) + fn try_from(error: &#path::Error) -> Result { + <_ as TryFrom<#path::Error>>::try_from(*error) } } - impl From<#enum_ident> for #path::Status { + impl From<#enum_ident> for #path::Error { #[inline(always)] - fn from(val: #enum_ident) -> #path::Status { + fn from(val: #enum_ident) -> #path::Error { match val { #(#intos,)* } } } - impl From<&#enum_ident> for #path::Status { + impl From<&#enum_ident> for #path::Error { #[inline(always)] - fn from(val: &#enum_ident) -> #path::Status { + fn from(val: &#enum_ident) -> #path::Error { <_ as From<#enum_ident>>::from(*val) } } @@ -133,18 +133,18 @@ pub fn derive_type_error_enum_int( impl #path::TryFromVal<#path::Env, #path::RawVal> for #enum_ident { type Error = #path::ConversionError; #[inline(always)] - fn try_from_val(env: &#path::Env, val: &#path::RawVal) -> Result { + fn try_from_val(env: &#path::Env, val: &#path::RawVal) -> Result { use #path::TryIntoVal; - let status: #path::Status = val.try_into_val(env)?; - status.try_into().map_err(|_| #path::ConversionError) + let error: #path::Error = val.try_into_val(env)?; + error.try_into().map_err(|_| #path::ConversionError) } } impl #path::TryFromVal<#path::Env, #enum_ident> for #path::RawVal { type Error = #path::ConversionError; #[inline(always)] - fn try_from_val(env: &#path::Env, val: &#enum_ident) -> Result { - let status: #path::Status = val.into(); - Ok(status.into()) + fn try_from_val(env: &#path::Env, val: &#enum_ident) -> Result { + let error: #path::Error = val.into(); + Ok(error.into()) } } } diff --git a/soroban-sdk-macros/src/derive_struct.rs b/soroban-sdk-macros/src/derive_struct.rs index e31d596f3..726206277 100644 --- a/soroban-sdk-macros/src/derive_struct.rs +++ b/soroban-sdk-macros/src/derive_struct.rs @@ -106,7 +106,7 @@ pub fn derive_type_struct( impl #path::TryFromVal<#path::Env, #path::RawVal> for #ident { type Error = #path::ConversionError; - fn try_from_val(env: &#path::Env, val: &#path::RawVal) -> Result { + fn try_from_val(env: &#path::Env, val: &#path::RawVal) -> Result { use #path::{TryIntoVal,EnvBase,ConversionError,RawVal,MapObject}; const KEYS: [&'static str; #field_count_usize] = [#(#field_names),*]; let mut vals: [RawVal; #field_count_usize] = [RawVal::VOID.to_raw(); #field_count_usize]; @@ -120,7 +120,7 @@ pub fn derive_type_struct( impl #path::TryFromVal<#path::Env, #ident> for #path::RawVal { type Error = #path::ConversionError; - fn try_from_val(env: &#path::Env, val: &#ident) -> Result { + fn try_from_val(env: &#path::Env, val: &#ident) -> Result { use #path::{TryIntoVal,EnvBase,ConversionError,RawVal}; const KEYS: [&'static str; #field_count_usize] = [#(#field_names),*]; let vals: [RawVal; #field_count_usize] = [ @@ -134,7 +134,7 @@ pub fn derive_type_struct( impl #path::TryFromVal<#path::Env, #path::xdr::ScMap> for #ident { type Error = #path::xdr::Error; #[inline(always)] - fn try_from_val(env: &#path::Env, val: &#path::xdr::ScMap) -> Result { + fn try_from_val(env: &#path::Env, val: &#path::xdr::ScMap) -> Result { use #path::xdr::Validate; use #path::TryIntoVal; let map = val; @@ -152,7 +152,7 @@ pub fn derive_type_struct( impl #path::TryFromVal<#path::Env, #path::xdr::ScVal> for #ident { type Error = #path::xdr::Error; #[inline(always)] - fn try_from_val(env: &#path::Env, val: &#path::xdr::ScVal) -> Result { + fn try_from_val(env: &#path::Env, val: &#path::xdr::ScVal) -> Result { if let #path::xdr::ScVal::Map(Some(map)) = val { <_ as #path::TryFromVal<_, _>>::try_from_val(env, map) } else { @@ -165,7 +165,7 @@ pub fn derive_type_struct( impl TryFrom<&#ident> for #path::xdr::ScMap { type Error = #path::xdr::Error; #[inline(always)] - fn try_from(val: &#ident) -> Result { + fn try_from(val: &#ident) -> Result { extern crate alloc; use #path::TryFromVal; #path::xdr::ScMap::sorted_from(alloc::vec![ @@ -178,7 +178,7 @@ pub fn derive_type_struct( impl TryFrom<#ident> for #path::xdr::ScMap { type Error = #path::xdr::Error; #[inline(always)] - fn try_from(val: #ident) -> Result { + fn try_from(val: #ident) -> Result { (&val).try_into() } } @@ -187,7 +187,7 @@ pub fn derive_type_struct( impl TryFrom<&#ident> for #path::xdr::ScVal { type Error = #path::xdr::Error; #[inline(always)] - fn try_from(val: &#ident) -> Result { + fn try_from(val: &#ident) -> Result { Ok(#path::xdr::ScVal::Map(Some(val.try_into()?))) } } @@ -196,7 +196,7 @@ pub fn derive_type_struct( impl TryFrom<#ident> for #path::xdr::ScVal { type Error = #path::xdr::Error; #[inline(always)] - fn try_from(val: #ident) -> Result { + fn try_from(val: #ident) -> Result { (&val).try_into() } } diff --git a/soroban-sdk-macros/src/derive_struct_tuple.rs b/soroban-sdk-macros/src/derive_struct_tuple.rs index 2d1fcdeb2..bd60b1b43 100644 --- a/soroban-sdk-macros/src/derive_struct_tuple.rs +++ b/soroban-sdk-macros/src/derive_struct_tuple.rs @@ -94,7 +94,7 @@ pub fn derive_type_struct_tuple( impl #path::TryFromVal<#path::Env, #path::RawVal> for #ident { type Error = #path::ConversionError; #[inline(always)] - fn try_from_val(env: &#path::Env, val: &#path::RawVal) -> Result { + fn try_from_val(env: &#path::Env, val: &#path::RawVal) -> Result { use #path::{TryIntoVal,EnvBase,ConversionError,VecObject,RawVal}; let vec: VecObject = (*val).try_into().map_err(|_| ConversionError)?; let mut vals: [RawVal; #field_count_usize] = [RawVal::VOID.to_raw(); #field_count_usize]; @@ -108,7 +108,7 @@ pub fn derive_type_struct_tuple( impl #path::TryFromVal<#path::Env, #ident> for #path::RawVal { type Error = #path::ConversionError; #[inline(always)] - fn try_from_val(env: &#path::Env, val: &#ident) -> Result { + fn try_from_val(env: &#path::Env, val: &#ident) -> Result { use #path::{TryIntoVal,EnvBase,ConversionError,RawVal}; let vals: [RawVal; #field_count_usize] = [ #((&val.#field_idx_lits).try_into_val(env).map_err(|_| ConversionError)?),* @@ -121,7 +121,7 @@ pub fn derive_type_struct_tuple( impl #path::TryFromVal<#path::Env, #path::xdr::ScVec> for #ident { type Error = #path::xdr::Error; #[inline(always)] - fn try_from_val(env: &#path::Env, val: &#path::xdr::ScVec) -> Result { + fn try_from_val(env: &#path::Env, val: &#path::xdr::ScVec) -> Result { use #path::xdr::Validate; use #path::TryIntoVal; let vec = val; @@ -138,7 +138,7 @@ pub fn derive_type_struct_tuple( impl #path::TryFromVal<#path::Env, #path::xdr::ScVal> for #ident { type Error = #path::xdr::Error; #[inline(always)] - fn try_from_val(env: &#path::Env, val: &#path::xdr::ScVal) -> Result { + fn try_from_val(env: &#path::Env, val: &#path::xdr::ScVal) -> Result { if let #path::xdr::ScVal::Vec(Some(vec)) = val { <_ as #path::TryFromVal<_, _>>::try_from_val(env, vec) } else { @@ -151,7 +151,7 @@ pub fn derive_type_struct_tuple( impl TryFrom<&#ident> for #path::xdr::ScVec { type Error = #path::xdr::Error; #[inline(always)] - fn try_from(val: &#ident) -> Result { + fn try_from(val: &#ident) -> Result { extern crate alloc; use #path::TryFromVal; Ok(#path::xdr::ScVec(alloc::vec![ @@ -164,7 +164,7 @@ pub fn derive_type_struct_tuple( impl TryFrom<#ident> for #path::xdr::ScVec { type Error = #path::xdr::Error; #[inline(always)] - fn try_from(val: #ident) -> Result { + fn try_from(val: #ident) -> Result { (&val).try_into() } } @@ -173,7 +173,7 @@ pub fn derive_type_struct_tuple( impl TryFrom<&#ident> for #path::xdr::ScVal { type Error = #path::xdr::Error; #[inline(always)] - fn try_from(val: &#ident) -> Result { + fn try_from(val: &#ident) -> Result { Ok(#path::xdr::ScVal::Vec(Some(val.try_into()?))) } } @@ -182,7 +182,7 @@ pub fn derive_type_struct_tuple( impl TryFrom<#ident> for #path::xdr::ScVal { type Error = #path::xdr::Error; #[inline(always)] - fn try_from(val: #ident) -> Result { + fn try_from(val: #ident) -> Result { (&val).try_into() } } diff --git a/soroban-sdk-macros/src/map_type.rs b/soroban-sdk-macros/src/map_type.rs index 9c200f7dd..198e11fc6 100644 --- a/soroban-sdk-macros/src/map_type.rs +++ b/soroban-sdk-macros/src/map_type.rs @@ -31,7 +31,7 @@ pub fn map_type(t: &Type) -> Result { "bool" => Ok(ScSpecTypeDef::Bool), "Symbol" => Ok(ScSpecTypeDef::Symbol), "String" => Ok(ScSpecTypeDef::String), - "Status" => Ok(ScSpecTypeDef::Status), + "Error" => Ok(ScSpecTypeDef::Error), "Bytes" => Ok(ScSpecTypeDef::Bytes), "Address" => Ok(ScSpecTypeDef::Address), s => Ok(ScSpecTypeDef::Udt(ScSpecTypeUdt { diff --git a/soroban-sdk-macros/src/syn_ext.rs b/soroban-sdk-macros/src/syn_ext.rs index a6a123ca1..cf8434ab3 100644 --- a/soroban-sdk-macros/src/syn_ext.rs +++ b/soroban-sdk-macros/src/syn_ext.rs @@ -147,16 +147,16 @@ impl<'a> Fn<'a> { } pub fn try_output(&self, crate_path: &Path) -> Type { let (t, e) = match self.output { - ReturnType::Default => (quote!(()), quote!(#crate_path::Status)), + ReturnType::Default => (quote!(()), quote!(#crate_path::Error)), ReturnType::Type(_, typ) => match unpack_result(typ) { Some((t, e)) => (quote!(#t), quote!(#e)), - None => (quote!(#typ), quote!(#crate_path::Status)), + None => (quote!(#typ), quote!(#crate_path::Error)), }, }; Type::Verbatim(quote! { Result< Result<#t, <#t as #crate_path::TryFromVal<#crate_path::Env, #crate_path::RawVal>>::Error>, - Result<#e, <#e as TryFrom<#crate_path::Status>>::Error> + Result<#e, <#e as TryFrom<#crate_path::Error>>::Error> > }) } diff --git a/soroban-sdk/Cargo.toml b/soroban-sdk/Cargo.toml index 74a37403f..55b65d6e8 100644 --- a/soroban-sdk/Cargo.toml +++ b/soroban-sdk/Cargo.toml @@ -21,7 +21,7 @@ bytes-lit = "0.0.5" soroban-env-guest = { workspace = true } [target.'cfg(not(target_family="wasm"))'.dependencies] -soroban-env-host = { workspace = true, features = ["vm", "hostfn_log_fmt_values"] } +soroban-env-host = { workspace = true, features = [] } soroban-ledger-snapshot = { workspace = true } stellar-strkey = { workspace = true } ed25519-dalek = { version = "1.0.1", optional = true } @@ -29,7 +29,7 @@ ed25519-dalek = { version = "1.0.1", optional = true } rand = "0.7.3" [dev-dependencies] -soroban-env-host = { workspace = true, features = ["vm", "hostfn_log_fmt_values", "testutils"] } +soroban-env-host = { workspace = true, features = ["testutils"] } stellar-xdr = { workspace = true, features = ["next", "std"] } soroban-spec = { workspace = true } ed25519-dalek = "1.0.1" diff --git a/soroban-sdk/src/address.rs b/soroban-sdk/src/address.rs index abc6c50e3..1859a0a70 100644 --- a/soroban-sdk/src/address.rs +++ b/soroban-sdk/src/address.rs @@ -129,7 +129,7 @@ impl TryFromVal for RawVal { #[cfg(not(target_family = "wasm"))] impl TryFrom<&Address> for ScVal { type Error = ConversionError; - fn try_from(v: &Address) -> Result { + fn try_from(v: &Address) -> Result { ScVal::try_from_val(&v.env, &v.obj.to_raw()) } } @@ -137,7 +137,7 @@ impl TryFrom<&Address> for ScVal { #[cfg(not(target_family = "wasm"))] impl TryFrom
for ScVal { type Error = ConversionError; - fn try_from(v: Address) -> Result { + fn try_from(v: Address) -> Result { (&v).try_into() } } diff --git a/soroban-sdk/src/bytes.rs b/soroban-sdk/src/bytes.rs index a296483e7..40ef7db45 100644 --- a/soroban-sdk/src/bytes.rs +++ b/soroban-sdk/src/bytes.rs @@ -231,7 +231,7 @@ impl From<&Bytes> for Bytes { #[cfg(not(target_family = "wasm"))] impl TryFrom<&Bytes> for ScVal { type Error = ConversionError; - fn try_from(v: &Bytes) -> Result { + fn try_from(v: &Bytes) -> Result { ScVal::try_from_val(&v.env, &v.obj.to_raw()) } } @@ -239,7 +239,7 @@ impl TryFrom<&Bytes> for ScVal { #[cfg(not(target_family = "wasm"))] impl TryFrom for ScVal { type Error = ConversionError; - fn try_from(v: Bytes) -> Result { + fn try_from(v: Bytes) -> Result { (&v).try_into() } } @@ -847,7 +847,7 @@ impl From<&BytesN> for Bytes { #[cfg(not(target_family = "wasm"))] impl TryFrom<&BytesN> for ScVal { type Error = ConversionError; - fn try_from(v: &BytesN) -> Result { + fn try_from(v: &BytesN) -> Result { ScVal::try_from_val(&v.0.env, &v.0.obj.to_raw()) } } @@ -855,7 +855,7 @@ impl TryFrom<&BytesN> for ScVal { #[cfg(not(target_family = "wasm"))] impl TryFrom> for ScVal { type Error = ConversionError; - fn try_from(v: BytesN) -> Result { + fn try_from(v: BytesN) -> Result { (&v).try_into() } } diff --git a/soroban-sdk/src/env.rs b/soroban-sdk/src/env.rs index 199b8be29..9a99403bc 100644 --- a/soroban-sdk/src/env.rs +++ b/soroban-sdk/src/env.rs @@ -88,10 +88,10 @@ pub use internal::xdr; pub use internal::Compare; pub use internal::ConversionError; pub use internal::EnvBase; +pub use internal::Error; pub use internal::MapObject; pub use internal::RawVal; pub use internal::RawValConvertible; -pub use internal::Status; pub use internal::SymbolStr; pub use internal::TryFromVal; pub use internal::TryIntoVal; @@ -258,8 +258,8 @@ impl Env { /// /// Equivalent to `panic!`, but with an error value instead of a string. #[doc(hidden)] - pub fn panic_with_error(&self, error: impl Into) { - _ = internal::Env::fail_with_status(self, error.into()); + pub fn panic_with_error(&self, error: impl Into) { + _ = internal::Env::fail_with_error(self, error.into()); unreachable!() } @@ -404,10 +404,10 @@ impl Env { ) -> Result, Result> where T: TryFromVal, - E: TryFrom, + E: TryFrom, { let Some(contract_id) = contract_id.try_contract_id() else { - return Err(E::try_from(Status::from_status(xdr::ScStatus::UnknownError(xdr::ScUnknownErrorCode::General)))); + return Err(E::try_from(Error::from_type_and_code(xdr::ScErrorType::Value, xdr::ScErrorCode::MissingValue))); }; let rv = internal::Env::try_call( self, @@ -416,8 +416,8 @@ impl Env { args.to_object(), ) .unwrap_infallible(); - match Status::try_from_val(self, &rv) { - Ok(status) => Err(E::try_from(status)), + match internal::Error::try_from_val(self, &rv) { + Ok(err) => Err(E::try_from(err)), Err(ConversionError) => Ok(T::try_from_val(self, &rv)), } } @@ -428,11 +428,6 @@ impl Env { Logger::new(self) } - #[doc(hidden)] - pub fn log_value>(&self, v: V) { - internal::Env::log_value(self, v.into_val(self)).unwrap_infallible(); - } - /// Replaces the executable of the current contract with the provided Wasm. /// /// The Wasm blob identified by the `wasm_hash` has to be already present @@ -459,7 +454,9 @@ use soroban_ledger_snapshot::LedgerSnapshot; #[cfg(any(test, feature = "testutils"))] use std::{path::Path, rc::Rc}; #[cfg(any(test, feature = "testutils"))] -use xdr::{ContractAuth, Hash, LedgerEntry, LedgerKey, LedgerKeyContractData}; +use xdr::{ + ContractAuth, Hash, LedgerEntry, LedgerKey, LedgerKeyContractData, ScErrorCode, ScErrorType, +}; #[cfg(any(test, feature = "testutils"))] #[cfg_attr(feature = "docs", doc(cfg(feature = "testutils")))] impl Env { @@ -476,10 +473,8 @@ impl Env { &self, _key: &Rc, ) -> Result, soroban_env_host::HostError> { - use xdr::{ScHostStorageErrorCode, ScStatus}; - let status: internal::Status = - ScStatus::HostStorageError(ScHostStorageErrorCode::MissingKeyInGet).into(); - Err(status.into()) + let err: internal::Error = (ScErrorType::Storage, ScErrorCode::MissingValue).into(); + Err(err.into()) } fn has(&self, _key: &Rc) -> Result { @@ -494,6 +489,7 @@ impl Env { env_impl.set_source_account(xdr::AccountId(xdr::PublicKey::PublicKeyTypeEd25519( xdr::Uint256(random()), ))); + env_impl.set_diagnostic_level(internal::DiagnosticLevel::Debug); let env = Env { env_impl, snapshot: None, @@ -998,7 +994,7 @@ impl Env { /// auth on the host side. /// /// This function requires to provide the template argument for error. Use - /// `soroban_sdk::Status` if `__check_auth` doesn't return a special + /// `soroban_sdk::Error` if `__check_auth` doesn't return a special /// contract error and use the error with `contracterror` attribute /// otherwise. /// @@ -1048,10 +1044,10 @@ impl Env { /// // as long as a valid error type used. /// Err(Ok(NoopAccountError::SomeError)) /// ); - /// // Succesful call of `__check_auth` with a `soroban_sdk::Status` + /// // Succesful call of `__check_auth` with a `soroban_sdk::Error` /// // error - this should be compatible with any error type. /// assert_eq!( - /// e.try_invoke_contract_check_auth::( + /// e.try_invoke_contract_check_auth::( /// &account_contract.address.contract_id(), /// &BytesN::random(&e), /// &vec![&e, 0_i32.into()], @@ -1061,7 +1057,7 @@ impl Env { /// ); /// } /// ``` - pub fn try_invoke_contract_check_auth>( + pub fn try_invoke_contract_check_auth>( &self, contract: &BytesN<32>, signature_payload: &BytesN<32>, @@ -1081,7 +1077,7 @@ impl Env { .call_account_contract_check_auth(contract.to_object(), args.to_object()); match res { Ok(rv) => Ok(rv.into_val(self)), - Err(e) => Err(e.status.try_into()), + Err(e) => Err(e.error.try_into()), } } @@ -1301,43 +1297,8 @@ impl internal::EnvBase for Env { Ok(self.env_impl.bytes_new_from_slice(slice).unwrap_optimized()) } - fn log_static_fmt_val(&self, fmt: &'static str, v: RawVal) -> Result<(), Self::Error> { - Ok(self.env_impl.log_static_fmt_val(fmt, v).unwrap_optimized()) - } - - fn log_static_fmt_static_str( - &self, - fmt: &'static str, - s: &'static str, - ) -> Result<(), Self::Error> { - Ok(self - .env_impl - .log_static_fmt_static_str(fmt, s) - .unwrap_optimized()) - } - - fn log_static_fmt_val_static_str( - &self, - fmt: &'static str, - v: RawVal, - s: &'static str, - ) -> Result<(), Self::Error> { - Ok(self - .env_impl - .log_static_fmt_val_static_str(fmt, v, s) - .unwrap_optimized()) - } - - fn log_static_fmt_general( - &self, - fmt: &'static str, - v: &[RawVal], - s: &[&'static str], - ) -> Result<(), Self::Error> { - Ok(self - .env_impl - .log_static_fmt_general(fmt, v, s) - .unwrap_optimized()) + fn log_from_slice(&self, msg: &str, args: &[RawVal]) -> Result { + Ok(self.env_impl.log_from_slice(msg, args).unwrap_optimized()) } fn string_copy_to_slice( diff --git a/soroban-sdk/src/events.rs b/soroban-sdk/src/events.rs index c124e8393..722354518 100644 --- a/soroban-sdk/src/events.rs +++ b/soroban-sdk/src/events.rs @@ -134,12 +134,12 @@ impl testutils::Events for Events { .0 .into_iter() .for_each(|e| { - if let internal::events::Event::Contract(xdr::ContractEvent { + if let xdr::ContractEvent { type_: xdr::ContractEventType::Contract, contract_id: Some(contract_id), body: xdr::ContractEventBody::V0(xdr::ContractEventV0 { topics, data }), .. - }) = e.event + } = e.event { vec.push_back(( Address::from_contract_id(&BytesN::from_array(env, &contract_id.0)), diff --git a/soroban-sdk/src/lib.rs b/soroban-sdk/src/lib.rs index 720a11db5..60b5d00db 100644 --- a/soroban-sdk/src/lib.rs +++ b/soroban-sdk/src/lib.rs @@ -110,7 +110,7 @@ pub use bytes_lit::bytes as __bytes_lit_bytes; #[doc(hidden)] pub use bytes_lit::bytesmin as __bytes_lit_bytesmin; -/// Generates conversions from the repr(u32) enum from/into a `Status`. +/// Generates conversions from the repr(u32) enum from/into an `Error`. /// /// There are some constraints on the types that are supported: /// - Enum must derive `Copy`. @@ -647,8 +647,8 @@ pub use env::TryIntoVal; mod envhidden { pub use super::env::EnvBase; + pub use super::env::Error; pub use super::env::MapObject; - pub use super::env::Status; pub use super::env::SymbolStr; pub use super::env::VecObject; } diff --git a/soroban-sdk/src/logging.rs b/soroban-sdk/src/logging.rs index 77d1aa527..aa659be33 100644 --- a/soroban-sdk/src/logging.rs +++ b/soroban-sdk/src/logging.rs @@ -3,17 +3,15 @@ //! See [`log`][crate::log] for how to conveniently log debug events. use core::fmt::Debug; -use crate::{ - env::internal::{self, EnvBase}, - unwrap::UnwrapInfallible, - Env, IntoVal, RawVal, Vec, -}; +use crate::{env::internal::EnvBase, Env, RawVal}; /// Log a debug event. /// -/// Takes a [Env], and a literal format string that containing `{}` for each -/// additional argument. Arguments may be any value that are convertible to -/// [`RawVal`]. +/// Takes a [Env], a literal string, and an optional trailing sequence of +/// arguments that may be any value that are convertible to [`RawVal`]. The +/// string and arguments are appended as-is to the log, as the body of a +/// structured diagnostic event. Such events may be emitted from the host as +/// auxiliary diagnostic XDR, or converted to strings later for debugging. /// /// `log!` statements are only enabled in non optimized builds that have /// `debug-assertions` enabled. To enable `debug-assertions` add the following @@ -26,7 +24,8 @@ use crate::{ /// debug-assertions = true /// ``` /// -/// [custom profiles]: https://doc.rust-lang.org/cargo/reference/profiles.html#custom-profiles +/// [custom profiles]: +/// https://doc.rust-lang.org/cargo/reference/profiles.html#custom-profiles /// /// ### Examples /// @@ -48,7 +47,7 @@ use crate::{ /// let env = Env::default(); /// /// let value = 5; -/// log!(&env, "a log entry: {}, {}", value, Symbol::short("another")); +/// log!(&env, "a log entry", value, Symbol::short("another")); /// ``` /// /// Assert on logs in tests: @@ -61,16 +60,11 @@ use crate::{ /// let env = Env::default(); /// /// let value = 5; -/// log!(&env, "a log entry: {}, {}", value, Symbol::short("another")); +/// log!(&env, "a log entry", value, Symbol::short("another")); /// /// use soroban_sdk::testutils::Logger; -/// -/// assert_eq!( -/// env.logger().all(), -/// std::vec![ -/// "a log entry: I32(5), Symbol(another)".to_string(), -/// ], -/// ); +/// let logentry = env.logger().all().last().unwrap().clone(); +/// assert!(logentry.contains("[\"a log entry\", 5, another]")); /// # } /// ``` #[macro_export] @@ -116,45 +110,43 @@ impl Logger { /// Log a debug event. /// - /// Takes a literal format string that containing `{}` for each argument in - /// the args slice. + /// Takes a literal string and a sequence of trailing values to add + /// as a log entry in the diagnostic event stream. /// /// See [`log`][crate::log] for how to conveniently log debug events. #[inline(always)] - pub fn log(&self, fmt: &'static str, args: &[RawVal]) { + pub fn log(&self, msg: &'static str, args: &[RawVal]) { if cfg!(debug_assertions) { let env = self.env(); - // If building for WASM logging will be transmitted through the - // guest interface via host types. This is very inefficient. When - // building on non-WASM environments where the environment Host is - // directly available, use the log static variants. - if cfg!(target_family = "wasm") { - let fmt: crate::String = fmt.into_val(env); - let args: Vec = Vec::from_slice(env, args); - internal::Env::log_fmt_values(env, fmt.to_object(), args.to_object()) - .unwrap_infallible(); - } else { - env.log_static_fmt_general(fmt, args, &[]).unwrap(); - } + env.log_from_slice(msg, args).unwrap(); } } } #[cfg(any(test, feature = "testutils"))] -use crate::{env::internal::events::Event, testutils}; +use crate::testutils; #[cfg(any(test, feature = "testutils"))] #[cfg_attr(feature = "docs", doc(cfg(feature = "testutils")))] impl testutils::Logger for Logger { fn all(&self) -> std::vec::Vec { + use crate::xdr::{ + ContractEventBody, ContractEventType, ScSymbol, ScVal, ScVec, StringM, VecM, + }; let env = self.env(); + let log_sym = ScSymbol(StringM::try_from("log").unwrap()); + let log_topics = ScVec(VecM::try_from(vec![ScVal::Symbol(log_sym)]).unwrap()); env.host() .get_events() .unwrap() .0 .into_iter() - .filter_map(|e| match e.event { - Event::Debug(de) => Some(format!("{}", de)), + .filter_map(|e| match (&e.event.type_, &e.event.body) { + (ContractEventType::Diagnostic, ContractEventBody::V0(ce)) + if &ce.topics == &log_topics => + { + Some(format!("{}", &e)) + } _ => None, }) .collect::>() diff --git a/soroban-sdk/src/map.rs b/soroban-sdk/src/map.rs index 67e1f9ec6..ea7c5faec 100644 --- a/soroban-sdk/src/map.rs +++ b/soroban-sdk/src/map.rs @@ -9,7 +9,7 @@ use crate::{ use super::{ env::internal::{Env as _, EnvBase as _, MapObject}, - ConversionError, Env, IntoVal, RawVal, Status, TryFromVal, TryIntoVal, Vec, + ConversionError, Env, Error, IntoVal, RawVal, TryFromVal, TryIntoVal, Vec, }; #[cfg(not(target_family = "wasm"))] @@ -216,7 +216,7 @@ where #[cfg(not(target_family = "wasm"))] impl TryFrom<&Map> for ScVal { type Error = ConversionError; - fn try_from(v: &Map) -> Result { + fn try_from(v: &Map) -> Result { ScVal::try_from_val(&v.env, &v.obj.to_raw()) } } @@ -224,7 +224,7 @@ impl TryFrom<&Map> for ScVal { #[cfg(not(target_family = "wasm"))] impl TryFrom> for ScVal { type Error = ConversionError; - fn try_from(v: Map) -> Result { + fn try_from(v: Map) -> Result { (&v).try_into() } } @@ -232,7 +232,7 @@ impl TryFrom> for ScVal { #[cfg(not(target_family = "wasm"))] impl TryFromVal> for ScVal { type Error = ConversionError; - fn try_from_val(_e: &Env, v: &Map) -> Result { + fn try_from_val(_e: &Env, v: &Map) -> Result { v.try_into() } } @@ -449,7 +449,7 @@ where fn next(&mut self) -> Option { let env = &self.0.env; let key = env.map_min_key(self.0.obj).unwrap_infallible(); - if Status::try_from(key).is_ok() { + if Error::try_from(key).is_ok() { return None; } let value = env.map_get(self.0.obj, key).unwrap_infallible(); @@ -482,7 +482,7 @@ where fn next_back(&mut self) -> Option { let env = &self.0.env; let key = env.map_max_key(self.0.obj).unwrap_infallible(); - if Status::try_from(key).is_ok() { + if Error::try_from(key).is_ok() { return None; } let value = env.map_get(self.0.obj, key).unwrap_infallible(); diff --git a/soroban-sdk/src/num.rs b/soroban-sdk/src/num.rs index f944c455e..1100a8553 100644 --- a/soroban-sdk/src/num.rs +++ b/soroban-sdk/src/num.rs @@ -107,7 +107,7 @@ macro_rules! impl_num_wrapping_val_type { #[cfg(not(target_family = "wasm"))] impl TryFrom<&$wrapper> for ScVal { type Error = ConversionError; - fn try_from(v: &$wrapper) -> Result { + fn try_from(v: &$wrapper) -> Result { if let Ok(ss) = <$small>::try_from(v.val) { ScVal::try_from(ss) } else { @@ -120,7 +120,7 @@ macro_rules! impl_num_wrapping_val_type { #[cfg(not(target_family = "wasm"))] impl TryFrom<$wrapper> for ScVal { type Error = ConversionError; - fn try_from(v: $wrapper) -> Result { + fn try_from(v: $wrapper) -> Result { (&v).try_into() } } diff --git a/soroban-sdk/src/string.rs b/soroban-sdk/src/string.rs index 914535751..8d09dcd21 100644 --- a/soroban-sdk/src/string.rs +++ b/soroban-sdk/src/string.rs @@ -136,7 +136,7 @@ impl From<&String> for String { #[cfg(not(target_family = "wasm"))] impl TryFrom<&String> for ScVal { type Error = ConversionError; - fn try_from(v: &String) -> Result { + fn try_from(v: &String) -> Result { ScVal::try_from_val(&v.env, &v.obj.to_raw()) } } @@ -144,7 +144,7 @@ impl TryFrom<&String> for ScVal { #[cfg(not(target_family = "wasm"))] impl TryFrom for ScVal { type Error = ConversionError; - fn try_from(v: String) -> Result { + fn try_from(v: String) -> Result { (&v).try_into() } } diff --git a/soroban-sdk/src/symbol.rs b/soroban-sdk/src/symbol.rs index d57d5ca3e..7695e6a69 100644 --- a/soroban-sdk/src/symbol.rs +++ b/soroban-sdk/src/symbol.rs @@ -138,7 +138,7 @@ impl TryFromVal for Symbol { #[cfg(not(target_family = "wasm"))] impl TryFrom<&Symbol> for ScVal { type Error = ConversionError; - fn try_from(v: &Symbol) -> Result { + fn try_from(v: &Symbol) -> Result { if let Ok(ss) = SymbolSmall::try_from(v.val) { ScVal::try_from(ss) } else { @@ -151,7 +151,7 @@ impl TryFrom<&Symbol> for ScVal { #[cfg(not(target_family = "wasm"))] impl TryFrom for ScVal { type Error = ConversionError; - fn try_from(v: Symbol) -> Result { + fn try_from(v: Symbol) -> Result { (&v).try_into() } } @@ -159,7 +159,7 @@ impl TryFrom for ScVal { #[cfg(not(target_family = "wasm"))] impl TryFromVal for ScVal { type Error = ConversionError; - fn try_from_val(_e: &Env, v: &Symbol) -> Result { + fn try_from_val(_e: &Env, v: &Symbol) -> Result { v.try_into() } } diff --git a/soroban-sdk/src/tests/contract_assert.rs b/soroban-sdk/src/tests/contract_assert.rs index a1cb85a14..bfd7ccc01 100644 --- a/soroban-sdk/src/tests/contract_assert.rs +++ b/soroban-sdk/src/tests/contract_assert.rs @@ -1,5 +1,5 @@ use crate as soroban_sdk; -use soroban_sdk::{contractimpl, xdr::ScStatusType, Env, Status}; +use soroban_sdk::{contractimpl, Env}; use soroban_sdk_macros::contracterror; pub struct Contract; @@ -19,8 +19,8 @@ impl Contract { } #[test] -#[should_panic(expected = "Status(ContractError(1)")] -fn test_invoke_expect_status() { +#[should_panic(expected = "Error(Contract, #1")] +fn test_invoke_expect_error() { let e = Env::default(); let contract_id = e.register_contract(None, Contract); @@ -33,11 +33,5 @@ fn test_try_invoke() { let contract_id = e.register_contract(None, Contract); let res = ContractClient::new(&e, &contract_id).try_assert(&0); - assert_eq!( - res, - Err(Ok(Status::from_type_and_code( - ScStatusType::ContractError, - 1, - ))) - ); + assert_eq!(res, Err(Ok(soroban_sdk::Error::from_contract_error(1)))); } diff --git a/soroban-sdk/src/tests/contract_invoke.rs b/soroban-sdk/src/tests/contract_invoke.rs index 4642c891d..708d61220 100644 --- a/soroban-sdk/src/tests/contract_invoke.rs +++ b/soroban-sdk/src/tests/contract_invoke.rs @@ -1,5 +1,6 @@ use crate as soroban_sdk; -use soroban_sdk::{contractimpl, xdr::ScStatusType, Env, Status}; +use soroban_sdk::{contractimpl, Env}; +use stellar_xdr::{ScErrorCode, ScErrorType}; pub struct Contract; @@ -20,8 +21,8 @@ fn test_invoke_expect_string() { } #[test] -#[should_panic(expected = "Status(UnknownError(0)")] -fn test_invoke_expect_status() { +#[should_panic(expected = "Error(Context, InternalError)")] +fn test_invoke_expect_error() { let e = Env::default(); let contract_id = e.register_contract(None, Contract); @@ -36,9 +37,9 @@ fn test_try_invoke() { let res = ContractClient::new(&e, &contract_id).try_panic(); assert_eq!( res, - Err(Ok(Status::from_type_and_code( - ScStatusType::UnknownError, - 0, + Err(Ok(soroban_sdk::Error::from_type_and_code( + ScErrorType::Context, + ScErrorCode::InternalError ))) ); } diff --git a/soroban-sdk/src/tests/contractfile_with_sha256.rs b/soroban-sdk/src/tests/contractfile_with_sha256.rs index 814fd6a0e..e6001e5f9 100644 --- a/soroban-sdk/src/tests/contractfile_with_sha256.rs +++ b/soroban-sdk/src/tests/contractfile_with_sha256.rs @@ -1,7 +1,7 @@ use crate as soroban_sdk; pub const WASM: &[u8] = soroban_sdk::contractfile!( file = "../target/wasm32-unknown-unknown/release/test_add_u64.wasm", - sha256 = "2a7398491f3999fa3c0b595cd84f15ad1d4449b6525b59ce67a4504595a72bfd", + sha256 = "c51f05bd99a9ca1bd77f57ddaba0ca42e6c652bb41be32c48b9fbbb490550e3c", ); #[test] diff --git a/soroban-sdk/src/tests/contractimport_with_sha256.rs b/soroban-sdk/src/tests/contractimport_with_sha256.rs index f3681cb8f..576a8fb9c 100644 --- a/soroban-sdk/src/tests/contractimport_with_sha256.rs +++ b/soroban-sdk/src/tests/contractimport_with_sha256.rs @@ -6,7 +6,7 @@ mod addcontract { use crate as soroban_sdk; soroban_sdk::contractimport!( file = "../target/wasm32-unknown-unknown/release/test_add_u64.wasm", - sha256 = "2a7398491f3999fa3c0b595cd84f15ad1d4449b6525b59ce67a4504595a72bfd", + sha256 = "c51f05bd99a9ca1bd77f57ddaba0ca42e6c652bb41be32c48b9fbbb490550e3c", ); } diff --git a/soroban-sdk/src/testutils.rs b/soroban-sdk/src/testutils.rs index 3afa0c59c..75254606c 100644 --- a/soroban-sdk/src/testutils.rs +++ b/soroban-sdk/src/testutils.rs @@ -148,9 +148,9 @@ pub trait Events { /// Test utilities for [`Logger`][crate::logging::Logger]. pub trait Logger { - /// Returns all debug events that have been logged. + /// Returns all diagnostic events that have been logged. fn all(&self) -> std::vec::Vec; - /// Prints all debug events to stdout. + /// Prints all diagnostic events to stdout. fn print(&self); } diff --git a/soroban-sdk/src/vec.rs b/soroban-sdk/src/vec.rs index 750533b84..f29b45da4 100644 --- a/soroban-sdk/src/vec.rs +++ b/soroban-sdk/src/vec.rs @@ -260,7 +260,7 @@ use super::xdr::{ScVal, ScVec}; #[cfg(not(target_family = "wasm"))] impl TryFrom<&Vec> for ScVal { type Error = ConversionError; - fn try_from(v: &Vec) -> Result { + fn try_from(v: &Vec) -> Result { ScVal::try_from_val(&v.env, &v.obj.to_raw()) } } @@ -268,7 +268,7 @@ impl TryFrom<&Vec> for ScVal { #[cfg(not(target_family = "wasm"))] impl TryFrom<&Vec> for ScVec { type Error = ConversionError; - fn try_from(v: &Vec) -> Result { + fn try_from(v: &Vec) -> Result { if let ScVal::Vec(Some(vec)) = ScVal::try_from(v)? { Ok(vec) } else { @@ -280,7 +280,7 @@ impl TryFrom<&Vec> for ScVec { #[cfg(not(target_family = "wasm"))] impl TryFrom> for ScVal { type Error = ConversionError; - fn try_from(v: Vec) -> Result { + fn try_from(v: Vec) -> Result { (&v).try_into() } } @@ -288,7 +288,7 @@ impl TryFrom> for ScVal { #[cfg(not(target_family = "wasm"))] impl TryFrom> for ScVec { type Error = ConversionError; - fn try_from(v: Vec) -> Result { + fn try_from(v: Vec) -> Result { (&v).try_into() } } @@ -299,7 +299,7 @@ where T: IntoVal + TryFromVal, { type Error = ConversionError; - fn try_from_val(env: &Env, val: &ScVal) -> Result { + fn try_from_val(env: &Env, val: &ScVal) -> Result { Ok( VecObject::try_from_val(env, &RawVal::try_from_val(env, val)?)? .try_into_val(env) diff --git a/soroban-spec/src/gen/json/types.rs b/soroban-spec/src/gen/json/types.rs index dad4ec972..619a96520 100644 --- a/soroban-spec/src/gen/json/types.rs +++ b/soroban-spec/src/gen/json/types.rs @@ -116,7 +116,7 @@ pub enum Type { I256, Bool, Symbol, - Status, + Error, Bytes, String, Address, @@ -202,7 +202,7 @@ impl From<&ScSpecTypeDef> for Type { ScSpecTypeDef::I256 => Type::I256, ScSpecTypeDef::Bool => Type::Bool, ScSpecTypeDef::Symbol => Type::Symbol, - ScSpecTypeDef::Status => Type::Status, + ScSpecTypeDef::Error => Type::Error, ScSpecTypeDef::Bytes => Type::Bytes, ScSpecTypeDef::String => Type::String, ScSpecTypeDef::Address => Type::Address, diff --git a/soroban-spec/src/gen/rust/types.rs b/soroban-spec/src/gen/rust/types.rs index 98c9f6882..4657e413d 100644 --- a/soroban-spec/src/gen/rust/types.rs +++ b/soroban-spec/src/gen/rust/types.rs @@ -144,7 +144,7 @@ pub fn generate_type_ident(spec: &ScSpecTypeDef) -> TokenStream { ScSpecTypeDef::I128 => quote! { i128 }, ScSpecTypeDef::Bool => quote! { bool }, ScSpecTypeDef::Symbol => quote! { soroban_sdk::Symbol }, - ScSpecTypeDef::Status => quote! { soroban_sdk::Status }, + ScSpecTypeDef::Error => quote! { soroban_sdk::Error }, ScSpecTypeDef::Bytes => quote! { soroban_sdk::Bytes }, ScSpecTypeDef::Address => quote! { soroban_sdk::Address }, ScSpecTypeDef::String => quote! { soroban_sdk::String }, diff --git a/tests/auth/src/lib.rs b/tests/auth/src/lib.rs index f8ddbe034..482e119a5 100644 --- a/tests/auth/src/lib.rs +++ b/tests/auth/src/lib.rs @@ -21,7 +21,7 @@ mod test_a { xdr::{ AddressWithNonce, AuthorizedInvocation, ContractAuth, ScAddress, ScVal, StringM, VecM, }, - Address, Env, RawVal, Status, Symbol, + Address, Env, Error, RawVal, Symbol, }; extern crate std; @@ -151,7 +151,7 @@ mod test_a { // auth_decline::Contract defined at the bottom of this file. The auth // contract's error is leaking into the contract being called and // propogating as its own contract, which should not be happening. - assert_eq!(r, Err(Ok(Status::from_contract_error(1)))); + assert_eq!(r, Err(Ok(Error::from_contract_error(1)))); assert_eq!(e.auths(), []); } @@ -217,7 +217,7 @@ mod test_b { contracterror, testutils::{Address as _, MockAuth, MockAuthInvoke}, xdr::{AddressWithNonce, AuthorizedInvocation, ContractAuth, ScAddress, ScVal, StringM}, - Address, Env, RawVal, Status, Symbol, + Address, Env, Error, RawVal, Symbol, }; extern crate std; @@ -394,7 +394,7 @@ mod test_b { // auth_decline::Contract defined at the bottom of this file. The auth // contract's error is leaking into the contract being called and // propogating as its own contract, which should not be happening. - assert_eq!(r, Err(Ok(Status::from_contract_error(1)))); + assert_eq!(r, Err(Ok(Error::from_contract_error(1)))); assert_eq!(e.auths(), []); } diff --git a/tests/errors/src/lib.rs b/tests/errors/src/lib.rs index 970b67a82..890af8397 100644 --- a/tests/errors/src/lib.rs +++ b/tests/errors/src/lib.rs @@ -38,8 +38,8 @@ impl Contract { #[cfg(test)] mod test { use soroban_sdk::{ - xdr::{ScStatus, ScUnknownErrorCode}, - Env, Status, Symbol, + xdr::{ScErrorCode, ScErrorType}, + Env, Symbol, }; use crate::{Contract, ContractClient, Error}; @@ -97,9 +97,10 @@ mod test { let res = client.try_hello(&3); assert_eq!( res, - Err(Err(Status::from_status(ScStatus::UnknownError( - ScUnknownErrorCode::General - ),))) + Err(Err(soroban_sdk::Error::from_type_and_code( + ScErrorType::Context, + ScErrorCode::InternalError + ))) ); assert!(!client.persisted()); } diff --git a/tests/logging/src/lib.rs b/tests/logging/src/lib.rs index a69b361b3..159bc10bc 100644 --- a/tests/logging/src/lib.rs +++ b/tests/logging/src/lib.rs @@ -8,17 +8,17 @@ impl Contract { pub fn hello(env: Env) { log!(&env, "none"); log!(&env, "none",); - log!(&env, "one: {}", Symbol::short("one")); - log!(&env, "one: {}", Symbol::short("one"),); + log!(&env, "one:", Symbol::short("one")); + log!(&env, "one:", Symbol::short("one"),); log!( &env, - "one: {}, two: {}", + "one and two:", Symbol::short("one"), Symbol::short("two") ); log!( &env, - "one: {}, two: {}", + "one and two:", Symbol::short("one"), Symbol::short("two"), ); @@ -28,7 +28,6 @@ impl Contract { #[cfg(test)] mod test { extern crate std; - use std::string::ToString; use soroban_sdk::{testutils::Logger, Env}; @@ -45,17 +44,17 @@ mod test { env.logger().print(); if cfg!(debug_assertions) { - assert_eq!( - env.logger().all(), - std::vec![ - "none".to_string(), - "none".to_string(), - "one: Symbol(one)".to_string(), - "one: Symbol(one)".to_string(), - "one: Symbol(one), two: Symbol(two)".to_string(), - "one: Symbol(one), two: Symbol(two)".to_string(), - ], - ); + let pats = std::vec![ + "\"none\"", + "\"none\"", + "[\"one:\", one]", + "[\"one:\", one]", + "[\"one and two:\", one, two]", + "[\"one and two:\", one, two]" + ]; + for (msg, pat) in env.logger().all().iter().zip(pats.iter()) { + assert!(msg.contains(pat)); + } } else { assert_eq!(env.logger().all(), std::vec![""; 0]); }