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

feat: Make voter electing staisfy finality #124

Merged
merged 14 commits into from
Oct 13, 2020
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
19 changes: 3 additions & 16 deletions consensus/state_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1924,8 +1924,7 @@ func proposeBlock(t *testing.T, cs *State, round int, vssMap map[crypto.PubKey]*
func TestStateFullRoundWithSelectedVoter(t *testing.T) {
cs, vss := randStateWithVoterParams(10, &types.VoterParams{
VoterElectionThreshold: 5,
MaxTolerableByzantinePercentage: 20,
ElectionPrecision: 2})
MaxTolerableByzantinePercentage: 20})
vss[0].Height = 1 // this is needed because of `incrementHeight(vss[1:]...)` of randStateWithVoterParams()
vssMap := makeVssMap(vss)
height, round := cs.Height, cs.Round
Expand Down Expand Up @@ -2014,8 +2013,7 @@ func TestStateBadVoterWithSelectedVoter(t *testing.T) {
// making him having 1/3 + 1 voting power of total
cs, vss := randStateWithVoterParams(9, &types.VoterParams{
VoterElectionThreshold: 5,
MaxTolerableByzantinePercentage: 20,
ElectionPrecision: 5})
MaxTolerableByzantinePercentage: 20})

assert.True(t, cs.Voters.Size() >= 4)

Expand Down Expand Up @@ -2154,8 +2152,7 @@ func TestStateAllVoterToSelectedVoter(t *testing.T) {
startValidators := 5
cs, vss := randStateWithVoterParamsWithApp(startValidators, &types.VoterParams{
VoterElectionThreshold: int32(startValidators),
MaxTolerableByzantinePercentage: 20,
ElectionPrecision: 2},
MaxTolerableByzantinePercentage: 20},
"TestStateAllVoterToSelectedVoter")
vss[0].Height = 1 // this is needed because of `incrementHeight(vss[1:]...)` of randStateWithVoterParams()
vssMap := makeVssMap(vss)
Expand Down Expand Up @@ -2208,13 +2205,6 @@ func TestStateAllVoterToSelectedVoter(t *testing.T) {
ensureNewRound(newRoundCh, height+1, 0)

endHeight := 20
voterCount := make([]int, endHeight-1)
for i := 0; i < len(voterCount); i++ {
voterCount[i] = int(types.CalNumOfVoterToElect(int64(startValidators+i), 0.2, 0.99))
if voterCount[i] < startValidators {
voterCount[i] = startValidators
}
}
for i := 2; i <= endHeight; i++ { // height 2~10
height = cs.Height
privPubKey, _ = cs.privValidator.GetPubKey()
Expand All @@ -2229,9 +2219,6 @@ func TestStateAllVoterToSelectedVoter(t *testing.T) {
voters = cs.Voters
voterPrivVals = votersPrivVals(voters, vssMap)

// verify voters count
assert.True(t, voters.Size() == voterCount[i-2])

signAddVotes(cs, types.PrevoteType, propBlock.Hash(), propBlock.MakePartSet(types.BlockPartSizeBytes).Header(),
voterPrivVals...)

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
Loading