diff --git a/auctioneer/batch.go b/auctioneer/batch.go index b85adfd..12c4fef 100644 --- a/auctioneer/batch.go +++ b/auctioneer/batch.go @@ -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 "+ diff --git a/clientdb/batch.go b/clientdb/batch.go index 5e36232..4448906 100644 --- a/clientdb/batch.go +++ b/clientdb/batch.go @@ -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 diff --git a/clientdb/batch_test.go b/clientdb/batch_test.go index f5fc2f1..4759512 100644 --- a/clientdb/batch_test.go +++ b/clientdb/batch_test.go @@ -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 diff --git a/cmd/pool/debug.go b/cmd/pool/debug.go index b5104eb..aad81ac 100644 --- a/cmd/pool/debug.go +++ b/cmd/pool/debug.go @@ -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) } diff --git a/rpcserver.go b/rpcserver.go index fa92863..9f802e1 100644 --- a/rpcserver.go +++ b/rpcserver.go @@ -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 }