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

refactor: add client entity to smart contracts #5073

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

mversic
Copy link
Contributor

@mversic mversic commented Sep 13, 2024

Context

  • introduce a host handle that smart contract writers use to interact with the host environment
  • introduce context of the execution that every smart contract is given (in the future this should be header of the block that is currently executing)
  • change all smart contract functions to receive (host, context)
  • rename Validate trait to Execute

Closes #4898

Migration guide

  • this is a breaking change for all smart contracts

Previous API:

#[iroha_trigger::main]
fn main(id: TriggerId, owner: AccountId, event: EventBox) {
    let EventBox::ExecuteTrigger(event) = event else {
        dbg_panic("Only work as by call trigger");
    };

    let rose_id = AssetId::new("rose#wonderland".parse().unwrap(), owner);
    let val: u32 = query_single(FindTriggerMetadata::new(id, "VAL".parse().unwrap()))
}

New API:

#[iroha_trigger::main]
fn main(host: Iroha, context: Context) {
    let EventBox::ExecuteTrigger(event) = context.event else {
        dbg_panic("Only work as by call trigger");
    };

    let rose_id = AssetId::new("rose#wonderland".parse().unwrap(), context.authority);
    let val: u32 = host
        .query_single(FindTriggerMetadata::new(context.id, "VAL".parse().unwrap()))
        .dbg_unwrap()
        .try_into_any()
        .dbg_unwrap();
}

@mversic mversic force-pushed the wasm_client branch 3 times, most recently from c27b520 to b1328d9 Compare September 13, 2024 02:11
@github-actions github-actions bot added the api-changes Changes in the API for client libraries label Sep 13, 2024
let account_id = args.account.id().clone();

Register::account(args.account)
.execute()
host.submit(&Register::account(args.account))
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the unfortunate difference from iroha client is that we take instruction by reference instead of value

@@ -15,17 +15,18 @@ static ALLOC: GlobalDlmalloc = GlobalDlmalloc;

getrandom::register_custom_getrandom!(iroha_executor::stub_getrandom);

#[derive(Constructor, ValidateEntrypoints, Validate, Visit)]
#[derive(Constructor, Visit, Execute, Entrypoints)]
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMO carries too much mental overhead for the end user. Can we simplify it?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If they always used together we can combine them.

@mversic mversic force-pushed the wasm_client branch 3 times, most recently from 7e6885c to 8cec7c0 Compare September 13, 2024 10:57
@mversic mversic assigned mversic, DCNick3 and 0x009922 and unassigned 0x009922 Sep 15, 2024
@Erigara Erigara self-assigned this Sep 19, 2024
Copy link
Contributor

@Erigara Erigara left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think since this is breaking change for smart-contracts, migration guide should be provided.

@@ -15,17 +15,18 @@ static ALLOC: GlobalDlmalloc = GlobalDlmalloc;

getrandom::register_custom_getrandom!(iroha_executor::stub_getrandom);

#[derive(Constructor, ValidateEntrypoints, Validate, Visit)]
#[derive(Constructor, Visit, Execute, Entrypoints)]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If they always used together we can combine them.

wasm_samples/mint_rose_trigger/src/lib.rs Show resolved Hide resolved
@mversic mversic force-pushed the wasm_client branch 2 times, most recently from c86c808 to 1cc3747 Compare September 23, 2024 05:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api-changes Changes in the API for client libraries
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Introduce a client-like object for smart contracts
4 participants