gpt4 book ai didi

rust - Substrate:如何与session模块集成

转载 作者:行者123 更新时间:2023-11-29 08:30:35 25 4
gpt4 key购买 nike

我正在研究玩具验证器添加模块,以更好地理解 session module 的工作原理.这是我的 complete attempt on github .

我的代码几乎可以正常工作,但我在执行 OnSessionEnding 时遇到了类型检查错误.

impl<T: Trait> OnSessionEnding<T::AccountId> for Module<T> {
fn on_session_ending(_ending_index: SessionIndex, _will_apply_at: SessionIndex) -> Option<Vec<T::AccountId>> {
match <QueuedValidator<T>>::get() {
Some(n00b) => {
// Get the list of current validators from the session module
let mut validators = session::Module::<T>::validators();
validators.push(T::ValidatorIdOf::convert(n00b.clone()).unwrap());
Some(validators.into())
}
None => None
}

}
}
// https://github.com/paritytech/substrate/blob/4a17a8aaa5042759d934abb10b1703ffdff7d902/bin/node-template/runtime/src/add_validator.rs#L66-L79

我不确定类型检查器需要什么才能理解 ValidatorIdAccountId 确实与我声明的类型相同。

impl session::Trait for Runtime {
// --snip--
type ValidatorId = <Self as system::Trait>::AccountId;
type ValidatorIdOf = ConvertInto;
}
// https://github.com/paritytech/substrate/blob/4a17a8aaa5042759d934abb10b1703ffdff7d902/bin/node-template/runtime/src/lib.rs#L250-L262

准确的错误是

error[E0277]: the trait bound `add_validator::sr_api_hidden_includes_decl_storage::hidden_include::sr_primitives::substrate_application_crypto::substrate_primitives::sr_std::prelude::Vec<<T as palette_system::Trait>::AccountId>: core::convert::From<add_validator::sr_api_hidden_includes_decl_storage::hidden_include::sr_primitives::substrate_application_crypto::substrate_primitives::sr_std::prelude::Vec<<T as pallet_session::Trait>::ValidatorId>>` is not satisfied
--> /home/joshy/ProgrammingProjects/substrate/bin/node-template/runtime/src/add_validator.rs:73:10
|
73 | Some(validators.into())
| ^^^^^^^^^^^^^^^^^ the trait `core::convert::From<add_validator::sr_api_hidden_includes_decl_storage::hidden_include::sr_primitives::substrate_application_crypto::substrate_primitives::sr_std::prelude::Vec<<T as pallet_session::Trait>::ValidatorId>>` is not implemented for `add_validator::sr_api_hidden_includes_decl_storage::hidden_include::sr_primitives::substrate_application_crypto::substrate_primitives::sr_std::prelude::Vec<<T as palette_system::Trait>::AccountId>`
|
= note: required because of the requirements on the impl of `core::convert::Into<add_validator::sr_api_hidden_includes_decl_storage::hidden_include::sr_primitives::substrate_application_crypto::substrate_primitives::sr_std::prelude::Vec<<T as palette_system::Trait>::AccountId>>` for `add_validator::sr_api_hidden_includes_decl_storage::hidden_include::sr_primitives::substrate_application_crypto::substrate_primitives::sr_std::prelude::Vec<<T as pallet_session::Trait>::ValidatorId>`

或者没有最后的 .into() 它变成了

error[E0308]: mismatched types
--> /home/joshy/ProgrammingProjects/substrate/bin/node-template/runtime/src/add_validator.rs:73:10
|
73 | Some(validators)
| ^^^^^^^^^^ expected palette_system::Trait::AccountId, found pallet_session::Trait::ValidatorId
|
= note: expected type `add_validator::sr_api_hidden_includes_decl_storage::hidden_include::sr_primitives::substrate_application_crypto::substrate_primitives::sr_std::prelude::Vec<<T as palette_system::Trait>::AccountId>`
found type `add_validator::sr_api_hidden_includes_decl_storage::hidden_include::sr_primitives::substrate_application_crypto::substrate_primitives::sr_std::prelude::Vec<<T as pallet_session::Trait>::ValidatorId>`

最佳答案

要修复上面的构建,请将 OnSessionEnding 的类型参数更改为 T::ValidatorId

- impl<T: Trait> OnSessionEnding<T::AccountId> for Module<T> {
+ impl<T: Trait> OnSessionEnding<T::ValidatorId> for Module<T> {

这是完整的工作实现

impl<T: Trait> OnSessionEnding<T::ValidatorId> for Module<T> {
fn on_session_ending(_ending_index: SessionIndex, _will_apply_at: SessionIndex) -> Option<Vec<T::ValidatorId>> {
match <QueuedValidator<T>>::get() {
Some(n00b) => {
// Get the list of current validators from the session module
let mut validators = session::Module::<T>::validators();

// Add the new validator to the list
//TODO handle the unwrapping better
validators.push(T::ValidatorIdOf::convert(n00b.clone()).unwrap());

// Clear the queued validator from local storage
<QueuedValidator<T>>::kill();

// Return the vector of new validators
Some(validators)
}
None => None
}

}
}

(此答案由@sushisource:matrix.org 提供)

关于rust - Substrate:如何与session模块集成,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59012154/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com