diff --git a/CHANGELOG_PENDING.md b/CHANGELOG_PENDING.md index a773c2db9..fe9d4fdc1 100644 --- a/CHANGELOG_PENDING.md +++ b/CHANGELOG_PENDING.md @@ -76,6 +76,7 @@ Friendly reminder: We have a [bug bounty program](https://hackerone.com/tendermi - [types] \#6120 use batch verification for verifying commits signatures. - If the key type supports the batch verification API it will try to batch verify. If the verification fails we will single verify each signature. - [privval/file] \#6185 Return error on `LoadFilePV`, `LoadFilePVEmptyState`. Allows for better programmatic control of Tendermint. +- [privval] /#6240 Add `context.Context` to privval interface. ### BUG FIXES diff --git a/cmd/tendermint/commands/init.go b/cmd/tendermint/commands/init.go index 51578253b..efe0f0244 100644 --- a/cmd/tendermint/commands/init.go +++ b/cmd/tendermint/commands/init.go @@ -1,6 +1,7 @@ package commands import ( + "context" "fmt" "github.com/spf13/cobra" @@ -86,7 +87,11 @@ func initFilesWithConfig(config *cfg.Config) error { PubKeyTypes: []string{types.ABCIPubKeyTypeSecp256k1}, } } - pubKey, err := pv.GetPubKey() + + ctx, cancel := context.WithTimeout(context.TODO(), ctxTimeout) + defer cancel() + + pubKey, err := pv.GetPubKey(ctx) if err != nil { return fmt.Errorf("can't get pubkey: %w", err) } diff --git a/cmd/tendermint/commands/root.go b/cmd/tendermint/commands/root.go index 6dba1180d..7d25cdbff 100644 --- a/cmd/tendermint/commands/root.go +++ b/cmd/tendermint/commands/root.go @@ -4,6 +4,7 @@ import ( "fmt" "os" "strings" + "time" "github.com/spf13/cobra" "github.com/spf13/viper" @@ -15,8 +16,9 @@ import ( ) var ( - config = cfg.DefaultConfig() - logger = log.NewTMLogger(log.NewSyncWriter(os.Stdout)) + config = cfg.DefaultConfig() + logger = log.NewTMLogger(log.NewSyncWriter(os.Stdout)) + ctxTimeout = 4 * time.Second ) func init() { diff --git a/cmd/tendermint/commands/show_validator.go b/cmd/tendermint/commands/show_validator.go index 3c83a4120..59cc5bbfc 100644 --- a/cmd/tendermint/commands/show_validator.go +++ b/cmd/tendermint/commands/show_validator.go @@ -1,6 +1,7 @@ package commands import ( + "context" "fmt" "github.com/spf13/cobra" @@ -36,7 +37,11 @@ func showValidator(cmd *cobra.Command, args []string) error { if err != nil { return fmt.Errorf("can't connect to remote validator %w", err) } - pubKey, err = pvsc.GetPubKey() + + ctx, cancel := context.WithTimeout(context.TODO(), ctxTimeout) + defer cancel() + + pubKey, err = pvsc.GetPubKey(ctx) if err != nil { return fmt.Errorf("can't get pubkey: %w", err) } @@ -52,7 +57,10 @@ func showValidator(cmd *cobra.Command, args []string) error { return err } - pubKey, err = pv.GetPubKey() + ctx, cancel := context.WithTimeout(context.TODO(), ctxTimeout) + defer cancel() + + pubKey, err = pv.GetPubKey(ctx) if err != nil { return fmt.Errorf("can't get pubkey: %w", err) } diff --git a/cmd/tendermint/commands/testnet.go b/cmd/tendermint/commands/testnet.go index ba693f208..ff52536ce 100644 --- a/cmd/tendermint/commands/testnet.go +++ b/cmd/tendermint/commands/testnet.go @@ -1,6 +1,7 @@ package commands import ( + "context" "fmt" "net" "os" @@ -149,7 +150,10 @@ func testnetFiles(cmd *cobra.Command, args []string) error { return err } - pubKey, err := pv.GetPubKey() + ctx, cancel := context.WithTimeout(context.TODO(), ctxTimeout) + defer cancel() + + pubKey, err := pv.GetPubKey(ctx) if err != nil { return fmt.Errorf("can't get pubkey: %w", err) } diff --git a/config/config.go b/config/config.go index 7313f0184..2a56117e8 100644 --- a/config/config.go +++ b/config/config.go @@ -838,6 +838,7 @@ type ConsensusConfig struct { WalPath string `mapstructure:"wal-file"` walFile string // overrides WalPath if set + // TODO: remove timeout configs, these should be global not local // How long we wait for a proposal block before prevoting nil TimeoutPropose time.Duration `mapstructure:"timeout-propose"` // How much timeout-propose increases with each round diff --git a/consensus/byzantine_test.go b/consensus/byzantine_test.go index cd4780420..7fed81395 100644 --- a/consensus/byzantine_test.go +++ b/consensus/byzantine_test.go @@ -1,6 +1,7 @@ package consensus import ( + "context" "fmt" "os" "path" @@ -200,7 +201,7 @@ func TestByzantinePrevoteEquivocation(t *testing.T) { propBlockID := types.BlockID{Hash: block.Hash(), PartSetHeader: blockParts.Header()} proposal := types.NewProposal(height, round, lazyNodeState.ValidRound, propBlockID) p := proposal.ToProto() - if err := lazyNodeState.privValidator.SignProposal(lazyNodeState.state.ChainID, p); err == nil { + if err := lazyNodeState.privValidator.SignProposal(context.Background(), lazyNodeState.state.ChainID, p); err == nil { proposal.Signature = p.Signature // send proposal and block parts on internal msg queue @@ -247,7 +248,7 @@ func TestByzantinePrevoteEquivocation(t *testing.T) { wg.Wait() - pubkey, err := bzNodeState.privValidator.GetPubKey() + pubkey, err := bzNodeState.privValidator.GetPubKey(context.Background()) require.NoError(t, err) for idx, ev := range evidenceFromEachValidator { diff --git a/consensus/common_test.go b/consensus/common_test.go index 264a1c003..bd844982c 100644 --- a/consensus/common_test.go +++ b/consensus/common_test.go @@ -108,7 +108,7 @@ func (vs *validatorStub) signVote( hash []byte, header types.PartSetHeader) (*types.Vote, error) { - pubKey, err := vs.PrivValidator.GetPubKey() + pubKey, err := vs.PrivValidator.GetPubKey(context.Background()) if err != nil { return nil, fmt.Errorf("can't get pubkey: %w", err) } @@ -123,7 +123,7 @@ func (vs *validatorStub) signVote( BlockID: types.BlockID{Hash: hash, PartSetHeader: header}, } v := vote.ToProto() - err = vs.PrivValidator.SignVote(config.ChainID(), v) + err = vs.PrivValidator.SignVote(context.Background(), config.ChainID(), v) vote.Signature = v.Signature return vote, err @@ -169,11 +169,11 @@ func (vss ValidatorStubsByPower) Len() int { } func (vss ValidatorStubsByPower) Less(i, j int) bool { - vssi, err := vss[i].GetPubKey() + vssi, err := vss[i].GetPubKey(context.Background()) if err != nil { panic(err) } - vssj, err := vss[j].GetPubKey() + vssj, err := vss[j].GetPubKey(context.Background()) if err != nil { panic(err) } @@ -220,7 +220,7 @@ func decideProposal( polRound, propBlockID := validRound, types.BlockID{Hash: block.Hash(), PartSetHeader: blockParts.Header()} proposal = types.NewProposal(height, round, polRound, propBlockID) p := proposal.ToProto() - if err := vs.SignProposal(chainID, p); err != nil { + if err := vs.SignProposal(context.Background(), chainID, p); err != nil { panic(err) } @@ -248,7 +248,7 @@ func signAddVotes( func validatePrevote(t *testing.T, cs *State, round int32, privVal *validatorStub, blockHash []byte) { prevotes := cs.Votes.Prevotes(round) - pubKey, err := privVal.GetPubKey() + pubKey, err := privVal.GetPubKey(context.Background()) require.NoError(t, err) address := pubKey.Address() var vote *types.Vote @@ -268,7 +268,7 @@ func validatePrevote(t *testing.T, cs *State, round int32, privVal *validatorStu func validateLastPrecommit(t *testing.T, cs *State, privVal *validatorStub, blockHash []byte) { votes := cs.LastCommit - pv, err := privVal.GetPubKey() + pv, err := privVal.GetPubKey(context.Background()) require.NoError(t, err) address := pv.Address() var vote *types.Vote @@ -290,7 +290,7 @@ func validatePrecommit( lockedBlockHash []byte, ) { precommits := cs.Votes.Precommits(thisRound) - pv, err := privVal.GetPubKey() + pv, err := privVal.GetPubKey(context.Background()) require.NoError(t, err) address := pv.Address() var vote *types.Vote diff --git a/consensus/invalid_test.go b/consensus/invalid_test.go index bbf28faee..31a4db4f5 100644 --- a/consensus/invalid_test.go +++ b/consensus/invalid_test.go @@ -1,6 +1,7 @@ package consensus import ( + "context" "sync" "testing" @@ -75,7 +76,7 @@ func invalidDoPrevoteFunc(t *testing.T, height int64, round int32, cs *State, r cs.mtx.Lock() cs.privValidator = pv - pubKey, err := cs.privValidator.GetPubKey() + pubKey, err := cs.privValidator.GetPubKey(context.Background()) require.NoError(t, err) addr := pubKey.Address() @@ -96,7 +97,7 @@ func invalidDoPrevoteFunc(t *testing.T, height int64, round int32, cs *State, r } p := precommit.ToProto() - err = cs.privValidator.SignVote(cs.state.ChainID, p) + err = cs.privValidator.SignVote(context.Background(), cs.state.ChainID, p) require.NoError(t, err) precommit.Signature = p.Signature diff --git a/consensus/msgs_test.go b/consensus/msgs_test.go index 1ff071d69..24c56d4a7 100644 --- a/consensus/msgs_test.go +++ b/consensus/msgs_test.go @@ -1,6 +1,7 @@ package consensus import ( + "context" "encoding/hex" "fmt" "math" @@ -62,7 +63,7 @@ func TestMsgToProto(t *testing.T) { pbProposal := proposal.ToProto() pv := types.NewMockPV() - pk, err := pv.GetPubKey() + pk, err := pv.GetPubKey(context.Background()) require.NoError(t, err) val := types.NewValidator(pk, 100) diff --git a/consensus/reactor_test.go b/consensus/reactor_test.go index 21252f23c..c0346d903 100644 --- a/consensus/reactor_test.go +++ b/consensus/reactor_test.go @@ -488,7 +488,7 @@ func TestReactorVotingPowerChange(t *testing.T) { // map of active validators activeVals := make(map[string]struct{}) for i := 0; i < n; i++ { - pubKey, err := states[i].privValidator.GetPubKey() + pubKey, err := states[i].privValidator.GetPubKey(context.Background()) require.NoError(t, err) addr := pubKey.Address() @@ -513,7 +513,7 @@ func TestReactorVotingPowerChange(t *testing.T) { blocksSubs = append(blocksSubs, sub) } - val1PubKey, err := states[0].privValidator.GetPubKey() + val1PubKey, err := states[0].privValidator.GetPubKey(context.Background()) require.NoError(t, err) val1PubKeyABCI, err := cryptoenc.PubKeyToProto(val1PubKey) @@ -588,7 +588,7 @@ func TestReactorValidatorSetChanges(t *testing.T) { // map of active validators activeVals := make(map[string]struct{}) for i := 0; i < nVals; i++ { - pubKey, err := states[i].privValidator.GetPubKey() + pubKey, err := states[i].privValidator.GetPubKey(context.Background()) require.NoError(t, err) activeVals[string(pubKey.Address())] = struct{}{} @@ -607,7 +607,7 @@ func TestReactorValidatorSetChanges(t *testing.T) { wg.Wait() - newValidatorPubKey1, err := states[nVals].privValidator.GetPubKey() + newValidatorPubKey1, err := states[nVals].privValidator.GetPubKey(context.Background()) require.NoError(t, err) valPubKey1ABCI, err := cryptoenc.PubKeyToProto(newValidatorPubKey1) @@ -640,7 +640,7 @@ func TestReactorValidatorSetChanges(t *testing.T) { // it includes the commit for block 4, which should have the updated validator set waitForBlockWithUpdatedValsAndValidateIt(t, nPeers, activeVals, blocksSubs, states) - updateValidatorPubKey1, err := states[nVals].privValidator.GetPubKey() + updateValidatorPubKey1, err := states[nVals].privValidator.GetPubKey(context.Background()) require.NoError(t, err) updatePubKey1ABCI, err := cryptoenc.PubKeyToProto(updateValidatorPubKey1) @@ -660,7 +660,7 @@ func TestReactorValidatorSetChanges(t *testing.T) { previousTotalVotingPower, states[nVals].GetRoundState().LastValidators.TotalVotingPower(), ) - newValidatorPubKey2, err := states[nVals+1].privValidator.GetPubKey() + newValidatorPubKey2, err := states[nVals+1].privValidator.GetPubKey(context.Background()) require.NoError(t, err) newVal2ABCI, err := cryptoenc.PubKeyToProto(newValidatorPubKey2) @@ -668,7 +668,7 @@ func TestReactorValidatorSetChanges(t *testing.T) { newValidatorTx2 := kvstore.MakeValSetChangeTx(newVal2ABCI, testMinPower) - newValidatorPubKey3, err := states[nVals+2].privValidator.GetPubKey() + newValidatorPubKey3, err := states[nVals+2].privValidator.GetPubKey(context.Background()) require.NoError(t, err) newVal3ABCI, err := cryptoenc.PubKeyToProto(newValidatorPubKey3) diff --git a/consensus/replay_test.go b/consensus/replay_test.go index 7da881e8f..0a7430ab8 100644 --- a/consensus/replay_test.go +++ b/consensus/replay_test.go @@ -343,7 +343,7 @@ func TestSimulateValidatorsChange(t *testing.T) { // HEIGHT 2 height++ incrementHeight(vss...) - newValidatorPubKey1, err := css[nVals].privValidator.GetPubKey() + newValidatorPubKey1, err := css[nVals].privValidator.GetPubKey(context.Background()) require.NoError(t, err) valPubKey1ABCI, err := cryptoenc.PubKeyToProto(newValidatorPubKey1) require.NoError(t, err) @@ -356,7 +356,7 @@ func TestSimulateValidatorsChange(t *testing.T) { proposal := types.NewProposal(vss[1].Height, round, -1, blockID) p := proposal.ToProto() - if err := vss[1].SignProposal(config.ChainID(), p); err != nil { + if err := vss[1].SignProposal(context.Background(), config.ChainID(), p); err != nil { t.Fatal("failed to sign bad proposal", err) } proposal.Signature = p.Signature @@ -373,7 +373,7 @@ func TestSimulateValidatorsChange(t *testing.T) { // HEIGHT 3 height++ incrementHeight(vss...) - updateValidatorPubKey1, err := css[nVals].privValidator.GetPubKey() + updateValidatorPubKey1, err := css[nVals].privValidator.GetPubKey(context.Background()) require.NoError(t, err) updatePubKey1ABCI, err := cryptoenc.PubKeyToProto(updateValidatorPubKey1) require.NoError(t, err) @@ -386,7 +386,7 @@ func TestSimulateValidatorsChange(t *testing.T) { proposal = types.NewProposal(vss[2].Height, round, -1, blockID) p = proposal.ToProto() - if err := vss[2].SignProposal(config.ChainID(), p); err != nil { + if err := vss[2].SignProposal(context.Background(), config.ChainID(), p); err != nil { t.Fatal("failed to sign bad proposal", err) } proposal.Signature = p.Signature @@ -403,14 +403,14 @@ func TestSimulateValidatorsChange(t *testing.T) { // HEIGHT 4 height++ incrementHeight(vss...) - newValidatorPubKey2, err := css[nVals+1].privValidator.GetPubKey() + newValidatorPubKey2, err := css[nVals+1].privValidator.GetPubKey(context.Background()) require.NoError(t, err) newVal2ABCI, err := cryptoenc.PubKeyToProto(newValidatorPubKey2) require.NoError(t, err) newValidatorTx2 := kvstore.MakeValSetChangeTx(newVal2ABCI, testMinPower) err = assertMempool(css[0].txNotifier).CheckTx(newValidatorTx2, nil, mempl.TxInfo{}) assert.Nil(t, err) - newValidatorPubKey3, err := css[nVals+2].privValidator.GetPubKey() + newValidatorPubKey3, err := css[nVals+2].privValidator.GetPubKey(context.Background()) require.NoError(t, err) newVal3ABCI, err := cryptoenc.PubKeyToProto(newValidatorPubKey3) require.NoError(t, err) @@ -426,10 +426,10 @@ func TestSimulateValidatorsChange(t *testing.T) { valIndexFn := func(cssIdx int) int { for i, vs := range newVss { - vsPubKey, err := vs.GetPubKey() + vsPubKey, err := vs.GetPubKey(context.Background()) require.NoError(t, err) - cssPubKey, err := css[cssIdx].privValidator.GetPubKey() + cssPubKey, err := css[cssIdx].privValidator.GetPubKey(context.Background()) require.NoError(t, err) if vsPubKey.Equals(cssPubKey) { @@ -443,7 +443,7 @@ func TestSimulateValidatorsChange(t *testing.T) { proposal = types.NewProposal(vss[3].Height, round, -1, blockID) p = proposal.ToProto() - if err := vss[3].SignProposal(config.ChainID(), p); err != nil { + if err := vss[3].SignProposal(context.Background(), config.ChainID(), p); err != nil { t.Fatal("failed to sign bad proposal", err) } proposal.Signature = p.Signature @@ -502,7 +502,7 @@ func TestSimulateValidatorsChange(t *testing.T) { selfIndex = valIndexFn(0) proposal = types.NewProposal(vss[1].Height, round, -1, blockID) p = proposal.ToProto() - if err := vss[1].SignProposal(config.ChainID(), p); err != nil { + if err := vss[1].SignProposal(context.Background(), config.ChainID(), p); err != nil { t.Fatal("failed to sign bad proposal", err) } proposal.Signature = p.Signature @@ -687,7 +687,7 @@ func testHandshakeReplay(t *testing.T, config *cfg.Config, nBlocks int, mode uin }) chain, commits, err = makeBlockchainFromWAL(wal) require.NoError(t, err) - pubKey, err := privVal.GetPubKey() + pubKey, err := privVal.GetPubKey(context.Background()) require.NoError(t, err) stateDB, genesisState, store = stateAndStore(config, pubKey, kvstore.ProtocolVersion) @@ -888,7 +888,7 @@ func TestHandshakePanicsIfAppReturnsWrongAppHash(t *testing.T) { privVal, err := privval.LoadFilePV(config.PrivValidatorKeyFile(), config.PrivValidatorStateFile()) require.NoError(t, err) const appVersion = 0x0 - pubKey, err := privVal.GetPubKey() + pubKey, err := privVal.GetPubKey(context.Background()) require.NoError(t, err) stateDB, state, store := stateAndStore(config, pubKey, appVersion) stateStore := sm.NewStore(stateDB) @@ -1224,7 +1224,7 @@ func TestHandshakeUpdatesValidators(t *testing.T) { privVal, err := privval.LoadFilePV(config.PrivValidatorKeyFile(), config.PrivValidatorStateFile()) require.NoError(t, err) - pubKey, err := privVal.GetPubKey() + pubKey, err := privVal.GetPubKey(context.Background()) require.NoError(t, err) stateDB, state, store := stateAndStore(config, pubKey, 0x0) stateStore := sm.NewStore(stateDB) diff --git a/consensus/state.go b/consensus/state.go index f84252b92..780fc3af5 100644 --- a/consensus/state.go +++ b/consensus/state.go @@ -2,6 +2,7 @@ package consensus import ( "bytes" + "context" "errors" "fmt" "io/ioutil" @@ -1136,7 +1137,11 @@ func (cs *State) defaultDecideProposal(height int64, round int32) { propBlockID := types.BlockID{Hash: block.Hash(), PartSetHeader: blockParts.Header()} proposal := types.NewProposal(height, round, cs.ValidRound, propBlockID) p := proposal.ToProto() - if err := cs.privValidator.SignProposal(cs.state.ChainID, p); err == nil { + + // wait the max amount we would wait for a proposal + ctx, cancel := context.WithTimeout(context.TODO(), cs.config.TimeoutPropose) + defer cancel() + if err := cs.privValidator.SignProposal(ctx, cs.state.ChainID, p); err == nil { proposal.Signature = p.Signature // send proposal and block parts on internal msg queue @@ -2181,7 +2186,24 @@ func (cs *State) signVote( } v := vote.ToProto() - err := cs.privValidator.SignVote(cs.state.ChainID, v) + + // If the signedMessageType is for precommit, + // use our local precommit Timeout as the max wait time for getting a singed commit. The same goes for prevote. + var timeout time.Duration + + switch msgType { + case tmproto.PrecommitType: + timeout = cs.config.TimeoutPrecommit + case tmproto.PrevoteType: + timeout = cs.config.TimeoutPrevote + default: + timeout = time.Second + } + + ctx, cancel := context.WithTimeout(context.TODO(), timeout) + defer cancel() + + err := cs.privValidator.SignVote(ctx, cs.state.ChainID, v) vote.Signature = v.Signature return vote, err @@ -2248,7 +2270,17 @@ func (cs *State) updatePrivValidatorPubKey() error { return nil } - pubKey, err := cs.privValidator.GetPubKey() + var timeout time.Duration + if cs.config.TimeoutPrecommit > cs.config.TimeoutPrevote { + timeout = cs.config.TimeoutPrecommit + } else { + timeout = cs.config.TimeoutPrevote + } + + // set a hard timeout for 2 seconds. This helps in avoiding blocking of the remote signer connection + ctx, cancel := context.WithTimeout(context.TODO(), timeout) + defer cancel() + pubKey, err := cs.privValidator.GetPubKey(ctx) if err != nil { return err } diff --git a/consensus/state_test.go b/consensus/state_test.go index a6c013c4e..de0f6cc09 100644 --- a/consensus/state_test.go +++ b/consensus/state_test.go @@ -71,7 +71,7 @@ func TestStateProposerSelection0(t *testing.T) { // Commit a block and ensure proposer for the next height is correct. prop := cs1.GetRoundState().Validators.GetProposer() - pv, err := cs1.privValidator.GetPubKey() + pv, err := cs1.privValidator.GetPubKey(context.Background()) require.NoError(t, err) address := pv.Address() if !bytes.Equal(prop.Address, address) { @@ -88,7 +88,7 @@ func TestStateProposerSelection0(t *testing.T) { ensureNewRound(newRoundCh, height+1, 0) prop = cs1.GetRoundState().Validators.GetProposer() - pv1, err := vss[1].GetPubKey() + pv1, err := vss[1].GetPubKey(context.Background()) require.NoError(t, err) addr := pv1.Address() if !bytes.Equal(prop.Address, addr) { @@ -116,7 +116,7 @@ func TestStateProposerSelection2(t *testing.T) { // everyone just votes nil. we get a new proposer each round for i := int32(0); int(i) < len(vss); i++ { prop := cs1.GetRoundState().Validators.GetProposer() - pvk, err := vss[int(i+round)%len(vss)].GetPubKey() + pvk, err := vss[int(i+round)%len(vss)].GetPubKey(context.Background()) require.NoError(t, err) addr := pvk.Address() correctProposer := addr @@ -218,7 +218,7 @@ func TestStateBadProposal(t *testing.T) { blockID := types.BlockID{Hash: propBlock.Hash(), PartSetHeader: propBlockParts.Header()} proposal := types.NewProposal(vs2.Height, round, -1, blockID) p := proposal.ToProto() - if err := vs2.SignProposal(config.ChainID(), p); err != nil { + if err := vs2.SignProposal(context.Background(), config.ChainID(), p); err != nil { t.Fatal("failed to sign bad proposal", err) } @@ -274,7 +274,7 @@ func TestStateOversizedBlock(t *testing.T) { blockID := types.BlockID{Hash: propBlock.Hash(), PartSetHeader: propBlockParts.Header()} proposal := types.NewProposal(height, round, -1, blockID) p := proposal.ToProto() - if err := vs2.SignProposal(config.ChainID(), p); err != nil { + if err := vs2.SignProposal(context.Background(), config.ChainID(), p); err != nil { t.Fatal("failed to sign bad proposal", err) } proposal.Signature = p.Signature @@ -616,7 +616,7 @@ func TestStateLockPOLRelock(t *testing.T) { timeoutWaitCh := subscribe(cs1.eventBus, types.EventQueryTimeoutWait) proposalCh := subscribe(cs1.eventBus, types.EventQueryCompleteProposal) - pv1, err := cs1.privValidator.GetPubKey() + pv1, err := cs1.privValidator.GetPubKey(context.Background()) require.NoError(t, err) addr := pv1.Address() voteCh := subscribeToVoter(cs1, addr) @@ -717,7 +717,7 @@ func TestStateLockPOLUnlock(t *testing.T) { timeoutWaitCh := subscribe(cs1.eventBus, types.EventQueryTimeoutWait) newRoundCh := subscribe(cs1.eventBus, types.EventQueryNewRound) unlockCh := subscribe(cs1.eventBus, types.EventQueryUnlock) - pv1, err := cs1.privValidator.GetPubKey() + pv1, err := cs1.privValidator.GetPubKey(context.Background()) require.NoError(t, err) addr := pv1.Address() voteCh := subscribeToVoter(cs1, addr) @@ -809,7 +809,7 @@ func TestStateLockPOLUnlockOnUnknownBlock(t *testing.T) { timeoutWaitCh := subscribe(cs1.eventBus, types.EventQueryTimeoutWait) proposalCh := subscribe(cs1.eventBus, types.EventQueryCompleteProposal) - pv1, err := cs1.privValidator.GetPubKey() + pv1, err := cs1.privValidator.GetPubKey(context.Background()) require.NoError(t, err) addr := pv1.Address() voteCh := subscribeToVoter(cs1, addr) @@ -939,7 +939,7 @@ func TestStateLockPOLSafety1(t *testing.T) { timeoutProposeCh := subscribe(cs1.eventBus, types.EventQueryTimeoutPropose) timeoutWaitCh := subscribe(cs1.eventBus, types.EventQueryTimeoutWait) newRoundCh := subscribe(cs1.eventBus, types.EventQueryNewRound) - pv1, err := cs1.privValidator.GetPubKey() + pv1, err := cs1.privValidator.GetPubKey(context.Background()) require.NoError(t, err) addr := pv1.Address() voteCh := subscribeToVoter(cs1, addr) @@ -1060,7 +1060,7 @@ func TestStateLockPOLSafety2(t *testing.T) { timeoutWaitCh := subscribe(cs1.eventBus, types.EventQueryTimeoutWait) newRoundCh := subscribe(cs1.eventBus, types.EventQueryNewRound) unlockCh := subscribe(cs1.eventBus, types.EventQueryUnlock) - pv1, err := cs1.privValidator.GetPubKey() + pv1, err := cs1.privValidator.GetPubKey(context.Background()) require.NoError(t, err) addr := pv1.Address() voteCh := subscribeToVoter(cs1, addr) @@ -1115,7 +1115,7 @@ func TestStateLockPOLSafety2(t *testing.T) { // in round 2 we see the polkad block from round 0 newProp := types.NewProposal(height, round, 0, propBlockID0) p := newProp.ToProto() - if err := vs3.SignProposal(config.ChainID(), p); err != nil { + if err := vs3.SignProposal(context.Background(), config.ChainID(), p); err != nil { t.Fatal(err) } @@ -1160,7 +1160,7 @@ func TestProposeValidBlock(t *testing.T) { timeoutProposeCh := subscribe(cs1.eventBus, types.EventQueryTimeoutPropose) newRoundCh := subscribe(cs1.eventBus, types.EventQueryNewRound) unlockCh := subscribe(cs1.eventBus, types.EventQueryUnlock) - pv1, err := cs1.privValidator.GetPubKey() + pv1, err := cs1.privValidator.GetPubKey(context.Background()) require.NoError(t, err) addr := pv1.Address() voteCh := subscribeToVoter(cs1, addr) @@ -1251,7 +1251,7 @@ func TestSetValidBlockOnDelayedPrevote(t *testing.T) { timeoutWaitCh := subscribe(cs1.eventBus, types.EventQueryTimeoutWait) newRoundCh := subscribe(cs1.eventBus, types.EventQueryNewRound) validBlockCh := subscribe(cs1.eventBus, types.EventQueryValidBlock) - pv1, err := cs1.privValidator.GetPubKey() + pv1, err := cs1.privValidator.GetPubKey(context.Background()) require.NoError(t, err) addr := pv1.Address() voteCh := subscribeToVoter(cs1, addr) @@ -1315,7 +1315,7 @@ func TestSetValidBlockOnDelayedProposal(t *testing.T) { timeoutProposeCh := subscribe(cs1.eventBus, types.EventQueryTimeoutPropose) newRoundCh := subscribe(cs1.eventBus, types.EventQueryNewRound) validBlockCh := subscribe(cs1.eventBus, types.EventQueryValidBlock) - pv1, err := cs1.privValidator.GetPubKey() + pv1, err := cs1.privValidator.GetPubKey(context.Background()) require.NoError(t, err) addr := pv1.Address() voteCh := subscribeToVoter(cs1, addr) @@ -1392,7 +1392,7 @@ func TestWaitingTimeoutProposeOnNewRound(t *testing.T) { timeoutWaitCh := subscribe(cs1.eventBus, types.EventQueryTimeoutPropose) newRoundCh := subscribe(cs1.eventBus, types.EventQueryNewRound) - pv1, err := cs1.privValidator.GetPubKey() + pv1, err := cs1.privValidator.GetPubKey(context.Background()) require.NoError(t, err) addr := pv1.Address() voteCh := subscribeToVoter(cs1, addr) @@ -1430,7 +1430,7 @@ func TestRoundSkipOnNilPolkaFromHigherRound(t *testing.T) { timeoutWaitCh := subscribe(cs1.eventBus, types.EventQueryTimeoutWait) newRoundCh := subscribe(cs1.eventBus, types.EventQueryNewRound) - pv1, err := cs1.privValidator.GetPubKey() + pv1, err := cs1.privValidator.GetPubKey(context.Background()) require.NoError(t, err) addr := pv1.Address() voteCh := subscribeToVoter(cs1, addr) @@ -1468,7 +1468,7 @@ func TestWaitTimeoutProposeOnNilPolkaForTheCurrentRound(t *testing.T) { timeoutProposeCh := subscribe(cs1.eventBus, types.EventQueryTimeoutPropose) newRoundCh := subscribe(cs1.eventBus, types.EventQueryNewRound) - pv1, err := cs1.privValidator.GetPubKey() + pv1, err := cs1.privValidator.GetPubKey(context.Background()) require.NoError(t, err) addr := pv1.Address() voteCh := subscribeToVoter(cs1, addr) @@ -1595,7 +1595,7 @@ func TestStartNextHeightCorrectlyAfterTimeout(t *testing.T) { newRoundCh := subscribe(cs1.eventBus, types.EventQueryNewRound) newBlockHeader := subscribe(cs1.eventBus, types.EventQueryNewBlockHeader) - pv1, err := cs1.privValidator.GetPubKey() + pv1, err := cs1.privValidator.GetPubKey(context.Background()) require.NoError(t, err) addr := pv1.Address() voteCh := subscribeToVoter(cs1, addr) @@ -1657,7 +1657,7 @@ func TestResetTimeoutPrecommitUponNewHeight(t *testing.T) { newRoundCh := subscribe(cs1.eventBus, types.EventQueryNewRound) newBlockHeader := subscribe(cs1.eventBus, types.EventQueryNewBlockHeader) - pv1, err := cs1.privValidator.GetPubKey() + pv1, err := cs1.privValidator.GetPubKey(context.Background()) require.NoError(t, err) addr := pv1.Address() voteCh := subscribeToVoter(cs1, addr) @@ -1798,7 +1798,7 @@ func TestStateHalt1(t *testing.T) { timeoutWaitCh := subscribe(cs1.eventBus, types.EventQueryTimeoutWait) newRoundCh := subscribe(cs1.eventBus, types.EventQueryNewRound) newBlockCh := subscribe(cs1.eventBus, types.EventQueryNewBlock) - pv1, err := cs1.privValidator.GetPubKey() + pv1, err := cs1.privValidator.GetPubKey(context.Background()) require.NoError(t, err) addr := pv1.Address() voteCh := subscribeToVoter(cs1, addr) diff --git a/consensus/types/height_vote_set_test.go b/consensus/types/height_vote_set_test.go index 68c4d98c0..e611c3981 100644 --- a/consensus/types/height_vote_set_test.go +++ b/consensus/types/height_vote_set_test.go @@ -1,6 +1,7 @@ package types import ( + "context" "fmt" "os" "testing" @@ -57,7 +58,7 @@ func TestPeerCatchupRounds(t *testing.T) { func makeVoteHR(t *testing.T, height int64, valIndex, round int32, privVals []types.PrivValidator) *types.Vote { privVal := privVals[valIndex] - pubKey, err := privVal.GetPubKey() + pubKey, err := privVal.GetPubKey(context.Background()) if err != nil { panic(err) } @@ -76,7 +77,7 @@ func makeVoteHR(t *testing.T, height int64, valIndex, round int32, privVals []ty chainID := config.ChainID() v := vote.ToProto() - err = privVal.SignVote(chainID, v) + err = privVal.SignVote(context.Background(), chainID, v) if err != nil { panic(fmt.Sprintf("Error signing vote: %v", err)) } diff --git a/evidence/pool_test.go b/evidence/pool_test.go index 522419a08..866ebd018 100644 --- a/evidence/pool_test.go +++ b/evidence/pool_test.go @@ -1,6 +1,7 @@ package evidence_test import ( + "context" "testing" "time" @@ -438,7 +439,7 @@ func initializeStateFromValidatorSet(t *testing.T, valSet *types.ValidatorSet, h } func initializeValidatorState(t *testing.T, privVal types.PrivValidator, height int64) sm.Store { - pubKey, _ := privVal.GetPubKey() + pubKey, _ := privVal.GetPubKey(context.Background()) validator := &types.Validator{Address: pubKey.Address(), VotingPower: 10, PubKey: pubKey} // create validator set and state diff --git a/evidence/verify_test.go b/evidence/verify_test.go index e97dc402a..30e5e4e71 100644 --- a/evidence/verify_test.go +++ b/evidence/verify_test.go @@ -1,6 +1,7 @@ package evidence_test import ( + "context" "testing" "time" @@ -304,11 +305,11 @@ func TestVerifyDuplicateVoteEvidence(t *testing.T) { vote1 := makeVote(t, val, chainID, 0, 10, 2, 1, blockID, defaultEvidenceTime) v1 := vote1.ToProto() - err := val.SignVote(chainID, v1) + err := val.SignVote(context.Background(), chainID, v1) require.NoError(t, err) badVote := makeVote(t, val, chainID, 0, 10, 2, 1, blockID, defaultEvidenceTime) bv := badVote.ToProto() - err = val2.SignVote(chainID, bv) + err = val2.SignVote(context.Background(), chainID, bv) require.NoError(t, err) vote1.Signature = v1.Signature @@ -386,7 +387,7 @@ func TestVerifyDuplicateVoteEvidence(t *testing.T) { func makeVote( t *testing.T, val types.PrivValidator, chainID string, valIndex int32, height int64, round int32, step int, blockID types.BlockID, time time.Time) *types.Vote { - pubKey, err := val.GetPubKey() + pubKey, err := val.GetPubKey(context.Background()) require.NoError(t, err) v := &types.Vote{ ValidatorAddress: pubKey.Address(), @@ -399,7 +400,7 @@ func makeVote( } vpb := v.ToProto() - err = val.SignVote(chainID, vpb) + err = val.SignVote(context.Background(), chainID, vpb) if err != nil { panic(err) } diff --git a/node/node.go b/node/node.go index 69e92d4a4..752b036b2 100644 --- a/node/node.go +++ b/node/node.go @@ -876,7 +876,7 @@ func NewNode(config *cfg.Config, } } - pubKey, err := privValidator.GetPubKey() + pubKey, err := privValidator.GetPubKey(context.TODO()) if err != nil { return nil, fmt.Errorf("can't get pubkey: %w", err) } @@ -1312,7 +1312,7 @@ func (n *Node) OnStop() { // ConfigureRPC makes sure RPC has all the objects it needs to operate. func (n *Node) ConfigureRPC() error { - pubKey, err := n.privValidator.GetPubKey() + pubKey, err := n.privValidator.GetPubKey(context.TODO()) if err != nil { return fmt.Errorf("can't get pubkey: %w", err) } @@ -1716,7 +1716,7 @@ func createAndStartPrivValidatorSocketClient( } // try to get a pubkey from private validate first time - _, err = pvsc.GetPubKey() + _, err = pvsc.GetPubKey(context.TODO()) if err != nil { return nil, fmt.Errorf("can't get pubkey: %w", err) } @@ -1741,7 +1741,7 @@ func createAndStartPrivValidatorGRPCClient( } // try to get a pubkey from private validate first time - _, err = pvsc.GetPubKey() + _, err = pvsc.GetPubKey(context.TODO()) if err != nil { return nil, fmt.Errorf("can't get pubkey: %w", err) } diff --git a/privval/file.go b/privval/file.go index 6f6e7b9b3..d20e9b94f 100644 --- a/privval/file.go +++ b/privval/file.go @@ -2,6 +2,7 @@ package privval import ( "bytes" + "context" "errors" "fmt" "io/ioutil" @@ -153,6 +154,8 @@ type FilePV struct { LastSignState FilePVLastSignState } +var _ types.PrivValidator = (*FilePV)(nil) + // NewFilePV generates a new validator from the given key and paths. func NewFilePV(privKey crypto.PrivKey, keyFilePath, stateFilePath string) *FilePV { return &FilePV{ @@ -257,13 +260,13 @@ func (pv *FilePV) GetAddress() types.Address { // GetPubKey returns the public key of the validator. // Implements PrivValidator. -func (pv *FilePV) GetPubKey() (crypto.PubKey, error) { +func (pv *FilePV) GetPubKey(ctx context.Context) (crypto.PubKey, error) { return pv.Key.PubKey, nil } // SignVote signs a canonical representation of the vote, along with the // chainID. Implements PrivValidator. -func (pv *FilePV) SignVote(chainID string, vote *tmproto.Vote) error { +func (pv *FilePV) SignVote(ctx context.Context, chainID string, vote *tmproto.Vote) error { if err := pv.signVote(chainID, vote); err != nil { return fmt.Errorf("error signing vote: %v", err) } @@ -272,7 +275,7 @@ func (pv *FilePV) SignVote(chainID string, vote *tmproto.Vote) error { // SignProposal signs a canonical representation of the proposal, along with // the chainID. Implements PrivValidator. -func (pv *FilePV) SignProposal(chainID string, proposal *tmproto.Proposal) error { +func (pv *FilePV) SignProposal(ctx context.Context, chainID string, proposal *tmproto.Proposal) error { if err := pv.signProposal(chainID, proposal); err != nil { return fmt.Errorf("error signing proposal: %v", err) } diff --git a/privval/file_test.go b/privval/file_test.go index 965257daf..5d283031d 100644 --- a/privval/file_test.go +++ b/privval/file_test.go @@ -1,6 +1,7 @@ package privval import ( + "context" "encoding/base64" "fmt" "io/ioutil" @@ -61,7 +62,7 @@ func TestResetValidator(t *testing.T) { randBytes := tmrand.Bytes(tmhash.Size) blockID := types.BlockID{Hash: randBytes, PartSetHeader: types.PartSetHeader{}} vote := newVote(privVal.Key.Address, 0, height, round, voteType, blockID) - err = privVal.SignVote("mychainid", vote.ToProto()) + err = privVal.SignVote(context.Background(), "mychainid", vote.ToProto()) assert.NoError(t, err, "expected no error signing vote") // priv val after signing is not same as empty @@ -186,11 +187,11 @@ func TestSignVote(t *testing.T) { // sign a vote for first time vote := newVote(privVal.Key.Address, 0, height, round, voteType, block1) v := vote.ToProto() - err = privVal.SignVote("mychainid", v) + err = privVal.SignVote(context.Background(), "mychainid", v) assert.NoError(err, "expected no error signing vote") // try to sign the same vote again; should be fine - err = privVal.SignVote("mychainid", v) + err = privVal.SignVote(context.Background(), "mychainid", v) assert.NoError(err, "expected no error on signing same vote") // now try some bad votes @@ -203,14 +204,14 @@ func TestSignVote(t *testing.T) { for _, c := range cases { cpb := c.ToProto() - err = privVal.SignVote("mychainid", cpb) + err = privVal.SignVote(context.Background(), "mychainid", cpb) assert.Error(err, "expected error on signing conflicting vote") } // try signing a vote with a different time stamp sig := vote.Signature vote.Timestamp = vote.Timestamp.Add(time.Duration(1000)) - err = privVal.SignVote("mychainid", v) + err = privVal.SignVote(context.Background(), "mychainid", v) assert.NoError(err) assert.Equal(sig, vote.Signature) } @@ -238,11 +239,11 @@ func TestSignProposal(t *testing.T) { // sign a proposal for first time proposal := newProposal(height, round, block1) pbp := proposal.ToProto() - err = privVal.SignProposal("mychainid", pbp) + err = privVal.SignProposal(context.Background(), "mychainid", pbp) assert.NoError(err, "expected no error signing proposal") // try to sign the same proposal again; should be fine - err = privVal.SignProposal("mychainid", pbp) + err = privVal.SignProposal(context.Background(), "mychainid", pbp) assert.NoError(err, "expected no error on signing same proposal") // now try some bad Proposals @@ -254,14 +255,14 @@ func TestSignProposal(t *testing.T) { } for _, c := range cases { - err = privVal.SignProposal("mychainid", c.ToProto()) + err = privVal.SignProposal(context.Background(), "mychainid", c.ToProto()) assert.Error(err, "expected error on signing conflicting proposal") } // try signing a proposal with a different time stamp sig := proposal.Signature proposal.Timestamp = proposal.Timestamp.Add(time.Duration(1000)) - err = privVal.SignProposal("mychainid", pbp) + err = privVal.SignProposal(context.Background(), "mychainid", pbp) assert.NoError(err) assert.Equal(sig, proposal.Signature) } @@ -283,7 +284,7 @@ func TestDifferByTimestamp(t *testing.T) { { proposal := newProposal(height, round, block1) pb := proposal.ToProto() - err := privVal.SignProposal(chainID, pb) + err := privVal.SignProposal(context.Background(), chainID, pb) assert.NoError(t, err, "expected no error signing proposal") signBytes := types.ProposalSignBytes(chainID, pb) @@ -294,7 +295,7 @@ func TestDifferByTimestamp(t *testing.T) { pb.Timestamp = pb.Timestamp.Add(time.Millisecond) var emptySig []byte proposal.Signature = emptySig - err = privVal.SignProposal("mychainid", pb) + err = privVal.SignProposal(context.Background(), "mychainid", pb) assert.NoError(t, err, "expected no error on signing same proposal") assert.Equal(t, timeStamp, pb.Timestamp) @@ -308,7 +309,7 @@ func TestDifferByTimestamp(t *testing.T) { blockID := types.BlockID{Hash: randbytes, PartSetHeader: types.PartSetHeader{}} vote := newVote(privVal.Key.Address, 0, height, round, voteType, blockID) v := vote.ToProto() - err := privVal.SignVote("mychainid", v) + err := privVal.SignVote(context.Background(), "mychainid", v) assert.NoError(t, err, "expected no error signing vote") signBytes := types.VoteSignBytes(chainID, v) @@ -319,7 +320,7 @@ func TestDifferByTimestamp(t *testing.T) { v.Timestamp = v.Timestamp.Add(time.Millisecond) var emptySig []byte v.Signature = emptySig - err = privVal.SignVote("mychainid", v) + err = privVal.SignVote(context.Background(), "mychainid", v) assert.NoError(t, err, "expected no error on signing same vote") assert.Equal(t, timeStamp, v.Timestamp) diff --git a/privval/grpc/client.go b/privval/grpc/client.go index 5b44973e8..77f3930aa 100644 --- a/privval/grpc/client.go +++ b/privval/grpc/client.go @@ -2,7 +2,6 @@ package grpc import ( "context" - "time" grpc "google.golang.org/grpc" "google.golang.org/grpc/status" @@ -55,9 +54,7 @@ func (sc *SignerClient) Close() error { // GetPubKey retrieves a public key from a remote signer // returns an error if client is not able to provide the key -func (sc *SignerClient) GetPubKey() (crypto.PubKey, error) { - ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second) // Todo: should this be configurable? - defer cancel() +func (sc *SignerClient) GetPubKey(ctx context.Context) (crypto.PubKey, error) { resp, err := sc.client.GetPubKey(ctx, &privvalproto.PubKeyRequest{ChainId: sc.chainID}) if err != nil { errStatus, _ := status.FromError(err) @@ -74,9 +71,7 @@ func (sc *SignerClient) GetPubKey() (crypto.PubKey, error) { } // SignVote requests a remote signer to sign a vote -func (sc *SignerClient) SignVote(chainID string, vote *tmproto.Vote) error { - ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second) - defer cancel() +func (sc *SignerClient) SignVote(ctx context.Context, chainID string, vote *tmproto.Vote) error { resp, err := sc.client.SignVote(ctx, &privvalproto.SignVoteRequest{ChainId: sc.chainID, Vote: vote}) if err != nil { errStatus, _ := status.FromError(err) @@ -90,9 +85,7 @@ func (sc *SignerClient) SignVote(chainID string, vote *tmproto.Vote) error { } // SignProposal requests a remote signer to sign a proposal -func (sc *SignerClient) SignProposal(chainID string, proposal *tmproto.Proposal) error { - ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second) - defer cancel() +func (sc *SignerClient) SignProposal(ctx context.Context, chainID string, proposal *tmproto.Proposal) error { resp, err := sc.client.SignProposal( ctx, &privvalproto.SignProposalRequest{ChainId: chainID, Proposal: proposal}) diff --git a/privval/grpc/client_test.go b/privval/grpc/client_test.go index 09feea731..98730df19 100644 --- a/privval/grpc/client_test.go +++ b/privval/grpc/client_test.go @@ -60,7 +60,7 @@ func TestSignerClient_GetPubKey(t *testing.T) { client, err := tmgrpc.NewSignerClient(conn, chainID, logger) require.NoError(t, err) - pk, err := client.GetPubKey() + pk, err := client.GetPubKey(context.Background()) require.NoError(t, err) assert.Equal(t, mockPV.PrivKey.PubKey(), pk) } @@ -108,12 +108,12 @@ func TestSignerClient_SignVote(t *testing.T) { pbHave := have.ToProto() - err = client.SignVote(chainID, pbHave) + err = client.SignVote(context.Background(), chainID, pbHave) require.NoError(t, err) pbWant := want.ToProto() - require.NoError(t, mockPV.SignVote(chainID, pbWant)) + require.NoError(t, mockPV.SignVote(context.Background(), chainID, pbWant)) assert.Equal(t, pbWant.Signature, pbHave.Signature) } @@ -157,12 +157,12 @@ func TestSignerClient_SignProposal(t *testing.T) { pbHave := have.ToProto() - err = client.SignProposal(chainID, pbHave) + err = client.SignProposal(context.Background(), chainID, pbHave) require.NoError(t, err) pbWant := want.ToProto() - require.NoError(t, mockPV.SignProposal(chainID, pbWant)) + require.NoError(t, mockPV.SignProposal(context.Background(), chainID, pbWant)) assert.Equal(t, pbWant.Signature, pbHave.Signature) } diff --git a/privval/grpc/server.go b/privval/grpc/server.go index 763e50aaa..f5c434b1b 100644 --- a/privval/grpc/server.go +++ b/privval/grpc/server.go @@ -39,7 +39,7 @@ func (ss *SignerServer) GetPubKey(ctx context.Context, req *privvalproto.PubKeyR *privvalproto.PubKeyResponse, error) { var pubKey crypto.PubKey - pubKey, err := ss.privVal.GetPubKey() + pubKey, err := ss.privVal.GetPubKey(ctx) if err != nil { return nil, status.Errorf(codes.NotFound, "error getting pubkey: %v", err) } @@ -60,12 +60,12 @@ func (ss *SignerServer) SignVote(ctx context.Context, req *privvalproto.SignVote *privvalproto.SignedVoteResponse, error) { vote := req.Vote - err := ss.privVal.SignVote(req.ChainId, vote) + err := ss.privVal.SignVote(ctx, req.ChainId, vote) if err != nil { return nil, status.Errorf(codes.InvalidArgument, "error signing vote: %v", err) } - ss.logger.Info("SignerServer: SignVote Success") + ss.logger.Info("SignerServer: SignVote Success", "height", req.Vote.Height) return &privvalproto.SignedVoteResponse{Vote: *vote}, nil } @@ -76,12 +76,12 @@ func (ss *SignerServer) SignProposal(ctx context.Context, req *privvalproto.Sign *privvalproto.SignedProposalResponse, error) { proposal := req.Proposal - err := ss.privVal.SignProposal(req.ChainId, proposal) + err := ss.privVal.SignProposal(ctx, req.ChainId, proposal) if err != nil { return nil, status.Errorf(codes.InvalidArgument, "error signing proposal: %v", err) } - ss.logger.Info("SignerServer: SignProposal Success") + ss.logger.Info("SignerServer: SignProposal Success", "height", req.Proposal.Height) return &privvalproto.SignedProposalResponse{Proposal: *proposal}, nil } diff --git a/privval/grpc/server_test.go b/privval/grpc/server_test.go index ab728fdad..9fec9f2fd 100644 --- a/privval/grpc/server_test.go +++ b/privval/grpc/server_test.go @@ -41,7 +41,7 @@ func TestGetPubKey(t *testing.T) { if tc.err { require.Error(t, err) } else { - pk, err := tc.pv.GetPubKey() + pk, err := tc.pv.GetPubKey(context.Background()) require.NoError(t, err) assert.Equal(t, resp.PubKey.GetEd25519(), pk.Bytes()) } @@ -114,7 +114,8 @@ func TestSignVote(t *testing.T) { } else { pbVote := tc.want.ToProto() - require.NoError(t, tc.pv.SignVote(ChainID, pbVote)) + require.NoError(t, tc.pv.SignVote(context.Background(), ChainID, pbVote)) + assert.Equal(t, pbVote.Signature, resp.Vote.Signature) } }) @@ -179,7 +180,7 @@ func TestSignProposal(t *testing.T) { require.Error(t, err) } else { pbProposal := tc.want.ToProto() - require.NoError(t, tc.pv.SignProposal(ChainID, pbProposal)) + require.NoError(t, tc.pv.SignProposal(context.Background(), ChainID, pbProposal)) assert.Equal(t, pbProposal.Signature, resp.Proposal.Signature) } }) diff --git a/privval/retry_signer_client.go b/privval/retry_signer_client.go index 92a7d0655..9bd702196 100644 --- a/privval/retry_signer_client.go +++ b/privval/retry_signer_client.go @@ -1,6 +1,7 @@ package privval import ( + "context" "fmt" "time" @@ -44,13 +45,13 @@ func (sc *RetrySignerClient) Ping() error { return sc.next.Ping() } -func (sc *RetrySignerClient) GetPubKey() (crypto.PubKey, error) { +func (sc *RetrySignerClient) GetPubKey(ctx context.Context) (crypto.PubKey, error) { var ( pk crypto.PubKey err error ) for i := 0; i < sc.retries || sc.retries == 0; i++ { - pk, err = sc.next.GetPubKey() + pk, err = sc.next.GetPubKey(ctx) if err == nil { return pk, nil } @@ -63,10 +64,10 @@ func (sc *RetrySignerClient) GetPubKey() (crypto.PubKey, error) { return nil, fmt.Errorf("exhausted all attempts to get pubkey: %w", err) } -func (sc *RetrySignerClient) SignVote(chainID string, vote *tmproto.Vote) error { +func (sc *RetrySignerClient) SignVote(ctx context.Context, chainID string, vote *tmproto.Vote) error { var err error for i := 0; i < sc.retries || sc.retries == 0; i++ { - err = sc.next.SignVote(chainID, vote) + err = sc.next.SignVote(ctx, chainID, vote) if err == nil { return nil } @@ -79,10 +80,10 @@ func (sc *RetrySignerClient) SignVote(chainID string, vote *tmproto.Vote) error return fmt.Errorf("exhausted all attempts to sign vote: %w", err) } -func (sc *RetrySignerClient) SignProposal(chainID string, proposal *tmproto.Proposal) error { +func (sc *RetrySignerClient) SignProposal(ctx context.Context, chainID string, proposal *tmproto.Proposal) error { var err error for i := 0; i < sc.retries || sc.retries == 0; i++ { - err = sc.next.SignProposal(chainID, proposal) + err = sc.next.SignProposal(ctx, chainID, proposal) if err == nil { return nil } diff --git a/privval/signer_client.go b/privval/signer_client.go index aecb0381e..d25584c8f 100644 --- a/privval/signer_client.go +++ b/privval/signer_client.go @@ -1,6 +1,7 @@ package privval import ( + "context" "fmt" "time" @@ -68,7 +69,7 @@ func (sc *SignerClient) Ping() error { // GetPubKey retrieves a public key from a remote signer // returns an error if client is not able to provide the key -func (sc *SignerClient) GetPubKey() (crypto.PubKey, error) { +func (sc *SignerClient) GetPubKey(ctx context.Context) (crypto.PubKey, error) { response, err := sc.endpoint.SendRequest(mustWrapMsg(&privvalproto.PubKeyRequest{ChainId: sc.chainID})) if err != nil { return nil, fmt.Errorf("send: %w", err) @@ -91,7 +92,7 @@ func (sc *SignerClient) GetPubKey() (crypto.PubKey, error) { } // SignVote requests a remote signer to sign a vote -func (sc *SignerClient) SignVote(chainID string, vote *tmproto.Vote) error { +func (sc *SignerClient) SignVote(ctx context.Context, chainID string, vote *tmproto.Vote) error { response, err := sc.endpoint.SendRequest(mustWrapMsg(&privvalproto.SignVoteRequest{Vote: vote, ChainId: chainID})) if err != nil { return err @@ -111,7 +112,7 @@ func (sc *SignerClient) SignVote(chainID string, vote *tmproto.Vote) error { } // SignProposal requests a remote signer to sign a proposal -func (sc *SignerClient) SignProposal(chainID string, proposal *tmproto.Proposal) error { +func (sc *SignerClient) SignProposal(ctx context.Context, chainID string, proposal *tmproto.Proposal) error { response, err := sc.endpoint.SendRequest(mustWrapMsg( &privvalproto.SignProposalRequest{Proposal: proposal, ChainId: chainID}, )) diff --git a/privval/signer_client_test.go b/privval/signer_client_test.go index 019fd2c96..9aa49e709 100644 --- a/privval/signer_client_test.go +++ b/privval/signer_client_test.go @@ -1,6 +1,7 @@ package privval import ( + "context" "fmt" "testing" "time" @@ -97,16 +98,16 @@ func TestSignerGetPubKey(t *testing.T) { } }) - pubKey, err := tc.signerClient.GetPubKey() + pubKey, err := tc.signerClient.GetPubKey(context.Background()) require.NoError(t, err) - expectedPubKey, err := tc.mockPV.GetPubKey() + expectedPubKey, err := tc.mockPV.GetPubKey(context.Background()) require.NoError(t, err) assert.Equal(t, expectedPubKey, pubKey) - pubKey, err = tc.signerClient.GetPubKey() + pubKey, err = tc.signerClient.GetPubKey(context.Background()) require.NoError(t, err) - expectedpk, err := tc.mockPV.GetPubKey() + expectedpk, err := tc.mockPV.GetPubKey(context.Background()) require.NoError(t, err) expectedAddr := expectedpk.Address() @@ -147,8 +148,8 @@ func TestSignerProposal(t *testing.T) { } }) - require.NoError(t, tc.mockPV.SignProposal(tc.chainID, want.ToProto())) - require.NoError(t, tc.signerClient.SignProposal(tc.chainID, have.ToProto())) + require.NoError(t, tc.mockPV.SignProposal(context.Background(), tc.chainID, want.ToProto())) + require.NoError(t, tc.signerClient.SignProposal(context.Background(), tc.chainID, have.ToProto())) assert.Equal(t, want.Signature, have.Signature) } @@ -191,8 +192,8 @@ func TestSignerVote(t *testing.T) { } }) - require.NoError(t, tc.mockPV.SignVote(tc.chainID, want.ToProto())) - require.NoError(t, tc.signerClient.SignVote(tc.chainID, have.ToProto())) + require.NoError(t, tc.mockPV.SignVote(context.Background(), tc.chainID, want.ToProto())) + require.NoError(t, tc.signerClient.SignVote(context.Background(), tc.chainID, have.ToProto())) assert.Equal(t, want.Signature, have.Signature) } @@ -237,8 +238,8 @@ func TestSignerVoteResetDeadline(t *testing.T) { time.Sleep(testTimeoutReadWrite2o3) - require.NoError(t, tc.mockPV.SignVote(tc.chainID, want.ToProto())) - require.NoError(t, tc.signerClient.SignVote(tc.chainID, have.ToProto())) + require.NoError(t, tc.mockPV.SignVote(context.Background(), tc.chainID, want.ToProto())) + require.NoError(t, tc.signerClient.SignVote(context.Background(), tc.chainID, have.ToProto())) assert.Equal(t, want.Signature, have.Signature) // TODO(jleni): Clarify what is actually being tested @@ -246,8 +247,8 @@ func TestSignerVoteResetDeadline(t *testing.T) { // This would exceed the deadline if it was not extended by the previous message time.Sleep(testTimeoutReadWrite2o3) - require.NoError(t, tc.mockPV.SignVote(tc.chainID, want.ToProto())) - require.NoError(t, tc.signerClient.SignVote(tc.chainID, have.ToProto())) + require.NoError(t, tc.mockPV.SignVote(context.Background(), tc.chainID, want.ToProto())) + require.NoError(t, tc.signerClient.SignVote(context.Background(), tc.chainID, have.ToProto())) assert.Equal(t, want.Signature, have.Signature) } } @@ -298,8 +299,8 @@ func TestSignerVoteKeepAlive(t *testing.T) { time.Sleep(testTimeoutReadWrite * 3) tc.signerServer.Logger.Debug("TEST: Forced Wait DONE---------------------------------------------") - require.NoError(t, tc.mockPV.SignVote(tc.chainID, want.ToProto())) - require.NoError(t, tc.signerClient.SignVote(tc.chainID, have.ToProto())) + require.NoError(t, tc.mockPV.SignVote(context.Background(), tc.chainID, want.ToProto())) + require.NoError(t, tc.signerClient.SignVote(context.Background(), tc.chainID, have.ToProto())) assert.Equal(t, want.Signature, have.Signature) } @@ -335,13 +336,13 @@ func TestSignerSignProposalErrors(t *testing.T) { Signature: []byte("signature"), } - err := tc.signerClient.SignProposal(tc.chainID, proposal.ToProto()) + err := tc.signerClient.SignProposal(context.Background(), tc.chainID, proposal.ToProto()) require.Equal(t, err.(*RemoteSignerError).Description, types.ErroringMockPVErr.Error()) - err = tc.mockPV.SignProposal(tc.chainID, proposal.ToProto()) + err = tc.mockPV.SignProposal(context.Background(), tc.chainID, proposal.ToProto()) require.Error(t, err) - err = tc.signerClient.SignProposal(tc.chainID, proposal.ToProto()) + err = tc.signerClient.SignProposal(context.Background(), tc.chainID, proposal.ToProto()) require.Error(t, err) } } @@ -378,18 +379,18 @@ func TestSignerSignVoteErrors(t *testing.T) { } }) - err := tc.signerClient.SignVote(tc.chainID, vote.ToProto()) + err := tc.signerClient.SignVote(context.Background(), tc.chainID, vote.ToProto()) require.Equal(t, err.(*RemoteSignerError).Description, types.ErroringMockPVErr.Error()) - err = tc.mockPV.SignVote(tc.chainID, vote.ToProto()) + err = tc.mockPV.SignVote(context.Background(), tc.chainID, vote.ToProto()) require.Error(t, err) - err = tc.signerClient.SignVote(tc.chainID, vote.ToProto()) + err = tc.signerClient.SignVote(context.Background(), tc.chainID, vote.ToProto()) require.Error(t, err) } } -func brokenHandler(privVal types.PrivValidator, request privvalproto.Message, +func brokenHandler(ctx context.Context, privVal types.PrivValidator, request privvalproto.Message, chainID string) (privvalproto.Message, error) { var res privvalproto.Message var err error @@ -433,7 +434,7 @@ func TestSignerUnexpectedResponse(t *testing.T) { ts := time.Now() want := &types.Vote{Timestamp: ts, Type: tmproto.PrecommitType} - e := tc.signerClient.SignVote(tc.chainID, want.ToProto()) + e := tc.signerClient.SignVote(context.Background(), tc.chainID, want.ToProto()) assert.EqualError(t, e, "empty response") } } diff --git a/privval/signer_requestHandler.go b/privval/signer_requestHandler.go index 682863b19..18ad8a996 100644 --- a/privval/signer_requestHandler.go +++ b/privval/signer_requestHandler.go @@ -1,6 +1,7 @@ package privval import ( + "context" "fmt" "github.com/tendermint/tendermint/crypto" @@ -12,6 +13,7 @@ import ( ) func DefaultValidationRequestHandler( + ctx context.Context, privVal types.PrivValidator, req privvalproto.Message, chainID string, @@ -31,7 +33,7 @@ func DefaultValidationRequestHandler( } var pubKey crypto.PubKey - pubKey, err = privVal.GetPubKey() + pubKey, err = privVal.GetPubKey(ctx) if err != nil { return res, err } @@ -57,7 +59,7 @@ func DefaultValidationRequestHandler( vote := r.SignVoteRequest.Vote - err = privVal.SignVote(chainID, vote) + err = privVal.SignVote(ctx, chainID, vote) if err != nil { res = mustWrapMsg(&privvalproto.SignedVoteResponse{ Vote: tmproto.Vote{}, Error: &privvalproto.RemoteSignerError{Code: 0, Description: err.Error()}}) @@ -76,7 +78,7 @@ func DefaultValidationRequestHandler( proposal := r.SignProposalRequest.Proposal - err = privVal.SignProposal(chainID, proposal) + err = privVal.SignProposal(ctx, chainID, proposal) if err != nil { res = mustWrapMsg(&privvalproto.SignedProposalResponse{ Proposal: tmproto.Proposal{}, Error: &privvalproto.RemoteSignerError{Code: 0, Description: err.Error()}}) diff --git a/privval/signer_server.go b/privval/signer_server.go index c14524e36..f6e150ca0 100644 --- a/privval/signer_server.go +++ b/privval/signer_server.go @@ -1,6 +1,7 @@ package privval import ( + "context" "io" "github.com/tendermint/tendermint/libs/service" @@ -11,6 +12,7 @@ import ( // ValidationRequestHandlerFunc handles different remoteSigner requests type ValidationRequestHandlerFunc func( + ctx context.Context, privVal types.PrivValidator, requestMessage privvalproto.Message, chainID string) (privvalproto.Message, error) @@ -76,7 +78,7 @@ func (ss *SignerServer) servicePendingRequest() { // limit the scope of the lock ss.handlerMtx.Lock() defer ss.handlerMtx.Unlock() - res, err = ss.validationRequestHandler(ss.privVal, req, ss.chainID) + res, err = ss.validationRequestHandler(context.TODO(), ss.privVal, req, ss.chainID) // todo if err != nil { // only log the error; we'll reply with an error in res ss.Logger.Error("SignerServer: handleMessage", "err", err) diff --git a/state/state_test.go b/state/state_test.go index 34685d8d0..483392b94 100644 --- a/state/state_test.go +++ b/state/state_test.go @@ -2,6 +2,7 @@ package state_test import ( "bytes" + "context" "fmt" "math" "math/big" @@ -363,7 +364,7 @@ func TestProposerFrequency(t *testing.T) { votePower := int64(tmrand.Int()%maxPower) + 1 totalVotePower += votePower privVal := types.NewMockPV() - pubKey, err := privVal.GetPubKey() + pubKey, err := privVal.GetPubKey(context.Background()) require.NoError(t, err) val := types.NewValidator(pubKey, votePower) val.ProposerPriority = tmrand.Int64() diff --git a/state/validation_test.go b/state/validation_test.go index 0c9376581..9019b75e3 100644 --- a/state/validation_test.go +++ b/state/validation_test.go @@ -1,6 +1,7 @@ package state_test import ( + "context" "testing" "time" @@ -199,7 +200,7 @@ func TestValidateBlockCommit(t *testing.T) { ) require.NoError(t, err, "height %d", height) - bpvPubKey, err := badPrivVal.GetPubKey() + bpvPubKey, err := badPrivVal.GetPubKey(context.Background()) require.NoError(t, err) badVote := &types.Vote{ @@ -215,9 +216,9 @@ func TestValidateBlockCommit(t *testing.T) { g := goodVote.ToProto() b := badVote.ToProto() - err = badPrivVal.SignVote(chainID, g) + err = badPrivVal.SignVote(context.Background(), chainID, g) require.NoError(t, err, "height %d", height) - err = badPrivVal.SignVote(chainID, b) + err = badPrivVal.SignVote(context.Background(), chainID, b) require.NoError(t, err, "height %d", height) goodVote.Signature, badVote.Signature = g.Signature, b.Signature diff --git a/tools/tm-signer-harness/internal/test_harness.go b/tools/tm-signer-harness/internal/test_harness.go index b0940f3db..4d333949a 100644 --- a/tools/tm-signer-harness/internal/test_harness.go +++ b/tools/tm-signer-harness/internal/test_harness.go @@ -2,6 +2,7 @@ package internal import ( "bytes" + "context" "fmt" "net" "os" @@ -195,12 +196,12 @@ func (th *TestHarness) Run() { // local Tendermint version. func (th *TestHarness) TestPublicKey() error { th.logger.Info("TEST: Public key of remote signer") - fpvk, err := th.fpv.GetPubKey() + fpvk, err := th.fpv.GetPubKey(context.Background()) if err != nil { return err } th.logger.Info("Local", "pubKey", fpvk) - sck, err := th.signerClient.GetPubKey() + sck, err := th.signerClient.GetPubKey(context.Background()) if err != nil { return err } @@ -234,7 +235,7 @@ func (th *TestHarness) TestSignProposal() error { } p := prop.ToProto() propBytes := types.ProposalSignBytes(th.chainID, p) - if err := th.signerClient.SignProposal(th.chainID, p); err != nil { + if err := th.signerClient.SignProposal(context.Background(), th.chainID, p); err != nil { th.logger.Error("FAILED: Signing of proposal", "err", err) return newTestHarnessError(ErrTestSignProposalFailed, err, "") } @@ -245,7 +246,7 @@ func (th *TestHarness) TestSignProposal() error { th.logger.Error("FAILED: Signed proposal is invalid", "err", err) return newTestHarnessError(ErrTestSignProposalFailed, err, "") } - sck, err := th.signerClient.GetPubKey() + sck, err := th.signerClient.GetPubKey(context.Background()) if err != nil { return err } @@ -284,7 +285,7 @@ func (th *TestHarness) TestSignVote() error { v := vote.ToProto() voteBytes := types.VoteSignBytes(th.chainID, v) // sign the vote - if err := th.signerClient.SignVote(th.chainID, v); err != nil { + if err := th.signerClient.SignVote(context.Background(), th.chainID, v); err != nil { th.logger.Error("FAILED: Signing of vote", "err", err) return newTestHarnessError(ErrTestSignVoteFailed, err, fmt.Sprintf("voteType=%d", voteType)) } @@ -295,7 +296,7 @@ func (th *TestHarness) TestSignVote() error { th.logger.Error("FAILED: Signed vote is invalid", "err", err) return newTestHarnessError(ErrTestSignVoteFailed, err, fmt.Sprintf("voteType=%d", voteType)) } - sck, err := th.signerClient.GetPubKey() + sck, err := th.signerClient.GetPubKey(context.Background()) if err != nil { return err } diff --git a/types/block_test.go b/types/block_test.go index 8259dd2ef..ab546e53b 100644 --- a/types/block_test.go +++ b/types/block_test.go @@ -3,6 +3,7 @@ package types import ( // it is ok to use math/rand here: we do not need a cryptographically secure random // number generator here and we can run the tests a bit faster + "context" "crypto/rand" "encoding/hex" "math" @@ -570,7 +571,7 @@ func TestCommitToVoteSetWithVotesForNilBlock(t *testing.T) { vi := int32(0) for n := range tc.blockIDs { for i := 0; i < tc.numVotes[n]; i++ { - pubKey, err := vals[vi].GetPubKey() + pubKey, err := vals[vi].GetPubKey(context.Background()) require.NoError(t, err) vote := &Vote{ ValidatorAddress: pubKey.Address(), diff --git a/types/evidence.go b/types/evidence.go index 8007763b7..8e3c7decc 100644 --- a/types/evidence.go +++ b/types/evidence.go @@ -2,6 +2,7 @@ package types import ( "bytes" + "context" "encoding/binary" "errors" "fmt" @@ -547,15 +548,15 @@ func NewMockDuplicateVoteEvidence(height int64, time time.Time, chainID string) // assumes voting power to be 10 and validator to be the only one in the set func NewMockDuplicateVoteEvidenceWithValidator(height int64, time time.Time, pv PrivValidator, chainID string) *DuplicateVoteEvidence { - pubKey, _ := pv.GetPubKey() + pubKey, _ := pv.GetPubKey(context.Background()) val := NewValidator(pubKey, 10) voteA := makeMockVote(height, 0, 0, pubKey.Address(), randBlockID(), time) vA := voteA.ToProto() - _ = pv.SignVote(chainID, vA) + _ = pv.SignVote(context.Background(), chainID, vA) voteA.Signature = vA.Signature voteB := makeMockVote(height, 0, 0, pubKey.Address(), randBlockID(), time) vB := voteB.ToProto() - _ = pv.SignVote(chainID, vB) + _ = pv.SignVote(context.Background(), chainID, vB) voteB.Signature = vB.Signature return NewDuplicateVoteEvidence(voteA, voteB, time, NewValidatorSet([]*Validator{val})) } diff --git a/types/evidence_test.go b/types/evidence_test.go index 4c6626df2..f50ed0b18 100644 --- a/types/evidence_test.go +++ b/types/evidence_test.go @@ -1,6 +1,7 @@ package types import ( + "context" "math" "testing" "time" @@ -220,7 +221,7 @@ func TestMockEvidenceValidateBasic(t *testing.T) { func makeVote( t *testing.T, val PrivValidator, chainID string, valIndex int32, height int64, round int32, step int, blockID BlockID, time time.Time) *Vote { - pubKey, err := val.GetPubKey() + pubKey, err := val.GetPubKey(context.Background()) require.NoError(t, err) v := &Vote{ ValidatorAddress: pubKey.Address(), @@ -233,7 +234,7 @@ func makeVote( } vpb := v.ToProto() - err = val.SignVote(chainID, vpb) + err = val.SignVote(context.Background(), chainID, vpb) if err != nil { panic(err) } diff --git a/types/priv_validator.go b/types/priv_validator.go index 3ce02511a..f82da8991 100644 --- a/types/priv_validator.go +++ b/types/priv_validator.go @@ -2,6 +2,7 @@ package types import ( "bytes" + "context" "errors" "fmt" @@ -13,10 +14,10 @@ import ( // PrivValidator defines the functionality of a local Tendermint validator // that signs votes and proposals, and never double signs. type PrivValidator interface { - GetPubKey() (crypto.PubKey, error) + GetPubKey(context.Context) (crypto.PubKey, error) - SignVote(chainID string, vote *tmproto.Vote) error - SignProposal(chainID string, proposal *tmproto.Proposal) error + SignVote(ctx context.Context, chainID string, vote *tmproto.Vote) error + SignProposal(ctx context.Context, chainID string, proposal *tmproto.Proposal) error } type PrivValidatorsByAddress []PrivValidator @@ -26,11 +27,11 @@ func (pvs PrivValidatorsByAddress) Len() int { } func (pvs PrivValidatorsByAddress) Less(i, j int) bool { - pvi, err := pvs[i].GetPubKey() + pvi, err := pvs[i].GetPubKey(context.Background()) if err != nil { panic(err) } - pvj, err := pvs[j].GetPubKey() + pvj, err := pvs[j].GetPubKey(context.Background()) if err != nil { panic(err) } @@ -65,12 +66,12 @@ func NewMockPVWithParams(privKey crypto.PrivKey, breakProposalSigning, breakVote } // Implements PrivValidator. -func (pv MockPV) GetPubKey() (crypto.PubKey, error) { +func (pv MockPV) GetPubKey(ctx context.Context) (crypto.PubKey, error) { return pv.PrivKey.PubKey(), nil } // Implements PrivValidator. -func (pv MockPV) SignVote(chainID string, vote *tmproto.Vote) error { +func (pv MockPV) SignVote(ctx context.Context, chainID string, vote *tmproto.Vote) error { useChainID := chainID if pv.breakVoteSigning { useChainID = "incorrect-chain-id" @@ -86,7 +87,7 @@ func (pv MockPV) SignVote(chainID string, vote *tmproto.Vote) error { } // Implements PrivValidator. -func (pv MockPV) SignProposal(chainID string, proposal *tmproto.Proposal) error { +func (pv MockPV) SignProposal(ctx context.Context, chainID string, proposal *tmproto.Proposal) error { useChainID := chainID if pv.breakProposalSigning { useChainID = "incorrect-chain-id" @@ -102,7 +103,7 @@ func (pv MockPV) SignProposal(chainID string, proposal *tmproto.Proposal) error } func (pv MockPV) ExtractIntoValidator(votingPower int64) *Validator { - pubKey, _ := pv.GetPubKey() + pubKey, _ := pv.GetPubKey(context.Background()) return &Validator{ Address: pubKey.Address(), PubKey: pubKey, @@ -112,7 +113,7 @@ func (pv MockPV) ExtractIntoValidator(votingPower int64) *Validator { // String returns a string representation of the MockPV. func (pv MockPV) String() string { - mpv, _ := pv.GetPubKey() // mockPV will never return an error, ignored here + mpv, _ := pv.GetPubKey(context.Background()) // mockPV will never return an error, ignored here return fmt.Sprintf("MockPV{%v}", mpv.Address()) } @@ -129,17 +130,17 @@ type ErroringMockPV struct { var ErroringMockPVErr = errors.New("erroringMockPV always returns an error") // Implements PrivValidator. -func (pv *ErroringMockPV) GetPubKey() (crypto.PubKey, error) { +func (pv *ErroringMockPV) GetPubKey(ctx context.Context) (crypto.PubKey, error) { return nil, ErroringMockPVErr } // Implements PrivValidator. -func (pv *ErroringMockPV) SignVote(chainID string, vote *tmproto.Vote) error { +func (pv *ErroringMockPV) SignVote(ctx context.Context, chainID string, vote *tmproto.Vote) error { return ErroringMockPVErr } // Implements PrivValidator. -func (pv *ErroringMockPV) SignProposal(chainID string, proposal *tmproto.Proposal) error { +func (pv *ErroringMockPV) SignProposal(ctx context.Context, chainID string, proposal *tmproto.Proposal) error { return ErroringMockPVErr } diff --git a/types/proposal_test.go b/types/proposal_test.go index 71d4d62cc..360d949bb 100644 --- a/types/proposal_test.go +++ b/types/proposal_test.go @@ -1,6 +1,7 @@ package types import ( + "context" "math" "testing" "time" @@ -56,7 +57,7 @@ func TestProposalString(t *testing.T) { func TestProposalVerifySignature(t *testing.T) { privVal := NewMockPV() - pubKey, err := privVal.GetPubKey() + pubKey, err := privVal.GetPubKey(context.Background()) require.NoError(t, err) prop := NewProposal( @@ -66,7 +67,7 @@ func TestProposalVerifySignature(t *testing.T) { signBytes := ProposalSignBytes("test_chain_id", p) // sign it - err = privVal.SignProposal("test_chain_id", p) + err = privVal.SignProposal(context.Background(), "test_chain_id", p) require.NoError(t, err) prop.Signature = p.Signature @@ -103,7 +104,7 @@ func BenchmarkProposalWriteSignBytes(b *testing.B) { func BenchmarkProposalSign(b *testing.B) { privVal := NewMockPV() for i := 0; i < b.N; i++ { - err := privVal.SignProposal("test_chain_id", pbp) + err := privVal.SignProposal(context.Background(), "test_chain_id", pbp) if err != nil { b.Error(err) } @@ -112,9 +113,9 @@ func BenchmarkProposalSign(b *testing.B) { func BenchmarkProposalVerifySignature(b *testing.B) { privVal := NewMockPV() - err := privVal.SignProposal("test_chain_id", pbp) + err := privVal.SignProposal(context.Background(), "test_chain_id", pbp) require.NoError(b, err) - pubKey, err := privVal.GetPubKey() + pubKey, err := privVal.GetPubKey(context.Background()) require.NoError(b, err) for i := 0; i < b.N; i++ { @@ -154,7 +155,7 @@ func TestProposalValidateBasic(t *testing.T) { 4, 2, 2, blockID) p := prop.ToProto() - err := privVal.SignProposal("test_chain_id", p) + err := privVal.SignProposal(context.Background(), "test_chain_id", p) prop.Signature = p.Signature require.NoError(t, err) tc.malleateProposal(prop) diff --git a/types/test_util.go b/types/test_util.go index 367fd0631..018f24392 100644 --- a/types/test_util.go +++ b/types/test_util.go @@ -1,6 +1,7 @@ package types import ( + "context" "fmt" "time" @@ -12,7 +13,7 @@ func MakeCommit(blockID BlockID, height int64, round int32, // all sign for i := 0; i < len(validators); i++ { - pubKey, err := validators[i].GetPubKey() + pubKey, err := validators[i].GetPubKey(context.Background()) if err != nil { return nil, fmt.Errorf("can't get pubkey: %w", err) } @@ -37,7 +38,7 @@ func MakeCommit(blockID BlockID, height int64, round int32, func signAddVote(privVal PrivValidator, vote *Vote, voteSet *VoteSet) (signed bool, err error) { v := vote.ToProto() - err = privVal.SignVote(voteSet.ChainID(), v) + err = privVal.SignVote(context.Background(), voteSet.ChainID(), v) if err != nil { return false, err } @@ -53,7 +54,7 @@ func MakeVote( chainID string, now time.Time, ) (*Vote, error) { - pubKey, err := privVal.GetPubKey() + pubKey, err := privVal.GetPubKey(context.Background()) if err != nil { return nil, fmt.Errorf("can't get pubkey: %w", err) } @@ -70,7 +71,7 @@ func MakeVote( } v := vote.ToProto() - if err := privVal.SignVote(chainID, v); err != nil { + if err := privVal.SignVote(context.Background(), chainID, v); err != nil { return nil, err } diff --git a/types/validator.go b/types/validator.go index 961b833e4..0f4f58146 100644 --- a/types/validator.go +++ b/types/validator.go @@ -2,6 +2,7 @@ package types import ( "bytes" + "context" "errors" "fmt" "strings" @@ -184,7 +185,7 @@ func RandValidator(randPower bool, minPower int64) (*Validator, PrivValidator) { if randPower { votePower += int64(tmrand.Uint32()) } - pubKey, err := privVal.GetPubKey() + pubKey, err := privVal.GetPubKey(context.Background()) if err != nil { panic(fmt.Errorf("could not retrieve pubkey %w", err)) } diff --git a/types/validator_set_test.go b/types/validator_set_test.go index 546df11fa..a61c704e0 100644 --- a/types/validator_set_test.go +++ b/types/validator_set_test.go @@ -2,6 +2,7 @@ package types import ( "bytes" + "context" "fmt" "math" "sort" @@ -755,7 +756,7 @@ func TestValidatorSet_VerifyCommit_CheckAllSignatures(t *testing.T) { // malleate 4th signature vote := voteSet.GetByIndex(3) v := vote.ToProto() - err = vals[3].SignVote("CentaurusA", v) + err = vals[3].SignVote(context.Background(), "CentaurusA", v) require.NoError(t, err) vote.Signature = v.Signature commit.Signatures[3] = vote.CommitSig() @@ -780,7 +781,7 @@ func TestValidatorSet_VerifyCommitLight_ReturnsAsSoonAsMajorityOfVotingPowerSign // malleate 4th signature (3 signatures are enough for 2/3+) vote := voteSet.GetByIndex(3) v := vote.ToProto() - err = vals[3].SignVote("CentaurusA", v) + err = vals[3].SignVote(context.Background(), "CentaurusA", v) require.NoError(t, err) vote.Signature = v.Signature commit.Signatures[3] = vote.CommitSig() @@ -803,7 +804,7 @@ func TestValidatorSet_VerifyCommitLightTrusting_ReturnsAsSoonAsTrustLevelOfVotin // malleate 3rd signature (2 signatures are enough for 1/3+ trust level) vote := voteSet.GetByIndex(2) v := vote.ToProto() - err = vals[2].SignVote("CentaurusA", v) + err = vals[2].SignVote(context.Background(), "CentaurusA", v) require.NoError(t, err) vote.Signature = v.Signature commit.Signatures[2] = vote.CommitSig() diff --git a/types/validator_test.go b/types/validator_test.go index 5eb2ed7bf..8f9ff9e04 100644 --- a/types/validator_test.go +++ b/types/validator_test.go @@ -1,6 +1,7 @@ package types import ( + "context" "testing" "github.com/stretchr/testify/assert" @@ -40,7 +41,7 @@ func TestValidatorProtoBuf(t *testing.T) { func TestValidatorValidateBasic(t *testing.T) { priv := NewMockPV() - pubKey, _ := priv.GetPubKey() + pubKey, _ := priv.GetPubKey(context.Background()) testCases := []struct { val *Validator err bool diff --git a/types/vote_set_test.go b/types/vote_set_test.go index 4899b04b2..d2dc31f26 100644 --- a/types/vote_set_test.go +++ b/types/vote_set_test.go @@ -2,6 +2,7 @@ package types import ( "bytes" + "context" "testing" "github.com/stretchr/testify/assert" @@ -18,7 +19,7 @@ func TestVoteSet_AddVote_Good(t *testing.T) { voteSet, _, privValidators := randVoteSet(height, round, tmproto.PrevoteType, 10, 1) val0 := privValidators[0] - val0p, err := val0.GetPubKey() + val0p, err := val0.GetPubKey(context.Background()) require.NoError(t, err) val0Addr := val0p.Address() @@ -61,7 +62,7 @@ func TestVoteSet_AddVote_Bad(t *testing.T) { // val0 votes for nil. { - pubKey, err := privValidators[0].GetPubKey() + pubKey, err := privValidators[0].GetPubKey(context.Background()) require.NoError(t, err) addr := pubKey.Address() vote := withValidator(voteProto, addr, 0) @@ -73,7 +74,7 @@ func TestVoteSet_AddVote_Bad(t *testing.T) { // val0 votes again for some block. { - pubKey, err := privValidators[0].GetPubKey() + pubKey, err := privValidators[0].GetPubKey(context.Background()) require.NoError(t, err) addr := pubKey.Address() vote := withValidator(voteProto, addr, 0) @@ -85,7 +86,7 @@ func TestVoteSet_AddVote_Bad(t *testing.T) { // val1 votes on another height { - pubKey, err := privValidators[1].GetPubKey() + pubKey, err := privValidators[1].GetPubKey(context.Background()) require.NoError(t, err) addr := pubKey.Address() vote := withValidator(voteProto, addr, 1) @@ -97,7 +98,7 @@ func TestVoteSet_AddVote_Bad(t *testing.T) { // val2 votes on another round { - pubKey, err := privValidators[2].GetPubKey() + pubKey, err := privValidators[2].GetPubKey(context.Background()) require.NoError(t, err) addr := pubKey.Address() vote := withValidator(voteProto, addr, 2) @@ -109,7 +110,7 @@ func TestVoteSet_AddVote_Bad(t *testing.T) { // val3 votes of another type. { - pubKey, err := privValidators[3].GetPubKey() + pubKey, err := privValidators[3].GetPubKey(context.Background()) require.NoError(t, err) addr := pubKey.Address() vote := withValidator(voteProto, addr, 3) @@ -135,7 +136,7 @@ func TestVoteSet_2_3Majority(t *testing.T) { } // 6 out of 10 voted for nil. for i := int32(0); i < 6; i++ { - pubKey, err := privValidators[i].GetPubKey() + pubKey, err := privValidators[i].GetPubKey(context.Background()) require.NoError(t, err) addr := pubKey.Address() vote := withValidator(voteProto, addr, i) @@ -147,7 +148,7 @@ func TestVoteSet_2_3Majority(t *testing.T) { // 7th validator voted for some blockhash { - pubKey, err := privValidators[6].GetPubKey() + pubKey, err := privValidators[6].GetPubKey(context.Background()) require.NoError(t, err) addr := pubKey.Address() vote := withValidator(voteProto, addr, 6) @@ -159,7 +160,7 @@ func TestVoteSet_2_3Majority(t *testing.T) { // 8th validator voted for nil. { - pubKey, err := privValidators[7].GetPubKey() + pubKey, err := privValidators[7].GetPubKey(context.Background()) require.NoError(t, err) addr := pubKey.Address() vote := withValidator(voteProto, addr, 7) @@ -190,7 +191,7 @@ func TestVoteSet_2_3MajorityRedux(t *testing.T) { // 66 out of 100 voted for nil. for i := int32(0); i < 66; i++ { - pubKey, err := privValidators[i].GetPubKey() + pubKey, err := privValidators[i].GetPubKey(context.Background()) require.NoError(t, err) addr := pubKey.Address() vote := withValidator(voteProto, addr, i) @@ -203,7 +204,7 @@ func TestVoteSet_2_3MajorityRedux(t *testing.T) { // 67th validator voted for nil { - pubKey, err := privValidators[66].GetPubKey() + pubKey, err := privValidators[66].GetPubKey(context.Background()) require.NoError(t, err) adrr := pubKey.Address() vote := withValidator(voteProto, adrr, 66) @@ -216,7 +217,7 @@ func TestVoteSet_2_3MajorityRedux(t *testing.T) { // 68th validator voted for a different BlockParts PartSetHeader { - pubKey, err := privValidators[67].GetPubKey() + pubKey, err := privValidators[67].GetPubKey(context.Background()) require.NoError(t, err) addr := pubKey.Address() vote := withValidator(voteProto, addr, 67) @@ -230,7 +231,7 @@ func TestVoteSet_2_3MajorityRedux(t *testing.T) { // 69th validator voted for different BlockParts Total { - pubKey, err := privValidators[68].GetPubKey() + pubKey, err := privValidators[68].GetPubKey(context.Background()) require.NoError(t, err) addr := pubKey.Address() vote := withValidator(voteProto, addr, 68) @@ -244,7 +245,7 @@ func TestVoteSet_2_3MajorityRedux(t *testing.T) { // 70th validator voted for different BlockHash { - pubKey, err := privValidators[69].GetPubKey() + pubKey, err := privValidators[69].GetPubKey(context.Background()) require.NoError(t, err) addr := pubKey.Address() vote := withValidator(voteProto, addr, 69) @@ -257,7 +258,7 @@ func TestVoteSet_2_3MajorityRedux(t *testing.T) { // 71st validator voted for the right BlockHash & BlockPartSetHeader { - pubKey, err := privValidators[70].GetPubKey() + pubKey, err := privValidators[70].GetPubKey(context.Background()) require.NoError(t, err) addr := pubKey.Address() vote := withValidator(voteProto, addr, 70) @@ -285,7 +286,7 @@ func TestVoteSet_Conflicts(t *testing.T) { BlockID: BlockID{nil, PartSetHeader{}}, } - val0, err := privValidators[0].GetPubKey() + val0, err := privValidators[0].GetPubKey(context.Background()) require.NoError(t, err) val0Addr := val0.Address() @@ -332,7 +333,7 @@ func TestVoteSet_Conflicts(t *testing.T) { // val1 votes for blockHash1. { - pv, err := privValidators[1].GetPubKey() + pv, err := privValidators[1].GetPubKey(context.Background()) assert.NoError(t, err) addr := pv.Address() vote := withValidator(voteProto, addr, 1) @@ -352,7 +353,7 @@ func TestVoteSet_Conflicts(t *testing.T) { // val2 votes for blockHash2. { - pv, err := privValidators[2].GetPubKey() + pv, err := privValidators[2].GetPubKey(context.Background()) assert.NoError(t, err) addr := pv.Address() vote := withValidator(voteProto, addr, 2) @@ -376,7 +377,7 @@ func TestVoteSet_Conflicts(t *testing.T) { // val2 votes for blockHash1. { - pv, err := privValidators[2].GetPubKey() + pv, err := privValidators[2].GetPubKey(context.Background()) assert.NoError(t, err) addr := pv.Address() vote := withValidator(voteProto, addr, 2) @@ -415,7 +416,7 @@ func TestVoteSet_MakeCommit(t *testing.T) { // 6 out of 10 voted for some block. for i := int32(0); i < 6; i++ { - pv, err := privValidators[i].GetPubKey() + pv, err := privValidators[i].GetPubKey(context.Background()) assert.NoError(t, err) addr := pv.Address() vote := withValidator(voteProto, addr, i) @@ -430,7 +431,7 @@ func TestVoteSet_MakeCommit(t *testing.T) { // 7th voted for some other block. { - pv, err := privValidators[6].GetPubKey() + pv, err := privValidators[6].GetPubKey(context.Background()) assert.NoError(t, err) addr := pv.Address() vote := withValidator(voteProto, addr, 6) @@ -443,7 +444,7 @@ func TestVoteSet_MakeCommit(t *testing.T) { // The 8th voted like everyone else. { - pv, err := privValidators[7].GetPubKey() + pv, err := privValidators[7].GetPubKey(context.Background()) assert.NoError(t, err) addr := pv.Address() vote := withValidator(voteProto, addr, 7) @@ -453,7 +454,7 @@ func TestVoteSet_MakeCommit(t *testing.T) { // The 9th voted for nil. { - pv, err := privValidators[8].GetPubKey() + pv, err := privValidators[8].GetPubKey(context.Background()) assert.NoError(t, err) addr := pv.Address() vote := withValidator(voteProto, addr, 8) diff --git a/types/vote_test.go b/types/vote_test.go index 51e20cd7a..28fc2dd42 100644 --- a/types/vote_test.go +++ b/types/vote_test.go @@ -1,6 +1,7 @@ package types import ( + "context" "testing" "time" @@ -148,7 +149,7 @@ func TestVoteProposalNotEq(t *testing.T) { func TestVoteVerifySignature(t *testing.T) { privVal := NewMockPV() - pubkey, err := privVal.GetPubKey() + pubkey, err := privVal.GetPubKey(context.Background()) require.NoError(t, err) vote := examplePrecommit() @@ -156,7 +157,7 @@ func TestVoteVerifySignature(t *testing.T) { signBytes := VoteSignBytes("test_chain_id", v) // sign it - err = privVal.SignVote("test_chain_id", v) + err = privVal.SignVote(context.Background(), "test_chain_id", v) require.NoError(t, err) // verify the same vote @@ -200,7 +201,7 @@ func TestIsVoteTypeValid(t *testing.T) { func TestVoteVerify(t *testing.T) { privVal := NewMockPV() - pubkey, err := privVal.GetPubKey() + pubkey, err := privVal.GetPubKey(context.Background()) require.NoError(t, err) vote := examplePrevote() @@ -255,7 +256,7 @@ func TestVoteValidateBasic(t *testing.T) { t.Run(tc.testName, func(t *testing.T) { vote := examplePrecommit() v := vote.ToProto() - err := privVal.SignVote("test_chain_id", v) + err := privVal.SignVote(context.Background(), "test_chain_id", v) vote.Signature = v.Signature require.NoError(t, err) tc.malleateVote(vote) @@ -268,7 +269,7 @@ func TestVoteProtobuf(t *testing.T) { privVal := NewMockPV() vote := examplePrecommit() v := vote.ToProto() - err := privVal.SignVote("test_chain_id", v) + err := privVal.SignVote(context.Background(), "test_chain_id", v) vote.Signature = v.Signature require.NoError(t, err)