Skip to content

Rule for string->uint mapping and the storage position #1550

Closed
@hievan2016

Description

@hievan2016

Env

solc --version
solc, the solidity compiler commandline interface
Version: 0.4.7+commit.822622cf.Linux.g++

./geth version
Geth
Version: 1.4.7-stable-667a386d
Protocol Versions: [63 62 61]
Network Id: 1
Go Version: go1.5.4
OS: linux

Problem

We are trying to figure out the mapping rule and storage position of saved data
base on this formula

keccack(LeftPad32(key, 0), LeftPad32(map position, 0))

which is mentioned at

https://github.com/ethereum/wiki/wiki/JSON-RPC#eth_getstorageat

however, seems this works for uint->string mapping, but not for string->uint
mapping, could anyone pls help on this?

Below is the test contract

contract testMapping{
mapping(string=>uint) s2i; //position is 0
mapping(uint=>string) i2s; //position is 1

function setStr(string key) {
    s2i[key] = 100;            
}

function setInt(uint key) {
    i2s[key] = "zzz";
}

}

then we use the mapping to save the data by calling

setStr("aaa") // key is aaa (ascii : 616161)
setInt(200) // key is 200 (hex : c8)

the contract storage is

storage: {
345f7c6c888721344af4147de0834159e0b302300ba13c4e7b6c0b60d8f2314e: "a07a7a7a0000000000000000000000000000000000000000000000000000000006",
69e08a2904d77becc5ebdff9265102784e25436cf0dfbaeb5c932ed98ffc834d: "64"
}

the first entry shows "zzz" (ascii : 7a7a7a) is saved at position "345f7c6c888721344af4147de0834159e0b302300ba13c4e7b6c0b60d8f2314e",
this position could be got from the formula.

but when using "aaa" as the key to do string->uint mapping to save value 100 (0x64),
we cannot get the correct storage position "69e08a2904d77becc5ebdff9265102784e25436cf0dfbaeb5c932ed98ffc834d"
based on the formula, you can see, the sha3 value is incorrect.

var key = "0000000000000000000000000000000000000000000000000000000000616161" + "0000000000000000000000000000000000000000000000000000000000000000"
web3.sha3(key, {"encoding":"hex"})

"0x15b5515e45c94bd2fbc0aa103673f2c4899d2996fe1407ba1e6e5ef9ac66c2f5"

Metadata

Metadata

Assignees

No one assigned

    Labels

    waiting for more inputIssues waiting for more input by the reporter

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions