From bf703254ba242293c39abd9102420be9e2cc2e25 Mon Sep 17 00:00:00 2001 From: Roman Volosovskyi Date: Wed, 2 Dec 2020 11:48:18 +0200 Subject: [PATCH] [wallet] Dispatch event on tx history fetching failure --- VERSION | 2 +- services/wallet/commands.go | 35 +++++++++++++++++++++++++++++++++++ services/wallet/events.go | 3 +++ services/wallet/reactor.go | 1 + 4 files changed, 40 insertions(+), 1 deletion(-) diff --git a/VERSION b/VERSION index e40e4fc33..1b37a5f7e 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -0.66.0 +0.66.1 diff --git a/services/wallet/commands.go b/services/wallet/commands.go index 346cbd5e8..cc7cdbb0d 100644 --- a/services/wallet/commands.go +++ b/services/wallet/commands.go @@ -451,6 +451,7 @@ type controlCommand struct { feed *event.Feed safetyDepth *big.Int watchNewBlocks bool + errorsCount int } // run fast indexing for every accont up to canonical chain head minus safety depth. @@ -708,6 +709,9 @@ func (c *controlCommand) Run(parent context.Context) error { head, err := c.client.HeaderByNumber(ctx, nil) cancel() if err != nil { + if c.NewError(err) { + return nil + } return err } @@ -720,11 +724,17 @@ func (c *controlCommand) Run(parent context.Context) error { lastKnownEthBlocks, accountsWithoutHistory, err := c.db.GetLastKnownBlockByAddresses(c.accounts) if err != nil { log.Error("failed to load last head from database", "error", err) + if c.NewError(err) { + return nil + } return err } fromMap, err := findFirstRanges(parent, accountsWithoutHistory, head.Number, c.client) if err != nil { + if c.NewError(err) { + return nil + } return err } @@ -760,6 +770,9 @@ func (c *controlCommand) Run(parent context.Context) error { err = cmnd.Command()(parent) if err != nil { + if c.NewError(err) { + return nil + } return err } @@ -771,6 +784,9 @@ func (c *controlCommand) Run(parent context.Context) error { } _, err = c.LoadTransfers(parent, downloader, 40) if err != nil { + if c.NewError(err) { + return nil + } return err } @@ -796,6 +812,9 @@ func (c *controlCommand) Run(parent context.Context) error { head, err = c.client.HeaderByNumber(ctx, target) cancel() if err != nil { + if c.NewError(err) { + return nil + } return err } @@ -815,6 +834,9 @@ func (c *controlCommand) Run(parent context.Context) error { err = cmd.Command()(parent) if err != nil { + if c.NewError(err) { + return nil + } log.Warn("error on running newBlocksTransfersCommand", "err", err) return err } @@ -830,6 +852,19 @@ func (c *controlCommand) Run(parent context.Context) error { return err } +func (c *controlCommand) NewError(err error) bool { + c.errorsCount++ + log.Error("controlCommand error", "error", err, "counter", c.errorsCount) + if c.errorsCount >= 3 { + c.feed.Send(Event{ + Type: EventFetchingHistoryError, + Message: err.Error(), + }) + return true + } + return false +} + func (c *controlCommand) Command() Command { return FiniteCommand{ Interval: 5 * time.Second, diff --git a/services/wallet/events.go b/services/wallet/events.go index e5b0dc225..ba138d1d7 100644 --- a/services/wallet/events.go +++ b/services/wallet/events.go @@ -21,6 +21,8 @@ const ( EventFetchingRecentHistory EventType = "recent-history-fetching" // EventRecentHistoryFetched emitted when fetching of lastest tx history is started EventRecentHistoryReady EventType = "recent-history-ready" + // EventRecentHistoryError emitted when fetching of tx history failed + EventFetchingHistoryError EventType = "fetching-history-error" ) // Event is a type for wallet events. @@ -30,4 +32,5 @@ type Event struct { Accounts []common.Address `json:"accounts"` NewTransactionsPerAccount map[common.Address]int `json:"newTransactions"` ERC20 bool `json:"erc20"` + Message string `json:"message"` } diff --git a/services/wallet/reactor.go b/services/wallet/reactor.go index 58ed2a425..e97636c3e 100644 --- a/services/wallet/reactor.go +++ b/services/wallet/reactor.go @@ -102,6 +102,7 @@ func (r *Reactor) newControlCommand(accounts []common.Address) *controlCommand { feed: r.feed, safetyDepth: reorgSafetyDepth(r.chain), watchNewBlocks: r.watchNewBlocks, + errorsCount: 0, } return ctl