在 substrate 中声明一个 hashmap (storageMap)

Declaring a hashmap (storageMap) in substrate

我想在 substrate 中创建一个 StorageMap 并按照 this 示例进行操作。存储映射的目的是拥有一个 HashMap,其中键将是用户的帐户 ID,值将是无效交易已完成的次数。例如:

Adkqwd4324dqlwdOqwdd: 2,
XCvqwd4324dqlwdOqwdd: 0,
Adkqwd4324dqlwdOqwPu: 0,
Xcvqwd4324dqlwdOqwdd: 1

我当前的 decl_storage 宏 transaction_payment>src>lib.rs 中看起来像这样:

decl_storage! {
    /// StorageMap to keep track of invalid transactions
    trait Store for Module<T: Trait> as InvalidTransactionCount {
        InvalidTransactionCount get(fn invalid_transaction_count): map hasher(identity) T::AccountId => u32;
    }
    
    /// already present in the substrate master code
    trait Store for Module<T: Config> as TransactionPayment {
        pub NextFeeMultiplier get(fn next_fee_multiplier): Multiplier = Multiplier::saturating_from_integer(1);

        StorageVersion build(|_: &GenesisConfig| Releases::V2): Releases;
    }
}

但是,当我编译此代码时,我收到与 NextFeeMultiplier 相关的错误,因为 decl_storage 宏中的错误导致它未正确初始化,因为InvalidTransactionCount StorageMap 的。完整的错误回溯如下:

error: unexpected token
   --> frame/transaction-payment/src/lib.rs:242:2
    |
242 |     trait Store for Module<T: Config> as TransactionPayment {
    |     ^^^^^

error[E0433]: failed to resolve: use of undeclared type `NextFeeMultiplier`
   --> frame/transaction-payment/src/lib.rs:259:4
    |
259 |             NextFeeMultiplier::mutate(|fm| {
    |             ^^^^^^^^^^^^^^^^^ use of undeclared type `NextFeeMultiplier`

error[E0433]: failed to resolve: use of undeclared type `NextFeeMultiplier`
   --> frame/transaction-payment/src/lib.rs:446:3
    |
446 |         NextFeeMultiplier::get().saturating_mul_int(Self::weight_to_fee(weight))
    |         ^^^^^^^^^^^^^^^^^ use of undeclared type `NextFeeMultiplier`

error[E0599]: no function or associated item named `next_fee_multiplier` found for struct `Module<T>` in the current scope
   --> frame/transaction-payment/src/lib.rs:414:27
    |
249 | / decl_module! {
250 | |     pub struct Module<T: Config> for enum Call where origin: T::Origin {
251 | |         /// The fee to be paid for making a transaction; the per-byte portion.
252 | |         const TransactionByteFee: BalanceOf<T> = T::TransactionByteFee::get();
...   |
304 | |     }
305 | | }
    | |_- function or associated item `next_fee_multiplier` not found for this
...
414 |               let multiplier = Self::next_fee_multiplier();
    |                                      ^^^^^^^^^^^^^^^^^^^ function or associated item 

not found in `Module<T>`

warning: unused import: `StorageMap`
  --> frame/transaction-payment/src/lib.rs:46:2
   |
46 |     StorageMap,
   |     ^^^^^^^^^^
   |
   = note: `#[warn(unused_imports)]` on by default

error: aborting due to 4 previous errors; 1 warning emitted

Some errors have detailed explanations: E0433, E0599.
For more information about an error, try `rustc --explain E0433`.
error: could not compile `pallet-transaction-payment`

如果我从 decl_storage 中删除 InvalidTransactionCount trait Store,那么代码编译正常。

任何有助于确定在 decl_storage 宏内声明存储映射的正确方法的帮助将不胜感激。谢谢!

这一行只能写一次。在 decl_module 宏中。

trait Store for Module<T: Config> as TransactionPayment {

如果您只需要多个存储项目:

decl_storage! {
    trait Store for Module<T: Trait> as InvalidTransactionCount {
        InvalidTransactionCount get(fn invalid_transaction_count): map hasher(identity) T::AccountId => u32;
    
        pub NextFeeMultiplier get(fn next_fee_multiplier): Multiplier = Multiplier::saturating_from_integer(1);

        StorageVersion build(|_: &GenesisConfig| Releases::V2): Releases;
    }
}