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

Split policy ID assets when overflow #236

Merged
merged 9 commits into from
Nov 24, 2021

Conversation

pedromtcosta
Copy link
Contributor

@pedromtcosta pedromtcosta commented Oct 29, 2021

Abstract

This PR makes changes the add_change_if_needed function from the TransactionBuilder class to split assets among multiple outputs when generating a change output which contains a large number of assets.

Motivation

While unlikely, it is possible that we run into an overflow on the size of the output when generating a change outputs which contains several assets. This can be handled by splitting these assets among multiple outputs.

Background

When we create a new instance of TransactionBuilder we specify a value for the max_value_size, which is a size limit for the value of each output in the transaction. If while building the change output it gets larger than max_value_size, the transaction building will result in an error.
See the task description on Asana.

Implementation

To handle the scenario described above, instead of just inserting all the assets into the change output directly, we loop through all the asset names from the Assets object which comes from the change estimator MultiAsset and insert them one by one into a new Assets object, defined as rebuilt_assets, and only afterwards we insert this rebuilt_assets object into the MultiAsset and then finally we add the MultiAsset into the output.

Before inserting each individual asset into the rebuilt_assets object, we simulate an insert with cloned objects to verify if adding this asset to the output will cause an overflow, by using the newly defined will_adding_asset_make_output_overflow function. If it doesn't cause the overflow, we just continue normally. If it does cause an overflow, we insert the current rebuilt_assets into the output and generate a new one and continue inserting the remaining assets into this new output.

We store all the MultiAsset objects from the outputs generated by the pack_nfts_for_change function into a Vec<MultiAsset> and change pack_nfts_for_change to return this vector instead of a single MultiAsset. Finally, we use the same logic for adding the NFT change we had before for each of the items returned by pack_nfts_for_change

before adding the fix for this error, we first make add test with a situation which leads to the exact error we are expecting, in this case the "NFTs too large for change output" error
@pedromtcosta pedromtcosta marked this pull request as ready for review October 29, 2021 22:28
@vsubhuman
Copy link
Contributor

@pedromtcosta , please resolve conflicts

@pedromtcosta pedromtcosta force-pushed the feature/split-policy-id-assets-in-overflow branch from b41526e to 17b02f1 Compare November 23, 2021 19:51
@pedromtcosta
Copy link
Contributor Author

@vsubhuman Conflicts resolved

@vsubhuman
Copy link
Contributor

@ozgrakkurt plz review

@vsubhuman
Copy link
Contributor

@rooooooooob , check plz if you have a chance

Copy link
Contributor

@rooooooooob rooooooooob left a comment

Choose a reason for hiding this comment

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

LGTM

let (policy, asset_name, value) = asset_to_add;
let mut current_assets_clone = current_assets.clone();
current_assets_clone.insert(&asset_name, &value);
let mut amount_clone = output.amount.clone();
Copy link
Contributor

Choose a reason for hiding this comment

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

We should be able to avoid this clone but it's nothing important

@rooooooooob
Copy link
Contributor

rooooooooob commented Nov 24, 2021

We should squash and merge this instead of the create a merge commit option IMO. We've been having so many merging master into PR branches lately and the commit history has been getting really dirty. It might be worth doing that in general now.

@vsubhuman vsubhuman added this to the 10.0.0 milestone Nov 24, 2021
@vsubhuman vsubhuman merged commit 0fe8676 into master Nov 24, 2021
@vsubhuman vsubhuman deleted the feature/split-policy-id-assets-in-overflow branch November 24, 2021 11:53
@vsubhuman vsubhuman mentioned this pull request Feb 6, 2022
40 tasks
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.

3 participants