[#4649] Do not restart reactor when it is already running (#4663)

This commit is contained in:
Roman Volosovskyi 2024-02-01 17:26:40 +01:00 committed by GitHub
parent 31a2d403ef
commit 241595a871
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 58 additions and 22 deletions

View File

@ -189,15 +189,24 @@ func (c *findNewBlocksCommand) Run(parent context.Context) error {
c.blockChainState.SetLastBlockNumber(c.chainClient.NetworkID(), headNum.Uint64())
if len(accountsWithDetectedChanges) != 0 {
_ = c.findAndSaveEthBlocks(parent, c.fromBlockNumber, headNum, accountsToCheck)
log.Debug("findNewBlocksCommand detected accounts with changes, proceeding", "accounts", accountsWithDetectedChanges)
err = c.findAndSaveEthBlocks(parent, c.fromBlockNumber, headNum, accountsToCheck)
if err != nil {
return err
}
} else if c.iteration%nonceCheckIntervalIterations == 0 && len(accountsWithOutsideTransfers) > 0 {
log.Debug("findNewBlocksCommand nonce check", "accounts", accountsWithOutsideTransfers)
accountsWithNonceChanges, err := c.detectNonceChange(parent, c.fromBlockNumber, headNum, accountsWithOutsideTransfers)
if err != nil {
return err
}
if len(accountsWithNonceChanges) > 0 {
_ = c.findAndSaveEthBlocks(parent, c.fromBlockNumber, headNum, accountsWithNonceChanges)
log.Debug("findNewBlocksCommand detected nonce diff", "accounts", accountsWithNonceChanges)
err = c.findAndSaveEthBlocks(parent, c.fromBlockNumber, headNum, accountsWithNonceChanges)
if err != nil {
return err
}
}
for _, account := range accountsToCheck {
@ -212,7 +221,10 @@ func (c *findNewBlocksCommand) Run(parent context.Context) error {
}
if len(accountsWithDetectedChanges) != 0 || c.iteration%logsCheckIntervalIterations == 0 {
_ = c.findAndSaveTokenBlocks(parent, c.fromBlockNumber, headNum)
err = c.findAndSaveTokenBlocks(parent, c.fromBlockNumber, headNum)
if err != nil {
return err
}
}
c.fromBlockNumber = headNum
c.iteration++

View File

@ -75,6 +75,20 @@ func (c *Controller) Stop() {
}
}
func sameChains(chainIDs1 []uint64, chainIDs2 []uint64) bool {
if len(chainIDs1) != len(chainIDs2) {
return false
}
for _, chainID := range chainIDs1 {
if !slices.Contains(chainIDs2, chainID) {
return false
}
}
return true
}
func (c *Controller) CheckRecentHistory(chainIDs []uint64, accounts []common.Address) error {
if len(accounts) == 0 {
return nil
@ -95,34 +109,39 @@ func (c *Controller) CheckRecentHistory(chainIDs []uint64, accounts []common.Add
}
if c.reactor != nil {
err := c.reactor.restart(chainClients, accounts)
if err != nil {
return err
}
} else {
multiaccSettings, err := c.accountsDB.GetSettings()
if err != nil {
return err
}
omitHistory := multiaccSettings.OmitTransfersHistoryScan
if omitHistory {
err := c.accountsDB.SaveSettingField(settings.OmitTransfersHistoryScan, false)
if !sameChains(chainIDs, c.reactor.chainIDs) {
err := c.reactor.restart(chainClients, accounts)
if err != nil {
return err
}
}
c.reactor = NewReactor(c.db, c.blockDAO, c.blockRangesSeqDAO, c.accountsDB, c.TransferFeed, c.transactionManager,
c.pendingTxManager, c.tokenManager, c.balanceCacher, omitHistory, c.blockChainState)
return nil
}
err = c.reactor.start(chainClients, accounts)
multiaccSettings, err := c.accountsDB.GetSettings()
if err != nil {
return err
}
omitHistory := multiaccSettings.OmitTransfersHistoryScan
if omitHistory {
err := c.accountsDB.SaveSettingField(settings.OmitTransfersHistoryScan, false)
if err != nil {
return err
}
c.startAccountWatcher(chainIDs)
}
c.reactor = NewReactor(c.db, c.blockDAO, c.blockRangesSeqDAO, c.accountsDB, c.TransferFeed, c.transactionManager,
c.pendingTxManager, c.tokenManager, c.balanceCacher, omitHistory, c.blockChainState)
err = c.reactor.start(chainClients, accounts)
if err != nil {
return err
}
c.startAccountWatcher(chainIDs)
return nil
}

View File

@ -60,6 +60,7 @@ type Reactor struct {
balanceCacher balance.Cacher
omitHistory bool
blockChainState *blockchainstate.BlockChainState
chainIDs []uint64
}
func NewReactor(db *Database, blockDAO *BlockDAO, blockRangesSeqDAO *BlockRangeSequentialDAO, accountsDB *accounts.Database, feed *event.Feed, tm *TransactionManager,
@ -82,7 +83,11 @@ func NewReactor(db *Database, blockDAO *BlockDAO, blockRangesSeqDAO *BlockRangeS
// Start runs reactor loop in background.
func (r *Reactor) start(chainClients map[uint64]chain.ClientInterface, accounts []common.Address) error {
chainIDs := []uint64{}
for _, client := range chainClients {
chainIDs = append(chainIDs, client.NetworkID())
}
r.chainIDs = chainIDs
r.strategy = r.createFetchStrategy(chainClients, accounts)
return r.strategy.start()
}