Skip to content

Commit

Permalink
client/eth: Only call tipChange and peersChange for connected wallets
Browse files Browse the repository at this point in the history
TipChange was being called for tokenWallets that were not yet connected.
This caused core to call the token wallet to retrieve its balance, but
because it was not yet connected, it would return an error.
  • Loading branch information
martonp authored and chappjc committed Feb 17, 2023
1 parent a47bfb1 commit 3f1abe7
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 6 deletions.
28 changes: 22 additions & 6 deletions client/asset/eth/eth.go
Original file line number Diff line number Diff line change
Expand Up @@ -434,6 +434,7 @@ type assetWallet struct {
tipChange func(error)
log dex.Logger
atomicUnit string
connected atomic.Bool

lockedFunds struct {
mtx sync.RWMutex
Expand Down Expand Up @@ -797,6 +798,8 @@ func (w *ETHWallet) Connect(ctx context.Context) (_ *sync.WaitGroup, err error)
atomic.StoreInt64(&w.tipAtConnect, height.Int64())
w.log.Infof("Connected to geth, at height %d", height)

w.connected.Store(true)

var wg sync.WaitGroup
wg.Add(1)
go func() {
Expand All @@ -809,6 +812,12 @@ func (w *ETHWallet) Connect(ctx context.Context) (_ *sync.WaitGroup, err error)
defer wg.Done()
w.monitorPeers(ctx)
}()

go func() {
<-ctx.Done()
w.connected.Store(false)
}()

return &wg, nil
}

Expand All @@ -824,13 +833,16 @@ func (w *TokenWallet) Connect(ctx context.Context) (*sync.WaitGroup, error) {
return nil, err
}

w.connected.Store(true)

var wg sync.WaitGroup
wg.Add(1)
go func() {
defer wg.Done()
select {
case <-ctx.Done():
case <-w.parent.ctx.Done():
w.connected.Store(false)
}
}()
return &wg, nil
Expand Down Expand Up @@ -869,12 +881,16 @@ func (w *TokenWallet) Reconfigure(context.Context, *asset.WalletConfig, string)
return false, nil
}

func (eth *baseWallet) walletList() []*assetWallet {
func (eth *baseWallet) connectedWallets() []*assetWallet {
eth.walletsMtx.RLock()
defer eth.walletsMtx.RUnlock()

m := make([]*assetWallet, 0, len(eth.wallets))

for _, w := range eth.wallets {
m = append(m, w)
if w.connected.Load() {
m = append(m, w)
}
}
return m
}
Expand Down Expand Up @@ -2991,7 +3007,7 @@ func (eth *baseWallet) FeeRate() uint64 {
func (eth *ETHWallet) checkPeers() {
numPeers := eth.node.peerCount()

for _, w := range eth.walletList() {
for _, w := range eth.connectedWallets() {
prevPeer := atomic.SwapUint32(&w.lastPeerCount, numPeers)
if prevPeer != numPeers {
w.peersChange(numPeers, nil)
Expand Down Expand Up @@ -3059,15 +3075,15 @@ func (eth *ETHWallet) checkForNewBlocks(reportErr func(error)) {
eth.log.Debugf("tip change: %s (%s) => %s (%s)", prevTip.Number,
currentTipHash, bestHdr.Number, bestHash)

walletList := eth.walletList()
connectedWallets := eth.connectedWallets()

go func() {
for _, w := range walletList {
for _, w := range connectedWallets {
w.tipChange(nil)
}
}()
go func() {
for _, w := range walletList {
for _, w := range connectedWallets {
w.checkFindRedemptions()
}
}()
Expand Down
1 change: 1 addition & 0 deletions client/asset/eth/eth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -441,6 +441,7 @@ func TestCheckForNewBlocks(t *testing.T) {
currentTip: header0,
}
w.wallets = map[uint32]*assetWallet{BipID: w.assetWallet}
w.assetWallet.connected.Store(true)
w.checkForNewBlocks(tipChange)

if test.hasTipChange {
Expand Down

0 comments on commit 3f1abe7

Please sign in to comment.