gpt4 book ai didi

substrate - 基底存储中私有(private)变量的可能性

转载 作者:行者123 更新时间:2023-12-03 08:54:07 32 4
gpt4 key购买 nike

是否可以将私有(private)变量存储在底层存储中,特别是以以下形式存储并在私有(private)函数中访问它们?

#[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(())
}
}

最佳答案

由于区 block 链系统的分布式特性,所有存储本质上都是向世界公开的。由于任何人都可以同步区 block 链,因此他们最终将能够生成当前的区 block 链状态,包括存储中的任何变量。为了就这些存储项目的状态达成共识,它们必须对各方可见且已知。

为了在公共(public)区 block 链上保留“ secret 数据”,您可以使用的一种解决方案是在数据上链之前对数据进行加密,并将加密 key 保留在链外。

如果您以后希望其他用户知道 secret 数据,用户可以“泄露”加密 key ,从而暴露最初加密的数据。

这种提交/显示模式用于一些简单的区 block 链游戏,如石头、剪刀、布。

关于substrate - 基底存储中私有(private)变量的可能性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56867236/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com