multi: replace PendingBatch with PendingBatchSnapshot

To be able to fully clean up an old, pending batch snapshot on daemon
startup, we first need to switch to a method that returns the full
snapshot instead of just its ID.
This commit is contained in:
Oliver Gugger 2020-12-08 11:44:07 +01:00
parent 4b2291aaa8
commit 8b57bb9a4e
No known key found for this signature in database
GPG key ID: 8E4256593F177720
5 changed files with 28 additions and 34 deletions

View file

@ -9,7 +9,7 @@ import (
"github.com/btcsuite/btcd/wire"
"github.com/lightninglabs/pool/account"
"github.com/lightninglabs/pool/order"
"github.com/lightninglabs/pool/clientdb"
"github.com/lightninglabs/pool/poolrpc"
)
@ -22,9 +22,9 @@ var (
// BatchSource abstracts the source of a trader's pending batch.
type BatchSource interface {
// PendingBatch retrieves the ID and transaction of the current pending
// batch. If one does not exist, account.ErrNoPendingBatch is returned.
PendingBatch() (order.BatchID, *wire.MsgTx, error)
// PendingBatchSnapshot retrieves the snapshot of the currently pending
// batch. If there isn't one, account.ErrNoPendingBatch is returned.
PendingBatchSnapshot() (*clientdb.LocalBatchSnapshot, error)
// DeletePendingBatch removes all references to the current pending
// batch without applying its staged updates to accounts and orders. If
@ -36,7 +36,7 @@ type BatchSource interface {
// auctioneer considers finalized. If they don't match, then the pending batch
// is deleted without applying its staged updates.
func (c *Client) checkPendingBatch() error {
id, tx, err := c.cfg.BatchSource.PendingBatch()
snapshot, err := c.cfg.BatchSource.PendingBatchSnapshot()
if err == account.ErrNoPendingBatch {
// If there's no pending batch, there's nothing to do.
return nil
@ -45,7 +45,7 @@ func (c *Client) checkPendingBatch() error {
return fmt.Errorf("loading pending batch failed: %v", err)
}
finalizedTx, err := c.finalizedBatchTx(id)
finalizedTx, err := c.finalizedBatchTx(snapshot)
// If the batch has not been finalized yet, there's nothing to do but
// wait to receive its Finalize message.
//
@ -57,7 +57,7 @@ func (c *Client) checkPendingBatch() error {
return fmt.Errorf("querying finalized batch TX failed: %v", err)
}
if tx.TxHash() != finalizedTx.TxHash() {
if snapshot.BatchTX.TxHash() != finalizedTx.TxHash() {
return c.cfg.BatchSource.DeletePendingBatch()
}
@ -66,8 +66,10 @@ func (c *Client) checkPendingBatch() error {
// finalizedBatchTx retrieves the finalized transaction of a batch according to
// the auctioneer, i.e., the transaction that will be broadcast to the network.
func (c *Client) finalizedBatchTx(id order.BatchID) (*wire.MsgTx, error) {
req := &poolrpc.BatchSnapshotRequest{BatchId: id[:]}
func (c *Client) finalizedBatchTx(
snapshot *clientdb.LocalBatchSnapshot) (*wire.MsgTx, error) {
req := &poolrpc.BatchSnapshotRequest{BatchId: snapshot.BatchID[:]}
batch, err := c.client.BatchSnapshot(context.Background(), req)
if err != nil {
return nil, fmt.Errorf("querying relevant batch snapshot "+

View file

@ -152,24 +152,16 @@ func (db *DB) StorePendingBatch(batch *order.Batch, orders []order.Nonce,
})
}
// PendingBatchID retrieves the ID of the currently pending batch. If there
// isn't one, account.ErrNoPendingBatch is returned.
func (db *DB) PendingBatch() (order.BatchID, *wire.MsgTx, error) {
var (
batchID order.BatchID
batchTx *wire.MsgTx
)
// PendingBatchSnapshot retrieves the snapshot of the currently pending batch.
// If there isn't one, account.ErrNoPendingBatch is returned.
func (db *DB) PendingBatchSnapshot() (*LocalBatchSnapshot, error) {
var batchSnapshot *LocalBatchSnapshot
err := db.View(func(tx *bbolt.Tx) error {
var err error
batchID, err = pendingBatchID(tx)
if err != nil {
return err
}
batchTx, err = pendingBatchTx(tx)
batchSnapshot, err = fetchPendingBatchSnapshot(tx)
return err
})
return batchID, batchTx, err
return batchSnapshot, err
}
// pendingBatchID retrieves the stored pending batch ID within a database

View file

@ -113,7 +113,7 @@ var (
runTest: func(db *DB, a *order.Ask, b *order.Bid,
acct *account.Account) error {
_, _, err := db.PendingBatch()
_, err := db.PendingBatchSnapshot()
return err
},
},
@ -155,20 +155,20 @@ var (
// The pending batch ID and transaction should
// reflect correctly.
dbBatchID, dbBatchTx, err := db.PendingBatch()
dbSnapshot, err := db.PendingBatchSnapshot()
if err != nil {
return err
}
if dbBatchID != testBatchID {
if dbSnapshot.BatchID != testBatchID {
return fmt.Errorf("expected pending "+
"batch id %x, got %x",
testBatchID, dbBatchID)
testBatchID, dbSnapshot.BatchID)
}
if dbBatchTx.TxHash() != testBatch.BatchTX.TxHash() {
if dbSnapshot.BatchTX.TxHash() != testBatch.BatchTX.TxHash() {
return fmt.Errorf("expected pending "+
"batch tx %v, got %v",
testBatch.BatchTX.TxHash(),
dbBatchTx.TxHash())
dbSnapshot.BatchTX.TxHash())
}
// Verify the updates have not been applied to

View file

@ -185,16 +185,16 @@ func dumpPendingBatch(ctx *cli.Context) error {
return fmt.Errorf("error loading DB: %v", err)
}
batchID, tx, err := db.PendingBatch()
snapshot, err := db.PendingBatchSnapshot()
if err != nil {
return fmt.Errorf("error getting pending batch: %v", err)
}
fmt.Printf("Batch ID:\t%x\n", batchID[:])
fmt.Printf("TXID:\t\t%s\n", tx.TxHash())
fmt.Printf("Batch ID:\t%x\n", snapshot.BatchID[:])
fmt.Printf("TXID:\t\t%s\n", snapshot.BatchTX.TxHash())
var buf bytes.Buffer
err = tx.Serialize(&buf)
err = snapshot.BatchTX.Serialize(&buf)
if err != nil {
return fmt.Errorf("error serializing TX: %v", err)
}

View file

@ -83,7 +83,7 @@ type accountStore struct {
var _ account.Store = (*accountStore)(nil)
func (s *accountStore) PendingBatch() error {
_, _, err := s.DB.PendingBatch()
_, err := s.DB.PendingBatchSnapshot()
return err
}