Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Problem: New validator projection. Implement migration scripts and views [Part 2] #684

Open
wants to merge 24 commits into
base: feature/652-validator-delegation
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
fe5ce23
Commit migration scripts and some of the implemented views.
ysong42 Jan 18, 2022
1670d06
Implemented remaining views.
ysong42 Jan 19, 2022
9dcbc8a
Add logic for projection prepare config
ysong42 Jan 19, 2022
cd561ef
Add config init. Fixed migration script.
ysong42 Jan 19, 2022
23000b2
Added logic for handle MsgCreateValidator in Genesis
ysong42 Jan 21, 2022
69ad576
Added test APIs to fetch validators and delegations
ysong42 Jan 21, 2022
46f686e
Add temporary operation performance log DB schema.
ysong42 Jan 24, 2022
2413edf
Add height index on delegations table
ysong42 Jan 24, 2022
50e375b
Refactor migrations
ysong42 Jan 24, 2022
0e75837
Fixed validator json fields
ysong42 Jan 25, 2022
24c890e
Fix a bug that validator is not removed when its token decrease to 0.
ysong42 Jan 28, 2022
826fc25
Update migrations script to test range type performance
ysong42 Jan 28, 2022
1d33b46
Apply range types for other tables in the new projection.
ysong42 Feb 4, 2022
c321a08
Apply range type to delegations, unbondingDelegations, redelegations …
ysong42 Feb 7, 2022
467494f
Add what HTTP API for unbondingDelegations and redelegations.
ysong42 Feb 7, 2022
5387cab
Update index and constraints for the new projection
ysong42 Feb 8, 2022
541bf2e
Fix migrations script
ysong42 Feb 8, 2022
886dd28
Commit latest working code. Include the performance log related debug…
ysong42 Feb 11, 2022
0081eeb
Update example app
ysong42 Feb 11, 2022
0e0c234
Removed performance trace related code
ysong42 Feb 11, 2022
f5211d1
Updated example
ysong42 Feb 11, 2022
720b1da
Changed back a example config
ysong42 Feb 11, 2022
38ae3fe
Updated the PR according to comments.
ysong42 Feb 21, 2022
d80fba5
Updated example accordingly
ysong42 Feb 21, 2022
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions example/Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
build: chain-indexing-app
chain-indexing-app:
build: example-app
example-app:
go build ./app/example-app/
clean:
rm -i example-app
60 changes: 60 additions & 0 deletions example/app/example-app/config/bridgesapi.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package config

import (
"github.com/crypto-com/chain-indexing/external/bridges/parsers"
"github.com/crypto-com/chain-indexing/external/bridges/parsers/cronos"
"github.com/crypto-com/chain-indexing/infrastructure/httpapi/handlers"
)

type BridgesAPIConfig struct {
Networks []BridgeNetworkConfig `yaml:"networks"`
Chains []handlers.BridgeChainConfig `yaml:"chains"`
}

type BridgeNetworkConfig struct {
ChainName string `yaml:"chain_name" mapstructure:"chain_name"`
// Chain network unique abbreviation, used in URL query params
Abbreviation handlers.NetworkAbbreviation `yaml:"abbreviation" mapstructure:"abbreviation"`
MaybeAddressHookKey *string `yaml:"maybe_address_hook_key"`
MaybeCronosAccountAddressPrefix *string `yaml:"maybe_cronos_account_address_prefix"`
}

var BridgeAddressHooks = (func() map[string]func(
networkConfig *BridgeNetworkConfig,
) handlers.AddressHook {
hooks := make(map[string]func(config *BridgeNetworkConfig) handlers.AddressHook, 0)

hooks["DefaultLowercaseAddressHook"] = func(_ *BridgeNetworkConfig) handlers.AddressHook {
return parsers.DefaultLowercaseAddressHook
}
hooks["DefaultCronosEVMAddressHookGenerator"] = func(config *BridgeNetworkConfig) handlers.AddressHook {
return cronos.DefaultCronosEVMAddressHookGenerator(*config.MaybeCronosAccountAddressPrefix)
}

return hooks
})()

