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

feat: add CMD parse-genesis #257

Merged
merged 33 commits into from
Dec 13, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
c80934e
add parse genesis CMD
huichiaotsou Nov 7, 2021
a0255dc
possibility to parse off node
huichiaotsou Nov 8, 2021
0690929
update error handling text
huichiaotsou Nov 8, 2021
ad6beca
change naming
huichiaotsou Nov 8, 2021
81b97e3
move registered modules place
huichiaotsou Nov 8, 2021
ae06240
add PostRun; add utils_gentx: StoreValidatorsFromMsgCreateValidator
huichiaotsou Nov 15, 2021
135fcf9
revert naming: createValMsg
huichiaotsou Nov 15, 2021
0cc493f
remove postRun, add modules as args
huichiaotsou Nov 15, 2021
cf672b1
#
huichiaotsou Nov 15, 2021
601dafa
log out invalid module name input
huichiaotsou Nov 16, 2021
4340336
remove log.Debug from HandleGenesis methods
huichiaotsou Nov 16, 2021
29954df
remove log.Debug
huichiaotsou Nov 16, 2021
3319ecc
merge 2 cmd: parse all and parse modules
huichiaotsou Nov 17, 2021
dead38b
fix comments
huichiaotsou Nov 17, 2021
e55d09f
fix comments
huichiaotsou Nov 17, 2021
8d749c1
Merge branch 'v2/cosmos/stargate' into v2/aaron/parse_genesis_cmd
huichiaotsou Nov 20, 2021
8422185
change way to obtain genesis file path
huichiaotsou Nov 29, 2021
eb4c71b
modif package name
huichiaotsou Nov 29, 2021
edf1c3d
get modules with parseCtc
huichiaotsou Dec 3, 2021
ace6119
remove method getRegisteredModules
huichiaotsou Dec 3, 2021
f50acf3
call juno func Get Gen Doc and state
huichiaotsou Dec 6, 2021
dbe8c93
use juno utils to get gen doc and state
huichiaotsou Dec 6, 2021
d22cce5
remove get GetGenesisDocAndState method
huichiaotsou Dec 7, 2021
7fe5c9b
add back logger for HandleGenesis
huichiaotsou Dec 7, 2021
5650a00
same as last
huichiaotsou Dec 7, 2021
7be3cda
move import position
huichiaotsou Dec 7, 2021
268216a
same as last;
huichiaotsou Dec 7, 2021
d625870
move import position
huichiaotsou Dec 7, 2021
079396f
re-implement logic for parsing specified moduels
huichiaotsou Dec 7, 2021
84f132c
rearrange position of genDocState
huichiaotsou Dec 7, 2021
86e0926
small fixes
RiccardoM Dec 13, 2021
ede6352
Merge branch 'v2/cosmos/stargate' of github.com:forbole/bdjuno into v…
RiccardoM Dec 13, 2021
d58ff51
minor improvements
RiccardoM Dec 13, 2021
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
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ stop-docker-test:

start-docker-test: stop-docker-test
@echo "Starting Docker container..."
@docker run --name bdjuno-test-db -e POSTGRES_USER=bdjuno -e POSTGRES_PASSWORD=password -e POSTGRES_DB=bdjuno -d -p 5433:5432 postgres
@docker run --name bdjuno-test-db -e POSTGRES_USER=bdjuno -e POSTGRES_PASSWORD=password -e POSTGRES_DB=bdjuno -d -p 6433:5432 postgres
.PHONY: start-docker-test

