From 3547ce2516971ad6aec7547a91d2b8c48db81324 Mon Sep 17 00:00:00 2001 From: Ismail Khoffi Date: Wed, 12 Dec 2018 13:59:40 +0100 Subject: [PATCH] Rework PR to memoize public key on init: - split PubKeyMsg into PubKeyRequest / PubKeyResponse like other msgs - NewRemoteSignerClient sends a pubkey request (once), return this err OnStart() - NewRemoteSignerClient returns an error if retrieving the pubkey fails - remove GetAddress from PrivValidator interface - GetPublicKey doesn't err but returns memoized public key from init - fix tests --- blockchain/reactor_test.go | 5 +- cmd/tendermint/commands/init.go | 5 +- cmd/tendermint/commands/show_validator.go | 9 +-- cmd/tendermint/commands/testnet.go | 5 +- consensus/common_test.go | 22 ++---- consensus/reactor_test.go | 25 +++--- consensus/replay_test.go | 5 +- consensus/state.go | 32 ++------ consensus/state_test.go | 42 ++++------ consensus/types/height_vote_set_test.go | 7 +- node/node.go | 15 +--- privval/ipc.go | 5 +- privval/priv_validator.go | 12 +-- privval/priv_validator_test.go | 17 ++-- privval/remote_signer.go | 62 +++++++-------- privval/tcp.go | 6 +- privval/tcp_test.go | 23 +++--- types/evidence_test.go | 10 +-- types/priv_validator.go | 27 ++----- types/proposal_test.go | 8 +- types/protobuf_test.go | 5 +- types/test_util.go | 7 +- types/validator.go | 2 +- types/vote_set_test.go | 94 +++++++++-------------- types/vote_test.go | 11 +-- 25 files changed, 158 insertions(+), 303 deletions(-) diff --git a/blockchain/reactor_test.go b/blockchain/reactor_test.go index 74b25bd62..f6c29d65d 100644 --- a/blockchain/reactor_test.go +++ b/blockchain/reactor_test.go @@ -42,10 +42,7 @@ 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, err := privVal.GetAddress() - if err != nil { - panic(err) - } + addr := privVal.GetPubKey().Address() idx, _ := valset.GetByAddress(addr) vote := &types.Vote{ ValidatorAddress: addr, diff --git a/cmd/tendermint/commands/init.go b/cmd/tendermint/commands/init.go index 4c62d6518..9472dcff1 100644 --- a/cmd/tendermint/commands/init.go +++ b/cmd/tendermint/commands/init.go @@ -57,10 +57,7 @@ func initFilesWithConfig(config *cfg.Config) error { GenesisTime: tmtime.Now(), ConsensusParams: types.DefaultConsensusParams(), } - key, err := pv.GetPubKey() - if err != nil { - return err - } + key := pv.GetPubKey() genDoc.Validators = []types.GenesisValidator{{ Address: key.Address(), PubKey: key, diff --git a/cmd/tendermint/commands/show_validator.go b/cmd/tendermint/commands/show_validator.go index a79fb8bd2..ad12dbb11 100644 --- a/cmd/tendermint/commands/show_validator.go +++ b/cmd/tendermint/commands/show_validator.go @@ -2,7 +2,6 @@ package commands import ( "fmt" - "os" "github.com/spf13/cobra" @@ -17,12 +16,10 @@ var ShowValidatorCmd = &cobra.Command{ } func showValidator(cmd *cobra.Command, args []string) { + // TODO(ismail): add a flag and check if we actually want to see the pub key + // of the remote signer instead of the FilePV privValidator := privval.LoadOrGenFilePV(config.PrivValidatorFile()) - key, err := privValidator.GetPubKey() - if err != nil { - fmt.Fprintf(os.Stderr, "Failed to read pubkey from private validator file (%s): %v", config.PrivValidatorFile(), err) - os.Exit(1) - } + key := privValidator.GetPubKey() pubKeyJSONBytes, _ := cdc.MarshalJSON(key) fmt.Println(string(pubKeyJSONBytes)) } diff --git a/cmd/tendermint/commands/testnet.go b/cmd/tendermint/commands/testnet.go index e5fce778f..1c00844df 100644 --- a/cmd/tendermint/commands/testnet.go +++ b/cmd/tendermint/commands/testnet.go @@ -90,10 +90,7 @@ 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 - } + pubKey := pv.GetPubKey() genVals[i] = types.GenesisValidator{ Address: pubKey.Address(), PubKey: pubKey, diff --git a/consensus/common_test.go b/consensus/common_test.go index fc20b7be6..f2ac4ab6a 100644 --- a/consensus/common_test.go +++ b/consensus/common_test.go @@ -72,10 +72,7 @@ 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 - } + addr := vs.PrivValidator.GetPubKey().Address() vote := &types.Vote{ ValidatorIndex: vs.Index, ValidatorAddress: addr, @@ -85,7 +82,7 @@ func (vs *validatorStub) signVote(voteType types.SignedMsgType, hash []byte, hea Type: voteType, BlockID: types.BlockID{hash, header}, } - err = vs.PrivValidator.SignVote(config.ChainID(), vote) + err := vs.PrivValidator.SignVote(config.ChainID(), vote) return vote, err } @@ -155,10 +152,7 @@ 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) - } + address := privVal.GetPubKey().Address() var vote *types.Vote if vote = prevotes.GetByAddress(address); vote == nil { panic("Failed to find prevote from validator") @@ -176,10 +170,7 @@ 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) - } + address := privVal.GetPubKey().Address() var vote *types.Vote if vote = votes.GetByAddress(address); vote == nil { panic("Failed to find precommit from validator") @@ -191,10 +182,7 @@ 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) - } + address := privVal.GetPubKey().Address() var vote *types.Vote if vote = precommits.GetByAddress(address); vote == nil { panic("Failed to find precommit from validator") diff --git a/consensus/reactor_test.go b/consensus/reactor_test.go index 8474734fe..a35a3d9ff 100644 --- a/consensus/reactor_test.go +++ b/consensus/reactor_test.go @@ -143,8 +143,7 @@ func TestReactorWithEvidence(t *testing.T) { // mock the evidence pool // everyone includes evidence of another double signing vIdx := (i + 1) % nValidators - addr, err := privVals[vIdx].GetAddress() - require.NoError(t, err) + addr := privVals[vIdx].GetPubKey().Address() evpool := newMockEvidencePool(addr) // Make ConsensusState @@ -270,8 +269,7 @@ func TestReactorVotingPowerChange(t *testing.T) { // map of active validators activeVals := make(map[string]struct{}) for i := 0; i < nVals; i++ { - addr, err := css[i].privValidator.GetAddress() - require.NoError(t, err) + addr := css[i].privValidator.GetPubKey().Address() activeVals[string(addr)] = struct{}{} } @@ -283,8 +281,8 @@ func TestReactorVotingPowerChange(t *testing.T) { //--------------------------------------------------------------------------- logger.Debug("---------------------------- Testing changing the voting power of one validator a few times") - pubKey, err := css[0].privValidator.GetPubKey() - require.NoError(t, err) + pubKey := css[0].privValidator.GetPubKey() + val1PubKey := pubKey val1PubKeyABCI := types.TM2PB.PubKey(val1PubKey) updateValidatorTx := kvstore.MakeValSetChangeTx(val1PubKeyABCI, 25) @@ -337,8 +335,7 @@ func TestReactorValidatorSetChanges(t *testing.T) { // map of active validators activeVals := make(map[string]struct{}) for i := 0; i < nVals; i++ { - addr, err := css[i].privValidator.GetAddress() - require.NoError(t, err) + addr := css[i].privValidator.GetPubKey().Address() activeVals[string(addr)] = struct{}{} } @@ -350,8 +347,7 @@ func TestReactorValidatorSetChanges(t *testing.T) { //--------------------------------------------------------------------------- logger.Info("---------------------------- Testing adding one validator") - pubKey, err := css[nVals].privValidator.GetPubKey() - require.NoError(t, err) + pubKey := css[nVals].privValidator.GetPubKey() newValidatorPubKey1 := pubKey valPubKey1ABCI := types.TM2PB.PubKey(newValidatorPubKey1) newValidatorTx1 := kvstore.MakeValSetChangeTx(valPubKey1ABCI, testMinPower) @@ -379,8 +375,7 @@ func TestReactorValidatorSetChanges(t *testing.T) { //--------------------------------------------------------------------------- logger.Info("---------------------------- Testing changing the voting power of one validator") - pubKey, err = css[nVals].privValidator.GetPubKey() - require.NoError(t, err) + pubKey = css[nVals].privValidator.GetPubKey() updateValidatorPubKey1 := pubKey updatePubKey1ABCI := types.TM2PB.PubKey(updateValidatorPubKey1) updateValidatorTx1 := kvstore.MakeValSetChangeTx(updatePubKey1ABCI, 25) @@ -398,14 +393,12 @@ func TestReactorValidatorSetChanges(t *testing.T) { //--------------------------------------------------------------------------- logger.Info("---------------------------- Testing adding two validators at once") - pubKey, err = css[nVals+1].privValidator.GetPubKey() - require.NoError(t, err) + pubKey = css[nVals+1].privValidator.GetPubKey() newValidatorPubKey2 := pubKey newVal2ABCI := types.TM2PB.PubKey(newValidatorPubKey2) newValidatorTx2 := kvstore.MakeValSetChangeTx(newVal2ABCI, testMinPower) - pubKey, err = css[nVals+2].privValidator.GetPubKey() - require.NoError(t, err) + pubKey = css[nVals+2].privValidator.GetPubKey() newValidatorPubKey3 := pubKey newVal3ABCI := types.TM2PB.PubKey(newValidatorPubKey3) newValidatorTx3 := kvstore.MakeValSetChangeTx(newVal3ABCI, testMinPower) diff --git a/consensus/replay_test.go b/consensus/replay_test.go index f9e6ec60c..540e4a58c 100644 --- a/consensus/replay_test.go +++ b/consensus/replay_test.go @@ -331,7 +331,7 @@ func testHandshakeReplay(t *testing.T, nBlocks int, mode uint) { chain, commits, err := makeBlockchainFromWAL(wal) require.NoError(t, err) - pubKey, err := privVal.GetPubKey() + pubKey := privVal.GetPubKey() require.NoError(t, err) stateDB, state, store := stateAndStore(config, pubKey, kvstore.ProtocolVersion) store.chain = chain @@ -636,8 +636,7 @@ func TestInitChainUpdateValidators(t *testing.T) { config := ResetConfig("proxy_test_") privVal := privval.LoadFilePV(config.PrivValidatorFile()) - pubKey, err := privVal.GetPubKey() - require.NoError(t, err) + pubKey := privVal.GetPubKey() stateDB, state, store := stateAndStore(config, pubKey, 0x0) oldValAddr := state.Validators.Validators[0].Address diff --git a/consensus/state.go b/consensus/state.go index f5766b584..30122b7d8 100644 --- a/consensus/state.go +++ b/consensus/state.go @@ -830,11 +830,7 @@ func (cs *ConsensusState) enterPropose(height int64, round int) { } // if not a validator, we're done - address, err := cs.privValidator.GetAddress() - if err != nil { - logger.Error("enterPropose: Can not propose without validator's address", "err", err) - return - } + address := cs.privValidator.GetPubKey().Address() if !cs.Validators.HasAddress(address) { logger.Debug("This node is not a validator", "addr", address, "vals", cs.Validators) return @@ -935,11 +931,7 @@ func (cs *ConsensusState) createProposalBlock() (block *types.Block, blockParts cs.state.Validators.Size(), len(evidence), ), maxGas) - proposerAddr, err := cs.privValidator.GetAddress() - if err != nil { - cs.Logger.Error("enterPropose: Cannot create block without private validator's address", "err", err) - return - } + proposerAddr := cs.privValidator.GetPubKey().Address() block, parts := cs.state.MakeBlock(cs.Height, txs, commit, evidence, proposerAddr) return block, parts @@ -1484,11 +1476,7 @@ 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 { - addr, err := cs.privValidator.GetAddress() - if err != nil { - cs.Logger.Error("Can not add vote without validator's address", "err", err) - return added, err - } + addr := cs.privValidator.GetPubKey().Address() 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 @@ -1654,11 +1642,7 @@ 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, err := cs.privValidator.GetAddress() - if err != nil { - cs.Logger.Error("Can not sign vote without private validator's address", "err", err) - return nil, err - } + addr := cs.privValidator.GetPubKey().Address() valIndex, _ := cs.Validators.GetByAddress(addr) vote := &types.Vote{ @@ -1670,7 +1654,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 } @@ -1694,11 +1678,7 @@ 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 - privValAddr, err := cs.privValidator.GetAddress() - if err != nil { - cs.Logger.Error("Can not sign vote without validator's address", "err", err, "height", cs.Height, "round", cs.Round) - return nil - } + privValAddr := cs.privValidator.GetPubKey().Address() if cs.privValidator == nil || !cs.Validators.HasAddress(privValAddr) { return nil } diff --git a/consensus/state_test.go b/consensus/state_test.go index 630d0c677..40103e472 100644 --- a/consensus/state_test.go +++ b/consensus/state_test.go @@ -73,8 +73,7 @@ func TestStateProposerSelection0(t *testing.T) { // Commit a block and ensure proposer for the next height is correct. prop := cs1.GetRoundState().Validators.GetProposer() - address, err := cs1.privValidator.GetAddress() - require.NoError(t, err) + address := cs1.privValidator.GetPubKey().Address() if !bytes.Equal(prop.Address, address) { t.Fatalf("expected proposer to be validator %d. Got %X", 0, prop.Address) } @@ -89,8 +88,7 @@ func TestStateProposerSelection0(t *testing.T) { ensureNewRound(newRoundCh, height+1, 0) prop = cs1.GetRoundState().Validators.GetProposer() - addr, err := vss[1].GetAddress() - require.NoError(t, err) + addr := vss[1].GetPubKey().Address() if !bytes.Equal(prop.Address, addr) { panic(fmt.Sprintf("expected proposer to be validator %d. Got %X", 1, prop.Address)) } @@ -114,8 +112,7 @@ 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() - addr, err := vss[(i+round)%len(vss)].GetAddress() - require.NoError(t, err) + addr := vss[(i+round)%len(vss)].GetPubKey().Address() 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)) @@ -511,8 +508,7 @@ func TestStateLockPOLRelock(t *testing.T) { timeoutWaitCh := subscribe(cs1.eventBus, types.EventQueryTimeoutWait) proposalCh := subscribe(cs1.eventBus, types.EventQueryCompleteProposal) - addr, err := cs1.privValidator.GetAddress() - require.NoError(t, err) + addr := cs1.privValidator.GetPubKey().Address() voteCh := subscribeToVoter(cs1, addr) newRoundCh := subscribe(cs1.eventBus, types.EventQueryNewRound) newBlockCh := subscribe(cs1.eventBus, types.EventQueryNewBlockHeader) @@ -604,8 +600,7 @@ func TestStateLockPOLUnlock(t *testing.T) { timeoutWaitCh := subscribe(cs1.eventBus, types.EventQueryTimeoutWait) newRoundCh := subscribe(cs1.eventBus, types.EventQueryNewRound) unlockCh := subscribe(cs1.eventBus, types.EventQueryUnlock) - addr, err := cs1.privValidator.GetAddress() - require.NoError(t, err) + addr := cs1.privValidator.GetPubKey().Address() voteCh := subscribeToVoter(cs1, addr) // everything done from perspective of cs1 @@ -699,8 +694,7 @@ func TestStateLockPOLSafety1(t *testing.T) { timeoutProposeCh := subscribe(cs1.eventBus, types.EventQueryTimeoutPropose) timeoutWaitCh := subscribe(cs1.eventBus, types.EventQueryTimeoutWait) newRoundCh := subscribe(cs1.eventBus, types.EventQueryNewRound) - addr, err := cs1.privValidator.GetAddress() - require.NoError(t, err) + addr := cs1.privValidator.GetPubKey().Address() voteCh := subscribeToVoter(cs1, addr) // start round and wait for propose and prevote @@ -817,8 +811,7 @@ func TestStateLockPOLSafety2(t *testing.T) { timeoutWaitCh := subscribe(cs1.eventBus, types.EventQueryTimeoutWait) newRoundCh := subscribe(cs1.eventBus, types.EventQueryNewRound) unlockCh := subscribe(cs1.eventBus, types.EventQueryUnlock) - addr, err := cs1.privValidator.GetAddress() - require.NoError(t, err) + addr := cs1.privValidator.GetPubKey().Address() voteCh := subscribeToVoter(cs1, addr) // the block for R0: gets polkad but we miss it @@ -910,8 +903,7 @@ func TestProposeValidBlock(t *testing.T) { timeoutProposeCh := subscribe(cs1.eventBus, types.EventQueryTimeoutPropose) newRoundCh := subscribe(cs1.eventBus, types.EventQueryNewRound) unlockCh := subscribe(cs1.eventBus, types.EventQueryUnlock) - addr, err := cs1.privValidator.GetAddress() - require.NoError(t, err) + addr := cs1.privValidator.GetPubKey().Address() voteCh := subscribeToVoter(cs1, addr) // start round and wait for propose and prevote @@ -998,8 +990,7 @@ func TestSetValidBlockOnDelayedPrevote(t *testing.T) { timeoutWaitCh := subscribe(cs1.eventBus, types.EventQueryTimeoutWait) newRoundCh := subscribe(cs1.eventBus, types.EventQueryNewRound) validBlockCh := subscribe(cs1.eventBus, types.EventQueryValidBlock) - addr, err := cs1.privValidator.GetAddress() - require.NoError(t, err) + addr := cs1.privValidator.GetPubKey().Address() voteCh := subscribeToVoter(cs1, addr) // start round and wait for propose and prevote @@ -1059,8 +1050,7 @@ func TestSetValidBlockOnDelayedProposal(t *testing.T) { timeoutProposeCh := subscribe(cs1.eventBus, types.EventQueryTimeoutPropose) newRoundCh := subscribe(cs1.eventBus, types.EventQueryNewRound) validBlockCh := subscribe(cs1.eventBus, types.EventQueryValidBlock) - addr, err := cs1.privValidator.GetAddress() - require.NoError(t, err) + addr := cs1.privValidator.GetPubKey().Address() voteCh := subscribeToVoter(cs1, addr) proposalCh := subscribe(cs1.eventBus, types.EventQueryCompleteProposal) @@ -1131,8 +1121,7 @@ func TestWaitingTimeoutProposeOnNewRound(t *testing.T) { timeoutWaitCh := subscribe(cs1.eventBus, types.EventQueryTimeoutPropose) newRoundCh := subscribe(cs1.eventBus, types.EventQueryNewRound) - addr, err := cs1.privValidator.GetAddress() - require.NoError(t, err) + addr := cs1.privValidator.GetPubKey().Address() voteCh := subscribeToVoter(cs1, addr) // start round @@ -1166,8 +1155,7 @@ func TestRoundSkipOnNilPolkaFromHigherRound(t *testing.T) { timeoutWaitCh := subscribe(cs1.eventBus, types.EventQueryTimeoutWait) newRoundCh := subscribe(cs1.eventBus, types.EventQueryNewRound) - addr, err := cs1.privValidator.GetAddress() - require.NoError(t, err) + addr := cs1.privValidator.GetPubKey().Address() voteCh := subscribeToVoter(cs1, addr) // start round @@ -1201,8 +1189,7 @@ func TestWaitTimeoutProposeOnNilPolkaForTheCurrentRound(t *testing.T) { timeoutProposeCh := subscribe(cs1.eventBus, types.EventQueryTimeoutPropose) newRoundCh := subscribe(cs1.eventBus, types.EventQueryNewRound) - addr, err := cs1.privValidator.GetAddress() - require.NoError(t, err) + addr := cs1.privValidator.GetPubKey().Address() voteCh := subscribeToVoter(cs1, addr) // start round in which PO is not proposer @@ -1387,8 +1374,7 @@ func TestStateHalt1(t *testing.T) { timeoutWaitCh := subscribe(cs1.eventBus, types.EventQueryTimeoutWait) newRoundCh := subscribe(cs1.eventBus, types.EventQueryNewRound) newBlockCh := subscribe(cs1.eventBus, types.EventQueryNewBlock) - addr, err := cs1.privValidator.GetAddress() - require.NoError(t, err) + addr := cs1.privValidator.GetPubKey().Address() voteCh := subscribeToVoter(cs1, addr) // start round and wait for propose and prevote diff --git a/consensus/types/height_vote_set_test.go b/consensus/types/height_vote_set_test.go index 7b49ee57d..4460cd3ec 100644 --- a/consensus/types/height_vote_set_test.go +++ b/consensus/types/height_vote_set_test.go @@ -50,10 +50,7 @@ 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) - } + addr := privVal.GetPubKey().Address() vote := &types.Vote{ ValidatorAddress: addr, ValidatorIndex: valIndex, @@ -64,7 +61,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 diff --git a/node/node.go b/node/node.go index 76b9ce033..00effa2ef 100644 --- a/node/node.go +++ b/node/node.go @@ -240,19 +240,13 @@ func NewNode(config *cfg.Config, fastSync := config.FastSync if state.Validators.Size() == 1 { addr, _ := state.Validators.GetByIndex(0) - privValAddr, err := privValidator.GetAddress() - if err != nil { - return nil, err - } + privValAddr := privValidator.GetPubKey().Address() if bytes.Equal(privValAddr, addr) { fastSync = false } } - pubKey, err := privValidator.GetPubKey() - if err != nil { - return nil, err - } + pubKey := privValidator.GetPubKey() addr := pubKey.Address() // Log whether this node is a validator or an observer if state.Validators.HasAddress(addr) { @@ -625,10 +619,7 @@ func (n *Node) ConfigureRPC() { rpccore.SetEvidencePool(n.evidencePool) rpccore.SetP2PPeers(n.sw) rpccore.SetP2PTransport(n) - pubKey, err := n.privValidator.GetPubKey() - if err != nil { - n.Logger.Error("Error configuring RPC", "err", err) - } + pubKey := n.privValidator.GetPubKey() rpccore.SetPubKey(pubKey) rpccore.SetGenesisDoc(n.genesisDoc) rpccore.SetAddrBook(n.addrBook) diff --git a/privval/ipc.go b/privval/ipc.go index eda23fe6f..1c82db33f 100644 --- a/privval/ipc.go +++ b/privval/ipc.go @@ -67,7 +67,10 @@ func (sc *IPCVal) OnStart() error { return err } - sc.RemoteSignerClient = NewRemoteSignerClient(sc.conn) + sc.RemoteSignerClient, err = NewRemoteSignerClient(sc.conn) + if err != nil { + return err + } // Start a routine to keep the connection alive sc.cancelPing = make(chan struct{}, 1) diff --git a/privval/priv_validator.go b/privval/priv_validator.go index ebd3e780b..4ac652bcd 100644 --- a/privval/priv_validator.go +++ b/privval/priv_validator.go @@ -56,16 +56,10 @@ type FilePV struct { mtx sync.Mutex } -// GetAddress returns the address of the validator. -// Implements PrivValidator. -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, error) { - return pv.PubKey, nil +func (pv *FilePV) GetPubKey() crypto.PubKey { + return pv.PubKey } // GenFilePV generates a new validator with randomly generated private key @@ -293,7 +287,7 @@ func (pv *FilePV) saveSigned(height int64, round int, step int8, // String returns a string representation of the FilePV. func (pv *FilePV) String() string { // does not error in FilePV: - addr, _ := pv.GetAddress() + addr := pv.GetPubKey().Address() return fmt.Sprintf("PrivValidator{%v LH:%v, LR:%v, LS:%v}", addr, pv.LastHeight, pv.LastRound, pv.LastStep) } diff --git a/privval/priv_validator_test.go b/privval/priv_validator_test.go index 6b7f1b036..8dfc03334 100644 --- a/privval/priv_validator_test.go +++ b/privval/priv_validator_test.go @@ -25,12 +25,10 @@ func TestGenLoadValidator(t *testing.T) { height := int64(100) privVal.LastHeight = height privVal.Save() - addr, err := privVal.GetAddress() - require.NoError(t, err) + addr := privVal.GetPubKey().Address() privVal = LoadFilePV(tempFile.Name()) - loadedAddr, err := privVal.GetAddress() - require.NoError(t, err) + loadedAddr := privVal.GetPubKey().Address() assert.Equal(addr, loadedAddr) assert.Equal(height, privVal.LastHeight, "expected privval.LastHeight to have been saved") } @@ -45,10 +43,9 @@ func TestLoadOrGenValidator(t *testing.T) { t.Error(err) } privVal := LoadOrGenFilePV(tempFilePath) - addr, err := privVal.GetAddress() - require.NoError(t, err) + addr := privVal.GetPubKey().Address() privVal = LoadOrGenFilePV(tempFilePath) - loadedAddr, err := privVal.GetAddress() + loadedAddr := privVal.GetPubKey().Address() assert.Equal(addr, loadedAddr) } @@ -86,11 +83,9 @@ func TestUnmarshalValidator(t *testing.T) { require.Nil(err, "%+v", err) // make sure the values match - loadedAddr, err := val.GetAddress() - require.NoError(err) + loadedAddr := val.GetPubKey().Address() assert.EqualValues(addr, loadedAddr) - loadedKey, err := val.GetPubKey() - require.NoError(err) + loadedKey := val.GetPubKey() assert.EqualValues(pubKey, loadedKey) assert.EqualValues(privKey, val.PrivKey) diff --git a/privval/remote_signer.go b/privval/remote_signer.go index 8675155ea..ed2a28773 100644 --- a/privval/remote_signer.go +++ b/privval/remote_signer.go @@ -17,8 +17,9 @@ import ( // RemoteSignerClient implements PrivValidator, it uses a socket to request signatures // from an external process. type RemoteSignerClient struct { - conn net.Conn - lock sync.Mutex + conn net.Conn + consensusPubKey crypto.PubKey + lock sync.Mutex } // Check that RemoteSignerClient implements PrivValidator. @@ -27,38 +28,29 @@ var _ types.PrivValidator = (*RemoteSignerClient)(nil) // NewRemoteSignerClient returns an instance of RemoteSignerClient. func NewRemoteSignerClient( conn net.Conn, -) *RemoteSignerClient { +) (*RemoteSignerClient, error) { sc := &RemoteSignerClient{ conn: conn, } - return sc -} - -// GetAddress implements PrivValidator. -func (sc *RemoteSignerClient) GetAddress() (types.Address, error) { pubKey, err := sc.getPubKey() if err != nil { - return nil, errors.Wrap(err, "failed to get private validator's public key") + return nil, cmn.ErrorWrap(err, "error while retrieving public key for remote signer") } - - return pubKey.Address(), nil + // retrieve and memoize the consensus public key once: + sc.consensusPubKey = pubKey + return sc, nil } // GetPubKey implements PrivValidator. -func (sc *RemoteSignerClient) GetPubKey() (crypto.PubKey, error) { - pubKey, err := sc.getPubKey() - if err != nil { - return nil, errors.Wrap(err, "failed to get private validator's address") - } - - return pubKey, nil +func (sc *RemoteSignerClient) GetPubKey() crypto.PubKey { + return sc.consensusPubKey } func (sc *RemoteSignerClient) getPubKey() (crypto.PubKey, error) { sc.lock.Lock() defer sc.lock.Unlock() - err := writeMsg(sc.conn, &PubKeyMsg{}) + err := writeMsg(sc.conn, &PubKeyRequest{}) if err != nil { return nil, err } @@ -67,8 +59,16 @@ func (sc *RemoteSignerClient) getPubKey() (crypto.PubKey, error) { if err != nil { return nil, err } + pubKeyResp, ok := res.(*PubKeyResponse) + if !ok { + return nil, errors.Wrap(ErrUnexpectedResponse, "response is not PubKeyResponse") + } - return res.(*PubKeyMsg).PubKey, nil + if pubKeyResp.Error != nil { + return nil, errors.Wrap(pubKeyResp.Error, "failed to get private validator's public key") + } + + return pubKeyResp.PubKey, nil } // SignVote implements PrivValidator. @@ -154,7 +154,8 @@ type RemoteSignerMsg interface{} func RegisterRemoteSignerMsg(cdc *amino.Codec) { cdc.RegisterInterface((*RemoteSignerMsg)(nil), nil) - cdc.RegisterConcrete(&PubKeyMsg{}, "tendermint/remotesigner/PubKeyMsg", nil) + cdc.RegisterConcrete(&PubKeyRequest{}, "tendermint/remotesigner/PubKeyRequest", nil) + cdc.RegisterConcrete(&PubKeyResponse{}, "tendermint/remotesigner/PubKeyResponse", nil) cdc.RegisterConcrete(&SignVoteRequest{}, "tendermint/remotesigner/SignVoteRequest", nil) cdc.RegisterConcrete(&SignedVoteResponse{}, "tendermint/remotesigner/SignedVoteResponse", nil) cdc.RegisterConcrete(&SignProposalRequest{}, "tendermint/remotesigner/SignProposalRequest", nil) @@ -163,9 +164,13 @@ func RegisterRemoteSignerMsg(cdc *amino.Codec) { cdc.RegisterConcrete(&PingResponse{}, "tendermint/remotesigner/PingResponse", nil) } -// PubKeyMsg is a PrivValidatorSocket message containing the public key. -type PubKeyMsg struct { +// PubKeyRequest requests the consensus public key from the remote signer. +type PubKeyRequest struct{} + +// PubKeyResponse is a PrivValidatorSocket message containing the public key. +type PubKeyResponse struct { PubKey crypto.PubKey + Error *RemoteSignerError } // SignVoteRequest is a PrivValidatorSocket message containing a vote. @@ -229,15 +234,10 @@ func handleRequest(req RemoteSignerMsg, chainID string, privVal types.PrivValida var err error switch r := req.(type) { - case *PubKeyMsg: + case *PubKeyRequest: var p crypto.PubKey - p, err = privVal.GetPubKey() - if err != nil { - // TODO: split up PubKeyMsg into PubKeyRequest / PubKeyResponse and wrap the error - // into the response as done below. For now we just return the error: - return nil, err - } - res = &PubKeyMsg{p} + p = privVal.GetPubKey() + res = &PubKeyResponse{p, nil} case *SignVoteRequest: err = privVal.SignVote(chainID, r.Vote) if err != nil { diff --git a/privval/tcp.go b/privval/tcp.go index 11bd833c0..1fb736e6c 100644 --- a/privval/tcp.go +++ b/privval/tcp.go @@ -107,8 +107,10 @@ func (sc *TCPVal) OnStart() error { } sc.conn = conn - - sc.RemoteSignerClient = NewRemoteSignerClient(sc.conn) + sc.RemoteSignerClient, err = NewRemoteSignerClient(sc.conn) + if err != nil { + return err + } // Start a routine to keep the connection alive sc.cancelPing = make(chan struct{}, 1) diff --git a/privval/tcp_test.go b/privval/tcp_test.go index c407ef87e..98056cdff 100644 --- a/privval/tcp_test.go +++ b/privval/tcp_test.go @@ -25,11 +25,8 @@ func TestSocketPVAddress(t *testing.T) { defer sc.Stop() defer rs.Stop() - serverAddr, err := rs.privVal.GetAddress() - require.NoError(t, err) - - clientAddr, err := sc.GetAddress() - require.NoError(t, err) + serverAddr := rs.privVal.GetPubKey().Address() + clientAddr := sc.GetPubKey().Address() assert.Equal(t, serverAddr, clientAddr) } @@ -45,10 +42,10 @@ func TestSocketPVPubKey(t *testing.T) { clientKey, err := sc.getPubKey() require.NoError(t, err) - privKey, err := rs.privVal.GetPubKey() + privvalPubKey := rs.privVal.GetPubKey() require.NoError(t, err) - assert.Equal(t, privKey, clientKey) + assert.Equal(t, privvalPubKey, clientKey) } func TestSocketPVProposal(t *testing.T) { @@ -149,9 +146,9 @@ func TestSocketPVDeadline(t *testing.T) { go func(sc *TCPVal) { defer close(listenc) - require.NoError(t, sc.Start()) + assert.Equal(t, sc.Start().(cmn.Error).Data(), ErrConnTimeout) - assert.True(t, sc.IsRunning()) + assert.False(t, sc.IsRunning()) }(sc) for { @@ -170,9 +167,6 @@ func TestSocketPVDeadline(t *testing.T) { } <-listenc - - _, err := sc.getPubKey() - assert.Equal(t, err.(cmn.Error).Data(), ErrConnTimeout) } func TestRemoteSignerRetry(t *testing.T) { @@ -306,14 +300,15 @@ func TestErrUnexpectedResponse(t *testing.T) { testStartSocketPV(t, readyc, sc) defer sc.Stop() RemoteSignerConnDeadline(time.Millisecond)(rs) - RemoteSignerConnRetries(1e6)(rs) - + RemoteSignerConnRetries(100)(rs) // we do not want to Start() the remote signer here and instead use the connection to // reply with intentionally wrong replies below: rsConn, err := rs.connect() defer rsConn.Close() require.NoError(t, err) require.NotNil(t, rsConn) + // send over public key to get the remote signer running: + go testReadWriteResponse(t, &PubKeyResponse{}, rsConn) <-readyc // Proposal: diff --git a/types/evidence_test.go b/types/evidence_test.go index 95de44301..194271503 100644 --- a/types/evidence_test.go +++ b/types/evidence_test.go @@ -17,10 +17,7 @@ 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) - } + addr := val.GetPubKey().Address() v := &Vote{ ValidatorAddress: addr, ValidatorIndex: valIndex, @@ -29,7 +26,7 @@ func makeVote(val PrivValidator, chainID string, valIndex int, height int64, rou Type: SignedMsgType(step), BlockID: blockID, } - err = val.SignVote(chainID, v) + err := val.SignVote(chainID, v) if err != nil { panic(err) } @@ -68,8 +65,7 @@ func TestEvidence(t *testing.T) { {vote1, badVote, false}, // signed by wrong key } - pubKey, err := val.GetPubKey() - require.NoError(t, err) + pubKey := val.GetPubKey() for _, c := range cases { ev := &DuplicateVoteEvidence{ VoteA: c.vote1, diff --git a/types/priv_validator.go b/types/priv_validator.go index 4e185f49a..f0a19f401 100644 --- a/types/priv_validator.go +++ b/types/priv_validator.go @@ -12,10 +12,7 @@ import ( // PrivValidator defines the functionality of a local Tendermint validator // that signs votes and proposals, and never double signs. type PrivValidator interface { - // 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) + GetPubKey() crypto.PubKey SignVote(chainID string, vote *Vote) error SignProposal(chainID string, proposal *Proposal) error @@ -31,16 +28,7 @@ func (pvs PrivValidatorsByAddress) Len() int { } func (pvs PrivValidatorsByAddress) Less(i, j int) bool { - // 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 + return bytes.Compare(pvs[i].GetPubKey().Address(), pvs[j].GetPubKey().Address()) == -1 } func (pvs PrivValidatorsByAddress) Swap(i, j int) { @@ -63,13 +51,8 @@ func NewMockPV() *MockPV { } // Implements PrivValidator. -func (pv *MockPV) GetAddress() (Address, error) { - return pv.privKey.PubKey().Address(), nil -} - -// Implements PrivValidator. -func (pv *MockPV) GetPubKey() (crypto.PubKey, error) { - return pv.privKey.PubKey(), nil +func (pv *MockPV) GetPubKey() crypto.PubKey { + return pv.privKey.PubKey() } // Implements PrivValidator. @@ -96,7 +79,7 @@ func (pv *MockPV) SignProposal(chainID string, proposal *Proposal) error { // String returns a string representation of the MockPV. func (pv *MockPV) String() string { - addr, _ := pv.GetAddress() + addr := pv.GetPubKey().Address() return fmt.Sprintf("MockPV{%v}", addr) } diff --git a/types/proposal_test.go b/types/proposal_test.go index d267747b9..f1c048e1d 100644 --- a/types/proposal_test.go +++ b/types/proposal_test.go @@ -45,8 +45,7 @@ func TestProposalString(t *testing.T) { func TestProposalVerifySignature(t *testing.T) { privVal := NewMockPV() - pubKey, err := privVal.GetPubKey() - require.NoError(t, err) + pubKey := privVal.GetPubKey() prop := NewProposal( 4, 2, 2, @@ -54,7 +53,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 @@ -95,8 +94,7 @@ func BenchmarkProposalVerifySignature(b *testing.B) { privVal := NewMockPV() err := privVal.SignProposal("test_chain_id", testProposal) require.Nil(b, err) - pubKey, err := privVal.GetPubKey() - require.NoError(b, err) + pubKey := privVal.GetPubKey() for i := 0; i < b.N; i++ { pubKey.VerifyBytes(testProposal.SignBytes("test_chain_id"), testProposal.Signature) diff --git a/types/protobuf_test.go b/types/protobuf_test.go index 00b53f80f..18acf57a6 100644 --- a/types/protobuf_test.go +++ b/types/protobuf_test.go @@ -4,8 +4,6 @@ import ( "testing" "time" - "github.com/stretchr/testify/require" - "github.com/golang/protobuf/proto" "github.com/stretchr/testify/assert" @@ -144,8 +142,7 @@ 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() - require.NoError(t, err) + pubKey := val.GetPubKey() ev := &DuplicateVoteEvidence{ PubKey: pubKey, VoteA: makeVote(val, chainID, 0, 10, 2, 1, blockID), diff --git a/types/test_util.go b/types/test_util.go index 14e9f9632..18e472148 100644 --- a/types/test_util.go +++ b/types/test_util.go @@ -10,10 +10,7 @@ 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 - } + addr := validators[i].GetPubKey().Address() vote := &Vote{ ValidatorAddress: addr, ValidatorIndex: i, @@ -24,7 +21,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 } diff --git a/types/validator.go b/types/validator.go index 96ef24b65..1de326b00 100644 --- a/types/validator.go +++ b/types/validator.go @@ -101,7 +101,7 @@ func RandValidator(randPower bool, minPower int64) (*Validator, PrivValidator) { if randPower { votePower += int64(cmn.RandUint32()) } - pubKey, _ := privVal.GetPubKey() + pubKey := privVal.GetPubKey() val := NewValidator(pubKey, votePower) return val, privVal } diff --git a/types/vote_set_test.go b/types/vote_set_test.go index 08fd677f2..59205efc6 100644 --- a/types/vote_set_test.go +++ b/types/vote_set_test.go @@ -4,8 +4,6 @@ import ( "bytes" "testing" - "github.com/stretchr/testify/require" - "github.com/tendermint/tendermint/crypto" cmn "github.com/tendermint/tendermint/libs/common" tst "github.com/tendermint/tendermint/libs/test" @@ -68,8 +66,7 @@ func TestAddVote(t *testing.T) { // t.Logf(">> %v", voteSet) - val0Addr, err := val0.GetAddress() - require.NoError(t, err) + val0Addr := val0.GetPubKey().Address() if voteSet.GetByAddress(val0Addr) != nil { t.Errorf("Expected GetByAddress(val0.Address) to be nil") } @@ -90,7 +87,7 @@ 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) } @@ -122,10 +119,9 @@ func Test2_3Majority(t *testing.T) { } // 6 out of 10 voted for nil. for i := 0; i < 6; i++ { - addr, err := privValidators[i].GetAddress() - require.NoError(t, err) + addr := privValidators[i].GetPubKey().Address() vote := withValidator(voteProto, addr, i) - _, err = signAddVote(privValidators[i], vote, voteSet) + _, err := signAddVote(privValidators[i], vote, voteSet) if err != nil { t.Error(err) } @@ -137,10 +133,9 @@ func Test2_3Majority(t *testing.T) { // 7th validator voted for some blockhash { - addr, err := privValidators[6].GetAddress() - require.NoError(t, err) + addr := privValidators[6].GetPubKey().Address() vote := withValidator(voteProto, addr, 6) - _, err = signAddVote(privValidators[6], withBlockHash(vote, cmn.RandBytes(32)), voteSet) + _, err := signAddVote(privValidators[6], withBlockHash(vote, cmn.RandBytes(32)), voteSet) if err != nil { t.Error(err) } @@ -152,10 +147,9 @@ func Test2_3Majority(t *testing.T) { // 8th validator voted for nil. { - addr, err := privValidators[7].GetAddress() - require.NoError(t, err) + addr := privValidators[7].GetPubKey().Address() vote := withValidator(voteProto, addr, 7) - _, err = signAddVote(privValidators[7], vote, voteSet) + _, err := signAddVote(privValidators[7], vote, voteSet) if err != nil { t.Error(err) } @@ -186,10 +180,9 @@ func Test2_3MajorityRedux(t *testing.T) { // 66 out of 100 voted for nil. for i := 0; i < 66; i++ { - addr, err := privValidators[i].GetAddress() - require.NoError(t, err) + addr := privValidators[i].GetPubKey().Address() vote := withValidator(voteProto, addr, i) - _, err = signAddVote(privValidators[i], vote, voteSet) + _, err := signAddVote(privValidators[i], vote, voteSet) if err != nil { t.Error(err) } @@ -201,10 +194,9 @@ func Test2_3MajorityRedux(t *testing.T) { // 67th validator voted for nil { - adrr, err := privValidators[66].GetAddress() + adrr := privValidators[66].GetPubKey().Address() vote := withValidator(voteProto, adrr, 66) - require.NoError(t, err) - _, err = signAddVote(privValidators[66], withBlockHash(vote, nil), voteSet) + _, err := signAddVote(privValidators[66], withBlockHash(vote, nil), voteSet) if err != nil { t.Error(err) } @@ -216,11 +208,10 @@ func Test2_3MajorityRedux(t *testing.T) { // 68th validator voted for a different BlockParts PartSetHeader { - addr, err := privValidators[67].GetAddress() - require.NoError(t, err) + addr := privValidators[67].GetPubKey().Address() 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) } @@ -232,11 +223,10 @@ func Test2_3MajorityRedux(t *testing.T) { // 69th validator voted for different BlockParts Total { - addr, err := privValidators[68].GetAddress() - require.NoError(t, err) + addr := privValidators[68].GetPubKey().Address() 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) } @@ -248,10 +238,9 @@ func Test2_3MajorityRedux(t *testing.T) { // 70th validator voted for different BlockHash { - addr, err := privValidators[69].GetAddress() - require.NoError(t, err) + addr := privValidators[69].GetPubKey().Address() vote := withValidator(voteProto, addr, 69) - _, err = signAddVote(privValidators[69], withBlockHash(vote, cmn.RandBytes(32)), voteSet) + _, err := signAddVote(privValidators[69], withBlockHash(vote, cmn.RandBytes(32)), voteSet) if err != nil { t.Error(err) } @@ -263,10 +252,9 @@ func Test2_3MajorityRedux(t *testing.T) { // 71st validator voted for the right BlockHash & BlockPartsHeader { - addr, err := privValidators[70].GetAddress() - require.NoError(t, err) + addr := privValidators[70].GetPubKey().Address() vote := withValidator(voteProto, addr, 70) - _, err = signAddVote(privValidators[70], vote, voteSet) + _, err := signAddVote(privValidators[70], vote, voteSet) if err != nil { t.Error(err) } @@ -293,8 +281,7 @@ func TestBadVotes(t *testing.T) { // val0 votes for nil. { - addr, err := privValidators[0].GetAddress() - require.NoError(t, err) + addr := privValidators[0].GetPubKey().Address() vote := withValidator(voteProto, addr, 0) added, err := signAddVote(privValidators[0], vote, voteSet) if !added || err != nil { @@ -304,8 +291,7 @@ func TestBadVotes(t *testing.T) { // val0 votes again for some block. { - addr, err := privValidators[0].GetAddress() - require.NoError(t, err) + addr := privValidators[0].GetPubKey().Address() vote := withValidator(voteProto, addr, 0) added, err := signAddVote(privValidators[0], withBlockHash(vote, cmn.RandBytes(32)), voteSet) if added || err == nil { @@ -315,8 +301,7 @@ func TestBadVotes(t *testing.T) { // val1 votes on another height { - addr, err := privValidators[1].GetAddress() - require.NoError(t, err) + addr := privValidators[1].GetPubKey().Address() vote := withValidator(voteProto, addr, 1) added, err := signAddVote(privValidators[1], withHeight(vote, height+1), voteSet) if added || err == nil { @@ -326,8 +311,7 @@ func TestBadVotes(t *testing.T) { // val2 votes on another round { - addr, err := privValidators[2].GetAddress() - require.NoError(t, err) + addr := privValidators[2].GetPubKey().Address() vote := withValidator(voteProto, addr, 2) added, err := signAddVote(privValidators[2], withRound(vote, round+1), voteSet) if added || err == nil { @@ -337,8 +321,7 @@ func TestBadVotes(t *testing.T) { // val3 votes of another type. { - addr, err := privValidators[3].GetAddress() - require.NoError(t, err) + addr := privValidators[3].GetPubKey().Address() vote := withValidator(voteProto, addr, 3) added, err := signAddVote(privValidators[3], withType(vote, byte(PrecommitType)), voteSet) if added || err == nil { @@ -363,8 +346,7 @@ func TestConflicts(t *testing.T) { BlockID: BlockID{nil, PartSetHeader{}}, } - val0Addr, err := privValidators[0].GetAddress() - require.NoError(t, err) + val0Addr := privValidators[0].GetPubKey().Address() // val0 votes for nil. { vote := withValidator(voteProto, val0Addr, 0) @@ -418,8 +400,7 @@ func TestConflicts(t *testing.T) { // val1 votes for blockHash1. { - addr, err := privValidators[1].GetAddress() - require.NoError(t, err) + addr := privValidators[1].GetPubKey().Address() vote := withValidator(voteProto, addr, 1) added, err := signAddVote(privValidators[1], withBlockHash(vote, blockHash1), voteSet) if !added || err != nil { @@ -437,8 +418,7 @@ func TestConflicts(t *testing.T) { // val2 votes for blockHash2. { - addr, err := privValidators[2].GetAddress() - require.NoError(t, err) + addr := privValidators[2].GetPubKey().Address() vote := withValidator(voteProto, addr, 2) added, err := signAddVote(privValidators[2], withBlockHash(vote, blockHash2), voteSet) if !added || err != nil { @@ -459,8 +439,7 @@ func TestConflicts(t *testing.T) { // val2 votes for blockHash1. { - addr, err := privValidators[2].GetAddress() - require.NoError(t, err) + addr := privValidators[2].GetPubKey().Address() vote := withValidator(voteProto, addr, 2) added, err := signAddVote(privValidators[2], withBlockHash(vote, blockHash1), voteSet) if !added { @@ -502,10 +481,9 @@ func TestMakeCommit(t *testing.T) { // 6 out of 10 voted for some block. for i := 0; i < 6; i++ { - addr, err := privValidators[i].GetAddress() - require.NoError(t, err) + addr := privValidators[i].GetPubKey().Address() vote := withValidator(voteProto, addr, i) - _, err = signAddVote(privValidators[i], vote, voteSet) + _, err := signAddVote(privValidators[i], vote, voteSet) if err != nil { t.Error(err) } @@ -516,13 +494,12 @@ func TestMakeCommit(t *testing.T) { // 7th voted for some other block. { - addr, err := privValidators[6].GetAddress() - require.NoError(t, err) + addr := privValidators[6].GetPubKey().Address() 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) } @@ -530,10 +507,9 @@ func TestMakeCommit(t *testing.T) { // The 8th voted like everyone else. { - addr, err := privValidators[7].GetAddress() - require.NoError(t, err) + addr := privValidators[7].GetPubKey().Address() vote := withValidator(voteProto, addr, 7) - _, err = signAddVote(privValidators[7], vote, voteSet) + _, err := signAddVote(privValidators[7], vote, voteSet) if err != nil { t.Error(err) } diff --git a/types/vote_test.go b/types/vote_test.go index 70dc4aa6b..03110fbfa 100644 --- a/types/vote_test.go +++ b/types/vote_test.go @@ -140,15 +140,13 @@ func TestVoteProposalNotEq(t *testing.T) { func TestVoteVerifySignature(t *testing.T) { privVal := NewMockPV() - pubkey, err := privVal.GetPubKey() - require.NoError(t, err) + pubkey := privVal.GetPubKey() vote := examplePrecommit() signBytes := vote.SignBytes("test_chain_id") // sign it - err = privVal.SignVote("test_chain_id", vote) - require.NoError(t, err) + err := privVal.SignVote("test_chain_id", vote) // verify the same vote valid := pubkey.VerifyBytes(vote.SignBytes("test_chain_id"), vote.Signature) @@ -191,13 +189,12 @@ func TestIsVoteTypeValid(t *testing.T) { func TestVoteVerify(t *testing.T) { privVal := NewMockPV() - pubkey, err := privVal.GetPubKey() - require.NoError(t, err) + pubkey := privVal.GetPubKey() 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) }