Skip to content

Commit

Permalink
storage: split runtime events related accounts into a separate table
Browse files Browse the repository at this point in the history
  • Loading branch information
ptrus committed Dec 23, 2024
1 parent 0d27e93 commit de91652
Show file tree
Hide file tree
Showing 14 changed files with 253 additions and 191 deletions.
5 changes: 4 additions & 1 deletion analyzer/consensus/consensus.go
Original file line number Diff line number Diff line change
Expand Up @@ -797,12 +797,15 @@ func (m *processor) queueRootHashMessageUpserts(batch *storage.QueryBatch, data
// The runtime has its own staking account, which is what
// performs these actions, e.g. when sending or receiving the
// consensus token. Register that as related to the message.
if _, err := addresses.RegisterRelatedRuntimeAddress(messageData.addressPreimages, messageData.relatedAddresses, event.RoothashExecutorCommitted.RuntimeID); err != nil {
if runtimeAddr, err := addresses.RegisterRuntimeAddress(messageData.addressPreimages, event.RoothashExecutorCommitted.RuntimeID); err != nil {
logger.Info("register runtime address failed",
"runtime_id", event.RoothashExecutorCommitted.RuntimeID,
"err", err,
)
} else {
messageData.relatedAddresses[runtimeAddr] = struct{}{}
}

for addr, preimageData := range messageData.addressPreimages {
batch.Queue(queries.AddressPreimageInsert,
addr,
Expand Down
12 changes: 8 additions & 4 deletions analyzer/consensus/messages.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,14 @@ func extractMessageData(logger *log.Logger, m message.Message) MessageData {
break
}
messageData.body = body
_, err = addresses.RegisterRelatedOCSAddress(messageData.relatedAddresses, m.Staking.Transfer.To)
to, err := addresses.FromOCSAddress(m.Staking.Transfer.To)
if err != nil {
logger.Info("register related address 'to' failed",
"message_type", messageData.messageType,
"err", err,
)
}
messageData.relatedAddresses[to] = struct{}{}
case m.Staking.Withdraw != nil:
messageData.messageType = apiTypes.RoothashMessageTypeStakingWithdraw
body, err := json.Marshal(m.Staking.Withdraw)
Expand All @@ -53,13 +54,14 @@ func extractMessageData(logger *log.Logger, m message.Message) MessageData {
break
}
messageData.body = body
_, err = addresses.RegisterRelatedOCSAddress(messageData.relatedAddresses, m.Staking.Withdraw.From)
from, err := addresses.FromOCSAddress(m.Staking.Withdraw.From)
if err != nil {
logger.Info("register related address 'from' failed",
"message_type", messageData.messageType,
"err", err,
)
}
messageData.relatedAddresses[from] = struct{}{}
case m.Staking.AddEscrow != nil:
messageData.messageType = apiTypes.RoothashMessageTypeStakingAddEscrow
body, err := json.Marshal(m.Staking.AddEscrow)
Expand All @@ -71,13 +73,14 @@ func extractMessageData(logger *log.Logger, m message.Message) MessageData {
break
}
messageData.body = body
_, err = addresses.RegisterRelatedOCSAddress(messageData.relatedAddresses, m.Staking.AddEscrow.Account)
account, err := addresses.FromOCSAddress(m.Staking.AddEscrow.Account)
if err != nil {
logger.Info("register related address 'account' failed",
"message_type", messageData.messageType,
"err", err,
)
}
messageData.relatedAddresses[account] = struct{}{}
case m.Staking.ReclaimEscrow != nil:
messageData.messageType = apiTypes.RoothashMessageTypeStakingReclaimEscrow
body, err := json.Marshal(m.Staking.ReclaimEscrow)
Expand All @@ -89,13 +92,14 @@ func extractMessageData(logger *log.Logger, m message.Message) MessageData {
break
}
messageData.body = body
_, err = addresses.RegisterRelatedOCSAddress(messageData.relatedAddresses, m.Staking.ReclaimEscrow.Account)
account, err := addresses.FromOCSAddress(m.Staking.ReclaimEscrow.Account)
if err != nil {
logger.Info("register related address 'account' failed",
"message_type", messageData.messageType,
"err", err,
)
}
messageData.relatedAddresses[account] = struct{}{}
default:
logger.Info("unhandled staking message",
"staking_message", m.Staking,
Expand Down
6 changes: 5 additions & 1 deletion analyzer/queries/queries.go
Original file line number Diff line number Diff line change
Expand Up @@ -636,9 +636,13 @@ var (
tx_hash = $2`

RuntimeEventInsert = `
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)
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, event_index, related_account)
SELECT $1, $2, $3, unnest($4::text[])`

// We use COALESCE here to avoid overwriting existing data with null values.
RuntimeEventEvmParsedFieldsUpdate = `
UPDATE chain.runtime_events
Expand Down
Loading

0 comments on commit de91652

Please sign in to comment.