如何将标准Substrate extrinsic format解码为Transaction对象,以便能够获取Sender,最好是字符串?
我已经从这段代码开始,其中包含用于在extrinsic_hex变量中测试的硬编码样本外部数据:
use hex::decode;
use hex_literal::hex;
use parity_codec::{Decode, Encode, Input};
use primitives::generic::UncheckedMortalExtrinsic;
use std::fmt;
use std::fmt::Debug;
fn main() {
let extrinsic_hex: &'static str = "81ffd43593c715fdd31c61141abd04a99fd6822c8558854ccde39a5684e7a56da27d3c6b8941e2976034e67bdd1a999c3eff4403c8ceaf717f18d9760ab18573ab2ce870e9b751c2f14dd9883e54746e1eb6639978ceab49968c25176cc0d2507205040003000ca10f";
let result = hex::decode(extrinsic_hex);
match result {
Ok(v1) => {
let extr_option = UncheckedMortalExtrinsic::decode(&mut v1);
()
}
_ => {
println!("Error decoding");
()
}
}
}我得到的错误是:
error: duplicate lang item in crate `sr_io`: `panic_impl`.
|
= note: first defined in crate `std`.
error: duplicate lang item in crate `sr_io`: `oom`.
|
= note: first defined in crate `std`.
error[E0277]: the trait bound `std::vec::Vec<u8>: parity_codec::codec::Input` is not satisfied
--> core/decaddr/src/main.rs:13:20
|
13 | let extr_option=UncheckedMortalExtrinsic::decode(&mut v1);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `parity_codec::codec::Input` is not implemented for `std::vec::Vec<u8>`
|
= note: required by `parity_codec::codec::Decode::decode`让我们假设错误error: duplicate lang item in cratesr_io:panic_impl.暂时不存在。
如果我没理解错的话,它不能工作是因为Vec没有实现parity_codec::Input trait,对吗?如果是这样,如何将此特性添加到Vec中?或者更好地说,为了自动提供Input特征,我遗漏了底层框架的哪些功能?
发布于 2019-05-18 03:37:24
如果是这样的话,如何将这个特性添加到Vec中?
在你的代码中,你不能。出于一致性的原因,Rust说类型Y的特征X的实现必须存在于X的框或Y的框中。
或者更好地说,我遗漏了哪些来自Susbtrate框架的函数来自动提供输入特征?
如果你使用look at the "Implementors" part of the Input trait documentation -你可以看到每一个实现std::io::Read的类型都会实现Input。
正如评论所说,对您有用的实现是&[u8]上的实现,它之所以存在,是因为它实现了std::io::Read。
https://stackoverflow.com/questions/56189290
复制相似问题