Skip to content

Commit

Permalink
review changes
Browse files Browse the repository at this point in the history
Signed-off-by: Philemon Ukane <[email protected]>
  • Loading branch information
ukane-philemon committed Nov 14, 2023
1 parent 2c18d00 commit 167dbd4
Show file tree
Hide file tree
Showing 10 changed files with 169 additions and 170 deletions.
58 changes: 29 additions & 29 deletions dexcore/core.go → dexc/core.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package dexcore
package dexc

import (
"context"
Expand All @@ -12,8 +12,8 @@ import (
libutils "github.com/crypto-power/cryptopower/libwallet/utils"
)

// Implements the DEXCore interface.
type dexCore struct {
// DEXClient represents the Decred DEX client and embeds *core.Core.
type DEXClient struct {
*core.Core

shutdownChan <-chan struct{}
Expand All @@ -22,8 +22,16 @@ type dexCore struct {
log dex.Logger
}

// Shutdown returns a chan that will be closed if core exits.
func (dc *dexCore) Shutdown() <-chan struct{} {
func (dc *DEXClient) SetDEXPassword(pw []byte, seed []byte) error {
return dc.InitializeClient(pw, seed)
}

func (dc *DEXClient) IsDEXPasswordSet() bool {
return dc.IsInitialized()
}

// WaitForShutdown returns a chan that will be closed if core exits.
func (dc *DEXClient) WaitForShutdown() <-chan struct{} {
return dc.shutdownChan
}

Expand All @@ -32,13 +40,14 @@ type valStamp struct {
stamp time.Time
}

// BondsFeeBuffer is a caching helper for the bonds fee buffer because
// (*Core).BondsFeeBuffer returns a fresh fee buffer based on a current (but
// padded) fee rate estimate. Values for a given asset are cached for 45
// minutes. These values are meant to provide a sensible but well-padded fee
// buffer for bond transactions now and well into the future, so a long expiry
// is appropriate.
func (dc *dexCore) BondsFeeBuffer(assetID uint32) (feeBuffer uint64) {
// BondsFeeBuffer is a caching helper for the bonds fee buffer to assist the
// frontend by stabilizing this value for up to 45 minutes from the last request
// for a given asset and because (*Core).BondsFeeBuffer returns a fresh fee
// buffer based on a current (but padded) fee rate estimate. Values for a given
// asset are cached for 45 minutes. These values are meant to provide a sensible
// but well-padded fee buffer for bond transactions now and well into the
// future, so a long expiry is appropriate.
func (dc *DEXClient) BondsFeeBuffer(assetID uint32) (feeBuffer uint64) {
const expiry = 45 * time.Minute
dc.bondBufMtx.Lock()
defer dc.bondBufMtx.Unlock()
Expand All @@ -60,14 +69,7 @@ func (dc *dexCore) BondsFeeBuffer(assetID uint32) (feeBuffer uint64) {
return feeBuffer
}

// Client represents the Decred DEX client. Consumers should always check if the
// DEXCore.Ready() channel is closed before requesting data from or sending data
// to the DEX client. This allows for custom handling when DEX is not ready.
type Client struct {
DEXCore
}

func StartDEXClient(ctx context.Context, root, lang, logDir, logLvl string, net libutils.NetworkType, maxLogZips int) (*Client, error) {
func Start(ctx context.Context, root, lang, logDir, logLvl string, net libutils.NetworkType, maxLogZips int) (*DEXClient, error) {
dexNet, err := parseDEXNet(net)
if err != nil {
return nil, fmt.Errorf("error parsing network: %w", err)
Expand All @@ -93,20 +95,18 @@ func StartDEXClient(ctx context.Context, root, lang, logDir, logLvl string, net
}

shutdownChan := make(chan struct{})
dc := &Client{
DEXCore: &dexCore{
Core: clientCore,
bondBufferCache: make(map[uint32]valStamp),
shutdownChan: shutdownChan,
log: logger,
},
dc := &DEXClient{
Core: clientCore,
bondBufferCache: make(map[uint32]valStamp),
shutdownChan: shutdownChan,
log: logger,
}

// Use a goroutine to start dex core as it'll blocks until dex core exits.
// Use a goroutine to start dex core as it'll block until dex core exits.
go func() {
dc.Run(ctx)
close(shutdownChan)
dc.DEXCore = nil
dc.Core = nil
logCloser()
}()

Expand Down
2 changes: 1 addition & 1 deletion dexcore/log.go → dexc/log.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package dexcore
package dexc

import (
"fmt"
Expand Down
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ func main() {
logger.SetLogLevels(assetsManager.GetLogLevels())
}

win, err := ui.CreateWindow(assetsManager, Version, buildDate, cfg.MaxLogZips)
win, err := ui.CreateWindow(assetsManager, Version, buildDate)
if err != nil {
log.Errorf("Could not initialize window: %s\ns", err)
return
Expand Down
8 changes: 1 addition & 7 deletions ui/load/appinfo.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,14 @@ type AppInfo struct {
version string
buildDate time.Time
startUpTime time.Time
maxLogZips int
}

// StartApp returns an instance of AppInfo with the startUpTime set to the current time.
func StartApp(version string, buildDate time.Time, maxLogZips int) *AppInfo {
func StartApp(version string, buildDate time.Time) *AppInfo {
return &AppInfo{
version: version,
buildDate: buildDate,
startUpTime: time.Now(),
maxLogZips: maxLogZips,
}
}

Expand All @@ -33,7 +31,3 @@ func (app *AppInfo) Version() string {
func (app *AppInfo) StartupTime() time.Time {
return app.startUpTime
}

func (app *AppInfo) MaxLogZips() int {
return app.maxLogZips
}
13 changes: 7 additions & 6 deletions ui/page/dcrdex/dcrdex_page.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package dcrdex
import (
"gioui.org/layout"
"github.com/crypto-power/cryptopower/app"
"github.com/crypto-power/cryptopower/dexcore"
"github.com/crypto-power/cryptopower/dexc"
"github.com/crypto-power/cryptopower/libwallet/utils"
libutils "github.com/crypto-power/cryptopower/libwallet/utils"
"github.com/crypto-power/cryptopower/ui/cryptomaterial"
Expand All @@ -25,12 +25,13 @@ type DEXPage struct {

*load.Load

dexc *dexcore.Client // Might be nil
// Might be nil but interaction with this page will be disabled if it is.
dexc *dexc.DEXClient

generalSettingsBtn cryptomaterial.Button
}

func NewDEXPage(l *load.Load, dexc *dexcore.Client) *DEXPage {
func NewDEXPage(l *load.Load, dexc *dexc.DEXClient) *DEXPage {
pg := &DEXPage{
MasterPage: app.NewMasterPage(DCRDEXPageID),
Load: l,
Expand Down Expand Up @@ -90,9 +91,9 @@ func (pg *DEXPage) Layout(gtx C) D {
return pg.CurrentPage().Layout(gtx)
}

// isMultipleAssetTypeWalletAvailable checks if multiple asset types are
// available for dex functionality to run smoothly. Otherwise dex functionality
// is disable till different asset type wallets are created.
// isMultipleAssetTypeWalletAvailable checks if wallets exist for more than 1
// asset type. If not, dex functionality is disable till different asset type
// wallets are created.
func (pg *DEXPage) isMultipleAssetTypeWalletAvailable() bool {
allWallets := pg.WL.AssetsManager.AllWallets()
assetTypes := make(map[libutils.AssetType]bool)
Expand Down
15 changes: 6 additions & 9 deletions dexcore/dex_interface.go → ui/page/dcrdex/dex_interface.go
Original file line number Diff line number Diff line change
@@ -1,20 +1,17 @@
package dexcore
package dcrdex

import (
"context"

"decred.org/dcrdex/client/core"
)

type DEXCore interface {
Run(ctx context.Context)
type clientCore interface {
Ready() <-chan struct{}
Shutdown() <-chan struct{}
IsInitialized() bool
InitializeClient(pw, seed []byte) error
WaitForShutdown() <-chan struct{}
IsDEXPasswordSet() bool
SetDEXPassword(pw, seed []byte) error
Login(pw []byte) error
DiscoverAccount(dexAddr string, pass []byte, certI any) (*core.Exchange, bool, error)
BondsFeeBuffer(assetID uint32) uint64
PostBond(form *core.PostBondForm) (*core.PostBondResult, error)
NotificationFeed() <-chan core.Notification
Login(pw []byte) error
}
Loading

0 comments on commit 167dbd4

Please sign in to comment.