-
Notifications
You must be signed in to change notification settings - Fork 123
sweepbatcher: batch change outputs #976
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
Conversation
19b13cd
to
c7091de
Compare
/gemini review |
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.
Code Review
This pull request introduces the ability to handle change outputs in sweep batches, which is a significant feature. I've found a critical bug in fee calculation within CheckSignedTx
that needs to be addressed. I've also pointed out a minor code simplification and a gap in test coverage for the new logic.
c20504a
to
0567051
Compare
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.
Code Review
The pull request introduces functionality to handle change outputs in sweep batcher, specifically for presigned transactions. The changes involve modifying the sweep
struct, updating transaction construction to include change outputs, and adding a function to retrieve and de-duplicate change outputs. The code also includes unit tests for the new functionality. The changes look good overall, but there are a few areas that could be improved for robustness and clarity.
0567051
to
950fd4e
Compare
576270f
to
35f5810
Compare
This seems to be outdated. |
35f5810
to
4c58f85
Compare
fe4ec68
to
c0418e3
Compare
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! 🥇
Added a couple of proposals.
a728c80
to
1b31b88
Compare
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! 💾
I sent PR hieblmi#1 with some changes that I propose, mostly for tests, but also most importantly to use minRelayFeeRate in presign
and ensurePresigned
as start value for presigning and when passing to SignTx. Please review the changes. You can squash the changes into proper commits or keep them as a separate commit.
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! 🍇
I think, it is ready. Great work!
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.
Left some comments re multiple change outputs
@@ -210,6 +210,13 @@ func estimateBatchWeight(batch *batch) (feeDetails, error) { | |||
err) | |||
} | |||
|
|||
// Add change output weights. | |||
for _, s := range batch.sweeps { | |||
if s.change != nil { |
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.
Would it make sense to add a check for if a change pkscript repeats that we add to the other outputs value instead of adding a new output with the same pkscript? Is that something that can happen (e.g. on static loop in if change is the static addr change would be the same?)
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.
Very good suggestion!
sweepbatcher/sweep_batch.go
Outdated
@@ -1255,6 +1260,13 @@ func constructUnsignedTx(sweeps []sweep, address btcutil.Address, | |||
LockTime: uint32(currentHeight), | |||
} | |||
|
|||
var changeOutputs []*wire.TxOut | |||
for _, sweep := range sweeps { |
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.
I think here a similiar comment to my other comment, what happens if the pkscript is the same between multiple sweeps?
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.
It totally makes sense to consolidate values of change outputs with identical pkscript. I've changed the workings to
do so here and in the greedy batch selection. I will also add unit tests for this.
}, | ||
|
||
{ | ||
name: "all sweeps different change outputs", |
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.
I think a testcase "all sweeps same change output" makes sense and the result should be a single change output right?
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.
added "all sweeps identical change pkscripts"
I've added commit |
d2eee3f
to
fd4ee81
Compare
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! thanks for the additional change consolidation 🙏
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! 🌴
The code of last commit is great. I propose to add few tests to validate it.
} | ||
|
||
weight.AddOutput(s.change.PkScript) | ||
changeOutputs[pkScriptString] = struct{}{} |
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.
👍
Please add a test case to TestEstimateBatchWeight
.
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.
Added "two sweeps regular batch with identical change"
.
sweepbatcher/sweep_batch_test.go
Outdated
@@ -395,6 +395,57 @@ func TestConstructUnsignedTx(t *testing.T) { | |||
wantFee: 1248, | |||
}, | |||
|
|||
{ | |||
name: "all sweeps identical change pkscripts", |
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.
s/all sweeps identical change pkscripts/identical change pkscripts/
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.
changed
wantWeight: 1076, | ||
wantFeeForWeight: 1076, | ||
wantFee: 1076, | ||
}, |
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.
Please add another test case with equal change pkscripts, but different satoshi values.
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.
added "identical change pkscripts different values"
.
|
||
// Mine a blocks to trigger republishing. | ||
require.NoError(t, lnd.NotifyHeight(601)) | ||
} |
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.
I propose to add a unit test with two swaps sharing the same change output pkscript.
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.
added testPresigned_presigned_group_with_identical_change_pkscript
.
16ebc11
to
c7b0428
Compare
This commit adds an optional change output to the sweep struct.
Presigning sweeps takes change outputs into account. Each primary deposit id of a sweep group points to an optional change output. sweepbatcher.presign scans all passed sweeps for change outputs and passes them to constructUnsignedTx. Optional change of a swap is encoded in its sweeps as a pointer to the same change output. This change is taken into account when constructing the unsigned batch transaction when it comes to tx weight and outputs.
if constructUnsignedTx constructs a batch transaction that is below the minimum relay fee, an error is returned.
- ensurePresigned: use passed minRelayFeeRate instead of chainfee.FeePerKwFloor - presign: use minRelayFeeRate for start and minRelayFee - presign: make sure minRelayFeeRate is set - add tests for presign to test this new behavior; make sure the number of transactions is lower if minRelayFeeRate is higher - update error message in constructUnsignedTx: use <, not <= (more accurate) - use utils.DustLimitForPkScript instead of lnwallet.DustLimitForSize in tests - in tests adjust amounts to edge values, add controls
6679a5d
to
29161bb
Compare
@starius I had to sort the change outputs in |
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! 🥇
nit: I propose to use bip-0069 for outputs sorting.
sweepbatcher/sweep_batch.go
Outdated
} | ||
|
||
// Sort the keys | ||
sort.Strings(keys) |
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.
Let's use BIP-0069 for outputs sorting (change only, so main output is 0).
We have it implemented for inputs in utils/get_prevout_info.go
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.
I've added a bip69 compatible utils method to sort tx outputs. I've added it in a separate commit 245a666.
batch separate change outputs with identical pkscripts are tallied up and consolidated to a single change output.
29161bb
to
d00b78e
Compare
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! 🏆
The batcher receives the ability to sign and publish batches with change outputs.
Change is created when the clients choose fractional swap amounts from the input values to be swapped. Usually the change is sent back to client's addresses.
If change is provided to
ensurePresigned
,presign
andpublishPresigned
, it is expected to be placed within the passed[]sweep
.Each
sweep
optionally holds a pointer to a change output. Only the first sweep in a sweep group called primary deposit will hold a pointer to the respective swap's change output.