Skip to content

Commit

Permalink
Merge pull request #125 from line/feat/select_val_key
Browse files Browse the repository at this point in the history
feat: add an option for selecting priv key type
  • Loading branch information
Woosang Son authored Oct 12, 2020
2 parents f3a9ff1 + 0f9fe21 commit c392d08
Show file tree
Hide file tree
Showing 20 changed files with 141 additions and 61 deletions.
2 changes: 1 addition & 1 deletion .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -372,7 +372,7 @@ jobs:
# Build Tendermint
make build-linux
# Build contract-tests
docker run --rm -v "$PWD":/go/src/github.com/tendermint/tendermint -w /go/src/github.com/tendermint/tendermint ubuntu:20.10 ./scripts/prepare_dredd_test.sh
docker run --rm -v "$PWD":/go/src/github.com/tendermint/tendermint -w /go/src/github.com/tendermint/tendermint ubuntu:19.10 ./scripts/prepare_dredd_test.sh
# This docker image works with go 1.7, we can install here the hook handler that contract-tests is going to use
go get github.com/snikch/goodman/cmd/goodman
make contract-tests
Expand Down
1 change: 1 addition & 0 deletions CHANGELOG_PENDING.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
- [consensus] [\#101](https://github.com/line/tendermint/pull/101) Introduce composite key to delegate features to each function key

### FEATURES:
- [init command] [\#125](https://github.com/line/tendermint/pull/125) Add an option selecting private key type to init, testnet commands

### IMPROVEMENTS:

Expand Down
18 changes: 1 addition & 17 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -232,12 +232,8 @@ DOCKER_CMD = docker run --rm \
-w $(DOCKER_HOME)
DOCKER_IMG = golang:1.14.6-alpine3.12
BUILD_CMD = apk add --update --no-cache git make gcc libc-dev build-base curl jq file gmp-dev clang \
&& cd crypto/bls/internal/bls-eth-go-binary \
&& make CXX=clang++ \
&& cd $(DOCKER_HOME) \
&& go mod edit -replace github.com/herumi/bls-eth-go-binary=./crypto/bls/internal/bls-eth-go-binary \
&& make build \
&& go mod edit -dropreplace github.com/herumi/bls-eth-go-binary
&& make build

# Login docker-container for confirmation building linux binary
build-shell:
Expand All @@ -247,21 +243,9 @@ build-shell:
# Build linux binary on other platforms

build-linux:
# Download, build and add the BSL local library to modules
if [ ! -d $(SRCPATH)/crypto/bls/internal/bls-eth-go-binary ]; then \
mkdir -p $(SRCPATH)/crypto/bls/internal && \
git clone https://github.com/herumi/mcl $(SRCPATH)/crypto/bls/internal/mcl && \
git clone https://github.com/herumi/bls $(SRCPATH)/crypto/bls/internal/bls && \
git clone https://github.com/herumi/bls-eth-go-binary $(SRCPATH)/crypto/bls/internal/bls-eth-go-binary; \
fi

# Build Linux binary
$(DOCKER_CMD) ${DOCKER_IMG} /bin/sh -c "$(BUILD_CMD)"

# Remove the BLS local library from modules
rm -rf $(SRCPATH)/crypto/bls/internal/mcl
rm -rf $(SRCPATH)/crypto/bls/internal/bls
rm -rf $(SRCPATH)/crypto/bls/internal/bls-eth-go-binary
.PHONY: build-linux

build-docker-localnode:
Expand Down
2 changes: 1 addition & 1 deletion cmd/tendermint/commands/gen_validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ var GenValidatorCmd = &cobra.Command{
}

func genValidator(cmd *cobra.Command, args []string) {
pv := privval.GenFilePV("", "")
pv, _ := privval.GenFilePV("", "", privval.PrevKeyTypeEd25519)
jsbz, err := cdc.MarshalJSON(pv)
if err != nil {
panic(err)
Expand Down
30 changes: 23 additions & 7 deletions cmd/tendermint/commands/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,20 @@ import (
tmtime "github.com/tendermint/tendermint/types/time"
)

// InitFilesCmd initialises a fresh Tendermint Core instance.
var InitFilesCmd = &cobra.Command{
Use: "init",
Short: "Initialize Tendermint",
RunE: initFiles,
func NewInitCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "init",
Short: "Initialize Tendermint",
RunE: initFiles,
}

AddInitFlags(cmd)
return cmd
}

func AddInitFlags(cmd *cobra.Command) {
cmd.Flags().String("priv-key-type", config.PrivKeyType,
"Specify validator's private key type (ed25519 | composite)")
}

func initFiles(cmd *cobra.Command, args []string) error {
Expand All @@ -30,14 +39,21 @@ func initFilesWithConfig(config *cfg.Config) error {
// private validator
privValKeyFile := config.PrivValidatorKeyFile()
privValStateFile := config.PrivValidatorStateFile()
privKeyType := config.PrivValidatorKeyType()
var pv *privval.FilePV
if tmos.FileExists(privValKeyFile) {
pv = privval.LoadFilePV(privValKeyFile, privValStateFile)
logger.Info("Found private validator", "keyFile", privValKeyFile,
"stateFile", privValStateFile)
} else {
pv = privval.GenFilePV(privValKeyFile, privValStateFile)
pv.Save()
var err error
pv, err = privval.GenFilePV(privValKeyFile, privValStateFile, privKeyType)
if err != nil {
return err
}
if pv != nil {
pv.Save()
}
logger.Info("Generated private validator", "keyFile", privValKeyFile,
"stateFile", privValStateFile)
}
Expand Down
17 changes: 11 additions & 6 deletions cmd/tendermint/commands/reset_priv_validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,18 +35,19 @@ var ResetPrivValidatorCmd = &cobra.Command{
// it's only suitable for testnets.
func resetAll(cmd *cobra.Command, args []string) {
ResetAll(config.DBDir(), config.P2P.AddrBookFile(), config.PrivValidatorKeyFile(),
config.PrivValidatorStateFile(), logger)
config.PrivValidatorKeyType(), config.PrivValidatorStateFile(), logger)
}

// XXX: this is totally unsafe.
// it's only suitable for testnets.
func resetPrivValidator(cmd *cobra.Command, args []string) {
resetFilePV(config.PrivValidatorKeyFile(), config.PrivValidatorStateFile(), logger)
resetFilePV(config.PrivValidatorKeyFile(), config.PrivValidatorStateFile(),
config.PrivValidatorKeyType(), logger)
}

// ResetAll removes address book files plus all data, and resets the privValdiator data.
// Exported so other CLI tools can use it.
func ResetAll(dbDir, addrBookFile, privValKeyFile, privValStateFile string, logger log.Logger) {
func ResetAll(dbDir, addrBookFile, privValKeyFile, privValStateFile, privKeyType string, logger log.Logger) error {
if keepAddrBook {
logger.Info("The address book remains intact")
} else {
Expand All @@ -59,21 +60,25 @@ func ResetAll(dbDir, addrBookFile, privValKeyFile, privValStateFile string, logg
}
// recreate the dbDir since the privVal state needs to live there
tmos.EnsureDir(dbDir, 0700)
resetFilePV(privValKeyFile, privValStateFile, logger)
return resetFilePV(privValKeyFile, privValStateFile, privKeyType, logger)
}

func resetFilePV(privValKeyFile, privValStateFile string, logger log.Logger) {
func resetFilePV(privValKeyFile, privValStateFile, privKeyType string, logger log.Logger) error {
if _, err := os.Stat(privValKeyFile); err == nil {
pv := privval.LoadFilePVEmptyState(privValKeyFile, privValStateFile)
pv.Reset()
logger.Info("Reset private validator file to genesis state", "keyFile", privValKeyFile,
"stateFile", privValStateFile)
} else {
pv := privval.GenFilePV(privValKeyFile, privValStateFile)
pv, err := privval.GenFilePV(privValKeyFile, privValStateFile, privKeyType)
if err != nil {
return err
}
pv.Save()
logger.Info("Generated private validator file", "keyFile", privValKeyFile,
"stateFile", privValStateFile)
}
return nil
}

func removeAddrBook(addrBookFile string, logger log.Logger) {
Expand Down
4 changes: 4 additions & 0 deletions cmd/tendermint/commands/testnet.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ var (
hostnames []string
p2pPort int
randomMonikers bool
privKeyType string
)

const (
Expand Down Expand Up @@ -72,6 +73,8 @@ func init() {
"P2P Port")
TestnetFilesCmd.Flags().BoolVar(&randomMonikers, "random-monikers", false,
"Randomize the moniker for each generated node")
TestnetFilesCmd.Flags().StringVar(&privKeyType, "priv-key-type", privval.PrevKeyTypeEd25519,
"Specify validator's private key type (ed25519 | composite)")
}

// TestnetFilesCmd allows initialisation of files for a Tendermint testnet.
Expand Down Expand Up @@ -134,6 +137,7 @@ func testnetFiles(cmd *cobra.Command, args []string) error {
return err
}

config.PrivKeyType = privKeyType
initFilesWithConfig(config)

pvKeyFile := filepath.Join(nodeDir, config.BaseConfig.PrivValidatorKey)
Expand Down
2 changes: 1 addition & 1 deletion cmd/tendermint/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ func main() {
rootCmd := cmd.RootCmd
rootCmd.AddCommand(
cmd.GenValidatorCmd,
cmd.InitFilesCmd,
cmd.ProbeUpnpCmd,
cmd.LiteCmd,
cmd.ReplayCmd,
Expand All @@ -42,6 +41,7 @@ func main() {
nodeFunc := nm.DefaultNewNode

// Create & start node
rootCmd.AddCommand(cmd.NewInitCmd())
rootCmd.AddCommand(cmd.NewRunNodeCmd(nodeFunc))

cmd := cli.PrepareBaseCmd(rootCmd, "TM", os.ExpandEnv(filepath.Join("$HOME", cfg.DefaultTendermintDir)))
Expand Down
9 changes: 9 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"time"

"github.com/pkg/errors"
"github.com/tendermint/tendermint/privval"
)

const (
Expand Down Expand Up @@ -212,6 +213,9 @@ type BaseConfig struct { //nolint: maligned
// If true, query the ABCI app on connecting to a new peer
// so the app can decide if we should keep the connection or not
FilterPeers bool `mapstructure:"filter_peers"` // false

// Specify validator's private key type
PrivKeyType string `mapstructure:"priv-key-type"`
}

// DefaultBaseConfig returns a default base configuration for a Tendermint node
Expand All @@ -231,6 +235,7 @@ func DefaultBaseConfig() BaseConfig {
FilterPeers: false,
DBBackend: "goleveldb",
DBPath: "data",
PrivKeyType: privval.PrevKeyTypeEd25519,
}
}

Expand Down Expand Up @@ -273,6 +278,10 @@ func (cfg BaseConfig) DBDir() string {
return rootify(cfg.DBPath, cfg.RootDir)
}

func (cfg BaseConfig) PrivValidatorKeyType() string {
return cfg.PrivKeyType
}

// ValidateBasic performs basic validation (checking param bounds, etc.) and
// returns an error if any check fails.
func (cfg BaseConfig) ValidateBasic() error {
Expand Down
5 changes: 3 additions & 2 deletions consensus/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -419,7 +419,8 @@ func loadPrivValidator(config *cfg.Config) *privval.FilePV {
privValidatorKeyFile := config.PrivValidatorKeyFile()
ensureDir(filepath.Dir(privValidatorKeyFile), 0700)
privValidatorStateFile := config.PrivValidatorStateFile()
privValidator := privval.LoadOrGenFilePV(privValidatorKeyFile, privValidatorStateFile)
privKeyType := config.PrivValidatorKeyType()
privValidator, _ := privval.LoadOrGenFilePV(privValidatorKeyFile, privValidatorStateFile, privKeyType)
privValidator.Reset()
return privValidator
}
Expand Down Expand Up @@ -802,7 +803,7 @@ func randConsensusNetWithPeers(
panic(err)
}

privVal = privval.GenFilePV(tempKeyFile.Name(), tempStateFile.Name())
privVal, _ = privval.GenFilePV(tempKeyFile.Name(), tempStateFile.Name(), privval.PrevKeyTypeEd25519)
}

app := appFunc(path.Join(config.DBDir(), fmt.Sprintf("%s_%d", testName, i)))
Expand Down
6 changes: 5 additions & 1 deletion consensus/wal_generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,11 @@ func WALGenerateNBlocks(t *testing.T, wr io.Writer, numBlocks int) (err error) {
// NOTE: we don't do handshake so need to set state.Version.Consensus.App directly.
privValidatorKeyFile := config.PrivValidatorKeyFile()
privValidatorStateFile := config.PrivValidatorStateFile()
privValidator := privval.LoadOrGenFilePV(privValidatorKeyFile, privValidatorStateFile)
privKeyType := config.PrivValidatorKeyType()
privValidator, err := privval.LoadOrGenFilePV(privValidatorKeyFile, privValidatorStateFile, privKeyType)
if err != nil {
return errors.Wrap(err, "failed to create a private key")
}
genDoc, err := types.GenesisDocFromFile(config.GenesisFile())
if err != nil {
return errors.Wrap(err, "failed to read genesis file")
Expand Down
15 changes: 15 additions & 0 deletions docker_push.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#!/bin/bash

make build-docker
LINE_VERSION="$(awk -F\" '/LINECoreSemVer =/ {print $2; exit }' < ./version/version.go)"
GIT_COMMIT="$(git rev-parse --short=8 HEAD)"
TAG=docker-registry.linecorp.com/link-network/tendermint:latest
docker tag tendermint/tendermint:latest $TAG

read -p "==> Do you push docker image to [$TAG]? (y/n) " -n 1 -r
echo
if [[ $REPLY =~ ^[Yy]$ ]]
then
docker push $TAG
fi

4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ require (
github.com/golang/protobuf v1.4.0
github.com/gorilla/websocket v1.4.2
github.com/gtank/merlin v0.1.1
github.com/herumi/bls v0.0.0-20200721070902-5e2af1489a06
github.com/herumi/bls-eth-go-binary v0.0.0-20200522010937-01d282b5380b
github.com/herumi/bls v0.0.0-20200904110701-e4663751b56f
github.com/herumi/bls-eth-go-binary v0.0.0-20200923072303-32b29e5d8cbf
github.com/libp2p/go-buffer-pool v0.0.2
github.com/magiconair/properties v1.8.1
github.com/minio/highwayhash v1.0.0
Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -217,8 +217,12 @@ github.com/hashicorp/memberlist v0.1.3/go.mod h1:ajVTdAv/9Im8oMAAj5G31PhhMCZJV2p
github.com/hashicorp/serf v0.8.2/go.mod h1:6hOLApaqBFA1NXqRQAsxw9QxuDEvNxSQRwA/JwenrHc=
github.com/herumi/bls v0.0.0-20200721070902-5e2af1489a06 h1:4w5HyoC9FiD1UcO9j6MBkBIeBaLTe8JJOYmSiIFlo8c=
github.com/herumi/bls v0.0.0-20200721070902-5e2af1489a06/go.mod h1:i4wRNUUFF1nNmYFHM9UDl13MGoxEQkMVCLAd82qZz4s=
github.com/herumi/bls v0.0.0-20200904110701-e4663751b56f h1:MWNZl9RnZ8aDlChxHjlbNXAqFpwNPdLwoxJpV5LXUmw=
github.com/herumi/bls v0.0.0-20200904110701-e4663751b56f/go.mod h1:i4wRNUUFF1nNmYFHM9UDl13MGoxEQkMVCLAd82qZz4s=
github.com/herumi/bls-eth-go-binary v0.0.0-20200522010937-01d282b5380b h1:mu+F5uA3Y68oB6KXZqWlASKMetbNufhQx2stMI+sD+Y=
github.com/herumi/bls-eth-go-binary v0.0.0-20200522010937-01d282b5380b/go.mod h1:luAnRm3OsMQeokhGzpYmc0ZKwawY7o87PUEP11Z7r7U=
github.com/herumi/bls-eth-go-binary v0.0.0-20200923072303-32b29e5d8cbf h1:Lw7EOMVxu3O+7Ro5bqn9M20a7GwuCqZQsmdXNzmcKE4=
github.com/herumi/bls-eth-go-binary v0.0.0-20200923072303-32b29e5d8cbf/go.mod h1:luAnRm3OsMQeokhGzpYmc0ZKwawY7o87PUEP11Z7r7U=
github.com/hpcloud/tail v1.0.0 h1:nfCOvKYfkgYP8hkirhJocXT2+zOD8yUNjXaWfTlyFKI=
github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU=
github.com/hudl/fargo v1.3.0/go.mod h1:y3CKSmjA+wD2gak7sUSXTAoopbhU08POFhmITJgmKTg=
Expand Down
12 changes: 10 additions & 2 deletions node/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,16 @@ func DefaultNewNode(config *cfg.Config, logger log.Logger) (*Node, error) {
return nil, fmt.Errorf("failed to load or gen node key %s: %w", config.NodeKeyFile(), err)
}

return NewNode(config,
privval.LoadOrGenFilePV(config.PrivValidatorKeyFile(), config.PrivValidatorStateFile()),
privKey, err := privval.LoadOrGenFilePV(
config.PrivValidatorKeyFile(),
config.PrivValidatorStateFile(),
config.PrivValidatorKeyType())
if err != nil {
return nil, errors.Wrap(err, "failed to create a private key")
}
return NewNode(
config,
privKey,
nodeKey,
proxy.DefaultClientCreator(config.ProxyApp, config.ABCI, config.DBDir()),
DefaultGenesisDocProviderFunc(config),
Expand Down
4 changes: 3 additions & 1 deletion node/node_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -305,8 +305,10 @@ func TestNodeNewNodeCustomReactors(t *testing.T) {
nodeKey, err := p2p.LoadOrGenNodeKey(config.NodeKeyFile())
require.NoError(t, err)

pvKey, _ := privval.LoadOrGenFilePV(config.PrivValidatorKeyFile(), config.PrivValidatorStateFile(),
config.PrivValidatorKeyType())
n, err := NewNode(config,
privval.LoadOrGenFilePV(config.PrivValidatorKeyFile(), config.PrivValidatorStateFile()),
pvKey,
nodeKey,
proxy.DefaultClientCreator(config.ProxyApp, config.ABCI, config.DBDir()),
DefaultGenesisDocProviderFunc(config),
Expand Down
Loading

0 comments on commit c392d08

Please sign in to comment.