-
Notifications
You must be signed in to change notification settings - Fork 123
sweepbatcher: allow adding groups of inputs #911
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
f46a1d9 to
790b8ed
Compare
bhandras
left a comment
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.
Looks really good, just a few nits and comments.
| if err != nil { | ||
| return fmt.Errorf("failed to estimate tx weight for "+ | ||
| "sweep %x: %w", sweep.swapHash[:6], err) | ||
| "swap %s: %w", swap[:6], err) |
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.
nit: i'd not rename "sweep" to "swap" as sweep is more general. Applies to everywhere else too.
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.
sweepbatcher/sweep_batch.go
Outdated
| } | ||
|
|
||
| // Track if there is an existing sweep and a new sweep among the sweeps. | ||
| var hasExisting, hasNew bool |
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.
nit: instead of hasExisting and hasNew consider using a counter instead. It might make the code a bit more readable.
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.
Replaced the flags with counts numExisting, numNew.
sweepbatcher/sweep_batch.go
Outdated
| // Make sure the whole group is either new or existing. If this is not | ||
| // the case, this might be a bug, so print a warning. | ||
| if hasExisting && hasNew { | ||
| b.Warnf("There are existing and new sweeps among the group") |
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.
If using a counter approach we could also add more context to this warn log by logging the count of existing vs len(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.
Done.
sweepbatcher/sweep_batch.go
Outdated
| } | ||
|
|
||
| // Make sure all the sweeps spend different outpoints. | ||
| used := make(map[wire.OutPoint]struct{}, len(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.
nit: instead of used we could call it outpoints for readability.
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.
Renamed it to outpointsSet.
sweepbatcher/sweep_batcher.go
Outdated
| // Group specifies multiple inputs in the same request. All the inputs | ||
| // belong to the same swap and are added to the same batch. If it is | ||
| // specified, OutPoint and Value must not be specified. | ||
| Group []Input |
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.
Could we just have the group? We don't really need the Output and Value then.
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. Thanks!
Also renamed the field from Group to Inputs.
sweepbatcher/sweep_batcher.go
Outdated
|
|
||
| if len(sweepReq.Group) != 0 { | ||
| // Make sure sweepReq.Outpoint, sweepReq.Value were not filled. | ||
| if sweepReq.Outpoint != (wire.OutPoint{}) { |
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.
... So then we wouldn't need these checks either.
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.
sweepbatcher/sweep_batcher.go
Outdated
| return sweeps, nil | ||
| } | ||
|
|
||
| s, err := b.loadSweep( |
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.
... And this loadSweep.
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.
bhandras
left a comment
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 🎉
|
|
||
| // Make sure the whole group is either new or existing. If this is not | ||
| // the case, this might be a bug, so print a warning. | ||
| if numExisting > 0 && numNew > 0 { |
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.
nit: we could also just check countExisting != len(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.
We shouldn't return an error, if all the sweeps added are new. If all sweeps are new, countExisting=0 (and numNew=len(sweeps)).
sweepbatcher/sweep_batch.go
Outdated
| if err != nil { | ||
| return false, err | ||
| // For an existing group, update the sweeps in the batch. | ||
| if numExisting > 0 { |
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.
nit: ... and countExisting == len(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.
Fixed!
hieblmi
left a comment
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.
Nice improvement. I left a question.
sweepbatcher/sweep_batch.go
Outdated
| return false, err | ||
| } | ||
|
|
||
| // Track if there is an existing sweep and a new sweep among the 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.
nit: Track how many new and existing sweeps are among the 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.
Fixed. Thanks!
| // Add the sweep to the batch's sweeps. | ||
| b.Infof("adding sweep %x", sweep.swapHash[:6]) | ||
| b.sweeps[sweep.outpoint] = *sweep | ||
| // Here is the code to add new sweeps to a batch. |
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.
should we check here that len(sweeps) is numNew and return an error otherwise.
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.
This logically follows from the way numExisting and numNew were calculated and from the fact that we have checked above that only one of these categories has any sweeps in it.
I added the following double-check just in case:
} else if numNew != len(sweeps) {
// Sanity check: all the sweeps must be either existing or new.
// We have checked this above, let's check here as well.
return false, fmt.Errorf("bug in numExisting and numNew logic:"+
" numExisting=%d, numNew=%d, len(sweeps)=%d, "+
"len(b.sweeps)=%d", numExisting, numNew, len(sweeps),
len(b.sweeps))| } | ||
|
|
||
| if err := b.persistSweep(ctx, *s, false); err != nil { | ||
| return true, err |
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.
does this count as accepted? Should we return false here?
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 was accepted in-memory, but failed to be persisted. Loop will shutdown with this error anyway.
Also this matches to the previous behavior:
return true, b.persistSweep(ctx, *sweep, false)c1cb2bf to
8f050a7
Compare
45c24c0 to
283f31c
Compare
|
Added a check to addSweeps that the number of sweeps is not zero. // This must be a bug, so log a warning.
if len(sweeps) == 0 {
b.Warnf("An attempt to add zero sweeps.")
return false, nil
} |
hieblmi
left a comment
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!
|
Rebased |
A group of inputs can be added by passing it in SweepRequest.Inputs field. All the inputs belong to the same swap and are added to the same batch.
A group of inputs can be added by passing it in
SweepRequest.Inputsfield.All the inputs belong to the same swap and are added to the same batch.
Pull Request Checklist
release_notes.mdif your PR contains major features, breaking changes or bugfixes