Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 6 additions & 17 deletions sweepbatcher/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -213,8 +213,8 @@ type dbBatch struct {
// ID is the unique identifier of the batch.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

ID int32

// State is the current state of the batch.
State string
// Confirmed is set when the batch is fully confirmed.
Confirmed bool

// BatchTxid is the txid of the batch transaction.
BatchTxid chainhash.Hash
Expand Down Expand Up @@ -255,11 +255,8 @@ type dbSweep struct {
// convertBatchRow converts a batch row from db to a sweepbatcher.Batch struct.
func convertBatchRow(row sqlc.SweepBatch) *dbBatch {
batch := dbBatch{
ID: row.ID,
}

if row.Confirmed {
batch.State = batchOpen
ID: row.ID,
Confirmed: row.Confirmed,
}

if row.BatchTxID.Valid {
Expand Down Expand Up @@ -288,7 +285,7 @@ func convertBatchRow(row sqlc.SweepBatch) *dbBatch {
// it into the database.
func batchToInsertArgs(batch dbBatch) sqlc.InsertBatchParams {
args := sqlc.InsertBatchParams{
Confirmed: false,
Confirmed: batch.Confirmed,
BatchTxID: sql.NullString{
Valid: true,
String: batch.BatchTxid.String(),
Expand All @@ -305,10 +302,6 @@ func batchToInsertArgs(batch dbBatch) sqlc.InsertBatchParams {
MaxTimeoutDistance: batch.MaxTimeoutDistance,
}

if batch.State == batchConfirmed {
args.Confirmed = true
}

return args
}

Expand All @@ -317,7 +310,7 @@ func batchToInsertArgs(batch dbBatch) sqlc.InsertBatchParams {
func batchToUpdateArgs(batch dbBatch) sqlc.UpdateBatchParams {
args := sqlc.UpdateBatchParams{
ID: batch.ID,
Confirmed: false,
Confirmed: batch.Confirmed,
BatchTxID: sql.NullString{
Valid: true,
String: batch.BatchTxid.String(),
Expand All @@ -333,10 +326,6 @@ func batchToUpdateArgs(batch dbBatch) sqlc.UpdateBatchParams {
},
}

if batch.State == batchConfirmed {
args.Confirmed = true
}

return args
}

Expand Down
8 changes: 4 additions & 4 deletions sweepbatcher/store_mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func (s *StoreMock) FetchUnconfirmedSweepBatches(ctx context.Context) (

result := []*dbBatch{}
for _, batch := range s.batches {
if batch.State != "confirmed" {
if !batch.Confirmed {
result = append(result, &batch)
}
}
Expand Down Expand Up @@ -91,7 +91,7 @@ func (s *StoreMock) ConfirmBatch(ctx context.Context, id int32) error {
return errors.New("batch not found")
}

batch.State = "confirmed"
batch.Confirmed = true
s.batches[batch.ID] = batch

return nil
Expand Down Expand Up @@ -201,7 +201,7 @@ func (s *StoreMock) TotalSweptAmount(ctx context.Context, batchID int32) (
return 0, errors.New("batch not found")
}

if batch.State != batchConfirmed && batch.State != batchClosed {
if !batch.Confirmed {
return 0, nil
}

Expand All @@ -212,5 +212,5 @@ func (s *StoreMock) TotalSweptAmount(ctx context.Context, batchID int32) (
}
}

return 0, nil
return total, nil
}
Loading