Skip to content

Commit

Permalink
address comments
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrew7234 committed Jan 17, 2023
1 parent 02cbf69 commit 863090f
Show file tree
Hide file tree
Showing 11 changed files with 64 additions and 73 deletions.
22 changes: 0 additions & 22 deletions analyzer/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (

oasisConfig "github.com/oasisprotocol/oasis-sdk/client-sdk/go/config"

apiTypes "github.com/oasisprotocol/oasis-indexer/api/v1/types"
"github.com/oasisprotocol/oasis-indexer/storage"
)

Expand Down Expand Up @@ -82,27 +81,6 @@ type RoundRange struct {
To uint64
}

var StringToEvent map[string]apiTypes.ConsensusEventType = map[string]apiTypes.ConsensusEventType{
"staking.transfer": apiTypes.ConsensusEventTypeStakingTransfer,
"staking.burn": apiTypes.ConsensusEventTypeStakingBurn,
"staking.escrow.add": apiTypes.ConsensusEventTypeStakingEscrowAdd,
"staking.escrow.take": apiTypes.ConsensusEventTypeStakingEscrowTake,
"staking.escrow.debonding_start": apiTypes.ConsensusEventTypeStakingEscrowDebondingStart,
"staking.escrow.reclaim": apiTypes.ConsensusEventTypeStakingEscrowReclaim,
"staking.allowance_change": apiTypes.ConsensusEventTypeStakingAllowanceChange,
"registry.runtime": apiTypes.ConsensusEventTypeRegistryRuntime,
"registry.entity": apiTypes.ConsensusEventTypeRegistryEntity,
"registry.node": apiTypes.ConsensusEventTypeRegistryNode,
"registry.node_unfrozen": apiTypes.ConsensusEventTypeRegistryNodeUnfrozen,
"roothash.executor_committed": apiTypes.ConsensusEventTypeRoothashExecutorCommitted,
"roothash.execution_discrepancy_detected": apiTypes.ConsensusEventTypeRoothashExecutionDiscrepancy,
"roothash.finalized": apiTypes.ConsensusEventTypeRoothashFinalized,
"governance.proposal_submitted": apiTypes.ConsensusEventTypeGovernanceProposalSubmitted,
"governance.proposal_executed": apiTypes.ConsensusEventTypeGovernanceProposalExecuted,
"governance.proposal_finalized": apiTypes.ConsensusEventTypeGovernanceProposalFinalized,
"governance.vote": apiTypes.ConsensusEventTypeGovernanceVote,
}

// ChainID is the ID of a chain.
type ChainID string

