mirror of
https://github.com/kaspanet/kaspad.git
synced 2025-06-07 14:46:44 +00:00
Lazy wallet utxo sync after broadcasting a tx
This commit is contained in:
parent
629faa8436
commit
524a654886
@ -54,11 +54,7 @@ func (s *server) broadcast(transactions [][]byte, isDomain bool) ([]string, erro
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
err = s.refreshUTXOs()
|
s.forceSync()
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
return txIDs, nil
|
return txIDs, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -43,11 +43,6 @@ func (s *server) createUnsignedTransactions(address string, amount uint64, isSen
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
err = s.refreshUTXOs()
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
var fromAddresses []*walletAddress
|
var fromAddresses []*walletAddress
|
||||||
for _, from := range fromAddressesString {
|
for _, from := range fromAddressesString {
|
||||||
fromAddress, exists := s.addressSet[from]
|
fromAddress, exists := s.addressSet[from]
|
||||||
|
@ -36,6 +36,7 @@ type server struct {
|
|||||||
nextSyncStartIndex uint32
|
nextSyncStartIndex uint32
|
||||||
keysFile *keys.File
|
keysFile *keys.File
|
||||||
shutdown chan struct{}
|
shutdown chan struct{}
|
||||||
|
forceSyncChan chan struct{}
|
||||||
addressSet walletAddressSet
|
addressSet walletAddressSet
|
||||||
txMassCalculator *txmass.Calculator
|
txMassCalculator *txmass.Calculator
|
||||||
usedOutpoints map[externalapi.DomainOutpoint]time.Time
|
usedOutpoints map[externalapi.DomainOutpoint]time.Time
|
||||||
@ -91,6 +92,7 @@ func Start(params *dagconfig.Params, listen, rpcServer string, keysFilePath stri
|
|||||||
nextSyncStartIndex: 0,
|
nextSyncStartIndex: 0,
|
||||||
keysFile: keysFile,
|
keysFile: keysFile,
|
||||||
shutdown: make(chan struct{}),
|
shutdown: make(chan struct{}),
|
||||||
|
forceSyncChan: make(chan struct{}),
|
||||||
addressSet: make(walletAddressSet),
|
addressSet: make(walletAddressSet),
|
||||||
txMassCalculator: txmass.NewCalculator(params.MassPerTxByte, params.MassPerScriptPubKeyByte, params.MassPerSigOp),
|
txMassCalculator: txmass.NewCalculator(params.MassPerTxByte, params.MassPerScriptPubKeyByte, params.MassPerSigOp),
|
||||||
usedOutpoints: map[externalapi.DomainOutpoint]time.Time{},
|
usedOutpoints: map[externalapi.DomainOutpoint]time.Time{},
|
||||||
@ -100,8 +102,8 @@ func Start(params *dagconfig.Params, listen, rpcServer string, keysFilePath stri
|
|||||||
}
|
}
|
||||||
|
|
||||||
log.Infof("Read, syncing the wallet...")
|
log.Infof("Read, syncing the wallet...")
|
||||||
spawn("serverInstance.sync", func() {
|
spawn("serverInstance.syncLoop", func() {
|
||||||
err := serverInstance.sync()
|
err := serverInstance.syncLoop()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
printErrorAndExit(errors.Wrap(err, "error syncing the wallet"))
|
printErrorAndExit(errors.Wrap(err, "error syncing the wallet"))
|
||||||
}
|
}
|
||||||
|
@ -23,7 +23,7 @@ func (was walletAddressSet) strings() []string {
|
|||||||
return addresses
|
return addresses
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *server) sync() error {
|
func (s *server) syncLoop() error {
|
||||||
ticker := time.NewTicker(time.Second)
|
ticker := time.NewTicker(time.Second)
|
||||||
defer ticker.Stop()
|
defer ticker.Stop()
|
||||||
|
|
||||||
@ -37,8 +37,24 @@ func (s *server) sync() error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
for range ticker.C {
|
for {
|
||||||
err = s.collectFarAddresses()
|
select {
|
||||||
|
case <-ticker.C:
|
||||||
|
err := s.sync()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
case <-s.forceSyncChan:
|
||||||
|
err := s.sync()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *server) sync() error {
|
||||||
|
err := s.collectFarAddresses()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@ -48,13 +64,7 @@ func (s *server) sync() error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
err = s.refreshExistingUTXOsWithLock()
|
return s.refreshExistingUTXOsWithLock()
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const (
|
const (
|
||||||
@ -280,6 +290,10 @@ func (s *server) refreshUTXOs() error {
|
|||||||
return s.updateUTXOSet(getUTXOsByAddressesResponse.Entries, mempoolEntriesByAddresses.Entries)
|
return s.updateUTXOSet(getUTXOsByAddressesResponse.Entries, mempoolEntriesByAddresses.Entries)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (s *server) forceSync() {
|
||||||
|
s.forceSyncChan <- struct{}{}
|
||||||
|
}
|
||||||
|
|
||||||
func (s *server) isSynced() bool {
|
func (s *server) isSynced() bool {
|
||||||
return s.nextSyncStartIndex > s.maxUsedIndex()
|
return s.nextSyncStartIndex > s.maxUsedIndex()
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user