ask-lang / ask

The framework to write WASM smart contracts for Substrate Frame Pallet-Contracts in AssemblyScript.
https://ask-lang.github.io/ask-docs/
MIT License
32 stars 3 forks source link

CodeRejected: module imports a non-existent function #243

Open ashutoshvarma opened 1 year ago

ashutoshvarma commented 1 year ago

Problem: -

Error while deploying contracts that contains methods that deletes storage states. That includes operation like Mapping.delete() that call env().clearContractStroage().

Temporary Fix: -

Commented out the new seal_clear_storage from __unstable__.ts, that overshadows the old seal_clear_storage from seal0.ts

https://github.com/ask-lang/ask/blob/a1bd060593b247bc0e2a9d0b5bc88f95ec3fdfa1/as-packages/as-contract-runtime/assembly/unstable.ts#L34-L35

Substrate Contracts Node Log

v0.22

 DEBUG tokio-runtime-worker runtime::contracts: CodeRejected: module imports a non-existent function

v0.23

 DEBUG tokio-runtime-worker runtime::contracts: failed to instantiate code: cannot find definition for import __unstable__::seal_clear_storage: Func(DedupFuncType(GuardedEntity { guard_idx: EngineIdx(2), entity_idx: DedupFuncTypeIdx(0) }))

Steps to reproduce: -

Build and deploy any contract that has delete() method. Below is the minimal reproduction contract.

import { HashKeccak256, AccountId, Mapping } from "ask-lang";

@contract()
export class Contract {
    _map: Mapping<AccountId, u32, HashKeccak256> = new Mapping();

    @constructor()
    default(): void {}

    @message({ mutates: true })
    remove(k: AccountId): void {
        return this._map.delete(k);
    }
}

Notes (Not related to this issue) :-

Cannot deploy any contract build with ark-lang v0.4.0 on latest substrate-contracts-node v0.23. Always fails with below error.

DEBUG tokio-runtime-worker runtime::contracts: failed to instantiate code: found an unexpected start function with index 58

Maybe related to paritytech/substrate#207

yjhmelody commented 1 year ago

found an unexpected start function with index 58 this error means that wasm code exported a start function which is illegal in contract

ashutoshvarma commented 1 year ago

@yjhmelody did you get a chance to take a look at the fix (or the one suggested in this issue ).

ashutoshvarma commented 1 year ago

@yjhmelody Just wanted to bump this since this is a critical bug. The ask! v0.4.0 cannot be used with new pallet-contracts (polkadot-v0.9.32 or higher) due to: -

I believe it should be top priority as without fixing this ask! is essentially broken for all major parachains.

yjhmelody commented 1 year ago

Ok, will be updated

yjhmelody commented 1 year ago

AS always export start function (for init stdlib) which is now illegal in contracts (new AS supports --exportStart which can be used to export start function as either deploy() or call(). Not sure how will that work)

That's not true. You should config it such as:

{
    "targets": {
        "debug": {
            "sourceMap": true,
            "debug": true
        },
        "release": {
            "sourceMap": false,
            "optimizeLevel": 3,
            "shrinkLevel": 2,
            "converge": false,
            "noAssert": false
        }
    },
    "options": {
        "transform": ["ask-transform", "as-serde-transform"],
        "importMemory": true,
        "initialMemory": 2,
        "maximumMemory": 16,
        "noExportMemory": true,
        "runtime": "stub",
        "use": "abort=",
        "disable": ["Sign-extension"]
    }
}

or inherent this config:

    "extends": "ask-lang/asconfig.json",

I did not see start function when I did the above.

yjhmelody commented 1 year ago

Oh, I misunderstood, indeed, I need to study further

yjhmelody commented 1 year ago

Related issues about start functions: