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 <orinewman1@gmail.com>
This commit is contained in:
biospb 2022-05-19 17:58:33 +03:00 committed by GitHub
parent 4d0cf2169a
commit 48c7fa0104
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -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
}