Refactor priv_validator

Users can now just pass an object that implements the Signer interface.
This commit is contained in:
Adrian Brink
2017-09-13 00:18:07 +02:00
committed by Ethan Buchman
parent 0d392a0442
commit 7dd3c007c7
16 changed files with 163 additions and 176 deletions

View File

@ -1,47 +0,0 @@
package main
import (
tcrypto "github.com/tendermint/go-crypto"
tc "github.com/tendermint/tendermint/cmd/tendermint/commands"
cfg "github.com/tendermint/tendermint/config"
"github.com/tendermint/tendermint/types"
"github.com/tendermint/tmlibs/cli"
"github.com/tendermint/tmlibs/log"
"os"
)
var (
config = cfg.DefaultConfig()
logger = log.NewTMLogger(log.NewSyncWriter(os.Stdout)).With("module", "main")
)
func main() {
// TODO: Make it easier to build a tendermint instance from scratch.
// All commands should be exported and it should be easy to override
// certain aspects of a single command.
// Probably every command should have a constructor that allows a user
// to vary the configuration. This is at least true for run_node.go
rootCmd := tc.RootCmd
rootCmd.AddCommand(tc.GenValidatorCmd)
rootCmd.AddCommand(tc.InitFilesCmd)
rootCmd.AddCommand(tc.ProbeUpnpCmd)
rootCmd.AddCommand(tc.ReplayCmd)
rootCmd.AddCommand(tc.ReplayConsoleCmd)
rootCmd.AddCommand(tc.ResetAllCmd)
rootCmd.AddCommand(tc.ResetPrivValidatorCmd)
rootCmd.AddCommand(tc.ShowValidatorCmd)
rootCmd.AddCommand(tc.TestnetFilesCmd)
rootCmd.AddCommand(tc.VersionCmd)
signerGenerator := func(pk tcrypto.PrivKey) types.Signer {
// Return your own signer implementation here
return types.NewDefaultSigner(pk)
}
privValidator := types.LoadPrivValidatorWithSigner(config.PrivValidatorFile(), signerGenerator)
rootCmd.AddCommand(tc.NewRunNodeCmd(privValidator))
cmd := cli.PrepareBaseCmd(rootCmd, "TM", os.ExpandEnv("$HOME/.tendermint"))
cmd.Execute()
}

View File

