Skip to content

Commit

Permalink
invariant tests added
Browse files Browse the repository at this point in the history
  • Loading branch information
srene committed Mar 18, 2024
1 parent 4fa1618 commit 106dfd1
Show file tree
Hide file tree
Showing 2 changed files with 205 additions and 16 deletions.
24 changes: 8 additions & 16 deletions x/delayedack/keeper/invariants.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,41 +32,33 @@ func AllInvariants(k Keeper) sdk.Invariant {
// RollappFinalizedPackets checks that all rollapp packets stored for a rollapp finalized height are also finalized
func RollappFinalizedPackets(k Keeper) sdk.Invariant {
return func(ctx sdk.Context) (string, bool) {
var (
broken bool
msg string
)

var msg string
packets := k.GetAllRollappPackets(ctx)

for _, packet := range packets {
latestFinalizedStateIndex, found := k.rollappKeeper.GetLatestFinalizedStateIndex(ctx, packet.RollappId)
if !found {
msg += fmt.Sprintf("unable to find latest finalized state index for rollapp %s\n", packet.RollappId)
broken = true
continue

Check warning on line 42 in x/delayedack/keeper/invariants.go

View check run for this annotation

Codecov / codecov/patch

x/delayedack/keeper/invariants.go#L42

Added line #L42 was not covered by tests
}
latestFinalizedStateInfo, found := k.rollappKeeper.GetStateInfo(ctx, packet.RollappId, latestFinalizedStateIndex.Index)
if !found {
msg += fmt.Sprintf("unable to find latest finalized state info for rollapp %s\n", packet.RollappId)
broken = true
continue

Check warning on line 46 in x/delayedack/keeper/invariants.go

View check run for this annotation

Codecov / codecov/patch

x/delayedack/keeper/invariants.go#L46

Added line #L46 was not covered by tests
}
latestFinalizedHeight := latestFinalizedStateInfo.StartHeight + latestFinalizedStateInfo.NumBlocks - 1
if packet.ProofHeight <= latestFinalizedHeight && packet.Status != commontypes.Status_FINALIZED {
msg += fmt.Sprintf("rollapp packet for height %d from rollapp %s should be in finalized status, but is in %s status\n", packet.ProofHeight, packet.RollappId, packet.Status)
broken = true
return msg, true
}
}
return msg, broken
return msg, false
}
}

// RollappRevertedPackets checks that all rollapp packets stored for a rollapp reverted height are also reverted
func RollappRevertedPackets(k Keeper) sdk.Invariant {
return func(ctx sdk.Context) (string, bool) {
var (
broken bool
msg string
)
var msg string

rollapps := k.rollappKeeper.GetAllRollapps(ctx)

Expand All @@ -87,7 +79,7 @@ func RollappRevertedPackets(k Keeper) sdk.Invariant {
if packet.RollappId == rollapp.RollappId {
if packet.ProofHeight >= stateInfoToCheck.StartHeight && packet.ProofHeight < stateInfoToCheck.StartHeight+stateInfoToCheck.NumBlocks && packet.Status != commontypes.Status_REVERTED {
msg += fmt.Sprintf("rollapp packet for height %d from rollapp %s should be in reverted status, but is in %s status\n", packet.ProofHeight, packet.RollappId, packet.Status)
broken = true
return msg, true
}
}
}
Expand All @@ -99,6 +91,6 @@ func RollappRevertedPackets(k Keeper) sdk.Invariant {
}

}
return msg, broken
return msg, false
}
}
197 changes: 197 additions & 0 deletions x/delayedack/keeper/invariants_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
channeltypes "github.com/cosmos/ibc-go/v6/modules/core/04-channel/types"
commontypes "github.com/dymensionxyz/dymension/v3/x/common/types"
"github.com/dymensionxyz/dymension/v3/x/delayedack/keeper"
"github.com/dymensionxyz/dymension/v3/x/rollapp/types"
rollapptypes "github.com/dymensionxyz/dymension/v3/x/rollapp/types"
)

