Skip to content

Commit

Permalink
perf: skip pruning on check tx and recheck tx
Browse files Browse the repository at this point in the history
  • Loading branch information
colin-axner committed May 8, 2024
1 parent edeef93 commit 9b9adbe
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 1 deletion.
5 changes: 4 additions & 1 deletion modules/light-clients/07-tendermint/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,10 @@ func (cs ClientState) UpdateState(ctx sdk.Context, cdc codec.BinaryCodec, client
panic(fmt.Errorf("expected type %T, got %T", &Header{}, clientMsg))
}

cs.pruneOldestConsensusState(ctx, cdc, clientStore)
// don't do prune logic in CheckTx
if !ctx.IsCheckTx() && !ctx.IsReCheckTx() {
cs.pruneOldestConsensusState(ctx, cdc, clientStore)
}

// check for duplicate update
if _, found := GetConsensusState(clientStore, cdc, header.GetHeight()); found {
Expand Down
55 changes: 55 additions & 0 deletions modules/light-clients/07-tendermint/update_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -557,6 +557,61 @@ func (suite *TendermintTestSuite) TestUpdateState() {
}
}

func (suite *TendermintTestSuite) TestUpdateStateCheckTx() {
path := ibctesting.NewPath(suite.chainA, suite.chainB)
path.SetupClients()

createClientMessage := func() exported.ClientMessage {
trustedHeight, ok := path.EndpointA.GetClientLatestHeight().(clienttypes.Height)
suite.Require().True(ok)
header, err := path.EndpointB.Chain.IBCClientHeader(path.EndpointB.Chain.LatestCommittedHeader, trustedHeight)
suite.Require().NoError(err)
return header
}

// get the first height as it will be pruned first.
var pruneHeight exported.Height
getFirstHeightCb := func(height exported.Height) bool {
pruneHeight = height
return true
}
ctx := path.EndpointA.Chain.GetContext()
clientStore := path.EndpointA.Chain.App.GetIBCKeeper().ClientKeeper.ClientStore(ctx, path.EndpointA.ClientID)
ibctm.IterateConsensusStateAscending(clientStore, getFirstHeightCb)

// Increment the time by a week
suite.coordinator.IncrementTimeBy(7 * 24 * time.Hour)

lightClientModule, found := suite.chainA.App.GetIBCKeeper().ClientKeeper.Route(path.EndpointA.ClientID)
suite.Require().True(found)

ctx = path.EndpointA.Chain.GetContext().WithIsCheckTx(true)
lightClientModule.UpdateState(ctx, path.EndpointA.ClientID, createClientMessage())

// Increment the time by another week, then update the client.
// This will cause the first two consensus states to become expired.
suite.coordinator.IncrementTimeBy(7 * 24 * time.Hour)
ctx = path.EndpointA.Chain.GetContext().WithIsCheckTx(true)
lightClientModule.UpdateState(ctx, path.EndpointA.ClientID, createClientMessage())

// check that the first expired consensus state got deleted along with all associated metadata
consState, ok := path.EndpointA.Chain.GetConsensusState(path.EndpointA.ClientID, pruneHeight)
suite.Require().NotNil(consState, "expired consensus state pruned")
suite.Require().True(ok)

// check processed time metadata is pruned
processTime, ok := ibctm.GetProcessedTime(clientStore, pruneHeight)
suite.Require().NotEqual(uint64(0), processTime, "processed time metadata pruned")
suite.Require().True(ok)
processHeight, ok := ibctm.GetProcessedHeight(clientStore, pruneHeight)
suite.Require().NotNil(processHeight, "processed height metadata pruned")
suite.Require().True(ok)

// check iteration key metadata is pruned
consKey := ibctm.GetIterationKey(clientStore, pruneHeight)
suite.Require().NotNil(consKey, "iteration key pruned")
}

func (suite *TendermintTestSuite) TestPruneConsensusState() {
// create path and setup clients
path := ibctesting.NewPath(suite.chainA, suite.chainB)
Expand Down

0 comments on commit 9b9adbe

Please sign in to comment.