【问题标题】:In srml_support::storage::StorageMap, what is the difference between get() and take()在 srml_support::storage::StorageMap 中,get() 和 take() 有什么区别
【发布时间】:2019-06-25 09:35:29
【问题描述】:

srml_support::storage::StorageMap中,fn get()fn take()有什么区别?

【问题讨论】:

    标签: substrate


    【解决方案1】:

    get() 只是返回存储中的值:

    /// Load the bytes of a key from storage. Can panic if the type is incorrect.
    fn get<T: codec::Decode>(&self, key: &[u8]) -> Option<T>;
    

    take() 执行get() 以返回值,还执行kill() 从存储中删除密钥:

    /// Take a value from storage, deleting it after reading.
    fn take<T: codec::Decode>(&mut self, key: &[u8]) -> Option<T> {
        let value = self.get(key);
        self.kill(key);
        value
    }
    

    这意味着在take() 操作之后,您可以调用exists(),它会返回false

    使用take() 的常见模式是某种底池支付。假设在某场比赛结束时,获胜者将所有资金放入底池。您可以在底池值上调用take() 以获取应该转移给用户的金额,并将底池重置为“零”。

    请注意,此操作会写入存储,因此在运行时调用时,存储会被永久修改。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-05-10
      • 2014-12-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多