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

Muxed address support sketch for Soroban SDK. #1441

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
355 changes: 185 additions & 170 deletions Cargo.lock

Large diffs are not rendered by default.

23 changes: 13 additions & 10 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,17 @@ soroban-ledger-snapshot = { version = "22.0.7", path = "soroban-ledger-snapshot"
soroban-token-sdk = { version = "22.0.7", path = "soroban-token-sdk" }

[workspace.dependencies.soroban-env-common]
version = "=22.1.3"
version = "=23.0.0"
#git = "https://github.com/stellar/rs-soroban-env"
#rev = "bd0c80a1fe171e75f8d745f17975a73927d44ecd"

[workspace.dependencies.soroban-env-guest]
version = "=22.1.3"
version = "=23.0.0"
#git = "https://github.com/stellar/rs-soroban-env"
#rev = "bd0c80a1fe171e75f8d745f17975a73927d44ecd"

[workspace.dependencies.soroban-env-host]
version = "=22.1.3"
version = "=23.0.0"
#git = "https://github.com/stellar/rs-soroban-env"
#rev = "bd0c80a1fe171e75f8d745f17975a73927d44ecd"

Expand All @@ -44,16 +44,19 @@ version = "=0.0.9"
[workspace.dependencies.stellar-xdr]
version = "=22.1.0"
default-features = false
features = ["curr"]
features = ["next"]
#git = "https://github.com/stellar/rs-stellar-xdr"
#rev = "67be5955a15f1d3a4df83fe86e6ae107f687141b"

#[patch.crates-io]
#soroban-env-common = { path = "../rs-soroban-env/soroban-env-common" }
#soroban-env-guest = { path = "../rs-soroban-env/soroban-env-guest" }
#soroban-env-host = { path = "../rs-soroban-env/soroban-env-host" }
#[patch."https://github.com/stellar/rs-stellar-xdr"]
#stellar-xdr = { path = "../rs-stellar-xdr/" }
[patch.crates-io]
#[patch."https://github.com/stellar/rs-soroban-env"]
soroban-env-common = { path = "../rs-soroban-env2/soroban-env-common" }
soroban-env-guest = { path = "../rs-soroban-env2/soroban-env-guest" }
soroban-env-host = { path = "../rs-soroban-env2/soroban-env-host" }
stellar-xdr = { path = "../rs-stellar-xdr/" }

[patch."https://github.com/stellar/rs-stellar-xdr"]
stellar-xdr = { path = "../rs-stellar-xdr/" }

[profile.dev]
overflow-checks = true
Expand Down
2 changes: 1 addition & 1 deletion soroban-sdk-macros/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ crate-git-revision = "0.0.6"
soroban-spec = { workspace = true }
soroban-spec-rust = { workspace = true }
soroban-env-common = { workspace = true }
stellar-xdr = { workspace = true, features = ["curr", "std"] }
stellar-xdr = { workspace = true, features = ["next", "std"] }
syn = {version="2.0.77",features=["full"]}
quote = "1.0"
proc-macro2 = "1.0"
Expand Down
2 changes: 1 addition & 1 deletion soroban-sdk-macros/src/derive_enum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use proc_macro2::{Literal, TokenStream as TokenStream2};
use quote::{format_ident, quote};
use syn::{spanned::Spanned, Attribute, DataEnum, Error, Fields, Ident, Path, Visibility};

use stellar_xdr::curr as stellar_xdr;
use stellar_xdr::next as stellar_xdr;
use stellar_xdr::{
Error as XdrError, ScSpecEntry, ScSpecTypeDef, ScSpecUdtUnionCaseTupleV0, ScSpecUdtUnionCaseV0,
ScSpecUdtUnionCaseVoidV0, ScSpecUdtUnionV0, StringM, VecM, WriteXdr, SCSYMBOL_LIMIT,
Expand Down
2 changes: 1 addition & 1 deletion soroban-sdk-macros/src/derive_enum_int.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use itertools::MultiUnzip;
use proc_macro2::TokenStream as TokenStream2;
use quote::{format_ident, quote};
use stellar_xdr::curr as stellar_xdr;
use stellar_xdr::next as stellar_xdr;
use stellar_xdr::{ScSpecUdtEnumV0, StringM};
use syn::{spanned::Spanned, Attribute, DataEnum, Error, ExprLit, Ident, Lit, Path, Visibility};

Expand Down
2 changes: 1 addition & 1 deletion soroban-sdk-macros/src/derive_error_enum_int.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use itertools::MultiUnzip;
use proc_macro2::TokenStream as TokenStream2;
use quote::{format_ident, quote};
use stellar_xdr::curr as stellar_xdr;
use stellar_xdr::next as stellar_xdr;
use stellar_xdr::{ScSpecEntry, ScSpecUdtErrorEnumCaseV0, ScSpecUdtErrorEnumV0, StringM, WriteXdr};
use syn::{spanned::Spanned, Attribute, DataEnum, Error, ExprLit, Ident, Lit, Path};

Expand Down
2 changes: 1 addition & 1 deletion soroban-sdk-macros/src/derive_spec_fn.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use proc_macro2::TokenStream as TokenStream2;
use quote::{format_ident, quote};
use stellar_xdr::curr as stellar_xdr;
use stellar_xdr::next as stellar_xdr;
use stellar_xdr::{
ScSpecEntry, ScSpecFunctionInputV0, ScSpecFunctionV0, ScSpecTypeDef, ScSymbol, StringM, VecM,
WriteXdr, SCSYMBOL_LIMIT,
Expand Down
2 changes: 1 addition & 1 deletion soroban-sdk-macros/src/derive_struct.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use proc_macro2::{Literal, TokenStream as TokenStream2};
use quote::{format_ident, quote};
use syn::{Attribute, DataStruct, Error, Ident, Path, Visibility};

use stellar_xdr::curr as stellar_xdr;
use stellar_xdr::next as stellar_xdr;
use stellar_xdr::{
ScSpecEntry, ScSpecTypeDef, ScSpecUdtStructFieldV0, ScSpecUdtStructV0, StringM, WriteXdr,
};
Expand Down
2 changes: 1 addition & 1 deletion soroban-sdk-macros/src/derive_struct_tuple.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use proc_macro2::{Literal, TokenStream as TokenStream2};
use quote::{format_ident, quote};
use syn::{Attribute, DataStruct, Error, Ident, Path, Visibility};

use stellar_xdr::curr as stellar_xdr;
use stellar_xdr::next as stellar_xdr;
use stellar_xdr::{
ScSpecEntry, ScSpecTypeDef, ScSpecUdtStructFieldV0, ScSpecUdtStructV0, StringM, WriteXdr,
};
Expand Down
2 changes: 1 addition & 1 deletion soroban-sdk-macros/src/doc.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use itertools::Itertools;
use stellar_xdr::curr as stellar_xdr;
use stellar_xdr::next as stellar_xdr;
use stellar_xdr::StringM;
use syn::{Attribute, Expr, ExprLit, Lit, Meta, MetaNameValue};

Expand Down
11 changes: 9 additions & 2 deletions soroban-sdk-macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ use syn_ext::HasFnsItem;

use soroban_spec_rust::{generate_from_wasm, GenerateFromFileError};

use stellar_xdr::curr as stellar_xdr;
use stellar_xdr::next as stellar_xdr;
use stellar_xdr::{Limits, ScMetaEntry, ScMetaV0, StringM, WriteXdr};

pub(crate) const DEFAULT_XDR_RW_LIMITS: Limits = Limits {
Expand Down Expand Up @@ -624,6 +624,8 @@ struct ContractImportArgs {
file: String,
#[darling(default)]
sha256: darling::util::SpannedValue<Option<String>>,
#[darling(default)]
expose_muxed_addresses: darling::util::SpannedValue<bool>,
}
#[proc_macro]
pub fn contractimport(metadata: TokenStream) -> TokenStream {
Expand All @@ -650,7 +652,12 @@ pub fn contractimport(metadata: TokenStream) -> TokenStream {
};

// Generate.
match generate_from_wasm(&wasm, &args.file, args.sha256.as_deref()) {
match generate_from_wasm(
&wasm,
&args.file,
args.sha256.as_deref(),
*args.expose_muxed_addresses.as_ref(),
) {
Ok(code) => quote! { #code },
Err(e @ GenerateFromFileError::VerifySha256 { .. }) => {
Error::new(args.sha256.span(), e.to_string()).into_compile_error()
Expand Down
5 changes: 3 additions & 2 deletions soroban-sdk-macros/src/map_type.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use stellar_xdr::curr as stellar_xdr;
use stellar_xdr::next as stellar_xdr;
use stellar_xdr::{
ScSpecTypeBytesN, ScSpecTypeDef, ScSpecTypeMap, ScSpecTypeOption, ScSpecTypeResult,
ScSpecTypeTuple, ScSpecTypeUdt, ScSpecTypeVec,
Expand Down Expand Up @@ -34,7 +34,8 @@ pub fn map_type(t: &Type, allow_hash: bool) -> Result<ScSpecTypeDef, Error> {
"String" => Ok(ScSpecTypeDef::String),
"Error" => Ok(ScSpecTypeDef::Error),
"Bytes" => Ok(ScSpecTypeDef::Bytes),
"Address" => Ok(ScSpecTypeDef::Address),
"Address" => Ok(ScSpecTypeDef::AddressV2(false)),
"MuxedAddress" => Ok(ScSpecTypeDef::AddressV2(true)),
"Timepoint" => Ok(ScSpecTypeDef::Timepoint),
"Duration" => Ok(ScSpecTypeDef::Duration),
s => Ok(ScSpecTypeDef::Udt(ScSpecTypeUdt {
Expand Down
2 changes: 1 addition & 1 deletion soroban-sdk/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ ctor = { version = "0.2.9", optional = true }
[dev-dependencies]
soroban-sdk-macros = { workspace = true, features = ["testutils"] }
soroban-env-host = { workspace = true, features = ["testutils"] }
stellar-xdr = { workspace = true, features = ["curr", "std"] }
stellar-xdr = { workspace = true, features = ["next", "std"] }
soroban-spec = { workspace = true }
ed25519-dalek = "2.0.0"
rand = "0.8.5"
Expand Down
7 changes: 3 additions & 4 deletions soroban-sdk/src/address.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ impl Debug for Address {
let strkey = Strkey::Contract(Contract(contract_id.0));
write!(f, "Contract({})", strkey.to_string())?;
}
xdr::ScAddress::MuxedAccount(_) => {
write!(f, "MuxedAccount()")?;
}
}
} else {
return Err(core::fmt::Error);
Expand All @@ -80,10 +83,6 @@ impl PartialOrd for Address {

impl Ord for Address {
fn cmp(&self, other: &Self) -> Ordering {
#[cfg(not(target_family = "wasm"))]
if !self.env.is_same_env(&other.env) {
return ScVal::from(self).cmp(&ScVal::from(other));
}
let v = self
.env
.obj_cmp(self.obj.to_val(), other.obj.to_val())
Expand Down
4 changes: 0 additions & 4 deletions soroban-sdk/src/bytes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -256,10 +256,6 @@ impl PartialOrd for Bytes {

impl Ord for Bytes {
fn cmp(&self, other: &Self) -> core::cmp::Ordering {
#[cfg(not(target_family = "wasm"))]
if !self.env.is_same_env(&other.env) {
return ScVal::from(self).cmp(&ScVal::from(other));
}
let v = self
.env
.obj_cmp(self.obj.to_val(), other.obj.to_val())
Expand Down
30 changes: 12 additions & 18 deletions soroban-sdk/src/env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,8 @@ use crate::{
};
use internal::{
AddressObject, Bool, BytesObject, DurationObject, I128Object, I256Object, I256Val, I64Object,
StorageType, StringObject, Symbol, SymbolObject, TimepointObject, U128Object, U256Object,
U256Val, U32Val, U64Object, U64Val, Void,
MuxedAddressObject, StorageType, StringObject, Symbol, SymbolObject, TimepointObject,
U128Object, U256Object, U256Val, U32Val, U64Object, U64Val, Void,
};

#[doc(hidden)]
Expand Down Expand Up @@ -354,6 +354,15 @@ impl Env {
internal::Env::require_auth(self, address.to_object()).unwrap_infallible();
}

#[doc(hidden)]
pub(crate) fn muxed_address_to_address(&self, muxed_address: MuxedAddressObject) -> Address {
Address::try_from_val(
self,
&internal::Env::muxed_address_to_address(self, muxed_address).unwrap_infallible(),
)
.unwrap_infallible()
}

/// Invokes a function of a contract that is registered in the [Env].
///
/// # Panics
Expand Down Expand Up @@ -449,14 +458,6 @@ impl Env {
}
}

#[doc(hidden)]
#[cfg(not(target_family = "wasm"))]
impl Env {
pub(crate) fn is_same_env(&self, other: &Self) -> bool {
self.env_impl.check_same_env(&other.env_impl).is_ok()
}
}

#[cfg(any(test, feature = "testutils"))]
use crate::testutils::cost_estimate::CostEstimate;
#[cfg(any(test, feature = "testutils"))]
Expand Down Expand Up @@ -514,7 +515,7 @@ impl Env {

let rf = Rc::new(EmptySnapshotSource());
let info = internal::LedgerInfo {
protocol_version: 22,
protocol_version: 23,
sequence_number: 0,
timestamp: 0,
network_id: [0; 32],
Expand Down Expand Up @@ -1812,13 +1813,6 @@ impl internal::EnvBase for Env {
match e {}
}

fn check_same_env(&self, other: &Self) -> Result<(), Self::Error> {
Ok(self
.env_impl
.check_same_env(&other.env_impl)
.unwrap_optimized())
}

fn bytes_copy_from_slice(
&self,
b: BytesObject,
Expand Down
3 changes: 3 additions & 0 deletions soroban-sdk/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -758,6 +758,7 @@ pub mod unwrap;
mod env;

mod address;
mod muxed_address;
mod symbol;

pub use env::{ConversionError, Env};
Expand Down Expand Up @@ -815,10 +816,12 @@ mod map;
pub mod prng;
pub mod storage;
pub mod token;
pub mod mux_token;
mod vec;
pub use address::Address;
pub use bytes::{Bytes, BytesN};
pub use map::Map;
pub use muxed_address::MuxedAddress;
pub use symbol::Symbol;
pub use vec::Vec;
mod num;
Expand Down
4 changes: 0 additions & 4 deletions soroban-sdk/src/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,10 +139,6 @@ where
V: IntoVal<Env, Val> + TryFromVal<Env, Val>,
{
fn cmp(&self, other: &Self) -> core::cmp::Ordering {
#[cfg(not(target_family = "wasm"))]
if !self.env.is_same_env(&other.env) {
return ScVal::from(self).cmp(&ScVal::from(other));
}
let v = self
.env
.obj_cmp(self.obj.to_val(), other.obj.to_val())
Expand Down
Loading
Loading