diff --git a/da/mock/mock.go b/da/mock/mock.go index f4652a664d1..83e29ac0c9f 100644 --- a/da/mock/mock.go +++ b/da/mock/mock.go @@ -73,6 +73,10 @@ func (m *DataAvailabilityLayerClient) Init(_ types.NamespaceID, config []byte, d func (m *DataAvailabilityLayerClient) Start() error { m.logger.Debug("Mock Data Availability Layer Client starting") go func() { + // This infinite loop is expected to be stopped and cleaned up + // by the GC. This is OK because it is only a test helper and + // the block time is sufficiently long that there should not be + // a lot of wasted cycles. for { time.Sleep(m.config.BlockTime) m.updateDAHeight() diff --git a/da/mock/util.go b/da/mock/util.go index 048df9532cd..9e21e44075c 100644 --- a/da/mock/util.go +++ b/da/mock/util.go @@ -174,6 +174,10 @@ func RandNamespace() share.Namespace { rb := make([]byte, namespace.NamespaceVersionZeroIDSize) r.Read(rb) // nolint:gosec for { + // This infinite loop is here to ensure that we generate a valid + // namespace for testing. We don't care about errors from + // invalid namespaces and the chances of this loop firing a + // large number of times is very low. namespace, _ := share.NewBlobNamespaceV0(rb) if err := namespace.ValidateForData(); err != nil { continue diff --git a/state/txindex/indexer_service.go b/state/txindex/indexer_service.go index 08cd24fd267..5f842df2826 100644 --- a/state/txindex/indexer_service.go +++ b/state/txindex/indexer_service.go @@ -58,35 +58,44 @@ func (is *IndexerService) OnStart() error { go func() { for { - msg := <-blockHeadersSub.Out() - eventDataHeader := msg.Data().(types.EventDataNewBlockHeader) - height := eventDataHeader.Header.Height - batch := NewBatch(eventDataHeader.NumTxs) - - for i := int64(0); i < eventDataHeader.NumTxs; i++ { - msg2 := <-txsSub.Out() - txResult := msg2.Data().(types.EventDataTx).TxResult - - if err = batch.Add(&txResult); err != nil { - is.Logger.Error( - "failed to add tx to batch", - "height", height, - "index", txResult.Index, - "err", err, - ) + select { + case <-blockHeadersSub.Cancelled(): + return + case msg := <-blockHeadersSub.Out(): + eventDataHeader := msg.Data().(types.EventDataNewBlockHeader) + height := eventDataHeader.Header.Height + batch := NewBatch(eventDataHeader.NumTxs) + + for i := int64(0); i < eventDataHeader.NumTxs; i++ { + select { + case <-txsSub.Cancelled(): + return + case msg2 := <-txsSub.Out(): + txResult := msg2.Data().(types.EventDataTx).TxResult + + if err = batch.Add(&txResult); err != nil { + is.Logger.Error( + "failed to add tx to batch", + "height", height, + "index", txResult.Index, + "err", err, + ) + } + } } - } - if err := is.blockIdxr.Index(eventDataHeader); err != nil { - is.Logger.Error("failed to index block", "height", height, "err", err) - } else { - is.Logger.Info("indexed block", "height", height) - } + if err := is.blockIdxr.Index(eventDataHeader); err != nil { + is.Logger.Error("failed to index block", "height", height, "err", err) + } else { + is.Logger.Info("indexed block", "height", height) + } + + if err = is.txIdxr.AddBatch(batch); err != nil { + is.Logger.Error("failed to index block txs", "height", height, "err", err) + } else { + is.Logger.Debug("indexed block txs", "height", height, "num_txs", eventDataHeader.NumTxs) + } - if err = is.txIdxr.AddBatch(batch); err != nil { - is.Logger.Error("failed to index block txs", "height", height, "err", err) - } else { - is.Logger.Debug("indexed block txs", "height", height, "num_txs", eventDataHeader.NumTxs) } } }()