Skip to content
This repository was archived by the owner on Nov 24, 2025. It is now read-only.

Commit b5af337

Browse files
committed
fix misbehavior tests
1 parent c23b13d commit b5af337

File tree

1 file changed

+55
-30
lines changed

1 file changed

+55
-30
lines changed

modules/light-clients/07-tendermint/types/misbehaviour_test.go

Lines changed: 55 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package types_test
22

33
import (
4+
"context"
5+
"fmt"
46
"time"
57

68
"github.com/tendermint/tendermint/crypto/tmhash"
@@ -28,6 +30,47 @@ func (suite *TendermintTestSuite) TestMisbehaviour() {
2830
suite.Require().Equal(clientID, misbehaviour.GetClientID())
2931
}
3032

33+
// TODO: remove when integrating into monorepo, this is testing tooling that is currently private to the module and not worth bumping tm version for
34+
func makeExtCommit(ctx context.Context, blockID tmtypes.BlockID, height int64, round int32,
35+
voteSet *tmtypes.VoteSet, validators []tmtypes.PrivValidator, now time.Time) (*tmtypes.ExtendedCommit, error) {
36+
37+
// all sign
38+
for i := 0; i < len(validators); i++ {
39+
pubKey, err := validators[i].GetPubKey(ctx)
40+
if err != nil {
41+
return nil, fmt.Errorf("can't get pubkey: %w", err)
42+
}
43+
vote := &tmtypes.Vote{
44+
ValidatorAddress: pubKey.Address(),
45+
ValidatorIndex: int32(i),
46+
Height: height,
47+
Round: round,
48+
Type: tmproto.PrecommitType,
49+
BlockID: blockID,
50+
Timestamp: now,
51+
}
52+
53+
_, err = signAddVote(ctx, validators[i], vote, voteSet)
54+
if err != nil {
55+
return nil, err
56+
}
57+
}
58+
59+
return voteSet.MakeExtendedCommit(), nil
60+
}
61+
62+
// TODO: remove this when integrating into monorepo
63+
func signAddVote(ctx context.Context, privVal tmtypes.PrivValidator, vote *tmtypes.Vote, voteSet *tmtypes.VoteSet) (signed bool, err error) {
64+
v := vote.ToProto()
65+
err = privVal.SignVote(ctx, voteSet.ChainID(), v)
66+
if err != nil {
67+
return false, err
68+
}
69+
vote.Signature = v.Signature
70+
vote.ExtensionSignature = v.ExtensionSignature
71+
return voteSet.AddVote(vote)
72+
}
73+
3174
func (suite *TendermintTestSuite) TestMisbehaviourValidateBasic() {
3275
altPrivVal := ibctestingmock.NewPV()
3376
altPubKey, err := altPrivVal.GetPubKey(suite.T().Context())
@@ -48,7 +91,7 @@ func (suite *TendermintTestSuite) TestMisbehaviourValidateBasic() {
4891
_, suiteVal := suite.valSet.GetByIndex(0)
4992
bothSigners := ibctesting.CreateSortedSignerArray(altPrivVal, suite.privVal, altVal, suiteVal)
5093

51-
// altSigners := []tmtypes.PrivValidator{altPrivVal}
94+
altSigners := []tmtypes.PrivValidator{altPrivVal}
5295

5396
heightMinus1 := clienttypes.NewHeight(0, height.RevisionHeight-1)
5497

@@ -180,31 +223,14 @@ func (suite *TendermintTestSuite) TestMisbehaviourValidateBasic() {
180223
func(misbehaviour *types.Misbehaviour) error {
181224
// voteSet contains only altVal which is less than 2/3 of total power (height/1height)
182225
wrongVoteSet := tmtypes.NewVoteSet(chainID, int64(misbehaviour.Header1.GetHeight().GetRevisionHeight()), 1, tmproto.PrecommitType, altValSet)
183-
// blockID, err := tmtypes.BlockIDFromProto(&misbehaviour.Header1.Commit.BlockID)
184-
// if err != nil {
185-
// return err
186-
// }
187-
// TODO(udpatil): do we need this step
188-
// for i, val := range tmValSet.Validators {
189-
// privVal := signers[i]
190-
// vote := &tmtypes.Vote{
191-
// Type: tmproto.PrecommitType,
192-
// Height: blockHeight,
193-
// Round: 1,
194-
// BlockID: blockID,
195-
// Timestamp: timestamp,
196-
// ValidatorAddress: val.Address,
197-
// ValidatorIndex: int32(i),
198-
// }
199-
// v := vote.ToProto()
200-
// err := privVal.SignVote(context.Background(), chainID, v)
201-
// require.NoError(chain.T, err)
202-
// vote.Signature = v.Signature
203-
// voteSet.AddVote(vote)
204-
// }
226+
blockID, err := tmtypes.BlockIDFromProto(&misbehaviour.Header1.Commit.BlockID)
227+
if err != nil {
228+
return err
229+
}
205230

206-
tmCommit := wrongVoteSet.MakeExtendedCommit()
231+
tmCommit, err := makeExtCommit(suite.T().Context(), *blockID, int64(misbehaviour.Header2.GetHeight().GetRevisionHeight()), misbehaviour.Header1.Commit.Round, wrongVoteSet, altSigners, suite.now)
207232
misbehaviour.Header1.Commit = tmCommit.ToCommit().ToProto()
233+
208234
return err
209235
},
210236
false,
@@ -219,13 +245,12 @@ func (suite *TendermintTestSuite) TestMisbehaviourValidateBasic() {
219245
func(misbehaviour *types.Misbehaviour) error {
220246
// voteSet contains only altVal which is less than 2/3 of total power (height/1height)
221247
wrongVoteSet := tmtypes.NewVoteSet(chainID, int64(misbehaviour.Header2.GetHeight().GetRevisionHeight()), 1, tmproto.PrecommitType, altValSet)
222-
// blockID, err := tmtypes.BlockIDFromProto(&misbehaviour.Header2.Commit.BlockID)
223-
// if err != nil {
224-
// return err
225-
// }
226-
// TODO(udpatil): determine same as above
248+
blockID, err := tmtypes.BlockIDFromProto(&misbehaviour.Header2.Commit.BlockID)
249+
if err != nil {
250+
return err
251+
}
227252

228-
tmCommit := wrongVoteSet.MakeExtendedCommit()
253+
tmCommit, err := makeExtCommit(suite.T().Context(), *blockID, int64(misbehaviour.Header2.GetHeight().GetRevisionHeight()), misbehaviour.Header2.Commit.Round, wrongVoteSet, altSigners, suite.now)
229254
misbehaviour.Header2.Commit = tmCommit.ToCommit().ToProto()
230255
return err
231256
},

0 commit comments

Comments
 (0)