0

Mapping do not have a length, nor do they have the concept of key or a values being set.

Is this statement True or False?

Please provide the explanation also.

The solidity docs say “Mappings can be seen as hash tables which are virtually initialized such that every possible key exists and is mapped to a value whose byte-representation is all zeros

1 Answer 1

2

True, why because we do have the concept of keys and values in mappings but they are not really being set inside the mapping, only its keccak256 hash is used to look up the value.


Why they use keccak256 hash instead of actual value in mappings (As per docs) :

Types that do not occupy the full 32 bytes might contain “dirty higher order bits”. This is especially important if you access msg.data - it poses a malleability risk: You can craft transactions that call a function f(uint8 x) with a raw byte argument of 0xff000001 and with 0x00000001. Both are fed to the contract and both will look like the number 1 as far as x is concerned, but msg.data will be different, so if you use keccak256(msg.data) for anything, you will get different results.


If you want to read out some more info and want to understand how it works internally, you should try reading -> How you can clear Mappings ?

1
  • Have you been able to reproduce the example you mention? When I try to pass in 0xff000001 to a uint8 param I get the error Error encoding arguments: Error: value out-of-bounds (argument=null, value="0xff000001", code=INVALID_ARGUMENT, version=abi/5.5.0). This was in Remix. Commented Feb 13, 2022 at 5:40

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.