-
Notifications
You must be signed in to change notification settings - Fork 125
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
Conversation
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
…y-id-assets-in-overflow # Conflicts: # rust/src/tx_builder.rs
@pedromtcosta , please resolve conflicts |
b41526e
to
17b02f1
Compare
@vsubhuman Conflicts resolved |
…y-id-assets-in-overflow # Conflicts: # rust/src/tx_builder.rs
@ozgrakkurt plz review |
@rooooooooob , check plz if you have a chance |
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.
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(); |
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.
We should be able to avoid this clone but it's nothing important
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. |
Abstract
This PR makes changes the
add_change_if_needed
function from theTransactionBuilder
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 themax_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 thanmax_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 estimatorMultiAsset
and insert them one by one into a newAssets
object, defined asrebuilt_assets
, and only afterwards we insert thisrebuilt_assets
object into theMultiAsset
and then finally we add theMultiAsset
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 definedwill_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 currentrebuilt_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 thepack_nfts_for_change
function into aVec<MultiAsset>
and changepack_nfts_for_change
to return this vector instead of a singleMultiAsset
. Finally, we use the same logic for adding the NFT change we had before for each of the items returned bypack_nfts_for_change