Skip to content

Commit

Permalink
wip: support supernova consensus client
Browse files Browse the repository at this point in the history
  • Loading branch information
zsystm committed Oct 30, 2024
1 parent ca7cd66 commit d3c0951
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 16 deletions.
38 changes: 37 additions & 1 deletion server/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -433,7 +433,7 @@ func startSupernovaNode(
privval,
nodeKey,
proxy.NewLocalClientCreator(cmtApp),
stypes.DefaultGenesisDocProviderFunc(cfg),
supernovaGenDocProvider(cfg),
cmtcfg.DefaultDBProvider,
)

Expand Down Expand Up @@ -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
Expand Down
37 changes: 37 additions & 0 deletions x/genutil/types/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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 {
Expand Down
37 changes: 22 additions & 15 deletions x/genutil/utils.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package genutil

import (
"encoding/hex"
"encoding/json"
"errors"
"fmt"
Expand All @@ -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
Expand Down Expand Up @@ -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 {
Expand All @@ -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":
Expand All @@ -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)
}
Expand All @@ -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
Expand Down

0 comments on commit d3c0951

Please sign in to comment.