substrate常見問題整理

1 substrate debug版本執行過程不出塊

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<u8>

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 = (<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 國內源(官方 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
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容