坚固度0.5.0变量过多

时间:2019-01-10 09:10:20

标签: blockchain ethereum solidity smartcontracts evm

我尝试使用Truffle框架编译智能合约,并得到以下输出:

Compiling ./contracts/PartProduction.sol...

InternalCompilerError: Stack too deep, try using fewer variables.
Compilation failed. See above.
Truffle v5.0.1 (core: 5.0.1)
Node v11.6.0

以Solidity 0.5.0编写的智能合约的源代码为:

pragma solidity 0.5.0;

contract PartProduction {

    enum State {
        productionRequested,
        parametersObtained,
        manufacturerObtained,
        productionForwardedToManufacturer,
        printing,
        printed,
        postProcessing,
        postProcessed,
        qualityChecking,
        qualityChecked,
        productionFinished
    }

    enum Priority {
        low,
        normal,
        high
    }

    struct Company {
        address vehicleEthAddress;
        address myWebService;
    }

    struct Manufacturer {
        string id;
        string location;
        address productionMachine1;
        address productionMachine2;
        address productionMachine3;
    }

    struct Production {
        string productionParameters;
        string designParameters;
        State state;
        Priority priority;
        string partNumber;
        uint256 cost;
        uint256 productionForwardedTime;
        uint256 productionStartTime;
        uint256 productionEndTime;
        address partID;
        string myIdentifier;
        string location;
    }

    Company public company;
    Manufacturer public manufacturer;
    Production public production;

    constructor(

        address _vehicleEthAddress,
        string memory _myIdentifier,
        string memory _vehicleLocation,
        string memory _partNumber,
        Priority _priority)internal {

        company.myWebService = msg.sender;
        company.vehicleEthAddress = _vehicleEthAddress;
        production.partID = address(this);
        production.state = State.productionRequested;
        production.partNumber = _partNumber;
        production.priority = _priority;
        production.myIdentifier = _myIdentifier;
        production.location = _vehicleLocation;
    }

    function setParameters(string calldata _designParametersHash, string calldata _productionParametersHash) external {
        require(msg.sender == company.myWebService);
        require(production.state == State.productionRequested);
        production.designParameters = _designParametersHash;
        production.productionParameters = _productionParametersHash;
        production.state = State.parametersObtained;
    }

    function setManufacturer(string calldata _manufacturerId, string calldata _manufacturerLocation) external {
        require(msg.sender == company.myWebService);
        require(production.state == State.parametersObtained);
        manufacturer.id = _manufacturerId;
        manufacturer.location = _manufacturerLocation;
        production.state = State.manufacturerObtained;
    }

    function forwardProductionData(uint256 _productionForwardedTime) external {
        require(msg.sender == company.myWebService);
        require(production.state == State.manufacturerObtained);
        production.state = State.manufacturerObtained;
        production.productionForwardedTime = _productionForwardedTime;
    }

    function registerproductionMachine1(address _productionMachine1) external {
        require(msg.sender == company.myWebService);
        manufacturer.productionMachine1 = _productionMachine1;
    }

}

我想知道是否有一种方法可以理解和识别超出变量数量的部分。我应该把所有东西都变成一个映射吗?编译器未向我提供更多信息。

此外,我发现了this question,它很相似,但是我不确定这是同一个问题,这是在获取变量时将变量保存在内存中(memory关键字) ,另一种是设置它们。使用映射是解决此问题的唯一可能选择吗?此外,智能合约尚未缔结,稍后将添加许多其他部分。

1 个答案:

答案 0 :(得分:1)

据我所知,您的堆栈对于结构 production 而言太深了,您可以在此处使用技巧来解决此问题,创建一个单独的智能合约,名称为Production,定义在此处进行结构化并在此智能合约中导入合约。这将有助于您减少在部署合同时的耗油量,以及此烟囱过深的问题。 您也可以根据需要对其他结构执行此操作。您唯一需要注意的是在实现将结构映射到特定合同时的映射。对于每个联系人,它将被计为单独的地图。 让我知道您是否需要任何进一步的帮助。我希望您希望尝试该解决方案,它将起作用。