0

我尝试使用 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;
    }

}

我想知道是否有办法理解和识别变量数量超过的部分。我应该把所有东西都变成映射吗?编译器没有向我提供更多信息。

另外,我发现了这个问题,它很相似,但我不确定这是同一个问题,一个缺点是memory当你做一个 get 时将变量保存在内存(关键字)中,另一个是设置它们。使用映射是解决这个问题的唯一可能的选择吗?另外,智能合约还没有结束,后面会添加很多其他的部分。

4

1 回答 1

1

据我所知,你的堆栈对于结构生产来说太深了,你可以在这里使用一个技巧来解决这个问题,创建一个名为 Production 的单独智能合约,在那里定义结构并在这个智能合约中导入合约. 这将帮助您在部署合约时减少 gas 消耗,并解决这个堆栈太深的问题。如果您愿意,您也可以对其他结构执行此操作。您唯一需要注意的是在实现将结构映射到特定合约时的映射。至于每个联系人,它将被视为单独的地图。如果您需要任何进一步的帮助,请告诉我。我希望你尝试这个解决方案,这会奏效。

于 2019-01-18T10:09:01.360 回答