From 2b41970966beab6577f10919051327cae4e89d17 Mon Sep 17 00:00:00 2001 From: Callum Waters Date: Wed, 26 Oct 2022 14:39:16 +0200 Subject: [PATCH] update tests --- abci/types/types.pb.go | 12 +++--- blocksync/reactor_test.go | 59 +++++++++----------------- evidence/verify_test.go | 8 ---- internal/test/commit.go | 4 +- internal/test/factory_test.go | 9 ++++ internal/test/vote.go | 14 ++++++ proto/tendermint/crypto/keys.pb.go | 1 + proto/tendermint/mempool/types.pb.go | 1 + proto/tendermint/p2p/conn.pb.go | 1 + proto/tendermint/rpc/grpc/types.pb.go | 7 +-- proto/tendermint/state/types.pb.go | 10 ++--- proto/tendermint/statesync/types.pb.go | 1 + types/block_test.go | 1 + types/test_util.go | 3 ++ types/vote.go | 4 ++ types/vote_set.go | 6 ++- 16 files changed, 80 insertions(+), 61 deletions(-) diff --git a/abci/types/types.pb.go b/abci/types/types.pb.go index 08085dc54..b727f1791 100644 --- a/abci/types/types.pb.go +++ b/abci/types/types.pb.go @@ -7,9 +7,10 @@ import ( context "context" fmt "fmt" _ "github.com/cosmos/gogoproto/gogoproto" + grpc1 "github.com/cosmos/gogoproto/grpc" + proto "github.com/cosmos/gogoproto/proto" _ "github.com/cosmos/gogoproto/types" - proto "github.com/gogo/protobuf/proto" - github_com_cosmos_gogoproto_types "github.com/gogo/protobuf/types" + github_com_cosmos_gogoproto_types "github.com/cosmos/gogoproto/types" crypto "github.com/tendermint/tendermint/proto/tendermint/crypto" types1 "github.com/tendermint/tendermint/proto/tendermint/types" grpc "google.golang.org/grpc" @@ -1523,6 +1524,7 @@ func (m *RequestFinalizeBlock) GetProposerAddress() []byte { type Response struct { // Types that are valid to be assigned to Value: + // // *Response_Exception // *Response_Echo // *Response_Flush @@ -3863,10 +3865,10 @@ type ABCIClient interface { } type aBCIClient struct { - cc *grpc.ClientConn + cc grpc1.ClientConn } -func NewABCIClient(cc *grpc.ClientConn) ABCIClient { +func NewABCIClient(cc grpc1.ClientConn) ABCIClient { return &aBCIClient{cc} } @@ -4087,7 +4089,7 @@ func (*UnimplementedABCIServer) FinalizeBlock(ctx context.Context, req *RequestF return nil, status.Errorf(codes.Unimplemented, "method FinalizeBlock not implemented") } -func RegisterABCIServer(s *grpc.Server, srv ABCIServer) { +func RegisterABCIServer(s grpc1.Server, srv ABCIServer) { s.RegisterService(&_ABCI_serviceDesc, srv) } diff --git a/blocksync/reactor_test.go b/blocksync/reactor_test.go index 1056f7305..1e053564a 100644 --- a/blocksync/reactor_test.go +++ b/blocksync/reactor_test.go @@ -3,7 +3,6 @@ package blocksync import ( "fmt" "os" - "sort" "testing" "time" @@ -15,6 +14,7 @@ import ( abci "github.com/tendermint/tendermint/abci/types" cfg "github.com/tendermint/tendermint/config" + "github.com/tendermint/tendermint/internal/test" "github.com/tendermint/tendermint/libs/log" mpmocks "github.com/tendermint/tendermint/mempool/mocks" "github.com/tendermint/tendermint/p2p" @@ -27,24 +27,10 @@ import ( var config *cfg.Config -func randGenesisDoc(numValidators int, randPower bool, minPower int64) (*types.GenesisDoc, []types.PrivValidator) { - validators := make([]types.GenesisValidator, numValidators) - privValidators := make([]types.PrivValidator, numValidators) - for i := 0; i < numValidators; i++ { - val, privVal := types.RandValidator(randPower, minPower) - validators[i] = types.GenesisValidator{ - PubKey: val.PubKey, - Power: val.VotingPower, - } - privValidators[i] = privVal - } - sort.Sort(types.PrivValidatorsByAddress(privValidators)) - - return &types.GenesisDoc{ - GenesisTime: tmtime.Now(), - ChainID: config.ChainID(), - Validators: validators, - }, privValidators +func randGenesisDoc(t *testing.T) (*types.GenesisDoc, []types.PrivValidator) { + vals, privVals := test.ValidatorSet(t, 1, 30) + genDoc := test.GenesisDoc("", tmtime.Now(), vals.Validators, test.ConsensusParams()) + return genDoc, privVals } type ReactorPair struct { @@ -56,11 +42,8 @@ func newReactor( t *testing.T, logger log.Logger, genDoc *types.GenesisDoc, - privVals []types.PrivValidator, + privVal types.PrivValidator, maxBlockHeight int64) ReactorPair { - if len(privVals) != 1 { - panic("only support one validator") - } app := abci.NewBaseApplication() cc := proxy.NewLocalClientCreator(app) @@ -115,12 +98,12 @@ func newReactor( require.NoError(t, err) blockID := types.BlockID{Hash: thisBlock.Hash(), PartSetHeader: thisParts.Header()} - vote, err := types.MakeVote( + vote, err := test.MakePrecommit( + privVal, + 0, thisBlock.Header.Height, + 0, blockID, - state.Validators, - privVals[0], - thisBlock.Header.ChainID, time.Now(), ) if err != nil { @@ -153,14 +136,14 @@ func newReactor( func TestNoBlockResponse(t *testing.T) { config = cfg.ResetTestRoot("blockchain_reactor_test") defer os.RemoveAll(config.RootDir) - genDoc, privVals := randGenesisDoc(1, false, 30) + genDoc, privVals := randGenesisDoc(t) maxBlockHeight := int64(65) reactorPairs := make([]ReactorPair, 2) - reactorPairs[0] = newReactor(t, log.TestingLogger(), genDoc, privVals, maxBlockHeight) - reactorPairs[1] = newReactor(t, log.TestingLogger(), genDoc, privVals, 0) + reactorPairs[0] = newReactor(t, log.TestingLogger(), genDoc, privVals[0], maxBlockHeight) + reactorPairs[1] = newReactor(t, log.TestingLogger(), genDoc, privVals[0], 0) p2p.MakeConnectedSwitches(config.P2P, 2, func(i int, s *p2p.Switch) *p2p.Switch { s.AddReactor("BLOCKCHAIN", reactorPairs[i].reactor) @@ -215,13 +198,13 @@ func TestNoBlockResponse(t *testing.T) { func TestBadBlockStopsPeer(t *testing.T) { config = cfg.ResetTestRoot("blockchain_reactor_test") defer os.RemoveAll(config.RootDir) - genDoc, privVals := randGenesisDoc(1, false, 30) + genDoc, privVals := randGenesisDoc(t) maxBlockHeight := int64(148) // Other chain needs a different validator set - otherGenDoc, otherPrivVals := randGenesisDoc(1, false, 30) - otherChain := newReactor(t, log.TestingLogger(), otherGenDoc, otherPrivVals, maxBlockHeight) + otherGenDoc, otherPrivVals := randGenesisDoc(t) + otherChain := newReactor(t, log.TestingLogger(), otherGenDoc, otherPrivVals[0], maxBlockHeight) defer func() { err := otherChain.reactor.Stop() @@ -232,10 +215,10 @@ func TestBadBlockStopsPeer(t *testing.T) { reactorPairs := make([]ReactorPair, 4) - reactorPairs[0] = newReactor(t, log.TestingLogger(), genDoc, privVals, maxBlockHeight) - reactorPairs[1] = newReactor(t, log.TestingLogger(), genDoc, privVals, 0) - reactorPairs[2] = newReactor(t, log.TestingLogger(), genDoc, privVals, 0) - reactorPairs[3] = newReactor(t, log.TestingLogger(), genDoc, privVals, 0) + reactorPairs[0] = newReactor(t, log.TestingLogger(), genDoc, privVals[0], maxBlockHeight) + reactorPairs[1] = newReactor(t, log.TestingLogger(), genDoc, privVals[0], 0) + reactorPairs[2] = newReactor(t, log.TestingLogger(), genDoc, privVals[0], 0) + reactorPairs[3] = newReactor(t, log.TestingLogger(), genDoc, privVals[0], 0) switches := p2p.MakeConnectedSwitches(config.P2P, 4, func(i int, s *p2p.Switch) *p2p.Switch { s.AddReactor("BLOCKCHAIN", reactorPairs[i].reactor) @@ -273,7 +256,7 @@ func TestBadBlockStopsPeer(t *testing.T) { // race, but can't be easily avoided. reactorPairs[3].reactor.store = otherChain.reactor.store - lastReactorPair := newReactor(t, log.TestingLogger(), genDoc, privVals, 0) + lastReactorPair := newReactor(t, log.TestingLogger(), genDoc, privVals[0], 0) reactorPairs = append(reactorPairs, lastReactorPair) switches = append(switches, p2p.MakeConnectedSwitches(config.P2P, 1, func(i int, s *p2p.Switch) *p2p.Switch { diff --git a/evidence/verify_test.go b/evidence/verify_test.go index ff50a8e60..4744cab27 100644 --- a/evidence/verify_test.go +++ b/evidence/verify_test.go @@ -526,14 +526,6 @@ func makeLunaticEvidence( return ev, trusted, common } -// func makeEquivocationEvidence() *types.LightClientAttackEvidence { - -// } - -// func makeAmnesiaEvidence() *types.LightClientAttackEvidence { - -// } - 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 { diff --git a/internal/test/commit.go b/internal/test/commit.go index 681116f9c..fbf06a330 100644 --- a/internal/test/commit.go +++ b/internal/test/commit.go @@ -32,7 +32,9 @@ func MakeExtendedCommitFromVoteSet(blockID types.BlockID, voteSet *types.VoteSet return nil, err } vote.Signature = v.Signature - vote.ExtensionSignature = v.ExtensionSignature + if voteSet.ExtensionsEnabled() && !blockID.IsNil() { + vote.ExtensionSignature = v.ExtensionSignature + } if _, err := voteSet.AddVote(vote); err != nil { return nil, err } diff --git a/internal/test/factory_test.go b/internal/test/factory_test.go index 221e4d130..21f5709e1 100644 --- a/internal/test/factory_test.go +++ b/internal/test/factory_test.go @@ -14,3 +14,12 @@ func TestMakeHeader(t *testing.T) { require.NoError(t, header.ValidateBasic()) } + +func TestValidatorSet(t *testing.T) { + valSet, privVals := ValidatorSet(t, 5, 10) + for idx, val := range valSet.Validators { + pk, err := privVals[idx].GetPubKey() + require.NoError(t, err) + require.Equal(t, val.PubKey, pk) + } +} \ No newline at end of file diff --git a/internal/test/vote.go b/internal/test/vote.go index 180cb943d..0f0a95585 100644 --- a/internal/test/vote.go +++ b/internal/test/vote.go @@ -38,5 +38,19 @@ func MakeVote( } v.Signature = vpb.Signature + if tmproto.SignedMsgType(step) == tmproto.PrecommitType { + v.ExtensionSignature = vpb.ExtensionSignature + } return v, nil } + +func MakePrecommit( + val types.PrivValidator, + valIndex int32, + height int64, + round int32, + blockID types.BlockID, + time time.Time, +) (*types.Vote, error) { + return MakeVote(val, DefaultTestChainID, valIndex, height, round, int(tmproto.PrecommitType), blockID, time) +} diff --git a/proto/tendermint/crypto/keys.pb.go b/proto/tendermint/crypto/keys.pb.go index bbd97d446..cfd176c6e 100644 --- a/proto/tendermint/crypto/keys.pb.go +++ b/proto/tendermint/crypto/keys.pb.go @@ -27,6 +27,7 @@ const _ = proto.GoGoProtoPackageIsVersion3 // please upgrade the proto package // PublicKey defines the keys available for use with Tendermint Validators type PublicKey struct { // Types that are valid to be assigned to Sum: + // // *PublicKey_Ed25519 // *PublicKey_Secp256K1 Sum isPublicKey_Sum `protobuf_oneof:"sum"` diff --git a/proto/tendermint/mempool/types.pb.go b/proto/tendermint/mempool/types.pb.go index f7d16a64c..e63d67611 100644 --- a/proto/tendermint/mempool/types.pb.go +++ b/proto/tendermint/mempool/types.pb.go @@ -68,6 +68,7 @@ func (m *Txs) GetTxs() [][]byte { type Message struct { // Types that are valid to be assigned to Sum: + // // *Message_Txs Sum isMessage_Sum `protobuf_oneof:"sum"` } diff --git a/proto/tendermint/p2p/conn.pb.go b/proto/tendermint/p2p/conn.pb.go index dccdbe4a0..b2ace8eb8 100644 --- a/proto/tendermint/p2p/conn.pb.go +++ b/proto/tendermint/p2p/conn.pb.go @@ -158,6 +158,7 @@ func (m *PacketMsg) GetData() []byte { type Packet struct { // Types that are valid to be assigned to Sum: + // // *Packet_PacketPing // *Packet_PacketPong // *Packet_PacketMsg diff --git a/proto/tendermint/rpc/grpc/types.pb.go b/proto/tendermint/rpc/grpc/types.pb.go index 71ab7725f..4496379b9 100644 --- a/proto/tendermint/rpc/grpc/types.pb.go +++ b/proto/tendermint/rpc/grpc/types.pb.go @@ -6,6 +6,7 @@ package coregrpc import ( context "context" fmt "fmt" + grpc1 "github.com/cosmos/gogoproto/grpc" proto "github.com/cosmos/gogoproto/proto" types "github.com/tendermint/tendermint/abci/types" grpc "google.golang.org/grpc" @@ -245,10 +246,10 @@ type BroadcastAPIClient interface { } type broadcastAPIClient struct { - cc *grpc.ClientConn + cc grpc1.ClientConn } -func NewBroadcastAPIClient(cc *grpc.ClientConn) BroadcastAPIClient { +func NewBroadcastAPIClient(cc grpc1.ClientConn) BroadcastAPIClient { return &broadcastAPIClient{cc} } @@ -287,7 +288,7 @@ func (*UnimplementedBroadcastAPIServer) BroadcastTx(ctx context.Context, req *Re return nil, status.Errorf(codes.Unimplemented, "method BroadcastTx not implemented") } -func RegisterBroadcastAPIServer(s *grpc.Server, srv BroadcastAPIServer) { +func RegisterBroadcastAPIServer(s grpc1.Server, srv BroadcastAPIServer) { s.RegisterService(&_BroadcastAPI_serviceDesc, srv) } diff --git a/proto/tendermint/state/types.pb.go b/proto/tendermint/state/types.pb.go index 6abc696bb..ab9e94386 100644 --- a/proto/tendermint/state/types.pb.go +++ b/proto/tendermint/state/types.pb.go @@ -6,9 +6,9 @@ package state import ( fmt "fmt" _ "github.com/cosmos/gogoproto/gogoproto" + proto "github.com/cosmos/gogoproto/proto" _ "github.com/cosmos/gogoproto/types" - proto "github.com/gogo/protobuf/proto" - github_com_gogo_protobuf_types "github.com/gogo/protobuf/types" + github_com_cosmos_gogoproto_types "github.com/cosmos/gogoproto/types" types "github.com/tendermint/tendermint/abci/types" types1 "github.com/tendermint/tendermint/proto/tendermint/types" version "github.com/tendermint/tendermint/proto/tendermint/version" @@ -1082,7 +1082,7 @@ func (m *State) MarshalToSizedBuffer(dAtA []byte) (int, error) { i-- dAtA[i] = 0x32 } - n13, err13 := github_com_gogo_protobuf_types.StdTimeMarshalTo(m.LastBlockTime, dAtA[i-github_com_gogo_protobuf_types.SizeOfStdTime(m.LastBlockTime):]) + n13, err13 := github_com_cosmos_gogoproto_types.StdTimeMarshalTo(m.LastBlockTime, dAtA[i-github_com_cosmos_gogoproto_types.SizeOfStdTime(m.LastBlockTime):]) if err13 != nil { return 0, err13 } @@ -1281,7 +1281,7 @@ func (m *State) Size() (n int) { } l = m.LastBlockID.Size() n += 1 + l + sovTypes(uint64(l)) - l = github_com_gogo_protobuf_types.SizeOfStdTime(m.LastBlockTime) + l = github_com_cosmos_gogoproto_types.SizeOfStdTime(m.LastBlockTime) n += 1 + l + sovTypes(uint64(l)) if m.NextValidators != nil { l = m.NextValidators.Size() @@ -2355,7 +2355,7 @@ func (m *State) Unmarshal(dAtA []byte) error { if postIndex > l { return io.ErrUnexpectedEOF } - if err := github_com_gogo_protobuf_types.StdTimeUnmarshal(&m.LastBlockTime, dAtA[iNdEx:postIndex]); err != nil { + if err := github_com_cosmos_gogoproto_types.StdTimeUnmarshal(&m.LastBlockTime, dAtA[iNdEx:postIndex]); err != nil { return err } iNdEx = postIndex diff --git a/proto/tendermint/statesync/types.pb.go b/proto/tendermint/statesync/types.pb.go index 147e0f7d4..9ec4a1f36 100644 --- a/proto/tendermint/statesync/types.pb.go +++ b/proto/tendermint/statesync/types.pb.go @@ -24,6 +24,7 @@ const _ = proto.GoGoProtoPackageIsVersion3 // please upgrade the proto package type Message struct { // Types that are valid to be assigned to Sum: + // // *Message_SnapshotsRequest // *Message_SnapshotsResponse // *Message_ChunkRequest diff --git a/types/block_test.go b/types/block_test.go index d11c2f288..d78464bd0 100644 --- a/types/block_test.go +++ b/types/block_test.go @@ -606,6 +606,7 @@ func TestExtendedCommitToVoteSet(t *testing.T) { h := int64(3) voteSet, valSet, vals := randVoteSet(h-1, 1, tmproto.PrecommitType, 10, 1) + voteSet.extensionsEnabled = testCase.includeExtension extCommit, err := makeExtCommit(lastID, h-1, 1, voteSet, vals, time.Now()) assert.NoError(t, err) diff --git a/types/test_util.go b/types/test_util.go index 9d5b49e0b..05a2a66a0 100644 --- a/types/test_util.go +++ b/types/test_util.go @@ -44,6 +44,9 @@ func signAddVote(privVal PrivValidator, vote *Vote, voteSet *VoteSet) (signed bo return false, err } vote.Signature = v.Signature + if voteSet.extensionsEnabled { + vote.ExtensionSignature = v.ExtensionSignature + } return voteSet.AddVote(vote) } diff --git a/types/vote.go b/types/vote.go index 12b7c4765..92698541a 100644 --- a/types/vote.go +++ b/types/vote.go @@ -233,6 +233,10 @@ func (vote *Vote) VerifyVoteAndExtension(chainID string, pubKey crypto.PubKey) e } // We only verify vote extension signatures for non-nil precommits. if vote.Type == tmproto.PrecommitType && !IsProtoBlockIDNil(&v.BlockID) { + if len(vote.ExtensionSignature) == 0 { + return errors.New("expected vote extension signature") + } + extSignBytes := VoteExtensionSignBytes(chainID, v) if !pubKey.VerifySignature(extSignBytes, vote.ExtensionSignature) { return ErrVoteInvalidSignature diff --git a/types/vote_set.go b/types/vote_set.go index 08838fa37..a9448e99b 100644 --- a/types/vote_set.go +++ b/types/vote_set.go @@ -143,6 +143,10 @@ func (voteSet *VoteSet) Size() int { return voteSet.valSet.Size() } +func (voteSet *VoteSet) ExtensionsEnabled() bool { + return voteSet.extensionsEnabled +} + // Returns added=true if vote is valid and new. // Otherwise returns err=ErrVote[ // @@ -216,7 +220,7 @@ func (voteSet *VoteSet) addVote(vote *Vote) (added bool, err error) { // Check signature. if voteSet.extensionsEnabled { if err := vote.VerifyVoteAndExtension(voteSet.chainID, val.PubKey); err != nil { - return false, fmt.Errorf("failed to verify vote with ChainID %s and PubKey %s: %w", voteSet.chainID, val.PubKey, err) + return false, fmt.Errorf("failed to verify extended vote with ChainID %s and PubKey %s: %w", voteSet.chainID, val.PubKey, err) } } else { if err := vote.Verify(voteSet.chainID, val.PubKey); err != nil {