Skip to content

Commit

Permalink
storage: extract roothash related runtime and round
Browse files Browse the repository at this point in the history
  • Loading branch information
pro-wh committed Dec 30, 2023
1 parent 7f9882f commit 074327e
Show file tree
Hide file tree
Showing 7 changed files with 80 additions and 10 deletions.
18 changes: 15 additions & 3 deletions analyzer/consensus/consensus.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/oasisprotocol/oasis-core/go/common/crypto/signature"
sdkConfig "github.com/oasisprotocol/oasis-sdk/client-sdk/go/config"

"github.com/oasisprotocol/nexus/common"
"github.com/oasisprotocol/nexus/coreapi/v22.2.11/consensus/api/transaction"
genesis "github.com/oasisprotocol/nexus/coreapi/v22.2.11/genesis/api"
staking "github.com/oasisprotocol/nexus/coreapi/v22.2.11/staking/api"
Expand All @@ -37,9 +38,11 @@ const (
type EventType = apiTypes.ConsensusEventType // alias for brevity

type parsedEvent struct {
ty EventType
rawBody json.RawMessage
relatedAddresses []staking.Address
ty EventType
rawBody json.RawMessage
relatedAddresses []staking.Address
relatedRuntime *common.Runtime
relatedRuntimeRound *uint64
}

// OpenSignedTxNoVerify decodes the Transaction inside a Signed transaction
Expand Down Expand Up @@ -479,6 +482,8 @@ func (m *processor) queueTxEventInserts(batch *storage.QueryBatch, data *consens
txr.Transaction.Hash().Hex(),
i,
accounts,
eventData.relatedRuntime,
eventData.relatedRuntimeRound,
)
}
uniqueTxAccounts := extractUniqueAddresses(txAccounts)
Expand Down Expand Up @@ -1035,6 +1040,8 @@ func (m *processor) queueSingleEventInserts(batch *storage.QueryBatch, eventData
nil,
nil,
accounts,
eventData.relatedRuntime,
eventData.relatedRuntimeRound,
)

