1

Context:

A common access check or modifier is to use something like: require(boolMapping[msg.sender] == true); to ensure the caller is approved to use a function.

Since mapping (address => uint256) uintMapping defaults all unset values to 0, my question is:

Question: Is it safe to use this:

require(uintMapping[msg.sender] > 0, "not authorised");

instead of this:

require(boolMapping[msg.sender] == true, "not authorised");

to restrict use of a function, assuming pre-authorised accounts are set with values greater than zero?

(This would help condense the amount of storage required on a contract where users already have associated non-zero numbers, as we wouldn't need to use a struct or a second auth mapping).

Thank you!

2
  • 1
    Why? If zero isn't a valid value then it should be fine, but you have to enforce when saving to the mapping that zero isn't stored.
    – Ismael
    Commented Jan 29, 2022 at 19:54
  • Thanks @Ismael . Good point about enforcing non-zero values for members in the mapping. The reason is to save a bit of gas and increase efficiency with my storage structures (previously I had a uintMapping and a boolMapping, but the boolMapping was only ever used to check authorisation before using the uintMapping).
    – panda
    Commented Jan 29, 2022 at 20:50

1 Answer 1

2

As far as I know, there shouldn't be any issue with this approach.

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.