-
Notifications
You must be signed in to change notification settings - Fork 3.7k
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
[Aptos Framework][Token] fully support deletion of TokenData and Token if reach 0 #3367
Conversation
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.
Nice work, Bro.
@areshand boger memeda |
4f4e79b
to
97a2f3a
Compare
let mid = (left + right) / 2; | ||
let mid = left + (right - left) / 2; |
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.
left + ((right - left) / 2) = left + right/2 - left /2 = left/2 + right/2 = (left + right) / 2
Why bother?
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.
see kevin's comment
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.
please help me fix the ts tests tmr...
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.
thoughtful lol. then how about left/2 + right/2
?
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.
this cannot ensure the correctness because of division rounding towards 0
if left = 1 and right = 3, left/2 + right/2 = 1.... but you want 2...
@@ -520,11 +523,13 @@ module aptos_token::token { | |||
error::not_found(EBALANCE_NOT_PUBLISHED), | |||
); | |||
let balance = &mut table::borrow_mut(tokens, id).amount; | |||
if (id.property_version == 0) { | |||
if (id.property_version == 0 && *balance > amount) { |
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.
Why add && *balance > amount?
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.
if balance == amount, I will delete the entry from the map. otherwise it is still there since the final balance is not 0,
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.
Can we remove the id.property_version == 0 check ? If there's a single version and it gets deleted, balance and amount should both be 1?
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. @areshand
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.
another question is: is it possible that amount = 0, and return Token {amount: 0, ..}?
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.
@lightmark this is a good catch. we should assert amount > 0. It doesn't make sense to withdraw 0 token.
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.
Looks fine to me. I'll leave it to Bo to have a final say though
@@ -520,11 +523,13 @@ module aptos_token::token { | |||
error::not_found(EBALANCE_NOT_PUBLISHED), | |||
); | |||
let balance = &mut table::borrow_mut(tokens, id).amount; | |||
if (id.property_version == 0) { | |||
if (id.property_version == 0 && *balance > amount) { |
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.
Can we remove the id.property_version == 0 check ? If there's a single version and it gets deleted, balance and amount should both be 1?
19b64d9
to
885c44f
Compare
@jjleng plz take a look for |
885c44f
to
0ec428c
Compare
Forge is running with
|
…n from storage
Description
we need to delete a token entry from the table in
TokenStore
when its amount drops to 0.Same applies to
TokenData
in a collection if supply drops to 0.Move the sufficient balance checker inside
withdraw_with_event_internal
function to achieve better encapsulation.Also, I don't think (left + right) / 2 is a good practice for binary search (theoretically potential overflow).
Test Plan
ut
This change is