-
-
Notifications
You must be signed in to change notification settings - Fork 43
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
Allow exact match of target === input amount during accumulation. #106
Conversation
Please add a test. |
|
||
const t3 = (strategy) => { | ||
const FEE = 0n // no fee to test exact amounts | ||
const est = new btc._Estimator(inputs, [{ ...OUTPUTS[0], amount: inputsTotalAmount }], { | ||
feePerByte: FEE, | ||
changeAddress: '2MshuFeVGhXVdRv77UcJYvRBi2JyTNwgSR2', | ||
network: regtest, | ||
allowLegacyWitnessUtxo: true, | ||
createTx: true, | ||
allowSameUtxo: true, | ||
}); | ||
const acc = est.result(strategy); | ||
if (!acc) return; | ||
const i = acc.inputs.map((i) => i.witnessUtxo.amount); | ||
const o = acc.outputs.map((i) => i.amount); | ||
const tx = acc.tx; | ||
tx.sign(privKey1); | ||
tx.finalize(); | ||
const expFee = BigInt(tx.vsize) * FEE; | ||
return { | ||
i, | ||
o, | ||
txFee: tx.fee, | ||
expFee, | ||
fee: acc.fee, | ||
change: acc.change, | ||
}; | ||
}; | ||
deepStrictEqual(t3('default'), { | ||
i: inputs.map((i) => i.witnessUtxo.amount).reverse(), | ||
o: [inputsTotalAmount], | ||
txFee: 0n, | ||
expFee: 0n, | ||
fee: 0n, | ||
change: false, | ||
}); | ||
deepStrictEqual(t3('accumSmallest'), { | ||
i: inputs.map((i) => i.witnessUtxo.amount), | ||
o: [inputsTotalAmount], | ||
txFee: 0n, | ||
expFee: 0n, | ||
fee: 0n, | ||
change: false, | ||
}); | ||
deepStrictEqual(t3('all'), { | ||
i: inputs.map((i) => i.witnessUtxo.amount), | ||
o: [inputsTotalAmount], | ||
txFee: 0n, | ||
expFee: 0n, | ||
fee: 0n, | ||
change: false, | ||
}); |
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.
New tests to verify that input amounts === output amounts
with 0 fees results in all inputs selected. This is logically sound with regard to how accumulate should work imo.
fee: 1691n, | ||
}); | ||
est.amount = 321n; | ||
est.amount = 322n; |
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.
Step 1 outputs -> 2 outputs
is now 1 sat higher since exact match doesn't result in extra input
deepStrictEqual(t(est, est.biggest, true), { | ||
amounts: [512n], | ||
change: 221n, | ||
change: 0n, |
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.
seems right 321 + 191 = 512
@@ -677,7 +680,7 @@ describe('UTXO Select', () => { | |||
for (const idx of indices) { | |||
tx.addInput(inputs[idx]); | |||
} | |||
tx.addOutputAddress(OUTPUTS[0].address, 100n, regtest); | |||
tx.addOutputAddress(OUTPUTS[0].address, est.amount, regtest); |
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.
fixed hardcoded amount here
@@ -715,8 +718,8 @@ describe('UTXO Select', () => { | |||
est.amount = 256n; | |||
deepStrictEqual(t(est, est.biggest, true), { | |||
amounts: [512n], | |||
change: 221n, | |||
expFee: 191n, | |||
change: 66n, |
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.
change explained by fix on L683
221 - ( 256 - 100) = 65
I'm not sure where the 1n fee difference comes from.
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.
the difference comes from size of serialization:
length(varint(1)) != length(varint(2))
change: 106n, | ||
expFee: 1685n, |
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.
change explained by fix on L683
261 - ( 256 - 100) = 105
I'm not sure where the 1n fee difference comes from.
deepStrictEqual(t(est, est.biggest, true), { | ||
amounts: [512n, 256n], | ||
change: 329n, | ||
change: 107n, |
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.
change explained by fix on L683
(512 + 256) - (322 + 339) = 107
329 - ( 322 - 100) = 107
i: [256n], | ||
o: [65n], | ||
txFee: 191n, | ||
expFee: 191n, | ||
fee: 191n, | ||
change: false, |
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.
seems right 65 + 191 = 256
Friendly bump on this, is there anything I can do to help getting this merged and published? |
Thanks |
Currently this example fails to create a valid selection even though the amount of sats available exactly matches the amount of sats needed.
This PR fixes the comparison so an exact match results in a valid selection.