Skip to content

Commit

Permalink
order: Mark missing invoices as failed in the client db
Browse files Browse the repository at this point in the history
  • Loading branch information
positiveblue committed Apr 21, 2022
1 parent 9bdcb8e commit ba88b16
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 10 deletions.
8 changes: 8 additions & 0 deletions clientdb/order.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"errors"
"fmt"
"io"
"strings"

"github.com/btcsuite/btcd/btcutil"
"github.com/lightninglabs/pool/event"
Expand Down Expand Up @@ -65,6 +66,13 @@ var (
orderTlvKey = []byte("order-tlv")
)

// IsNotFoundErr tries to match the given error with ErrNoOrder.
func IsOrderNotFoundErr(err error) bool {
// The rpc error will look like
// rpc error: code = Unknown desc = no order found
return strings.Contains(err.Error(), ErrNoOrder.Error())
}

type extraOrderData struct {
minNodeTier order.NodeTier
minUnitsMatch order.SupplyUnit
Expand Down
36 changes: 26 additions & 10 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -754,6 +754,16 @@ func (s *Server) syncLocalOrderState() error {
noncesToUpdate []order.Nonce
ordersToUpdate [][]order.Modifier
)

addUpdate := func(nonce order.Nonce, state order.State) {
noncesToUpdate = append(noncesToUpdate, nonce)
ordersToUpdate = append(
ordersToUpdate,
[]order.Modifier{
order.StateModifier(state),
},
)
}
for _, dbOrder := range dbOrders {
orderNonce := dbOrder.Nonce()
localOrderState := dbOrder.Details().State
Expand All @@ -770,9 +780,22 @@ func (s *Server) syncLocalOrderState() error {
context.Background(), orderNonce,
)
if err != nil {
return fmt.Errorf("unable to fetch order(%v): %v",
orderNonce, err)
switch {
// If the server does not know about this order mark it
// as failed and allow the client start up with the
// rest of valid orders.
//
// NOTE: This should never happen.
case clientdb.IsOrderNotFoundErr(err):
addUpdate(orderNonce, order.StateFailed)

default:
return fmt.Errorf("unable to fetch order(%v): "+
"%v", orderNonce, err)
}
continue
}

remoteOrderState, err := rpcOrderStateToDBState(
orderStateResp.State,
)
Expand All @@ -791,14 +814,7 @@ func (s *Server) syncLocalOrderState() error {
// complete the handshake and poll to see if there're any
// pending batches we need to process.
if remoteOrderState == order.StateCanceled {
noncesToUpdate = append(noncesToUpdate, orderNonce)
ordersToUpdate = append(
ordersToUpdate,
[]order.Modifier{
order.StateModifier(order.StateCanceled),
},
)

addUpdate(orderNonce, order.StateCanceled)
log.Debugf("Order(%v) is cancelled, updating local "+
"state", orderNonce)
} else {
Expand Down

0 comments on commit ba88b16

Please sign in to comment.