mintlayer-core
mintlayer-core copied to clipboard
Fix/wallet fee calculation
- The initial bug was caused by the calculation of the TX size with 0 inputs, and the bug got triggered when a TX was constructed with 100+ inputs which caused the compact length to have 1 more byte then before.
This was fixed by passing the number of inputs to the
tx_size_with_outputs. - Currently as before the fee of a TX can be up to 16 bytes more per change output because of the unknown amount of change that will be returned, and the calculation always uses Amount::MAX.
- The fees test has been rewritten to have both a TX with many inputs up to 1k and a sweep TX which uses a separate implementation which doesn't have a change output and always pays correctly the exact fee.
- In the new test a rounding error was discovered, and fixed which happens in case of individual input fee calculation vs total fee calculation using entire tx size.
- So now we still calculate the fee per input for the selection algorithm to know which input has how much effective value, but at the end after the inputs have been selected we recalculate the fee and add the difference back as change. (it should always be up to number of inputs in atoms, which is not a lot but still was crushing the test)
- Also added the missing limit for tx size/weight, which now should prevent creating a TX with size over 1MB retrieved from chain_config.
Closes #1208