推送到solidity结构数组无法增加数组长度

时间:2018-03-28 21:59:48

标签: ethereum solidity truffle

我正在尝试创建一个包含三个元素的结构数组。在部署合同时运行此代码时,有时中间的鱼会以某种方式被最后一条鱼覆盖。然后我们观察到fishies数组变为长度为2而不是3。在观察Release事件时,我们会看到两个版本具有相同的fishId

在松露迁移中部署的javascript代码如下所示:

module.exports = function(deployer, network, accounts) {
  const coo = accounts[0];
  deployer.then(async() => {
    let fishery = await Fishery.deployed();
    await Fishery.releaseFish(1, 1, 200, AUCTION_DURATION);
    await Fishery.releaseFish(2, 1, 200, AUCTION_DURATION);
    await Fishery.releaseFish(3, 1, 300, AUCTION_DURATION);
  });
};

定义结构和函数的可靠性代码如下所示:

Fish[] fishies;

function releaseFish(
    uint128 _id,
    uint32 _category,
)
    returns (uint)
{
    Fish memory _fish = Fish({
        id: _id,
        category: _category
    });

    uint256 newFishId = fishies.push(_fish) - 1;
    Release(
        newFishId,
        uint128(_id),
        uint32(_category)
    );
}

发布活动时会发生重复的fishId = 1事件。

Release(0,1,1)
Release(1,2,1)
Release(1,3,1)

或详细说明:

Release event:[  
   {  
      "logIndex":0,
      "transactionIndex":0,
      "transactionHash":"0x2c48a2f9e5959f13a176d24d7f832ecf405ddd732e7d0f0d6e2f12bdde1cf7b6",
      "blockHash":"0xfe1bf47d71f0be3e921fe1df796b73befdf5f914b218684b2ae07ec4c45e4e9b",
      "blockNumber":9,
      "address":"0x345ca3e014aaf5dca488057592ee47305d9b3e10",
      "type":"mined",
      "event":"Release",
      "args":{  
         "owner":"0x627306090abab3a6e1400e9345bc60c78a8bef57",
         "fishId":"0",
         "id":"1",
         "category":"1"
      }
   },
   {  
      "logIndex":0,
      "transactionIndex":0,
      "transactionHash":"0x4983b107c7529bd96db6332c1a6980f3370b5805a1956f64c16c39c8a7ad62d5",
      "blockHash":"0x9cb5b5670ec29af38d3b84d6e0bd35f283a95b1f3bc7546f1ca1c349918aaf96",
      "blockNumber":10,
      "address":"0x345ca3e014aaf5dca488057592ee47305d9b3e10",
      "type":"mined",
      "event":"Release",
      "args":{  
         "owner":"0x345ca3e014aaf5dca488057592ee47305d9b3e10",
         "fishId":"1",
         "id":"2",
         "category":"1"
      }
   },
   {  
      "logIndex":0,
      "transactionIndex":0,
      "transactionHash":"0x20a61f53fd21c1eacc6a3fadece9992a3926448c65d6f372f9653d7bc760a088",
      "blockHash":"0x67e07b72ee4c29daa28923dbf26735830c438af88b59825006851b8dc6ccbc70",
      "blockNumber":11,
      "address":"0x345ca3e014aaf5dca488057592ee47305d9b3e10",
      "type":"mined",
      "event":"Release",
      "args":{  
         "owner":"0x345ca3e014aaf5dca488057592ee47305d9b3e10",
         "fishId":"1",
         "id":"3",
         "category":"1"
      }
   },
]

不幸的是,这并非完全一致。在不同的计算机上运行它,有不同的安排,或者只是随机地运行它。

请注意,我已经从实际代码中简化了一点 - 但我希望它能够捕获讨论该问题的必要元素。

系统:在Ganache上运行松露迁移,每个的事务都出现在不同的块上。

0 个答案:

没有答案