Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Initial implementation #1
Initial implementation #1
Changes from 5 commits
04e638a
1f10ee2
c6e67e2
b6b6d9a
7502c84
2f4dcff
223a48d
dfb9306
File filter
Filter by extension
Conversations
Jump to
There are no files selected for viewing
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure if relevant, but take a look at this use-ink/ink#1831 regarding overflow checks, because I think there are some subtleties
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Indeed it looks like soon all unchecked arithmetic is going to be banned from ink! contracts. I replaced all occurrences of + and - (which were impossible to overflow "by design") with saturating_add and saturating_sub.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wonder whether it wouldn't be slightly better to do it like:
self.data.allowances.remove((owner, spender))
in line L186 -remove
returns "previous" value.new_allowance
(amount
) equals0
:insert
.Seems like that would be 1 access to the
Mapping
less.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good point, it could be a little bit more efficient.
Currently there are 3 branches this code can end up with and the resulting
Mapping
operations are:In the version you propose that would be:
Not sure what is the relative cost of
remove
vsget
, but the total number of operations globally is the same. Obviously, we expect variant 2 to "fire" much more often than variant 1, so expected number of operations is lower in your version. But I feel like this is a miniscule gain that sacrifices quite a lot of readability in a code that is supposed to be shown to community, so I'd lean towards simplicity > efficiency in this caseThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
current_allowance < by
: 1 remove (no insert since we would fail early withInsufficientAllowance
current_allowance = by
: 1 removecurrent_allowance > by
: 1 remove + 1 insertThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since we're not 100% sure
Mapping.take()
is stable, lets stay with the simpler 100% safe implementation for now