Skip to content

Commit

Permalink
client/asset/{btc,dcr}: Lock connected check and timeout for shutdown
Browse files Browse the repository at this point in the history
client/asset/btc: remove unused client field
  • Loading branch information
chappjc committed Jan 5, 2021
1 parent dc67cdb commit 7321c36
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 11 deletions.
10 changes: 5 additions & 5 deletions client/asset/btc/btc.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ type rpcClient interface {
GetRawMempool() ([]*chainhash.Hash, error)
GetRawTransactionVerbose(txHash *chainhash.Hash) (*btcjson.TxRawResult, error)
RawRequest(method string, params []json.RawMessage) (json.RawMessage, error)
Disconnected() bool
}

// BTCCloneCFG holds clone specific parameters.
Expand Down Expand Up @@ -328,7 +329,6 @@ type ExchangeWallet struct {
// 64-bit atomic variables first. See
// https://golang.org/pkg/sync/atomic/#pkg-note-BUG
tipAtConnect int64
client *rpcclient.Client
node rpcClient
wallet *walletClient
walletInfo *asset.WalletInfo
Expand Down Expand Up @@ -434,10 +434,7 @@ func BTCCloneWallet(cfg *BTCCloneCFG) (*ExchangeWallet, error) {
return nil, fmt.Errorf("error creating BTC RPC client: %v", err)
}

btc := newWallet(cfg, btcCfg, client)
btc.client = client

return btc, nil
return newWallet(cfg, btcCfg, client), nil
}

// newWallet creates the ExchangeWallet and starts the block monitor.
Expand Down Expand Up @@ -980,6 +977,9 @@ func (btc *ExchangeWallet) Unlock(pw string) error {

// Lock locks the ExchangeWallet and the underlying bitcoind wallet.
func (btc *ExchangeWallet) Lock() error {
if btc.node.Disconnected() {
return asset.ErrConnectionDown
}
return btc.wallet.Lock()
}

Expand Down
2 changes: 2 additions & 0 deletions client/asset/btc/btc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,8 @@ func (c *tRPCClient) GetRawTransactionVerbose(txHash *chainhash.Hash) (*btcjson.
return c.mpVerboseTxs[txHash.String()], c.rawVerboseErr
}

func (c *tRPCClient) Disconnected() bool { return false }

func (c *tRPCClient) RawRequest(method string, params []json.RawMessage) (json.RawMessage, error) {
switch method {
case methodSignTx:
Expand Down
19 changes: 14 additions & 5 deletions client/asset/dcr/dcr.go
Original file line number Diff line number Diff line change
Expand Up @@ -395,9 +395,9 @@ func NewWallet(cfg *asset.WalletConfig, logger dex.Logger, network dex.Network)
logger.Infof("Setting up new DCR wallet at %s with TLS certificate %q.",
walletCfg.RPCListen, walletCfg.RPCCert)
dcr.client, err = newClient(walletCfg.RPCListen, walletCfg.RPCUser,
walletCfg.RPCPass, walletCfg.RPCCert)
walletCfg.RPCPass, walletCfg.RPCCert, logger)
if err != nil {
return nil, fmt.Errorf("DCR ExchangeWallet.Run error: %v", err)
return nil, fmt.Errorf("DCR ExchangeWallet.Run error: %w", err)
}
// Beyond this point, only node
dcr.node = dcr.client
Expand Down Expand Up @@ -436,7 +436,7 @@ func unconnectedWallet(cfg *asset.WalletConfig, dcrCfg *Config, logger dex.Logge

// newClient attempts to create a new websocket connection to a dcrwallet
// instance with the given credentials and notification handlers.
func newClient(host, user, pass, cert string) (*rpcclient.Client, error) {
func newClient(host, user, pass, cert string, logger dex.Logger) (*rpcclient.Client, error) {

certs, err := ioutil.ReadFile(cert)
if err != nil {
Expand All @@ -452,9 +452,15 @@ func newClient(host, user, pass, cert string) (*rpcclient.Client, error) {
DisableConnectOnNew: true,
}

cl, err := rpcclient.New(config, nil)
ntfnHandlers := &rpcclient.NotificationHandlers{
// Setup an on-connect handler for logging (re)connects.
OnClientConnected: func() {
logger.Infof("Connected to Decred wallet at %s", host)
},
}
cl, err := rpcclient.New(config, ntfnHandlers)
if err != nil {
return nil, fmt.Errorf("Failed to start dcrwallet RPC client: %v", err)
return nil, fmt.Errorf("Failed to start dcrwallet RPC client: %w", err)
}

return cl, nil
Expand Down Expand Up @@ -1724,6 +1730,9 @@ func (dcr *ExchangeWallet) Unlock(pw string) error {

// Lock locks the exchange wallet.
func (dcr *ExchangeWallet) Lock() error {
if dcr.client.Disconnected() {
return asset.ErrConnectionDown
}
return dcr.node.WalletLock()
}

Expand Down
5 changes: 4 additions & 1 deletion client/asset/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@ import (
// has been spent or it never existed. This error may be returned from
// AuditContract, Refund or Redeem as those methods expect the provided coin to
// exist and be unspent.
const CoinNotFoundError = dex.ErrorKind("coin not found")
const (
CoinNotFoundError = dex.ErrorKind("coin not found")
ErrConnectionDown = dex.ErrorKind("wallet not connected")
)

// WalletInfo is auxiliary information about an ExchangeWallet.
type WalletInfo struct {
Expand Down

0 comments on commit 7321c36

Please sign in to comment.