From 8c492c24313a4c79b1eeecff82f5212bcdbb5cbc Mon Sep 17 00:00:00 2001 From: Jernej Kos Date: Mon, 4 Apr 2022 14:10:02 +0200 Subject: [PATCH] go/runtime/txpool: Fix crash on early access --- .changelog/4638.bugfix.md | 1 + go/runtime/txpool/txpool.go | 18 ++++++++++++++++++ 2 files changed, 19 insertions(+) create mode 100644 .changelog/4638.bugfix.md diff --git a/.changelog/4638.bugfix.md b/.changelog/4638.bugfix.md new file mode 100644 index 00000000000..fb24602eddf --- /dev/null +++ b/.changelog/4638.bugfix.md @@ -0,0 +1 @@ +go/runtime/txpool: Fix crash on early access diff --git a/go/runtime/txpool/txpool.go b/go/runtime/txpool/txpool.go index ce006718405..ac61370d81b 100644 --- a/go/runtime/txpool/txpool.go +++ b/go/runtime/txpool/txpool.go @@ -273,6 +273,9 @@ func (t *txPool) RemoveTxBatch(txs []hash.Hash) { t.schedulerLock.Lock() defer t.schedulerLock.Unlock() + if t.schedulerQueue == nil { + return + } t.schedulerQueue.RemoveTxBatch(txs) pendingScheduleSize.With(t.getMetricLabels()).Set(float64(t.schedulerQueue.Size())) @@ -282,6 +285,9 @@ func (t *txPool) GetScheduledBatch(force bool) []*transaction.CheckedTransaction t.schedulerLock.Lock() defer t.schedulerLock.Unlock() + if t.schedulerQueue == nil { + return nil + } return t.schedulerQueue.GetBatch(force) } @@ -289,6 +295,9 @@ func (t *txPool) GetPrioritizedBatch(offset *hash.Hash, limit uint32) []*transac t.schedulerLock.Lock() defer t.schedulerLock.Unlock() + if t.schedulerQueue == nil { + return nil + } return t.schedulerQueue.GetPrioritizedBatch(offset, limit) } @@ -296,6 +305,15 @@ func (t *txPool) GetKnownBatch(batch []hash.Hash) ([]*transaction.CheckedTransac t.schedulerLock.Lock() defer t.schedulerLock.Unlock() + if t.schedulerQueue == nil { + result := make([]*transaction.CheckedTransaction, 0, len(batch)) + missing := make(map[hash.Hash]int) + for index, txHash := range batch { + result = append(result, nil) + missing[txHash] = index + } + return result, missing + } return t.schedulerQueue.GetKnownBatch(batch) }