Fix for issue with fee calculation and cbor overhead for complex utxo… #2603
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
I have tried address two fundamental issues here, the first is the decoding of the cbor transactions witness and encoding again, while the size should remain constant there is a possibility that additional data could be introduced, the data should only be encoded and never decoded, keep a copy if needed. this is addressed in the submit transaction which I merged to a single function for simplicity, can't see a reason for it being 2 ..
the second is the fee calculation:
the code is currently building a “draft” transaction with zero fee, stubbed in the correct number of fake signatures, serialised to get body+stubs size, then computed:
baseFee = a * draftSize + b
then adds change output by doing:
changeFee = a * sizeOf(changeOutput)
combines the fees*
totalFee = baseFee + changeFee
But it never ran the full
a * txSize + b
over the larger transaction.For complex transactions which may have numerous outputs / inputs adding additional data also introduces extra bytes for the CBOR map/array the difference might be tiny but compounds over a couple of areas to be sufficient enough to make the transaction fail.
What we were seeing:
transactions with no change were no problem they succeeded, when change was introduced the calulation was slightly too smal and did not rerun the calculation to include the cbor overhead of the extra outputs.
Where there was change but just a single utxo output these also seemed to work but anywhere there was multiple UTXOs and change it would fail.
There are a couple of options the one I drafted introduces a slight buffer depending on the number of inputs, the greater the inputs the more buffer required.
We need to decide which is best, a more precise fee calculation with the potential of some failed transactions or introducing a slight buffer when there are more inputs we may be unable to be precise with the calculation
Example response from wallet...