Skip to content

Commit

Permalink
Adding header interface for backwards compatibility (#3441)
Browse files Browse the repository at this point in the history
  • Loading branch information
chatton committed Apr 17, 2023
1 parent 2e3ea9a commit 15f11cf
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 18 deletions.
20 changes: 6 additions & 14 deletions e2e/tests/core/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ func (s *ClientTestSuite) TestClient_Update_Misbehaviour() {
trustedHeight clienttypes.Height
latestHeight clienttypes.Height
clientState ibcexported.ClientState
block *tmservice.Block
header testsuite.Header
signers []tmtypes.PrivValidator
validatorSet []*tmtypes.Validator
maliciousHeader *ibctm.Header
Expand Down Expand Up @@ -194,8 +194,8 @@ func (s *ClientTestSuite) TestClient_Update_Misbehaviour() {
t.Run("create validator set", func(t *testing.T) {
var validators []*tmservice.Validator

t.Run("fetch block at latest client state height", func(t *testing.T) {
block, err = s.GetBlockByHeight(ctx, chainB, latestHeight.GetRevisionHeight())
t.Run("fetch block header at latest client state height", func(t *testing.T) {
header, err = s.GetBlockHeaderByHeight(ctx, chainB, latestHeight.GetRevisionHeight())
s.Require().NoError(err)
})

Expand All @@ -221,7 +221,7 @@ func (s *ClientTestSuite) TestClient_Update_Misbehaviour() {
t.Run("create malicious header", func(t *testing.T) {
valSet := tmtypes.NewValidatorSet(validatorSet)
maliciousHeader, err = createMaliciousTMHeader(chainB.Config().ChainID, int64(latestHeight.GetRevisionHeight()), trustedHeight,
block.Header.GetTime(), valSet, valSet, signers, &block.Header)
header.GetTime(), valSet, valSet, signers, header)
s.Require().NoError(err)
})

Expand Down Expand Up @@ -283,22 +283,14 @@ func (s *ClientTestSuite) extractChainPrivateKeys(ctx context.Context, chain *co
}

// createMaliciousTMHeader creates a header with the provided trusted height with an invalid app hash.
func createMaliciousTMHeader(
chainID string,
blockHeight int64,
trustedHeight clienttypes.Height,
timestamp time.Time,
tmValSet, tmTrustedVals *tmtypes.ValidatorSet,
signers []tmtypes.PrivValidator,
oldHeader *tmservice.Header,
) (*ibctm.Header, error) {
func createMaliciousTMHeader(chainID string, blockHeight int64, trustedHeight clienttypes.Height, timestamp time.Time, tmValSet, tmTrustedVals *tmtypes.ValidatorSet, signers []tmtypes.PrivValidator, oldHeader testsuite.Header ) (*ibctm.Header, error) {
tmHeader := tmtypes.Header{
Version: tmprotoversion.Consensus{Block: tmversion.BlockProtocol, App: 2},
ChainID: chainID,
Height: blockHeight,
Time: timestamp,
LastBlockID: ibctesting.MakeBlockID(make([]byte, tmhash.Size), 10_000, make([]byte, tmhash.Size)),
LastCommitHash: oldHeader.LastCommitHash,
LastCommitHash: oldHeader.GetLastCommitHash(),
ValidatorsHash: tmValSet.Hash(),
NextValidatorsHash: tmValSet.Hash(),
DataHash: tmhash.Sum([]byte(invalidHashValue)),
Expand Down
21 changes: 17 additions & 4 deletions e2e/testsuite/grpc_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package testsuite
import (
"context"
"sort"
"time"

"github.com/cosmos/cosmos-sdk/client/grpc/tmservice"
govtypesv1 "github.com/cosmos/cosmos-sdk/x/gov/types/v1"
Expand All @@ -19,6 +20,13 @@ import (
ibcexported "github.com/cosmos/ibc-go/v7/modules/core/exported"
)

// Header defines an interface which is implemented by both the sdk block header and the cometbft Block Header.
// this interfaces allows us to use the same function to fetch the block header for both chains.
type Header interface {
GetTime() time.Time
GetLastCommitHash() []byte
}

// QueryClientState queries the client state on the given chain for the provided clientID.
func (s *E2ETestSuite) QueryClientState(ctx context.Context, chain ibc.Chain, clientID string) (ibcexported.ClientState, error) {
queryClient := s.GetChainGRCPClients(chain).ClientQueryClient
Expand Down Expand Up @@ -175,9 +183,8 @@ func (s *E2ETestSuite) QueryProposalV1(ctx context.Context, chain ibc.Chain, pro
return *res.Proposal, nil
}

// GetBlockByHeight fetches the block at a given height. Note: we are explicitly using the res.Block type which has been
// deprecated instead of res.SdkBlock to support backwards compatibility tests.
func (s *E2ETestSuite) GetBlockByHeight(ctx context.Context, chain ibc.Chain, height uint64) (*tmservice.Block, error) {
// GetBlockHeaderByHeight fetches the block header at a given height.
func (s *E2ETestSuite) GetBlockHeaderByHeight(ctx context.Context, chain ibc.Chain, height uint64) (Header, error) {
tmService := s.GetChainGRCPClients(chain).ConsensusServiceClient
res, err := tmService.GetBlockByHeight(ctx, &tmservice.GetBlockByHeightRequest{
Height: int64(height),
Expand All @@ -186,7 +193,13 @@ func (s *E2ETestSuite) GetBlockByHeight(ctx context.Context, chain ibc.Chain, he
return nil, err
}

return res.SdkBlock, nil
// versions newer than 0.47 SDK have the SdkBlock field.
if res.SdkBlock != nil {
return &res.SdkBlock.Header, nil
}

// older versions of the SDK do not have the SdkBlock field.
return &res.Block.Header, nil
}

// GetValidatorSetByHeight returns the validators of the given chain at the specified height. The returned validators
Expand Down

0 comments on commit 15f11cf

Please sign in to comment.