这是我的结构:
#[derive(PartialEq, Eq, PartialOrd, Ord, Default, Clone, Encode, Decode, TypeInfo)]
#[cfg_attr(feature = "std", derive(Debug))]
pub struct SortitionSumTree<AccountId> {
pub k: u128,
pub stack: Vec<u128>,
pub nodes: Vec<u128>,
pub ids_to_tree_indexes: BTreeMap<AccountId, u128>,
pub node_indexes_to_ids: BTreeMap<u128, AccountId>,
}
Run Code Online (Sandbox Code Playgroud)
我的存储:
#[pallet::storage]
#[pallet::getter(fn sortition_sum_trees)]
pub type SortitionSumTrees<T> = StorageMap<_, Blake2_128Concat, Vec<u8>, SortitionSumTree<T>>;
Run Code Online (Sandbox Code Playgroud)
但它给出了错误:
该特征parity_scale_codec::Encode未实现std::collections::BTreeMap<u128, T>
我想要一个投票阶段在一周后结束的投票系统。我不想使用 block_timestamp。我应该使用什么 Near_sdk 环境?
pub fn block_index() -> BlockHeight
Run Code Online (Sandbox Code Playgroud)
有epoch_height和block_index,我应该用什么?我听说这block_index可能不是连续的,并且可能缺少数字。真的吗?
FRAME2 存储使用以下语法定义:
#[pallet::storage]
type SomePrivateValue<T> = StorageValue<_, u32, ValueQuery>;
#[pallet::storage]
#[pallet::getter(fn some_primitive_value)]
pub(super) type SomePrimitiveValue<T> = StorageValue<_, u32, ValueQuery>;
Run Code Online (Sandbox Code Playgroud)
但存储无需 ValueQuery 关键字即可工作。
例如
#[pallet::storage]
type SomePrivateValue<T> = StorageValue<_, u32>;
Run Code Online (Sandbox Code Playgroud)
ValueQuery 有什么用?
同样设置默认值需要ValueStorage,而ValueStorage不允许使用getter函数。如何拥有设置默认值的 getter 函数?
https://substrate.dev/docs/en/knowledgebase/runtime/storage#default-values
在python中它是这样完成的:
>>> x = {1: 2, 3: 4, 4: 3, 2: 1, 0: 0}
>>> {k: v for k, v in sorted(x.items(), key=lambda item: item[1])}
{0: 0, 2: 1, 1: 2, 4: 3, 3: 4}
Run Code Online (Sandbox Code Playgroud)
如何通过rust中的值来缩短hashmap?
到目前为止我的代码:
use std::collections::HashMap;
fn main() {
let mut count: HashMap<String, u32>= HashMap::new();
count.insert(String::from("A"), 5);
count.insert(String::from("B"), 2);
count.insert(String::from("C"), 11);
count.insert(String::from("D"), 10);
let highest = count.iter().max_by(|a, b| a.1.cmp(&b.1)).unwrap();
println!("largest hash: {:?}", highest); // largest hash: ("C", 11)
}
Run Code Online (Sandbox Code Playgroud)