Solidity 0.5.0 变量太多
Posted
技术标签:
【中文标题】Solidity 0.5.0 变量太多【英文标题】:Solidity 0.5.0 too much variables 【发布时间】:2019-06-05 02:49:44 【问题描述】:我尝试使用 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,它很相似,但我不确定它是否是同一个问题,一个缺点是当你做一个get时将变量保存在内存中(memory
关键字),另一个是设置它们。使用映射是解决这个问题的唯一可能的选择吗?另外,智能合约还没有结束,后面会添加很多其他的部分。
【问题讨论】:
【参考方案1】:据我所知,你的堆栈对于结构 production 来说太深了,你可以在这里使用一个技巧来解决这个问题,创建一个名为 Production 的单独智能合约,定义在那里构建并在此智能合约中导入合约。这将帮助您在部署合约时减少 gas 消耗,并解决这个堆栈太深的问题。 如果您愿意,您也可以对其他结构执行此操作。您唯一需要注意的是在实现将结构映射到特定合约时的映射。至于每个联系人,它将被视为单独的地图。 如果您需要任何进一步的帮助,请告诉我。我希望你尝试这个解决方案,希望它会奏效。
【讨论】:
以上是关于Solidity 0.5.0 变量太多的主要内容,如果未能解决你的问题,请参考以下文章
如何在 Node.js 上使用solidity @0.5.0 部署工厂合约
如何在solidity 0.5.0中将etherenum发送到msg.sender