From d3c09515720d2b7e4ce1f2ce16caf35697810c10 Mon Sep 17 00:00:00 2001 From: zsystm Date: Wed, 30 Oct 2024 16:42:40 +0900 Subject: [PATCH] wip: support supernova consensus client --- server/start.go | 38 +++++++++++++++++++++++++++++++++++++- x/genutil/types/genesis.go | 37 +++++++++++++++++++++++++++++++++++++ x/genutil/utils.go | 37 ++++++++++++++++++++++--------------- 3 files changed, 96 insertions(+), 16 deletions(-) diff --git a/server/start.go b/server/start.go index c5b0312f6756..96daff2decbf 100644 --- a/server/start.go +++ b/server/start.go @@ -433,7 +433,7 @@ func startSupernovaNode( privval, nodeKey, proxy.NewLocalClientCreator(cmtApp), - stypes.DefaultGenesisDocProviderFunc(cfg), + supernovaGenDocProvider(cfg), cmtcfg.DefaultDBProvider, ) @@ -497,6 +497,42 @@ func getGenDocProvider(cfg *cmtcfg.Config) func() (node.ChecksummedGenesisDoc, e } } +// SetupTraceWriter sets up the trace writer and returns a cleanup function. +func supernovaGenDocProvider(cfg *cmtcfg.Config) func() (stypes.ChecksummedGenesisDoc, error) { + return func() (stypes.ChecksummedGenesisDoc, error) { + defaultGenesisDoc := stypes.ChecksummedGenesisDoc{ + Sha256Checksum: []byte{}, + } + + appGenesis, err := genutiltypes.AppGenesisFromFile(cfg.GenesisFile()) + if err != nil { + return defaultGenesisDoc, err + } + + gen, err := appGenesis.ToSupernovaGenesisDoc() + if err != nil { + return defaultGenesisDoc, err + } + + genbz, err := gen.AppState.MarshalJSON() + if err != nil { + return defaultGenesisDoc, err + } + + bz, err := json.Marshal(genbz) + if err != nil { + return defaultGenesisDoc, err + } + sum := sha256.Sum256(bz) + + return stypes.ChecksummedGenesisDoc{ + GenesisDoc: gen, + Sha256Checksum: sum[:], + }, nil + } + +} + // SetupTraceWriter sets up the trace writer and returns a cleanup function. func SetupTraceWriter(logger log.Logger, traceWriterFile string) (traceWriter io.WriteCloser, cleanup func(), err error) { // clean up the traceWriter when the server is shutting down diff --git a/x/genutil/types/genesis.go b/x/genutil/types/genesis.go index 023abfba082c..4bb0e48a2430 100644 --- a/x/genutil/types/genesis.go +++ b/x/genutil/types/genesis.go @@ -13,6 +13,7 @@ import ( cmtproto "github.com/cometbft/cometbft/api/cometbft/types/v1" cmtjson "github.com/cometbft/cometbft/libs/json" cmttypes "github.com/cometbft/cometbft/types" + stypes "github.com/meterio/supernova/types" cryptocodec "github.com/cosmos/cosmos-sdk/crypto/codec" sdk "github.com/cosmos/cosmos-sdk/types" @@ -216,6 +217,42 @@ func (ag *AppGenesis) ToGenesisDoc() (*cmttypes.GenesisDoc, error) { }, nil } +// ToSupernovaGenesisDoc converts the AppGenesis to a Supernova GenesisDoc. +func (ag *AppGenesis) ToSupernovaGenesisDoc() (*stypes.GenesisDoc, error) { + validators := []stypes.GenesisValidator{} + for _, val := range ag.Consensus.Validators { + pk, err := cryptocodec.PubKeyToProto(val.PubKey) + if err != nil { + return nil, err + } + cmtPk, err := cryptocodec.ToCmtPubKeyInterface(pk) + if err != nil { + return nil, err + } + val := stypes.GenesisValidator{ + Address: val.Address.Bytes(), + PubKey: cmtPk, + Power: val.Power, + Name: val.Name, + } + + validators = append(validators, val) + } + // assert nil value for empty validators set + if len(validators) == 0 { + validators = nil + } + return &stypes.GenesisDoc{ + GenesisTime: ag.GenesisTime, + ChainID: ag.ChainID, + InitialHeight: ag.InitialHeight, + AppHash: ag.AppHash, + AppState: ag.AppState, + Validators: validators, + ConsensusParams: ag.Consensus.Params, + }, nil +} + // ConsensusGenesis defines the consensus layer's genesis. // TODO(@julienrbrt) eventually abstract from CometBFT types type ConsensusGenesis struct { diff --git a/x/genutil/utils.go b/x/genutil/utils.go index ee7d11e39e80..d3486630df04 100644 --- a/x/genutil/utils.go +++ b/x/genutil/utils.go @@ -1,6 +1,7 @@ package genutil import ( + "encoding/hex" "encoding/json" "errors" "fmt" @@ -12,14 +13,14 @@ import ( cmtcrypto "github.com/cometbft/cometbft/crypto" cmtbls12381 "github.com/cometbft/cometbft/crypto/bls12381" tmed25519 "github.com/cometbft/cometbft/crypto/ed25519" - "github.com/cometbft/cometbft/p2p" "github.com/cometbft/cometbft/privval" "github.com/cosmos/go-bip39" - cryptocodec "github.com/cosmos/cosmos-sdk/crypto/codec" cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types" sdk "github.com/cosmos/cosmos-sdk/types" "github.com/cosmos/cosmos-sdk/x/genutil/types" + + stypes "github.com/meterio/supernova/types" ) // ExportGenesisFile creates and writes the genesis configuration to disk. An @@ -63,12 +64,12 @@ func InitializeNodeValidatorFilesFromMnemonic(config *cfg.Config, mnemonic, keyT if len(mnemonic) > 0 && !bip39.IsMnemonicValid(mnemonic) { return "", nil, errors.New("invalid mnemonic") } - nodeKey, err := p2p.LoadOrGenNodeKey(config.NodeKeyFile()) + nodeKey, err := stypes.LoadOrGenNodeKey(config.NodeKeyFile()) if err != nil { return "", nil, err } - nodeID = string(nodeKey.ID()) + nodeID = string(hex.EncodeToString(nodeKey.Bytes)) pvKeyFile := config.PrivValidatorKeyFile() if err := os.MkdirAll(filepath.Dir(pvKeyFile), 0o777); err != nil { @@ -85,6 +86,8 @@ func InitializeNodeValidatorFilesFromMnemonic(config *cfg.Config, mnemonic, keyT privKey cmtcrypto.PrivKey ) + // POC: test bls key + keyType = "bls" if len(mnemonic) == 0 { switch keyType { case "ed25519": @@ -95,6 +98,10 @@ func InitializeNodeValidatorFilesFromMnemonic(config *cfg.Config, mnemonic, keyT return "", nil, err } filePV = loadOrGenFilePV(privKey, pvKeyFile, pvStateFile) + case "bls": + privKey := stypes.NewBlsPrivKey() + filePV = privval.NewFilePV(privKey, pvKeyFile, pvStateFile) + filePV.Save() default: filePV = loadOrGenFilePV(tmed25519.GenPrivKey(), pvKeyFile, pvStateFile) } @@ -112,17 +119,17 @@ func InitializeNodeValidatorFilesFromMnemonic(config *cfg.Config, mnemonic, keyT filePV.Save() } - tmValPubKey, err := filePV.GetPubKey() - if err != nil { - return "", nil, err - } - - valPubKey, err = cryptocodec.FromCmtPubKeyInterface(tmValPubKey) - if err != nil { - return "", nil, err - } - - return nodeID, valPubKey, nil + // POC: Don't need to return valPubKey for init + //tmValPubKey, err := filePV.GetPubKey() + //if err != nil { + // return "", nil, err + //} + // + //valPubKey, err = cryptocodec.FromCmtPubKeyInterface(tmValPubKey) + //if err != nil { + // return "", nil, err + //} + return nodeID, nil, nil } // loadOrGenFilePV loads a FilePV from the given filePaths