Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adapt SDK to changes to {Try,}{From,Into}Val in env crates. #824

Merged
merged 1 commit into from
Jan 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,17 +37,17 @@ soroban-token-spec = { version = "0.4.3", path = "soroban-token-spec" }
[workspace.dependencies.soroban-env-common]
version = "0.0.12"
git = "https://github.com/stellar/rs-soroban-env"
rev = "65498c8"
rev = "993c527abce72748405d71467498bffd63e061c1"

[workspace.dependencies.soroban-env-guest]
version = "0.0.12"
git = "https://github.com/stellar/rs-soroban-env"
rev = "65498c8"
rev = "993c527abce72748405d71467498bffd63e061c1"

[workspace.dependencies.soroban-env-host]
version = "0.0.12"
git = "https://github.com/stellar/rs-soroban-env"
rev = "65498c8"
rev = "993c527abce72748405d71467498bffd63e061c1"

[workspace.dependencies.stellar-strkey]
version = "0.0.6"
Expand Down
6 changes: 3 additions & 3 deletions soroban-auth/src/tests/test_ed25519.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ impl ExampleContract {
&env,
&sig,
symbol!("examplefn"),
(&sig.identifier(&env), arg1, arg2),
(sig.identifier(&env), arg1, arg2),
);
}
}
Expand Down Expand Up @@ -54,7 +54,7 @@ fn test() {
&signer,
&contract_id,
symbol!("examplefn"),
(&id, &1, &2),
(id, 1, 2),
);
std::println!("signature: {:?}", sig);

Expand Down Expand Up @@ -92,7 +92,7 @@ fn test_build_keypair() {
&signer,
&contract_id,
symbol!("examplefn"),
(&id, &1, &2),
(id, 1, 2),
);
std::println!("signature: {:?}", sig);

