Skip to content

Commit

Permalink
add IndexedLogsByTxHash & index (#10731)
Browse files Browse the repository at this point in the history
* add IndexedLogsByTxHash & index

* add orm test

* don't use brin
  • Loading branch information
RensR authored Sep 21, 2023
1 parent 7b8e30e commit c1dee6c
Show file tree
Hide file tree
Showing 7 changed files with 135 additions and 0 deletions.
4 changes: 4 additions & 0 deletions core/chains/evm/logpoller/disabled.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,10 @@ func (disabled) IndexedLogsByBlockRange(start, end int64, eventSig common.Hash,
return nil, ErrDisabled
}

func (d disabled) IndexedLogsByTxHash(eventSig common.Hash, txHash common.Hash, qopts ...pg.QOpt) ([]Log, error) {
return nil, ErrDisabled
}

func (disabled) IndexedLogsTopicGreaterThan(eventSig common.Hash, address common.Address, topicIndex int, topicValueMin common.Hash, confs int, qopts ...pg.QOpt) ([]Log, error) {
return nil, ErrDisabled
}
Expand Down
5 changes: 5 additions & 0 deletions core/chains/evm/logpoller/log_poller.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ type LogPoller interface {
IndexedLogs(eventSig common.Hash, address common.Address, topicIndex int, topicValues []common.Hash, confs int, qopts ...pg.QOpt) ([]Log, error)
IndexedLogsByBlockRange(start, end int64, eventSig common.Hash, address common.Address, topicIndex int, topicValues []common.Hash, qopts ...pg.QOpt) ([]Log, error)
IndexedLogsCreatedAfter(eventSig common.Hash, address common.Address, topicIndex int, topicValues []common.Hash, after time.Time, confs int, qopts ...pg.QOpt) ([]Log, error)
IndexedLogsByTxHash(eventSig common.Hash, txHash common.Hash, qopts ...pg.QOpt) ([]Log, error)
IndexedLogsTopicGreaterThan(eventSig common.Hash, address common.Address, topicIndex int, topicValueMin common.Hash, confs int, qopts ...pg.QOpt) ([]Log, error)
IndexedLogsTopicRange(eventSig common.Hash, address common.Address, topicIndex int, topicValueMin common.Hash, topicValueMax common.Hash, confs int, qopts ...pg.QOpt) ([]Log, error)
IndexedLogsWithSigsExcluding(address common.Address, eventSigA, eventSigB common.Hash, topicIndex int, fromBlock, toBlock int64, confs int, qopts ...pg.QOpt) ([]Log, error)
Expand Down Expand Up @@ -961,6 +962,10 @@ func (lp *logPoller) IndexedLogsCreatedAfter(eventSig common.Hash, address commo
return lp.orm.SelectIndexedLogsCreatedAfter(address, eventSig, topicIndex, topicValues, after, confs, qopts...)
}

func (lp *logPoller) IndexedLogsByTxHash(eventSig common.Hash, txHash common.Hash, qopts ...pg.QOpt) ([]Log, error) {
return lp.orm.SelectIndexedLogsByTxHash(eventSig, txHash, qopts...)
}

// LogsDataWordGreaterThan note index is 0 based.
func (lp *logPoller) LogsDataWordGreaterThan(eventSig common.Hash, address common.Address, wordIndex int, wordValueMin common.Hash, confs int, qopts ...pg.QOpt) ([]Log, error) {
return lp.orm.SelectDataWordGreaterThan(address, eventSig, wordIndex, wordValueMin, confs, qopts...)
Expand Down
33 changes: 33 additions & 0 deletions core/chains/evm/logpoller/mocks/log_poller.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions core/chains/evm/logpoller/observability.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,12 @@ func (o *ObservedLogPoller) IndexedLogsCreatedAfter(eventSig common.Hash, addres
})
}

func (o *ObservedLogPoller) IndexedLogsByTxHash(eventSig common.Hash, txHash common.Hash, qopts ...pg.QOpt) ([]Log, error) {
return withObservedQueryAndResults(o, "IndexedLogsByTxHash", func() ([]Log, error) {
return o.LogPoller.IndexedLogsByTxHash(eventSig, txHash, qopts...)
})
}

func (o *ObservedLogPoller) IndexedLogsTopicGreaterThan(eventSig common.Hash, address common.Address, topicIndex int, topicValueMin common.Hash, confs int, qopts ...pg.QOpt) ([]Log, error) {
return withObservedQueryAndResults(o, "IndexedLogsTopicGreaterThan", func() ([]Log, error) {
return o.LogPoller.IndexedLogsTopicGreaterThan(eventSig, address, topicIndex, topicValueMin, confs, qopts...)
Expand Down
16 changes: 16 additions & 0 deletions core/chains/evm/logpoller/orm.go
Original file line number Diff line number Diff line change
Expand Up @@ -516,6 +516,22 @@ func (o *ORM) SelectIndexedLogsCreatedAfter(address common.Address, eventSig com
return logs, nil
}

func (o *ORM) SelectIndexedLogsByTxHash(eventSig common.Hash, txHash common.Hash, qopts ...pg.QOpt) ([]Log, error) {
q := o.q.WithOpts(qopts...)
var logs []Log
err := q.Select(&logs, `
SELECT * FROM evm.logs
WHERE evm.logs.evm_chain_id = $1
AND tx_hash = $2
AND event_sig = $3
ORDER BY (evm.logs.block_number, evm.logs.log_index)`,
utils.NewBig(o.chainID), txHash.Bytes(), eventSig.Bytes())
if err != nil {
return nil, err
}
return logs, nil
}

// SelectIndexedLogsWithSigsExcluding query's for logs that have signature A and exclude logs that have a corresponding signature B, matching is done based on the topic index both logs should be inside the block range and have the minimum number of confirmations
func (o *ORM) SelectIndexedLogsWithSigsExcluding(sigA, sigB common.Hash, topicIndex int, address common.Address, startBlock, endBlock int64, confs int, qopts ...pg.QOpt) ([]Log, error) {
if err := validateTopicIndex(topicIndex); err != nil {
Expand Down
66 changes: 66 additions & 0 deletions core/chains/evm/logpoller/orm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -502,6 +502,72 @@ func TestORM_IndexedLogs(t *testing.T) {
assert.Equal(t, 1, len(lgs))
}

func TestORM_SelectIndexedLogsByTxHash(t *testing.T) {
th := SetupTH(t, 0, 3, 2)
o1 := th.ORM
eventSig := common.HexToHash("0x1599")
txHash := common.HexToHash("0x1888")
addr := common.HexToAddress("0x1234")

require.NoError(t, o1.InsertBlock(common.HexToHash("0x1"), 1, time.Now()))
logs := []logpoller.Log{
{
EvmChainId: utils.NewBig(th.ChainID),
LogIndex: int64(0),
BlockHash: common.HexToHash("0x1"),
BlockNumber: int64(1),
EventSig: eventSig,
Topics: [][]byte{eventSig[:]},
Address: addr,
TxHash: txHash,
Data: logpoller.EvmWord(1).Bytes(),
},
{
EvmChainId: utils.NewBig(th.ChainID),
LogIndex: int64(1),
BlockHash: common.HexToHash("0x1"),
BlockNumber: int64(1),
EventSig: eventSig,
Topics: [][]byte{eventSig[:]},
Address: addr,
TxHash: txHash,
Data: append(logpoller.EvmWord(2).Bytes(), logpoller.EvmWord(3).Bytes()...),
},
// Different txHash
{
EvmChainId: utils.NewBig(th.ChainID),
LogIndex: int64(2),
BlockHash: common.HexToHash("0x1"),
BlockNumber: int64(1),
EventSig: eventSig,
Topics: [][]byte{eventSig[:]},
Address: addr,
TxHash: common.HexToHash("0x1889"),
Data: append(logpoller.EvmWord(2).Bytes(), logpoller.EvmWord(3).Bytes()...),
},
// Different eventSig
{
EvmChainId: utils.NewBig(th.ChainID),
LogIndex: int64(3),
BlockHash: common.HexToHash("0x1"),
BlockNumber: int64(1),
EventSig: common.HexToHash("0x1600"),
Topics: [][]byte{eventSig[:]},
Address: addr,
TxHash: txHash,
Data: append(logpoller.EvmWord(2).Bytes(), logpoller.EvmWord(3).Bytes()...),
},
}
require.NoError(t, o1.InsertLogs(logs))

retrievedLogs, err := o1.SelectIndexedLogsByTxHash(eventSig, txHash)
require.NoError(t, err)

require.Equal(t, 2, len(retrievedLogs))
require.Equal(t, retrievedLogs[0].LogIndex, logs[0].LogIndex)
require.Equal(t, retrievedLogs[1].LogIndex, logs[1].LogIndex)
}

func TestORM_DataWords(t *testing.T) {
th := SetupTH(t, 2, 3, 2)
o1 := th.ORM
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
-- +goose Up
create index evm_logs_idx_tx_hash on evm.logs (tx_hash);

-- +goose Down
DROP INDEX IF EXISTS evm_logs_idx_tx_hash;

0 comments on commit c1dee6c

Please sign in to comment.