2

我正在开发一个玩具验证器添加模块,以更好地了解会话模块的工作原理。这是我在 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

我不确定类型检查器需要理解什么,ValidatorId并且AccountId确实与我声明的类型相同。

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>`
4

1 回答 1

2

要修复上面的构建,请将OnSessionEnding's type 参数更改为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 提供)

于 2019-11-26T15:58:26.917 回答