Skip to content

Commit

Permalink
Merge branch 'main' into dong/TestPerfBalanceCallback
Browse files Browse the repository at this point in the history
  • Loading branch information
ThanhNhann authored Oct 23, 2023
2 parents 8e5f4a0 + 07545d7 commit e638e60
Showing 1 changed file with 114 additions and 35 deletions.
149 changes: 114 additions & 35 deletions x/interchainstaking/keeper/callbacks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (

ics23 "github.com/confio/ics23/go"
"github.com/stretchr/testify/require"
"github.com/tendermint/tendermint/proto/tendermint/types"

sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/bech32"
Expand All @@ -18,6 +19,7 @@ import (
distrtypes "github.com/cosmos/cosmos-sdk/x/distribution/types"
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"

ibctypes "github.com/cosmos/ibc-go/v5/modules/core/02-client/types"
lightclienttypes "github.com/cosmos/ibc-go/v5/modules/light-clients/07-tendermint/types"

"github.com/quicksilver-zone/quicksilver/app"
Expand All @@ -34,6 +36,40 @@ const (
storeStakingKey = "store/staking/key"
)

func (suite *KeeperTestSuite) setupIbc() (*app.Quicksilver, sdk.Context) {
// reset test suite
suite.SetupTest()
suite.setupTestZones()

// setup quicksilver test app
quicksilver := suite.GetQuicksilverApp(suite.chainA)
quicksilver.InterchainstakingKeeper.CallbackHandler().RegisterCallbacks()

// get chainA context
ctx := suite.chainA.GetContext()

// get zone chainB context
zone, _ := quicksilver.InterchainstakingKeeper.GetZone(ctx, suite.chainB.ChainID)
zone.DepositAddress.IncrementBalanceWaitgroup()
zone.WithdrawalAddress.IncrementBalanceWaitgroup()
quicksilver.InterchainstakingKeeper.SetZone(ctx, &zone)

// get the tx from fixture
txWithProofBz := decodeBase64NoErr(localDepositTxFixture)
txRes := icqtypes.GetTxWithProofResponse{}
err := quicksilver.InterchainQueryKeeper.IBCKeeper.Codec().Unmarshal(txWithProofBz, &txRes)
suite.NoError(err)

// update payload header to ensure we can validate it.
txRes.Header.Header.Time = ctx.BlockTime()
// setup ClientConsensusState for checking Header validation
// Cheat, and set the client state and consensus state for 07-tendermint-0 to match the incoming header.
quicksilver.IBCKeeper.ClientKeeper.SetClientState(ctx, "07-tendermint-0", lightclienttypes.NewClientState("gaiatest-1", lightclienttypes.DefaultTrustLevel, time.Hour, time.Hour, time.Second*50, txRes.Header.TrustedHeight, []*ics23.ProofSpec{}, []string{}, false, false))
quicksilver.IBCKeeper.ClientKeeper.SetClientConsensusState(ctx, "07-tendermint-0", txRes.Header.TrustedHeight, txRes.Header.ConsensusState())

return quicksilver, ctx
}

func (suite *KeeperTestSuite) TestHandleValsetCallback() {
newVal := addressutils.GenerateValAddressForTest()

Expand Down Expand Up @@ -1621,40 +1657,7 @@ func decodeBase64NoErr(str string) []byte {
}

func (suite *KeeperTestSuite) TestDepositTxCallback() {
setupIbc := func() (*app.Quicksilver, sdk.Context) {
// reset test suite
suite.SetupTest()
suite.setupTestZones()

// setup quicksilver test app
quicksilver := suite.GetQuicksilverApp(suite.chainA)
quicksilver.InterchainstakingKeeper.CallbackHandler().RegisterCallbacks()

// get chainA context
ctx := suite.chainA.GetContext()

// get zone chainB context
zone, _ := quicksilver.InterchainstakingKeeper.GetZone(ctx, suite.chainB.ChainID)
zone.DepositAddress.IncrementBalanceWaitgroup()
zone.WithdrawalAddress.IncrementBalanceWaitgroup()
quicksilver.InterchainstakingKeeper.SetZone(ctx, &zone)

// get the tx from fixture
txWithProofBz := decodeBase64NoErr(localDepositTxFixture)
txRes := icqtypes.GetTxWithProofResponse{}
err := quicksilver.InterchainQueryKeeper.IBCKeeper.Codec().Unmarshal(txWithProofBz, &txRes)
suite.NoError(err)

// update payload header to ensure we can validate it.
txRes.Header.Header.Time = ctx.BlockTime()
// setup ClientConsensusState for checking Header validation
// Cheat, and set the client state and consensus state for 07-tendermint-0 to match the incoming header.
quicksilver.IBCKeeper.ClientKeeper.SetClientState(ctx, "07-tendermint-0", lightclienttypes.NewClientState("gaiatest-1", lightclienttypes.DefaultTrustLevel, time.Hour, time.Hour, time.Second*50, txRes.Header.TrustedHeight, []*ics23.ProofSpec{}, []string{}, false, false))
quicksilver.IBCKeeper.ClientKeeper.SetClientConsensusState(ctx, "07-tendermint-0", txRes.Header.TrustedHeight, txRes.Header.ConsensusState())

return quicksilver, ctx
}

// testcases
testCases := []struct {
name string
txHash string
Expand Down Expand Up @@ -1704,7 +1707,7 @@ func (suite *KeeperTestSuite) TestDepositTxCallback() {
for _, tc := range testCases {
suite.Run(fmt.Sprintf("Case %s", tc.name), func() {
// setup quicksilver and counter chain context
qckApp, ctx := setupIbc()
qckApp, ctx := suite.setupIbc()

// construct request data using txHash
requestData := tx.GetTxRequest{
Expand All @@ -1723,6 +1726,82 @@ func (suite *KeeperTestSuite) TestDepositTxCallback() {
}
}

func (suite *KeeperTestSuite) TestCheckTMHeaderForZone() {
testCases := []struct {
name string
expectedErr bool
changeCtx func(qck *app.Quicksilver, ctx *sdk.Context, txRes *icqtypes.GetTxWithProofResponse, zoneB *icstypes.Zone)
}{
{
name: "Check TM Header for Zone successful",
expectedErr: false,
changeCtx: nil,
},
{
name: "Check TM Header for Zone failed: unable to fetch client state",
expectedErr: true,
changeCtx: func(qck *app.Quicksilver, ctx *sdk.Context, txRes *icqtypes.GetTxWithProofResponse, zone *icstypes.Zone) {
zone.ConnectionId = "connection-1"
},
},
{
name: "Check TM Header for Zone failed: unable to fetch consensus state on trusted height",
expectedErr: true,
changeCtx: func(qck *app.Quicksilver, ctx *sdk.Context, txRes *icqtypes.GetTxWithProofResponse, zone *icstypes.Zone) {
txRes.Header.TrustedHeight = ibctypes.Height{RevisionNumber: 1, RevisionHeight: 24072000}
},
},
{
name: "Check TM Header for Zone failed: unable to verify TM Header",
expectedErr: true,
changeCtx: func(qck *app.Quicksilver, ctx *sdk.Context, txRes *icqtypes.GetTxWithProofResponse, zone *icstypes.Zone) {
*ctx = ctx.WithBlockTime(ctx.BlockHeader().Time.Add(time.Hour * 24 * 8))
},
},
{
name: "Check TM Header for Zone failed: unable to marshal proof",
expectedErr: true,
changeCtx: func(qck *app.Quicksilver, ctx *sdk.Context, txRes *icqtypes.GetTxWithProofResponse, zone *icstypes.Zone) {
txRes.Proof = &types.TxProof{}
},
},
{
name: "Check TM Header for Zone failed: unable to verify TM Header from wrong data hash",
expectedErr: true,
changeCtx: func(qck *app.Quicksilver, ctx *sdk.Context, txRes *icqtypes.GetTxWithProofResponse, zone *icstypes.Zone) {
txRes.Header.Header.DataHash = []byte("wrong data hash")
},
},
}

for _, tc := range testCases {
suite.Run(fmt.Sprintf("Case %s", tc.name), func() {
// setup app and counter chain ctx
qckApp, ctx := suite.setupIbc()

// get chain B
zone, _ := qckApp.InterchainstakingKeeper.GetZone(ctx, suite.chainB.ChainID)

// get the tx from fixture
txWithProofBz := decodeBase64NoErr(localDepositTxFixture)
txRes := icqtypes.GetTxWithProofResponse{}
_ = qckApp.InterchainQueryKeeper.IBCKeeper.Codec().Unmarshal(txWithProofBz, &txRes)

// setup ctx for testcase
if tc.expectedErr {
tc.changeCtx(qckApp, &ctx, &txRes, &zone)
}

err := qckApp.InterchainstakingKeeper.CheckTMHeaderForZone(ctx, &zone, txRes)
if tc.expectedErr {
suite.Error(err)
} else {
suite.NoError(err)
}
})
}
}

var localDepositTxFixture = `GsEDCiCFDDobCzFK2Vf0BXcgdEycLSdJL8IP7PEVWKelDQeJ3xL2AgrXAQrUAQocL2Nvc21vcy5iYW5rLnYxYmV0YTEuTXNnU2VuZBKzAQotY29zbW9zMWEyemh0OHgyajBkcXZ1ZWpyOHB4cHU3ZHVlM3FtazQwbGdkeTNoEkFjb3Ntb3MxYXZ2ZWhmM25wdm42d2V5eHR2eXU3bWh3d3Zqcnl6dzY5ZzQzdHEwbmw4MHdxamdscjZoc2U1bWN6NBo/Cjdjb3Ntb3N2YWxvcGVyMWdnN3c4dzJ5OWpmdjc2YTJ5eWFoZTQyeTA5ZzlyeTJyYWE1cnFmLzE0EgQ1MDAwElgKUApGCh8vY29zbW9zLmN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gOeUAdpun5LhBSfIBHUbK7Zjyzn8VRr1ZwYhCESPQoUeQRs3t3nFppZq/OiJ+/f0AsW+twSIgogx4ew5LC25gOeUAdpun5LhBSfIBHUbK7Zjyzn8VRr1ZwYhCEaBggBEKvdDiJ+Cj0KFHkEbN7d5xaaWavzoifv39ALFvrcEiIKIMeHsOSwtuYDnlAHabp+S4QUnyAR1Gyu2Y8s5/FUa9WcGIQhEj0KFHkEbN7d5xaaWavzoifv39ALFvrcEiIKIMeHsOSwtuYDnlAHabp+S4QUnyAR1Gyu2Y8s5/FUa9WcGIQh`

func (suite *KeeperTestSuite) TestPerfBalanceCallbackUpdate() {
Expand Down

0 comments on commit e638e60

Please sign in to comment.