Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
ptrus committed Jan 25, 2025
1 parent 564b0a1 commit abee4ae
Show file tree
Hide file tree
Showing 15 changed files with 181 additions and 174 deletions.
22 changes: 7 additions & 15 deletions analyzer/evmabibackfill/evm_abi_backfill.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,9 @@ type abiEncodedTx struct {

type abiEncodedEvent struct {
// Event primary key.
Runtime common.Runtime
Round uint64
TxIndex *int
Type string
TypeIndex int
Runtime common.Runtime
Round uint64
EventIndex int

EventBody sdkEVM.Event
}
Expand Down Expand Up @@ -129,9 +127,7 @@ func (p *processor) GetItems(ctx context.Context, limit uint64) ([]*abiEncodedIt
&item.Abi,
&ev.Runtime,
&ev.Round,
&ev.TxIndex,
&ev.Type,
&ev.TypeIndex,
&ev.EventIndex,
&ev.EventBody,
); err != nil {
return nil, fmt.Errorf("scanning verified contract event: %w", err)
Expand Down Expand Up @@ -246,9 +242,7 @@ func (p *processor) ProcessItem(ctx context.Context, batch *storage.QueryBatch,
queries.RuntimeEventEvmParsedFieldsUpdate,
item.Event.Runtime,
item.Event.Round,
item.Event.TxIndex,
item.Event.Type,
item.Event.TypeIndex,
item.Event.EventIndex,
nil,
nil,
nil,
Expand All @@ -271,16 +265,14 @@ func (p *processor) ProcessItem(ctx context.Context, batch *storage.QueryBatch,
if item.Event != nil {
eventName, eventArgs, eventSig, err := p.parseEvent(item.Event, contractAbi)
if err != nil {
p.logger.Warn("error parsing event with abi", "err", err, "contract_address", item.ContractAddr, "event_round", item.Event.Round, "event_tx_index", item.Event.TxIndex)
p.logger.Warn("error parsing event with abi", "err", err, "contract_address", item.ContractAddr, "event_round", item.Event.Round, "event_index", item.Event.EventIndex)
// Write to the DB regardless of error so we don't keep retrying the same item.
}
batch.Queue(
queries.RuntimeEventEvmParsedFieldsUpdate,
item.Event.Runtime,
item.Event.Round,
item.Event.TxIndex,
item.Event.Type,
item.Event.TypeIndex,
item.Event.EventIndex,
eventName,
eventArgs,
eventSig,
Expand Down
1 change: 0 additions & 1 deletion analyzer/evmabibackfill/evm_abi_backfill_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ func unmarshalEvmEvent(t *testing.T, body []byte) *abiEncodedEvent {
require.Nil(t, err)
return &abiEncodedEvent{
Round: 0,
TxIndex: nil,
EventBody: ev,
}
}
Expand Down
18 changes: 7 additions & 11 deletions analyzer/queries/queries.go
Original file line number Diff line number Diff line change
Expand Up @@ -636,27 +636,25 @@ var (
tx_hash = $2`

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

RuntimeEventRelatedAccountsInsert = `
INSERT INTO chain.runtime_events_related_accounts (runtime, round, tx_index, type, type_index, account_address)
INSERT INTO chain.runtime_events_related_accounts (runtime, round, event_index, tx_index, type, account_address)
SELECT $1, $2, $3, $4, $5, unnest($6::text[])`

// We use COALESCE here to avoid overwriting existing data with null values.
RuntimeEventEvmParsedFieldsUpdate = `
UPDATE chain.runtime_events
SET
evm_log_name = COALESCE($6, evm_log_name),
evm_log_params = COALESCE($7, evm_log_params),
evm_log_signature = COALESCE($8, evm_log_signature),
evm_log_name = COALESCE($4, evm_log_name),
evm_log_params = COALESCE($5, evm_log_params),
evm_log_signature = COALESCE($6, evm_log_signature),
abi_parsed_at = CURRENT_TIMESTAMP
WHERE
runtime = $1 AND
round = $2 AND
tx_index = $3 AND
type = $4 AND
type_index = $5`
event_index = $3`

RuntimeMintInsert = `
INSERT INTO chain.runtime_transfers (runtime, round, sender, receiver, symbol, amount)
Expand Down Expand Up @@ -1141,9 +1139,7 @@ var (
abi_contracts.abi,
evs.runtime,
evs.round,
evs.tx_index,
evs.type,
evs.type_index,
evs.event_index,
evs.body
FROM abi_contracts
JOIN chain.address_preimages as preimages ON
Expand Down
59 changes: 35 additions & 24 deletions analyzer/runtime/extract.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,8 @@ type EventData struct {
TxIndex *int // nil for non-tx events
TxHash *string // nil for non-tx events
TxEthHash *string // nil for non-evm-tx events
EventIdx int // Unique event index within the block.
Type apiTypes.RuntimeEventType
TypeIndex int // Events of the same type within the block are ordered by index.
Body EventBody
WithScope ScopedSdkEvent
EvmLogName *string
Expand Down Expand Up @@ -237,18 +237,16 @@ func ExtractRound(blockHeader nodeapi.RuntimeBlockHeader, txrs []nodeapi.Runtime
return nil, fmt.Errorf("extract non-tx events: %w", err)
}
for _, event := range extractedEvents {
if event.TxIndex == nil {
if event.TxHash == nil {
blockData.EventData = append(blockData.EventData, event)
}
}

// Extract info from transactions.
for txIndex, txr := range txrs {
txr := txr // For safe usage of `&txr` inside this long loop.
var blockTransactionData BlockTransactionData
blockTransactionData.Index = txIndex
txHash := txr.Tx.Hash()
blockTransactionData.Hash = txHash.Hex()
blockTransactionData.Hash = txr.Tx.Hash().Hex()
if len(txr.Tx.AuthProofs) == 1 && txr.Tx.AuthProofs[0].Module == "evm.ethereum.v0" {
ethHash := hex.EncodeToString(eth.Keccak256(txr.Tx.Body))
blockTransactionData.EthHash = &ethHash
Expand Down Expand Up @@ -558,7 +556,7 @@ func ExtractRound(blockHeader nodeapi.RuntimeBlockHeader, txrs []nodeapi.Runtime
// Find extracted events for this tx.
var extractedTxEvents []*EventData
for _, event := range extractedEvents {
if event.TxIndex != nil && *event.TxIndex == txIndex {
if event.TxHash != nil && *event.TxHash == txr.Tx.Hash().String() {
extractedTxEvents = append(extractedTxEvents, event)
// Register related addresses found in the event for the transaction as well.
for addr := range event.RelatedAddresses {
Expand Down Expand Up @@ -706,19 +704,20 @@ func tryParseErrorMessage(errorModule string, errorCode uint32, msg string) *str
func extractEvents(blockData *BlockData, eventsRaw []nodeapi.RuntimeEvent) ([]*EventData, error) { //nolint:gocyclo
extractedEvents := []*EventData{}
if err := VisitSdkEvents(eventsRaw, &SdkEventHandler{
Core: func(event *core.Event, eventIdx int) error {
Core: func(event *core.Event, eventTxHash *string, eventIdx int) error {
if event.GasUsed != nil {
eventData := EventData{
EventIdx: eventIdx,
TxHash: eventTxHash,
Type: apiTypes.RuntimeEventTypeCoreGasUsed,
TypeIndex: eventIdx,
Body: event.GasUsed,
WithScope: ScopedSdkEvent{Core: event},
}
extractedEvents = append(extractedEvents, &eventData)
}
return nil
},
Accounts: func(event *accounts.Event, eventIdx int) error {
Accounts: func(event *accounts.Event, eventTxHash *string, eventIdx int) error {
if event.Transfer != nil {
fromAddr, err1 := addresses.FromSdkAddress(&event.Transfer.From)
if err1 != nil {
Expand All @@ -729,8 +728,9 @@ func extractEvents(blockData *BlockData, eventsRaw []nodeapi.RuntimeEvent) ([]*E
return fmt.Errorf("to: %w", err1)
}
eventData := EventData{
EventIdx: eventIdx,
TxHash: eventTxHash,
Type: apiTypes.RuntimeEventTypeAccountsTransfer,
TypeIndex: eventIdx,
Body: event.Transfer,
WithScope: ScopedSdkEvent{Accounts: event},
RelatedAddresses: map[apiTypes.Address]struct{}{fromAddr: {}, toAddr: {}},
Expand All @@ -743,8 +743,9 @@ func extractEvents(blockData *BlockData, eventsRaw []nodeapi.RuntimeEvent) ([]*E
return fmt.Errorf("owner: %w", err1)
}
eventData := EventData{
EventIdx: eventIdx,
TxHash: eventTxHash,
Type: apiTypes.RuntimeEventTypeAccountsBurn,
TypeIndex: eventIdx,
Body: event.Burn,
WithScope: ScopedSdkEvent{Accounts: event},
RelatedAddresses: map[apiTypes.Address]struct{}{ownerAddr: {}},
Expand All @@ -757,8 +758,9 @@ func extractEvents(blockData *BlockData, eventsRaw []nodeapi.RuntimeEvent) ([]*E
return fmt.Errorf("owner: %w", err1)
}
eventData := EventData{
EventIdx: eventIdx,
TxHash: eventTxHash,
Type: apiTypes.RuntimeEventTypeAccountsMint,
TypeIndex: eventIdx,
Body: event.Mint,
WithScope: ScopedSdkEvent{Accounts: event},
RelatedAddresses: map[apiTypes.Address]struct{}{ownerAddr: {}},
Expand All @@ -767,7 +769,7 @@ func extractEvents(blockData *BlockData, eventsRaw []nodeapi.RuntimeEvent) ([]*E
}
return nil
},
ConsensusAccounts: func(event *consensusaccounts.Event, eventIndex int) error {
ConsensusAccounts: func(event *consensusaccounts.Event, eventTxHash *string, eventIdx int) error {
if event.Deposit != nil {
// NOTE: .From is a _consensus_ addr (not runtime). It's still related though.
fromAddr, err1 := addresses.FromSdkAddress(&event.Deposit.From)
Expand All @@ -779,8 +781,9 @@ func extractEvents(blockData *BlockData, eventsRaw []nodeapi.RuntimeEvent) ([]*E
return fmt.Errorf("to: %w", err1)
}
eventData := EventData{
EventIdx: eventIdx,
TxHash: eventTxHash,
Type: apiTypes.RuntimeEventTypeConsensusAccountsDeposit,
TypeIndex: eventIndex,
Body: event.Deposit,
WithScope: ScopedSdkEvent{ConsensusAccounts: event},
RelatedAddresses: map[apiTypes.Address]struct{}{fromAddr: {}, toAddr: {}},
Expand All @@ -798,8 +801,9 @@ func extractEvents(blockData *BlockData, eventsRaw []nodeapi.RuntimeEvent) ([]*E
return fmt.Errorf("to: %w", err1)
}
eventData := EventData{
EventIdx: eventIdx,
TxHash: eventTxHash,
Type: apiTypes.RuntimeEventTypeConsensusAccountsWithdraw,
TypeIndex: eventIndex,
Body: event.Withdraw,
WithScope: ScopedSdkEvent{ConsensusAccounts: event},
RelatedAddresses: map[apiTypes.Address]struct{}{fromAddr: {}, toAddr: {}},
Expand All @@ -818,8 +822,9 @@ func extractEvents(blockData *BlockData, eventsRaw []nodeapi.RuntimeEvent) ([]*E
return fmt.Errorf("to: %w", err1)
}
eventData := EventData{
EventIdx: eventIdx,
TxHash: eventTxHash,
Type: apiTypes.RuntimeEventTypeConsensusAccountsDelegate,
TypeIndex: eventIndex,
Body: event.Delegate,
WithScope: ScopedSdkEvent{ConsensusAccounts: event},
RelatedAddresses: map[apiTypes.Address]struct{}{fromAddr: {}, toAddr: {}},
Expand All @@ -836,8 +841,9 @@ func extractEvents(blockData *BlockData, eventsRaw []nodeapi.RuntimeEvent) ([]*E
return fmt.Errorf("to: %w", err1)
}
eventData := EventData{
EventIdx: eventIdx,
TxHash: eventTxHash,
Type: apiTypes.RuntimeEventTypeConsensusAccountsUndelegateStart,
TypeIndex: eventIndex,
Body: event.UndelegateStart,
WithScope: ScopedSdkEvent{ConsensusAccounts: event},
RelatedAddresses: map[apiTypes.Address]struct{}{fromAddr: {}, toAddr: {}},
Expand All @@ -855,8 +861,9 @@ func extractEvents(blockData *BlockData, eventsRaw []nodeapi.RuntimeEvent) ([]*E
return fmt.Errorf("to: %w", err1)
}
eventData := EventData{
EventIdx: eventIdx,
TxHash: eventTxHash,
Type: apiTypes.RuntimeEventTypeConsensusAccountsUndelegateDone,
TypeIndex: eventIndex,
Body: event.UndelegateDone,
WithScope: ScopedSdkEvent{ConsensusAccounts: event},
RelatedAddresses: map[apiTypes.Address]struct{}{fromAddr: {}, toAddr: {}},
Expand All @@ -865,14 +872,15 @@ func extractEvents(blockData *BlockData, eventsRaw []nodeapi.RuntimeEvent) ([]*E
}
return nil
},
EVM: func(event *sdkEVM.Event, eventIndex int) error {
EVM: func(event *sdkEVM.Event, eventTxHash *string, eventIdx int) error {
eventAddr, err1 := addresses.RegisterEthAddress(blockData.AddressPreimages, event.Address)
if err1 != nil {
return fmt.Errorf("event address: %w", err1)
}
eventData := EventData{
EventIdx: eventIdx,
TxHash: eventTxHash,
Type: apiTypes.RuntimeEventTypeEvmLog,
TypeIndex: eventIndex,
Body: event,
WithScope: ScopedSdkEvent{EVM: event},
RelatedAddresses: map[apiTypes.Address]struct{}{eventAddr: {}},
Expand Down Expand Up @@ -1411,29 +1419,32 @@ func extractEvents(blockData *BlockData, eventsRaw []nodeapi.RuntimeEvent) ([]*E
extractedEvents = append(extractedEvents, &eventData)
return nil
},
Rofl: func(event *rofl.Event, eventIndex int) error {
Rofl: func(event *rofl.Event, eventTxHash *string, eventIdx int) error {
if event.AppCreated != nil {
eventData := EventData{
EventIdx: eventIdx,
TxHash: eventTxHash,
Type: apiTypes.RuntimeEventTypeRoflAppCreated,
TypeIndex: eventIndex,
Body: event.AppCreated,
WithScope: ScopedSdkEvent{Rofl: event},
}
extractedEvents = append(extractedEvents, &eventData)
}
if event.AppRemoved != nil {
eventData := EventData{
EventIdx: eventIdx,
TxHash: eventTxHash,
Type: apiTypes.RuntimeEventTypeRoflAppRemoved,
TypeIndex: eventIndex,
Body: event.AppRemoved,
WithScope: ScopedSdkEvent{Rofl: event},
}
extractedEvents = append(extractedEvents, &eventData)
}
if event.AppUpdated != nil {
eventData := EventData{
EventIdx: eventIdx,
TxHash: eventTxHash,
Type: apiTypes.RuntimeEventTypeRoflAppUpdated,
TypeIndex: eventIndex,
Body: event.AppUpdated,
WithScope: ScopedSdkEvent{Rofl: event},
}
Expand Down
14 changes: 5 additions & 9 deletions analyzer/runtime/runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -406,20 +406,16 @@ func (m *processor) queueDbUpdates(batch *storage.QueryBatch, data *BlockData) {

// Insert events.
for _, eventData := range data.EventData {
txIdx := -1
if eventData.TxIndex != nil {
txIdx = *eventData.TxIndex
}
batch.Queue(
queries.RuntimeEventInsert,
m.runtime,
data.Header.Round,
txIdx,
eventData.Type,
eventData.TypeIndex,
eventData.EventIdx,
eventData.TxIndex,
eventData.TxHash,
eventData.TxEthHash,
data.Header.Timestamp,
eventData.Type,
eventData.Body,
eventData.EvmLogName,
eventData.EvmLogParams,
Expand All @@ -430,9 +426,9 @@ func (m *processor) queueDbUpdates(batch *storage.QueryBatch, data *BlockData) {
queries.RuntimeEventRelatedAccountsInsert,
m.runtime,
data.Header.Round,
txIdx,
eventData.EventIdx,
eventData.TxIndex,
eventData.Type,
eventData.TypeIndex,
addresses.SliceFromSet(eventData.RelatedAddresses),
)
}
Expand Down
4 changes: 2 additions & 2 deletions analyzer/runtime/runtime_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,9 @@ func (mock *mockNode) GetEventsRaw(ctx context.Context, round uint64) ([]nodeapi

// Include events that were part of transactions.
txrs := mock.Txs[round]
for i, tx := range txrs {
for _, tx := range txrs {
for _, ev := range tx.Events {
events = append(events, nodeapi.RuntimeEvent{Event: ev, Index: uint64(i)})
events = append(events, nodeapi.RuntimeEvent(*ev))
}
}

Expand Down
Loading

0 comments on commit abee4ae

Please sign in to comment.