Expand Down
52 changes: 23 additions & 29 deletions analyzer/consensus/consensus.go
Original file line number Diff line number Diff line change
Expand Up @@ -464,12 +464,12 @@ func (m *Main) queueTxEventInserts(batch *storage.QueryBatch, data *storage.Cons
for i := 0; i < len(data.Results); i++ {
var txAccounts []staking.Address
for j := 0; j < len(data.Results[i].Events); j++ {
eventData, err := extractEventData(data.Results[i].Events[j])
eventData, err := m.extractEventData(data.Results[i].Events[j])
if err != nil {
return err
}
txAccounts = append(txAccounts, eventData.relatedAddresses...)
accounts := m.extractUniqueAddresses(eventData.relatedAddresses)
accounts := extractUniqueAddresses(eventData.relatedAddresses)
body, err := json.Marshal(eventData.body)
if err != nil {
return err
Expand All @@ -484,7 +484,7 @@ func (m *Main) queueTxEventInserts(batch *storage.QueryBatch, data *storage.Cons
accounts,
)
}
uniqueTxAccounts := m.extractUniqueAddresses(txAccounts)
uniqueTxAccounts := extractUniqueAddresses(txAccounts)
for _, addr := range uniqueTxAccounts {
batch.Queue(accountRelatedTransactionInsertQuery,
addr,
Expand Down Expand Up @@ -963,14 +963,13 @@ func (m *Main) queueGovernanceEventInserts(batch *storage.QueryBatch, data *stor
}

func (m *Main) queueSingleEventInserts(batch *storage.QueryBatch, eventData *parsedEvent, height int64) error {
eventInsertQuery := m.qf.ConsensusEventInsertQuery()
accounts := m.extractUniqueAddresses(eventData.relatedAddresses)
accounts := extractUniqueAddresses(eventData.relatedAddresses)
body, err := json.Marshal(eventData.body)
if err != nil {
return err
}

batch.Queue(eventInsertQuery,
batch.Queue(m.qf.ConsensusEventInsertQuery(),
string(eventData.ty),
string(body),
height,
Expand All @@ -982,7 +981,7 @@ func (m *Main) queueSingleEventInserts(batch *storage.QueryBatch, eventData *par
return nil
}

func (m *Main) extractUniqueAddresses(accounts []staking.Address) []string {
func extractUniqueAddresses(accounts []staking.Address) []string {
var uniqueAccounts []string
seen := make(map[string]struct{})
for _, addr := range accounts {
Expand All @@ -998,7 +997,7 @@ func (m *Main) extractUniqueAddresses(accounts []staking.Address) []string {
}

// extractEventData extracts the type, the body (JSON-serialized), and the related accounts of an event.
func extractEventData(event *results.Event) (*parsedEvent, error) {
func (m *Main) extractEventData(event *results.Event) (*parsedEvent, error) {
switch e := event; {
case e.Staking != nil:
return extractStakingEvent(event.Staking)
Expand All @@ -1010,6 +1009,8 @@ func extractEventData(event *results.Event) (*parsedEvent, error) {
return extractGovernanceEvent(event.Governance)
}

m.logger.Error("cannot infer consensus event type from event struct", "event", event)

return &parsedEvent{
ty: EventType("unknown"),
body: []byte{},
Expand All @@ -1019,13 +1020,12 @@ func extractEventData(event *results.Event) (*parsedEvent, error) {

func extractGovernanceEvent(event *governance.Event) (*parsedEvent, error) {
var eventData parsedEvent
var accounts []staking.Address
var err error
switch event := event; {
case event.ProposalSubmitted != nil:
eventData.ty = apiTypes.ConsensusEventTypeGovernanceProposalSubmitted
eventData.body = event.ProposalSubmitted
accounts = append(accounts, event.ProposalSubmitted.Submitter)
eventData.relatedAddresses = []staking.Address{event.ProposalSubmitted.Submitter}
case event.ProposalExecuted != nil:
eventData.ty = apiTypes.ConsensusEventTypeGovernanceProposalExecuted
eventData.body = event.ProposalExecuted
Expand All @@ -1035,24 +1035,22 @@ func extractGovernanceEvent(event *governance.Event) (*parsedEvent, error) {
case event.Vote != nil:
eventData.ty = apiTypes.ConsensusEventTypeGovernanceVote
eventData.body = event.Vote
accounts = append(accounts, event.Vote.Submitter)
eventData.relatedAddresses = []staking.Address{event.Vote.Submitter}
default:
err = fmt.Errorf("unsupported registry event type: %#v", event)
}
eventData.relatedAddresses = accounts
return &eventData, err
}

func extractRootHashEvent(event *roothash.Event) (*parsedEvent, error) {
var eventData parsedEvent
var accounts []staking.Address
var err error
switch event := event; {
case event.ExecutorCommitted != nil:
eventData.ty = apiTypes.ConsensusEventTypeRoothashExecutorCommitted
nodeAddr := staking.NewAddress(event.ExecutorCommitted.Commit.NodeID)
eventData.body = event.ExecutorCommitted
accounts = append(accounts, nodeAddr)
eventData.relatedAddresses = []staking.Address{nodeAddr}
case event.ExecutionDiscrepancyDetected != nil:
eventData.ty = apiTypes.ConsensusEventTypeRoothashExecutionDiscrepancy
eventData.body = event.ExecutionDiscrepancyDetected
Expand All @@ -1062,13 +1060,11 @@ func extractRootHashEvent(event *roothash.Event) (*parsedEvent, error) {
default:
err = fmt.Errorf("unsupported registry event type: %#v", event)
}
eventData.relatedAddresses = accounts
return &eventData, err
}

func extractRegistryEvent(event *registry.Event) (*parsedEvent, error) {
var eventData parsedEvent
var accounts []staking.Address
var err error
switch event := event; {
case event.RuntimeEvent != nil:
Expand All @@ -1078,68 +1074,66 @@ func extractRegistryEvent(event *registry.Event) (*parsedEvent, error) {
eventData.ty = apiTypes.ConsensusEventTypeRegistryEntity
eventData.body = event.EntityEvent
addr := staking.NewAddress(event.EntityEvent.Entity.ID)
accounts = append(accounts, addr)
accounts := []staking.Address{addr}
for _, node := range event.EntityEvent.Entity.Nodes {
nodeAddr := staking.NewAddress(node)
accounts = append(accounts, nodeAddr)
}
eventData.relatedAddresses = accounts
case event.NodeEvent != nil:
eventData.ty = apiTypes.ConsensusEventTypeRegistryNode
eventData.body = event.NodeEvent
nodeAddr := staking.NewAddress(event.NodeEvent.Node.EntityID)
entityAddr := staking.NewAddress(event.NodeEvent.Node.ID)
accounts = append(accounts, nodeAddr, entityAddr)
eventData.relatedAddresses = []staking.Address{nodeAddr, entityAddr}
case event.NodeUnfrozenEvent != nil:
eventData.ty = apiTypes.ConsensusEventTypeRegistryNodeUnfrozen
eventData.body = event.NodeUnfrozenEvent
nodeAddr := staking.NewAddress(event.NodeUnfrozenEvent.NodeID)
accounts = append(accounts, nodeAddr)
eventData.relatedAddresses = []staking.Address{nodeAddr}
default:
err = fmt.Errorf("unsupported registry event type: %#v", event)
}
eventData.relatedAddresses = accounts
return &eventData, err
}

func extractStakingEvent(event *staking.Event) (*parsedEvent, error) {
var eventData parsedEvent
var accounts []staking.Address
var err error
switch event := event; {
case event.Transfer != nil:
eventData.ty = apiTypes.ConsensusEventTypeStakingTransfer
eventData.body = event.Transfer
accounts = append(accounts, event.Transfer.From, event.Transfer.To)
eventData.relatedAddresses = []staking.Address{event.Transfer.From, event.Transfer.To}
case event.Burn != nil:
eventData.ty = apiTypes.ConsensusEventTypeStakingBurn
eventData.body = event.Burn
accounts = append(accounts, event.Burn.Owner)
eventData.relatedAddresses = []staking.Address{event.Burn.Owner}
case event.Escrow != nil:
switch t := event.Escrow; {
case t.Add != nil:
eventData.ty = apiTypes.ConsensusEventTypeStakingEscrowAdd
eventData.body = event.Escrow.Add
accounts = append(accounts, t.Add.Owner, t.Add.Escrow)
eventData.relatedAddresses = []staking.Address{t.Add.Owner, t.Add.Escrow}
case t.Take != nil:
eventData.ty = apiTypes.ConsensusEventTypeStakingEscrowTake
eventData.body = event.Escrow.Take
accounts = append(accounts, t.Take.Owner)
eventData.relatedAddresses = []staking.Address{t.Take.Owner}
case t.DebondingStart != nil:
eventData.ty = apiTypes.ConsensusEventTypeStakingEscrowDebondingStart
eventData.body = event.Escrow.DebondingStart
accounts = append(accounts, t.DebondingStart.Owner, t.DebondingStart.Escrow)
eventData.relatedAddresses = []staking.Address{t.DebondingStart.Owner, t.DebondingStart.Escrow}
case t.Reclaim != nil:
eventData.ty = apiTypes.ConsensusEventTypeStakingEscrowReclaim
eventData.body = event.Escrow.Reclaim
accounts = append(accounts, t.Reclaim.Owner, t.Reclaim.Escrow)
eventData.relatedAddresses = []staking.Address{t.Reclaim.Owner, t.Reclaim.Escrow}
}
case event.AllowanceChange != nil:
eventData.ty = apiTypes.ConsensusEventTypeStakingAllowanceChange
eventData.body, err = json.Marshal(event.AllowanceChange)
accounts = append(accounts, event.AllowanceChange.Owner, event.AllowanceChange.Beneficiary)
eventData.relatedAddresses = []staking.Address{event.AllowanceChange.Owner, event.AllowanceChange.Beneficiary}
default:
err = fmt.Errorf("unsupported registry event type: %#v", event)
}
eventData.relatedAddresses = accounts
return &eventData, err
}
4 changes: 2 additions & 2 deletions api/spec/v1.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,7 @@ paths:
- *limit
- *offset
- in: query
name: height
name: block
schema:
type: integer
format: int64
Expand Down Expand Up @@ -1065,7 +1065,7 @@ components:
type: object
required: [height, type, body]
properties:
height:
block:
type: integer
format: int64
description: The block height at which this event was generated.
Expand Down
11 changes: 5 additions & 6 deletions api/v1/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import (
governance "github.com/oasisprotocol/oasis-core/go/governance/api"
staking "github.com/oasisprotocol/oasis-core/go/staking/api"

analyzerApi "github.com/oasisprotocol/oasis-indexer/analyzer"
apiCommon "github.com/oasisprotocol/oasis-indexer/api/common"
apiTypes "github.com/oasisprotocol/oasis-indexer/api/v1/types"
"github.com/oasisprotocol/oasis-indexer/common"
Expand Down Expand Up @@ -81,7 +80,7 @@ func validateConsensusAddress(param string) (*staking.Address, error) {

// validateEventType parses a consensus event type url parameter.
func validateEventType(param string) (*string, error) {
_, ok := analyzerApi.StringToEvent[param]
_, ok := apiTypes.StringToEvent[param]
if ok {
return &param, nil
}
Expand Down Expand Up @@ -257,16 +256,16 @@ func (c *storageClient) Events(ctx context.Context, r *http.Request) (*apiTypes.
var q storage.EventsRequest

params := r.URL.Query()
if v := params.Get("height"); v != "" {
height, err := validateInt64(v)
if v := params.Get("block"); v != "" {
block, err := validateInt64(v)
if err != nil {
return nil, apiCommon.ErrBadRequest
}
q.Height = &height
q.Block = &block
}
if v := params.Get("tx_index"); v != "" {
// If tx_index is provided, height must also be provided.
if q.Height == nil {
if q.Block == nil {
return nil, apiCommon.ErrBadRequest
}

Expand Down
10 changes: 5 additions & 5 deletions api/v1/types/openapi.gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 22 additions & 0 deletions api/v1/types/util.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package types

var StringToEvent map[string]ConsensusEventType = map[string]ConsensusEventType{
"staking.transfer": ConsensusEventTypeStakingTransfer,
"staking.burn": ConsensusEventTypeStakingBurn,
"staking.escrow.add": ConsensusEventTypeStakingEscrowAdd,
"staking.escrow.take": ConsensusEventTypeStakingEscrowTake,
"staking.escrow.debonding_start": ConsensusEventTypeStakingEscrowDebondingStart,
"staking.escrow.reclaim": ConsensusEventTypeStakingEscrowReclaim,
"staking.allowance_change": ConsensusEventTypeStakingAllowanceChange,
"registry.runtime": ConsensusEventTypeRegistryRuntime,
"registry.entity": ConsensusEventTypeRegistryEntity,
"registry.node": ConsensusEventTypeRegistryNode,
"registry.node_unfrozen": ConsensusEventTypeRegistryNodeUnfrozen,
"roothash.executor_committed": ConsensusEventTypeRoothashExecutorCommitted,
"roothash.execution_discrepancy_detected": ConsensusEventTypeRoothashExecutionDiscrepancy,
"roothash.finalized": ConsensusEventTypeRoothashFinalized,
"governance.proposal_submitted": ConsensusEventTypeGovernanceProposalSubmitted,
"governance.proposal_executed": ConsensusEventTypeGovernanceProposalExecuted,
"governance.proposal_finalized": ConsensusEventTypeGovernanceProposalFinalized,
"governance.vote": ConsensusEventTypeGovernanceVote,
}
2 changes: 1 addition & 1 deletion storage/client/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ type TransactionRequest struct {
}

type EventsRequest struct {
Height *int64
Block *int64
TxIndex *int32
TxHash *string
Rel *staking.Address
Expand Down
4 changes: 2 additions & 2 deletions storage/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -355,7 +355,7 @@ func (c *StorageClient) Events(ctx context.Context, r *EventsRequest, p *apiComm
rows, err = c.db.Query(
ctx,
qf.EventsQuery(),
r.Height,
r.Block,
r.TxIndex,
r.TxHash,
r.Type,
Expand All @@ -379,7 +379,7 @@ func (c *StorageClient) Events(ctx context.Context, r *EventsRequest, p *apiComm

for rows.Next() {
var e Event
if err := rows.Scan(&e.Height, &e.TxIndex, &e.TxHash, &e.Type, &e.Body); err != nil {
if err := rows.Scan(&e.Block, &e.TxIndex, &e.TxHash, &e.Type, &e.Body); err != nil {
c.logger.Info("query failed",
"err", err.Error(),
)
Expand Down
2 changes: 1 addition & 1 deletion storage/client/queries.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ func (qf QueryFactory) EventsQuery() string {
($2::integer IS NULL OR tx_index = $2::integer) AND
($3::text IS NULL OR tx_hash = $3::text) AND
($4::text IS NULL OR type = $4::text) AND
($5::text IS NULL OR $5::text = ANY(related_accounts))
($5::text IS NULL OR ARRAY[$5::text] <@ related_accounts)
ORDER BY tx_block DESC, tx_index
LIMIT $6::bigint
OFFSET $7::bigint`, qf.chainID)
Expand Down
Loading

0 comments on commit 863090f

Please sign in to comment.