Join GitHub today
GitHub is home to over 28 million developers working together to host and review code, manage projects, and build software together.Sign up
Rule for string->uint mapping and the storage position #1550
We are trying to figure out the mapping rule and storage position of saved data
keccack(LeftPad32(key, 0), LeftPad32(map position, 0))
which is mentioned at
however, seems this works for uint->string mapping, but not for string->uint
Below is the test contract
then we use the mapping to save the data by calling
setStr("aaa") // key is aaa (ascii : 616161)
the contract storage is
the first entry shows "zzz" (ascii : 7a7a7a) is saved at position "345f7c6c888721344af4147de0834159e0b302300ba13c4e7b6c0b60d8f2314e",
but when using "aaa" as the key to do string->uint mapping to save value 100 (0x64),
var key = "0000000000000000000000000000000000000000000000000000000000616161" + "0000000000000000000000000000000000000000000000000000000000000000"
I still cannot get the correct hash value "69e08a2904d77becc5ebdff9265102784e25436cf0dfbaeb5c932ed98ffc834d", could you help check whether I miss anything?
var key0 = "00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000616161"
sha3 : "0x8f34d90832af21f9fc31ef2888a966ad8090cdd9bc1a16ecf819059d118ae6db"
var key0 = "616161"
sha3 : "0xe4998bf274557cf66b8123e4803a12c6dca7f047052b01d6b4b51776b0eb04d7"
var key0 = "0000000000000000000000000000000000000000000000000000000000616161"
sha3 : "0x4b9ff950998e1bfecbf6ed1b5e7531fee7fac216130cafe3d3c03d0b1ba697b8"
@hievan2016 @chriseth is there anyway to return an element of a mapping without knowing the key but using an index (i.e. pos 1, 2, etc)? Carrying on your example from above, if
Assuming we did not know the keys but we knew that i2s was in position 2?
I have worked out how to do this for arrays but not for maps.
@VoR0220 thanks for responding. I should have clarified that I need to do this in the same way as discussed in this thread - using getStorageAt function which @hievan2016 was using. I am aware that this can be done relatively easily within the contract itself.
@chriseth there's been a lot of questions relate to this on stackexchange and they all point to the page below. This seems to suggest access to map elements (via getStorageAt) can only be accessed if the keys are known.
It is a bit tricky, because there is another hash function evaluation envolved. If you did a full sync, yes. If you fast-synced, you might not have all values, but I think the geth team is currently building a remote server where you can get the missing values.
I have a question:
and I have been deploy this contract used by Remix,then invoke the test1() method. I debug this function In Debugger ,so Can see
I know the hash 0x28a06524829b6fed74df277d29b0e9d5843614df83ac4257ee812125675fdd54 and 0x4a9ab6c152794dd891130d65b49535f6222d24e4c9a3f06f616c3634b24c9fac,