Skip to content

Commit

Permalink
move tx body mapping outside; add cobalt-specific types
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrew7234 committed Mar 8, 2024
1 parent 04d863b commit 797cd9e
Show file tree
Hide file tree
Showing 10 changed files with 70 additions and 108 deletions.
72 changes: 68 additions & 4 deletions analyzer/consensus/consensus.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,33 @@ import (
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"
"github.com/oasisprotocol/oasis-core/go/common/entity"
sdkConfig "github.com/oasisprotocol/oasis-sdk/client-sdk/go/config"

"github.com/oasisprotocol/nexus/common"
beaconCobalt "github.com/oasisprotocol/nexus/coreapi/v21.1.1/beacon/api"
governanceCobalt "github.com/oasisprotocol/nexus/coreapi/v21.1.1/governance/api"
registryCobalt "github.com/oasisprotocol/nexus/coreapi/v21.1.1/registry/api"
roothashCobalt "github.com/oasisprotocol/nexus/coreapi/v21.1.1/roothash/api"

beacon "github.com/oasisprotocol/nexus/coreapi/v22.2.11/beacon/api"
"github.com/oasisprotocol/nexus/coreapi/v22.2.11/common/node"
"github.com/oasisprotocol/nexus/coreapi/v22.2.11/consensus/api/transaction"
genesis "github.com/oasisprotocol/nexus/coreapi/v22.2.11/genesis/api"
governance "github.com/oasisprotocol/nexus/coreapi/v22.2.11/governance/api"
keymanager "github.com/oasisprotocol/nexus/coreapi/v22.2.11/keymanager/api"
registry "github.com/oasisprotocol/nexus/coreapi/v22.2.11/registry/api"
roothash "github.com/oasisprotocol/nexus/coreapi/v22.2.11/roothash/api"
staking "github.com/oasisprotocol/nexus/coreapi/v22.2.11/staking/api"

consensusEden "github.com/oasisprotocol/nexus/coreapi/v23.0/consensus/api"
keymanagerEden "github.com/oasisprotocol/nexus/coreapi/v23.0/keymanager/api"

"github.com/oasisprotocol/nexus/analyzer"
"github.com/oasisprotocol/nexus/analyzer/block"
"github.com/oasisprotocol/nexus/analyzer/queries"
"github.com/oasisprotocol/nexus/analyzer/util"
apiTypes "github.com/oasisprotocol/nexus/api/v1/types"
"github.com/oasisprotocol/nexus/common"
"github.com/oasisprotocol/nexus/config"
"github.com/oasisprotocol/nexus/log"
"github.com/oasisprotocol/nexus/metrics"
Expand All @@ -38,6 +53,46 @@ const (
consensusAnalyzerName = "consensus"
)

// The Cobalt (v21.1.1) version of oasis-core defines some transaction structs
// that are incompatible with the structs used in Damask onwards. We track
// those structs here and use them as a backup if the initial unmarshalling fails.
var bodyTypeForTxMethodCobalt = map[string]interface{}{
"governance.SubmitProposal": governanceCobalt.ProposalContent{},
"registry.DeregisterEntity": nil,
"registry.RegisterRuntime": registryCobalt.Runtime{},
"roothash.ExecutorCommit": roothashCobalt.ExecutorCommit{},
"roothash.Evidence": roothashCobalt.Evidence{},
}

var bodyTypeForTxMethod = map[string]interface{}{
"beacon.PVSSCommit": beaconCobalt.PVSSCommit{},
"beacon.PVSSReveal": beaconCobalt.PVSSReveal{},
"beacon.VRFProve": beacon.VRFProve{},
"consensus.Meta": consensusEden.BlockMetadata{},
"governance.SubmitProposal": governance.ProposalContent{},
"governance.CastVote": governance.ProposalVote{},
"keymanager.PublishMasterSecret": keymanagerEden.SignedEncryptedMasterSecret{},
"keymanager.PublishEphemeralSecret": keymanagerEden.SignedEncryptedEphemeralSecret{},
"keymanager.UpdatePolicy": keymanager.SignedPolicySGX{},
"registry.RegisterEntity": entity.SignedEntity{},
"registry.DeregisterEntity": registry.DeregisterEntity{},
"registry.RegisterNode": node.MultiSignedNode{},
"registry.UnfreezeNode": registry.UnfreezeNode{},
"registry.RegisterRuntime": registry.Runtime{},
"registry.ProveFreshness": registry.Runtime{},
"roothash.ExecutorCommit": roothash.ExecutorCommit{},
"roothash.ExecutorProposerTimeout": roothash.ExecutorProposerTimeoutRequest{},
"roothash.Evidence": roothash.Evidence{},
"roothash.SubmitMsg": roothash.SubmitMsg{},
"staking.Transfer": staking.Transfer{},
"staking.Burn": staking.Burn{},
"staking.AddEscrow": staking.Escrow{},
"staking.ReclaimEscrow": staking.ReclaimEscrow{},
"staking.AmendCommissionSchedule": staking.AmendCommissionSchedule{},
"staking.Allow": staking.Allow{},
"staking.Withdraw": staking.Withdraw{},
}

type EventType = apiTypes.ConsensusEventType // alias for brevity

type parsedEvent struct {
Expand Down Expand Up @@ -380,15 +435,24 @@ func (m *processor) queueEpochInserts(batch *storage.QueryBatch, data *consensus

// Adapted from https://github.com/oasisprotocol/oasis-core/blob/master/go/consensus/api/transaction/transaction.go#L58
func unpackTxBody(t *transaction.Transaction) (interface{}, error) {
bodyType := t.Method.BodyType()
if bodyType == nil {
bodyType, ok := bodyTypeForTxMethod[string(t.Method)]
if !ok {
return nil, fmt.Errorf("unknown method body: %s", t.Method)
}

// Deserialize into correct type.
v := reflect.New(reflect.TypeOf(bodyType)).Interface()
if err := cbor.Unmarshal(t.Body, v); err != nil {
return nil, fmt.Errorf("unable to cbor-decode consensus tx body: %w, body: %s", err, base64.StdEncoding.EncodeToString(t.Body))
// This may be an older tx from Cobalt; attempt to unmarshal into
// the appropriate Cobalt tx type.
bodyType, ok = bodyTypeForTxMethodCobalt[string(t.Method)]
if !ok {
return nil, fmt.Errorf("unable to cbor-decode consensus tx body: %w, body: %s", err, base64.StdEncoding.EncodeToString(t.Body))
}
v = reflect.New(reflect.TypeOf(bodyType)).Interface()
if err = cbor.Unmarshal(t.Body, v); err != nil {
return nil, fmt.Errorf("unable to cbor-decode consensus tx body into Cobalt type: %w, body: %s", err, base64.StdEncoding.EncodeToString(t.Body))
}
}

return v, nil
Expand Down
3 changes: 0 additions & 3 deletions coreapi/v22.2.11/beacon/api/vrf.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,6 @@ import (
// GasOpVRFProve is the gas operation identifier for VRF proof submission.
const GasOpVRFProve transaction.Op = "vrf_prove"

// MethodVRFProve is the method name for a VRF proof.
var MethodVRFProve = transaction.NewMethodName(ModuleName, "VRFProve", VRFProve{})

// removed var block

// VRFParameters are the beacon parameters for the VRF backend.
Expand Down
21 changes: 2 additions & 19 deletions coreapi/v22.2.11/consensus/api/transaction/transaction.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
package transaction

import (
"fmt"
"sync"

"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"
Expand All @@ -13,9 +10,6 @@ import (
const moduleName = "consensus/transaction"

// removed var block
var (
registeredMethods sync.Map
)

// Transaction is an unsigned consensus transaction.
type Transaction struct {
Expand Down Expand Up @@ -119,10 +113,7 @@ type MethodName string
// removed func

// BodyType returns the registered body type associated with this method.
func (m MethodName) BodyType() interface{} {
bodyType, _ := registeredMethods.Load(string(m))
return bodyType
}
// removed func

// Metadata returns the method metadata.
// removed func
Expand All @@ -134,15 +125,7 @@ func (m MethodName) BodyType() interface{} {
//
// Module and method pair must be unique. If they are not, this method
// will panic.
func NewMethodName(module, method string, bodyType interface{}) MethodName {
// Check for duplicate method names.
name := module + MethodSeparator + method
if _, isRegistered := registeredMethods.LoadOrStore(name, bodyType); isRegistered {
panic(fmt.Errorf("transaction: method already registered: %s", name))
}

return MethodName(name)
}
// removed func

// Proof is a proof of transaction inclusion in a block.
type Proof struct {
Expand Down
7 changes: 0 additions & 7 deletions coreapi/v22.2.11/governance/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,6 @@ const ModuleName = "governance"
// ProposalContent.
const ProposalContentInvalidText = "(invalid)"

var (
// MethodSubmitProposal submits a new consensus layer governance proposal.
MethodSubmitProposal = transaction.NewMethodName(ModuleName, "SubmitProposal", ProposalContent{})
// MethodCastVote casts a vote for a consensus layer governance proposal.
MethodCastVote = transaction.NewMethodName(ModuleName, "CastVote", ProposalVote{})
)

// removed var block

// ProposalContent is a consensus layer governance proposal content.
Expand Down
7 changes: 0 additions & 7 deletions coreapi/v22.2.11/keymanager/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ package api
import (
"github.com/oasisprotocol/oasis-core/go/common"
"github.com/oasisprotocol/oasis-core/go/common/crypto/signature"

"github.com/oasisprotocol/nexus/coreapi/v22.2.11/consensus/api/transaction"
)

const (
Expand All @@ -16,11 +14,6 @@ const (
ChecksumSize = 32
)

var (
// MethodUpdatePolicy is the method name for policy updates.
MethodUpdatePolicy = transaction.NewMethodName(ModuleName, "UpdatePolicy", SignedPolicySGX{})
)

// removed var block

// Status is the current key manager status.
Expand Down
13 changes: 0 additions & 13 deletions coreapi/v22.2.11/registry/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,19 +30,6 @@ var (
// RegisterNodeSignatureContext is the context used for node
// registration.
RegisterNodeSignatureContext = original.RegisterNodeSignatureContext

// MethodRegisterEntity is the method name for entity registrations.
MethodRegisterEntity = transaction.NewMethodName(ModuleName, "RegisterEntity", entity.SignedEntity{})
// MethodDeregisterEntity is the method name for entity deregistrations.
MethodDeregisterEntity = transaction.NewMethodName(ModuleName, "DeregisterEntity", DeregisterEntity{})
// MethodRegisterNode is the method name for node registrations.
MethodRegisterNode = transaction.NewMethodName(ModuleName, "RegisterNode", node.MultiSignedNode{})
// MethodUnfreezeNode is the method name for unfreezing nodes.
MethodUnfreezeNode = transaction.NewMethodName(ModuleName, "UnfreezeNode", UnfreezeNode{})
// MethodRegisterRuntime is the method name for registering runtimes.
MethodRegisterRuntime = transaction.NewMethodName(ModuleName, "RegisterRuntime", Runtime{})
// MethodProveFreshness is the method name for freshness proofs.
MethodProveFreshness = transaction.NewMethodName(ModuleName, "ProveFreshness", Runtime{})
)

// Backend is a registry implementation.
Expand Down
14 changes: 0 additions & 14 deletions coreapi/v22.2.11/roothash/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,20 +37,6 @@ const (
LogEventHistoryReindexing = "roothash/history_reindexing"
)

var (
// MethodExecutorCommit is the method name for executor commit submission.
MethodExecutorCommit = transaction.NewMethodName(ModuleName, "ExecutorCommit", ExecutorCommit{})

// MethodExecutorProposerTimeout is the method name for executor proposer timeout.
MethodExecutorProposerTimeout = transaction.NewMethodName(ModuleName, "ExecutorProposerTimeout", ExecutorProposerTimeoutRequest{})

// MethodEvidence is the method name for submitting evidence of node misbehavior.
MethodEvidence = transaction.NewMethodName(ModuleName, "Evidence", Evidence{})

// MethodSubmitMsg is the method name for queuing incoming runtime messages.
MethodSubmitMsg = transaction.NewMethodName(ModuleName, "SubmitMsg", SubmitMsg{})
)

// removed var block

// Backend is a root hash implementation.
Expand Down
15 changes: 0 additions & 15 deletions coreapi/v22.2.11/staking/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,21 +27,6 @@ var (
CommonPoolAddress = original.CommonPoolAddress
FeeAccumulatorAddress = original.FeeAccumulatorAddress
GovernanceDepositsAddress = original.GovernanceDepositsAddress

// MethodTransfer is the method name for transfers.
MethodTransfer = transaction.NewMethodName(ModuleName, "Transfer", Transfer{})
// MethodBurn is the method name for burns.
MethodBurn = transaction.NewMethodName(ModuleName, "Burn", Burn{})
// MethodAddEscrow is the method name for escrows.
MethodAddEscrow = transaction.NewMethodName(ModuleName, "AddEscrow", Escrow{})
// MethodReclaimEscrow is the method name for escrow reclamations.
MethodReclaimEscrow = transaction.NewMethodName(ModuleName, "ReclaimEscrow", ReclaimEscrow{})
// MethodAmendCommissionSchedule is the method name for amending commission schedules.
MethodAmendCommissionSchedule = transaction.NewMethodName(ModuleName, "AmendCommissionSchedule", AmendCommissionSchedule{})
// MethodAllow is the method name for setting a beneficiary allowance.
MethodAllow = transaction.NewMethodName(ModuleName, "Allow", Allow{})
// MethodWithdraw is the method name for
MethodWithdraw = transaction.NewMethodName(ModuleName, "Withdraw", Withdraw{})
)

var (
Expand Down
11 changes: 0 additions & 11 deletions storage/oasis/nodeapi/cobalt/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (
coreCommon "github.com/oasisprotocol/oasis-core/go/common"

consensus "github.com/oasisprotocol/nexus/coreapi/v22.2.11/consensus/api"
"github.com/oasisprotocol/nexus/coreapi/v22.2.11/consensus/api/transaction"
genesis "github.com/oasisprotocol/nexus/coreapi/v22.2.11/genesis/api"
governance "github.com/oasisprotocol/nexus/coreapi/v22.2.11/governance/api"
registry "github.com/oasisprotocol/nexus/coreapi/v22.2.11/registry/api"
Expand All @@ -25,7 +24,6 @@ import (
"github.com/oasisprotocol/nexus/storage/oasis/nodeapi"

// data types for Cobalt gRPC APIs.
beaconCobalt "github.com/oasisprotocol/nexus/coreapi/v21.1.1/beacon/api"
consensusCobalt "github.com/oasisprotocol/nexus/coreapi/v21.1.1/consensus/api"
txResultsCobalt "github.com/oasisprotocol/nexus/coreapi/v21.1.1/consensus/api/transaction/results"
genesisCobalt "github.com/oasisprotocol/nexus/coreapi/v21.1.1/genesis/api"
Expand All @@ -37,15 +35,6 @@ import (
stakingCobalt "github.com/oasisprotocol/nexus/coreapi/v21.1.1/staking/api"
)

// We must register non-damask method types with the damask 'transaction' module.
var (
// MethodPVSSCommit is the method name for a PVSS commitment.
MethodPVSSCommit = transaction.NewMethodName(beaconCobalt.ModuleName, "PVSSCommit", beaconCobalt.PVSSCommit{})

// MethodPVSSReveal is the method name for a PVSS reveal.
MethodPVSSReveal = transaction.NewMethodName(beaconCobalt.ModuleName, "PVSSReveal", beaconCobalt.PVSSReveal{})
)

func convertProposal(p *governanceCobalt.Proposal) *governance.Proposal {
results := make(map[governance.Vote]quantity.Quantity)
for k, v := range p.Results {
Expand Down
15 changes: 0 additions & 15 deletions storage/oasis/nodeapi/eden/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
coreCommon "github.com/oasisprotocol/oasis-core/go/common"

"github.com/oasisprotocol/nexus/coreapi/v22.2.11/common/node"
"github.com/oasisprotocol/nexus/coreapi/v22.2.11/consensus/api/transaction"
genesis "github.com/oasisprotocol/nexus/coreapi/v22.2.11/genesis/api"
governance "github.com/oasisprotocol/nexus/coreapi/v22.2.11/governance/api"
registry "github.com/oasisprotocol/nexus/coreapi/v22.2.11/registry/api"
Expand All @@ -24,29 +23,15 @@ import (

// data types for Eden gRPC APIs.
nodeEden "github.com/oasisprotocol/nexus/coreapi/v23.0/common/node"
consensusEden "github.com/oasisprotocol/nexus/coreapi/v23.0/consensus/api"
txResultsEden "github.com/oasisprotocol/nexus/coreapi/v23.0/consensus/api/transaction/results"
genesisEden "github.com/oasisprotocol/nexus/coreapi/v23.0/genesis/api"
governanceEden "github.com/oasisprotocol/nexus/coreapi/v23.0/governance/api"
keymanagerEden "github.com/oasisprotocol/nexus/coreapi/v23.0/keymanager/api"
registryEden "github.com/oasisprotocol/nexus/coreapi/v23.0/registry/api"
roothashEden "github.com/oasisprotocol/nexus/coreapi/v23.0/roothash/api"
schedulerEden "github.com/oasisprotocol/nexus/coreapi/v23.0/scheduler/api"
stakingEden "github.com/oasisprotocol/nexus/coreapi/v23.0/staking/api"
)

// We must register non-damask method types with the damask 'transaction' module.
var (
// MethodMeta is the method name for the special block metadata transaction.
MethodMeta = transaction.NewMethodName(consensusEden.ModuleName, "Meta", consensusEden.BlockMetadata{})

// MethodPublishMasterSecret is the method name for publishing master secret.
MethodPublishMasterSecret = transaction.NewMethodName(keymanagerEden.ModuleName, "PublishMasterSecret", keymanagerEden.SignedEncryptedMasterSecret{})

// MethodPublishEphemeralSecret is the method name for publishing ephemeral secret.
MethodPublishEphemeralSecret = transaction.NewMethodName(keymanagerEden.ModuleName, "PublishEphemeralSecret", keymanagerEden.SignedEncryptedEphemeralSecret{})
)

func convertProposal(p *governanceEden.Proposal) *governance.Proposal {
results := make(map[governance.Vote]quantity.Quantity)
for k, v := range p.Results {
Expand Down

0 comments on commit 797cd9e

Please sign in to comment.