0

I try to compile a smart contract using Truffle framework and I get following output:

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

The source code of the smart contract written with Solidity 0.5.0 is:

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;
    }

}

I would like to know if there is a way to understand and identify the part where the number of variables exceeds. Should I turn everything into a mapping? The compiler does not provide me with further information.

Also, I found this question, it's similar, but I'm not sure it's the same problem, a cons it is to save the variables in memory (memory keyword) when you do a get, another is to set them. Is the use of mapping the only possible alternative to tackle this problem? Furthermore, the smart contract is not concluded, many other parts will be added later.

shogitai
  • 1,823
  • 1
  • 23
  • 50

1 Answers1

1

As far as I can see, your stack is too deep for the struct production, you can use a trick here to solve this issue, create a separate smart contract with the name Production, define the structure there and import the contract in this smart contract. This will help you in both reducing your gas consumption while deploying the contract and this stack too deep issue. This you can do for other structs as well if you wish. The only thing you need to take care of is the mapping while you implement for mapping the structure to a particular contract. As for each contact it will be counted as separate map. Let me know if you need any further assistance. I want you to try out this solution hopefully, this will work.