Skip to content

Commit

Permalink
birthday: Set correctly for existing wallets.
Browse files Browse the repository at this point in the history
  • Loading branch information
JoeGruffins committed Jun 19, 2024
1 parent eccbb0d commit 594fb24
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 4 deletions.
85 changes: 85 additions & 0 deletions chain/sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -508,6 +508,87 @@ func (s *Syncer) waitRPCSync(ctx context.Context, minHeight int64) error {
}
}

// SetBirthState sets the wallet birthstate if required and if we already have
// enough info to do so.
func SetBirthState(ctx context.Context, w *wallet.Wallet, rescanPoint *chainhash.Hash) error {
birthState, err := w.BirthState(ctx)
if err != nil {
return err
}

if birthState != nil && (birthState.SetFromTime || birthState.SetFromHeight) {
var syncedHeader *wire.BlockHeader
if rescanPoint != nil {
syncedHeader, err = w.BlockHeader(ctx, rescanPoint)
if err != nil {
return err
}
} else {
tipHash, _ := w.MainChainTip(ctx)
syncedHeader, err = w.BlockHeader(ctx, &tipHash)
if err != nil {
return err
}
}
if birthState.SetFromTime {
if syncedHeader.Timestamp.After(birthState.Time) {
h := syncedHeader
for {
if h.Height == 0 {
bh := h.BlockHash()
birthState.Hash = bh
birthState.Height = 0
birthState.SetFromTime = false
if err := w.SetBirthState(ctx, birthState); err != nil {
return err
}
log.Infof("Set wallet birthday to block 0 (%v).", bh)
break
}
h, err = w.BlockHeader(ctx, &h.PrevBlock)
if err != nil {
return err
}
if h.Timestamp.Before(birthState.Time) {
bh := h.PrevBlock
height := h.Height - 1
birthState.Hash = bh
birthState.Height = height
birthState.SetFromTime = false
if err := w.SetBirthState(ctx, birthState); err != nil {
return err
}
log.Infof("Set wallet birthday to block %d (%v).", height, bh)
break
}
}
}
}
if birthState.SetFromHeight {
if syncedHeader.Height >= birthState.Height {
h := syncedHeader
for {
if h.Height == birthState.Height {
bh := h.BlockHash()
birthState.Hash = bh
birthState.SetFromHeight = false
if err := w.SetBirthState(ctx, birthState); err != nil {
return err
}
log.Infof("Set wallet birthday to block %d (%v).", h.Height, bh)
break
}
h, err = w.BlockHeader(ctx, &h.PrevBlock)
if err != nil {
return err
}
}
}
}
}
return nil
}

// hashStop is a zero value stop hash for fetching all possible data using
// locators.
var hashStop chainhash.Hash
Expand Down Expand Up @@ -627,6 +708,10 @@ func (s *Syncer) Run(ctx context.Context) (err error) {
log.Infof("Transactions synced through block %v height %d", &tipHash, tipHeight)
}

if err := SetBirthState(ctx, s.wallet, rescanPoint); err != nil {
return err
}

err = s.waitRPCSync(ctx, int64(tipHeight))
if err != nil {
return err
Expand Down
6 changes: 6 additions & 0 deletions spv/sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"sync/atomic"
"time"

"decred.org/dcrwallet/v4/chain"
"decred.org/dcrwallet/v4/errors"
"decred.org/dcrwallet/v4/lru"
"decred.org/dcrwallet/v4/p2p"
Expand Down Expand Up @@ -1724,6 +1725,11 @@ func (s *Syncer) initialSyncRescan(ctx context.Context) error {
if err != nil {
return err
}

if err := chain.SetBirthState(ctx, s.wallet, rescanPoint); err != nil {
return err
}

if rescanPoint == nil {
// The wallet is already up to date with transactions in all
// blocks. Load the data filters to check for transactions in
Expand Down
9 changes: 7 additions & 2 deletions wallet/rescan.go
Original file line number Diff line number Diff line change
Expand Up @@ -494,9 +494,13 @@ func (w *Wallet) rescanPoint(dbtx walletdb.ReadTx) (*chainhash.Hash, error) {
return &rescanPoint, nil
}

// SetBirthState sets the birthday state in the database.
// SetBirthState sets the birthday state in the database. This should be called
// before syncing is started.
func (w *Wallet) SetBirthState(ctx context.Context, bs *udb.BirthdayState) error {
const op errors.Op = "wallet.SetBirthState"
if bs == nil {
return errors.E(op, errors.Invalid, "nil birthday state")
}
err := walletdb.Update(ctx, w.db, func(dbtx walletdb.ReadWriteTx) error {
return udb.SetBirthState(dbtx, bs)
})
Expand All @@ -506,7 +510,8 @@ func (w *Wallet) SetBirthState(ctx context.Context, bs *udb.BirthdayState) error
return nil
}

// BirthState returns the birthday state.
// BirthState returns the birthday state. Will return a nil state if none has
// been set.
func (w *Wallet) BirthState(ctx context.Context) (bs *udb.BirthdayState, err error) {
const op errors.Op = "wallet.BirthState"
err = walletdb.View(ctx, w.db, func(dbtx walletdb.ReadTx) error {
Expand Down
6 changes: 4 additions & 2 deletions wallet/udb/txmined.go
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,8 @@ type BirthdayState struct {
SetFromHeight, SetFromTime bool
}

// SetBirthState sets the birthday state in the database.
// SetBirthState sets the birthday state in the database. *BirthdayState must
// not be nil.
//
// [0:1] Options (1 byte)
// [1:33] Birthblock block header hash (32 bytes)
Expand Down Expand Up @@ -361,7 +362,8 @@ func SetBirthState(dbtx walletdb.ReadWriteTx, bs *BirthdayState) error {
return ns.Put(rootBirthState, v)
}

// BirthState returns the current birthday state.
// BirthState returns the current birthday state. Will return nil if none has
// been set.
func BirthState(dbtx walletdb.ReadTx) *BirthdayState {
ns := dbtx.ReadBucket(wtxmgrBucketKey)
const (
Expand Down

0 comments on commit 594fb24

Please sign in to comment.