Skip to content

Commit

Permalink
feature: add DEX seed backup and restore
Browse files Browse the repository at this point in the history
Signed-off-by: Philemon Ukane <[email protected]>
  • Loading branch information
ukane-philemon committed Jan 19, 2024
1 parent be725eb commit 00af8d8
Show file tree
Hide file tree
Showing 11 changed files with 405 additions and 115 deletions.
14 changes: 11 additions & 3 deletions dexc/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ const (
type DEXClient struct {
ctx context.Context
cancelFn context.CancelFunc
DBPath string
dbPath string
*core.Core

loggedIn bool
Expand Down Expand Up @@ -63,6 +63,10 @@ func (dc *DEXClient) Login(pw []byte) error {
return nil
}

func (dc *DEXClient) DBPath() string {
return dc.dbPath
}

// WaitForShutdown returns a chan that will be closed if core exits.
func (dc *DEXClient) WaitForShutdown() <-chan struct{} {
return dc.shutdownChan
Expand All @@ -87,6 +91,10 @@ type valStamp struct {
func (dc *DEXClient) BondsFeeBuffer(assetID uint32) uint64 {
const expiry = 45 * time.Minute

if !dc.HasWallet(int32(assetID)) { // no configured wallet
return 0
}

buf, ok := dc.bondBufferCache.Load(assetID)
var cachedFeeBuffer valStamp
if ok {
Expand All @@ -107,7 +115,7 @@ func (dc *DEXClient) BondsFeeBuffer(assetID uint32) uint64 {
default:
feeBuffer, err := dc.Core.BondsFeeBuffer(assetID)
if err != nil {
dc.log.Error("Error fetching bond fee buffer: %v", err)
dc.log.Errorf("Error fetching bond fee buffer: %v", err)
} else {
dc.log.Tracef("Obtained fresh bond fee buffer: %d", feeBuffer)
dc.bondBufferCache.Store(assetID, valStamp{feeBuffer, time.Now()})
Expand Down Expand Up @@ -150,7 +158,7 @@ func Start(ctx context.Context, root, lang, logDir, logLvl string, net libutils.

shutdownChan := make(chan struct{})
dc := &DEXClient{
DBPath: dbPath,
dbPath: dbPath,
Core: clientCore,
shutdownChan: shutdownChan,
log: logger,
Expand Down
35 changes: 21 additions & 14 deletions libwallet/assets_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,8 @@ type AssetsManager struct {
RateSource ext.RateSource

dexcMtx sync.RWMutex
dexc *dexc.DEXClient
dexcCtx context.Context
dexc DexClient
startingDEX atomic.Bool
}

Expand Down Expand Up @@ -904,7 +905,7 @@ func (mgr *AssetsManager) CalculateAssetsUSDBalance(balances map[utils.AssetType
}

// DexClient returns a dexc client that MUST never be modified.
func (mgr *AssetsManager) DexClient() *dexc.DEXClient {
func (mgr *AssetsManager) DexClient() DexClient {
mgr.dexcMtx.RLock()
defer mgr.dexcMtx.RUnlock()
return mgr.dexc
Expand All @@ -913,7 +914,8 @@ func (mgr *AssetsManager) DexClient() *dexc.DEXClient {
func (mgr *AssetsManager) DexcReady() bool {
mgr.dexcMtx.RLock()
defer mgr.dexcMtx.RUnlock()
return mgr.dexc != nil
dexClient, _ := mgr.dexc.(*dexc.DEXClient) // check concrete type
return dexClient != nil && dexClient.Core != nil
}

// InitializeDEX initializes mgr.dexc. Support for Cryptopower wallets are
Expand All @@ -932,28 +934,22 @@ func (mgr *AssetsManager) InitializeDEX(ctx context.Context) {
}

// Prevent multiple initialization.
mgr.dexcCtx = ctx
mgr.startingDEX.Store(true)
defer func() {
mgr.startingDEX.Store(false)
}()

logDir := filepath.Dir(mgr.LogFile())
dexcl, err := dexc.Start(ctx, mgr.RootDir(), mgr.GetLanguagePreference(), logDir, mgr.GetLogLevels(), mgr.NetType(), 0 /* TODO: Make configurable */)
dexClient, err := dexc.Start(ctx, mgr.RootDir(), mgr.GetLanguagePreference(), logDir, mgr.GetLogLevels(), mgr.NetType(), 0 /* TODO: Make configurable */)
if err != nil {
log.Errorf("Error starting dex client: %v", err)
return
}

mgr.dexcMtx.Lock()
mgr.dexc = dexcl
mgr.dexc = dexClient
mgr.dexcMtx.Unlock()

go func() {
<-mgr.dexc.WaitForShutdown()
mgr.dexcMtx.Lock()
mgr.dexc = nil
mgr.dexcMtx.Unlock()
}()
}

func (mgr *AssetsManager) DeleteDEXData() error {
Expand All @@ -965,15 +961,26 @@ func (mgr *AssetsManager) DeleteDEXData() error {

log.Debug("Shutting down DEX client and removing dex data dir....")

dexDBFile := mgr.dexc.DBPath
dexDBFile := mgr.dexc.DBPath()
shutdownChan := mgr.dexc.WaitForShutdown()

// Shutdown the DEX client.
mgr.dexc.Shutdown()
<-shutdownChan // wait for shutdown

mgr.dexcMtx.Lock()
mgr.dexc = nil
mgr.dexcMtx.Unlock()

// Delete dex client db.
return os.Remove(dexDBFile)
err = os.Remove(dexDBFile)
if err != nil {
return err
}

// Start a new client.
mgr.InitializeDEX(mgr.dexcCtx)
return nil
}

// prepareDexSupportForDCRWallet sets up the DEX client to allow using a
Expand Down
36 changes: 36 additions & 0 deletions libwallet/dex_interface.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package libwallet

import (
"decred.org/dcrdex/client/core"
"decred.org/dcrdex/client/orderbook"
"decred.org/dcrdex/dex"
)

type DexClient interface {
Ready() <-chan struct{}
WaitForShutdown() <-chan struct{}
Shutdown()
IsDEXPasswordSet() bool
IsLoggedIn() bool
InitWithPassword(pw, seed []byte) error
Login(pw []byte) error
Logout() error
DBPath() string
GetDEXConfig(dexAddr string, certI any) (*core.Exchange, error)
BondsFeeBuffer(assetID uint32) uint64
HasWallet(assetID int32) bool
AddWallet(assetID uint32, settings map[string]string, appPW, walletPW []byte) error
PostBond(form *core.PostBondForm) (*core.PostBondResult, error)
NotificationFeed() *core.NoteFeed
Exchanges() map[string]*core.Exchange
Exchange(host string) (*core.Exchange, error)
ExportSeed(pw []byte) ([]byte, error)
SyncBook(dex string, base, quote uint32) (*orderbook.OrderBook, core.BookFeed, error)
Orders(filter *core.OrderFilter) ([]*core.Order, error)
TradeAsync(pw []byte, form *core.TradeForm) (*core.InFlightOrder, error)
WalletState(assetID uint32) *core.WalletState
WalletSettings(assetID uint32) (map[string]string, error)
MaxBuy(host string, base, quote uint32, rate uint64) (*core.MaxOrderEstimate, error)
MaxSell(host string, base, quote uint32) (*core.MaxOrderEstimate, error)
Cancel(oid dex.Bytes) error
}
6 changes: 4 additions & 2 deletions ui/page/dcrdex/dcrdex_page.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,11 @@ func (pg *DEXPage) OnNavigatedTo() {

pg.dexIsLoading = true
go func() {
<-pg.AssetsManager.DexClient().Ready()
dexc := pg.AssetsManager.DexClient()
<-dexc.Ready()

showOnBoardingPage := true
if len(pg.AssetsManager.DexClient().Exchanges()) != 0 { // has at least one exchange
if len(dexc.Exchanges()) != 0 { // has at least one exchange
_, _, pendingBond := pendingBondConfirmation(pg.AssetsManager)
showOnBoardingPage = pendingBond != nil
}
Expand Down
Loading

0 comments on commit 00af8d8

Please sign in to comment.