问题描述
我正在尝试将基板教程 (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);
编辑了 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 (将#修改为@)