Blockchain Solidity 0.5.0变量过多

Blockchain Solidity 0.5.0变量过多,blockchain,ethereum,solidity,smartcontracts,evm,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

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

}
我想知道是否有办法理解和识别变量数量超过的部分。我是否应该将所有内容都转换为映射?编译器没有向我提供更多信息


另外,我发现,这是相似的,但我不确定这是否是同一个问题,一个缺点是在执行get时将变量保存在内存中(
memory
关键字),另一个缺点是设置它们。使用映射是解决此问题的唯一可能的替代方法吗?此外,智能合约尚未签订,许多其他部分将在以后添加。

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