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

Allow contractimpls across mods #1322

Merged
merged 9 commits into from
Sep 10, 2024
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
7 changes: 7 additions & 0 deletions Cargo.lock

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

5 changes: 2 additions & 3 deletions soroban-sdk-macros/src/derive_fn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -181,9 +181,8 @@ pub fn derive_contract_function_registration_ctor<'a>(
})
.multiunzip();

let ty_str = quote!(#ty).to_string();
let ty_str = quote!(#ty).to_string().replace(' ', "").replace(':', "_");
let trait_str = quote!(#trait_ident).to_string();
let fn_set_registry_ident = format_ident!("__{ty_str}_fn_set_registry");
let methods_hash = format!("{:x}", Sha256::digest(idents.join(",").as_bytes()));
let ctor_ident = format_ident!("__{ty_str}_{trait_str}_{methods_hash}_ctor");

Expand All @@ -192,7 +191,7 @@ pub fn derive_contract_function_registration_ctor<'a>(
#[#crate_path::reexports_for_macros::ctor::ctor]
fn #ctor_ident() {
#(
#fn_set_registry_ident::register(
<#ty as #crate_path::testutils::ContractFunctionRegister>::register(
#idents,
#[allow(deprecated)]
&#wrap_idents::invoke_raw_slice,
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
Expand Up @@ -15,7 +15,7 @@ use crate::{doc::docs_from_attrs, map_type::map_type, DEFAULT_XDR_RW_LIMITS};

#[allow(clippy::too_many_arguments)]
pub fn derive_fn_spec(
ty: &Ident,
ty: &Type,
ident: &Ident,
attrs: &[Attribute],
inputs: &Punctuated<FnArg, Comma>,
Expand Down
13 changes: 9 additions & 4 deletions soroban-sdk-macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ fn default_crate_path() -> Path {

#[derive(Debug, FromMeta)]
struct ContractSpecArgs {
name: String,
name: Type,
export: Option<bool>,
}

Expand All @@ -87,10 +87,9 @@ pub fn contractspecfn(metadata: TokenStream, input: TokenStream) -> TokenStream
let methods: Vec<_> = item.fns();
let export = args.export.unwrap_or(true);

let ty = format_ident!("{}", args.name);
let derived: Result<proc_macro2::TokenStream, proc_macro2::TokenStream> = methods
.iter()
.map(|m| derive_fn_spec(&ty, m.ident, m.attrs, m.inputs, m.output, export))
.map(|m| derive_fn_spec(&args.name, m.ident, m.attrs, m.inputs, m.output, export))
.collect();

match derived {
Expand Down Expand Up @@ -150,7 +149,7 @@ pub fn contract(metadata: TokenStream, input: TokenStream) -> TokenStream {
use std::sync::Mutex;
use std::collections::BTreeMap;

type F = dyn Send + Sync + Fn(#crate_path::Env, &[#crate_path::Val]) -> #crate_path::Val;
pub(crate) type F = #crate_path::testutils::ContractFunctionF;

static FUNCS: Mutex<BTreeMap<&'static str, &'static F>> = Mutex::new(BTreeMap::new());

Expand All @@ -164,6 +163,12 @@ pub fn contract(metadata: TokenStream, input: TokenStream) -> TokenStream {
}
}

impl #crate_path::testutils::ContractFunctionRegister for #ty {
fn register(name: &'static str, func: &'static #fn_set_registry_ident::F) {
#fn_set_registry_ident::register(name, func);
}
}

#[doc(hidden)]
impl #crate_path::testutils::ContractFunctionSet for #ty {
fn call(&self, func: &str, env: #crate_path::Env, args: &[#crate_path::Val]) -> Option<#crate_path::Val> {
Expand Down
6 changes: 6 additions & 0 deletions soroban-sdk/src/testutils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,12 @@ impl Generators {
}
}

#[doc(hidden)]
pub type ContractFunctionF = dyn Send + Sync + Fn(Env, &[Val]) -> Val;
#[doc(hidden)]
pub trait ContractFunctionRegister {
fn register(name: &'static str, func: &'static ContractFunctionF);
}
#[doc(hidden)]
pub trait ContractFunctionSet {
fn call(&self, func: &str, env: Env, args: &[Val]) -> Option<Val>;
Expand Down
18 changes: 18 additions & 0 deletions tests/modular/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
[package]
name = "test_modular"
version.workspace = true
authors = ["Stellar Development Foundation <info@stellar.org>"]
license = "Apache-2.0"
edition = "2021"
publish = false
rust-version.workspace = true

[lib]
crate-type = ["cdylib"]
doctest = false

[dependencies]
soroban-sdk = {path = "../../soroban-sdk"}

[dev-dependencies]
soroban-sdk = {path = "../../soroban-sdk", features = ["testutils"]}
11 changes: 11 additions & 0 deletions tests/modular/src/feat1.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
use soroban_sdk::contractimpl;

use crate::Contract;
use crate::ContractClient;

#[contractimpl]
impl Contract {
pub fn one() -> u32 {
1
}
}
10 changes: 10 additions & 0 deletions tests/modular/src/feat2.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
use soroban_sdk::contractimpl;

use crate::ContractClient;

#[contractimpl]
impl super::Contract {
pub fn two() -> u32 {
2
}
}
16 changes: 16 additions & 0 deletions tests/modular/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#![no_std]
use soroban_sdk::{contract, contractimpl};

mod feat1;
mod feat2;
mod test;

#[contract]
pub struct Contract;

#[contractimpl]
impl Contract {
pub fn zero() -> u32 {
0
}
}
16 changes: 16 additions & 0 deletions tests/modular/src/test.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#![cfg(test)]

use crate::{Contract, ContractClient};
use soroban_sdk::Env;

#[test]
fn test() {
let env = Env::default();

let id = env.register_contract(None, Contract);
let client = ContractClient::new(&env, &id);

assert_eq!(client.zero(), 0);
assert_eq!(client.one(), 1);
assert_eq!(client.two(), 2);
}
219 changes: 219 additions & 0 deletions tests/modular/test_snapshots/test/test.1.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,219 @@
{
"generators": {
"address": 1,
"nonce": 0
},
"auth": [
[],
[],
[]
],
"ledger": {
"protocol_version": 21,
"sequence_number": 0,
"timestamp": 0,
"network_id": "0000000000000000000000000000000000000000000000000000000000000000",
"base_reserve": 0,
"min_persistent_entry_ttl": 4096,
"min_temp_entry_ttl": 16,
"max_entry_ttl": 6312000,
"ledger_entries": [
[
{
"contract_data": {
"contract": "CAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAD2KM",
"key": "ledger_key_contract_instance",
"durability": "persistent"
}
},
[
{
"last_modified_ledger_seq": 0,
"data": {
"contract_data": {
"ext": "v0",
"contract": "CAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAD2KM",
"key": "ledger_key_contract_instance",
"durability": "persistent",
"val": {
"contract_instance": {
"executable": {
"wasm": "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"
},
"storage": null
}
}
}
},
"ext": "v0"
},
4095
]
],
[
{
"contract_code": {
"hash": "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"
}
},
[
{
"last_modified_ledger_seq": 0,
"data": {
"contract_code": {
"ext": "v0",
"hash": "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855",
"code": ""
}
},
"ext": "v0"
},
4095
]
]
]
},
"events": [
{
"event": {
"ext": "v0",
"contract_id": null,
"type_": "diagnostic",
"body": {
"v0": {
"topics": [
{
"symbol": "fn_call"
},
{
"bytes": "0000000000000000000000000000000000000000000000000000000000000001"
},
{
"symbol": "zero"
}
],
"data": "void"
}
}
},
"failed_call": false
},
{
"event": {
"ext": "v0",
"contract_id": "0000000000000000000000000000000000000000000000000000000000000001",
"type_": "diagnostic",
"body": {
"v0": {
"topics": [
{
"symbol": "fn_return"
},
{
"symbol": "zero"
}
],
"data": {
"u32": 0
}
}
}
},
"failed_call": false
},
{
"event": {
"ext": "v0",
"contract_id": null,
"type_": "diagnostic",
"body": {
"v0": {
"topics": [
{
"symbol": "fn_call"
},
{
"bytes": "0000000000000000000000000000000000000000000000000000000000000001"
},
{
"symbol": "one"
}
],
"data": "void"
}
}
},
"failed_call": false
},
{
"event": {
"ext": "v0",
"contract_id": "0000000000000000000000000000000000000000000000000000000000000001",
"type_": "diagnostic",
"body": {
"v0": {
"topics": [
{
"symbol": "fn_return"
},
{
"symbol": "one"
}
],
"data": {
"u32": 1
}
}
}
},
"failed_call": false
},
{
"event": {
"ext": "v0",
"contract_id": null,
"type_": "diagnostic",
"body": {
"v0": {
"topics": [
{
"symbol": "fn_call"
},
{
"bytes": "0000000000000000000000000000000000000000000000000000000000000001"
},
{
"symbol": "two"
}
],
"data": "void"
}
}
},
"failed_call": false
},
{
"event": {
"ext": "v0",
"contract_id": "0000000000000000000000000000000000000000000000000000000000000001",
"type_": "diagnostic",
"body": {
"v0": {
"topics": [
{
"symbol": "fn_return"
},
{
"symbol": "two"
}
],
"data": {
"u32": 2
}
}
}
},
"failed_call": false
}
]
}
Loading