From 48c7fa010421f91d3563a7006ce5c02a41891d68 Mon Sep 17 00:00:00 2001 From: biospb <42325079+biospb@users.noreply.github.com> Date: Thu, 19 May 2022 17:58:33 +0300 Subject: [PATCH] Wallet synchronization improvement (#2025) * Wallet synchronization improvement * Much faster sync on startup * Avoid double scan of the same address range * Eliminate 1 sec delay on start * Rename constant and add numIndexesToQueryForRecentAddresses const Co-authored-by: Ori Newman --- cmd/kaspawallet/daemon/server/sync.go | 48 ++++++++++++++++++--------- 1 file changed, 33 insertions(+), 15 deletions(-) diff --git a/cmd/kaspawallet/daemon/server/sync.go b/cmd/kaspawallet/daemon/server/sync.go index 41e54331c..029d57594 100644 --- a/cmd/kaspawallet/daemon/server/sync.go +++ b/cmd/kaspawallet/daemon/server/sync.go @@ -26,19 +26,28 @@ func (s *server) sync() error { ticker := time.NewTicker(time.Second) defer ticker.Stop() - for range ticker.C { - err := s.collectRecentAddresses() - if err != nil { - return err - } + err := s.collectRecentAddresses() + if err != nil { + return err + } + err = s.refreshExistingUTXOsWithLock() + if err != nil { + return err + } + + for range ticker.C { err = s.collectFarAddresses() if err != nil { return err } - err = s.refreshExistingUTXOsWithLock() + err = s.collectRecentAddresses() + if err != nil { + return err + } + err = s.refreshExistingUTXOsWithLock() if err != nil { return err } @@ -47,7 +56,8 @@ func (s *server) sync() error { return nil } -const numIndexesToQuery = 100 +const numIndexesToQueryForFarAddresses = 100 +const numIndexesToQueryForRecentAddresses = 1000 // addressesToQuery scans the addresses in the given range. Because // each cosigner in a multisig has its own unique path for generating @@ -75,17 +85,17 @@ func (s *server) addressesToQuery(start, end uint32) (walletAddressSet, error) { return addresses, nil } -// collectFarAddresses collects numIndexesToQuery addresses +// collectFarAddresses collects numIndexesToQueryForFarAddresses addresses // from the last point it stopped in the previous call. func (s *server) collectFarAddresses() error { s.lock.Lock() defer s.lock.Unlock() - err := s.collectAddresses(s.nextSyncStartIndex, s.nextSyncStartIndex+numIndexesToQuery) + err := s.collectAddresses(s.nextSyncStartIndex, s.nextSyncStartIndex+numIndexesToQueryForFarAddresses) if err != nil { return err } - s.nextSyncStartIndex += numIndexesToQuery + s.nextSyncStartIndex += numIndexesToQueryForFarAddresses return nil } @@ -102,18 +112,26 @@ func (s *server) maxUsedIndex() uint32 { } // collectRecentAddresses collects addresses from used addresses until -// the address with the index of the last used address + 1000. -// collectRecentAddresses scans addresses in batches of numIndexesToQuery, +// the address with the index of the last used address + numIndexesToQueryForRecentAddresses. +// collectRecentAddresses scans addresses in batches of numIndexesToQueryForRecentAddresses, // and releases the lock between scans. func (s *server) collectRecentAddresses() error { - maxUsedIndex := s.maxUsedIndex() - for i := uint32(0); i < maxUsedIndex+1000; i += numIndexesToQuery { - err := s.collectAddressesWithLock(i, i+numIndexesToQuery) + index := uint32(0) + maxUsedIndex := uint32(0) + for ; index < maxUsedIndex+numIndexesToQueryForRecentAddresses; index += numIndexesToQueryForRecentAddresses { + err := s.collectAddressesWithLock(index, index+numIndexesToQueryForRecentAddresses) if err != nil { return err } + maxUsedIndex = s.maxUsedIndex() } + s.lock.Lock() + if index > s.nextSyncStartIndex { + s.nextSyncStartIndex = index + } + s.lock.Unlock() + return nil }