Mappings are kept in storage. Cost of storing in a mapping is approximately the cost of an SSTORE operation, which varies depending on if you're writing an entirely new value or if you're overwriting an old value. Check out evm.codes for more information about the cost of an SSTORE operation.
Every smart contract on Ethereum has its own key/value storage where keys and values are both 32 bytes. Solidity mapping values are placed into this key/value storage according to the logic defined in the Solidity documentation regarding state variable layout in storage. For mappings, this is:
storage_key = keccak256(concat(uint256(mapping_key), uint256(mapping_variable_position)))
Where mapping_key
is the key in the mapping that you're updating and mapping_variable_position
is the position of the mapping within your contract. mapping_variable_position
literally is just the order in which your mapping was defined -- if the mapping is the very first variable in the contract then mapping_variable_position
will be 0. Second variable will be 1, etc etc.
For your example, assuming the id
mapping is the first variable defined in the contract, id[1]
is found at:
mapping_key = 1
mapping_variable_position = 0
storage_key = keccak256(concat(uint256(mapping_key), uint256(mapping_variable_position)))
= keccak256(
concat(
0x0000000000000000000000000000000000000000000000000000000000000001,
0x0000000000000000000000000000000000000000000000000000000000000000
)
)
= 0xada5013122d395ba3c54772283fb069b10426056ef8ca54750cb9bb552a59e7d