func ParseBridgesConfig(rawConfig *BridgesAPIConfig) handlers.BridgesConfig {
config := handlers.BridgesConfig{
Networks: make([]handlers.BridgeNetworkConfig, 0, len(rawConfig.Networks)),
Chains: rawConfig.Chains,
}

for i, network := range rawConfig.Networks {
if network.MaybeAddressHookKey == nil {
config.Networks = append(config.Networks, handlers.BridgeNetworkConfig{
ChainName: network.ChainName,
Abbreviation: network.Abbreviation,
})
} else {
hook := BridgeAddressHooks[*network.MaybeAddressHookKey](&rawConfig.Networks[i])
config.Networks = append(config.Networks, handlers.BridgeNetworkConfig{
ChainName: network.ChainName,
Abbreviation: network.Abbreviation,
MaybeAddressHook: &hook,
})
}
}

return config
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package main
package config

import (
"github.com/crypto-com/chain-indexing/bootstrap/config"
)

type CustomConfig struct {
ServerGithubAPI ServerGithubAPIConfig `yaml:"server_github_api"`
BridgeAPI BridgesAPIConfig `yaml:"bridges_api"`
}

type ServerGithubAPIConfig struct {
Expand Down
2 changes: 1 addition & 1 deletion example/app/example-app/cronjobs.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func InitCronJob(name string, params InitCronJobParams) projection_entity.CronJo
params.Logger.Panicf(err.Error())
}

return bridge_activity_matcher.New(params.Logger, params.RdbConn, migrationHelper, config)
return bridge_activity_matcher.New(config, params.Logger, params.RdbConn, migrationHelper)
// register more cronjobs here
default:
panic(fmt.Sprintf("Unrecognized cron job: %s", name))
Expand Down
42 changes: 34 additions & 8 deletions example/app/example-app/projections.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,18 @@ import (
"github.com/crypto-com/chain-indexing/projection/proposal"
"github.com/crypto-com/chain-indexing/projection/transaction"
"github.com/crypto-com/chain-indexing/projection/validator"
"github.com/crypto-com/chain-indexing/projection/validator_delegation"
"github.com/crypto-com/chain-indexing/projection/validatorstats"
"github.com/ettle/strcase"

appconfig "github.com/crypto-com/chain-indexing/example/app/example-app/config"
)

func initProjections(
logger applogger.Logger,
rdbConn rdb.Conn,
config *configuration.Config,
customConfig *CustomConfig,
customConfig *appconfig.CustomConfig,
) []projection_entity.Projection {
if !config.IndexService.Enable {
return []projection_entity.Projection{}
Expand All @@ -60,9 +63,10 @@ func initProjections(

ExtraConfigs: config.IndexService.Projection.ExtraConfigs,

CosmosAppClient: cosmosAppClient,
AccountAddressPrefix: config.Blockchain.AccountAddressPrefix,
ConsNodeAddressPrefix: config.Blockchain.ConNodeAddressPrefix,
CosmosAppClient: cosmosAppClient,
AccountAddressPrefix: config.Blockchain.AccountAddressPrefix,
ValidatorAddressPrefix: config.Blockchain.ValidatorAddressPrefix,
ConsNodeAddressPrefix: config.Blockchain.ConNodeAddressPrefix,

GithubAPIUser: config.IndexService.GithubAPI.Username,
GithubAPIToken: config.IndexService.GithubAPI.Token,
Expand Down Expand Up @@ -320,7 +324,28 @@ func InitProjection(name string, params InitProjectionParams) projection_entity.
params.Logger.Panicf(err.Error())
}

return bridge_pending_activity.NewBridgePendingActivity(params.Logger, params.RdbConn, migrationHelper, config)
return bridge_pending_activity.New(config, params.Logger, params.RdbConn, migrationHelper)
case "ValidatorDelegation":
sourceURL := github_migrationhelper.GenerateDefaultSourceURL(name, githubMigrationHelperConfig)
databaseURL := migrationhelper.GenerateDefaultDatabaseURL(name, connString)
migrationHelper := github_migrationhelper.NewGithubMigrationHelper(sourceURL, databaseURL)

customConfig, err := validator_delegation.CustomConfigFromInterface(params.ExtraConfigs[name])
if err != nil {
params.Logger.Panicf(err.Error())
}

config, err := validator_delegation.PrepareConfig(
customConfig,
params.AccountAddressPrefix,
params.ValidatorAddressPrefix,
params.ConsNodeAddressPrefix,
)
if err != nil {
params.Logger.Panicf(err.Error())
}

return validator_delegation.NewValidatorDelegation(params.Logger, params.RdbConn, config, migrationHelper)
}

return nil
Expand Down Expand Up @@ -348,9 +373,10 @@ type InitProjectionParams struct {

ExtraConfigs map[string]interface{}

CosmosAppClient cosmosapp.Client
AccountAddressPrefix string
ConsNodeAddressPrefix string
CosmosAppClient cosmosapp.Client
AccountAddressPrefix string
ValidatorAddressPrefix string
ConsNodeAddressPrefix string

GithubAPIUser string
GithubAPIToken string
Expand Down
46 changes: 45 additions & 1 deletion example/app/example-app/routes/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,20 @@ import (
"github.com/crypto-com/chain-indexing/appinterface/tendermint"
"github.com/crypto-com/chain-indexing/bootstrap"
"github.com/crypto-com/chain-indexing/bootstrap/config"
custom_httpapi_handlers "github.com/crypto-com/chain-indexing/example/httpapi/handlers"
applogger "github.com/crypto-com/chain-indexing/external/logger"
cosmosapp_infrastructure "github.com/crypto-com/chain-indexing/infrastructure/cosmosapp"
httpapi_handlers "github.com/crypto-com/chain-indexing/infrastructure/httpapi/handlers"
tendermint_infrastructure "github.com/crypto-com/chain-indexing/infrastructure/tendermint"

appconfig "github.com/crypto-com/chain-indexing/example/app/example-app/config"
custom_httpapi_handlers "github.com/crypto-com/chain-indexing/example/httpapi/handlers"
)

func InitRouteRegistry(
logger applogger.Logger,
rdbConn rdb.Conn,
config *config.Config,
customConfig *appconfig.CustomConfig,
) bootstrap.RouteRegistry {
var cosmosAppClient cosmosapp.Client
if config.CosmosApp.Insecure {
Expand Down Expand Up @@ -335,6 +338,7 @@ func InitRouteRegistry(
logger,
rdbConn.ToHandle(),
accountAddressPrefix,
appconfig.ParseBridgesConfig(&customConfig.BridgeAPI),
)
routes = append(routes,
Route{
Expand Down Expand Up @@ -366,5 +370,45 @@ func InitRouteRegistry(
},
)

validatorDelegationHandler := httpapi_handlers.NewValidatorDelegation(
logger,
config.Blockchain.AccountAddressPrefix,
config.Blockchain.ValidatorAddressPrefix,
config.Blockchain.ConNodeAddressPrefix,
rdbConn.ToHandle(),
)
routes = append(routes,
Route{
Method: GET,
path: "api/test/validators",
handler: validatorDelegationHandler.ListValidator,
},
Route{
Method: GET,
path: "api/test/validators/{address}",
handler: validatorDelegationHandler.FindValidatorByAddress,
},
Route{
Method: GET,
path: "api/test/validators/{address}/delegations",
handler: validatorDelegationHandler.ListDelegationByValidator,
},
Route{
Method: GET,
path: "api/test/validators/{address}/unbonding_delegations",
handler: validatorDelegationHandler.ListUnbondingDelegationByValidator,
},
Route{
Method: GET,
path: "api/test/validators/{srcValAddress}/redelegations",
handler: validatorDelegationHandler.ListRedelegationBySrcValidator,
},
Route{
Method: GET,
path: "api/test/delegators/{address}/delegations",
handler: validatorDelegationHandler.ListDelegationByDelegator,
},
)

return &RouteRegistry{routes: routes}
}
18 changes: 10 additions & 8 deletions example/app/example-app/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,17 @@ import (
"os"
"path/filepath"

"github.com/crypto-com/chain-indexing/bootstrap"
configuration "github.com/crypto-com/chain-indexing/bootstrap/config"
"github.com/crypto-com/chain-indexing/example/app/example-app/routes"
"github.com/crypto-com/chain-indexing/example/internal/filereader/yaml"
"github.com/urfave/cli/v2"

"github.com/crypto-com/chain-indexing/bootstrap"
configuration "github.com/crypto-com/chain-indexing/bootstrap/config"
applogger "github.com/crypto-com/chain-indexing/external/logger"
"github.com/crypto-com/chain-indexing/external/primptr"
"github.com/crypto-com/chain-indexing/infrastructure"

appconfig "github.com/crypto-com/chain-indexing/example/app/example-app/config"
"github.com/crypto-com/chain-indexing/example/app/example-app/routes"
"github.com/crypto-com/chain-indexing/example/internal/filereader/yaml"
)

func run(args []string) error {
Expand Down Expand Up @@ -100,13 +102,13 @@ func run(args []string) error {
return fmt.Errorf("error config from yaml: %v", err)
}

var customConfig CustomConfig
var customConfig appconfig.CustomConfig
err = yaml.FromYAMLFile(configPath, &customConfig)
if err != nil {
return fmt.Errorf("error custom config from yaml: %v", err)
}

cliConfig := CLIConfig{
cliConfig := appconfig.CLIConfig{
LogLevel: ctx.String("logLevel"),

DatabaseHost: ctx.String("dbHost"),
Expand All @@ -131,7 +133,7 @@ func run(args []string) error {
cliConfig.DatabasePort = primptr.Int32(int32(ctx.Int("dbPort")))
}

OverrideByCLIConfig(&config, &cliConfig)
appconfig.OverrideByCLIConfig(&config, &cliConfig)

// Create logger
logLevel := parseLogLevel(config.Logger.Level)
Expand All @@ -144,7 +146,7 @@ func run(args []string) error {
initProjections(logger, app.GetRDbConn(), &config, &customConfig),
initCronJobs(logger, app.GetRDbConn(), &config),
)
app.InitHTTPAPIServer(routes.InitRouteRegistry(logger, app.GetRDbConn(), &config))
app.InitHTTPAPIServer(routes.InitRouteRegistry(logger, app.GetRDbConn(), &config, &customConfig))

app.Run()

Expand Down
23 changes: 15 additions & 8 deletions example/config/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -28,22 +28,28 @@ index_service:
"Validator",
"ValidatorStats",
"NFT",
# "CryptoComNFT",
# "CryptoComNFT",
"IBCChannel",
# "IBCChannelTxMsgTrace",
# "IBCChannelTxMsgTrace",
"IBCChannelMessage",
"BridgePendingActivity",
"Example",
]
# "ValidatorDelegation",
]
extra_configs:
BridgePendingActivity:
this_chain_id: "testnet-croeseid-4"
this_chain_name: "Crypto.org-Chain"
counterparty_chain_name: "Cronos"
channel_id: "channel-131"
starting_height: 899374
ValidatorDelegation:
unbonding_time: "2419200000000000ns"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggest to change to ms / s

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh this is from Crypto.org genesis. I just copied from the genesis.

Later when we support proposals updaing params in this projection, we will remove this config.

slash_fraction_double_sign: "0.050000000000000000"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

May i know that are those slash fractions can be change by proposal?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

https://crypto.org/explorer/proposals

You can check those proposals there. So there are different type of proposals. And there are module params that could be configured through gov module's proposal.

slash_fraction_downtime: "0.000000000000000000"
default_power_reduction: "1000000"
cronjob:
enables: [ ]
enables: []
cosmos_version_enabled_height:
v0_42_7: 0
github_api:
Expand All @@ -59,9 +65,10 @@ http_service:
# A list of origins a cross-domain request is allowed to be requested from
# Default value '[]' disables CORS support
# Use '["*"]' to allow request from any origin
cors_allowed_origins: [ ]
cors_allowed_methods: [ "HEAD", "GET" ]
cors_allowed_headers: [ "Origin", "Accept", "Content-Type", "X-Requested-With", "X-Server-Time" ]
cors_allowed_origins: []
cors_allowed_methods: ["HEAD", "GET"]
cors_allowed_headers:
["Origin", "Accept", "Content-Type", "X-Requested-With", "X-Server-Time"]

tendermint_app:
http_rpc_url: "https://testnet-croeseid-4.crypto.org:26657"
Expand Down Expand Up @@ -105,4 +112,4 @@ prometheus:

# Custom config for example
server_github_api:
migration_repo_ref: ""
migration_repo_ref: ""
Loading