substrate常见问题整理

1 substrate debug版本执行过程不出块

substrate常见问题整理_第1张图片
image.png

交易一直发送中,然后日志说产块时间过长?

Discarding proposal for slot 786483613; block production took too long 

使用release版本编译后运行

原因:出块流程里有很多超时检查,Debug版太慢。
Substrate 的 Debug 是双重慢,Wasm 用 Debug 编译慢,wasmi 用 Debug 编译第二重慢,Debug 版本的 wasmi 跑 Debug 版本的 Wasm runtime 双重慢

2 substrate的runtime如何引入外部包

比如使用大小端字节序的包byteorder
《 1 runtime/cargo.toml 尾部加

[dependencies]
byteorder = { version = "1.3.1", default-features = false }

《2 runtime/src/xxx.rs增加引用和使用的地方
//顶部导入字节序包

use byteorder::{ByteOrder, BigEndian};

//使用时

let dna_hash = BigEndian::read_u128(&_dna_hash_array[0..16]);

用的库必须支持 no_std,应该cargo.toml加个default-features = false

用的库必须支持no_std是什么意思?
wasm环境不支持std标准库,所以runtime 用到的所有库都必须支持没有std的编译环境

3 runtime如何使用类似字符串或byte数组类型

使用 Vec

4 runtime中的自动类型转换

 let index: T::KittyIndex = 0.into();
 let next = index + 1.into();

编译器就可以推测出要into成什么类型

5 runtime中的伪随机数生成

感觉目前比较好的做法是用 random_seed , nounce,sender三个内容拼起来再用sr_io::blake2_128方法进行hash,得到[u8; 16],最后用bytecodes的 read方法转成u128。
其中random_seed代表来自区块的随机信息,sender代表了用户的信息

// `nonce` and `random_hash` generation can stay here
            let nonce = Nonce::get();
            let random_hash = (>::random_seed(), &sender, nonce)
                .using_encoded(::Hashing::hash);
            let _dna_hash_array = random_hash.as_ref();
            let dna_hash = BigEndian::read_u128(&_dna_hash_array[0..16]);

6 substrate runtime cargo编译前下载资源加速

Rust.cc 国内源(官方 crates.io 加速源)
国内访问 crates.io 源太慢,有解决办法:Rustcc 联合 LongHash 提供了国内 Rust 开发者专属 crates.io 镜像。把下面内容填充到你的 ~/.cargo/config 文件中(没有就创建一个)。

[source.crates-io]
replace-with = "rustcc"

[source.rustcc]
registry = "https://code.aliyun.com/rustcc/crates.io-index.git"

7 执行“WASM_BUILD_TYPE=release cargo run -- --dev”提示 "Rust nightly not installed, please install it! "

发现有https://github.com/paritytech/polkadot/issues/455
,安装nightly构建版本的rust即可(rustup update nightly)

You need to install nightly. You installed stable. Please just run the init.sh script in scripts.
执行
rustup update nightly

8 centos7 执行“WASM_BUILD_TYPE=release cargo run -- --dev”提示 "thread 'main' panicked at 'Unable to find libclang: "

yum install clang

你可能感兴趣的:(substrate常见问题整理)