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

fix: check mainnet genesis hash #667

Merged
merged 2 commits into from
Sep 2, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
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
9 changes: 4 additions & 5 deletions cmd/doctor.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package cmd

import (
"bytes"
"encoding/hex"
"encoding/json"
"fmt"
"os"
Expand All @@ -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"
Expand Down Expand Up @@ -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
}
17 changes: 9 additions & 8 deletions server/start.go
Original file line number Diff line number Diff line change
@@ -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"
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
}
Expand All @@ -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 {
Expand All @@ -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
}
14 changes: 2 additions & 12 deletions types/version.go
Original file line number Diff line number Diff line change
@@ -1,19 +1,16 @@
package types

import (
"crypto/sha256"
"encoding/hex"
"fmt"
"math/big"
"strings"
"sync"
)

// mainnet
const (
MainnetChainId = "fxcore"
mainnetEvmChainID = 530
MainnetGenesisHash = "56629F685970FEC1E35521FC943ACE9AEB2C53448544A0560E4DD5799E1A5593"
MainnetGenesisHash = "3d6cb5ebc05d42371581cd2f7bc23590e5623c3377424a89b1db982e1938fbad"
MainnetBlockHeightV2 = 5_713_000
MainnetBlockHeightV3 = 8_756_000
MainnetBlockHeightV4 = 10_477_500
Expand All @@ -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
Expand Down Expand Up @@ -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)))
}