From 1bd1be26d79eb68b60618ff934ebac1ae4ddeac4 Mon Sep 17 00:00:00 2001 From: vaibhavchellani Date: Mon, 25 Nov 2019 15:38:51 +0530 Subject: [PATCH 1/4] added genesis checker --- cmd/heimdalld/main.go | 79 +++++++++++++++++++++++++++++++++++++++++++ helper/config.go | 1 - staking/handler.go | 2 +- 3 files changed, 80 insertions(+), 2 deletions(-) diff --git a/cmd/heimdalld/main.go b/cmd/heimdalld/main.go index 789c1b426..0957d97f1 100644 --- a/cmd/heimdalld/main.go +++ b/cmd/heimdalld/main.go @@ -1,6 +1,7 @@ package main import ( + "bytes" "encoding/hex" "encoding/json" "fmt" @@ -95,6 +96,8 @@ func main() { rootCmd.AddCommand(hmserver.ServeCommands(cdc, hmserver.RegisterRoutes)) rootCmd.AddCommand(InitCmd(ctx, cdc)) rootCmd.AddCommand(TestnetCmd(ctx, cdc)) + rootCmd.AddCommand(VerifyGenesis(ctx, cdc)) + // prepare and add flags executor := cli.PrepareBaseCmd(rootCmd, "HD", os.ExpandEnv("$HOME/.heimdalld")) err := executor.Execute() @@ -223,6 +226,82 @@ func InitCmd(ctx *server.Context, cdc *codec.Codec) *cobra.Command { return cmd } +// VerifyGenesis verifies the genesis file and brings it in sync with on-chain contract +func VerifyGenesis(ctx *server.Context, cdc *codec.Codec) *cobra.Command { + cmd := &cobra.Command{ + Use: "verify-genesis", + Short: "Verify if the genesis matches", + Args: cobra.NoArgs, + RunE: func(_ *cobra.Command, _ []string) error { + config := ctx.Config + config.SetRoot(viper.GetString(cli.HomeFlag)) + helper.InitHeimdallConfig("") + + // Loading genesis doc + genDoc, err := tmTypes.GenesisDocFromFile(filepath.Join(config.RootDir, "config/genesis.json")) + if err != nil { + return err + } + + // get genesis state + var genesisState app.GenesisState + err = json.Unmarshal(genDoc.AppState, &genesisState) + if err != nil { + return err + } + contractCaller, err := helper.NewContractCaller() + if err != nil { + return err + } + + // check header count + currentHeaderIndex, err := contractCaller.CurrentHeaderBlock() + if err != nil { + return nil + } + + if genesisState.CheckpointData.AckCount*helper.GetConfig().ChildBlockInterval != currentHeaderIndex { + fmt.Println("Header Count doesn't match", + "ExpectedHeader", currentHeaderIndex, + "HeaderIndexFound", genesisState.CheckpointData.AckCount*helper.GetConfig().ChildBlockInterval) + return nil + } + fmt.Println("ACK count valid!","count",currentHeaderIndex) + + // check all headers + for i, header := range genesisState.CheckpointData.Headers { + ackCount := uint64(i + 1) + root, start, end, _, _, err := contractCaller.GetHeaderInfo(ackCount * helper.GetConfig().ChildBlockInterval) + if err != nil { + return err + } + if header.StartBlock != start || header.EndBlock != end || bytes.Equal(header.RootHash.Bytes(), root.Bytes()) { + return fmt.Errorf("Checkpoint block doesnt match", + "StartExpected", header.StartBlock, "StartReceived", start, + "EndExpected", header.EndBlock, "EndReceived", header.EndBlock, + "RootHashExpected", header.RootHash.String(), "RootHashReceivd", root.String()) + } + fmt.Println("Checkpoint block valid!", "start", start, "end", end, "root", root.String()) + } + + // validate validators + validators := genesisState.StakingData.Validators + for _, v := range validators { + val, err := contractCaller.GetValidatorInfo(v.ID) + if err != nil { + return err + } + if val.VotingPower != v.VotingPower { + return fmt.Errorf("Voting power mismatch. Expected: %v Recived: %v ValID: %v", val.VotingPower, v.VotingPower, v.ID) + } + } + return nil + }, + } + + return cmd +} + // initialise files required to start heimdall testnet func TestnetCmd(ctx *server.Context, cdc *codec.Codec) *cobra.Command { cmd := &cobra.Command{ diff --git a/helper/config.go b/helper/config.go index 89b59eb54..faaabf71e 100644 --- a/helper/config.go +++ b/helper/config.go @@ -202,7 +202,6 @@ func InitHeimdallConfigWith(homeDir string, heimdallConfigFilePath string) { } maticClient = ethclient.NewClient(maticRPCClient) - // Loading genesis doc genDoc, err := tmTypes.GenesisDocFromFile(filepath.Join(configDir, "genesis.json")) if err != nil { diff --git a/staking/handler.go b/staking/handler.go index 9b1ca2a43..47206b838 100644 --- a/staking/handler.go +++ b/staking/handler.go @@ -91,7 +91,7 @@ func HandleMsgValidatorJoin(ctx sdk.Context, msg MsgValidatorJoin, k Keeper, con ID: validator.ID, StartEpoch: validator.StartEpoch, EndEpoch: validator.EndEpoch, - VotingPower: validator.VotingPower, + VotingPower: types.GetValidatorPower(validator.VotingPower), PubKey: pubkey, Signer: validator.Signer, LastUpdated: 0, From df4f0d9f1cb5bfc6f1ceb4c3fbe0e83eb7646353 Mon Sep 17 00:00:00 2001 From: Jaynti Kanani Date: Thu, 5 Dec 2019 01:05:30 +0530 Subject: [PATCH 2/4] remove unwanted function --- staking/handler.go | 2 +- types/validator.go | 11 ----------- 2 files changed, 1 insertion(+), 12 deletions(-) diff --git a/staking/handler.go b/staking/handler.go index 47206b838..9b1ca2a43 100644 --- a/staking/handler.go +++ b/staking/handler.go @@ -91,7 +91,7 @@ func HandleMsgValidatorJoin(ctx sdk.Context, msg MsgValidatorJoin, k Keeper, con ID: validator.ID, StartEpoch: validator.StartEpoch, EndEpoch: validator.EndEpoch, - VotingPower: types.GetValidatorPower(validator.VotingPower), + VotingPower: validator.VotingPower, PubKey: pubkey, Signer: validator.Signer, LastUpdated: 0, diff --git a/types/validator.go b/types/validator.go index d09f6dd4d..6bd8e41fc 100644 --- a/types/validator.go +++ b/types/validator.go @@ -159,17 +159,6 @@ func (v *Validator) MinimalVal() MinimalVal { } } -// GetValidatorPower converts amount to power -func GetValidatorPower(amount string) uint64 { - result := big.NewInt(0) - result.SetString(amount, 10) - if len(amount) >= 18 { - t, _ := big.NewInt(0).SetString("1000000000000000000", 10) - result.Div(result, t) - } - return result.Uint64() -} - // -------- // ValidatorID validator ID and helper functions From f5d97227a1fef8e435b5b8c4634d0626ed572ed1 Mon Sep 17 00:00:00 2001 From: Jaynti Kanani Date: Thu, 5 Dec 2019 13:32:59 +0530 Subject: [PATCH 3/4] add logs --- cmd/heimdalld/main.go | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/cmd/heimdalld/main.go b/cmd/heimdalld/main.go index 0957d97f1..4996488c4 100644 --- a/cmd/heimdalld/main.go +++ b/cmd/heimdalld/main.go @@ -266,7 +266,7 @@ func VerifyGenesis(ctx *server.Context, cdc *codec.Codec) *cobra.Command { "HeaderIndexFound", genesisState.CheckpointData.AckCount*helper.GetConfig().ChildBlockInterval) return nil } - fmt.Println("ACK count valid!","count",currentHeaderIndex) + fmt.Println("ACK count valid:", "count", currentHeaderIndex) // check all headers for i, header := range genesisState.CheckpointData.Headers { @@ -281,7 +281,7 @@ func VerifyGenesis(ctx *server.Context, cdc *codec.Codec) *cobra.Command { "EndExpected", header.EndBlock, "EndReceived", header.EndBlock, "RootHashExpected", header.RootHash.String(), "RootHashReceivd", root.String()) } - fmt.Println("Checkpoint block valid!", "start", start, "end", end, "root", root.String()) + fmt.Println("Checkpoint block valid:", "start", start, "end", end, "root", root.String()) } // validate validators @@ -291,10 +291,13 @@ func VerifyGenesis(ctx *server.Context, cdc *codec.Codec) *cobra.Command { if err != nil { return err } + if val.VotingPower != v.VotingPower { - return fmt.Errorf("Voting power mismatch. Expected: %v Recived: %v ValID: %v", val.VotingPower, v.VotingPower, v.ID) + return fmt.Errorf("Voting power mismatch. Expected: %v Received: %v ValID: %v", val.VotingPower, v.VotingPower, v.ID) } } + + fmt.Println("Validators information is valid:", "validatorCount", len(validators)) return nil }, } From dd9ffb6a371756284405e8d0d0f65681388cb183 Mon Sep 17 00:00:00 2001 From: Jaynti Kanani Date: Thu, 5 Dec 2019 13:54:37 +0530 Subject: [PATCH 4/4] bug fix while checking root hash for checkpoint --- cmd/heimdalld/main.go | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/cmd/heimdalld/main.go b/cmd/heimdalld/main.go index 4996488c4..1f1712b20 100644 --- a/cmd/heimdalld/main.go +++ b/cmd/heimdalld/main.go @@ -275,11 +275,17 @@ func VerifyGenesis(ctx *server.Context, cdc *codec.Codec) *cobra.Command { if err != nil { return err } - if header.StartBlock != start || header.EndBlock != end || bytes.Equal(header.RootHash.Bytes(), root.Bytes()) { - return fmt.Errorf("Checkpoint block doesnt match", - "StartExpected", header.StartBlock, "StartReceived", start, - "EndExpected", header.EndBlock, "EndReceived", header.EndBlock, - "RootHashExpected", header.RootHash.String(), "RootHashReceivd", root.String()) + + if header.StartBlock != start || header.EndBlock != end || !bytes.Equal(header.RootHash.Bytes(), root.Bytes()) { + return fmt.Errorf( + "Checkpoint block doesnt match: startExpected %v, startReceived %v, endExpected %v, endReceived %v, rootHashExpected %v, rootHashReceived %v", + header.StartBlock, + start, + header.EndBlock, + header.EndBlock, + header.RootHash.String(), + root.String(), + ) } fmt.Println("Checkpoint block valid:", "start", start, "end", end, "root", root.String()) }