github.com/badrootd/celestia-core@v0.0.0-20240305091328-aa4207a4b25d/state/indexer/sink/psql/backport.go (about)

     1  package psql
     2  
     3  // This file adds code to the psql package that is needed for integration with
     4  // v0.34, but which is not part of the original implementation.
     5  //
     6  // In v0.35, ADR 65 was implemented in which the TxIndexer and BlockIndexer
     7  // interfaces were merged into a hybrid EventSink interface. The Backport*
     8  // types defined here bridge the psql EventSink (which was built in terms of
     9  // the v0.35 interface) to the old interfaces.
    10  //
    11  // We took this narrower approach to backporting to avoid pulling in a much
    12  // wider-reaching set of changes in v0.35 that would have broken several of the
    13  // v0.34.x APIs. The result is sufficient to work with the node plumbing as it
    14  // exists in the v0.34 branch.
    15  
    16  import (
    17  	"context"
    18  	"errors"
    19  
    20  	abci "github.com/badrootd/celestia-core/abci/types"
    21  	"github.com/badrootd/celestia-core/libs/pubsub/query"
    22  	"github.com/badrootd/celestia-core/state/txindex"
    23  	"github.com/badrootd/celestia-core/types"
    24  )
    25  
    26  const (
    27  	eventTypeBeginBlock = "begin_block"
    28  	eventTypeEndBlock   = "end_block"
    29  )
    30  
    31  // TxIndexer returns a bridge from es to the CometBFT v0.34 transaction indexer.
    32  func (es *EventSink) TxIndexer() BackportTxIndexer {
    33  	return BackportTxIndexer{psql: es}
    34  }
    35  
    36  // BackportTxIndexer implements the txindex.TxIndexer interface by delegating
    37  // indexing operations to an underlying PostgreSQL event sink.
    38  type BackportTxIndexer struct{ psql *EventSink }
    39  
    40  // AddBatch indexes a batch of transactions in Postgres, as part of TxIndexer.
    41  func (b BackportTxIndexer) AddBatch(batch *txindex.Batch) error {
    42  	return b.psql.IndexTxEvents(batch.Ops)
    43  }
    44  
    45  // Index indexes a single transaction result in Postgres, as part of TxIndexer.
    46  func (b BackportTxIndexer) Index(txr *abci.TxResult) error {
    47  	return b.psql.IndexTxEvents([]*abci.TxResult{txr})
    48  }
    49  
    50  // Get is implemented to satisfy the TxIndexer interface, but is not supported
    51  // by the psql event sink and reports an error for all inputs.
    52  func (BackportTxIndexer) Get([]byte) (*abci.TxResult, error) {
    53  	return nil, errors.New("the TxIndexer.Get method is not supported")
    54  }
    55  
    56  // Search is implemented to satisfy the TxIndexer interface, but it is not
    57  // supported by the psql event sink and reports an error for all inputs.
    58  func (BackportTxIndexer) Search(context.Context, *query.Query) ([]*abci.TxResult, error) {
    59  	return nil, errors.New("the TxIndexer.Search method is not supported")
    60  }
    61  
    62  // BlockIndexer returns a bridge that implements the CometBFT v0.34 block
    63  // indexer interface, using the Postgres event sink as a backing store.
    64  func (es *EventSink) BlockIndexer() BackportBlockIndexer {
    65  	return BackportBlockIndexer{psql: es}
    66  }
    67  
    68  // BackportBlockIndexer implements the indexer.BlockIndexer interface by
    69  // delegating indexing operations to an underlying PostgreSQL event sink.
    70  type BackportBlockIndexer struct{ psql *EventSink }
    71  
    72  // Has is implemented to satisfy the BlockIndexer interface, but it is not
    73  // supported by the psql event sink and reports an error for all inputs.
    74  func (BackportBlockIndexer) Has(height int64) (bool, error) {
    75  	return false, errors.New("the BlockIndexer.Has method is not supported")
    76  }
    77  
    78  // Index indexes block begin and end events for the specified block.  It is
    79  // part of the BlockIndexer interface.
    80  func (b BackportBlockIndexer) Index(block types.EventDataNewBlockHeader) error {
    81  	return b.psql.IndexBlockEvents(block)
    82  }
    83  
    84  // Search is implemented to satisfy the BlockIndexer interface, but it is not
    85  // supported by the psql event sink and reports an error for all inputs.
    86  func (BackportBlockIndexer) Search(context.Context, *query.Query) ([]int64, error) {
    87  	return nil, errors.New("the BlockIndexer.Search method is not supported")
    88  }