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

Reconsider the UTXO sorting algorithm in DataSource.getUtxos() #241

Open
ShookLyngs opened this issue Jun 19, 2024 · 1 comment
Open

Reconsider the UTXO sorting algorithm in DataSource.getUtxos() #241

ShookLyngs opened this issue Jun 19, 2024 · 1 comment
Labels
enhancement New feature or request

Comments

@ShookLyngs
Copy link
Collaborator

ShookLyngs commented Jun 19, 2024

Issue

The current sorting algorithm used in the DataSource.getUtxos() API is: oldest comes first, newest comes last. It means when collecting UTXOs for paying transaction fees, the oldest UTXOs always get selected first.

Ref: https://github.com/ckb-cell/rgbpp-sdk/blob/develop/packages/btc/src/query/source.ts#L81-L88

This pattern can be problematic when constructing a transaction with an address that has many tiny and old UTXOs. These tiny, old UTXOs are hard to spend because including them in the transaction will cost more in network fees than the value contained in the UTXOs. To somewaht avoid the "value-cost-trap" issue, it is best to include the largest UTXOs in the transaction whenever possible.

Possible resolvers

Refactor the sorting algorithm

We can change the sorting algorithm in the mentioned code to: largest comes first, smallest comes last.

The benefit is that the success rate of transaction construction should rise, as tiny UTXOs will have less chance to be included for fees, based on the condition that the target address contains both large and tiny UTXOs. However, this sorting algorithm also comes with downsides. One of them being that there may be more transaction chains constructed on-chain, if an address has a large UTXO that contains high value, the UTXO can be spent over and over.

Add a sort option

We can add an option sort to the DataSource.getUtxos() API, so then the caller can desice which sorting algorithm to use. In normal transfers, the user can just use the oldest UTXOs for fee, and when sometimes the "value-cost trap" issue is occured, the user can manually specify and change the sorting algorithm, to use the largest UTXOs for fee.

@Flouse Flouse added the enhancement New feature or request label Jun 19, 2024
@Flouse
Copy link
Contributor

Flouse commented Jun 19, 2024

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants