substrate - 如何使用 RPC 调用从 Substrate 读取哈希值并获取相应的 AccountId?
问题描述
我有一个基板节点启动并运行,存储项目为:value(Hash): Option<AccountId>
。我的目标是提供哈希值(例如,0x0000000000000000000000000000000000000000000000000000000000000001
并获得相应的帐户 ID 作为回报)。
我想通过 RPC 调用执行相同的任务。看完这篇博文后,我意识到我的情况是阅读StorageMaps
,所以我开始运行一些查询。如果我没记错的话,模块是Substratekitties
,存储项是value
。映射将value
是AccountId
。
我打了前两个电话:
util_crypto.xxhashAsHex("Substratekitties", 128)
"0xe4a154b5ba85d6072b187ee66e4fef05"
util_crypto.xxhashAsHex("Value", 128)
"0x6b2f21989c43cc4e06ac1ad3e2027000"
但我对第三次调用编码感到困惑:编码文件 sha256 哈希。怎么做?运行util_crypto.blake2AsHex("0000000000000000000000000000000000000000000000000000000000000001", 256)
"0x16821de47d8b3b0fa4ca43e5db1028d75207cbd1c379a4738144972b105355aa"
将不起作用,也不起作用。
我的意思是说,我在执行此查询时得到“空”值。这是存储结构:
use frame_support::{decl_module, decl_storage, dispatch::result::Result, ensure, StorageMap};
use frame_system::ensure_signed;
use sp_runtime::DispatchError;
// pub trait Trait: balances::Trait {}
pub trait Trait: pallet_balances::Trait {}
decl_storage! {
trait Store for Module<T: Trait> as KittyStorage {
// Value: map T::Hash => Option<T::AccountId>;
// TODO: check whether this is the appropriate datatype(hash).
Value: map hasher(blake2_256) T::Hash => Option<T::AccountId>;
// Balances: map hasher(blake2_256) (T::AssetId, T::AccountId) => T::Balance;
}
}
decl_module! {
pub struct Module<T: Trait> for enum Call where origin: T::Origin {
fn set_value(origin, value: T::Hash) -> Result<(), DispatchError> {
let sender = ensure_signed(origin)?;
ensure!(!<Value<T>>::contains_key(value), "key already exists");
<Value<T>>::insert(value, sender);
Ok(())
}
}
}
更新:我的文字查询:
curl -H "Content-Type: application/json" -d '{"id":1, "jsonrpc":"2.0", "method": "state_getStorage", "params": ["0x3fd011a1ea758d2e1b46ed3cec43fc86b2f21989c43cc4e06ac1ad3e2027000d3585436436a2253c5163fa0cfe54a648fa533ef32ea10dbd966ac438af77b71"]}' http://localhost:9933/
十六进制查询:
util_crypto.xxhashAsHex("KittyStorage", 128)
"0xe3fd011a1ea758d2e1b46ed3cec43fc8"
util_crypto.xxhashAsHex("Value", 128)
"0x6b2f21989c43cc4e06ac1ad3e2027000"
util_crypto.blake2AsHex("0x0000000000000000000000000000000000000000000000000000000000001234")
"0xd3585436436a2253c5163fa0cfe54a648fa533ef32ea10dbd966ac438af77b71"
解决方案
我认为这里的问题是您正在获取字符串“0000...0001”的哈希而不是字节。尝试添加0x
到字符串的前面。
编辑:试试这样:
util_crypto.blake2AsHex("0x0000000000000000000000000000000000000000000000000000000000000001")
> "0x33e423980c9b37d048bd5fadbd4a2aeb95146922045405accc2f468d0ef96988"
编辑 2:这里的另一个问题是您使用了错误的密钥。
您的示例显示您正在使用存储密钥“Substratekitties”,但您的存储密钥在宏KittyStorage
中指定。decl_storage!
所以你的第一个哈希应该是:0xe3fd011a1ea758d2e1b46ed3cec43fc8
推荐阅读
- amazon-web-services - Dynamodb 触发器使用 lambda 更新另一个 dynamobo 表
- node.js - TypeError:无法在反应中读取正确的长度
- javascript - 如何在 JS 中使用 map 进行过滤
- d3.js - 在多个图表上组合 elasticY
- batch-file - 什么是错误“无法创建已经存在的文件”
- python - 没有外部库的简单多项式微分器
- java - 如何从 firebase auth 中删除用户,以便用户自动从应用程序中注销?
- java - 如何将文件列表移动到某个目录
- php - 如何在php sql查询中写入变量?
- c# - DbSet , 实体框架和获取集