Skip to content

Commit

Permalink
[db] add timestamp to runtime events table
Browse files Browse the repository at this point in the history
[openapi] add timestamp and evm_log_details to /runtime/events

[analyzer] store timestamp of events

[api] return token info in evm_log_details

[api] only return token details for Transfer events

nit

api typo; add storage migration

rename migration

nit

fix bug that left out core.gas_used runtime events

update e2e tests

nit: handle evm token types

address comments

nit: make unknown token_type null for events

rename evm_event_details->evm_token
  • Loading branch information
Andrew7234 committed Jul 31, 2023
1 parent 7530747 commit b661cdc
Show file tree
Hide file tree
Showing 8 changed files with 195 additions and 19 deletions.
4 changes: 2 additions & 2 deletions analyzer/queries/queries.go
Original file line number Diff line number Diff line change
Expand Up @@ -342,8 +342,8 @@ var (
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15, $16, $17, $18, $19, $20, $21, $22, $23, $24)`

RuntimeEventInsert = `
INSERT INTO chain.runtime_events (runtime, round, tx_index, tx_hash, tx_eth_hash, type, body, related_accounts, evm_log_name, evm_log_params, evm_log_signature)
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11)`
INSERT INTO chain.runtime_events (runtime, round, tx_index, tx_hash, tx_eth_hash, timestamp, type, body, related_accounts, evm_log_name, evm_log_params, evm_log_signature)
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12)`

RuntimeMintInsert = `
INSERT INTO chain.runtime_transfers (runtime, round, sender, receiver, symbol, amount)
Expand Down
1 change: 1 addition & 0 deletions analyzer/runtime/runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,7 @@ func (m *processor) queueDbUpdates(batch *storage.QueryBatch, data *BlockData) {
eventData.TxIndex,
eventData.TxHash,
eventData.TxEthHash,
data.Header.Timestamp,
eventData.Type,
eventData.Body,
eventRelatedAddresses,
Expand Down
31 changes: 29 additions & 2 deletions api/spec/v1.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1345,7 +1345,7 @@ components:
type: string
format: date-time
description: |
The second-granular consensus time this tx's block, i.e. roughly when the
The second-granular consensus time of this tx's block, i.e. roughly when the
[block was proposed](https://github.com/tendermint/tendermint/blob/v0.34.x/spec/core/data_structures.md#header).
example: *iso_timestamp_3
hash:
Expand Down Expand Up @@ -2060,7 +2060,7 @@ components:
RuntimeEvent:
type: object
required: [round, type, body]
required: [round, timestamp, type, body]
properties:
round:
type: integer
Expand All @@ -2085,6 +2085,12 @@ components:
description: |
Ethereum trasnsaction hash of this event's originating transaction.
Absent if the event did not originate from an EVM transaction.
timestamp:
type: string
format: date-time
description: |
The second-granular consensus time of this event's block.
example: *iso_timestamp_1
type:
description: The type of the event.
$ref: '#/components/schemas/RuntimeEventType'
Expand Down Expand Up @@ -2112,6 +2118,8 @@ components:
The decoded `evm.log` event data. We currently support only two types of evm events, ERC20 `Transfer`
and `Approve`.
Absent if the event type is not `evm.log`.
evm_token:
$ref: '#/components/schemas/EvmEventToken'
description: An event emitted by the runtime layer

RuntimeEventType:
Expand Down Expand Up @@ -2145,6 +2153,25 @@ components:
Values of EVM type `bytes` and `bytes<N>` are represented as base64 strings.
Values of other EVM types (integer types, strings, arrays, etc.) are represented as their JSON counterpart.
EvmEventToken:
type: object
properties:
type:
$ref: '#/components/schemas/EvmTokenType'
symbol:
type: string
description: Symbol of the token, as provided by token contract's `symbol()` method.
example: USDT
decimals:
type: integer
description: |
The number of least significant digits in base units that should be displayed as
decimals when displaying tokens. `tokens = base_units / (10**decimals)`.
Affects display only. Often equals 18, to match ETH.
example: 18
description: |
Details about the EVM token involved in the event, if any.
RuntimeEvmContract:
type: object
properties:
Expand Down
8 changes: 8 additions & 0 deletions storage/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -1296,18 +1296,26 @@ func (c *StorageClient) RuntimeEvents(ctx context.Context, p apiTypes.GetRuntime
}
for res.rows.Next() {
var e RuntimeEvent
var et apiTypes.EvmEventToken
if err := res.rows.Scan(
&e.Round,
&e.TxIndex,
&e.TxHash,
&e.EthTxHash,
&e.Timestamp,
&e.Type,
&e.Body,
&e.EvmLogName,
&e.EvmLogParams,
&et.Symbol,
&et.Type,
&et.Decimals,
); err != nil {
return nil, wrapError(err)
}
if et != (apiTypes.EvmEventToken{}) {
e.EvmToken = &et
}
es.Events = append(es.Events, e)
}

Expand Down
51 changes: 36 additions & 15 deletions storage/client/queries/queries.go
Original file line number Diff line number Diff line change
Expand Up @@ -394,18 +394,40 @@ const (
`

