Looking into CCs compiled wasm format

I am looking into doing a toy-project on Concordium - compiling a simple language into .wasm, and using it for smart contracts.

When looking at generated .wasm files (for example from the piggy_bank example), I see a set of imported functions:

  (import "concordium" "get_receive_sender" (func $_ZN14concordium_std5prims18get_receive_sender17hd39205c7d0dcf66aE (type 3)))
  (import "concordium" "get_receive_self_balance" (func $_ZN14concordium_std5prims24get_receive_self_balance17hc54893914a7c6d9dE (type 4)))
  (import "concordium" "state_entry_size" (func $_ZN14concordium_std5prims16state_entry_size17h25c60b53484a3ddfE (type 5)))
  (import "concordium" "write_output" (func $_ZN14concordium_std5prims12write_output17hed3dd8123a9ee388E (type 2)))
  (import "concordium" "state_entry_resize" (func $_ZN14concordium_std5prims18state_entry_resize17h3ec61de16d4731c9E (type 6)))
  (import "concordium" "state_entry_read" (func $_ZN14concordium_std5prims16state_entry_read17he52c5fb6878ae74fE (type 7)))
  (import "concordium" "state_entry_write" (func $_ZN14concordium_std5prims17state_entry_write17hd471dfecd7904772E (type 7)))
  (import "concordium" "state_create_entry" (func $_ZN14concordium_std5prims18state_create_entry17h95c30732be73dd73E (type 8)))
  (import "concordium" "state_lookup_entry" (func $_ZN14concordium_std5prims18state_lookup_entry17h0bff202e4235f703E (type 8)))
  (import "concordium" "invoke" (func $_ZN14concordium_std5prims6invoke17h829d429c5bbf3d23E (type 9)))

These functions seems to be documented here: Contract host functions — Concordium documentation

And also a set of exports (which are the functions from the smart contract itself). But there are also several functions that only have a definition and no impl., meaning that they are linked somehow from another wasm module - they are also from the concordium rust std lib:

(func $_ZN14concordium_std5impls153_$LT$impl$u20$concordium_std..traits..HasInitContext$u20$for$u20$concordium_std..types..ExternContext$LT$concordium_std..types..ExternInitContext$GT$$GT$4open17h3517201963d84a13E (type 13))

Now - before I start to dive into the code - will it even be possible to make a valid Concordim .wasm file without using the rust compiler ? If I generate a .wasm file that adheres to the “limitations” as described here: Smart contract modules — Concordium documentation - will I actually be able to deploy it on chain ?

I just started to look into it, and would like somebody from the dev team tell me, if this can be done, or if Concordium is still too early in the development to break out from the default build chain. I hope not, because I have a few cool ideas I want to try out.

Yes, this can certainly be done. There is no “rust” on the chain itself. It’s only Wasm.

For example we write a number of test contract directly in .wat (a text format for wasm).

You can look at them here https://github.com/Concordium/concordium-base/tree/main/smart-contracts/testdata/contracts/v1

Note that these are mainly for specific unit tests, they are not very useful contracts.

For example here is a test for transfers https://github.com/Concordium/concordium-base/blob/main/smart-contracts/testdata/contracts/v1/transfer.wat

Note that when you build a contract with just cargo you’ll typically get a lot of debug information embedded in the wasm file.

Right. Thanks for pointing me to these links. Things make much more sense now.

I have a question - the invoke host function is used extensively:

concordium.invoke(*tag* , *start* , *length* ) → i64

But - in the docs (Contract host functions — Concordium documentation), only tag 0-4 is documented, but I see in some of the samples usage of tag 6, for example:

      ;; query account keys, tag of the operation is 6.
      (call $invoke (i32.const 6) (i32.const 0) (i32.const 32))

Where can I lookup all the tags available for that function?

Yeah, the documentation seems a bit out of date there.

That is the link to the implementation which is authoritative :slight_smile:

1 Like