[Ethereum] Is it possible to have mappings inside mappings

contract-developmentmappingsoliditystruct

Is this possible?

mapping(address => mapping(uint => customStruct[])) someName

Because I cannot debug and find my mistake… is there not enough gas, or it crashes on this code, while trying to fill someName.

For example:

uint length = someName[msg.sender][1].length;
someName[msg.sender][1][length].timestamp = block.timestamp;

Best Answer

I suppose customStruct is a struct with timestamp property. Then your code should work. However here my test scenario:

import "dapple/test.sol";


contract MyTest is Test {

  struct Struct {
    uint timestamp;
  }

  // Mapping test
  mapping(uint => mapping(uint => uint)) mymap;

  mapping(address => mapping(uint => Struct[])) someName;

  function testNestedMappings() {
    //@log test nested mappings
    mymap[1][2] = 42;
    //@log mymap[1][2] = `uint mymap[1][2]`
    //@log test struct array:
    //@log someName[msg.sender][1].length = `uint someName[msg.sender][1].length`
    //@log incrementing length
    someName[msg.sender][1].length++;
    //@log saving timestamp to last entry
    someName[msg.sender][1][someName[msg.sender][1].length - 1].timestamp = block.timestamp;
    //@log `uint someName[msg.sender][1][someName[msg.sender][1].length-1].timestamp`
  }
}

Which run with dapple test --report outputs the following:

MyTest
  test nested mappings
  LOG:  test nested mappings
  LOG:  mymap[1][2] = 42
  LOG:  test struct array:
  LOG:  someName[msg.sender][1].length = 0
  LOG:  incrementing length
  LOG:  saving timestamp to last entry
  LOG:  1460035092
  Passed!

Cuz i can not debug

...but this is very important

Related Topic