Skip to content

Commit

Permalink
storage/runtime: Support method filter for native transfers
Browse files Browse the repository at this point in the history
  • Loading branch information
ptrus committed Jan 20, 2025
1 parent ad43e7f commit 17c7649
Show file tree
Hide file tree
Showing 18 changed files with 10,268 additions and 2 deletions.
9 changes: 9 additions & 0 deletions .changelog/890.feature.2.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
storage/runtime: Support method filter for native transfers

Two special values for `method` filter are added:

- `method=native_transfers`: Returns "likely to be native" transactions
- All accounts.Transfer transactions and EVM Calls with empty data

- `method=evm.Call_no_native`: Returns EVM Calls which are not "likely to be
native" transfers
9 changes: 8 additions & 1 deletion api/spec/v1.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -939,7 +939,14 @@ paths:
name: method
schema:
type: string
description: A filter on the runtime transaction method.
description: |
A filter on the runtime transaction method.
In addition to the existing method names, the following special values are supported:
- 'native_transfers': Returns transactions "likely to be native transfers".
- These include accounts.Transfer transactions and evm.Calls with an empty 'body' field.
- 'evm.Call_no_native': Returns EVM calls that are "not likely to be native transfers".
example: 'accounts.Transfer'
responses:
'200':
Expand Down
17 changes: 16 additions & 1 deletion storage/client/queries/queries.go
Original file line number Diff line number Diff line change
Expand Up @@ -537,7 +537,22 @@ const (
($2::bigint IS NULL OR txs.round = $2::bigint) AND
($3::text IS NULL OR txs.tx_hash = $3::text OR txs.tx_eth_hash = $3::text) AND
($4::text IS NULL OR rel.account_address = $4::text) AND
($5::text IS NULL or txs.method = $5::text) AND
(
CASE
-- No filtering on method.
WHEN $5::text IS NULL THEN
TRUE
-- Special case to return are 'likely to be native transfers'.
WHEN $5::text = 'native_transfers' THEN
(txs.method = 'accounts.Transfer' OR (txs.method = 'evm.Call' AND (txs.body ->> 'data') = ''))
-- Special case to return all evm.Calls that are likely not native transfers.
WHEN $5::text = 'evm.Call_no_native' THEN
(txs.method = 'evm.Call' AND (txs.body ->> 'data') != '')
-- Regular case.
ELSE
(txs.method = $5::text)
END
) AND
($6::timestamptz IS NULL OR txs.timestamp >= $6::timestamptz) AND
($7::timestamptz IS NULL OR txs.timestamp < $7::timestamptz)
GROUP BY
Expand Down
7 changes: 7 additions & 0 deletions storage/migrations/01_runtimes.up.sql
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,13 @@ CREATE INDEX ix_runtime_transactions_to ON chain.runtime_transactions(runtime, "
CREATE INDEX ix_runtime_transactions_to_abi_parsed_at ON chain.runtime_transactions (runtime, "to", abi_parsed_at);
-- CREATE INDEX ix_runtime_transactions_method_round ON chain.runtime_transactions (runtime, method, round, tx_index); -- Added in 08_runtime_transactions_method_idx.up.sql

-- Added in 12_related_transactions_method_idx.up.sql.
-- Indexes for efficient query of 'likely native transfers':
-- EVM Calls, where the body is an empty data field (likely native transfers)
-- CREATE INDEX ix_runtime_transactions_evm_call_empty_data ON chain.runtime_transactions (runtime, round, tx_index) WHERE method = 'evm.Call' AND (body ->> 'data') = '';
-- EVM Calls, where the body is non-empty data field (likely not native transfers).
-- CREATE INDEX ix_runtime_transactions_evm_call_non_empty_data ON chain.runtime_transactions (runtime, round, tx_index) WHERE method = 'evm.Call' AND (body ->> 'data') != '';

CREATE TABLE chain.runtime_transaction_signers
(
runtime runtime NOT NULL,
Expand Down
6 changes: 6 additions & 0 deletions storage/migrations/12_related_transactions_method_idx.up.sql
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,10 @@ DROP INDEX IF EXISTS chain.ix_transactions_method_height;
CREATE INDEX IF NOT EXISTS ix_accounts_related_transactions_address_block_desc_tx_index ON chain.accounts_related_transactions (account_address, tx_block DESC, tx_index);
DROP INDEX IF EXISTS chain.ix_accounts_related_transactions_address_block;

-- Indexes for efficient query of 'likely native transfers':
-- EVM Calls, where the body is an empty data field (likely native transfers)
CREATE INDEX IF NOT EXISTS ix_runtime_transactions_evm_call_empty_data ON chain.runtime_transactions (runtime, round, tx_index) WHERE method = 'evm.Call' AND (body ->> 'data') = '';
-- EVM Calls, where the body is non-empty data field (likely not native transfers).
CREATE INDEX IF NOT EXISTS ix_runtime_transactions_evm_call_non_empty_data ON chain.runtime_transactions (runtime, round, tx_index) WHERE method = 'evm.Call' AND (body ->> 'data') != '';

COMMIT;
3 changes: 3 additions & 0 deletions tests/e2e_regression/common_test_cases.sh
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,9 @@ commonTestCases=(
'emerald_blocks /v1/emerald/blocks'
'emerald_txs /v1/emerald/transactions'
'emerald_txs_by_method /v1/emerald/transactions?method=consensus.Withdraw'
'emerald_txs_native_transfers /v1/emerald/transactions?method=native_transfers'
'emerald_txs_evm_call_no_native /v1/emerald/transactions?method=evm.Call_no_native'
'emerald_txs_evm_call /v1/emerald/transactions?method=evm.Call'
'emerald_events /v1/emerald/events'
'emerald_events_by_type /v1/emerald/events?type=accounts.transfer'
'emerald_tokens /v1/emerald/evm_tokens'
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"is_total_count_clipped": false,
"total_count": 0,
"transactions": []
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
HTTP/1.1 200 OK
Content-Type: application/json
Vary: Origin
Date: UNINTERESTING
Content-Length: UNINTERESTING

Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"is_total_count_clipped": false,
"total_count": 0,
"transactions": []
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
HTTP/1.1 200 OK
Content-Type: application/json
Vary: Origin
Date: UNINTERESTING
Content-Length: UNINTERESTING

Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"is_total_count_clipped": false,
"total_count": 0,
"transactions": []
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
HTTP/1.1 200 OK
Content-Type: application/json
Vary: Origin
Date: UNINTERESTING
Content-Length: UNINTERESTING

Loading

0 comments on commit 17c7649

Please sign in to comment.