I have the code below -
pragma solidity ^0.4.18;
contract CA3 {
struct Student {
uint16 age;
uint16 income;
uint16 score;
uint16 attendance;
}
struct Employee {
uint16 age;
uint16 income;
uint16 timeWorked;
}
Student[] public students;
Employee[] public employees;
function setStudent (uint16 _age, uint16 _income, uint16 _score, uint16 _attendan
ce) public {
Student memory tempStudent = Student({age: _age, income: _income, score: _scor
e, attendance: _attendance});
students.push(tempStudent);
//students.push(Student({age: _age, income: _income, score: _score, attendance: _
attendance}));
}
function setEmployee(uint16 _age, uint16 _income, uint16 _timeWorked) public {
Employee memory tempEmployee = Employee({age: _age, income: _income, timeWorked:
_timeWorked});
employees.push(tempEmployee);
}
}
I tested earlier the code without Employee and the following did show the storage for students -
var n = 2;
var startSlot = web3.toBigNumber(
web3.sha3(web3.padLeft("0", 64), { encoding: 'hex' }));
var slot = "0x" + startSlot.add(n).toString(16);
web3.eth.getStorageAt(con.address, slot, function (err, result) {
console.log(result);
});
But when I test with Employees I get the following -
> con.students(0)
[1, 2, 3, 4]
> con.employees(0)
[11, 12, 13]
> eth.getStorageAt(con.address,0) "0x0000000000000000000000000000000000000000000000000000000000000002"
> eth.getStorageAt(con.address,1) "0x0000000000000000000000000000000000000000000000000000000000000001"
This correctly finds the number of entries in the arrays.
> var startSlot = web3.toBigNumber(
... web3.sha3(web3.padLeft("0", 64), { encoding: 'hex' }));
undefined
> startSlot
1.8569430475105882587588266137607568536673111973893317399460219858819262702947e+76
> var slot = "0x" + startSlot.add(n).toString(16);
undefined
> slot
"0x290decd9548b62a8d60345a988386fc84ba6bc95484008f6362f93160ef3e564"
This works for students -
> n=0
0
> var slot = "0x" + startSlot.add(n).toString(16);
undefined
> eth.getStorageAt(con.address,slot)
"0x0000000000000000000000000000000000000000000000000004000300020001"
But how is Employee found? I assume we need to code a displacement somehow for the startslot.