diff --git a/cmd/index.go b/cmd/index.go index a045137d..05ee9e96 100644 --- a/cmd/index.go +++ b/cmd/index.go @@ -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) diff --git a/db/events.go b/db/events.go index 2d5cdc52..4a983974 100644 --- a/db/events.go +++ b/db/events.go @@ -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 { @@ -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 { diff --git a/go.mod b/go.mod index 2b9a025c..0ca96276 100644 --- a/go.mod +++ b/go.mod @@ -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 diff --git a/go.sum b/go.sum index 414286aa..f9eded37 100644 --- a/go.sum +++ b/go.sum @@ -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= diff --git a/indexer/registration.go b/indexer/registration.go index 557fff0b..95312fed 100644 --- a/indexer/registration.go +++ b/indexer/registration.go @@ -7,6 +7,7 @@ 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" ) @@ -14,6 +15,23 @@ func (indexer *Indexer) RegisterCustomModuleBasics(basics []module.AppModuleBasi indexer.CustomModuleBasics = append(indexer.CustomModuleBasics, basics...) } +func (indexer *Indexer) RegisterCustomMsgTypesByTypeURLs(customMessageTypeURLSToTypes map[string]sdkTypes.Msg) error { + + 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 { + indexer.CustomMsgTypeRegistry[url] = msg + } + } + + return nil +} + func (indexer *Indexer) RegisterMessageTypeFilter(filter filter.MessageTypeFilter) { indexer.MessageTypeFilters = append(indexer.MessageTypeFilters, filter) } diff --git a/indexer/types.go b/indexer/types.go index e2e69cf4..4d8bb73b 100644 --- a/indexer/types.go +++ b/indexer/types.go @@ -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" ) @@ -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 diff --git a/probe/probe.go b/probe/probe.go index 83922562..815a1857 100644 --- a/probe/probe.go +++ b/probe/probe.go @@ -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 @@ -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, } }