Skip to content

Commit

Permalink
Merge branch 'master' into toggleReputationUI
Browse files Browse the repository at this point in the history
  • Loading branch information
buck54321 authored Oct 15, 2024
2 parents 15504f3 + 139cebe commit d5ec073
Show file tree
Hide file tree
Showing 33 changed files with 598 additions and 391 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ permissions:
jobs:
build-go:
name: Go CI
runs-on: ubuntu-latest
runs-on: ubuntu-22.04
strategy:
matrix:
go: ['1.22', '1.23']
Expand Down
5 changes: 2 additions & 3 deletions client/asset/btc/rpcclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -907,9 +907,8 @@ func (wc *rpcClient) estimateSendTxFee(tx *wire.MsgTx, feeRate uint64, subtract
args = append(args, options)

var res struct {
TxBytes dex.Bytes `json:"hex"`
Fees float64 `json:"fee"`
ChangePosition uint32 `json:"changepos"`
TxBytes dex.Bytes `json:"hex"`
Fees float64 `json:"fee"`
}
err = wc.call(methodFundRawTransaction, args, &res)
if err != nil {
Expand Down
19 changes: 6 additions & 13 deletions client/asset/dcr/dcr.go
Original file line number Diff line number Diff line change
Expand Up @@ -5936,7 +5936,7 @@ func isMixTx(tx *wire.MsgTx) (isMix bool, mixDenom int64) {
}

// idUnknownTx identifies the type and details of a transaction either made
// or recieved by the wallet.
// or received by the wallet.
func (dcr *ExchangeWallet) idUnknownTx(ctx context.Context, tx *ListTransactionsResult) (*asset.WalletTransaction, error) {
txHash, err := chainhash.NewHashFromStr(tx.TxID)
if err != nil {
Expand Down Expand Up @@ -6252,7 +6252,7 @@ func (dcr *ExchangeWallet) idUnknownTx(ctx context.Context, tx *ListTransactions
}

// addUnknownTransactionsToHistory checks for any transactions the wallet has
// made or recieved that are not part of the transaction history. It scans
// made or received that are not part of the transaction history. It scans
// from the last point to which it had previously scanned to the current tip.
func (dcr *ExchangeWallet) addUnknownTransactionsToHistory(tip uint64) {
txHistoryDB := dcr.txDB()
Expand Down Expand Up @@ -6320,7 +6320,7 @@ func (dcr *ExchangeWallet) addUnknownTransactionsToHistory(tip uint64) {
}

// syncTxHistory checks to see if there are any transactions which the wallet
// has made or recieved that are not part of the transaction history, then
// has made or received that are not part of the transaction history, then
// identifies and adds them. It also checks all the pending transactions to see
// if they have been mined into a block, and if so, updates the transaction
// history to reflect the block height.
Expand Down Expand Up @@ -6699,11 +6699,10 @@ func (dcr *ExchangeWallet) handleTipChange(ctx context.Context, newTipHash *chai
dcr.cycleMixer()
}

var wg sync.WaitGroup
wg.Add(1)
dcr.wg.Add(1)
go func() {
defer wg.Done()
dcr.syncTxHistory(ctx, uint64(newTipHeight))
dcr.wg.Done()
}()

// Search for contract redemption in new blocks if there
Expand Down Expand Up @@ -6759,13 +6758,7 @@ func (dcr *ExchangeWallet) handleTipChange(ctx context.Context, newTipHash *chai

// Run the redemption search from the startHeight determined above up
// till the current tip height.
wg.Add(1)
go func() {
defer wg.Done()
dcr.findRedemptionsInBlockRange(startHeight, newTipHeight, contractOutpoints)
}()

wg.Wait()
dcr.findRedemptionsInBlockRange(startHeight, newTipHeight, contractOutpoints)
}

func (dcr *ExchangeWallet) getBestBlock(ctx context.Context) (*block, error) {
Expand Down
4 changes: 2 additions & 2 deletions client/asset/firo/firo.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ const (
version = 0
// Zcoin XZC
BipID = 136
// Lelantus Spark. Net proto 90031. Wallet version 130000
minNetworkVersion = 141303
// Consensus changes v0.14.14.0
minNetworkVersion = 141400
walletTypeRPC = "firodRPC"
walletTypeElectrum = "electrumRPC"
estimateFeeConfs = 2 // 2 blocks should be enough
Expand Down
25 changes: 19 additions & 6 deletions client/comms/wsconn.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ type WsConn interface {
RequestWithTimeout(msg *msgjson.Message, respHandler func(*msgjson.Message), expireTime time.Duration, expire func()) error
Connect(ctx context.Context) (*sync.WaitGroup, error)
MessageSource() <-chan *msgjson.Message
UpdateURL(string)
}

// When the DEX sends a request to the client, a responseHandler is created
Expand Down Expand Up @@ -161,6 +162,7 @@ type wsConn struct {
cfg *WsCfg
tlsCfg *tls.Config
readCh chan *msgjson.Message
urlV atomic.Value // string

wsMtx sync.Mutex
ws *websocket.Conn
Expand Down Expand Up @@ -203,14 +205,25 @@ func NewWsConn(cfg *WsCfg) (WsConn, error) {
ServerName: uri.Hostname(),
}

return &wsConn{
conn := &wsConn{
cfg: cfg,
log: cfg.Logger,
tlsCfg: tlsConfig,
readCh: make(chan *msgjson.Message, readBuffSize),
respHandlers: make(map[uint64]*responseHandler),
reconnectCh: make(chan struct{}, 1),
}, nil
}
conn.urlV.Store(cfg.URL)

return conn, nil
}

func (conn *wsConn) UpdateURL(uri string) {
conn.urlV.Store(uri)
}

func (conn *wsConn) url() string {
return conn.urlV.Load().(string)
}

// IsDown indicates if the connection is known to be down.
Expand Down Expand Up @@ -240,7 +253,7 @@ func (conn *wsConn) connect(ctx context.Context) error {
dialer.Proxy = http.ProxyFromEnvironment
}

ws, _, err := dialer.DialContext(ctx, conn.cfg.URL, conn.cfg.ConnectHeaders)
ws, _, err := dialer.DialContext(ctx, conn.url(), conn.cfg.ConnectHeaders)
if err != nil {
if isErrorInvalidCert(err) {
conn.setConnectionStatus(InvalidCert)
Expand Down Expand Up @@ -331,7 +344,7 @@ func (conn *wsConn) handleReadError(err error) {

var netErr net.Error
if errors.As(err, &netErr) && netErr.Timeout() {
conn.log.Errorf("Read timeout on connection to %s.", conn.cfg.URL)
conn.log.Errorf("Read timeout on connection to %s.", conn.url())
reconnect()
return
}
Expand Down Expand Up @@ -457,11 +470,11 @@ func (conn *wsConn) keepAlive(ctx context.Context) {
return
}

conn.log.Infof("Attempting to reconnect to %s...", conn.cfg.URL)
conn.log.Infof("Attempting to reconnect to %s...", conn.url())
err := conn.connect(ctx)
if err != nil {
conn.log.Errorf("Reconnect failed. Scheduling reconnect to %s in %.1f seconds.",
conn.cfg.URL, rcInt.Seconds())
conn.url(), rcInt.Seconds())
time.AfterFunc(rcInt, func() {
conn.reconnectCh <- struct{}{}
})
Expand Down
70 changes: 48 additions & 22 deletions client/core/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ import (
"github.com/decred/dcrd/dcrec/secp256k1/v4"
)

// disconnectDEX unsubscribes from the dex's orderbooks, ends the connection
// with the dex, and removes it from the connection map.
func (c *Core) disconnectDEX(dc *dexConnection) {
// stopDEXConnection unsubscribes from the dex's orderbooks and ends the
// connection with the dex. The dexConnection will still remain in c.conns map.
func (c *Core) stopDEXConnection(dc *dexConnection) {
// Stop dexConnection books.
dc.cfgMtx.RLock()
if dc.cfg != nil {
Expand All @@ -34,42 +34,68 @@ func (c *Core) disconnectDEX(dc *dexConnection) {
}
}
dc.cfgMtx.RUnlock()
dc.connMaster.Disconnect() // disconnect
}

// disconnectDEX disconnects a dex and removes it from the connection map.
func (c *Core) disconnectDEX(dc *dexConnection) {
// Disconnect and delete connection from map.
dc.connMaster.Disconnect()
c.stopDEXConnection(dc)
c.connMtx.Lock()
delete(c.conns, dc.acct.host)
c.connMtx.Unlock()
}

// AccountDisable is used to disable an account by given host and application
// password.
func (c *Core) AccountDisable(pw []byte, addr string) error {
// ToggleAccountStatus is used to disable or enable an account by given host and
// application password.
func (c *Core) ToggleAccountStatus(pw []byte, host string, disable bool) error {
// Validate password.
_, err := c.encryptionKey(pw)
crypter, err := c.encryptionKey(pw)
if err != nil {
return codedError(passwordErr, err)
}

// Get dex connection by host.
dc, _, err := c.dex(addr)
// Get dex connection by host. All exchange servers (enabled or not) are loaded as
// dexConnections but disabled servers are not connected.
dc, _, err := c.dex(host)
if err != nil {
return newError(unknownDEXErr, "error retrieving dex conn: %w", err)
}

// Check active orders or bonds.
if dc.hasActiveOrders() {
return fmt.Errorf("cannot disable account with active orders")
if dc.acct.isDisabled() == disable {
return nil // no-op
}
if dc.hasUnspentBond() {
return fmt.Errorf("cannot disable account with unspent bonds")

if disable {
// Check active orders or bonds.
if dc.hasActiveOrders() {
return fmt.Errorf("cannot disable account with active orders")
}

if dc.hasUnspentBond() {
c.log.Info("Disabling dex server with unspent bonds. Bonds will be refunded when expired.")
}
}

err = c.db.DisableAccount(dc.acct.host)
err = c.db.ToggleAccountStatus(host, disable)
if err != nil {
return newError(accountDisableErr, "error disabling account: %w", err)
return newError(accountStatusUpdateErr, "error updating account status: %w", err)
}

c.disconnectDEX(dc)
if disable {
dc.acct.toggleAccountStatus(true)
c.stopDEXConnection(dc)
} else {
acctInfo, err := c.db.Account(host)
if err != nil {
return err
}
dc, connected := c.connectAccount(acctInfo)
if !connected {
return fmt.Errorf("failed to connected re-enabled account: %w", err)
}
c.initializeDEXConnection(dc, crypter)
}

return nil
}
Expand Down Expand Up @@ -188,7 +214,7 @@ func (c *Core) AccountImport(pw []byte, acct *Account, bonds []*db.Bond) error {
return err
}
c.addDexConnection(dc)
c.initializeDEXConnections(crypter)
c.initializeDEXConnection(dc, crypter)
return nil
}

Expand Down Expand Up @@ -255,7 +281,7 @@ func (c *Core) AccountImport(pw []byte, acct *Account, bonds []*db.Bond) error {
return err
}
c.addDexConnection(dc)
c.initializeDEXConnections(crypter)
c.initializeDEXConnection(dc, crypter)
return nil
}

Expand Down Expand Up @@ -368,9 +394,9 @@ func (c *Core) UpdateDEXHost(oldHost, newHost string, appPW []byte, certI any) (
}
}

err = c.db.DisableAccount(oldDc.acct.host)
err = c.db.ToggleAccountStatus(oldDc.acct.host, true)
if err != nil {
return nil, newError(accountDisableErr, "error disabling account: %w", err)
return nil, newError(accountStatusUpdateErr, "error updating account status: %w", err)
}

updatedHost = true
Expand Down
Loading

0 comments on commit d5ec073

Please sign in to comment.