Skip to content
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

Merged
merged 1 commit into from
Aug 25, 2022

Conversation

lightmark
Copy link
Contributor

@lightmark lightmark commented Aug 23, 2022

…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 Reviewable

Copy link
Contributor

@areshand areshand left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice work, Bro.

@lightmark
Copy link
Contributor Author

@areshand boger memeda

@lightmark lightmark enabled auto-merge (squash) August 24, 2022 00:12
@lightmark lightmark force-pushed the token_deletion branch 2 times, most recently from 4f4e79b to 97a2f3a Compare August 24, 2022 04:14
@lightmark lightmark changed the title [Aptos Framework][Token] fully support deletion of TokenData and Toke… [Aptos Framework][Token] fully support deletion of TokenData and Token if reach 0 Aug 24, 2022
let mid = (left + right) / 2;
let mid = left + (right - left) / 2;
Copy link
Contributor

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?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

see kevin's comment

Copy link
Contributor Author

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...

Copy link
Contributor

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?

Copy link
Contributor Author

@lightmark lightmark Aug 24, 2022

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...

aptos-move/framework/aptos-token/sources/token.move Outdated Show resolved Hide resolved
@@ -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) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why add && *balance > amount?

Copy link
Contributor Author

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,

Copy link
Contributor

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?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point. @areshand

Copy link
Contributor Author

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, ..}?

Copy link
Contributor

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.

Copy link
Contributor

@movekevin movekevin left a 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) {
Copy link
Contributor

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?

@lightmark lightmark requested a review from 0xchloe August 24, 2022 19:41
@lightmark lightmark force-pushed the token_deletion branch 2 times, most recently from 19b64d9 to 885c44f Compare August 24, 2022 22:39
@lightmark
Copy link
Contributor Author

@jjleng plz take a look for .ts files

@github-actions
Copy link
Contributor

Forge is running with 153974bf53aaf8d40b6300e07ecafa45bdc6ce65

❌ Forge test failure on 153974bf53aaf8d40b6300e07ecafa45bdc6ce65

Forge test runner terminated

Forge is running with 4f4e79b8733679feaea165ac235e5448e79292e0

✅ Forge test success on 4f4e79b8733679feaea165ac235e5448e79292e0

performance benchmark with full nodes : 6761 TPS, 4427 ms latency, 7000 ms p99 latency,no expired txns
Test Ok

Forge is running with 97a2f3af343b919ceeac8f7948d928bbf2a5e8d1

✅ Forge test success on 97a2f3af343b919ceeac8f7948d928bbf2a5e8d1

performance benchmark with full nodes : 6533 TPS, 4570 ms latency, 6000 ms p99 latency,no expired txns
Test Ok

Forge is running with 885c44f303c4bafc481416c8e9aeb5a68ccd2f4b

✅ Forge test success on 885c44f303c4bafc481416c8e9aeb5a68ccd2f4b

performance benchmark with full nodes : 6289 TPS, 4777 ms latency, 6000 ms p99 latency,no expired txns
Test Ok

Forge is running with 0ec428c0ecccbbe5b6ba180ac8c73ba06de07547

✅ Forge test success on 0ec428c0ecccbbe5b6ba180ac8c73ba06de07547

performance benchmark with full nodes : 6297 TPS, 4751 ms latency, 6050 ms p99 latency,no expired txns
Test Ok

@lightmark lightmark merged commit 0e05976 into main Aug 25, 2022
@lightmark lightmark deleted the token_deletion branch August 25, 2022 01:44
0xchloe pushed a commit that referenced this pull request Aug 28, 2022
Markuze pushed a commit to Markuze/aptos-core that referenced this pull request Sep 2, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants