1

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.

1 Answer 1

1

The answer is to use +1 on the key as follows -

> var key = web3.sha3("0x0000000000000000000000000000000000000000000000000000000000000001", {encoding:"hex"});
undefined
> key
"0xb10e2d527612073b26eecdfd717e6a320cf44b4afac2b0732d9fcbe2b7fa0cf6"


> var bn = web3.toBigNumber(key);
undefined
> bn
8.0084422859880547211683076133703299733277748156566366325829078699459944778998e+76



> eth.getStorageAt(con.address, bn)
"0x0000000000000000000000000000000000000000000000000000000d000c000b"

> con.students(0)
[1, 2, 3, 4]
> con.employees(0)
[11, 12, 13]

I do not know why this works. But it seems that the 0 entry in key is the first array and then +1 is next.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.