Skip to content

Commit

Permalink
client/core: self-governed trades when server is down
Browse files Browse the repository at this point in the history
When the client has active swaps on a server that is either down or
mysteriously looses the market on which the trades was started, it is
still necessary to complete these orders if a swap has been broadcast.
In particular, the refund and auto-redeem/find-redemption paths need
to work as expected.

This change allows such trades to be loaded on startup even when
the asset configurations are unavailable from the server for any
reason. The lack of a market configuration or a DEX connection are also
conditions for special handling. Trades in this state are flagged as
"self-governed".

In addition to flagging self-governed trades on startup and login
(resumeTrades), the DEX connect and reconnect handlers now toggle the
flag. The reconnect handler only removes the self-governed flag if the
fresh market config includes the trade's market.

To support safely redeeming a counter-party swap and other actions
that depend on the asset configs (e.g. SwapConf, MaxFeeRate, etc).,
this supplements the db.OrderMetaData struct with both to/from SwapConf
values as they were when the order was placed. This also adds EpochDur
to the struct to help with existing logic for handling stale cancels
and other epoch orders, as well as epoch index computations when
considered with the order's ServerTime stamp.

While it is generally ill-advised to perform a client upgrade with
active orders, this also tries during order loading to get these
values if they were not stored (load as zero).
  • Loading branch information
chappjc committed Nov 9, 2022
1 parent 2e00ff3 commit ee9c630
Show file tree
Hide file tree
Showing 11 changed files with 514 additions and 258 deletions.
3 changes: 1 addition & 2 deletions client/asset/btc/btc.go
Original file line number Diff line number Diff line change
Expand Up @@ -1438,9 +1438,8 @@ func (btc *baseWallet) legacyBalance() (*asset.Balance, error) {
func (btc *baseWallet) feeRate(_ RawRequester, confTarget uint64) (uint64, error) {
feeResult, err := btc.node.estimateSmartFee(int64(confTarget), &btcjson.EstimateModeConservative)
if err != nil {
btc.log.Errorf("Failed to get fee rate with estimate smart fee rate: %v", err)

if !btc.apiFeeFallback() {
btc.log.Warnf("Failed to get local fee rate estimate: %v", err)
return 0, err
}
btc.log.Debug("Retrieving fee rate from external API: ", externalApiUrl)
Expand Down
14 changes: 14 additions & 0 deletions client/comms/wsconn.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,20 @@ const (
InvalidCert
)

// String gives a human readable string for each connection status.
func (cs ConnectionStatus) String() string {
switch cs {
case Disconnected:
return "disconnected"
case Connected:
return "connected"
case InvalidCert:
return "invalid certificate"
default:
return "unknown status"
}
}

// ErrInvalidCert is the error returned when attempting to use an invalid cert
// to set up a ws connection.
var ErrInvalidCert = fmt.Errorf("invalid certificate")
Expand Down

0 comments on commit ee9c630

Please sign in to comment.