diff --git a/analyzer/consensus/consensus.go b/analyzer/consensus/consensus.go index 0b8b3c1bb..fd119b6eb 100644 --- a/analyzer/consensus/consensus.go +++ b/analyzer/consensus/consensus.go @@ -10,6 +10,7 @@ import ( "os" "strings" + coreCommon "github.com/oasisprotocol/oasis-core/go/common" "github.com/oasisprotocol/oasis-core/go/common/cbor" "github.com/oasisprotocol/oasis-core/go/common/crypto/signature" sdkConfig "github.com/oasisprotocol/oasis-sdk/client-sdk/go/config" @@ -624,8 +625,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 { @@ -644,6 +651,61 @@ func (m *processor) queueRootHashMessageUpserts(batch *storage.QueryBatch, data messageJSON, ) } + 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, + data.Height, + 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 { + var resultAny any + if err := cbor.Unmarshal(message.Result, &resultAny); err != nil { + m.logger.Info("roothash message result cbor unmarshal error", + "runtime", runtime, + "round", round, + "index", message.Index, + "err", err, + ) + } + resultJSON, err := json.Marshal(resultAny) + if err != nil { + m.logger.Info("roothash message result json marshal error", + "runtime", runtime, + "round", round, + "index", message.Index, + "err", err, + ) + } + batch.Queue(queries.ConsensusRoothashMessageFinalizeUpsert, + runtime, + round, + message.Index, + data.Height, + message.Module, + message.Code, + resultJSON, + ) } } diff --git a/analyzer/queries/queries.go b/analyzer/queries/queries.go index 2b93f93a2..250a4a850 100644 --- a/analyzer/queries/queries.go +++ b/analyzer/queries/queries.go @@ -219,6 +219,18 @@ var ( schedule_height = excluded.schedule_height, message = excluded.message` + ConsensusRoothashMessageFinalizeUpsert = ` + INSERT INTO chain.roothash_messages + (runtime, round, message_index, finalize_height, error_module, error_code, result) + VALUES + ($1, $2, $3, $4, $5, $6, $7) + ON CONFLICT (runtime, round, message_index) DO UPDATE + SET + finalize_height = excluded.finalize_height, + 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)`