具有结构的永恒存储模式


冷月如霜·胡狼
2025-03-09 05:47:45 (1天前)
  1. 我正在研究一种可升级的智能合约设计,我发现永久存储模式非常有用(键值对中的单独逻辑和数据),但是,如果值是......它似乎是合适的。

2 条回复
  1. 0# 与之 | 2019-08-31 10-32



    如果问题是:“但是当涉及结构等复杂类型时,分别存储和访问每个字段是否有意义?”



    然后答案是,这不是一个意义上的问题,是唯一的方法。永久存储为您提供了一种方法来存储您现在使用的每个可能的数据,并且您将来会使用这些数据。为此,它为每种数据类型提供256位映射。



    如果问题是如何在永久存储中模拟结构,我会给你2美分:



    假设您要翻译以下代码:




    1. struct thing{
      uint a;
      }
      mapping(byte32 => thing) things;
      insertThing(byte32 index, uint v) {
      things[index].a = v;
      }
      getThingValue(byte32 thingIndex){
      return things[thingIndex].a;
      }

    2. </code>


    到使用永久存储的代码。我的2美分如下:




    1. […]
      EternalStorage s;
      insertThing(byte32 index, uint v){
      s.setUint(keccak256(“things”, index, a”), v);
      }
      getThingValue(byte32 index) returns (uint){
      return s.getUint(keccak256(“things”, index, a”));
      }

    2. </code>

登录 后才能参与评论