Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

storage: add index for efficient fetching of events emitted by a specific contract #808

Merged
merged 1 commit into from
Dec 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .changelog/808.feature.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
storage: add index for fetching events emitted by a specific contract
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' must be used with 'contract_address'")
case p.ContractAddress != nil && p.NftId == nil && p.EvmLogSignature == nil:
return nil, fmt.Errorf("'contract_address' must be used with 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
Loading