Skip to content

Commit

Permalink
revert invariant improved
Browse files Browse the repository at this point in the history
  • Loading branch information
srene committed Mar 18, 2024
1 parent 0b17218 commit 5338daa
Show file tree
Hide file tree
Showing 5 changed files with 144 additions and 27 deletions.
3 changes: 3 additions & 0 deletions testutil/keeper/delayedack.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,9 @@ func (RollappKeeperStub) GetLatestStateInfoIndex(ctx sdk.Context, rollappId stri
func (RollappKeeperStub) GetLatestFinalizedStateIndex(ctx sdk.Context, rollappId string) (val rollapptypes.StateInfoIndex, found bool) {
return rollapptypes.StateInfoIndex{}, false

Check warning on line 104 in testutil/keeper/delayedack.go

View check run for this annotation

Codecov / codecov/patch

testutil/keeper/delayedack.go#L103-L104

Added lines #L103 - L104 were not covered by tests
}
func (RollappKeeperStub) GetAllRollapps(ctx sdk.Context) (list []rollapptypes.Rollapp) {
return []rollapptypes.Rollapp{}

Check warning on line 107 in testutil/keeper/delayedack.go

View check run for this annotation

Codecov / codecov/patch

testutil/keeper/delayedack.go#L106-L107

Added lines #L106 - L107 were not covered by tests
}

type SequencerKeeperStub struct{}

Expand Down
47 changes: 20 additions & 27 deletions x/delayedack/keeper/invariants.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ func RollappFinalizedPackets(k Keeper) sdk.Invariant {
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)
Expand Down Expand Up @@ -69,42 +68,36 @@ func RollappRevertedPackets(k Keeper) sdk.Invariant {
msg string
)

packets := k.GetAllRollappPackets(ctx)

for _, packet := range packets {
rollapps := k.rollappKeeper.GetAllRollapps(ctx)

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
}
latestFinalizedStateInfo, found := k.rollappKeeper.GetStateInfo(ctx, packet.RollappId, latestFinalizedStateIndex.Index)
for _, rollapp := range rollapps {
latestFinalizedStateIndex, found := k.rollappKeeper.GetLatestFinalizedStateIndex(ctx, rollapp.RollappId)
if !found {
msg += fmt.Sprintf("unable to find latest finalized state info for rollapp %s\n", packet.RollappId)
broken = true
continue
}
latestFinalizedHeight := latestFinalizedStateInfo.StartHeight + latestFinalizedStateInfo.NumBlocks - 1
if packet.ProofHeight > latestFinalizedHeight {
stateInfoIndex := latestFinalizedStateIndex.Index + 1
for {
stateInfoToCheck, found := k.rollappKeeper.GetStateInfo(ctx, packet.RollappId, stateInfoIndex)
if found {
if stateInfoToCheck.Status == commontypes.Status_REVERTED {
if stateInfoToCheck.StartHeight >= packet.ProofHeight && stateInfoToCheck.StartHeight+stateInfoToCheck.NumBlocks < packet.ProofHeight {
if packet.Status != commontypes.Status_REVERTED {
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)

stateInfoIndex := latestFinalizedStateIndex.Index + 1
//Checking that all packets after the latest finalized height, that belong to a reverted state info, are also in reverted state
for {
stateInfoToCheck, found := k.rollappKeeper.GetStateInfo(ctx, rollapp.RollappId, stateInfoIndex)
if found {
if stateInfoToCheck.Status == commontypes.Status_REVERTED {
//TODO (srene) add GetRollappPacketByRollap to be more efficient
for _, packet := range k.GetAllRollappPackets(ctx) {
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
break
}

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

View check run for this annotation

Codecov / codecov/patch

x/delayedack/keeper/invariants.go#L85-L91

Added lines #L85 - L91 were not covered by tests
}
}
} else {
break
}
stateInfoIndex++
} else {
break
}

stateInfoIndex++
}

}
return msg, broken
}
Expand Down
119 changes: 119 additions & 0 deletions x/delayedack/keeper/invariants_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,126 @@ func (suite *DelayedAckTestSuite) TestInvariants() {
//progress finalization queue
suite.App.RollappKeeper.FinalizeQueue(suite.Ctx)

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

View workflow job for this annotation

GitHub Actions / golangci-lint

Error return value of `suite.App.RollappKeeper.FinalizeQueue` is not checked (errcheck)

//test fraud
for rollapp := range seqPerRollapp {
err := suite.App.DelayedAckKeeper.HandleFraud(ctx, rollapp)
suite.Require().Nil(err)
break
}

// check invariant
msg, bool := keeper.AllInvariants(suite.App.DelayedAckKeeper)(suite.Ctx)
suite.Require().False(bool, msg)
}

/*func (suite *RollappTestSuite) TestRollappFinalizedStateInvariant() {
suite.SetupTest()
ctx := suite.Ctx
rollapp1, rollapp2, rollapp3 := "rollapp1", "rollapp2", "rollapp3"
cases := []struct {
name string
rollappId string
stateInfo *types.StateInfo
latestFinalizedStateInfo types.StateInfo
latestStateInfoIndex types.StateInfo
expectedIsBroken bool
}{
{
"successful invariant check",
"rollapp1",
&types.StateInfo{
StateInfoIndex: types.StateInfoIndex{
RollappId: rollapp1,
Index: 1,
},
Status: commontypes.Status_FINALIZED,
},
types.StateInfo{
StateInfoIndex: types.StateInfoIndex{
RollappId: rollapp1,
Index: 2,
},
Status: commontypes.Status_FINALIZED,
},
types.StateInfo{
StateInfoIndex: types.StateInfoIndex{
RollappId: rollapp1,
Index: 3,
},
Status: commontypes.Status_PENDING,
},
false,
},
{
"failed invariant check - state not found",
rollapp2,
nil,
types.StateInfo{
StateInfoIndex: types.StateInfoIndex{
RollappId: rollapp2,
Index: 2,
},
Status: commontypes.Status_FINALIZED,
},
types.StateInfo{
StateInfoIndex: types.StateInfoIndex{
RollappId: rollapp2,
Index: 3,
},
Status: commontypes.Status_PENDING,
},
true,
},
{
"failed invariant check - state not finalized",
rollapp3,
&types.StateInfo{
StateInfoIndex: types.StateInfoIndex{
RollappId: rollapp3,
Index: 1,
},
Status: commontypes.Status_PENDING,
},
types.StateInfo{
StateInfoIndex: types.StateInfoIndex{
RollappId: rollapp3,
Index: 2,
},
Status: commontypes.Status_FINALIZED,
},
types.StateInfo{
StateInfoIndex: types.StateInfoIndex{
RollappId: rollapp3,
Index: 3,
},
Status: commontypes.Status_PENDING,
},
true,
},
}
for _, tc := range cases {
suite.Run(tc.name, func() {
// create rollapp
suite.CreateRollappWithName(tc.rollappId)
// update state infos
if tc.stateInfo != nil {
suite.App.RollappKeeper.SetStateInfo(ctx, *tc.stateInfo)
}
// update latest finalized state info
suite.App.RollappKeeper.SetStateInfo(ctx, tc.latestFinalizedStateInfo)
suite.App.RollappKeeper.SetLatestFinalizedStateIndex(ctx, types.StateInfoIndex{
RollappId: tc.rollappId,
Index: tc.latestFinalizedStateInfo.GetIndex().Index,
})
// update latest state info index
suite.App.RollappKeeper.SetStateInfo(ctx, tc.latestStateInfoIndex)
suite.App.RollappKeeper.SetLatestStateInfoIndex(ctx, types.StateInfoIndex{
RollappId: tc.rollappId,
Index: tc.latestStateInfoIndex.GetIndex().Index,
})
// check invariant
_, isBroken := keeper.RollappFinalizedStateInvariant(suite.App.RollappKeeper)(ctx)
suite.Require().Equal(tc.expectedIsBroken, isBroken)
})
}
}*/
1 change: 1 addition & 0 deletions x/delayedack/types/expected_keepers.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ type RollappKeeper interface {
StateInfo(ctx context.Context, req *rollapptypes.QueryGetStateInfoRequest) (*rollapptypes.QueryGetStateInfoResponse, error)
GetLatestStateInfoIndex(ctx sdk.Context, rollappId string) (val rollapptypes.StateInfoIndex, found bool)
GetLatestFinalizedStateIndex(ctx sdk.Context, rollappId string) (val types.StateInfoIndex, found bool)
GetAllRollapps(ctx sdk.Context) (list []types.Rollapp)
}

type SequencerKeeper interface {
Expand Down
1 change: 1 addition & 0 deletions x/rollapp/keeper/block_height_to_finalization_queue.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ func (k Keeper) FinalizeQueue(ctx sdk.Context) {
pendingFinalizationQueue := k.GetAllFinalizationQueueUntilHeight(ctx, finalizationHeight)

for _, blockHeightToFinalizationQueue := range pendingFinalizationQueue {

// finalize pending states
var failedToFinalizeQueue []types.StateInfoIndex
for _, stateInfoIndex := range blockHeightToFinalizationQueue.FinalizationQueue {
Expand Down

0 comments on commit 5338daa

Please sign in to comment.