I have this mapping:
mapping (uint256 => string ) private storedData;
I am trying to do this:
modifier dataNotStored(uint256 _index) {
require( keccak256(abi.encodePacked(storedData[_index])) == keccak256(abi.encodePacked('')) );
_;
}
function set(uint _index, string calldata _data_to_store) external dataNotStored(_index) {
storedData[_index] = _data_to_store;
}
My goal is to allow only one write for each entry of the mapping.
I think it is working, because from Remix IDE I get error only when trying a rewrite.
Is there a more efficient and intelligent way to avoid to overwrite a mapping?
modifieris just sugar-syntax. It has no impact on the output byte-code, hence no impact on gas-cost performance. – goodvibration Jan 22 '20 at 17:17