Skip to content
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

cmd/debug: add DeleteOrder command #358

Merged
merged 4 commits into from
Apr 19, 2022
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
22 changes: 22 additions & 0 deletions clientdb/order.go
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,28 @@ func (db *DB) GetOrders() ([]order.Order, error) {
return orders, nil
}

// DeleteOrder removes the order with the given nonce. If no order with that
// nonce exists in the store, ErrNoOrder is returned.
//
// NOTE: This is part of the Store interface.
func (db *DB) DeleteOrder(nonce order.Nonce) error {
return db.Update(func(tx *bbolt.Tx) error {
// First, we'll grab our main order bucket key.
rootBucket, err := getBucket(tx, ordersBucketKey)
if err != nil {
return err
}

// Check that the order exists in the main bucket.
orderBucket := rootBucket.Bucket(nonce[:])
if orderBucket == nil {
return ErrNoOrder
}

return rootBucket.DeleteBucket(nonce[:])
})
}

// storeOrderTX saves a byte serialized order in its specific sub bucket within
// the root orders bucket.
func storeOrderTX(rootBucket *bbolt.Bucket, nonce order.Nonce,
Expand Down
51 changes: 51 additions & 0 deletions cmd/pool/debug.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"bytes"
"encoding/hex"
"fmt"
"path"
"path/filepath"
Expand All @@ -27,6 +28,7 @@ var debugCommands = []cli.Command{
dumpOrdersCommand,
dumpPendingBatcheCommand,
removePendingBatchCommand,
deleteOrderCommand,
},
},
}
Expand Down Expand Up @@ -163,6 +165,55 @@ func dumpOrders(ctx *cli.Context) error {
return nil
}

var deleteOrderCommand = cli.Command{
Name: "deleteorder",
Usage: "delete an order from the local database",
Flags: []cli.Flag{
cli.StringFlag{
Name: "order_nonce",
Usage: "the order nonce of the order to delete",
},
cli.StringFlag{
Name: "db",
Usage: "the specific pool database to use instead " +
"of the default one on ~/.pool/<network>/" +
"pool.db",
},
},
Action: deleteOrder,
}

func deleteOrder(ctx *cli.Context) error {
var (
nonceHex string
args = ctx.Args()
)

db, err := getPoolDB(ctx)
if err != nil {
return fmt.Errorf("error loading DB: %v", err)
}

switch {
case ctx.IsSet("order_nonce"):
nonceHex = ctx.String("order_nonce")
case args.Present():
nonceHex = args.First()
default:
return fmt.Errorf("order_nonce argument missing")
}

decodedOnce, err := hex.DecodeString(nonceHex)
if err != nil {
return fmt.Errorf("cannot hex decode order nonce: %v", err)
}

var nonce order.Nonce
copy(nonce[:], decodedOnce)

return db.DeleteOrder(nonce)
}

var dumpPendingBatcheCommand = cli.Command{
Name: "dumppendingbatch",
ShortName: "dpb",
Expand Down
6 changes: 6 additions & 0 deletions order/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -757,6 +757,12 @@ type Store interface {
// GetOrders returns all orders that are currently known to the store.
GetOrders() ([]Order, error)

// DeleteOrder removes the order with the given Nonce.
//
// Note: this method deletes the order without checking if it is
// referenced somewhere else (e.g. pending batch).
DeleteOrder(Nonce) error

// StorePendingBatch atomically stages all modified orders/accounts as a
// result of a pending batch. If any single operation fails, the whole
// set of changes is rolled back. Once the batch has been
Expand Down
14 changes: 14 additions & 0 deletions order/mock_interfaces.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions order/mock_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,8 @@ func (s *mockStore) GetOrders() ([]Order, error) {
return orders, nil
}

// DelOrder removes the order with the given nonce from the local store.
func (s *mockStore) DelOrder(nonce Nonce) error {
// DeleteOrder removes the order with the given nonce from the local store.
Roasbeef marked this conversation as resolved.
Show resolved Hide resolved
func (s *mockStore) DeleteOrder(nonce Nonce) error {
delete(s.orders, nonce)
return nil
}
Expand Down
3 changes: 2 additions & 1 deletion server.go
Original file line number Diff line number Diff line change
Expand Up @@ -770,7 +770,8 @@ func (s *Server) syncLocalOrderState() error {
context.Background(), orderNonce,
)
if err != nil {
return fmt.Errorf("unable to fetch order state: %v", err)
return fmt.Errorf("unable to fetch order(%v): %v",
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is needed so the user knows which ones are the offender orders

orderNonce, err)
}
remoteOrderState, err := rpcOrderStateToDBState(
orderStateResp.State,
Expand Down