是否可以将私有变量存储在底层存储中,特别是以以下形式存储并在私有函数中访问它们?
#[derive(Encode, Decode, Default, Clone, PartialEq, Debug)]
pub struct MyStruct {
id: Hash, // the `id` is public
// 1. Can this be a private variable not returned by API / to client?
private_var: u64,
}
decl_storage! {
trait Store for Module<T: Trait> as MyModule {
// 2. Can this be private storage used only within module function implementation, but cannot be called by API/client?
PrivateStorage: u64 = 0;
PublicStruct: MyStruct;
}
}
decl_module! { }
impl<T: Trait> Module<T> {
fn _private_function() -> Result {
//can access both private variable/storage
let var1 = <PrivateStorage<T>>::get();
let var2 = <MyStruct<T>>::get();
if var2.private_var == 0 {
// some more code
}
Ok(())
}
}
Run Code Online (Sandbox Code Playgroud)
由于区块链系统的分布式特性,所有存储本质上都是向世界公开的。由于任何人都可以同步区块链,因此他们最终将能够生成当前的区块链状态,包括存储中的任何变量。为了就这些存储项目的状态达成共识,它们必须对各方可见且已知。
为了将“秘密数据”保留在公共区块链上,您可以使用的一种解决方案是在数据上链之前对数据进行加密,并将加密密钥保留在链外。
如果您以后希望其他用户知道秘密数据,则用户可以“泄露”加密密钥,从而暴露最初加密的数据。
这种提交/显示模式用于一些简单的区块链游戏,如石头、剪刀、布。
| 归档时间: |
|
| 查看次数: |
299 次 |
| 最近记录: |