Skip to content

Commit

Permalink
Merge pull request #498 from oasisprotocol/andrew7234/preimage-data-i…
Browse files Browse the repository at this point in the history
…ndex

add index for chain.addres_preimages.address_data
  • Loading branch information
Andrew7234 authored Aug 7, 2023
2 parents f3354c0 + 948be17 commit 983a647
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 5 deletions.
7 changes: 6 additions & 1 deletion storage/client/queries/queries.go
Original file line number Diff line number Diff line change
Expand Up @@ -412,8 +412,13 @@ const (
END AS token_type,
tokens.decimals
FROM chain.runtime_events as evs
-- Look up the oasis-style address derived from evs.body.address.
-- The derivation is just a keccak hash and we could theoretically compute it instead of looking it up,
-- but the implementing/importing the right hash function in postgres would take some work.
LEFT JOIN chain.address_preimages AS preimages ON
DECODE(evs.body ->> 'address', 'base64')=preimages.address_data
DECODE(evs.body ->> 'address', 'base64')=preimages.address_data AND
preimages.context_identifier = 'oasis-runtime-sdk/address: secp256k1eth' AND
preimages.context_version = 0
LEFT JOIN chain.evm_tokens as tokens ON
(evs.runtime=tokens.runtime) AND
(preimages.address=tokens.token_address)
Expand Down
16 changes: 12 additions & 4 deletions storage/migrations/02_runtimes.up.sql
Original file line number Diff line number Diff line change
Expand Up @@ -141,10 +141,15 @@ CREATE INDEX ix_runtime_events_evm_log_params ON chain.runtime_events USING gin(
-- we've encountered the preimage before to find out what the address was
-- derived from.
--
-- If you need to go the other way, from context + data to address, you'd just
-- run the derivation. Thus we don't provide an index for going that way. See
-- oasis-core/go/common/crypto/address/address.go for details. Consider
-- inserting the preimage here if you're ingesting new blockchain data though.
-- If you need to go the other way, from context + data to address, you'd
-- normally just run the derivation. See oasis-core/go/common/crypto/address/address.go
-- for details. Consider inserting the preimage here if you're ingesting new
-- blockchain data.
--
-- However, we do provide an index going the other way because certain queries
-- require computing the derivation within Postgres and implementing/importing
-- the right hash function will take some work.
-- TODO: import keccak hash into postgres.
--
-- Retain this across hard forks as long as the address derivation scheme is
-- compatible.
Expand All @@ -162,6 +167,9 @@ CREATE TABLE chain.address_preimages
-- Ethereum address. For a "staking" context, this is the ed25519 pubkey.
address_data BYTEA NOT NULL
);
-- Added in 10_runtime_address_preimage_idx.up.sql
-- CREATE INDEX IF NOT EXISTS ix_address_preimages_address_data ON chain.address_preimages (address_data)
-- WHERE context_identifier = 'oasis-runtime-sdk/address: secp256k1eth' AND context_version = 0;

-- -- -- -- -- -- -- -- -- -- -- -- -- Module evm -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --

Expand Down
8 changes: 8 additions & 0 deletions storage/migrations/10_runtime_address_preimage_idx.up.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
BEGIN;

-- Note: We use `IF NOT EXISTS` because the index has already been added manually in some places.
CREATE INDEX IF NOT EXISTS ix_address_preimages_address_data ON chain.address_preimages (address_data)
WHERE context_identifier = 'oasis-runtime-sdk/address: secp256k1eth' AND
context_version = 0;

COMMIT;

0 comments on commit 983a647

Please sign in to comment.