Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sync: Recreate syncer every try. #18

Merged
merged 1 commit into from
Mar 12, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@ jobs:
go: ["1.22", "1.23"]
steps:
- name: Check out source
uses: actions/checkout@692973e3d937129bcbf40652eb9f2f61becf3332 # v4.1.7
uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 #v4.2.2
- name: Set up Go
uses: actions/setup-go@0a12ed9d6a96ab950c8f026ed9f722fe0da7ef32 # v5.0.2
uses: actions/setup-go@f111f3307d8850f501ac008e886eec1fd1932a34 #v5.3.0
with:
go-version: ${{ matrix.go }}
- name: Use lint cache
uses: actions/cache@0c45773b623bea8c8e75f6c82b208c3cf94ea4f9 # v4.0.2
uses: actions/cache@1bd1e32a3bdc45362d1e726936510720a7c30a57 #v4.2.0
with:
path: |
~/.cache/golangci-lint
Expand Down
34 changes: 25 additions & 9 deletions asset/dcr/sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,24 +27,36 @@ func (w *Wallet) StartSync(ctx context.Context, ntfns *spv.Notifications, connec
addr := &net.TCPAddr{IP: net.ParseIP("::1"), Port: 0}
amgr := addrmgr.New(w.dir, net.LookupIP)
lp := p2p.NewLocalPeer(w.ChainParams(), addr, amgr)
syncer := spv.NewSyncer(w.mainWallet, lp)
if len(connectPeers) > 0 {
syncer.SetPersistentPeers(connectPeers)
}
syncer.SetNotifications(ntfns)

// TODO: Set a birthday to sync from. I don't think dcrwallet allows
// this currently.
// We must create a new syncer for every attempt or we will get a
// closing closed channel panic when close(s.initialSyncDone) happens
// for the second time inside dcrwallet.
newSyncer := func() *spv.Syncer {
w.syncerMtx.Lock()
defer w.syncerMtx.Unlock()
syncer := spv.NewSyncer(w.mainWallet, lp)
if len(connectPeers) > 0 {
syncer.SetPersistentPeers(connectPeers)
}
syncer.SetNotifications(ntfns)

// TODO: Set a birthday to sync from. I don't think dcrwallet allows
// this currently.

w.syncer = syncer
w.SetNetworkBackend(syncer)
w.syncer = syncer
w.SetNetworkBackend(syncer)
return syncer
}

// Start the syncer in a goroutine, monitor when the sync ctx is canceled
// and then disconnect the sync.
go func() {
for {
syncer := newSyncer()
err := syncer.Run(ctx)
if ctx.Err() != nil {
w.syncerMtx.Lock()
defer w.syncerMtx.Unlock()
// sync ctx canceled, quit syncing
w.syncer = nil
w.SetNetworkBackend(nil)
Expand Down Expand Up @@ -77,6 +89,8 @@ func (w *Wallet) IsSyncing(ctx context.Context) bool {
// IsSynced returns true if the wallet has synced up to the best block on the
// mainchain.
func (w *Wallet) IsSynced(ctx context.Context) (bool, int32) {
w.syncerMtx.RLock()
defer w.syncerMtx.RUnlock()
if w.syncer != nil {
return w.syncer.Synced(ctx)
}
Expand All @@ -89,5 +103,7 @@ func (w *Wallet) IsSynced(ctx context.Context) (bool, int32) {
// completes or ends in an error. p is closed before returning.
func (w *Wallet) RescanProgressFromHeight(ctx context.Context,
startHeight int32, p chan<- dcrwallet.RescanProgress) {
w.syncerMtx.RLock()
defer w.syncerMtx.RUnlock()
w.mainWallet.RescanProgressFromHeight(ctx, w.syncer, startHeight, p)
}
2 changes: 2 additions & 0 deletions asset/dcr/transactions.go
Original file line number Diff line number Diff line change
Expand Up @@ -262,5 +262,7 @@ func (w *Wallet) SendRawTransaction(ctx context.Context, txHex string) (*chainha
if err := msgTx.FromBytes(msgBytes); err != nil {
return nil, fmt.Errorf("unable to create msgtx from bytes: %v", err)
}
w.syncerMtx.RLock()
defer w.syncerMtx.RUnlock()
return w.mainWallet.PublishTransaction(ctx, msgTx, w.syncer)
}
4 changes: 3 additions & 1 deletion asset/dcr/wallet.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"fmt"
"path/filepath"
"sync"

"decred.org/dcrwallet/v4/spv"
"decred.org/dcrwallet/v4/wallet"
Expand All @@ -24,7 +25,8 @@ type Wallet struct {
db wallet.DB
*mainWallet

syncer *spv.Syncer
syncerMtx sync.RWMutex
syncer *spv.Syncer
}

// MainWallet returns the main dcr wallet with the core wallet functionalities.
Expand Down
Loading