From b79015ca898b8a08d4806f8f9d40f3875e1edd3b Mon Sep 17 00:00:00 2001 From: Warren He Date: Fri, 5 Jan 2024 16:43:51 -0800 Subject: [PATCH] consensus: store finalized roothash messages --- analyzer/consensus/consensus.go | 41 +++++++++++++++++++++++++++++++++ analyzer/queries/queries.go | 11 +++++++++ 2 files changed, 52 insertions(+) diff --git a/analyzer/consensus/consensus.go b/analyzer/consensus/consensus.go index 9cf6eb815..d5e9f174c 100644 --- a/analyzer/consensus/consensus.go +++ b/analyzer/consensus/consensus.go @@ -627,8 +627,14 @@ func (m *processor) queueRegistryEventInserts(batch *storage.QueryBatch, data *r } func (m *processor) queueRootHashMessageUpserts(batch *storage.QueryBatch, data *rootHashData) error { + finalized := map[coreCommon.Namespace]uint64{} for _, event := range data.Events { switch { + case event.RoothashMisc != nil: + switch event.Type { + case apiTypes.ConsensusEventTypeRoothashFinalized: + finalized[event.RoothashMisc.RuntimeID] = *event.RoothashMisc.Round + } case event.RoothashExecutorCommitted != nil: runtime := RuntimeFromID(event.RoothashExecutorCommitted.RuntimeID, m.network) if runtime == nil { @@ -655,6 +661,41 @@ func (m *processor) queueRootHashMessageUpserts(batch *storage.QueryBatch, data relatedAddresses, ) } + case event.RoothashMessage != nil: + runtime := RuntimeFromID(event.RoothashMessage.RuntimeID, m.network) + if runtime == nil { + break + } + batch.Queue(queries.ConsensusRoothashMessageFinalizeUpsert, + runtime, + finalized[event.RoothashMessage.RuntimeID], + event.RoothashMessage.Index, + event.RoothashMessage.Module, + event.RoothashMessage.Code, + nil, + ) + } + } + for rtid, results := range data.LastRoundResults { + runtime := RuntimeFromID(rtid, m.network) + if runtime == nil { + // We shouldn't even have gathered last round results for unknown + // runtimes. But prevent nil-runtime inserts anyway. + continue + } + round, ok := finalized[rtid] + if !ok { + continue + } + for _, message := range results.Messages { + batch.Queue(queries.ConsensusRoothashMessageFinalizeUpsert, + runtime, + round, + message.Index, + message.Module, + message.Code, + cbor.Marshal(message.Result), + ) } } diff --git a/analyzer/queries/queries.go b/analyzer/queries/queries.go index 21e94b9cf..a9eedee88 100644 --- a/analyzer/queries/queries.go +++ b/analyzer/queries/queries.go @@ -225,6 +225,17 @@ var ( body = excluded.body, related_accounts = excluded.related_accounts` + ConsensusRoothashMessageFinalizeUpsert = ` + INSERT INTO chain.roothash_messages + (runtime, round, message_index, error_module, error_code, result) + VALUES + ($1, $2, $3, $4, $5, $6) + ON CONFLICT (runtime, round, message_index) DO UPDATE + SET + error_module = excluded.error_module, + error_code = excluded.error_code, + result = excluded.result` + ConsensusAccountRelatedTransactionInsert = ` INSERT INTO chain.accounts_related_transactions (account_address, tx_block, tx_index) VALUES ($1, $2, $3)`