return nil
Expand Down Expand Up @@ -1068,7 +1075,12 @@ func (m *processor) extractEventData(event nodeapi.Event) parsedEvent {
eventData.relatedAddresses = []staking.Address{event.GovernanceProposalSubmitted.Submitter}
case event.GovernanceVote != nil:
eventData.relatedAddresses = []staking.Address{event.GovernanceVote.Submitter}
case event.RoothashMisc != nil:
eventData.relatedRuntime = RuntimeFromId(event.RoothashMisc.RuntimeID, m.network)
eventData.relatedRuntimeRound = event.RoothashMisc.Round
case event.RoothashExecutorCommitted != nil && event.RoothashExecutorCommitted.NodeID != nil:
eventData.relatedRuntime = RuntimeFromId(event.RoothashExecutorCommitted.RuntimeID, m.network)
eventData.relatedRuntimeRound = event.RoothashExecutorCommitted.Round
nodeAddr := staking.NewAddress(*event.RoothashExecutorCommitted.NodeID)
eventData.relatedAddresses = []staking.Address{nodeAddr}
case event.RegistryEntity != nil:
Expand Down
4 changes: 2 additions & 2 deletions analyzer/queries/queries.go
Original file line number Diff line number Diff line change
Expand Up @@ -206,8 +206,8 @@ var (
schedule = excluded.schedule`

ConsensusEventInsert = `
INSERT INTO chain.events (type, body, tx_block, tx_hash, tx_index, related_accounts)
VALUES ($1, $2, $3, $4, $5, $6)`
INSERT INTO chain.events (type, body, tx_block, tx_hash, tx_index, related_accounts, related_runtime, related_runtime_round)
VALUES ($1, $2, $3, $4, $5, $6, $7, $8)`

ConsensusAccountRelatedTransactionInsert = `
INSERT INTO chain.accounts_related_transactions (account_address, tx_block, tx_index)
Expand Down
10 changes: 9 additions & 1 deletion storage/oasis/nodeapi/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ type Event struct {
RegistryNode *NodeEvent
RegistryNodeUnfrozen *NodeUnfrozenEvent

RoothashMisc *RoohashEvent
RoothashExecutorCommitted *ExecutorCommittedEvent

GovernanceProposalSubmitted *ProposalSubmittedEvent
Expand Down Expand Up @@ -183,8 +184,15 @@ type (

// .................... RootHash ....................

type RoohashEvent struct {
RuntimeID coreCommon.Namespace
Round *uint64
}

type ExecutorCommittedEvent struct {
NodeID *signature.PublicKey // Available starting in Damask.
RuntimeID coreCommon.Namespace
Round *uint64
NodeID *signature.PublicKey // Available starting in Damask.
}

// .................... Governance ....................
Expand Down
23 changes: 22 additions & 1 deletion storage/oasis/nodeapi/cobalt/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package cobalt
import (
"strings"

"github.com/oasisprotocol/oasis-core/go/common/cbor"
"github.com/oasisprotocol/oasis-core/go/common/crypto/hash"
"github.com/oasisprotocol/oasis-core/go/common/crypto/signature"
"github.com/oasisprotocol/oasis-core/go/common/quantity"
Expand All @@ -28,6 +29,7 @@ import (
genesisCobalt "github.com/oasisprotocol/nexus/coreapi/v21.1.1/genesis/api"
governanceCobalt "github.com/oasisprotocol/nexus/coreapi/v21.1.1/governance/api"
registryCobalt "github.com/oasisprotocol/nexus/coreapi/v21.1.1/registry/api"
commitmentCobalt "github.com/oasisprotocol/nexus/coreapi/v21.1.1/roothash/api/commitment"
schedulerCobalt "github.com/oasisprotocol/nexus/coreapi/v21.1.1/scheduler/api"
stakingCobalt "github.com/oasisprotocol/nexus/coreapi/v21.1.1/staking/api"
)
Expand Down Expand Up @@ -318,25 +320,44 @@ func convertEvent(e txResultsCobalt.Event) nodeapi.Event {
case e.RootHash != nil:
switch {
case e.RootHash.ExecutorCommitted != nil:
var computeBody commitmentCobalt.ComputeBody
if err := cbor.Unmarshal(e.RootHash.ExecutorCommitted.Commit.Blob, &computeBody); err != nil {
logger.Error("convert event: roothash executor committed: commit error unmarshaling",
"event", e,
"err", err,
)
}
ret = nodeapi.Event{
RoothashExecutorCommitted: &nodeapi.ExecutorCommittedEvent{
NodeID: &e.RootHash.ExecutorCommitted.Commit.Signature.PublicKey,
RuntimeID: e.RootHash.RuntimeID,
Round: &computeBody.Header.Round,
NodeID: &e.RootHash.ExecutorCommitted.Commit.Signature.PublicKey,
},
RawBody: common.TryAsJSON(e.RootHash.ExecutorCommitted),
Type: apiTypes.ConsensusEventTypeRoothashExecutorCommitted,
}
case e.RootHash.ExecutionDiscrepancyDetected != nil:
ret = nodeapi.Event{
RoothashMisc: &nodeapi.RoohashEvent{
RuntimeID: e.RootHash.RuntimeID,
},
RawBody: common.TryAsJSON(e.RootHash.ExecutionDiscrepancyDetected),
Type: apiTypes.ConsensusEventTypeRoothashExecutionDiscrepancy,
}
case e.RootHash.Finalized != nil:
ret = nodeapi.Event{
RoothashMisc: &nodeapi.RoohashEvent{
RuntimeID: e.RootHash.RuntimeID,
Round: &e.RootHash.Finalized.Round,
},
RawBody: common.TryAsJSON(e.RootHash.Finalized),
Type: apiTypes.ConsensusEventTypeRoothashFinalized,
}
case e.RootHash.Message != nil:
ret = nodeapi.Event{
RoothashMisc: &nodeapi.RoohashEvent{
RuntimeID: e.RootHash.RuntimeID,
},
RawBody: common.TryAsJSON(e.RootHash.Message),
Type: apiTypes.ConsensusEventTypeRoothashMessage,
}
Expand Down
4 changes: 3 additions & 1 deletion storage/oasis/nodeapi/cobalt/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ import (
stakingCobalt "github.com/oasisprotocol/nexus/coreapi/v21.1.1/staking/api"
)

var logger = log.NewDefaultLogger("cobalt-consensus-api-lite")

// ConsensusApiLite provides low-level access to the consensus API of a
// Cobalt node. To be able to use the old gRPC API, this struct uses gRPC
// directly, skipping the convenience wrappers provided by oasis-core.
Expand Down Expand Up @@ -86,7 +88,7 @@ func (c *ConsensusApiLite) GetTransactionsWithResults(ctx context.Context, heigh
for i, txBytes := range rsp.Transactions {
var tx consensusTx.SignedTransaction
if err := cbor.Unmarshal(txBytes, &tx); err != nil {
log.NewDefaultLogger("cobalt-consensus-api-lite").Error("malformed consensus transaction, leaving empty",
logger.Error("malformed consensus transaction, leaving empty",
"height", height,
"index", i,
"tx_bytes", txBytes,
Expand Down
15 changes: 14 additions & 1 deletion storage/oasis/nodeapi/damask/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,23 +150,36 @@ func convertEvent(e txResultsDamask.Event) nodeapi.Event {
case e.RootHash.ExecutorCommitted != nil:
ret = nodeapi.Event{
RoothashExecutorCommitted: &nodeapi.ExecutorCommittedEvent{
NodeID: &e.RootHash.ExecutorCommitted.Commit.NodeID,
RuntimeID: e.RootHash.RuntimeID,
Round: &e.RootHash.ExecutorCommitted.Commit.Header.Round,
NodeID: &e.RootHash.ExecutorCommitted.Commit.NodeID,
},
RawBody: common.TryAsJSON(e.RootHash.ExecutorCommitted),
Type: apiTypes.ConsensusEventTypeRoothashExecutorCommitted,
}
case e.RootHash.ExecutionDiscrepancyDetected != nil:
ret = nodeapi.Event{
RoothashMisc: &nodeapi.RoohashEvent{
RuntimeID: e.RootHash.RuntimeID,
},
RawBody: common.TryAsJSON(e.RootHash.ExecutionDiscrepancyDetected),
Type: apiTypes.ConsensusEventTypeRoothashExecutionDiscrepancy,
}
case e.RootHash.Finalized != nil:
ret = nodeapi.Event{
RoothashMisc: &nodeapi.RoohashEvent{
RuntimeID: e.RootHash.RuntimeID,
Round: &e.RootHash.Finalized.Round,
},
RawBody: common.TryAsJSON(e.RootHash.Finalized),
Type: apiTypes.ConsensusEventTypeRoothashFinalized,
}
case e.RootHash.InMsgProcessed != nil:
ret = nodeapi.Event{
RoothashMisc: &nodeapi.RoohashEvent{
RuntimeID: e.RootHash.RuntimeID,
Round: &e.RootHash.InMsgProcessed.Round,
},
RawBody: common.TryAsJSON(e.RootHash.InMsgProcessed),
Type: apiTypes.ConsensusEventTypeRoothashInMsgProcessed,
}
Expand Down
16 changes: 15 additions & 1 deletion storage/oasis/nodeapi/eden/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -401,23 +401,37 @@ func convertEvent(e txResultsEden.Event) nodeapi.Event {
case e.RootHash.ExecutorCommitted != nil:
ret = nodeapi.Event{
RoothashExecutorCommitted: &nodeapi.ExecutorCommittedEvent{
NodeID: &e.RootHash.ExecutorCommitted.Commit.NodeID,
RuntimeID: e.RootHash.RuntimeID,
Round: &e.RootHash.ExecutorCommitted.Commit.Header.Header.Round,
NodeID: &e.RootHash.ExecutorCommitted.Commit.NodeID,
},
RawBody: common.TryAsJSON(e.RootHash.ExecutorCommitted),
Type: apiTypes.ConsensusEventTypeRoothashExecutorCommitted,
}
case e.RootHash.ExecutionDiscrepancyDetected != nil:
ret = nodeapi.Event{
RoothashMisc: &nodeapi.RoohashEvent{
RuntimeID: e.RootHash.RuntimeID,
// TODO: Round in oasis-core 23.0.3+
},
RawBody: common.TryAsJSON(e.RootHash.ExecutionDiscrepancyDetected),
Type: apiTypes.ConsensusEventTypeRoothashExecutionDiscrepancy,
}
case e.RootHash.Finalized != nil:
ret = nodeapi.Event{
RoothashMisc: &nodeapi.RoohashEvent{
RuntimeID: e.RootHash.RuntimeID,
Round: &e.RootHash.Finalized.Round,
},
RawBody: common.TryAsJSON(e.RootHash.Finalized),
Type: apiTypes.ConsensusEventTypeRoothashFinalized,
}
case e.RootHash.InMsgProcessed != nil:
ret = nodeapi.Event{
RoothashMisc: &nodeapi.RoohashEvent{
RuntimeID: e.RootHash.RuntimeID,
Round: &e.RootHash.InMsgProcessed.Round,
},
RawBody: common.TryAsJSON(e.RootHash.InMsgProcessed),
Type: apiTypes.ConsensusEventTypeRoothashFinalized,
}
Expand Down

0 comments on commit 074327e

Please sign in to comment.