decred / dcrwallet

A secure Decred wallet daemon written in Go (golang).
https://decred.org
ISC License
216 stars 155 forks source link

chain: Request block ntfns only after initial sync #2310

Closed matheusd closed 10 months ago

matheusd commented 10 months ago

Previously, request for notifications about connected blocks were sent to the underlying dcrd instance before the chain was fully synced. This could cause a race issue if the underlying dcrd instance was also performing an initial sync, causing blocks to be connected by the blockConnected handler before the active data filter was loaded, causing transactions to be missed and the wallet's balance to be wrong, requiring a rescan to be fixed.

This could happen, for example, if both the dcrd and dcrwallet processes were started at the same time or if dcrd took just enough time to connect to the network that the wallet was also initialized (for exemple, from within Decrediton).

This fixes the issue by refactoring the initial sync code to only request block notifications after the initial header sync has been completed by the wallet.

Note that, despite a large(ish) diff, this is mostly a code move PR.

One way to reproduce the above issue in simnet is the following:

diff --git a/chain/sync.go b/chain/sync.go
index 0881e218..583cdcb2 100644
--- a/chain/sync.go
+++ b/chain/sync.go
@@ -13,6 +13,7 @@ import (
    "runtime/trace"
    "sync"
    "sync/atomic"
+   "time"

    "decred.org/dcrwallet/v4/errors"
    "decred.org/dcrwallet/v4/rpc/client/dcrd"
@@ -328,6 +329,12 @@ func (s *Syncer) Run(ctx context.Context) (err error) {
        return err
    }

+   log.Infof("XXXXXXXXXXXXXXX Gonna wait 5 seconds")
+   for i := 0; i < 5; i++ {
+       time.Sleep(time.Second)
+       log.Infof("XXXXXXXXXXXX %d", i+1)
+   }
+
    cnet := s.wallet.ChainParams().Net
    s.fetchHeadersStart()
    for {
jrick commented 10 months ago

needs a rebase