func (suite *DelayedAckTestSuite) TestInvariants() {
Expand Down Expand Up @@ -81,3 +83,198 @@ func (suite *DelayedAckTestSuite) TestInvariants() {
msg, bool := keeper.AllInvariants(suite.App.DelayedAckKeeper)(suite.Ctx)
suite.Require().False(bool, msg)
}

func (suite *DelayedAckTestSuite) TestRollappPacketsCasesInvariant() {
suite.SetupTest()
ctx := suite.Ctx
rollapp := "rollapp1"
cases := []struct {
name string
rollappId string
stateInfo *rollapptypes.StateInfo
stateInfo2 *rollapptypes.StateInfo
packet commontypes.RollappPacket
packet2 commontypes.RollappPacket
expectedIsBroken bool
}{
{
"successful invariant check",
rollapp,
&rollapptypes.StateInfo{
StateInfoIndex: rollapptypes.StateInfoIndex{
RollappId: rollapp,
Index: 1,
},
StartHeight: 1,
NumBlocks: 10,
Status: commontypes.Status_FINALIZED,
},
&rollapptypes.StateInfo{
StateInfoIndex: rollapptypes.StateInfoIndex{
RollappId: rollapp,
Index: 2,
},
StartHeight: 10,
NumBlocks: 10,
Status: commontypes.Status_PENDING,
},
commontypes.RollappPacket{
RollappId: rollapp,
Status: commontypes.Status_FINALIZED,
ProofHeight: 5,
Packet: &channeltypes.Packet{
SourcePort: "testSourcePort",
SourceChannel: "testSourceChannel",
DestinationPort: "testDestinationPort",
DestinationChannel: "testDestinationChannel",
Data: []byte("testData"),
Sequence: uint64(1),
},
},
commontypes.RollappPacket{
RollappId: rollapp,
Status: commontypes.Status_PENDING,
ProofHeight: 15,
Packet: &channeltypes.Packet{
SourcePort: "testSourcePort",
SourceChannel: "testSourceChannel",
DestinationPort: "testDestinationPort",
DestinationChannel: "testDestinationChannel",
Data: []byte("testData"),
Sequence: uint64(2),
},
},
false,
},
{
"error non-finalized packet",
rollapp,
&rollapptypes.StateInfo{
StateInfoIndex: rollapptypes.StateInfoIndex{
RollappId: rollapp,
Index: 1,
},
StartHeight: 1,
NumBlocks: 10,
Status: commontypes.Status_FINALIZED,
},
&rollapptypes.StateInfo{
StateInfoIndex: rollapptypes.StateInfoIndex{
RollappId: rollapp,
Index: 2,
},
StartHeight: 10,
NumBlocks: 10,
Status: commontypes.Status_FINALIZED,
},
commontypes.RollappPacket{
RollappId: rollapp,
Status: commontypes.Status_FINALIZED,
ProofHeight: 5,
Packet: &channeltypes.Packet{
SourcePort: "testSourcePort",
SourceChannel: "testSourceChannel",
DestinationPort: "testDestinationPort",
DestinationChannel: "testDestinationChannel",
Data: []byte("testData"),
Sequence: uint64(1),
},
},
commontypes.RollappPacket{
RollappId: rollapp,
Status: commontypes.Status_PENDING,
ProofHeight: 15,
Packet: &channeltypes.Packet{
SourcePort: "testSourcePort",
SourceChannel: "testSourceChannel",
DestinationPort: "testDestinationPort",
DestinationChannel: "testDestinationChannel",
Data: []byte("testData"),
Sequence: uint64(2),
},
},
true,
},
{
"wrong invariant revert check",
rollapp,
&rollapptypes.StateInfo{
StateInfoIndex: rollapptypes.StateInfoIndex{
RollappId: rollapp,
Index: 1,
},
StartHeight: 1,
NumBlocks: 10,
Status: commontypes.Status_FINALIZED,
},
&rollapptypes.StateInfo{
StateInfoIndex: rollapptypes.StateInfoIndex{
RollappId: rollapp,
Index: 2,
},
StartHeight: 10,
NumBlocks: 10,
Status: commontypes.Status_REVERTED,
},
commontypes.RollappPacket{
RollappId: rollapp,
Status: commontypes.Status_FINALIZED,
ProofHeight: 5,
Packet: &channeltypes.Packet{
SourcePort: "testSourcePort",
SourceChannel: "testSourceChannel",
DestinationPort: "testDestinationPort",
DestinationChannel: "testDestinationChannel",
Data: []byte("testData"),
Sequence: uint64(1),
},
},
commontypes.RollappPacket{
RollappId: rollapp,
Status: commontypes.Status_PENDING,
ProofHeight: 15,
Packet: &channeltypes.Packet{
SourcePort: "testSourcePort",
SourceChannel: "testSourceChannel",
DestinationPort: "testDestinationPort",
DestinationChannel: "testDestinationChannel",
Data: []byte("testData"),
Sequence: uint64(2),
},
},
true,
},
}
for _, tc := range cases {
suite.Run(tc.name, func() {
// create rollapp
suite.CreateRollappWithName(tc.rollappId)
// update state infos
suite.App.RollappKeeper.SetStateInfo(ctx, *tc.stateInfo)
if tc.stateInfo.Status == commontypes.Status_FINALIZED {
suite.App.RollappKeeper.SetLatestFinalizedStateIndex(ctx, types.StateInfoIndex{
RollappId: tc.rollappId,
Index: tc.stateInfo.GetIndex().Index,
})
}
suite.App.RollappKeeper.SetStateInfo(ctx, *tc.stateInfo2)
if tc.stateInfo2.Status == commontypes.Status_FINALIZED {
suite.App.RollappKeeper.SetLatestFinalizedStateIndex(ctx, types.StateInfoIndex{
RollappId: tc.rollappId,
Index: tc.stateInfo2.GetIndex().Index,
})
}
suite.App.RollappKeeper.SetLatestStateInfoIndex(ctx, types.StateInfoIndex{
RollappId: tc.rollappId,
Index: tc.stateInfo2.GetIndex().Index,
})

suite.App.DelayedAckKeeper.SetRollappPacket(ctx, tc.packet)

Check failure on line 272 in x/delayedack/keeper/invariants_test.go

View workflow job for this annotation

GitHub Actions / golangci-lint

Error return value of `suite.App.DelayedAckKeeper.SetRollappPacket` is not checked (errcheck)
suite.App.DelayedAckKeeper.SetRollappPacket(ctx, tc.packet2)

Check failure on line 273 in x/delayedack/keeper/invariants_test.go

View workflow job for this annotation

GitHub Actions / golangci-lint

Error return value of `suite.App.DelayedAckKeeper.SetRollappPacket` is not checked (errcheck)

// check invariant
_, isBroken := keeper.AllInvariants(suite.App.DelayedAckKeeper)(suite.Ctx)
suite.Require().Equal(tc.expectedIsBroken, isBroken)
})
}
}

0 comments on commit 106dfd1

Please sign in to comment.