I have tried to create a test of my contract which includes a function returns Struct instance. But, it returns a meaningless error.
Here's my contract which is created by Solidity v0.6.0
pragma solidity ^0.6.0;
pragma experimental ABIEncoderV2;
contract Stack{
struct Item{
string name;
uint256 quantity;
bool isSolid;
}
mapping(address => Item[]) userItemsMap;
function addItem(string memory _name, uint256 _quantity, bool _isSolid) public {
userItemsMap[msg.sender].push(Item({
name:_name, quantity:_quantity,
isSolid :_isSolid
}));
}
function getItems() public view returns(Item[] memory){
return userItemsMap[msg.sender];
}
function getLastItem() public view returns(Item memory){
Item[] memory userItems = userItemsMap[msg.sender];
require(userItems.length>0);
return userItems[userItems.length - 1];
}
}
And its sample test contract :
pragma solidity ^0.6.0;
import "truffle/Assert.sol";
import "truffle/DeployedAddresses.sol";
import "../contracts/Stack.sol";
contract TestStackContract{
function testAddingItem() public {
Stack stackInstance = Stack(DeployedAddresses.Stack());
stackInstance.addItem("Apple",125,true);
Stack.Item memory lastItem = stackInstance.getLastItem();
Assert.equal(lastItem.name, "Apple", "Name should be Apple");
}
}
I run truffle test and it should pass the test or return an error message
It returns an InternalCompilerError without any error message
Using network 'development'.
Compiling your contracts...
===========================
> Compiling ./test/TestBiddingContract.sol
> Compiling ./test/TestStack.sol
InternalCompilerError:
Compilation failed. See above.
Truffle v5.1.13 (core: 5.1.13)
Node v8.17.0
truffle version): v5.1.13node --version): v8.17.0npm --version): 6.13.4@safakoks Thanks for reporting this, I'll try and reproduce your error to get to the bottom of this!
Using Truffle without Geth or Quorum I get the following error
InternalCompilerError: Unknown dynamically sized type: struct Stack.Item memory
It looks like that message comes from the decoder and perhaps Geth/Quorum setups give differently formatted messages? Maybe @haltman-at might understand what is going on here a little bit better than I do.
Is there any solution for this? Because I could not understand what is wrong and why it happens?
InternalCompilerError is a solc error message; it's solc throwing that error, not Truffle. This has nothing to do with the decoder, it's a compilation error.
Basically, if you see InternalCompilerError, you should report it to the Solidity team. Unless for some reason compiling those tests only fails in Truffle?
@haltman-at thanks for your answer, yes it happened because of Solidity. I asked them, why it occurs, they recommend me adding pragma experimental ABIEncoderV2; into Test Contract, It works fine on Remix. But still, truffle does not accept the contract during truffle test
It returns the same error without any message :
Using network 'development'.
Compiling your contracts...
===========================
> Compiling ./contracts/Stack.sol
> Compiling ./test/TestStack.sol
InternalCompilerError:
Compilation failed. See above.
Truffle v5.1.13 (core: 5.1.13)
Node v8.17.0
@safakoks , which version of solidity are you specifically using to compile with while testing?
@CruzMolina , here's my compile config in truffle-config.js
compilers: {
solc: {
version: "0.6.0"
}
}
Hmmm. odd. this is what I see
➜ reprod-error truffle test
Compiling your contracts...
===========================
> Compiling ./contracts/Migrations.sol
> Compiling ./contracts/Stack.sol
> Compiling ./test/TestExample.sol
InternalCompilerError: Unknown dynamically sized type: struct Stack.Item memory
Compilation failed. See above.
Truffle v5.1.14 (core: 5.1.14)
Node v10.18.0
Same output when using Node v8.17.0.
@CruzMolina may it happens because I am using Truffle v5.1.13?
@safakoks Hmmm. No, I'm not seeing any changes to the relevant code in v5.1.14.
At this point, I think we need to see a repo of a truffle project example where this exact bug is consistently reproduced.