privval: add ctx to privval interface (#6240)

## Description

- Add `context.Context` to Privval interface

This pr does not introduce context into our custom privval connection protocol because this will be removed in the next release. When this pr is released.
This commit is contained in:
Marko
2021-03-16 14:41:03 +00:00
committed by GitHub
parent fa781e6bb7
commit efd2fde474
43 changed files with 297 additions and 219 deletions
+2 -1
View File
@@ -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(),
+4 -3
View File
@@ -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}))
}
+3 -2
View File
@@ -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)
}
+14 -13
View File
@@ -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
}
+7 -6
View File
@@ -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)
+5 -4
View File
@@ -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
}
+2 -1
View File
@@ -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))
}
+4 -3
View File
@@ -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()
+2 -1
View File
@@ -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
+24 -23
View File
@@ -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)
+6 -5
View File
@@ -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)