Skip to content

Commit

Permalink
consensus: store finalized roothash messages
Browse files Browse the repository at this point in the history
  • Loading branch information
pro-wh committed Jan 10, 2024
1 parent c837776 commit 9fe9dd5
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 0 deletions.
62 changes: 62 additions & 0 deletions analyzer/consensus/consensus.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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 {
Expand All @@ -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,
)
}
}

Expand Down
12 changes: 12 additions & 0 deletions analyzer/queries/queries.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)`
Expand Down

0 comments on commit 9fe9dd5

Please sign in to comment.