Skip to content

Commit

Permalink
storage: add index for efficient fetching of events emitted by a spec…
Browse files Browse the repository at this point in the history
…ific contract
  • Loading branch information
ptrus committed Dec 1, 2024
1 parent 7170c28 commit e57a8dc
Show file tree
Hide file tree
Showing 6 changed files with 4,037 additions and 2 deletions.
12 changes: 10 additions & 2 deletions storage/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -1582,9 +1582,17 @@ func (c *StorageClient) RuntimeEvents(ctx context.Context, p apiTypes.GetRuntime
h := ethCommon.HexToHash(*p.EvmLogSignature)
evmLogSignature = &h
}
if p.NftId != nil && p.ContractAddress == nil {
return nil, fmt.Errorf("must specify contract_address with nft_id")

// Validate query parameter constraints.
// Due to DB indexes setup, other query combinations are inefficient and not supported.
switch {
case p.NftId != nil && p.ContractAddress == nil:
return nil, fmt.Errorf("'nft_id' requires the use of 'contract_address'")
case p.ContractAddress != nil && p.NftId == nil && p.EvmLogSignature == nil:
return nil, fmt.Errorf("'contract_address' requires the use of either 'nft_id' or 'evm_log_signature'")
default:
}

ocAddrContract, err := apiTypes.UnmarshalToOcAddress(p.ContractAddress)
if err != nil {
return nil, err
Expand Down
5 changes: 5 additions & 0 deletions storage/migrations/01_runtimes.up.sql
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,11 @@ CREATE INDEX ix_runtime_events_nft_transfers ON chain.runtime_events (runtime, (
type = 'evm.log' AND
evm_log_signature = '\xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef' AND
jsonb_array_length(body -> 'topics') = 4;
-- Added in 07_runtime_events_evm_contracts_events.up.sql
-- Index used for fetching events emitted by a specific contract.
-- CREATE INDEX ix_runtime_events_evm_contract_events ON chain.runtime_events (runtime, (body ->> 'address'), evm_log_signature, round)
-- WHERE
-- type = 'evm.log';

-- Roothash messages are small structures that a runtime can send to
-- communicate with the consensus layer. They are agreed upon for each runtime
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
BEGIN;

-- Index used for fetching all events of a specific type, of a specific contract (sorted by round).
-- For example listing all ERC20 Transfers of a specific token.
CREATE INDEX IF NOT EXISTS ix_runtime_events_evm_specific_contract_events ON chain.runtime_events (runtime, (body ->> 'address'), evm_log_signature, round)
WHERE
type = 'evm.log';

COMMIT;
Loading

0 comments on commit e57a8dc

Please sign in to comment.