Function modifiers #195

Closed
opened 2019-12-09 21:21:39 +09:00 by zxq9 · 6 comments
zxq9 commented 2019-12-09 21:21:39 +09:00 (Migrated from gitlab.com)

Created by: UlfNorell

From the Sophia discussion at the Sofia workshop.

Solidity lets you create modifiers that can wrap function bodies in arbitrary code.

*Created by: UlfNorell* *From the Sophia discussion at the Sofia workshop.* Solidity lets you create modifiers that can wrap function bodies in arbitrary code.
zxq9 commented 2019-12-09 21:26:14 +09:00 (Migrated from gitlab.com)

Created by: UlfNorell

Currently you can achieve this as

    function modifier(margs, body : () => 'a) : T('a) =
      stuff .. body() .. more_stuff

    function my_fun(args) =
      modifier(margs, () => body(args))

We could conceivably add some nicer syntax for applying the modifier.

What modifiers are people interested in using? The Solidity documentation has priced, owned, and noReentrancy. The latter is not needed in Sophia and the first two are simple require checks at the top of the function. It's not obvious that adding special syntax for this is worthwhile.

*Created by: UlfNorell* Currently you can achieve this as ``` function modifier(margs, body : () => 'a) : T('a) = stuff .. body() .. more_stuff function my_fun(args) = modifier(margs, () => body(args)) ``` We could conceivably add some nicer syntax for applying the modifier. What modifiers are people interested in using? The Solidity documentation has `priced`, `owned`, and `noReentrancy`. The latter is not needed in Sophia and the first two are simple `require` checks at the top of the function. It's not obvious that adding special syntax for this is worthwhile.
zxq9 commented 2019-12-11 08:52:40 +09:00 (Migrated from gitlab.com)

Created by: nikita-fuchs

Modifiers are highly useful in cases where you want to restrict the access to some functions to a specific set of users. E.g. 5 functions of the contract shall be only accessible if the user is "Rank A", owns a certain amount of value/tokens and also paid something to actually call this function. It makes a contract safer, smaller and more readable not to have to include all this logic multiple times everywhere.

*Created by: nikita-fuchs* Modifiers are highly useful in cases where you want to restrict the access to some functions to a specific set of users. E.g. 5 functions of the contract shall be only accessible if the user is "Rank A", owns a certain amount of value/tokens and also paid something to actually call this function. It makes a contract safer, smaller and more readable not to have to include all this logic multiple times everywhere.
zxq9 commented 2019-12-11 15:11:44 +09:00 (Migrated from gitlab.com)

Created by: UlfNorell

not to have to include all this logic multiple times everywhere

You are allowed to not copy-paste code even without special syntax for modifiers.

The only the difference is between (imaginary syntax)

@mustBeRankA()
entrypoint foo(...) =
  bla

and

entrypoint foo(...) =
  mustBeRankA()
  bla
*Created by: UlfNorell* > not to have to include all this logic multiple times everywhere You are allowed to not copy-paste code even without special syntax for modifiers. The only the difference is between (imaginary syntax) ```sophia @mustBeRankA() entrypoint foo(...) = bla ``` and ```sophia entrypoint foo(...) = mustBeRankA() bla ```
zxq9 commented 2019-12-11 19:11:08 +09:00 (Migrated from gitlab.com)

Created by: nikita-fuchs

Almost, as a modifier wraps the function call, right now it would be something like

entrypoint foo(params) =
  performChecks(params)
  ...function body...
  tidyUpAfterwards(params)

I forgot to mention the last part - sometimes you put sanity checks to double-check the outcome into the modifier or/and add some additional automation, which one might forget to perform when needing to add manually to function's end. If sophia improvements are decided to be done anyway, modifiers would be really nice, as they used to come really handy for not forgetting to implement safety checks in the past. Is the decorator syntax the one you'd propose, Ulf ? (I have no strong opinion here)

*Created by: nikita-fuchs* Almost, as a modifier _wraps_ the function call, right now it would be something like ``` entrypoint foo(params) = performChecks(params) ...function body... tidyUpAfterwards(params) ``` I forgot to mention the last part - sometimes you put sanity checks to double-check the outcome into the modifier or/and add some additional automation, which one might forget to perform when needing to add manually to function's end. If sophia improvements are decided to be done anyway, modifiers would be really nice, as they used to come really handy for not forgetting to implement safety checks in the past. Is the decorator syntax the one you'd propose, Ulf ? (I have no strong opinion here)
zxq9 commented 2019-12-11 19:15:27 +09:00 (Migrated from gitlab.com)

Created by: UlfNorell

That's why I asked for use cases which aren't simple access checks. What is a concrete example of tidyUpAfterwards(..)?

*Created by: UlfNorell* That's why I asked for use cases which aren't simple access checks. What is a concrete example of `tidyUpAfterwards(..)`?
zxq9 commented 2020-01-14 22:23:26 +09:00 (Migrated from gitlab.com)

Created by: UlfNorell

Closing for now. Reopen if needed with actual use cases.

*Created by: UlfNorell* Closing for now. Reopen if needed with actual use cases.
Sign in to join this conversation.
No Milestone
No project
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: QPQ-AG/sophia#195
No description provided.