微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何将 Substrate 本机接口导出到运行时?

如何解决如何将 Substrate 本机接口导出到运行时?

我正在尝试将基板教程 (https://github.com/substrate-developer-hub/substrate-node-template) 链接到某个 C 库。我将 C 库包装到一个名为 cfun 的 crate 中,它可以被节点层调用

我发现我可以使用 marco runtime_interface 导出本机接口以供我的托盘调用。但是,虽然可以编译成功,但是wasm运行时无法实例化。

知道发生了什么吗?

// node/cfun/src/lib.rs
#![cfg_attr(not(feature = "std"),no_std)]

#[link(name = "test",kind = "static")]
extern "C" {
    pub fn double_int(input: i32) -> i32;
    pub fn double_uint(input: u32) -> u32;
}
# node/cint/Cargo.toml
[package]
edition = '2018'
name = 'cint'
version = '2.0.1'

[dependencies]
# local dependencies
sp-runtime-interface = { version = "2.0.1",default-features = false}
cfun = { path = '../cfun',default-features = false,version = '2.0.1',optional = true }

[features]
default = ['std']
std = [
    'sp-runtime-interface/std','cfun',]
// node/cint/src/lib.rs
#![cfg_attr(not(feature = "std"),no_std)]

#[cfg(feature = "std")]
extern crate cfun;

use sp_runtime_interface::runtime_interface;

#[runtime_interface]
pub trait CFunctions {
    fn c_double_uint(input: u32) -> u32 {
        unsafe {
            cfun::double_uint(input)
        }
    }
}
// pallets/template/src/lib.rs

// import module at the beginning
extern crate cint;

// ...
// modify the original `do_something` function
// from:
// Something::put(something);
// to:
let smt = cint::c_functions::c_double_uint(something);
Something::put(smt);

runtime error

编辑了 1

# pallets/template.Cargo.toml and runtime/Cargo.toml
# path is a bit different,but both included all interfaces

[dependencies]
# ...
sp-runtime-interface = { version = "2.0.1",default-features = false}
cfun = { path = '../../node/cfun',optional = true }
cint = { path = '../../node/cint',version = '2.0.1'}

[features]
default = ['std']
std = [
    # ...
    'sp-runtime/std','cint/std',]

回购: https://github.com/killalau/substrate-node-template/tree/franky

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。