Expand Down
10 changes: 5 additions & 5 deletions soroban-auth/src/tests/test_ed25519_with_nonce.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ pub enum DataKey {

fn read_nonce(e: &Env, id: &Identifier) -> i128 {
let key = DataKey::Nonce(id.clone());
e.storage().get(key).unwrap_or(Ok(0)).unwrap()
e.storage().get(&key).unwrap_or(Ok(0)).unwrap()
}

fn verify_and_consume_nonce(e: &Env, id: &Identifier, expected_nonce: i128) {
Expand All @@ -29,7 +29,7 @@ fn verify_and_consume_nonce(e: &Env, id: &Identifier, expected_nonce: i128) {
if nonce != expected_nonce {
panic!("incorrect nonce")
}
e.storage().set(key, &nonce + 1);
e.storage().set(&key, &(nonce + 1));
}

pub struct TestContract;
Expand All @@ -41,7 +41,7 @@ impl TestContract {

verify_and_consume_nonce(&e, &auth_id, nonce);

verify(&e, &sig, symbol!("verify_sig"), (&auth_id, nonce));
verify(&e, &sig, symbol!("verify_sig"), (auth_id, nonce));
}

pub fn nonce(e: Env, id: Identifier) -> i128 {
Expand All @@ -54,7 +54,7 @@ pub struct OuterTestContract;
#[contractimpl]
impl OuterTestContract {
pub fn authorize(e: Env, contract_id: BytesN<32>) {
let client = TestContractClient::new(&e, contract_id);
let client = TestContractClient::new(&e, &contract_id);
client.verify_sig(&Signature::Invoker, &0);
}
}
Expand All @@ -74,7 +74,7 @@ fn test() {
&signer,
&contract_id,
symbol!("verify_sig"),
(&id, &nonce),
(id, nonce),
);

client.verify_sig(&sig, &nonce);
Expand Down
2 changes: 1 addition & 1 deletion soroban-sdk-macros/src/derive_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ pub fn derive_client(name: &str, fns: &[ClientFn]) -> TokenStream {
}

impl #client_ident {
pub fn new(env: &soroban_sdk::Env, contract_id: impl soroban_sdk::IntoVal<soroban_sdk::Env, soroban_sdk::BytesN<32>>) -> Self {
pub fn new(env: &soroban_sdk::Env, contract_id: &impl soroban_sdk::IntoVal<soroban_sdk::Env, soroban_sdk::BytesN<32>>) -> Self {
Self {
env: env.clone(),
contract_id: contract_id.into_val(env),
Expand Down
81 changes: 24 additions & 57 deletions soroban-sdk-macros/src/derive_enum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ pub fn derive_type_enum(
let mut errors = Vec::<Error>::new();

let variants = &data.variants;
let (spec_cases, discriminant_consts, try_froms, intos, try_from_xdrs, into_xdrs): (Vec<_>, Vec<_>, Vec<_>, Vec<_>, Vec<_>, Vec<_>) = variants
let (spec_cases, discriminant_consts, try_froms, try_intos, try_from_xdrs, into_xdrs): (Vec<_>, Vec<_>, Vec<_>, Vec<_>, Vec<_>, Vec<_>) = variants
.iter()
.map(|v| {
// TODO: Choose discriminant type based on repr type of enum.
Expand Down Expand Up @@ -72,7 +72,12 @@ pub fn derive_type_enum(
Self::#ident(iter.next().ok_or(#path::ConversionError)??.try_into_val(env)?)
}
};
let into = quote! { #enum_ident::#ident(ref value) => (#discriminant_const_sym_ident, value).into_val(env) };
let try_into = quote! {
#enum_ident::#ident(ref value) => {
let tup: (#path::RawVal, #path::RawVal) = (#discriminant_const_sym_ident.into(), value.try_into_val(env)?);
tup.try_into_val(env)
}
};
let try_from_xdr = quote! {
#name => {
if iter.len() > 1 {
Expand All @@ -83,7 +88,7 @@ pub fn derive_type_enum(
}
};
let into_xdr = quote! { #enum_ident::#ident(value) => (#name, value).try_into().map_err(|_| #path::xdr::Error::Invalid)? };
(spec_case, discriminant_const, try_from, into, try_from_xdr, into_xdr)
(spec_case, discriminant_const, try_from, try_into, try_from_xdr, into_xdr)
} else {
let spec_case = ScSpecUdtUnionCaseV0 {
name: name.try_into().unwrap_or_else(|_| StringM::default()),
Expand All @@ -97,7 +102,12 @@ pub fn derive_type_enum(
Self::#ident
}
};
let into = quote! { #enum_ident::#ident => (#discriminant_const_sym_ident,).into_val(env) };
let try_into = quote! {
#enum_ident::#ident => {
let tup: (#path::RawVal,) = (#discriminant_const_sym_ident.into(),);
tup.try_into_val(env)
}
};
let try_from_xdr = quote! {
#name => {
if iter.len() > 0 {
Expand All @@ -107,7 +117,7 @@ pub fn derive_type_enum(
}
};
let into_xdr = quote! { #enum_ident::#ident => (#name,).try_into().map_err(|_| #path::xdr::Error::Invalid)? };
(spec_case, discriminant_const, try_from, into, try_from_xdr, into_xdr)
(spec_case, discriminant_const, try_from, try_into, try_from_xdr, into_xdr)
}
})
.multiunzip();
Expand Down Expand Up @@ -150,7 +160,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<Self, Self::Error> {
fn try_from_val(env: &#path::Env, val: &#path::RawVal) -> Result<Self, Self::Error> {
use #path::TryIntoVal;
#(#discriminant_consts)*
let vec: #path::Vec<#path::RawVal> = val.try_into_val(env)?;
Expand All @@ -163,30 +173,14 @@ pub fn derive_type_enum(
}
}

impl #path::TryIntoVal<#path::Env, #enum_ident> for #path::RawVal {
graydon marked this conversation as resolved.
Show resolved Hide resolved
impl #path::TryFromVal<#path::Env, #enum_ident> for #path::RawVal {
type Error = #path::ConversionError;
#[inline(always)]
fn try_into_val(self, env: &#path::Env) -> Result<#enum_ident, Self::Error> {
<_ as #path::TryFromVal<_, _>>::try_from_val(env, self)
}
}

impl #path::IntoVal<#path::Env, #path::RawVal> for #enum_ident {
#[inline(always)]
fn into_val(self, env: &#path::Env) -> #path::RawVal {
#(#discriminant_consts)*
match &self {
#(#intos,)*
}
}
}

impl #path::IntoVal<#path::Env, #path::RawVal> for &#enum_ident {
#[inline(always)]
fn into_val(self, env: &#path::Env) -> #path::RawVal {
fn try_from_val(env: &#path::Env, val: &#enum_ident) -> Result<Self, Self::Error> {
use #path::TryIntoVal;
#(#discriminant_consts)*
match self {
#(#intos,)*
match val {
#(#try_intos,)*
}
}
}
Expand All @@ -195,7 +189,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<Self, Self::Error> {
fn try_from_val(env: &#path::Env, val: &#path::xdr::ScVec) -> Result<Self, Self::Error> {
use #path::xdr::Validate;
use #path::TryIntoVal;

Expand All @@ -211,20 +205,11 @@ pub fn derive_type_enum(
}
}

#[cfg(any(test, feature = "testutils"))]
impl #path::TryIntoVal<#path::Env, #enum_ident> for #path::xdr::ScVec {
type Error = #path::xdr::Error;
#[inline(always)]
fn try_into_val(self, env: &#path::Env) -> Result<#enum_ident, Self::Error> {
<_ as #path::TryFromVal<_, _>>::try_from_val(env, self)
}
}

#[cfg(any(test, feature = "testutils"))]
impl #path::TryFromVal<#path::Env, #path::xdr::ScObject> for #enum_ident {
type Error = #path::xdr::Error;
#[inline(always)]
fn try_from_val(env: &#path::Env, val: #path::xdr::ScObject) -> Result<Self, Self::Error> {
fn try_from_val(env: &#path::Env, val: &#path::xdr::ScObject) -> Result<Self, Self::Error> {
if let #path::xdr::ScObject::Vec(vec) = val {
<_ as #path::TryFromVal<_, _>>::try_from_val(env, vec)
} else {
Expand All @@ -233,20 +218,11 @@ pub fn derive_type_enum(
}
}

#[cfg(any(test, feature = "testutils"))]
impl #path::TryIntoVal<#path::Env, #enum_ident> for #path::xdr::ScObject {
type Error = #path::xdr::Error;
#[inline(always)]
fn try_into_val(self, env: &#path::Env) -> Result<#enum_ident, Self::Error> {
<_ as #path::TryFromVal<_, _>>::try_from_val(env, self)
}
}

#[cfg(any(test, feature = "testutils"))]
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<Self, Self::Error> {
fn try_from_val(env: &#path::Env, val: &#path::xdr::ScVal) -> Result<Self, Self::Error> {
if let #path::xdr::ScVal::Object(Some(obj)) = val {
<_ as #path::TryFromVal<_, _>>::try_from_val(env, obj)
} else {
Expand All @@ -255,15 +231,6 @@ pub fn derive_type_enum(
}
}

#[cfg(any(test, feature = "testutils"))]
impl #path::TryIntoVal<#path::Env, #enum_ident> for #path::xdr::ScVal {
type Error = #path::xdr::Error;
#[inline(always)]
fn try_into_val(self, env: &#path::Env) -> Result<#enum_ident, Self::Error> {
<_ as #path::TryFromVal<_, _>>::try_from_val(env, self)
}
}

#[cfg(any(test, feature = "testutils"))]
impl TryInto<#path::xdr::ScVec> for &#enum_ident {
type Error = #path::xdr::Error;
Expand Down
Loading