@ -9,6 +9,8 @@ import (
"github.com/tendermint/tendermint/types" "github.com/tendermint/tendermint/types"
) )
// GenValidatorCmd allows the generation of a keypair for a
// validator.
var GenValidatorCmd = &cobra.Command{ var GenValidatorCmd = &cobra.Command{
Use: "gen_validator", Use: "gen_validator",
Short: "Generate new validator keypair", Short: "Generate new validator keypair",

View File

@ -9,6 +9,7 @@ import (
cmn "github.com/tendermint/tmlibs/common" cmn "github.com/tendermint/tmlibs/common"
) )
// InitFilesCmd initialises a fresh Tendermint Core instance.
var InitFilesCmd = &cobra.Command{ var InitFilesCmd = &cobra.Command{
Use: "init", Use: "init",
Short: "Initialize Tendermint", Short: "Initialize Tendermint",

View File

@ -9,6 +9,7 @@ import (
"github.com/tendermint/tendermint/p2p/upnp" "github.com/tendermint/tendermint/p2p/upnp"
) )
// ProbeUpnpCmd adds capabilities to test the UPnP functionality.
var ProbeUpnpCmd = &cobra.Command{ var ProbeUpnpCmd = &cobra.Command{
Use: "probe_upnp", Use: "probe_upnp",
Short: "Test UPnP functionality", Short: "Test UPnP functionality",

View File

@ -6,6 +6,7 @@ import (
"github.com/tendermint/tendermint/consensus" "github.com/tendermint/tendermint/consensus"
) )
// ReplayCmd allows replaying of messages from the WAL.
var ReplayCmd = &cobra.Command{ var ReplayCmd = &cobra.Command{
Use: "replay", Use: "replay",
Short: "Replay messages from WAL", Short: "Replay messages from WAL",
@ -14,6 +15,8 @@ var ReplayCmd = &cobra.Command{
}, },
} }
// ReplayConsoleCmd allows replaying of messages from the WAL in a
// console.
var ReplayConsoleCmd = &cobra.Command{ var ReplayConsoleCmd = &cobra.Command{
Use: "replay_console", Use: "replay_console",
Short: "Replay messages from WAL in a console", Short: "Replay messages from WAL in a console",

View File

@ -9,18 +9,29 @@ import (
"github.com/tendermint/tmlibs/log" "github.com/tendermint/tmlibs/log"
) )
// ResetAllCmd removes the database of this Tendermint core
// instance.
var ResetAllCmd = &cobra.Command{ var ResetAllCmd = &cobra.Command{
Use: "unsafe_reset_all", Use: "unsafe_reset_all",
Short: "(unsafe) Remove all the data and WAL, reset this node's validator", Short: "(unsafe) Remove all the data and WAL, reset this node's validator",
Run: resetAll, Run: resetAll,
} }
// ResetPrivValidatorCmd resets the private validator files.
var ResetPrivValidatorCmd = &cobra.Command{ var ResetPrivValidatorCmd = &cobra.Command{
Use: "unsafe_reset_priv_validator", Use: "unsafe_reset_priv_validator",
Short: "(unsafe) Reset this node's validator", Short: "(unsafe) Reset this node's validator",
Run: resetPrivValidator, Run: resetPrivValidator,
} }
// ResetAll removes the privValidator files.
// Exported so other CLI tools can use it
func ResetAll(dbDir, privValFile string, logger log.Logger) {
resetPrivValidatorLocal(privValFile, logger)
os.RemoveAll(dbDir)
logger.Info("Removed all data", "dir", dbDir)
}
// XXX: this is totally unsafe. // XXX: this is totally unsafe.
// it's only suitable for testnets. // it's only suitable for testnets.
func resetAll(cmd *cobra.Command, args []string) { func resetAll(cmd *cobra.Command, args []string) {
@ -33,13 +44,6 @@ func resetPrivValidator(cmd *cobra.Command, args []string) {
resetPrivValidatorLocal(config.PrivValidatorFile(), logger) resetPrivValidatorLocal(config.PrivValidatorFile(), logger)
} }
// Exported so other CLI tools can use it
func ResetAll(dbDir, privValFile string, logger log.Logger) {
resetPrivValidatorLocal(privValFile, logger)
os.RemoveAll(dbDir)
logger.Info("Removed all data", "dir", dbDir)
}
func resetPrivValidatorLocal(privValFile string, logger log.Logger) { func resetPrivValidatorLocal(privValFile string, logger log.Logger) {
// Get PrivValidator // Get PrivValidator
var privValidator *types.PrivValidator var privValidator *types.PrivValidator

View File

@ -34,6 +34,7 @@ func ParseConfig() (*cfg.Config, error) {
return conf, err return conf, err
} }
// RootCmd is the root command for Tendermint core.
var RootCmd = &cobra.Command{ var RootCmd = &cobra.Command{
Use: "tendermint", Use: "tendermint",
Short: "Tendermint Core (BFT Consensus) in Go", Short: "Tendermint Core (BFT Consensus) in Go",

View File

@ -12,52 +12,6 @@ import (
"github.com/tendermint/tendermint/types" "github.com/tendermint/tendermint/types"
) )
// RunNodeCmd creates and starts a tendermint node.
var RunNodeCmd = &cobra.Command{
Use: "node",
Short: "Run the tendermint node",
RunE: runNode,
}
// NewRunNodeCmd creates and starts a tendermint node. It allows the user to
// use a custom PrivValidator.
func NewRunNodeCmd(privVal *types.PrivValidator) *cobra.Command {
return &cobra.Command{
Use: "node",
Short: "Run the tendermint node",
RunE: func(cmd *cobra.Command, args []string) error {
// Wait until the genesis doc becomes available
// This is for Mintnet compatibility.
// TODO: If Mintnet gets deprecated or genesis_file is
// always available, remove.
genDocFile := config.GenesisFile()
for !cmn.FileExists(genDocFile) {
logger.Info(cmn.Fmt("Waiting for genesis file %v...", genDocFile))
time.Sleep(time.Second)
}
genDoc, err := types.GenesisDocFromFile(genDocFile)
if err != nil {
return err
}
config.ChainID = genDoc.ChainID
// Create & start node
n := node.NewNode(config, privVal, proxy.DefaultClientCreator(config.ProxyApp, config.ABCI, config.DBDir()), logger)
if _, err := n.Start(); err != nil {
return fmt.Errorf("Failed to start node: %v", err)
} else {
logger.Info("Started node", "nodeInfo", n.Switch().NodeInfo())
}
// Trap signal, run forever.
n.RunForever()
return nil
},
}
}
func init() { func init() {
AddNodeFlags(RunNodeCmd) AddNodeFlags(RunNodeCmd)
} }
@ -90,6 +44,57 @@ func AddNodeFlags(cmd *cobra.Command) {
cmd.Flags().Bool("consensus.create_empty_blocks", config.Consensus.CreateEmptyBlocks, "Set this to false to only produce blocks when there are txs or when the AppHash changes") cmd.Flags().Bool("consensus.create_empty_blocks", config.Consensus.CreateEmptyBlocks, "Set this to false to only produce blocks when there are txs or when the AppHash changes")
} }
// RunNodeCmd creates and starts a tendermint node.
var RunNodeCmd = &cobra.Command{
Use: "node",
Short: "Run the tendermint node",
RunE: runNode,
}
// NewRunNodeCmd returns the command that allows the CLI to start a
// node. It can be used with a custom PrivValidator.
func NewRunNodeCmd(privVal *types.PrivValidator) *cobra.Command {
return &cobra.Command{
Use: "node",
Short: "Run the tendermint node",
RunE: func(cmd *cobra.Command, args []string) error {
// Wait until the genesis doc becomes available
// This is for Mintnet compatibility.
// TODO: If Mintnet gets deprecated or genesis_file is
// always available, remove.
genDocFile := config.GenesisFile()
for !cmn.FileExists(genDocFile) {
logger.Info(cmn.Fmt("Waiting for genesis file %v...", genDocFile))
time.Sleep(time.Second)
}
genDoc, err := types.GenesisDocFromFile(genDocFile)
if err != nil {
return err
}
config.ChainID = genDoc.ChainID
// Create & start node
var n *node.Node
if privVal == nil {
n = node.NewNodeDefault(config, logger.With("module", "node"))
}
n = node.NewNode(config, privVal, proxy.DefaultClientCreator(config.ProxyApp, config.ABCI, config.DBDir()), logger)
if _, err := n.Start(); err != nil {
return fmt.Errorf("Failed to start node: %v", err)
} else {
logger.Info("Started node", "nodeInfo", n.Switch().NodeInfo())
}
// Trap signal, run forever.
n.RunForever()
return nil
},
}
}
// Users wishing to: // Users wishing to:
// * Use an external signer for their validators // * Use an external signer for their validators
// * Supply an in-proc abci app // * Supply an in-proc abci app

View File

@ -9,6 +9,7 @@ import (
"github.com/tendermint/tendermint/types" "github.com/tendermint/tendermint/types"
) )
// ShowValidatorCmd adds capabilities for showing the validator info.
var ShowValidatorCmd = &cobra.Command{ var ShowValidatorCmd = &cobra.Command{
Use: "show_validator", Use: "show_validator",
Short: "Show this node's validator info", Short: "Show this node's validator info",
@ -16,7 +17,7 @@ var ShowValidatorCmd = &cobra.Command{
} }
func showValidator(cmd *cobra.Command, args []string) { func showValidator(cmd *cobra.Command, args []string) {
privValidator := types.LoadOrGenPrivValidator(config.PrivValidatorFile(), logger) privValidator := types.LoadOrGenPrivValidator(config.PrivValidatorFile())
pubKeyJSONBytes, _ := data.ToJSON(privValidator.PubKey) pubKeyJSONBytes, _ := data.ToJSON(privValidator.PubKey)
fmt.Println(string(pubKeyJSONBytes)) fmt.Println(string(pubKeyJSONBytes))
} }

View File

@ -11,12 +11,6 @@ import (
cmn "github.com/tendermint/tmlibs/common" cmn "github.com/tendermint/tmlibs/common"
) )
var TestnetFilesCmd = &cobra.Command{
Use: "testnet",
Short: "Initialize files for a Tendermint testnet",
Run: testnetFiles,
}
//flags //flags
var ( var (
nValidators int nValidators int
@ -30,6 +24,14 @@ func init() {
"Directory to store initialization data for the testnet") "Directory to store initialization data for the testnet")
} }
// TestnetFilesCmd allows initialisation of files for a
// Tendermint testnet.
var TestnetFilesCmd = &cobra.Command{
Use: "testnet",
Short: "Initialize files for a Tendermint testnet",
Run: testnetFiles,
}
func testnetFiles(cmd *cobra.Command, args []string) { func testnetFiles(cmd *cobra.Command, args []string) {
genVals := make([]types.GenesisValidator, nValidators) genVals := make([]types.GenesisValidator, nValidators)

View File

@ -8,6 +8,7 @@ import (
"github.com/tendermint/tendermint/version" "github.com/tendermint/tendermint/version"
) )
// VersionCmd ...
var VersionCmd = &cobra.Command{ var VersionCmd = &cobra.Command{
Use: "version", Use: "version",
Short: "Show version infoooooooo", Short: "Show version infoooooooo",

View File

@ -3,11 +3,29 @@ package main
import ( import (
"os" "os"
"github.com/tendermint/tendermint/cmd/tendermint/commands" // crypto "github.com/tendermint/go-crypto"
"github.com/tendermint/tmlibs/cli" "github.com/tendermint/tmlibs/cli"
. "github.com/tendermint/tendermint/cmd/tendermint/commands"
// "github.com/tendermint/tendermint/types"
) )
func main() { func main() {
cmd := cli.PrepareBaseCmd(commands.RootCmd, "TM", os.ExpandEnv("$HOME/.tendermint")) rootCmd := RootCmd
rootCmd.AddCommand(GenValidatorCmd, InitFilesCmd, ProbeUpnpCmd,
ReplayCmd, ReplayConsoleCmd, ResetAllCmd, ResetPrivValidatorCmd,
ShowValidatorCmd, TestnetFilesCmd, VersionCmd)
// NOTE: Implement your own type that implements the Signer interface
// and then instantiate it here.
// signer := types.NewDefaultSigner(pk)
// privValidator := types.LoadPrivValidatorWithSigner(signer)
// rootCmd.AddCommand(NewRunNodeCmd(privValidator))
// Create & start node
rootCmd.AddCommand(RunNodeCmd)
cmd := cli.PrepareBaseCmd(rootCmd, "TM", os.ExpandEnv("$HOME/.tendermint"))
cmd.Execute() cmd.Execute()
} }

View File

@ -261,7 +261,7 @@ func newConsensusStateWithConfig(thisConfig *cfg.Config, state *sm.State, pv *ty
func loadPrivValidator(config *cfg.Config) *types.PrivValidator { func loadPrivValidator(config *cfg.Config) *types.PrivValidator {
privValidatorFile := config.PrivValidatorFile() privValidatorFile := config.PrivValidatorFile()
ensureDir(path.Dir(privValidatorFile), 0700) ensureDir(path.Dir(privValidatorFile), 0700)
privValidator := types.LoadOrGenPrivValidator(privValidatorFile, log.TestingLogger()) privValidator := types.LoadOrGenPrivValidator(privValidatorFile)
privValidator.Reset() privValidator.Reset()
return privValidator return privValidator
} }

View File

@ -60,7 +60,7 @@ type Node struct {
func NewNodeDefault(config *cfg.Config, logger log.Logger) *Node { func NewNodeDefault(config *cfg.Config, logger log.Logger) *Node {
// Get PrivValidator // Get PrivValidator
privValidator := types.LoadOrGenPrivValidator(config.PrivValidatorFile(), logger) privValidator := types.LoadOrGenPrivValidator(config.PrivValidatorFile())
return NewNode(config, privValidator, return NewNode(config, privValidator,
proxy.DefaultClientCreator(config.ProxyApp, config.ABCI, config.DBDir()), logger) proxy.DefaultClientCreator(config.ProxyApp, config.ABCI, config.DBDir()), logger)
} }

View File

@ -79,7 +79,7 @@ func NewTendermint(app abci.Application) *nm.Node {
logger := log.NewTMLogger(log.NewSyncWriter(os.Stdout)) logger := log.NewTMLogger(log.NewSyncWriter(os.Stdout))
logger = log.NewFilter(logger, log.AllowError()) logger = log.NewFilter(logger, log.AllowError())
privValidatorFile := config.PrivValidatorFile() privValidatorFile := config.PrivValidatorFile()
privValidator := types.LoadOrGenPrivValidator(privValidatorFile, logger) privValidator := types.LoadOrGenPrivValidator(privValidatorFile)
papp := proxy.NewLocalClientCreator(app) papp := proxy.NewLocalClientCreator(app)
node := nm.NewNode(config, privValidator, papp, logger) node := nm.NewNode(config, privValidator, papp, logger)
return node return node

View File

@ -12,7 +12,6 @@ import (
crypto "github.com/tendermint/go-crypto" crypto "github.com/tendermint/go-crypto"
data "github.com/tendermint/go-wire/data" data "github.com/tendermint/go-wire/data"
. "github.com/tendermint/tmlibs/common" . "github.com/tendermint/tmlibs/common"
"github.com/tendermint/tmlibs/log"
) )
// TODO: type ? // TODO: type ?
@ -35,30 +34,6 @@ func voteToStep(vote *Vote) int8 {
} }
} }
// PrivValidator implements the functionality for signing blocks.
type PrivValidator struct {
Address data.Bytes `json:"address"`
PubKey crypto.PubKey `json:"pub_key"`
LastHeight int `json:"last_height"`
LastRound int `json:"last_round"`
LastStep int8 `json:"last_step"`
LastSignature crypto.Signature `json:"last_signature,omitempty"` // so we dont lose signatures
LastSignBytes data.Bytes `json:"last_signbytes,omitempty"` // so we dont lose signatures
// PrivKey should be empty if a Signer other than the default is being used.
PrivKey crypto.PrivKey `json:"priv_key"`
Signer `json:"-"`
// For persistence.
// Overloaded for testing.
filePath string
mtx sync.Mutex
}
type SignerGenerator func(pk crypto.PrivKey) (Signer)
// This is used to sign votes. // This is used to sign votes.
// It is the caller's duty to verify the msg before calling Sign, // It is the caller's duty to verify the msg before calling Sign,
// eg. to avoid double signing. // eg. to avoid double signing.
@ -90,15 +65,53 @@ func (ds *DefaultSigner) PubKey() crypto.PubKey {
return ds.priv.PubKey() return ds.priv.PubKey()
} }
func (privVal *PrivValidator) SetSigner(s Signer) { // PrivValidator implements the functionality for signing blocks.
privVal.Signer = s type PrivValidator struct {
privVal.setPubKeyAndAddress() Address data.Bytes `json:"address"`
PubKey crypto.PubKey `json:"pub_key"`
LastHeight int `json:"last_height"`
LastRound int `json:"last_round"`
LastStep int8 `json:"last_step"`
LastSignature crypto.Signature `json:"last_signature,omitempty"` // so we dont lose signatures
LastSignBytes data.Bytes `json:"last_signbytes,omitempty"` // so we dont lose signatures
// PrivKey should be empty if a Signer other than the default is being used.
PrivKey crypto.PrivKey `json:"priv_key"`
Signer `json:"-"`
// For persistence.
// Overloaded for testing.
filePath string
mtx sync.Mutex
} }
// Overwrite address and pubkey for convenience func LoadOrGenPrivValidator(filePath string) *PrivValidator {
func (privVal *PrivValidator) setPubKeyAndAddress() { var privValidator *PrivValidator
privVal.PubKey = privVal.Signer.PubKey() if _, err := os.Stat(filePath); err == nil {
privVal.Address = privVal.PubKey.Address() privValidator = LoadPrivValidator(filePath)
} else {
privValidator = GenPrivValidator()
privValidator.SetFile(filePath)
privValidator.Save()
}
return privValidator
}
func LoadPrivValidator(filePath string) *PrivValidator {
privValJSONBytes, err := ioutil.ReadFile(filePath)
if err != nil {
Exit(err.Error())
}
privVal := PrivValidator{}
err = json.Unmarshal(privValJSONBytes, &privVal)
if err != nil {
Exit(Fmt("Error reading PrivValidator from %v: %v\n", filePath, err))
}
privVal.filePath = filePath
privVal.Signer = NewDefaultSigner(privVal.PrivKey)
privVal.setPubKeyAndAddress()
return &privVal
} }
// Generates a new validator with private key. // Generates a new validator with private key.
@ -115,43 +128,25 @@ func GenPrivValidator() *PrivValidator {
} }
} }
func LoadPrivValidator(filePath string) *PrivValidator { func LoadPrivValidatorWithSigner(signer Signer) *PrivValidator {
return LoadPrivValidatorWithSigner(filePath, func(pk crypto.PrivKey) Signer { return &PrivValidator{
return NewDefaultSigner(pk) Address: signer.PubKey().Address(),
}) PubKey: signer.PubKey(),
LastStep: stepNone,
filePath: "",
Signer: signer,
}
} }
func LoadPrivValidatorWithSigner(filePath string, generator SignerGenerator) *PrivValidator { func (privVal *PrivValidator) SetSigner(s Signer) {
privValJSONBytes, err := ioutil.ReadFile(filePath) privVal.Signer = s
if err != nil {
Exit(err.Error())
}
privVal := PrivValidator{}
err = json.Unmarshal(privValJSONBytes, &privVal)
if err != nil {
Exit(Fmt("Error reading PrivValidator from %v: %v\n", filePath, err))
}
privVal.filePath = filePath
privVal.Signer = generator(privVal.PrivKey)
privVal.setPubKeyAndAddress() privVal.setPubKeyAndAddress()
return &privVal
} }
func LoadOrGenPrivValidator(filePath string, logger log.Logger) *PrivValidator { // Overwrite address and pubkey for convenience
var privValidator *PrivValidator func (privVal *PrivValidator) setPubKeyAndAddress() {
if _, err := os.Stat(filePath); err == nil { privVal.PubKey = privVal.Signer.PubKey()
privValidator = LoadPrivValidator(filePath) privVal.Address = privVal.PubKey.Address()
logger.Info("Loaded PrivValidator",
"file", filePath, "privValidator", privValidator)
} else {
privValidator = GenPrivValidator()
privValidator.SetFile(filePath)
privValidator.Save()
logger.Info("Generated PrivValidator", "file", filePath)
}
return privValidator
} }
func (privVal *PrivValidator) SetFile(filePath string) { func (privVal *PrivValidator) SetFile(filePath string) {