Skip to content

Commit

Permalink
Wallet synchronization improvement (kaspanet#2025)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>
  • Loading branch information
biospb and someone235 authored May 19, 2022
1 parent 4d0cf21 commit 48c7fa0
Showing 1 changed file with 30 additions and 12 deletions.
42 changes: 30 additions & 12 deletions cmd/kaspawallet/daemon/server/sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,19 +26,28 @@ func (s *server) sync() error {
ticker := time.NewTicker(time.Second)
defer ticker.Stop()

err := s.collectRecentAddresses()
if err != nil {
return err
}

err = s.refreshExistingUTXOsWithLock()
if err != nil {
return err
}

for range ticker.C {
err := s.collectRecentAddresses()
err = s.collectFarAddresses()
if err != nil {
return err
}

err = s.collectFarAddresses()
err = s.collectRecentAddresses()
if err != nil {
return err
}

err = s.refreshExistingUTXOsWithLock()

if err != nil {
return err
}
Expand All @@ -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
Expand Down Expand Up @@ -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
}

Expand All @@ -102,17 +112,25 @@ 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
}
Expand Down

0 comments on commit 48c7fa0

Please sign in to comment.