test-unit: start-docker-test
Expand Down
2 changes: 2 additions & 0 deletions cmd/bdjuno/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (

fixcmd "github.com/forbole/bdjuno/v2/cmd/fix"
migratecmd "github.com/forbole/bdjuno/v2/cmd/migrate"
parsegenesiscmd "github.com/forbole/bdjuno/v2/cmd/parse-genesis"

"github.com/forbole/bdjuno/v2/types/config"

Expand All @@ -35,6 +36,7 @@ func main() {
parsecmd.ParseCmd(cfg.GetParseConfig()),
migratecmd.NewMigrateCmd(),
fixcmd.NewFixCmd(cfg.GetParseConfig()),
parsegenesiscmd.NewParseGenesisCmd(cfg.GetParseConfig()),
)

executor := cmd.PrepareRootCmd(cfg.GetName(), rootCmd)
Expand Down
71 changes: 71 additions & 0 deletions cmd/parse-genesis/parseGenesis.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package parsegenesis

import (
"fmt"

"github.com/forbole/juno/v2/cmd/parse"
"github.com/forbole/juno/v2/modules"
"github.com/forbole/juno/v2/types/config"
junoutils "github.com/forbole/juno/v2/types/utils"
"github.com/spf13/cobra"
)

// NewParseGenesisCmd returns the Cobra command allowing to parse the genesis file
func NewParseGenesisCmd(parseCfg *parse.Config) *cobra.Command {
return &cobra.Command{
Use: "parse-genesis [[module names]]",
Short: "Parse genesis file. To parse specific modules, input module names as arguments",
Example: "bdjuno parse-genesis auth bank consensus gov history staking",
PreRunE: parse.ReadConfig(parseCfg),
RunE: func(cmd *cobra.Command, args []string) error {
parseCtx, err := parse.GetParsingContext(parseCfg)
if err != nil {
return err
}

// Get the modules to parse
var modulesToParse []modules.Module
for _, moduleName := range args {
module, found := getModule(moduleName, parseCtx)
if !found {
return fmt.Errorf("module %s is not registered", moduleName)
}

modulesToParse = append(modulesToParse, module)
}

// Default to all the modules
if len(modulesToParse) == 0 {
modulesToParse = parseCtx.Modules
}

// Get the genesis doc and state
genesisDoc, genesisState, err := junoutils.GetGenesisDocAndState(config.Cfg.Parser.GenesisFilePath, parseCtx.Node)
if err != nil {
return fmt.Errorf("error while getting genesis doc and state: %s", err)
}

// For each module, parse the genesis
for _, module := range modulesToParse {
if genesisModule, ok := module.(modules.GenesisModule); ok {
err = genesisModule.HandleGenesis(genesisDoc, genesisState)
if err != nil {
return fmt.Errorf("error while parsing genesis of %s module: %s", module.Name(), err)
}
}
}

return nil
},
}
}

// doesModuleExist tells whether a module with the given name exist inside the specified context ot not
func getModule(module string, parseCtx *parse.Context) (modules.Module, bool) {
for _, mod := range parseCtx.Modules {
if module == mod.Name() {
return mod, true
}
}
return nil, false
}
2 changes: 1 addition & 1 deletion database/database_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func (suite *DbTestSuite) SetupTest() {
dbCfg := dbconfig.NewDatabaseConfig(
"bdjuno",
"localhost",
5433,
6433,
"bdjuno",
"password",
"",
Expand Down
2 changes: 1 addition & 1 deletion database/types/coins.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ func (coins DbCoins) ToCoins() sdk.Coins {
return sdkCoins
}

//_______________________________________________________
// --------------------------------------------------------------------------------------------------------------------

// DbDecCoin represents the information stored inside the database about a single coin
type DbDecCoin struct {
Expand Down
2 changes: 1 addition & 1 deletion database/types/staking_validators.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ func (v ValidatorInfoRow) Equal(w ValidatorInfoRow) bool {
v.Height == w.Height
}

//________________________________________________________________
// --------------------------------------------------------------------------------------------------------------------

// ValidatorDescriptionRow represent a row in validator_description
type ValidatorDescriptionRow struct {
Expand Down
7 changes: 1 addition & 6 deletions database/types/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,6 @@ type ModuleRow struct {
Module string `db:"module_name"`
}

//NewModuleRow return a new instance of ModuleRow
func NewModuleRow(name string) ModuleRow {
return ModuleRow{Module: name}
}

// Equal return true if two moduleRow is equal
func (v ModuleRow) Equal(w ModuleRow) bool {
return v.Module == w.Module
Expand All @@ -18,7 +13,7 @@ func (v ModuleRow) Equal(w ModuleRow) bool {
// ModuleRows represent an array of ModulerRow
type ModuleRows []*ModuleRow

//NewModuleRows return a new instance of ModuleRows
// NewModuleRows return a new instance of ModuleRows
func NewModuleRows(names []string) ModuleRows {
rows := make([]*ModuleRow, 0)
for _, name := range names {
Expand Down
2 changes: 1 addition & 1 deletion database/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ func (db *Db) InsertEnableModules(modules []string) error {
return nil
}

//clear table first
// Remove existing modules
stmt := "DELETE FROM modules WHERE TRUE"
_, err := db.Sql.Exec(stmt)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ go 1.16

require (
github.com/cosmos/cosmos-sdk v0.42.9
github.com/forbole/juno/v2 v2.0.0-20211122103116-54945048da0a
github.com/forbole/juno/v2 v2.0.0-20211206070432-9314a5057955
github.com/go-co-op/gocron v1.11.0
github.com/gogo/protobuf v1.3.3
github.com/jmoiron/sqlx v1.2.1-0.20200324155115-ee514944af4b
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -186,8 +186,8 @@ github.com/facebookgo/subset v0.0.0-20150612182917-8dac2c3c4870/go.mod h1:5tD+ne
github.com/fatih/color v1.7.0/go.mod h1:Zm6kSWBoL9eyXnKyktHP6abPY2pDugNf5KwzbycvMj4=
github.com/felixge/httpsnoop v1.0.1 h1:lvB5Jl89CsZtGIWuTcDM1E/vkVs49/Ml7JJe07l8SPQ=
github.com/felixge/httpsnoop v1.0.1/go.mod h1:m8KPJKqk1gH5J9DgRY2ASl2lWCfGKXixSwevea8zH2U=
github.com/forbole/juno/v2 v2.0.0-20211122103116-54945048da0a h1:I8haGuUrZt5jUsr7VEIZ6Gb1o1xVbHyJBuf0KuTrfGU=
github.com/forbole/juno/v2 v2.0.0-20211122103116-54945048da0a/go.mod h1:nOZEHr5+sZDXEDpV2E+NHkmH0Zvf4prkt5+UVKwScQ8=
github.com/forbole/juno/v2 v2.0.0-20211206070432-9314a5057955 h1:a8XH7gpsjUgmXjPkeU2C1b3yRppPxc8nVKWqK4vXAGI=
github.com/forbole/juno/v2 v2.0.0-20211206070432-9314a5057955/go.mod h1:nOZEHr5+sZDXEDpV2E+NHkmH0Zvf4prkt5+UVKwScQ8=
github.com/forbole/tendermint v0.34.13-0.20210820072129-a2a4af55563d h1:pUqGUgTUU24ibHeloQeg1F2pFbgQllddsuZ+x+CcUzw=
github.com/forbole/tendermint v0.34.13-0.20210820072129-a2a4af55563d/go.mod h1:aeHL7alPh4uTBIJQ8mgFEE8VwJLXI1VD3rVOmH2Mcy0=
github.com/fortytw2/leaktest v1.3.0 h1:u8491cBMTQ8ft8aeV+adlcytMZylmA5nnwwkRZjI8vw=
Expand Down
1 change: 1 addition & 0 deletions modules/auth/handle_genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
// HandleGenesis implements modules.GenesisModule
func (m *Module) HandleGenesis(_ *tmtypes.GenesisDoc, appState map[string]json.RawMessage) error {
log.Debug().Str("module", "auth").Msg("parsing genesis")
huichiaotsou marked this conversation as resolved.
Show resolved Hide resolved

accounts, err := GetGenesisAccounts(appState, m.cdc)
if err != nil {
return fmt.Errorf("error while getting genesis accounts: %s", err)
Expand Down
3 changes: 2 additions & 1 deletion modules/distribution/handle_genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ import (
"encoding/json"
"fmt"

"github.com/forbole/bdjuno/v2/types"
tmtypes "github.com/tendermint/tendermint/types"

"github.com/forbole/bdjuno/v2/types"

distrtypes "github.com/cosmos/cosmos-sdk/x/distribution/types"
"github.com/rs/zerolog/log"
)
Expand Down
3 changes: 3 additions & 0 deletions modules/history/handle_genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,14 @@ package history
import (
"encoding/json"

"github.com/rs/zerolog/log"
tmtypes "github.com/tendermint/tendermint/types"
)

// HandleGenesis implements modules.GenesisModule
func (m *Module) HandleGenesis(doc *tmtypes.GenesisDoc, _ map[string]json.RawMessage) error {
log.Debug().Str("module", "history").Msg("parsing genesis")

accounts, err := m.db.GetAccounts()
if err != nil {
return err
Expand Down
3 changes: 2 additions & 1 deletion modules/mint/handle_genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ import (
"encoding/json"
"fmt"

"github.com/forbole/bdjuno/v2/types"
tmtypes "github.com/tendermint/tendermint/types"

"github.com/forbole/bdjuno/v2/types"

minttypes "github.com/cosmos/cosmos-sdk/x/mint/types"
"github.com/rs/zerolog/log"
)
Expand Down
3 changes: 2 additions & 1 deletion modules/slashing/handle_genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ import (
"encoding/json"
"fmt"

"github.com/forbole/bdjuno/v2/types"
tmtypes "github.com/tendermint/tendermint/types"

"github.com/forbole/bdjuno/v2/types"

slashingtypes "github.com/cosmos/cosmos-sdk/x/slashing/types"
"github.com/rs/zerolog/log"
)
Expand Down
1 change: 1 addition & 0 deletions modules/staking/expected_modules.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"time"

sdk "github.com/cosmos/cosmos-sdk/types"

"github.com/forbole/bdjuno/v2/types"
)

Expand Down
3 changes: 2 additions & 1 deletion modules/staking/handle_genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,11 +100,12 @@ func (m *Module) parseGenesisTransactions(doc *tmtypes.GenesisDoc, appState map[
continue
}

err = m.handleMsgCreateValidator(doc.InitialHeight, createValMsg)
err = m.StoreValidatorsFromMsgCreateValidator(doc.InitialHeight, createValMsg)
if err != nil {
return fmt.Errorf("error while storing validators from MsgCreateValidator: %s", err)
}
}

}

return nil
Expand Down
82 changes: 82 additions & 0 deletions modules/staking/utils_gentx.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package staking

import (
"fmt"

cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types"
sdk "github.com/cosmos/cosmos-sdk/types"
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"

"github.com/forbole/bdjuno/v2/modules/staking/keybase"
"github.com/forbole/bdjuno/v2/types"
)

// StoreValidatorFromMsgCreateValidator handles properly a MsgCreateValidator instance by
// saving into the database all the data associated to such validator
func (m *Module) StoreValidatorsFromMsgCreateValidator(height int64, msg *stakingtypes.MsgCreateValidator) error {
var pubKey cryptotypes.PubKey
err := m.cdc.UnpackAny(msg.Pubkey, &pubKey)
if err != nil {
return fmt.Errorf("error while unpacking pub key: %s", err)
}
avatarURL, err := keybase.GetAvatarURL(msg.Description.Identity)
if err != nil {
return fmt.Errorf("error while getting Avatar URL: %s", err)
}

// Save the validators
err = m.db.SaveValidatorData(
types.NewValidator(
sdk.ConsAddress(pubKey.Address()).String(),
msg.ValidatorAddress, pubKey.String(),
msg.DelegatorAddress,
&msg.Commission.MaxChangeRate,
&msg.Commission.MaxRate,
height,
),
)
if err != nil {
return err
}

// Save the descriptions
err = m.db.SaveValidatorDescription(
types.NewValidatorDescription(
msg.ValidatorAddress,
msg.Description,
avatarURL,
height,
),
)
if err != nil {
return err
}

// Save the first self-delegations
err = m.db.SaveDelegations([]types.Delegation{
types.NewDelegation(
msg.DelegatorAddress,
msg.ValidatorAddress,
msg.Value,
height,
),
})
if err != nil {
return err
}

// Save the commissions
err = m.db.SaveValidatorCommission(
types.NewValidatorCommission(
msg.ValidatorAddress,
&msg.Commission.Rate,
&msg.MinSelfDelegation,
height,
),
)
if err != nil {
return err
}

return nil
}