RuntimeEvents = `
SELECT evs.round, evs.tx_index, evs.tx_hash, evs.tx_eth_hash, evs.type, evs.body, evs.evm_log_name, evs.evm_log_params
FROM chain.runtime_events as evs
WHERE (evs.runtime = $1) AND
($2::bigint IS NULL OR evs.round = $2::bigint) AND
($3::integer IS NULL OR evs.tx_index = $3::integer) AND
($4::text IS NULL OR evs.tx_hash = $4::text OR evs.tx_eth_hash = $4::text) AND
($5::text IS NULL OR evs.type = $5::text) AND
($6::bytea IS NULL OR evs.evm_log_signature = $6::bytea) AND
($7::text IS NULL OR evs.related_accounts @> ARRAY[$7::text])
ORDER BY evs.round DESC, evs.tx_index, evs.type, evs.body::text
LIMIT $8::bigint
OFFSET $9::bigint`
SELECT
evs.round,
evs.tx_index,
evs.tx_hash,
evs.tx_eth_hash,
evs.timestamp,
evs.type,
evs.body,
evs.evm_log_name,
evs.evm_log_params,
tokens.symbol,
CASE -- NOTE: There are three queries that use this CASE via copy-paste; edit both if changing.
WHEN tokens.token_type = 20 THEN 'ERC20'
WHEN tokens.token_type = 721 THEN 'ERC721'
ELSE NULL -- Our openapi spec doesn't allow us to output this, but better this than a null value (which causes nil dereference)
END AS token_type,
tokens.decimals
FROM chain.runtime_events as evs
LEFT JOIN chain.address_preimages AS preimages ON
DECODE(evs.body ->> 'address', 'base64')=preimages.address_data
LEFT JOIN chain.evm_tokens as tokens ON
(evs.runtime=tokens.runtime) AND
(preimages.address=tokens.token_address)
WHERE
(evs.runtime = $1) AND
($2::bigint IS NULL OR evs.round = $2::bigint) AND
($3::integer IS NULL OR evs.tx_index = $3::integer) AND
($4::text IS NULL OR evs.tx_hash = $4::text OR evs.tx_eth_hash = $4::text) AND
($5::text IS NULL OR evs.type = $5::text) AND
($6::bytea IS NULL OR evs.evm_log_signature = $6::bytea) AND
($7::text IS NULL OR evs.related_accounts @> ARRAY[$7::text])
ORDER BY evs.round DESC, evs.tx_index, evs.type, evs.body::text
LIMIT $8::bigint
OFFSET $9::bigint`

RuntimeEvmContract = `
SELECT
Expand All @@ -420,7 +442,6 @@ const (
compilation_metadata,
source_files
FROM chain.evm_contracts
WHERE (runtime = $1) AND (contract_address = $2::text)`

AddressPreimage = `
Expand Down Expand Up @@ -455,7 +476,7 @@ const (
tokens.symbol,
tokens.decimals,
tokens.total_supply,
CASE -- NOTE: There are two queries that use this CASE via copy-paste; edit both if changing.
CASE -- NOTE: There are three queries that use this CASE via copy-paste; edit both if changing.
WHEN tokens.token_type = 20 THEN 'ERC20'
WHEN tokens.token_type = 721 THEN 'ERC721'
ELSE 'unexpected_other_type' -- Our openapi spec doesn't allow us to output this, but better this than a null value (which causes nil dereference)
Expand Down Expand Up @@ -506,7 +527,7 @@ const (
balances.token_address AS token_address,
tokens.symbol AS token_symbol,
tokens.token_name AS token_name,
CASE -- NOTE: There are two queries that use this CASE via copy-paste; edit both if changing.
CASE -- NOTE: There are three queries that use this CASE via copy-paste; edit both if changing.
WHEN tokens.token_type = 20 THEN 'ERC20'
WHEN tokens.token_type = 721 THEN 'ERC721'
ELSE 'unexpected_other_type' -- Our openapi spec doesn't allow us to output this, but better this than a null value (which causes nil dereference)
Expand Down
3 changes: 3 additions & 0 deletions storage/migrations/02_runtimes.up.sql
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,9 @@ CREATE TABLE chain.runtime_events

tx_hash HEX64,
tx_eth_hash HEX64,
-- Added in 08_runtime_events_timestamp.up.sql
-- timestamp TIMESTAMP WITH TIME ZONE NOT NULL,

-- TODO: add link to openapi spec section with runtime event types.
type TEXT NOT NULL,
-- The raw event, as returned by the oasis-sdk runtime client.
Expand Down
16 changes: 16 additions & 0 deletions storage/migrations/08_runtime_events_timestamp.up.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
BEGIN;

ALTER TABLE chain.runtime_events
ADD COLUMN TIMESTAMP timestamp WITH time zone;

UPDATE chain.runtime_events AS evs
SET timestamp = blocks.timestamp
FROM chain.runtime_blocks AS blocks
WHERE evs.runtime = blocks.runtime
AND evs.round = blocks.round;

ALTER TABLE chain.runtime_events
ALTER COLUMN timestamp
SET NOT NULL;

COMMIT;
Loading

0 comments on commit b661cdc

Please sign in to comment.