如何在 pallet 中使用 serde_json?
How to use serde_json in pallet?
在我的托盘依赖项中使用 serde_json 时会抛出此错误:-
``
#[weight=10_000]
pub fn insert_data_class(origin,app_id:Vec<u8>,db_id:Vec<u8>,cid:Vec<u8>,data:Vec<u8>,signature:H512)->DispatchResult{
let caller = ensure_signed(origin)?;
let app_data = alioth_register::Module::<T>::check_ownership(&caller,&app_id);
ensure!(app_data.0,Error::<T>::AppNotFound);
let data_str = String::from_utf8(data.clone()).unwrap();
let data_json = json!(data_str);
let uid:Vec<u8> = data_json["uid"].to_string().as_bytes().to_vec();
//let uuid_str = data_json["uuid"].to_string();
let uuid:Vec<u8> = data_json["uuid"].to_string().as_bytes().to_vec();
let data1 = data.clone();
let data1 = String::from_utf8(data1).unwrap();
let user_data = alioth_users::Module::<T>::check_user_extern(&uuid,&app_id);
ensure!(user_data.0,Error::<T>::UserNotFound);
let class_data = alioth_class::Module::<T>::check_class(&db_id,&cid);
ensure!(class_data.0,Error::<T>::ClassOrDatabaseNotFound);
//I needed a public key for this purpose we are going to write a function in alioth-users
let pub_key_ref:(bool,H256) = alioth_users::Module::<T>::get_public_key(&uuid,&app_id);
ensure!(pub_key_ref.0,Error::<T>::ObjectNotCreated);
//now we are going to verify the signature for the further transaction
ensure!(sp_io::crypto::sr25519_verify(&Signature::from_h512(signature),data1.as_bytes(),&Public::from_h256(pub_key_ref.1)),Error::<T>::SignatureNotVerified);
//its over now we are going ahead to check the uniqness of data and insert it later.
let data_from_self = Self::check_obj_store(&uid,&cid);
ensure!(!data_from_self.0,Error::<T>::DataIsNotUnique);
let struct_data = ObjectData{
duid:uid,
object:data,
};
if data_from_self.1==0{
let vec_data = vec![struct_data];
<ObjectStore>::insert(cid,vec_data);
Self::deposit_event(Event::ObjectCreated());
}
else{
let mut vec_data = <ObjectStore>::get(&cid);
vec_data.insert(vec_data.len(),struct_data);
<ObjectStore>::insert(cid,vec_data);
Self::deposit_event(Event::ObjectCreated());
}
Ok(())
}**this code uses serde_json::json macro**
错误:运行 为 node-template-runtime v3.0.0 (/home/corteri/Desktop/blockchain/new_tmp/substrate-node-template/runtime)
自定义构建命令失败
原因:
进程未成功退出:/home/corteri/Desktop/blockchain/new_tmp/substrate-node-template/target/debug/build/node-template-runtime-e56bc02a2834241f/build-script-build
(退出状态:1)
--- 标准输出
错误报告中应包含的信息。
执行构建命令:"rustup" "运行" "nightly" "cargo" "-Zfeatures=build_dep" "rustc" "--target=wasm32-unknown-unknown" "--manifest-path =/home/corteri/Desktop/blockchain/new_tmp/substrate-node-template/target/debug/wbuild/node-template-runtime/Cargo.toml" "--color=always" "--release"
使用 rustc 版本:rustc 1.56.0-nightly (b7404c898 2021-09-03)
--- 标准错误
警告:标志 -Z features
已在 1.51 版本中稳定,不再需要
现在可以通过在 Cargo.toml 中指定 resolver = "2"
来使用新功能解析器。
有关详细信息,请参阅 https://doc.rust-lang.org/nightly/cargo/reference/features.html#feature-resolver-version-2。
Compiling getrandom v0.2.1
错误:不支持目标,有关详细信息,请参阅:https://docs.rs/getrandom/#unsupported-targets
--> /home/corteri/.cargo/registry/src/github.com-1ecc6299db9ec823/getrandom-0.2.1/src/lib.rs:214:9
|
214 | / compile_error!("不支持目标,有关详细信息,请参阅:
215 | | https://docs.rs/getrandom/#unsupported-targets");
| |_________________________________________________________________________^
错误[E0433]:未能解决:使用未声明的板条箱或模块imp
--> /home/corteri/.cargo/registry/src/github.com-1ecc6299db9ec823/getrandom-0.2.1/src/lib.rs:236:5
|
236 | imp::getrandom_inner(目的地)
| ^^^ 使用未声明的板条箱或模块 imp
有关此错误的更多信息,请尝试 rustc --explain E0433
。
错误:由于之前的 2 个错误
,无法编译 getrandom
getrandom/js 功能需要在您的 Cargo.toml:
中激活
# We enable it only for web-wasm check
# See https://docs.rs/getrandom/0.2.1/getrandom/#webassembly-support
getrandom = { version = "0.2", features = ["js"] }
(顺便说一句,我认为这不是 serde_json 特定错误)
在我的托盘依赖项中使用 serde_json 时会抛出此错误:- ``
#[weight=10_000]
pub fn insert_data_class(origin,app_id:Vec<u8>,db_id:Vec<u8>,cid:Vec<u8>,data:Vec<u8>,signature:H512)->DispatchResult{
let caller = ensure_signed(origin)?;
let app_data = alioth_register::Module::<T>::check_ownership(&caller,&app_id);
ensure!(app_data.0,Error::<T>::AppNotFound);
let data_str = String::from_utf8(data.clone()).unwrap();
let data_json = json!(data_str);
let uid:Vec<u8> = data_json["uid"].to_string().as_bytes().to_vec();
//let uuid_str = data_json["uuid"].to_string();
let uuid:Vec<u8> = data_json["uuid"].to_string().as_bytes().to_vec();
let data1 = data.clone();
let data1 = String::from_utf8(data1).unwrap();
let user_data = alioth_users::Module::<T>::check_user_extern(&uuid,&app_id);
ensure!(user_data.0,Error::<T>::UserNotFound);
let class_data = alioth_class::Module::<T>::check_class(&db_id,&cid);
ensure!(class_data.0,Error::<T>::ClassOrDatabaseNotFound);
//I needed a public key for this purpose we are going to write a function in alioth-users
let pub_key_ref:(bool,H256) = alioth_users::Module::<T>::get_public_key(&uuid,&app_id);
ensure!(pub_key_ref.0,Error::<T>::ObjectNotCreated);
//now we are going to verify the signature for the further transaction
ensure!(sp_io::crypto::sr25519_verify(&Signature::from_h512(signature),data1.as_bytes(),&Public::from_h256(pub_key_ref.1)),Error::<T>::SignatureNotVerified);
//its over now we are going ahead to check the uniqness of data and insert it later.
let data_from_self = Self::check_obj_store(&uid,&cid);
ensure!(!data_from_self.0,Error::<T>::DataIsNotUnique);
let struct_data = ObjectData{
duid:uid,
object:data,
};
if data_from_self.1==0{
let vec_data = vec![struct_data];
<ObjectStore>::insert(cid,vec_data);
Self::deposit_event(Event::ObjectCreated());
}
else{
let mut vec_data = <ObjectStore>::get(&cid);
vec_data.insert(vec_data.len(),struct_data);
<ObjectStore>::insert(cid,vec_data);
Self::deposit_event(Event::ObjectCreated());
}
Ok(())
}**this code uses serde_json::json macro**
错误:运行 为 node-template-runtime v3.0.0 (/home/corteri/Desktop/blockchain/new_tmp/substrate-node-template/runtime)
原因:
进程未成功退出:/home/corteri/Desktop/blockchain/new_tmp/substrate-node-template/target/debug/build/node-template-runtime-e56bc02a2834241f/build-script-build
(退出状态:1)
--- 标准输出
错误报告中应包含的信息。
执行构建命令:"rustup" "运行" "nightly" "cargo" "-Zfeatures=build_dep" "rustc" "--target=wasm32-unknown-unknown" "--manifest-path =/home/corteri/Desktop/blockchain/new_tmp/substrate-node-template/target/debug/wbuild/node-template-runtime/Cargo.toml" "--color=always" "--release"
使用 rustc 版本:rustc 1.56.0-nightly (b7404c898 2021-09-03)
--- 标准错误
警告:标志 -Z features
已在 1.51 版本中稳定,不再需要
现在可以通过在 Cargo.toml 中指定 resolver = "2"
来使用新功能解析器。
有关详细信息,请参阅 https://doc.rust-lang.org/nightly/cargo/reference/features.html#feature-resolver-version-2。
Compiling getrandom v0.2.1
错误:不支持目标,有关详细信息,请参阅:https://docs.rs/getrandom/#unsupported-targets
--> /home/corteri/.cargo/registry/src/github.com-1ecc6299db9ec823/getrandom-0.2.1/src/lib.rs:214:9
|
214 | / compile_error!("不支持目标,有关详细信息,请参阅:
215 | | https://docs.rs/getrandom/#unsupported-targets");
| |_________________________________________________________________________^
错误[E0433]:未能解决:使用未声明的板条箱或模块imp
--> /home/corteri/.cargo/registry/src/github.com-1ecc6299db9ec823/getrandom-0.2.1/src/lib.rs:236:5
|
236 | imp::getrandom_inner(目的地)
| ^^^ 使用未声明的板条箱或模块 imp
有关此错误的更多信息,请尝试 rustc --explain E0433
。
错误:由于之前的 2 个错误
getrandom
getrandom/js 功能需要在您的 Cargo.toml:
中激活# We enable it only for web-wasm check
# See https://docs.rs/getrandom/0.2.1/getrandom/#webassembly-support
getrandom = { version = "0.2", features = ["js"] }
(顺便说一句,我认为这不是 serde_json 特定错误)