PrivValidator.GetAddress() and PrivValidator.GetPublicKey() return an

error instead of panic
This commit is contained in:
Ismail Khoffi
2018-11-29 18:52:19 +01:00
parent 44b769b1ac
commit 417c30b9c1
23 changed files with 345 additions and 151 deletions

View File

@ -42,7 +42,10 @@ func randGenesisDoc(numValidators int, randPower bool, minPower int64) (*types.G
}
func makeVote(header *types.Header, blockID types.BlockID, valset *types.ValidatorSet, privVal types.PrivValidator) *types.Vote {
addr := privVal.GetAddress()
addr, err := privVal.GetAddress()
if err != nil {
panic(err)
}
idx, _ := valset.GetByAddress(addr)
vote := &types.Vote{
ValidatorAddress: addr,

View File

@ -57,9 +57,13 @@ func initFilesWithConfig(config *cfg.Config) error {
GenesisTime: tmtime.Now(),
ConsensusParams: types.DefaultConsensusParams(),
}
key, err := pv.GetPubKey()
if err != nil {
return err
}
genDoc.Validators = []types.GenesisValidator{{
Address: pv.GetPubKey().Address(),
PubKey: pv.GetPubKey(),
Address: key.Address(),
PubKey: key,
Power: 10,
}}

View File

@ -2,6 +2,7 @@ package commands
import (
"fmt"
"os"
"github.com/spf13/cobra"
@ -17,6 +18,11 @@ var ShowValidatorCmd = &cobra.Command{
func showValidator(cmd *cobra.Command, args []string) {
privValidator := privval.LoadOrGenFilePV(config.PrivValidatorFile())
pubKeyJSONBytes, _ := cdc.MarshalJSON(privValidator.GetPubKey())
key, err := privValidator.GetPubKey()
if err != nil {
fmt.Fprintf(os.Stderr, "Could not read public key from FilePV %v", privValidator)
os.Exit(1)
}
pubKeyJSONBytes, _ := cdc.MarshalJSON(key)
fmt.Println(string(pubKeyJSONBytes))
}

View File

@ -90,9 +90,13 @@ func testnetFiles(cmd *cobra.Command, args []string) error {
pvFile := filepath.Join(nodeDir, config.BaseConfig.PrivValidator)
pv := privval.LoadFilePV(pvFile)
pubKey, err := pv.GetPubKey()
if err != nil {
return err
}
genVals[i] = types.GenesisValidator{
Address: pv.GetPubKey().Address(),
PubKey: pv.GetPubKey(),
Address: pubKey.Address(),
PubKey: pubKey,
Power: 1,
Name: nodeDirName,
}

View File

@ -72,16 +72,20 @@ func NewValidatorStub(privValidator types.PrivValidator, valIndex int) *validato
}
func (vs *validatorStub) signVote(voteType types.SignedMsgType, hash []byte, header types.PartSetHeader) (*types.Vote, error) {
addr, err := vs.PrivValidator.GetAddress()
if err != nil {
return nil, err
}
vote := &types.Vote{
ValidatorIndex: vs.Index,
ValidatorAddress: vs.PrivValidator.GetAddress(),
ValidatorAddress: addr,
Height: vs.Height,
Round: vs.Round,
Timestamp: tmtime.Now(),
Type: voteType,
BlockID: types.BlockID{hash, header},
}
err := vs.PrivValidator.SignVote(config.ChainID(), vote)
err = vs.PrivValidator.SignVote(config.ChainID(), vote)
return vote, err
}
@ -151,8 +155,12 @@ func signAddVotes(to *ConsensusState, voteType types.SignedMsgType, hash []byte,
func validatePrevote(t *testing.T, cs *ConsensusState, round int, privVal *validatorStub, blockHash []byte) {
prevotes := cs.Votes.Prevotes(round)
address, err := privVal.GetAddress()
if err != nil {
panic(err)
}
var vote *types.Vote
if vote = prevotes.GetByAddress(privVal.GetAddress()); vote == nil {
if vote = prevotes.GetByAddress(address); vote == nil {
panic("Failed to find prevote from validator")
}
if blockHash == nil {
@ -168,8 +176,12 @@ func validatePrevote(t *testing.T, cs *ConsensusState, round int, privVal *valid
func validateLastPrecommit(t *testing.T, cs *ConsensusState, privVal *validatorStub, blockHash []byte) {
votes := cs.LastCommit
address, err := privVal.GetAddress()
if err != nil {
panic(err)
}
var vote *types.Vote
if vote = votes.GetByAddress(privVal.GetAddress()); vote == nil {
if vote = votes.GetByAddress(address); vote == nil {
panic("Failed to find precommit from validator")
}
if !bytes.Equal(vote.BlockID.Hash, blockHash) {
@ -179,8 +191,12 @@ func validateLastPrecommit(t *testing.T, cs *ConsensusState, privVal *validatorS
func validatePrecommit(t *testing.T, cs *ConsensusState, thisRound, lockRound int, privVal *validatorStub, votedBlockHash, lockedBlockHash []byte) {
precommits := cs.Votes.Precommits(thisRound)
address, err := privVal.GetAddress()
if err != nil {
panic(err)
}
var vote *types.Vote
if vote = precommits.GetByAddress(privVal.GetAddress()); vote == nil {
if vote = precommits.GetByAddress(address); vote == nil {
panic("Failed to find precommit from validator")
}

View File

@ -143,7 +143,9 @@ func TestReactorWithEvidence(t *testing.T) {
// mock the evidence pool
// everyone includes evidence of another double signing
vIdx := (i + 1) % nValidators
evpool := newMockEvidencePool(privVals[vIdx].GetAddress())
addr, err := privVals[vIdx].GetAddress()
assert.NoError(t, err)
evpool := newMockEvidencePool(addr)
// Make ConsensusState
blockExec := sm.NewBlockExecutor(stateDB, log.TestingLogger(), proxyAppConnCon, mempool, evpool)
@ -268,7 +270,9 @@ func TestReactorVotingPowerChange(t *testing.T) {
// map of active validators
activeVals := make(map[string]struct{})
for i := 0; i < nVals; i++ {
activeVals[string(css[i].privValidator.GetAddress())] = struct{}{}
addr, err := css[i].privValidator.GetAddress()
assert.NoError(t, err)
activeVals[string(addr)] = struct{}{}
}
// wait till everyone makes block 1
@ -279,7 +283,9 @@ func TestReactorVotingPowerChange(t *testing.T) {
//---------------------------------------------------------------------------
logger.Debug("---------------------------- Testing changing the voting power of one validator a few times")
val1PubKey := css[0].privValidator.GetPubKey()
pubKey, err := css[0].privValidator.GetPubKey()
assert.NoError(t, err)
val1PubKey := pubKey
val1PubKeyABCI := types.TM2PB.PubKey(val1PubKey)
updateValidatorTx := kvstore.MakeValSetChangeTx(val1PubKeyABCI, 25)
previousTotalVotingPower := css[0].GetRoundState().LastValidators.TotalVotingPower()
@ -331,7 +337,9 @@ func TestReactorValidatorSetChanges(t *testing.T) {
// map of active validators
activeVals := make(map[string]struct{})
for i := 0; i < nVals; i++ {
activeVals[string(css[i].privValidator.GetAddress())] = struct{}{}
addr, err := css[i].privValidator.GetAddress()
assert.NoError(t, err)
activeVals[string(addr)] = struct{}{}
}
// wait till everyone makes block 1
@ -342,7 +350,9 @@ func TestReactorValidatorSetChanges(t *testing.T) {
//---------------------------------------------------------------------------
logger.Info("---------------------------- Testing adding one validator")
newValidatorPubKey1 := css[nVals].privValidator.GetPubKey()
pubKey, err := css[nVals].privValidator.GetPubKey()
assert.NoError(t, err)
newValidatorPubKey1 := pubKey
valPubKey1ABCI := types.TM2PB.PubKey(newValidatorPubKey1)
newValidatorTx1 := kvstore.MakeValSetChangeTx(valPubKey1ABCI, testMinPower)
@ -369,7 +379,9 @@ func TestReactorValidatorSetChanges(t *testing.T) {
//---------------------------------------------------------------------------
logger.Info("---------------------------- Testing changing the voting power of one validator")
updateValidatorPubKey1 := css[nVals].privValidator.GetPubKey()
pubKey, err = css[nVals].privValidator.GetPubKey()
assert.NoError(t, err)
updateValidatorPubKey1 := pubKey
updatePubKey1ABCI := types.TM2PB.PubKey(updateValidatorPubKey1)
updateValidatorTx1 := kvstore.MakeValSetChangeTx(updatePubKey1ABCI, 25)
previousTotalVotingPower := css[nVals].GetRoundState().LastValidators.TotalVotingPower()
@ -386,11 +398,15 @@ func TestReactorValidatorSetChanges(t *testing.T) {
//---------------------------------------------------------------------------
logger.Info("---------------------------- Testing adding two validators at once")
newValidatorPubKey2 := css[nVals+1].privValidator.GetPubKey()
pubKey, err = css[nVals+1].privValidator.GetPubKey()
assert.NoError(t, err)
newValidatorPubKey2 := pubKey
newVal2ABCI := types.TM2PB.PubKey(newValidatorPubKey2)
newValidatorTx2 := kvstore.MakeValSetChangeTx(newVal2ABCI, testMinPower)
newValidatorPubKey3 := css[nVals+2].privValidator.GetPubKey()
pubKey, err = css[nVals+2].privValidator.GetPubKey()
assert.NoError(t, err)
newValidatorPubKey3 := pubKey
newVal3ABCI := types.TM2PB.PubKey(newValidatorPubKey3)
newValidatorTx3 := kvstore.MakeValSetChangeTx(newVal3ABCI, testMinPower)

View File

@ -331,7 +331,9 @@ func testHandshakeReplay(t *testing.T, nBlocks int, mode uint) {
chain, commits, err := makeBlockchainFromWAL(wal)
require.NoError(t, err)
stateDB, state, store := stateAndStore(config, privVal.GetPubKey(), kvstore.ProtocolVersion)
pubKey, err := privVal.GetPubKey()
assert.NoError(t, err)
stateDB, state, store := stateAndStore(config, pubKey, kvstore.ProtocolVersion)
store.chain = chain
store.commits = commits
@ -346,7 +348,7 @@ func testHandshakeReplay(t *testing.T, nBlocks int, mode uint) {
// run nBlocks against a new client to build up the app state.
// use a throwaway tendermint state
proxyApp := proxy.NewAppConns(clientCreator2)
stateDB, state, _ := stateAndStore(config, privVal.GetPubKey(), kvstore.ProtocolVersion)
stateDB, state, _ := stateAndStore(config, pubKey, kvstore.ProtocolVersion)
buildAppStateFromChain(proxyApp, stateDB, state, chain, nBlocks, mode)
}
@ -634,7 +636,9 @@ func TestInitChainUpdateValidators(t *testing.T) {
config := ResetConfig("proxy_test_")
privVal := privval.LoadFilePV(config.PrivValidatorFile())
stateDB, state, store := stateAndStore(config, privVal.GetPubKey(), 0x0)
pubKey, err := privVal.GetPubKey()
assert.NoError(t, err)
stateDB, state, store := stateAndStore(config, pubKey, 0x0)
oldValAddr := state.Validators.Validators[0].Address
@ -659,12 +663,6 @@ func TestInitChainUpdateValidators(t *testing.T) {
assert.Equal(t, newValAddr, expectValAddr)
}
func newInitChainApp(vals []abci.ValidatorUpdate) *initChainApp {
return &initChainApp{
vals: vals,
}
}
// returns the vals on InitChain
type initChainApp struct {
abci.BaseApplication

View File

@ -829,13 +829,18 @@ func (cs *ConsensusState) enterPropose(height int64, round int) {
}
// if not a validator, we're done
if !cs.Validators.HasAddress(cs.privValidator.GetAddress()) {
logger.Debug("This node is not a validator", "addr", cs.privValidator.GetAddress(), "vals", cs.Validators)
address, err := cs.privValidator.GetAddress()
if err != nil {
logger.Error("Could not retrieve potenial validator address", cs.privValidator)
return
}
if !cs.Validators.HasAddress(address) {
logger.Debug("This node is not a validator", "addr", address, "vals", cs.Validators)
return
}
logger.Debug("This node is a validator")
if cs.isProposer() {
if cs.isProposer(address) {
logger.Info("enterPropose: Our turn to propose", "proposer", cs.Validators.GetProposer().Address, "privValidator", cs.privValidator)
cs.decideProposal(height, round)
} else {
@ -843,8 +848,8 @@ func (cs *ConsensusState) enterPropose(height int64, round int) {
}
}
func (cs *ConsensusState) isProposer() bool {
return bytes.Equal(cs.Validators.GetProposer().Address, cs.privValidator.GetAddress())
func (cs *ConsensusState) isProposer(address []byte) bool {
return bytes.Equal(cs.Validators.GetProposer().Address, address)
}
func (cs *ConsensusState) defaultDecideProposal(height int64, round int) {
@ -929,7 +934,11 @@ func (cs *ConsensusState) createProposalBlock() (block *types.Block, blockParts
cs.state.Validators.Size(),
len(evidence),
), maxGas)
proposerAddr := cs.privValidator.GetAddress()
proposerAddr, err := cs.privValidator.GetAddress()
if err != nil {
cs.Logger.Error("could not retrieve proposer's address from privValidator", cs.privValidator)
return
}
block, parts := cs.state.MakeBlock(cs.Height, txs, commit, evidence, proposerAddr)
return block, parts
@ -1474,7 +1483,9 @@ func (cs *ConsensusState) tryAddVote(vote *types.Vote, peerID p2p.ID) (bool, err
if err == ErrVoteHeightMismatch {
return added, err
} else if voteErr, ok := err.(*types.ErrVoteConflictingVotes); ok {
if bytes.Equal(vote.ValidatorAddress, cs.privValidator.GetAddress()) {
addr, err := cs.privValidator.GetAddress()
cs.Logger.Error("Can not retrieve privValidator's address", "err", err)
if bytes.Equal(vote.ValidatorAddress, addr) {
cs.Logger.Error("Found conflicting vote from ourselves. Did you unsafe_reset a validator?", "height", vote.Height, "round", vote.Round, "type", vote.Type)
return added, err
}
@ -1639,7 +1650,11 @@ func (cs *ConsensusState) addVote(vote *types.Vote, peerID p2p.ID) (added bool,
}
func (cs *ConsensusState) signVote(type_ types.SignedMsgType, hash []byte, header types.PartSetHeader) (*types.Vote, error) {
addr := cs.privValidator.GetAddress()
addr, err := cs.privValidator.GetAddress()
if err != nil {
cs.Logger.Error("Could not retrieve privValidator's address. The remote signer's connection might have dropped?", "err", err)
return nil, err
}
valIndex, _ := cs.Validators.GetByAddress(addr)
vote := &types.Vote{
@ -1651,7 +1666,7 @@ func (cs *ConsensusState) signVote(type_ types.SignedMsgType, hash []byte, heade
Type: type_,
BlockID: types.BlockID{hash, header},
}
err := cs.privValidator.SignVote(cs.state.ChainID, vote)
err = cs.privValidator.SignVote(cs.state.ChainID, vote)
return vote, err
}
@ -1675,7 +1690,12 @@ func (cs *ConsensusState) voteTime() time.Time {
// sign the vote and publish on internalMsgQueue
func (cs *ConsensusState) signAddVote(type_ types.SignedMsgType, hash []byte, header types.PartSetHeader) *types.Vote {
// if we don't have a key or we're not in the validator set, do nothing
if cs.privValidator == nil || !cs.Validators.HasAddress(cs.privValidator.GetAddress()) {
privValAddr, err := cs.privValidator.GetAddress()
if err != nil {
cs.Logger.Error("Error signing vote. Could not retrieve privValidator's address.", "privValidator", cs.privValidator, "height", cs.Height, "round", cs.Round)
return nil
}
if cs.privValidator == nil || !cs.Validators.HasAddress(privValAddr) {
return nil
}
vote, err := cs.signVote(type_, hash, header)

View File

@ -73,7 +73,9 @@ func TestStateProposerSelection0(t *testing.T) {
// Commit a block and ensure proposer for the next height is correct.
prop := cs1.GetRoundState().Validators.GetProposer()
if !bytes.Equal(prop.Address, cs1.privValidator.GetAddress()) {
address, err := cs1.privValidator.GetAddress()
assert.NoError(t, err)
if !bytes.Equal(prop.Address, address) {
t.Fatalf("expected proposer to be validator %d. Got %X", 0, prop.Address)
}
@ -87,7 +89,9 @@ func TestStateProposerSelection0(t *testing.T) {
ensureNewRound(newRoundCh, height+1, 0)
prop = cs1.GetRoundState().Validators.GetProposer()
if !bytes.Equal(prop.Address, vss[1].GetAddress()) {
addr, err := vss[1].GetAddress()
assert.NoError(t, err)
if !bytes.Equal(prop.Address, addr) {
panic(fmt.Sprintf("expected proposer to be validator %d. Got %X", 1, prop.Address))
}
}
@ -110,7 +114,9 @@ func TestStateProposerSelection2(t *testing.T) {
// everyone just votes nil. we get a new proposer each round
for i := 0; i < len(vss); i++ {
prop := cs1.GetRoundState().Validators.GetProposer()
correctProposer := vss[(i+round)%len(vss)].GetAddress()
addr, err := vss[(i+round)%len(vss)].GetAddress()
assert.NoError(t, err)
correctProposer := addr
if !bytes.Equal(prop.Address, correctProposer) {
panic(fmt.Sprintf("expected RoundState.Validators.GetProposer() to be validator %d. Got %X", (i+2)%len(vss), prop.Address))
}
@ -505,7 +511,9 @@ func TestStateLockPOLRelock(t *testing.T) {
timeoutWaitCh := subscribe(cs1.eventBus, types.EventQueryTimeoutWait)
proposalCh := subscribe(cs1.eventBus, types.EventQueryCompleteProposal)
voteCh := subscribeToVoter(cs1, cs1.privValidator.GetAddress())
addr, err := cs1.privValidator.GetAddress()
assert.NoError(t, err)
voteCh := subscribeToVoter(cs1, addr)
newRoundCh := subscribe(cs1.eventBus, types.EventQueryNewRound)
newBlockCh := subscribe(cs1.eventBus, types.EventQueryNewBlockHeader)
@ -596,7 +604,9 @@ func TestStateLockPOLUnlock(t *testing.T) {
timeoutWaitCh := subscribe(cs1.eventBus, types.EventQueryTimeoutWait)
newRoundCh := subscribe(cs1.eventBus, types.EventQueryNewRound)
unlockCh := subscribe(cs1.eventBus, types.EventQueryUnlock)
voteCh := subscribeToVoter(cs1, cs1.privValidator.GetAddress())
addr, err := cs1.privValidator.GetAddress()
assert.NoError(t, err)
voteCh := subscribeToVoter(cs1, addr)
// everything done from perspective of cs1
@ -689,7 +699,9 @@ func TestStateLockPOLSafety1(t *testing.T) {
timeoutProposeCh := subscribe(cs1.eventBus, types.EventQueryTimeoutPropose)
timeoutWaitCh := subscribe(cs1.eventBus, types.EventQueryTimeoutWait)
newRoundCh := subscribe(cs1.eventBus, types.EventQueryNewRound)
voteCh := subscribeToVoter(cs1, cs1.privValidator.GetAddress())
addr, err := cs1.privValidator.GetAddress()
assert.NoError(t, err)
voteCh := subscribeToVoter(cs1, addr)
// start round and wait for propose and prevote
startTestRound(cs1, cs1.Height, round)
@ -805,7 +817,9 @@ func TestStateLockPOLSafety2(t *testing.T) {
timeoutWaitCh := subscribe(cs1.eventBus, types.EventQueryTimeoutWait)
newRoundCh := subscribe(cs1.eventBus, types.EventQueryNewRound)
unlockCh := subscribe(cs1.eventBus, types.EventQueryUnlock)
voteCh := subscribeToVoter(cs1, cs1.privValidator.GetAddress())
addr, err := cs1.privValidator.GetAddress()
assert.NoError(t, err)
voteCh := subscribeToVoter(cs1, addr)
// the block for R0: gets polkad but we miss it
// (even though we signed it, shhh)
@ -896,7 +910,9 @@ func TestProposeValidBlock(t *testing.T) {
timeoutProposeCh := subscribe(cs1.eventBus, types.EventQueryTimeoutPropose)
newRoundCh := subscribe(cs1.eventBus, types.EventQueryNewRound)
unlockCh := subscribe(cs1.eventBus, types.EventQueryUnlock)
voteCh := subscribeToVoter(cs1, cs1.privValidator.GetAddress())
addr, err := cs1.privValidator.GetAddress()
assert.NoError(t, err)
voteCh := subscribeToVoter(cs1, addr)
// start round and wait for propose and prevote
startTestRound(cs1, cs1.Height, round)
@ -982,7 +998,9 @@ func TestSetValidBlockOnDelayedPrevote(t *testing.T) {
timeoutWaitCh := subscribe(cs1.eventBus, types.EventQueryTimeoutWait)
newRoundCh := subscribe(cs1.eventBus, types.EventQueryNewRound)
validBlockCh := subscribe(cs1.eventBus, types.EventQueryValidBlock)
voteCh := subscribeToVoter(cs1, cs1.privValidator.GetAddress())
addr, err := cs1.privValidator.GetAddress()
assert.NoError(t, err)
voteCh := subscribeToVoter(cs1, addr)
// start round and wait for propose and prevote
startTestRound(cs1, cs1.Height, round)
@ -1041,7 +1059,9 @@ func TestSetValidBlockOnDelayedProposal(t *testing.T) {
timeoutProposeCh := subscribe(cs1.eventBus, types.EventQueryTimeoutPropose)
newRoundCh := subscribe(cs1.eventBus, types.EventQueryNewRound)
validBlockCh := subscribe(cs1.eventBus, types.EventQueryValidBlock)
voteCh := subscribeToVoter(cs1, cs1.privValidator.GetAddress())
addr, err := cs1.privValidator.GetAddress()
assert.NoError(t, err)
voteCh := subscribeToVoter(cs1, addr)
proposalCh := subscribe(cs1.eventBus, types.EventQueryCompleteProposal)
round = round + 1 // move to round in which P0 is not proposer
@ -1111,7 +1131,9 @@ func TestWaitingTimeoutProposeOnNewRound(t *testing.T) {
timeoutWaitCh := subscribe(cs1.eventBus, types.EventQueryTimeoutPropose)
newRoundCh := subscribe(cs1.eventBus, types.EventQueryNewRound)
voteCh := subscribeToVoter(cs1, cs1.privValidator.GetAddress())
addr, err := cs1.privValidator.GetAddress()
assert.NoError(t, err)
voteCh := subscribeToVoter(cs1, addr)
// start round
startTestRound(cs1, height, round)
@ -1144,7 +1166,9 @@ func TestRoundSkipOnNilPolkaFromHigherRound(t *testing.T) {
timeoutWaitCh := subscribe(cs1.eventBus, types.EventQueryTimeoutWait)
newRoundCh := subscribe(cs1.eventBus, types.EventQueryNewRound)
voteCh := subscribeToVoter(cs1, cs1.privValidator.GetAddress())
addr, err := cs1.privValidator.GetAddress()
assert.NoError(t, err)
voteCh := subscribeToVoter(cs1, addr)
// start round
startTestRound(cs1, height, round)
@ -1177,7 +1201,9 @@ func TestWaitTimeoutProposeOnNilPolkaForTheCurrentRound(t *testing.T) {
timeoutProposeCh := subscribe(cs1.eventBus, types.EventQueryTimeoutPropose)
newRoundCh := subscribe(cs1.eventBus, types.EventQueryNewRound)
voteCh := subscribeToVoter(cs1, cs1.privValidator.GetAddress())
addr, err := cs1.privValidator.GetAddress()
assert.NoError(t, err)
voteCh := subscribeToVoter(cs1, addr)
// start round in which PO is not proposer
startTestRound(cs1, height, round)
@ -1361,7 +1387,9 @@ func TestStateHalt1(t *testing.T) {
timeoutWaitCh := subscribe(cs1.eventBus, types.EventQueryTimeoutWait)
newRoundCh := subscribe(cs1.eventBus, types.EventQueryNewRound)
newBlockCh := subscribe(cs1.eventBus, types.EventQueryNewBlock)
voteCh := subscribeToVoter(cs1, cs1.privValidator.GetAddress())
addr, err := cs1.privValidator.GetAddress()
assert.NoError(t, err)
voteCh := subscribeToVoter(cs1, addr)
// start round and wait for propose and prevote
startTestRound(cs1, height, round)

View File

@ -50,8 +50,12 @@ func TestPeerCatchupRounds(t *testing.T) {
func makeVoteHR(t *testing.T, height int64, round int, privVals []types.PrivValidator, valIndex int) *types.Vote {
privVal := privVals[valIndex]
addr, err := privVal.GetAddress()
if err != nil {
panic(err)
}
vote := &types.Vote{
ValidatorAddress: privVal.GetAddress(),
ValidatorAddress: addr,
ValidatorIndex: valIndex,
Height: height,
Round: round,
@ -60,7 +64,7 @@ func makeVoteHR(t *testing.T, height int64, round int, privVals []types.PrivVali
BlockID: types.BlockID{[]byte("fakehash"), types.PartSetHeader{}},
}
chainID := config.ChainID()
err := privVal.SignVote(chainID, vote)
err = privVal.SignVote(chainID, vote)
if err != nil {
panic(fmt.Sprintf("Error signing vote: %v", err))
return nil

View File

@ -235,16 +235,25 @@ func NewNode(config *cfg.Config,
fastSync := config.FastSync
if state.Validators.Size() == 1 {
addr, _ := state.Validators.GetByIndex(0)
if bytes.Equal(privValidator.GetAddress(), addr) {
privValAddr, err := privValidator.GetAddress()
if err != nil {
return nil, errors.Wrap(err, "Error while retrieving private validator's address")
}
if bytes.Equal(privValAddr, addr) {
fastSync = false
}
}
pubKey, err := privValidator.GetPubKey()
if err != nil {
return nil, errors.Wrap(err, "Error while retrieving private validator's public key")
}
addr := pubKey.Address()
// Log whether this node is a validator or an observer
if state.Validators.HasAddress(privValidator.GetAddress()) {
consensusLogger.Info("This node is a validator", "addr", privValidator.GetAddress(), "pubKey", privValidator.GetPubKey())
if state.Validators.HasAddress(addr) {
consensusLogger.Info("This node is a validator", "addr", addr, "pubKey", pubKey)
} else {
consensusLogger.Info("This node is not a validator", "addr", privValidator.GetAddress(), "pubKey", privValidator.GetPubKey())
consensusLogger.Info("This node is not a validator", "addr", addr, "pubKey", pubKey)
}
csMetrics, p2pMetrics, memplMetrics, smMetrics := metricsProvider()
@ -611,7 +620,11 @@ func (n *Node) ConfigureRPC() {
rpccore.SetEvidencePool(n.evidencePool)
rpccore.SetP2PPeers(n.sw)
rpccore.SetP2PTransport(n)
rpccore.SetPubKey(n.privValidator.GetPubKey())
pubKey, err := n.privValidator.GetPubKey()
if err != nil {
n.Logger.Error("Error configuring RPC. Can not retrieve privValidator's public key", "err", err)
}
rpccore.SetPubKey(pubKey)
rpccore.SetGenesisDoc(n.genesisDoc)
rpccore.SetAddrBook(n.addrBook)
rpccore.SetProxyAppQuery(n.proxyApp.Query())

View File

@ -58,14 +58,14 @@ type FilePV struct {
// GetAddress returns the address of the validator.
// Implements PrivValidator.
func (pv *FilePV) GetAddress() types.Address {
return pv.Address
func (pv *FilePV) GetAddress() (types.Address, error) {
return pv.Address, nil
}
// GetPubKey returns the public key of the validator.
// Implements PrivValidator.
func (pv *FilePV) GetPubKey() crypto.PubKey {
return pv.PubKey
func (pv *FilePV) GetPubKey() (crypto.PubKey, error) {
return pv.PubKey, nil
}
// GenFilePV generates a new validator with randomly generated private key
@ -292,7 +292,9 @@ func (pv *FilePV) saveSigned(height int64, round int, step int8,
// String returns a string representation of the FilePV.
func (pv *FilePV) String() string {
return fmt.Sprintf("PrivValidator{%v LH:%v, LR:%v, LS:%v}", pv.GetAddress(), pv.LastHeight, pv.LastRound, pv.LastStep)
// does not error in FilePV:
addr, _ := pv.GetAddress()
return fmt.Sprintf("PrivValidator{%v LH:%v, LR:%v, LS:%v}", addr, pv.LastHeight, pv.LastRound, pv.LastStep)
}
//-------------------------------------

View File

@ -25,10 +25,13 @@ func TestGenLoadValidator(t *testing.T) {
height := int64(100)
privVal.LastHeight = height
privVal.Save()
addr := privVal.GetAddress()
addr, err := privVal.GetAddress() // FilePV doesn't err here
assert.NoError(err)
privVal = LoadFilePV(tempFile.Name())
assert.Equal(addr, privVal.GetAddress(), "expected privval addr to be the same")
loadedAddr, err := privVal.GetAddress()
assert.NoError(err)
assert.Equal(addr, loadedAddr, "expected privval addr to be the same")
assert.Equal(height, privVal.LastHeight, "expected privval.LastHeight to have been saved")
}
@ -42,9 +45,11 @@ func TestLoadOrGenValidator(t *testing.T) {
t.Error(err)
}
privVal := LoadOrGenFilePV(tempFilePath)
addr := privVal.GetAddress()
addr, err := privVal.GetAddress()
assert.NoError(err)
privVal = LoadOrGenFilePV(tempFilePath)
assert.Equal(addr, privVal.GetAddress(), "expected privval addr to be the same")
loadedAddr, err := privVal.GetAddress()
assert.Equal(addr, loadedAddr, "expected privval addr to be the same")
}
func TestUnmarshalValidator(t *testing.T) {
@ -81,8 +86,12 @@ func TestUnmarshalValidator(t *testing.T) {
require.Nil(err, "%+v", err)
// make sure the values match
assert.EqualValues(addr, val.GetAddress())
assert.EqualValues(pubKey, val.GetPubKey())
loadedAddr, err := val.GetAddress()
assert.NoError(err)
assert.EqualValues(addr, loadedAddr)
loadedKey, err := val.GetPubKey()
assert.NoError(err)
assert.EqualValues(pubKey, loadedKey)
assert.EqualValues(privKey, val.PrivKey)
// export it and make sure it is the same

View File

@ -33,23 +33,23 @@ func NewRemoteSignerClient(
}
// GetAddress implements PrivValidator.
func (sc *RemoteSignerClient) GetAddress() types.Address {
func (sc *RemoteSignerClient) GetAddress() (types.Address, error) {
pubKey, err := sc.getPubKey()
if err != nil {
panic(err)
return nil, err
}
return pubKey.Address()
return pubKey.Address(), nil
}
// GetPubKey implements PrivValidator.
func (sc *RemoteSignerClient) GetPubKey() crypto.PubKey {
func (sc *RemoteSignerClient) GetPubKey() (crypto.PubKey, error) {
pubKey, err := sc.getPubKey()
if err != nil {
panic(err)
return nil, err
}
return pubKey
return pubKey, nil
}
func (sc *RemoteSignerClient) getPubKey() (crypto.PubKey, error) {
@ -229,7 +229,10 @@ func handleRequest(req RemoteSignerMsg, chainID string, privVal types.PrivValida
switch r := req.(type) {
case *PubKeyMsg:
var p crypto.PubKey
p = privVal.GetPubKey()
p, err = privVal.GetPubKey()
if err != nil {
}
res = &PubKeyMsg{p}
case *SignVoteRequest:
err = privVal.SignVote(chainID, r.Vote)

View File

@ -25,15 +25,13 @@ func TestSocketPVAddress(t *testing.T) {
defer sc.Stop()
defer rs.Stop()
serverAddr := rs.privVal.GetAddress()
serverAddr, err := rs.privVal.GetAddress()
assert.NoError(t, err)
clientAddr := sc.GetAddress()
clientAddr, err := sc.GetAddress()
assert.NoError(t, err)
assert.Equal(t, serverAddr, clientAddr)
// TODO(xla): Remove when PrivValidator2 replaced PrivValidator.
assert.Equal(t, serverAddr, sc.GetAddress())
}
func TestSocketPVPubKey(t *testing.T) {
@ -47,12 +45,10 @@ func TestSocketPVPubKey(t *testing.T) {
clientKey, err := sc.getPubKey()
require.NoError(t, err)
privKey := rs.privVal.GetPubKey()
privKey, err := rs.privVal.GetPubKey()
assert.NoError(t, err)
assert.Equal(t, privKey, clientKey)
// TODO(xla): Remove when PrivValidator2 replaced PrivValidator.
assert.Equal(t, privKey, sc.GetPubKey())
}
func TestSocketPVProposal(t *testing.T) {

View File

@ -17,15 +17,19 @@ type voteData struct {
}
func makeVote(val PrivValidator, chainID string, valIndex int, height int64, round, step int, blockID BlockID) *Vote {
addr, err := val.GetAddress()
if err != nil {
panic(err)
}
v := &Vote{
ValidatorAddress: val.GetAddress(),
ValidatorAddress: addr,
ValidatorIndex: valIndex,
Height: height,
Round: round,
Type: SignedMsgType(step),
BlockID: blockID,
}
err := val.SignVote(chainID, v)
err = val.SignVote(chainID, v)
if err != nil {
panic(err)
}
@ -64,7 +68,8 @@ func TestEvidence(t *testing.T) {
{vote1, badVote, false}, // signed by wrong key
}
pubKey := val.GetPubKey()
pubKey, err := val.GetPubKey()
assert.NoError(t, err)
for _, c := range cases {
ev := &DuplicateVoteEvidence{
VoteA: c.vote1,

View File

@ -12,8 +12,10 @@ import (
// PrivValidator defines the functionality of a local Tendermint validator
// that signs votes and proposals, and never double signs.
type PrivValidator interface {
GetAddress() Address // redundant since .PubKey().Address()
GetPubKey() crypto.PubKey
// TODO: shouldn't we remove GetAddress? In case of a remote signer this will trigger
// another request even if the pubkey was already retrieved.
GetAddress() (Address, error) // redundant since .PubKey().Address()
GetPubKey() (crypto.PubKey, error)
SignVote(chainID string, vote *Vote) error
SignProposal(chainID string, proposal *Proposal) error
@ -29,7 +31,16 @@ func (pvs PrivValidatorsByAddress) Len() int {
}
func (pvs PrivValidatorsByAddress) Less(i, j int) bool {
return bytes.Compare(pvs[i].GetAddress(), pvs[j].GetAddress()) == -1
// this is used in tests only; it's OK to panic here
addr_i, err := pvs[i].GetAddress()
if err != nil {
panic(err)
}
addr_j, err := pvs[j].GetAddress()
if err != nil {
panic(err)
}
return bytes.Compare(addr_i, addr_j) == -1
}
func (pvs PrivValidatorsByAddress) Swap(i, j int) {
@ -52,13 +63,13 @@ func NewMockPV() *MockPV {
}
// Implements PrivValidator.
func (pv *MockPV) GetAddress() Address {
return pv.privKey.PubKey().Address()
func (pv *MockPV) GetAddress() (Address, error) {
return pv.privKey.PubKey().Address(), nil
}
// Implements PrivValidator.
func (pv *MockPV) GetPubKey() crypto.PubKey {
return pv.privKey.PubKey()
func (pv *MockPV) GetPubKey() (crypto.PubKey, error) {
return pv.privKey.PubKey(), nil
}
// Implements PrivValidator.
@ -85,7 +96,8 @@ func (pv *MockPV) SignProposal(chainID string, proposal *Proposal) error {
// String returns a string representation of the MockPV.
func (pv *MockPV) String() string {
return fmt.Sprintf("MockPV{%v}", pv.GetAddress())
addr, _ := pv.GetAddress()
return fmt.Sprintf("MockPV{%v}", addr)
}
// XXX: Implement.

View File

@ -45,7 +45,8 @@ func TestProposalString(t *testing.T) {
func TestProposalVerifySignature(t *testing.T) {
privVal := NewMockPV()
pubKey := privVal.GetPubKey()
pubKey, err := privVal.GetPubKey()
assert.NoError(t, err)
prop := NewProposal(
4, 2, 2,
@ -53,7 +54,7 @@ func TestProposalVerifySignature(t *testing.T) {
signBytes := prop.SignBytes("test_chain_id")
// sign it
err := privVal.SignProposal("test_chain_id", prop)
err = privVal.SignProposal("test_chain_id", prop)
require.NoError(t, err)
// verify the same proposal
@ -94,7 +95,8 @@ func BenchmarkProposalVerifySignature(b *testing.B) {
privVal := NewMockPV()
err := privVal.SignProposal("test_chain_id", testProposal)
require.Nil(b, err)
pubKey := privVal.GetPubKey()
pubKey, err := privVal.GetPubKey()
require.NoError(b, err)
for i := 0; i < b.N; i++ {
pubKey.VerifyBytes(testProposal.SignBytes("test_chain_id"), testProposal.Signature)

View File

@ -142,14 +142,16 @@ func TestABCIEvidence(t *testing.T) {
blockID := makeBlockID([]byte("blockhash"), 1000, []byte("partshash"))
blockID2 := makeBlockID([]byte("blockhash2"), 1000, []byte("partshash"))
const chainID = "mychain"
pubKey, err := val.GetPubKey()
assert.NoError(t, err)
ev := &DuplicateVoteEvidence{
PubKey: val.GetPubKey(),
PubKey: pubKey,
VoteA: makeVote(val, chainID, 0, 10, 2, 1, blockID),
VoteB: makeVote(val, chainID, 0, 10, 2, 1, blockID2),
}
abciEv := TM2PB.Evidence(
ev,
NewValidatorSet([]*Validator{NewValidator(val.GetPubKey(), 10)}),
NewValidatorSet([]*Validator{NewValidator(pubKey, 10)}),
time.Now(),
)

View File

@ -10,9 +10,12 @@ func MakeCommit(blockID BlockID, height int64, round int,
// all sign
for i := 0; i < len(validators); i++ {
addr, err := validators[i].GetAddress()
if err != nil {
return nil, err
}
vote := &Vote{
ValidatorAddress: validators[i].GetAddress(),
ValidatorAddress: addr,
ValidatorIndex: i,
Height: height,
Round: round,
@ -21,7 +24,7 @@ func MakeCommit(blockID BlockID, height int64, round int,
Timestamp: tmtime.Now(),
}
_, err := signAddVote(validators[i], vote, voteSet)
_, err = signAddVote(validators[i], vote, voteSet)
if err != nil {
return nil, err
}

View File

@ -101,6 +101,7 @@ func RandValidator(randPower bool, minPower int64) (*Validator, PrivValidator) {
if randPower {
votePower += int64(cmn.RandUint32())
}
val := NewValidator(privVal.GetPubKey(), votePower)
pubKey, _ := privVal.GetPubKey()
val := NewValidator(pubKey, votePower)
return val, privVal
}

View File

@ -2,6 +2,7 @@ package types
import (
"bytes"
"github.com/stretchr/testify/assert"
"testing"
"github.com/tendermint/tendermint/crypto"
@ -66,7 +67,9 @@ func TestAddVote(t *testing.T) {
// t.Logf(">> %v", voteSet)
if voteSet.GetByAddress(val0.GetAddress()) != nil {
val0Addr, err := val0.GetAddress()
assert.NoError(t, err)
if voteSet.GetByAddress(val0Addr) != nil {
t.Errorf("Expected GetByAddress(val0.Address) to be nil")
}
if voteSet.BitArray().GetIndex(0) {
@ -78,7 +81,7 @@ func TestAddVote(t *testing.T) {
}
vote := &Vote{
ValidatorAddress: val0.GetAddress(),
ValidatorAddress: val0Addr,
ValidatorIndex: 0, // since privValidators are in order
Height: height,
Round: round,
@ -86,12 +89,12 @@ func TestAddVote(t *testing.T) {
Timestamp: tmtime.Now(),
BlockID: BlockID{nil, PartSetHeader{}},
}
_, err := signAddVote(val0, vote, voteSet)
_, err = signAddVote(val0, vote, voteSet)
if err != nil {
t.Error(err)
}
if voteSet.GetByAddress(val0.GetAddress()) == nil {
if voteSet.GetByAddress(val0Addr) == nil {
t.Errorf("Expected GetByAddress(val0.Address) to be present")
}
if !voteSet.BitArray().GetIndex(0) {
@ -118,8 +121,10 @@ func Test2_3Majority(t *testing.T) {
}
// 6 out of 10 voted for nil.
for i := 0; i < 6; i++ {
vote := withValidator(voteProto, privValidators[i].GetAddress(), i)
_, err := signAddVote(privValidators[i], vote, voteSet)
addr, err := privValidators[i].GetAddress()
assert.NoError(t, err)
vote := withValidator(voteProto, addr, i)
_, err = signAddVote(privValidators[i], vote, voteSet)
if err != nil {
t.Error(err)
}
@ -131,8 +136,10 @@ func Test2_3Majority(t *testing.T) {
// 7th validator voted for some blockhash
{
vote := withValidator(voteProto, privValidators[6].GetAddress(), 6)
_, err := signAddVote(privValidators[6], withBlockHash(vote, cmn.RandBytes(32)), voteSet)
addr, err := privValidators[6].GetAddress()
assert.NoError(t, err)
vote := withValidator(voteProto, addr, 6)
_, err = signAddVote(privValidators[6], withBlockHash(vote, cmn.RandBytes(32)), voteSet)
if err != nil {
t.Error(err)
}
@ -144,8 +151,10 @@ func Test2_3Majority(t *testing.T) {
// 8th validator voted for nil.
{
vote := withValidator(voteProto, privValidators[7].GetAddress(), 7)
_, err := signAddVote(privValidators[7], vote, voteSet)
addr, err := privValidators[7].GetAddress()
assert.NoError(t, err)
vote := withValidator(voteProto, addr, 7)
_, err = signAddVote(privValidators[7], vote, voteSet)
if err != nil {
t.Error(err)
}
@ -176,8 +185,10 @@ func Test2_3MajorityRedux(t *testing.T) {
// 66 out of 100 voted for nil.
for i := 0; i < 66; i++ {
vote := withValidator(voteProto, privValidators[i].GetAddress(), i)
_, err := signAddVote(privValidators[i], vote, voteSet)
addr, err := privValidators[i].GetAddress()
assert.NoError(t, err)
vote := withValidator(voteProto, addr, i)
_, err = signAddVote(privValidators[i], vote, voteSet)
if err != nil {
t.Error(err)
}
@ -189,8 +200,10 @@ func Test2_3MajorityRedux(t *testing.T) {
// 67th validator voted for nil
{
vote := withValidator(voteProto, privValidators[66].GetAddress(), 66)
_, err := signAddVote(privValidators[66], withBlockHash(vote, nil), voteSet)
adrr, err := privValidators[66].GetAddress()
vote := withValidator(voteProto, adrr, 66)
assert.NoError(t, err)
_, err = signAddVote(privValidators[66], withBlockHash(vote, nil), voteSet)
if err != nil {
t.Error(err)
}
@ -202,9 +215,11 @@ func Test2_3MajorityRedux(t *testing.T) {
// 68th validator voted for a different BlockParts PartSetHeader
{
vote := withValidator(voteProto, privValidators[67].GetAddress(), 67)
addr, err := privValidators[67].GetAddress()
assert.NoError(t, err)
vote := withValidator(voteProto, addr, 67)
blockPartsHeader := PartSetHeader{blockPartsTotal, crypto.CRandBytes(32)}
_, err := signAddVote(privValidators[67], withBlockPartsHeader(vote, blockPartsHeader), voteSet)
_, err = signAddVote(privValidators[67], withBlockPartsHeader(vote, blockPartsHeader), voteSet)
if err != nil {
t.Error(err)
}
@ -216,9 +231,11 @@ func Test2_3MajorityRedux(t *testing.T) {
// 69th validator voted for different BlockParts Total
{
vote := withValidator(voteProto, privValidators[68].GetAddress(), 68)
addr, err := privValidators[68].GetAddress()
assert.NoError(t, err)
vote := withValidator(voteProto, addr, 68)
blockPartsHeader := PartSetHeader{blockPartsTotal + 1, blockPartsHeader.Hash}
_, err := signAddVote(privValidators[68], withBlockPartsHeader(vote, blockPartsHeader), voteSet)
_, err = signAddVote(privValidators[68], withBlockPartsHeader(vote, blockPartsHeader), voteSet)
if err != nil {
t.Error(err)
}
@ -230,8 +247,10 @@ func Test2_3MajorityRedux(t *testing.T) {
// 70th validator voted for different BlockHash
{
vote := withValidator(voteProto, privValidators[69].GetAddress(), 69)
_, err := signAddVote(privValidators[69], withBlockHash(vote, cmn.RandBytes(32)), voteSet)
addr, err := privValidators[69].GetAddress()
assert.NoError(t, err)
vote := withValidator(voteProto, addr, 69)
_, err = signAddVote(privValidators[69], withBlockHash(vote, cmn.RandBytes(32)), voteSet)
if err != nil {
t.Error(err)
}
@ -243,8 +262,10 @@ func Test2_3MajorityRedux(t *testing.T) {
// 71st validator voted for the right BlockHash & BlockPartsHeader
{
vote := withValidator(voteProto, privValidators[70].GetAddress(), 70)
_, err := signAddVote(privValidators[70], vote, voteSet)
addr, err := privValidators[70].GetAddress()
assert.NoError(t, err)
vote := withValidator(voteProto, addr, 70)
_, err = signAddVote(privValidators[70], vote, voteSet)
if err != nil {
t.Error(err)
}
@ -271,7 +292,9 @@ func TestBadVotes(t *testing.T) {
// val0 votes for nil.
{
vote := withValidator(voteProto, privValidators[0].GetAddress(), 0)
addr, err := privValidators[0].GetAddress()
assert.NoError(t, err)
vote := withValidator(voteProto, addr, 0)
added, err := signAddVote(privValidators[0], vote, voteSet)
if !added || err != nil {
t.Errorf("Expected VoteSet.Add to succeed")
@ -280,7 +303,9 @@ func TestBadVotes(t *testing.T) {
// val0 votes again for some block.
{
vote := withValidator(voteProto, privValidators[0].GetAddress(), 0)
addr, err := privValidators[0].GetAddress()
assert.NoError(t, err)
vote := withValidator(voteProto, addr, 0)
added, err := signAddVote(privValidators[0], withBlockHash(vote, cmn.RandBytes(32)), voteSet)
if added || err == nil {
t.Errorf("Expected VoteSet.Add to fail, conflicting vote.")
@ -289,7 +314,9 @@ func TestBadVotes(t *testing.T) {
// val1 votes on another height
{
vote := withValidator(voteProto, privValidators[1].GetAddress(), 1)
addr, err := privValidators[1].GetAddress()
assert.NoError(t, err)
vote := withValidator(voteProto, addr, 1)
added, err := signAddVote(privValidators[1], withHeight(vote, height+1), voteSet)
if added || err == nil {
t.Errorf("Expected VoteSet.Add to fail, wrong height")
@ -298,7 +325,9 @@ func TestBadVotes(t *testing.T) {
// val2 votes on another round
{
vote := withValidator(voteProto, privValidators[2].GetAddress(), 2)
addr, err := privValidators[2].GetAddress()
assert.NoError(t, err)
vote := withValidator(voteProto, addr, 2)
added, err := signAddVote(privValidators[2], withRound(vote, round+1), voteSet)
if added || err == nil {
t.Errorf("Expected VoteSet.Add to fail, wrong round")
@ -307,7 +336,9 @@ func TestBadVotes(t *testing.T) {
// val3 votes of another type.
{
vote := withValidator(voteProto, privValidators[3].GetAddress(), 3)
addr, err := privValidators[3].GetAddress()
assert.NoError(t, err)
vote := withValidator(voteProto, addr, 3)
added, err := signAddVote(privValidators[3], withType(vote, byte(PrecommitType)), voteSet)
if added || err == nil {
t.Errorf("Expected VoteSet.Add to fail, wrong type")
@ -331,9 +362,11 @@ func TestConflicts(t *testing.T) {
BlockID: BlockID{nil, PartSetHeader{}},
}
val0Addr, err := privValidators[0].GetAddress()
assert.NoError(t, err)
// val0 votes for nil.
{
vote := withValidator(voteProto, privValidators[0].GetAddress(), 0)
vote := withValidator(voteProto, val0Addr, 0)
added, err := signAddVote(privValidators[0], vote, voteSet)
if !added || err != nil {
t.Errorf("Expected VoteSet.Add to succeed")
@ -342,7 +375,7 @@ func TestConflicts(t *testing.T) {
// val0 votes again for blockHash1.
{
vote := withValidator(voteProto, privValidators[0].GetAddress(), 0)
vote := withValidator(voteProto, val0Addr, 0)
added, err := signAddVote(privValidators[0], withBlockHash(vote, blockHash1), voteSet)
if added {
t.Errorf("Expected VoteSet.Add to fail, conflicting vote.")
@ -357,7 +390,7 @@ func TestConflicts(t *testing.T) {
// val0 votes again for blockHash1.
{
vote := withValidator(voteProto, privValidators[0].GetAddress(), 0)
vote := withValidator(voteProto, val0Addr, 0)
added, err := signAddVote(privValidators[0], withBlockHash(vote, blockHash1), voteSet)
if !added {
t.Errorf("Expected VoteSet.Add to succeed, called SetPeerMaj23().")
@ -372,7 +405,7 @@ func TestConflicts(t *testing.T) {
// val0 votes again for blockHash1.
{
vote := withValidator(voteProto, privValidators[0].GetAddress(), 0)
vote := withValidator(voteProto, val0Addr, 0)
added, err := signAddVote(privValidators[0], withBlockHash(vote, blockHash2), voteSet)
if added {
t.Errorf("Expected VoteSet.Add to fail, duplicate SetPeerMaj23() from peerA")
@ -384,7 +417,9 @@ func TestConflicts(t *testing.T) {
// val1 votes for blockHash1.
{
vote := withValidator(voteProto, privValidators[1].GetAddress(), 1)
addr, err := privValidators[1].GetAddress()
assert.NoError(t, err)
vote := withValidator(voteProto, addr, 1)
added, err := signAddVote(privValidators[1], withBlockHash(vote, blockHash1), voteSet)
if !added || err != nil {
t.Errorf("Expected VoteSet.Add to succeed")
@ -401,7 +436,9 @@ func TestConflicts(t *testing.T) {
// val2 votes for blockHash2.
{
vote := withValidator(voteProto, privValidators[2].GetAddress(), 2)
addr, err := privValidators[2].GetAddress()
assert.NoError(t, err)
vote := withValidator(voteProto, addr, 2)
added, err := signAddVote(privValidators[2], withBlockHash(vote, blockHash2), voteSet)
if !added || err != nil {
t.Errorf("Expected VoteSet.Add to succeed")
@ -421,7 +458,9 @@ func TestConflicts(t *testing.T) {
// val2 votes for blockHash1.
{
vote := withValidator(voteProto, privValidators[2].GetAddress(), 2)
addr, err := privValidators[2].GetAddress()
assert.NoError(t, err)
vote := withValidator(voteProto, addr, 2)
added, err := signAddVote(privValidators[2], withBlockHash(vote, blockHash1), voteSet)
if !added {
t.Errorf("Expected VoteSet.Add to succeed")
@ -462,8 +501,10 @@ func TestMakeCommit(t *testing.T) {
// 6 out of 10 voted for some block.
for i := 0; i < 6; i++ {
vote := withValidator(voteProto, privValidators[i].GetAddress(), i)
_, err := signAddVote(privValidators[i], vote, voteSet)
addr, err := privValidators[i].GetAddress()
assert.NoError(t, err)
vote := withValidator(voteProto, addr, i)
_, err = signAddVote(privValidators[i], vote, voteSet)
if err != nil {
t.Error(err)
}
@ -474,11 +515,13 @@ func TestMakeCommit(t *testing.T) {
// 7th voted for some other block.
{
vote := withValidator(voteProto, privValidators[6].GetAddress(), 6)
addr, err := privValidators[6].GetAddress()
assert.NoError(t, err)
vote := withValidator(voteProto, addr, 6)
vote = withBlockHash(vote, cmn.RandBytes(32))
vote = withBlockPartsHeader(vote, PartSetHeader{123, cmn.RandBytes(32)})
_, err := signAddVote(privValidators[6], vote, voteSet)
_, err = signAddVote(privValidators[6], vote, voteSet)
if err != nil {
t.Error(err)
}
@ -486,8 +529,10 @@ func TestMakeCommit(t *testing.T) {
// The 8th voted like everyone else.
{
vote := withValidator(voteProto, privValidators[7].GetAddress(), 7)
_, err := signAddVote(privValidators[7], vote, voteSet)
addr, err := privValidators[7].GetAddress()
assert.NoError(t, err)
vote := withValidator(voteProto, addr, 7)
_, err = signAddVote(privValidators[7], vote, voteSet)
if err != nil {
t.Error(err)
}

View File

@ -140,13 +140,14 @@ func TestVoteProposalNotEq(t *testing.T) {
func TestVoteVerifySignature(t *testing.T) {
privVal := NewMockPV()
pubkey := privVal.GetPubKey()
pubkey, err := privVal.GetPubKey()
assert.NoError(t, err)
vote := examplePrecommit()
signBytes := vote.SignBytes("test_chain_id")
// sign it
err := privVal.SignVote("test_chain_id", vote)
err = privVal.SignVote("test_chain_id", vote)
require.NoError(t, err)
// verify the same vote
@ -190,12 +191,13 @@ func TestIsVoteTypeValid(t *testing.T) {
func TestVoteVerify(t *testing.T) {
privVal := NewMockPV()
pubkey := privVal.GetPubKey()
pubkey, err := privVal.GetPubKey()
assert.NoError(t, err)
vote := examplePrevote()
vote.ValidatorAddress = pubkey.Address()
err := vote.Verify("test_chain_id", ed25519.GenPrivKey().PubKey())
err = vote.Verify("test_chain_id", ed25519.GenPrivKey().PubKey())
if assert.Error(t, err) {
assert.Equal(t, ErrVoteInvalidValidatorAddress, err)
}