1 substrate debug版本執(zhí)行過(guò)程不出塊
交易一直發(fā)送中晚岭,然后日志說(shuō)產(chǎn)塊時(shí)間過(guò)長(zhǎng)碑隆?
Discarding proposal for slot 786483613; block production took too long
使用release版本編譯后運(yùn)行
原因:出塊流程里有很多超時(shí)檢查恭陡,Debug版太慢。
Substrate 的 Debug 是雙重慢上煤,Wasm 用 Debug 編譯慢休玩,wasmi 用 Debug 編譯第二重慢,Debug 版本的 wasmi 跑 Debug 版本的 Wasm runtime 雙重慢
2 substrate的runtime如何引入外部包
比如使用大小端字節(jié)序的包byteorder
《 1 runtime/cargo.toml 尾部加
[dependencies]
byteorder = { version = "1.3.1", default-features = false }
《2 runtime/src/xxx.rs增加引用和使用的地方
//頂部導(dǎo)入字節(jié)序包
use byteorder::{ByteOrder, BigEndian};
//使用時(shí)
let dna_hash = BigEndian::read_u128(&_dna_hash_array[0..16]);
用的庫(kù)必須支持 no_std,應(yīng)該cargo.toml
加個(gè)default-features = false
用的庫(kù)必須支持no_std是什么意思劫狠?
wasm環(huán)境不支持std標(biāo)準(zhǔn)庫(kù)拴疤,所以runtime 用到的所有庫(kù)都必須支持沒(méi)有std的編譯環(huán)境
3 runtime如何使用類似字符串或byte數(shù)組類型
使用 Vec<u8>
4 runtime中的自動(dòng)類型轉(zhuǎn)換
let index: T::KittyIndex = 0.into();
let next = index + 1.into();
編譯器就可以推測(cè)出要into成什么類型
5 runtime中的偽隨機(jī)數(shù)生成
感覺(jué)目前比較好的做法是用 random_seed , nounce,sender三個(gè)內(nèi)容拼起來(lái)再用sr_io::blake2_128方法進(jìn)行hash独泞,得到[u8; 16]呐矾,最后用bytecodes的 read方法轉(zhuǎn)成u128。
其中random_seed代表來(lái)自區(qū)塊的隨機(jī)信息懦砂,sender代表了用戶的信息
// `nonce` and `random_hash` generation can stay here
let nonce = Nonce::get();
let random_hash = (<system::Module<T>>::random_seed(), &sender, nonce)
.using_encoded(<T as system::Trait>::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 國(guó)內(nèi)源(官方 crates.io 加速源)
國(guó)內(nèi)訪問(wèn) crates.io 源太慢蜒犯,有解決辦法:Rustcc 聯(lián)合 LongHash 提供了國(guó)內(nèi) Rust 開發(fā)者專屬 crates.io 鏡像。把下面內(nèi)容填充到你的 ~/.cargo/config 文件中(沒(méi)有就創(chuàng)建一個(gè))荞膘。
[source.crates-io]
replace-with = "rustcc"
[source.rustcc]
registry = "https://code.aliyun.com/rustcc/crates.io-index.git"
7 執(zhí)行“WASM_BUILD_TYPE=release cargo run -- --dev”提示 "Rust nightly not installed, please install it! "
發(fā)現(xiàn)有https://github.com/paritytech/polkadot/issues/455
罚随,安裝nightly構(gòu)建版本的rust即可(rustup update nightly)
You need to install nightly. You installed stable. Please just run the init.sh script in scripts.
執(zhí)行
rustup update nightly
8 centos7 執(zhí)行“WASM_BUILD_TYPE=release cargo run -- --dev”提示 "thread 'main' panicked at 'Unable to find libclang: "
yum install clang