github.com/aakash4dev/cometbft@v0.38.2/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 "github.com/aakash4dev/cometbft/libs/log" 21 22 abci "github.com/aakash4dev/cometbft/abci/types" 23 "github.com/aakash4dev/cometbft/libs/pubsub/query" 24 "github.com/aakash4dev/cometbft/state/txindex" 25 "github.com/aakash4dev/cometbft/types" 26 ) 27 28 const ( 29 eventTypeFinalizeBlock = "finalize_block" 30 ) 31 32 // TxIndexer returns a bridge from es to the CometBFT v0.34 transaction indexer. 33 func (es *EventSink) TxIndexer() BackportTxIndexer { 34 return BackportTxIndexer{psql: es} 35 } 36 37 // BackportTxIndexer implements the txindex.TxIndexer interface by delegating 38 // indexing operations to an underlying PostgreSQL event sink. 39 type BackportTxIndexer struct{ psql *EventSink } 40 41 // AddBatch indexes a batch of transactions in Postgres, as part of TxIndexer. 42 func (b BackportTxIndexer) AddBatch(batch *txindex.Batch) error { 43 return b.psql.IndexTxEvents(batch.Ops) 44 } 45 46 // Index indexes a single transaction result in Postgres, as part of TxIndexer. 47 func (b BackportTxIndexer) Index(txr *abci.TxResult) error { 48 return b.psql.IndexTxEvents([]*abci.TxResult{txr}) 49 } 50 51 // Get is implemented to satisfy the TxIndexer interface, but is not supported 52 // by the psql event sink and reports an error for all inputs. 53 func (BackportTxIndexer) Get([]byte) (*abci.TxResult, error) { 54 return nil, errors.New("the TxIndexer.Get method is not supported") 55 } 56 57 // Search is implemented to satisfy the TxIndexer interface, but it is not 58 // supported by the psql event sink and reports an error for all inputs. 59 func (BackportTxIndexer) Search(context.Context, *query.Query) ([]*abci.TxResult, error) { 60 return nil, errors.New("the TxIndexer.Search method is not supported") 61 } 62 63 func (BackportTxIndexer) SetLogger(log.Logger) {} 64 65 // BlockIndexer returns a bridge that implements the CometBFT v0.34 block 66 // indexer interface, using the Postgres event sink as a backing store. 67 func (es *EventSink) BlockIndexer() BackportBlockIndexer { 68 return BackportBlockIndexer{psql: es} 69 } 70 71 // BackportBlockIndexer implements the indexer.BlockIndexer interface by 72 // delegating indexing operations to an underlying PostgreSQL event sink. 73 type BackportBlockIndexer struct{ psql *EventSink } 74 75 // Has is implemented to satisfy the BlockIndexer interface, but it is not 76 // supported by the psql event sink and reports an error for all inputs. 77 func (BackportBlockIndexer) Has(_ int64) (bool, error) { 78 return false, errors.New("the BlockIndexer.Has method is not supported") 79 } 80 81 // Index indexes block begin and end events for the specified block. It is 82 // part of the BlockIndexer interface. 83 func (b BackportBlockIndexer) Index(block types.EventDataNewBlockEvents) error { 84 return b.psql.IndexBlockEvents(block) 85 } 86 87 // Search is implemented to satisfy the BlockIndexer interface, but it is not 88 // supported by the psql event sink and reports an error for all inputs. 89 func (BackportBlockIndexer) Search(context.Context, *query.Query) ([]int64, error) { 90 return nil, errors.New("the BlockIndexer.Search method is not supported") 91 } 92 93 func (BackportBlockIndexer) SetLogger(log.Logger) {}