sweepbatcher: notify caller about confirmations#919
Conversation
| @@ -213,8 +213,8 @@ type dbBatch struct { | |||
| // ID is the unique identifier of the batch. | |||
There was a problem hiding this comment.
Maybe stupid question, but missing lots of context. This commit changes more than the mock, and feels a bit complicated, can you maybe elaborate what the changes to the other files are?
There was a problem hiding this comment.
I updated the commit message:
sweepbatcher: align dbBatch type with DB schema
Previously, dbBatch had a State field (enum: Open, Closed, Confirmed), but in
the database it is represented as a boolean Confirmed. The Closed state was
stored the same way as Open. This wasn't an issue in practice, since an Open
batch is quickly transitioned to Closed after startup.
However, the in-memory mock stores plain dbBatch instances, leading to
inconsistent behavior between the mock and the real DB-backed store. This
commit updates dbBatch to match the database representation by replacing
Also added a note to the description of Closed const:
// Closed is the state in which the batch is no longer able to accept
// new sweeps. NOTE: this state exists only in-memory. In the database
// it is stored as Open and converted to Closed after a spend
// notification arrives (quickly after start of Batch.Run).
Closed batchState = 1
bhandras
left a comment
There was a problem hiding this comment.
Looks good. Perhaps we don't even need the "closed" state?
| // If the sweep's notifier is empty then this means that a swap | ||
| // is not waiting to read an update from it, so we can skip | ||
| // the notification part. | ||
| if s.notifier == nil || *s.notifier == (SpendNotifier{}) { |
There was a problem hiding this comment.
Why check *s.notifier == (SpendNotifier{} ?
There was a problem hiding this comment.
It was a copy-paste. I updated this line:
if s.notifier == nil || s.notifier.SpendErrChan == nil {|
|
||
| if row.Confirmed { | ||
| batch.State = batchOpen | ||
| batch.State = batchConfirmed |
There was a problem hiding this comment.
I assume this just resulted in the batches restarting (and then getting to confirmed state again after spend notification received)?
There was a problem hiding this comment.
Yes, and then after confirmation notification (after 3 confs).
Open -(spend notif)-> Closed -(conf notif)-> Confirmed
| // Then we get the total amount that was swept by the batch. | ||
| totalSwept, err := b.store.TotalSweptAmount(ctx, parentBatchID) | ||
| if err != nil { | ||
| cancel() |
There was a problem hiding this comment.
Hmm, this is a great fix, but how did we ever not get cancelled?
There was a problem hiding this comment.
This code path is executed only when a fully confirmed sweep is added again to the sweeper. This may happen if the daemon was down when the sweep was fully confirmed (got 3rd confirmation). This is not the main code path.
| // manages to be added during this time, it will be | ||
| // detected as missing when analyzing the spend | ||
| // notification and will be added to new batch. | ||
| batch.state = Open |
There was a problem hiding this comment.
But this also means that any new sweeps could be added, and then we just would try to publish the batch again (and fail as inputs are spent). It's robust so not a huge issue, but perhaps we could avoid.
There was a problem hiding this comment.
Avoiding this would require a DB migration, which doesn’t seem worthwhile just to improve this edge case.
I think it makes sense to keep Also maybe greedy batch selection can work worse if we let it add to Closed. |
|
Converting to draft. I'm working to add more commits here and to rebase it on top of #891 |
a67a666 to
e234022
Compare
494ebfb to
c292ec2
Compare
bhandras
left a comment
There was a problem hiding this comment.
Really nice set of fixes! Making sweep-batcher more robust one step at a time 🚀
LGTM 🎉
| // new sweeps. | ||
| // new sweeps. NOTE: this state exists only in-memory. In the database | ||
| // it is stored as Open and converted to Closed after a spend | ||
| // notification arrives (quickly after start of Batch.Run). |
| // manages to be added during this time, it will be | ||
| // detected as missing when analyzing the spend | ||
| // notification and will be added to new batch. | ||
| batch.state = Open |
|
|
||
| spendDetail := SpendDetail{ | ||
| Tx: spendTx, | ||
| OnChainFeePortion: getFeePortionPaidBySweep( |
There was a problem hiding this comment.
Alternatively we could communicate the fee portion once the sweep is fully-confirmed. wdyt?
There was a problem hiding this comment.
I added OnChainFeePortion to type ConfDetail, which is send through ConfChan.
I did it in a separate commit to make it clear what changed. I plan to squash it with the previous commit before merging.
Forgot to return the calculated total value.
Support sending errors to error channels returned by RegisterSpendNtfn and RegisterConfirmationsNtfn.
Function monitorSpendAndNotify used to cancel the context passed to RegisterSpendNtfn right after starting the goroutine processing results. Spend notifications were missed. Now the context is canceled when the goroutine finishes.
|
I removed commits related to presigned mode from this PR. I'll open another PR. |
hieblmi
left a comment
There was a problem hiding this comment.
LGTM, great set of fixes!
| // Then we get the total amount that was swept by the batch. | ||
| totalSwept, err := b.store.TotalSweptAmount(ctx, parentBatchID) | ||
| if err != nil { | ||
| cancel() |
| primarySweep.outpoint.String()) | ||
|
|
||
| for { | ||
| select { |
There was a problem hiding this comment.
please fix typo in commit message.
| b.state = Closed | ||
|
|
||
| return b.persist(ctx) | ||
| if err := b.persist(ctx); err != nil { |
There was a problem hiding this comment.
nit: can do err = b.persist...
| b.state = Closed | ||
|
|
||
| return b.persist(ctx) | ||
| if err := b.persist(ctx); err != nil { |
| // continue. | ||
| case <-notifier.QuitChan: | ||
| } | ||
| }(s.notifier) |
The loop always had exactly one iteration.
If monitorConfirmations fails, we still want to persist the state to DB.
Previously, dbBatch had a State field (enum: Open, Closed, Confirmed), but in the database it is represented as a boolean Confirmed. The Closed state was stored the same way as Open. This wasn't an issue in practice, since an Open batch is quickly transitioned to Closed after startup. However, the in-memory mock stores plain dbBatch instances, leading to inconsistent behavior between the mock and the real DB-backed store. This commit updates dbBatch to match the database representation by replacing the State field with a Confirmed boolean.
Add fields ConfChan and ConfErrChan to SpendNotifier type which is a part of SweepRequest passed to AddSweep method. This is needed to reuse confirmation notifications on the calling side the same way it is done for spending notifications.
| case <-ctx.Done(): | ||
| return | ||
| } | ||
| case <-ctx.Done(): |
There was a problem hiding this comment.
prior we returned here and in all other ctx.done cases, is this intended? We just fall through to the last return right?
There was a problem hiding this comment.
Prior we returned in all the cases of the select, so the for loop always used to have exactly one iteration. That is why the for loop is not needed and return statements can be removed, because this is the end of the function anyway.
Add fields
ConfChanandConfErrChantosweepbatcher.SpendNotifiertype which is a part ofSweepRequestpassed toAddSweepmethod.This is needed to reuse confirmation notifications on the calling side the same way it is done for spending notifications.
Added missing tests for notifications sent through channels of
SpendNotifier.Pull Request Checklist
release_notes.mdif your PR contains major features, breaking changes or bugfixes