Code –
pragma solidity ^0.4.18;
contract CA4 {
mapping(address=>uint16) public balances;
function getBalance() view returns (uint16) {
uint16 mybalance = balances[msg.sender];
return mybalance;
}
function setBalance(uint16 newbalance) public {
balances[msg.sender]=newbalance;
}
}
Geth –
> eth.getStorageAt(con.address,0)
"0x0000000000000000000000000000000000000000000000000000000000000000"
> var key = web3.sha3("0x0000000000000000000000000000000000000000000000000000000000000000", {encoding:"hex"});
undefined
> var bn = web3.toBigNumber(key);
undefined
> eth.getStorageAt(con.address, bn)
"0x0000000000000000000000000000000000000000000000000000000000000000"
> eth.getStorageAt(con.address,1)
"0x0000000000000000000000000000000000000000000000000000000000000000"
> con.getBalance()
1000
The array balances is stored somewhere. I am unsure how to find it. I tried a method which worked when we had an array of structs. But that did not work for the mapping of addresses to uint16.
How can we find the actual storage of balances using for example getStorageAt(con.address, slot)?
More Details –
> eth.defaultAccount
"0x46fb9a22689c4a4bfb494baeafbb8b2993725305"
> key=web3.sha3("0x46fb9a22689c4a4bfb494baeafbb8b2993725305", {encoding:"hex"});
"0x0679d661b585d1b4f0c3fd18f943f19e7fa9777fe23cb96b94f469c20d6f0bd5"
> var bn = web3.toBigNumber(key);
undefined
> eth.getStorageAt(con.address, bn)
"0x0000000000000000000000000000000000000000000000000000000000000000"
>
Best Answer
I finally solved it.
Then to solve this you just put the key (ie the address) into the a key+pos sha3 and convert to a bigNumber. The vital part also is to remove 0x and to format as 64 chars.
Here is the final output -
Pos is 1 because I added a uint to the contract before the mapping to test this. The two keys are then the accounts with 0x removed and reformatted to 64 chars. ie.
Here is the amended contract -