From 378a4c0ac9a3aa4a9b698b15073448f60382fca0 Mon Sep 17 00:00:00 2001 From: zakir <80246097+zakir-code@users.noreply.github.com> Date: Mon, 2 Sep 2024 09:46:40 +0800 Subject: [PATCH] fix: check mainnet genesis hash --- cmd/doctor.go | 9 ++++----- server/start.go | 17 +++++++++-------- types/version.go | 14 ++------------ 3 files changed, 15 insertions(+), 25 deletions(-) diff --git a/cmd/doctor.go b/cmd/doctor.go index b38ba53f..048f8219 100644 --- a/cmd/doctor.go +++ b/cmd/doctor.go @@ -2,6 +2,7 @@ package cmd import ( "bytes" + "encoding/hex" "encoding/json" "fmt" "os" @@ -11,6 +12,7 @@ import ( "strings" tmcfg "github.com/cometbft/cometbft/config" + "github.com/cometbft/cometbft/crypto/tmhash" tmjson "github.com/cometbft/cometbft/libs/json" "github.com/cometbft/cometbft/privval" tmtypes "github.com/cometbft/cometbft/types" @@ -525,13 +527,10 @@ func getGenesisDocAndSha256(genesisFile string) (*tmtypes.GenesisDoc, string, er if err != nil { return nil, "", err } - dst := &bytes.Buffer{} - if err = json.Compact(dst, genesisFileData); err != nil { - return nil, "", err - } genesisDoc, err := tmtypes.GenesisDocFromJSON(genesisFileData) if err != nil { return nil, "", err } - return genesisDoc, fxtypes.Sha256Hex(dst.Bytes()), nil + genesisHash := hex.EncodeToString(tmhash.Sum(genesisFileData)) + return genesisDoc, genesisHash, nil } diff --git a/server/start.go b/server/start.go index bbe58f83..d4ab4184 100644 --- a/server/start.go +++ b/server/start.go @@ -1,13 +1,14 @@ package server import ( + "encoding/hex" "errors" "fmt" "os" "time" tmcfg "github.com/cometbft/cometbft/config" - tmjson "github.com/cometbft/cometbft/libs/json" + "github.com/cometbft/cometbft/crypto/tmhash" "github.com/cometbft/cometbft/node" "github.com/cometbft/cometbft/store" tmtypes "github.com/cometbft/cometbft/types" @@ -41,15 +42,15 @@ func StartCmd(appCreator types.AppCreator, defaultNodeHome string) *cobra.Comman return fmt.Errorf("couldn't read GenesisDoc file: %w", err) } expectGenesisHash := serverCtx.Viper.GetString("genesis_hash") - actualGenesisHash := fxtypes.Sha256Hex(genesisBytes) - if len(expectGenesisHash) != 0 && fxtypes.Sha256Hex(genesisBytes) != expectGenesisHash { + actualGenesisHash := hex.EncodeToString(tmhash.Sum(genesisBytes)) + if len(expectGenesisHash) != 0 && actualGenesisHash != expectGenesisHash { return fmt.Errorf("--genesis_hash=%s does not match %s hash: %s", expectGenesisHash, genDocFile, actualGenesisHash) } genesisDoc, err := tmtypes.GenesisDocFromJSON(genesisBytes) if err != nil { return fmt.Errorf("error reading GenesisDoc at %s: %w", genDocFile, err) } - if err = checkMainnetAndBlock(genesisDoc, serverCtx.Config); err != nil { + if err = checkMainnetAndBlock(genesisDoc, actualGenesisHash, serverCtx.Config); err != nil { return err } fxtypes.SetChainId(genesisDoc.ChainID) @@ -79,7 +80,7 @@ func StartCmd(appCreator types.AppCreator, defaultNodeHome string) *cobra.Comman return startCmd } -func checkMainnetAndBlock(genesisDoc *tmtypes.GenesisDoc, config *tmcfg.Config) error { +func checkMainnetAndBlock(genesisDoc *tmtypes.GenesisDoc, genesisHash string, config *tmcfg.Config) error { if genesisDoc.InitialHeight > 1 || genesisDoc.ChainID != fxtypes.MainnetChainId || config.StateSync.Enable { return nil } @@ -96,8 +97,7 @@ func checkMainnetAndBlock(genesisDoc *tmtypes.GenesisDoc, config *tmcfg.Config) }() blockStore := store.NewBlockStore(blockStoreDB) if genesisDoc.GenesisTime.Equal(genesisTime) { - genesisBytes, _ := tmjson.Marshal(genesisDoc) - if fxtypes.Sha256Hex(genesisBytes) != fxtypes.MainnetGenesisHash { + if genesisHash != fxtypes.MainnetGenesisHash { return nil } if blockStore.Height() < fxtypes.MainnetBlockHeightV2 { @@ -124,7 +124,8 @@ func checkMainnetAndBlock(genesisDoc *tmtypes.GenesisDoc, config *tmcfg.Config) return errors.New("invalid version: The current block height is less than the v7.5.0 upgrade height(16_838_000)," + " please use the v6.x.x version to synchronize the block or download the latest snapshot") } - return errors.New("invalid version: The current version is not released, please use the corresponding version") + // TODO: The line of code below must be removed before the release. + // return errors.New("invalid version: The current version is not released, please use the corresponding version") } return nil } diff --git a/types/version.go b/types/version.go index 718cbd24..b40c8b9e 100644 --- a/types/version.go +++ b/types/version.go @@ -1,11 +1,8 @@ package types import ( - "crypto/sha256" - "encoding/hex" "fmt" "math/big" - "strings" "sync" ) @@ -13,7 +10,7 @@ import ( const ( MainnetChainId = "fxcore" mainnetEvmChainID = 530 - MainnetGenesisHash = "56629F685970FEC1E35521FC943ACE9AEB2C53448544A0560E4DD5799E1A5593" + MainnetGenesisHash = "3d6cb5ebc05d42371581cd2f7bc23590e5623c3377424a89b1db982e1938fbad" MainnetBlockHeightV2 = 5_713_000 MainnetBlockHeightV3 = 8_756_000 MainnetBlockHeightV4 = 10_477_500 @@ -26,7 +23,7 @@ const ( const ( TestnetChainId = "dhobyghaut" testnetEvmChainID = 90001 - TestnetGenesisHash = "06D0A9659E1EC5B0E57E8E2E5F1B1266094808BC9B4081E1A55011FEF4586ACE" + TestnetGenesisHash = "ec2bf940c025434d1fd17e2338a60b8803900310dc71f71ea55c185b24ddba23" TestnetBlockHeightV2 = 3_418_880 TestnetBlockHeightV3 = 6_578_000 TestnetBlockHeightV4 = 8_088_000 @@ -73,10 +70,3 @@ func ChainIdWithEIP155() string { } return fmt.Sprintf("%s_%d-1", MainnetChainId, mainnetEvmChainID) } - -// Sha256Hex calculate SHA-256 hash -func Sha256Hex(b []byte) string { - sha := sha256.New() - sha.Write(b) - return strings.ToUpper(hex.EncodeToString(sha.Sum(nil))) -}