Skip to content

Commit

Permalink
Merge pull request #122 from DefiantLabs/pharr117/custom-msg-type-reg…
Browse files Browse the repository at this point in the history
…istry

pharr117/Add custom msg type url map registration function to indexer
  • Loading branch information
pharr117 authored Sep 14, 2024
2 parents eaf166d + 14bd6a9 commit 4b15faa
Show file tree
Hide file tree
Showing 7 changed files with 71 additions and 45 deletions.
6 changes: 5 additions & 1 deletion cmd/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,11 @@ func setupIndexer() *indexerPackage.Indexer {

config.SetChainConfig(indexer.Config.Probe.AccountPrefix)

indexer.ChainClient = probe.GetProbeClient(indexer.Config.Probe, indexer.CustomModuleBasics)
indexer.ChainClient, err = probe.GetProbeClient(indexer.Config.Probe, indexer.CustomModuleBasics, indexer.CustomMsgTypeRegistry)

if err != nil {
config.Log.Fatal("Failed to create probe client", err)
}

// Depending on the app configuration, wait for the chain to catch up
chainCatchingUp, err := rpc.IsCatchingUp(indexer.ChainClient)
Expand Down
54 changes: 29 additions & 25 deletions db/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,20 +44,22 @@ func IndexBlockEvents(db *gorm.DB, dryRun bool, blockDBWrapper *BlockDBWrapper,
uniqueBlockEventTypes = append(uniqueBlockEventTypes, value)
}

// Bulk find or create on unique event types
if err := dbTransaction.Clauses(
clause.Returning{
Columns: []clause.Column{
{Name: "id"}, {Name: "type"},
if len(uniqueBlockEventTypes) != 0 {
// Bulk find or create on unique event types
if err := dbTransaction.Clauses(
clause.Returning{
Columns: []clause.Column{
{Name: "id"}, {Name: "type"},
},
},
},
clause.OnConflict{
Columns: []clause.Column{{Name: "type"}},
DoUpdates: clause.AssignmentColumns([]string{"type"}),
},
).Create(&uniqueBlockEventTypes).Error; err != nil {
config.Log.Error("Error creating begin block event types.", err)
return err
clause.OnConflict{
Columns: []clause.Column{{Name: "type"}},
DoUpdates: clause.AssignmentColumns([]string{"type"}),
},
).Create(&uniqueBlockEventTypes).Error; err != nil {
config.Log.Error("Error creating begin block event types.", err)
return err
}
}

for _, value := range uniqueBlockEventTypes {
Expand All @@ -70,19 +72,21 @@ func IndexBlockEvents(db *gorm.DB, dryRun bool, blockDBWrapper *BlockDBWrapper,
uniqueBlockEventAttributeKeys = append(uniqueBlockEventAttributeKeys, value)
}

if err := dbTransaction.Clauses(
clause.Returning{
Columns: []clause.Column{
{Name: "id"}, {Name: "key"},
if len(uniqueBlockEventAttributeKeys) != 0 {
if err := dbTransaction.Clauses(
clause.Returning{
Columns: []clause.Column{
{Name: "id"}, {Name: "key"},
},
},
},
clause.OnConflict{
Columns: []clause.Column{{Name: "key"}},
DoUpdates: clause.AssignmentColumns([]string{"key"}),
},
).Create(&uniqueBlockEventAttributeKeys).Error; err != nil {
config.Log.Error("Error creating begin block event attribute keys.", err)
return err
clause.OnConflict{
Columns: []clause.Column{{Name: "key"}},
DoUpdates: clause.AssignmentColumns([]string{"key"}),
},
).Create(&uniqueBlockEventAttributeKeys).Error; err != nil {
config.Log.Error("Error creating begin block event attribute keys.", err)
return err
}
}

for _, value := range uniqueBlockEventAttributeKeys {
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ go 1.22
toolchain go1.22.1

require (
github.com/DefiantLabs/probe v0.0.0-20240820043230-ffbbb1824466
github.com/DefiantLabs/probe v1.0.0
github.com/cometbft/cometbft v0.37.4
github.com/cosmos/cosmos-sdk v0.47.7
github.com/cosmos/ibc-go/v7 v7.3.1
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -219,8 +219,8 @@ github.com/CosmWasm/wasmd v0.40.0/go.mod h1:SuxskRBB7+bpwXGhUXaEfdpjg5WKpdxBy7Tm
github.com/CosmWasm/wasmvm v1.2.3 h1:OKYlobwmVGbl0eSn0mXoAAjE5hIuXnQCLPjbNd91sVY=
github.com/CosmWasm/wasmvm v1.2.3/go.mod h1:vW/E3h8j9xBQs9bCoijDuawKo9kCtxOaS8N8J7KFtkc=
github.com/DataDog/datadog-go v3.2.0+incompatible/go.mod h1:LButxg5PwREeZtORoXG3tL4fMGNddJ+vMq1mwgfaqoQ=
github.com/DefiantLabs/probe v0.0.0-20240820043230-ffbbb1824466 h1:OyEKu8aQoiysfrZp+bN6R8utbD+x8f7ZCnkgE4mpmgg=
github.com/DefiantLabs/probe v0.0.0-20240820043230-ffbbb1824466/go.mod h1:6zCXSvZTy/w8iXPU56Cxcppa7aOttQbePgT0QbS9Ecs=
github.com/DefiantLabs/probe v1.0.0 h1:EnKRGpFbTTRqfpAii2AdOdfiHT/8TlrXgK1+o+ch8b0=
github.com/DefiantLabs/probe v1.0.0/go.mod h1:6zCXSvZTy/w8iXPU56Cxcppa7aOttQbePgT0QbS9Ecs=
github.com/Knetic/govaluate v3.0.1-0.20171022003610-9aa49832a739+incompatible/go.mod h1:r7JcOSlj0wfOMncg0iLm8Leh48TZaKVeNIfJntJ2wa0=
github.com/Microsoft/go-winio v0.6.0 h1:slsWYD/zyx7lCXoZVlvQrj0hPTM1HI4+v1sIda2yDvg=
github.com/Microsoft/go-winio v0.6.0/go.mod h1:cTAf44im0RAYeL23bpB+fzCyDH2MJiz2BO69KH/soAE=
Expand Down
18 changes: 18 additions & 0 deletions indexer/registration.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,31 @@ import (
"github.com/DefiantLabs/cosmos-indexer/db/models"
"github.com/DefiantLabs/cosmos-indexer/filter"
"github.com/DefiantLabs/cosmos-indexer/parsers"
sdkTypes "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/module"
)

func (indexer *Indexer) RegisterCustomModuleBasics(basics []module.AppModuleBasic) {
indexer.CustomModuleBasics = append(indexer.CustomModuleBasics, basics...)
}

func (indexer *Indexer) RegisterCustomMsgTypesByTypeURLs(customMessageTypeURLSToTypes map[string]sdkTypes.Msg) error {

Check failure on line 19 in indexer/registration.go

View workflow job for this annotation

GitHub Actions / lint

File is not `gofumpt`-ed (gofumpt)
if indexer.CustomMsgTypeRegistry == nil {
indexer.CustomMsgTypeRegistry = make(map[string]sdkTypes.Msg)
}

for url, msg := range customMessageTypeURLSToTypes {
if _, ok := indexer.CustomMsgTypeRegistry[url]; ok {
return fmt.Errorf("found duplicate message type with URL \"%s\", message types must be uniquely identified", url)
} else {

Check warning on line 27 in indexer/registration.go

View workflow job for this annotation

GitHub Actions / lint

indent-error-flow: if block ends with a return statement, so drop this else and outdent its block (move short variable declaration to its own line if necessary) (revive)
indexer.CustomMsgTypeRegistry[url] = msg
}
}

return nil
}

func (indexer *Indexer) RegisterMessageTypeFilter(filter filter.MessageTypeFilter) {
indexer.MessageTypeFilters = append(indexer.MessageTypeFilters, filter)
}
Expand Down
2 changes: 2 additions & 0 deletions indexer/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/DefiantLabs/cosmos-indexer/filter"
"github.com/DefiantLabs/cosmos-indexer/parsers"
"github.com/DefiantLabs/probe/client"
sdkTypes "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/module"
"gorm.io/gorm"
)
Expand Down Expand Up @@ -51,6 +52,7 @@ type Indexer struct {
BlockEventFilterRegistries BlockEventFilterRegistries
MessageTypeFilters []filter.MessageTypeFilter
MessageFilters []filter.MessageFilter
CustomMsgTypeRegistry map[string]sdkTypes.Msg
CustomBeginBlockEventParserRegistry map[string][]parsers.BlockEventParser // Used for associating parsers to block event types in BeginBlock events
CustomEndBlockEventParserRegistry map[string][]parsers.BlockEventParser // Used for associating parsers to block event types in EndBlock events
CustomBeginBlockParserTrackers map[string]models.BlockEventParser // Used for tracking block event parsers in the database
Expand Down
30 changes: 14 additions & 16 deletions probe/probe.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,12 @@ package probe
import (
"github.com/DefiantLabs/cosmos-indexer/config"
probeClient "github.com/DefiantLabs/probe/client"
sdkTypes "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/module"
)

func GetProbeClient(conf config.Probe, appModuleBasicsExtensions []module.AppModuleBasic) *probeClient.ChainClient {
cl, err := probeClient.NewChainClient(GetProbeConfig(conf, true, appModuleBasicsExtensions), "", nil, nil)
if err != nil {
config.Log.Fatalf("Error connecting to chain. Err: %v", err)
}
return cl
func GetProbeClient(conf config.Probe, appModuleBasicsExtensions []module.AppModuleBasic, customMsgTypeRegistry map[string]sdkTypes.Msg) (*probeClient.ChainClient, error) {
return probeClient.NewChainClient(GetProbeConfig(conf, true, appModuleBasicsExtensions, customMsgTypeRegistry), "", nil, nil)
}

// Will include the protos provided by the Probe package for Osmosis module interfaces
Expand All @@ -24,20 +21,21 @@ func IncludeTendermintInterfaces(client *probeClient.ChainClient) {
probeClient.RegisterTendermintLiquidityInterfaces(client.Codec.Amino, client.Codec.InterfaceRegistry)
}

func GetProbeConfig(conf config.Probe, debug bool, appModuleBasicsExtensions []module.AppModuleBasic) *probeClient.ChainClientConfig {
func GetProbeConfig(conf config.Probe, debug bool, appModuleBasicsExtensions []module.AppModuleBasic, customMsgTypeRegistry map[string]sdkTypes.Msg) *probeClient.ChainClientConfig {
moduleBasics := []module.AppModuleBasic{}
moduleBasics = append(moduleBasics, probeClient.DefaultModuleBasics...)
moduleBasics = append(moduleBasics, appModuleBasicsExtensions...)

return &probeClient.ChainClientConfig{
Key: "default",
ChainID: conf.ChainID,
RPCAddr: conf.RPC,
AccountPrefix: conf.AccountPrefix,
KeyringBackend: "test",
Debug: debug,
Timeout: "30s",
OutputFormat: "json",
Modules: moduleBasics,
Key: "default",
ChainID: conf.ChainID,
RPCAddr: conf.RPC,
AccountPrefix: conf.AccountPrefix,
KeyringBackend: "test",
Debug: debug,
Timeout: "30s",
OutputFormat: "json",
Modules: moduleBasics,
CustomMsgTypeRegistry: customMsgTypeRegistry,
}
}

0 comments on commit 4b15faa

Please sign in to comment.