固态状态变量重置为默认内部功能

时间:2018-06-27 16:42:40

标签: blockchain ethereum solidity remix

对于这个项目,我正在使用Solidity为以太坊编写智能合约。

在以下代码中,变量numCertificates最初应为1。尽管如此,分配时newCertificateId为0。根据调试器(我正在使用remix.ethereum.org),由于某种原因,一旦到达分配newCertificateId的行,numCertificates就会变为0。

我在混音上玩了很多,根据它,numCertificates为1,直到精确达到上述行为止。另外,我认为如果我在另一个函数中修改numCertificates,则该变量将保持与以前相同。

我试图弄清楚这一点使我相信,当我访问numCertificates时,我不是在访问公共状态变量,而是在其他地方。

为什么此代码有此问题,如何解决?

pragma solidity ^0.4.21;

contract MyToken {
    // ... lots of irrelevant stuff here
    mapping (uint64 => Certificate) public certificates;
    uint64 numCertificates = 1;

    // ... lots of irrelevant stuff here

    function MyToken() public {
      // ... likely irrelevant code
    }

    function produceCertificate(
      // ... likely irrelevant parameters
      ) public {
        // Create the certificate in memory
        numCertificates;// only for debbuging
        Certificate storage newCertificate = constructCertificate(meterId, timestamp, carbonMitigationValue, owner);
        uint64 newCertificateId = numCertificates;
        newCertificate.certificateId = newCertificateId;

        // Save the certificate
        certificates[newCertificateId] = newCertificate;
        numCertificates++;
    }



    function constructCertificate(
      uint64 meterId,
      uint32 timestamp,
      uint48 value,
      address owner
      ) internal returns (Certificate storage newCertificate) {
        newCertificate.meterId = meterId;
        newCertificate.timestamp = timestamp;
        newCertificate.value = value;

        newCertificate.owners.push(owner);
        newCertificate.ownershipSplit[owner] = value;

        newCertificate.burned = false; // by default new certificates are fresh

        return newCertificate;
    }
}

0 个答案:

没有答案