Skip to content

Commit

Permalink
Merge pull request #92 from maticnetwork/genesis-checker
Browse files Browse the repository at this point in the history
Added genesis checker
  • Loading branch information
jdkanani authored Dec 5, 2019
2 parents faa1764 + dd9ffb6 commit 3684aeb
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 12 deletions.
88 changes: 88 additions & 0 deletions cmd/heimdalld/main.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"bytes"
"encoding/hex"
"encoding/json"
"fmt"
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -223,6 +226,91 @@ 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 %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())
}

// 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 Received: %v ValID: %v", val.VotingPower, v.VotingPower, v.ID)
}
}

fmt.Println("Validators information is valid:", "validatorCount", len(validators))
return nil
},
}

return cmd
}

// initialise files required to start heimdall testnet
func TestnetCmd(ctx *server.Context, cdc *codec.Codec) *cobra.Command {
cmd := &cobra.Command{
Expand Down
1 change: 0 additions & 1 deletion helper/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
11 changes: 0 additions & 11 deletions types/validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 3684aeb

Please sign in to comment.