Skip to content

Commit

Permalink
feat(migrations): renamed vulenrable to obsolete
Browse files Browse the repository at this point in the history
  • Loading branch information
keruch committed Nov 7, 2024
1 parent 4fe9719 commit 23ad1e8
Show file tree
Hide file tree
Showing 19 changed files with 373 additions and 373 deletions.
8 changes: 4 additions & 4 deletions proto/dymensionxyz/dymension/rollapp/events.proto
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ message EventAppRemoved {
App app = 1;
}

message EventMarkVulnerableRollapps {
// VulnerableRollappNum is a number of rollapps that were marked as vulnerable.
uint64 vulnerable_rollapp_num = 1;
// DrsVersions is a list of DRS versions that were marked as vulnerable.
message EventMarkObsoleteRollapps {
// ObsoleteRollappNum is a number of rollapps that were marked as obsolete.
uint64 obsolete_rollapp_num = 1;
// DrsVersions is a list of DRS versions that were marked as obsolete.
repeated uint32 drs_versions = 2;
}
4 changes: 2 additions & 2 deletions proto/dymensionxyz/dymension/rollapp/genesis.proto
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ message GenesisState {
repeated App appList = 8 [(gogoproto.nullable) = false];
repeated RollappRegisteredDenoms registeredDenoms = 9 [(gogoproto.nullable) = false];
repeated SequencerHeightPair sequencerHeightPairs = 10 [(gogoproto.nullable) = false];
// VulnerableDrsVersions is a list of DRS versions that are marked vulnerable
repeated uint32 vulnerable_drs_versions = 11;
// ObsoleteDrsVersions is a list of DRS versions that are marked obsolete
repeated uint32 obsolete_drs_versions = 11;
}

message SequencerHeightPair {
Expand Down
10 changes: 5 additions & 5 deletions proto/dymensionxyz/dymension/rollapp/tx.proto
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ service Msg {
rpc AddApp(MsgAddApp) returns (MsgAddAppResponse);
rpc UpdateApp(MsgUpdateApp) returns (MsgUpdateAppResponse);
rpc RemoveApp(MsgRemoveApp) returns (MsgRemoveAppResponse);
rpc MarkVulnerableRollapps(MsgMarkVulnerableRollapps) returns (MsgMarkVulnerableRollappsResponse);
rpc MarkObsoleteRollapps(MsgMarkObsoleteRollapps) returns (MsgMarkObsoleteRollappsResponse);
}

// MsgCreateRollapp creates a new rollapp chain on the hub.
Expand Down Expand Up @@ -168,15 +168,15 @@ message MsgRemoveApp {
message MsgRemoveAppResponse {
}

// MsgMarkVulnerableRollapps marks specified versions as vulnerable as well as
// MsgMarkObsoleteRollapps marks specified versions as obsolete as well as
// all corresponding rollapps. Must be called by the governance.
message MsgMarkVulnerableRollapps {
message MsgMarkObsoleteRollapps {
option (cosmos.msg.v1.signer) = "authority";

// Authority is the authority address.
string authority = 1;
// DrsVersions is a list of DRS versions that will be marked vulnerable.
// DrsVersions is a list of DRS versions that will be marked obsolete.
repeated uint32 drs_versions = 2;
}

message MsgMarkVulnerableRollappsResponse {}
message MsgMarkObsoleteRollappsResponse {}
10 changes: 5 additions & 5 deletions x/rollapp/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,9 @@ func InitGenesis(ctx sdk.Context, k keeper.Keeper, genState types.GenesisState)
panic(err)
}
}
// Set all the vulnerable DRS versions
for _, elem := range genState.VulnerableDrsVersions {
err := k.SetVulnerableDRSVersion(ctx, elem)
// Set all the obsolete DRS versions
for _, elem := range genState.ObsoleteDrsVersions {
err := k.SetObsoleteDRSVersion(ctx, elem)
if err != nil {
panic(err)
}
Expand Down Expand Up @@ -99,11 +99,11 @@ func ExportGenesis(ctx sdk.Context, k keeper.Keeper) *types.GenesisState {
panic(err)
}

drsVersions, err := k.GetAllVulnerableDRSVersions(ctx)
drsVersions, err := k.GetAllObsoleteDRSVersions(ctx)
if err != nil {
panic(err)
}
genesis.VulnerableDrsVersions = drsVersions
genesis.ObsoleteDrsVersions = drsVersions

return genesis
}
8 changes: 4 additions & 4 deletions x/rollapp/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ type Keeper struct {
bankKeeper BankKeeper
transferKeeper TransferKeeper

vulnerableDRSVersions collections.KeySet[uint32]
obsoleteDRSVersions collections.KeySet[uint32]
registeredRollappDenoms collections.KeySet[collections.Pair[string, string]]

finalizePending func(ctx sdk.Context, stateInfoIndex types.StateInfoIndex) error
Expand Down Expand Up @@ -74,10 +74,10 @@ func NewKeeper(
sequencerKeeper: sequencerKeeper,
bankKeeper: bankKeeper,
transferKeeper: transferKeeper,
vulnerableDRSVersions: collections.NewKeySet(
obsoleteDRSVersions: collections.NewKeySet(
sb,
collections.NewPrefix(types.VulnerableDRSVersionsKeyPrefix),
"vulnerable_drs_versions",
collections.NewPrefix(types.ObsoleteDRSVersionsKeyPrefix),
"obsolete_drs_versions",
collections.Uint32Key,
),
registeredRollappDenoms: collections.NewKeySet[collections.Pair[string, string]](
Expand Down
52 changes: 26 additions & 26 deletions x/rollapp/keeper/msg_server_mark_vulnerable_rollapps.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,74 +13,74 @@ import (
"github.com/dymensionxyz/dymension/v3/x/rollapp/types"
)

func (k msgServer) MarkVulnerableRollapps(goCtx context.Context, msg *types.MsgMarkVulnerableRollapps) (*types.MsgMarkVulnerableRollappsResponse, error) {
func (k msgServer) MarkObsoleteRollapps(goCtx context.Context, msg *types.MsgMarkObsoleteRollapps) (*types.MsgMarkObsoleteRollappsResponse, error) {
err := msg.ValidateBasic()
if err != nil {
return nil, err
}

if msg.Authority != k.authority {
return nil, errorsmod.Wrap(gerrc.ErrInvalidArgument, "only the gov module can mark vulnerable rollapps")
return nil, errorsmod.Wrap(gerrc.ErrInvalidArgument, "only the gov module can mark obsolete rollapps")
}

ctx := sdk.UnwrapSDKContext(goCtx)

vulnerableNum, err := k.Keeper.MarkVulnerableRollapps(ctx, msg.DrsVersions)
obsoleteNum, err := k.Keeper.MarkObsoleteRollapps(ctx, msg.DrsVersions)
if err != nil {
return nil, fmt.Errorf("mark vulnerable rollapps: %w", err)
return nil, fmt.Errorf("mark obsolete rollapps: %w", err)
}

err = uevent.EmitTypedEvent(ctx, &types.EventMarkVulnerableRollapps{
VulnerableRollappNum: uint64(vulnerableNum),
DrsVersions: msg.DrsVersions,
err = uevent.EmitTypedEvent(ctx, &types.EventMarkObsoleteRollapps{
ObsoleteRollappNum: uint64(obsoleteNum),
DrsVersions: msg.DrsVersions,
})
if err != nil {
return nil, fmt.Errorf("emit event: %w", err)
}

return &types.MsgMarkVulnerableRollappsResponse{}, nil
return &types.MsgMarkObsoleteRollappsResponse{}, nil
}

func (k Keeper) MarkVulnerableRollapps(ctx sdk.Context, drsVersions []uint32) (int, error) {
vulnerableVersions := make(map[uint32]struct{})
func (k Keeper) MarkObsoleteRollapps(ctx sdk.Context, drsVersions []uint32) (int, error) {
obsoleteVersions := make(map[uint32]struct{})
for _, v := range drsVersions {
vulnerableVersions[v] = struct{}{}
// this also saves in the state the vulnerable version
err := k.SetVulnerableDRSVersion(ctx, v)
obsoleteVersions[v] = struct{}{}
// this also saves in the state the obsolete version
err := k.SetObsoleteDRSVersion(ctx, v)
if err != nil {
return 0, fmt.Errorf("set vulnerable DRS version: %w", err)
return 0, fmt.Errorf("set obsolete DRS version: %w", err)
}
}

var (
logger = k.Logger(ctx)
nonVulnerable = k.FilterRollapps(ctx, FilterNonVulnerable)
vulnerableNum int
logger = k.Logger(ctx)
nonObsolete = k.FilterRollapps(ctx, FilterNonObsolete)
obsoleteNum int
)
for _, rollapp := range nonVulnerable {
for _, rollapp := range nonObsolete {
info, found := k.GetLatestStateInfo(ctx, rollapp.RollappId)
if !found {
logger.With("rollapp_id", rollapp.RollappId).Info("no latest state info for rollapp")
continue
}

// check only last block descriptor DRS, since if that last is not vulnerable it means the rollapp already upgraded and is not vulnerable anymore
// check only last block descriptor DRS, since if that last is not obsolete it means the rollapp already upgraded and is not obsolete anymore
bd := info.BDs.BD[len(info.BDs.BD)-1]

_, vulnerable := vulnerableVersions[bd.DrsVersion]
if vulnerable {
_, obsolete := obsoleteVersions[bd.DrsVersion]
if obsolete {
// If this fails, no state change happens
err := osmoutils.ApplyFuncIfNoError(ctx, func(ctx sdk.Context) error {
return k.MarkRollappAsVulnerable(ctx, rollapp.RollappId)
return k.MarkRollappAsObsolete(ctx, rollapp.RollappId)
})
if err != nil {
// We do not want to fail if one rollapp cannot to be marked as vulnerable
// We do not want to fail if one rollapp cannot to be marked as obsolete
k.Logger(ctx).With("rollapp_id", rollapp.RollappId, "drs_version", bd.DrsVersion, "error", err.Error()).
Error("Failed to mark rollapp as vulnerable")
Error("Failed to mark rollapp as obsolete")
}
vulnerableNum++
obsoleteNum++
}
}

return vulnerableNum, nil
return obsoleteNum, nil
}
94 changes: 47 additions & 47 deletions x/rollapp/keeper/msg_server_mark_vulnerable_rollapps_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,19 @@ import (
"github.com/dymensionxyz/dymension/v3/x/rollapp/types"
)

func (s *RollappTestSuite) TestMarkVulnerableRollapps() {
func (s *RollappTestSuite) TestMarkObsoleteRollapps() {
type rollapp struct {
name string
drsVersion uint32
}
govModule := authtypes.NewModuleAddress(govtypes.ModuleName).String()

tests := []struct {
name string
authority string
rollapps []rollapp
vulnVersions []uint32
expError error
name string
authority string
rollapps []rollapp
obsoleteVersions []uint32
expError error
}{
{
name: "happy path 1",
Expand All @@ -39,7 +39,7 @@ func (s *RollappTestSuite) TestMarkVulnerableRollapps() {
{name: "rollappe_5-1", drsVersion: 1},
{name: "rollappf_6-1", drsVersion: 2},
},
vulnVersions: []uint32{
obsoleteVersions: []uint32{
1,
2,
},
Expand All @@ -52,7 +52,7 @@ func (s *RollappTestSuite) TestMarkVulnerableRollapps() {
{name: "rollappa_1-1", drsVersion: 2},
{name: "rollappd_2-1", drsVersion: 1},
},
vulnVersions: []uint32{
obsoleteVersions: []uint32{
1,
},
expError: nil,
Expand All @@ -68,14 +68,14 @@ func (s *RollappTestSuite) TestMarkVulnerableRollapps() {
{name: "rollappe_5-1", drsVersion: 0},
{name: "rollappf_6-1", drsVersion: 2},
},
vulnVersions: []uint32{
obsoleteVersions: []uint32{
1,
2,
},
expError: nil,
},
{
name: "empty DRS version is also vulnerable",
name: "empty DRS version is also obsolete",
authority: govModule,
rollapps: []rollapp{
{name: "rollappa_1-1", drsVersion: 3},
Expand All @@ -85,7 +85,7 @@ func (s *RollappTestSuite) TestMarkVulnerableRollapps() {
{name: "rollappe_5-1", drsVersion: 0},
{name: "rollappf_6-1", drsVersion: 1},
},
vulnVersions: []uint32{
obsoleteVersions: []uint32{
0,
1,
2,
Expand All @@ -99,7 +99,7 @@ func (s *RollappTestSuite) TestMarkVulnerableRollapps() {
{name: "rollappa_1-1", drsVersion: 2},
{name: "rollappe_2-1", drsVersion: 1},
},
vulnVersions: []uint32{
obsoleteVersions: []uint32{
1,
},
expError: gerrc.ErrInvalidArgument,
Expand All @@ -111,11 +111,11 @@ func (s *RollappTestSuite) TestMarkVulnerableRollapps() {
s.SetupTest()

// prepare test data
vulnVersions := uslice.ToKeySet(tc.vulnVersions)
// list of expected vulnerable rollapps
expectedVulnRollappIDs := make([]string, 0, len(tc.vulnVersions))
// list of expected non-vulnerable rollapps
expectedNonVulnRollappIDs := make([]string, 0, len(tc.vulnVersions))
obsoleteVersions := uslice.ToKeySet(tc.obsoleteVersions)
// list of expected obsolete rollapps
expectedObsoleteRollappIDs := make([]string, 0, len(tc.obsoleteVersions))
// list of expected non-obsolete rollapps
expectedNonObsoleteRollappIDs := make([]string, 0, len(tc.obsoleteVersions))
// create rollapps for every rollapp record from the test case
for _, ra := range tc.rollapps {
// create a rollapp
Expand All @@ -128,17 +128,17 @@ func (s *RollappTestSuite) TestMarkVulnerableRollapps() {
s.Require().Nil(err)

// fill lists with expectations
if _, vuln := vulnVersions[ra.drsVersion]; vuln {
expectedVulnRollappIDs = append(expectedVulnRollappIDs, ra.name)
if _, obsolete := obsoleteVersions[ra.drsVersion]; obsolete {
expectedObsoleteRollappIDs = append(expectedObsoleteRollappIDs, ra.name)
} else {
expectedNonVulnRollappIDs = append(expectedNonVulnRollappIDs, ra.name)
expectedNonObsoleteRollappIDs = append(expectedNonObsoleteRollappIDs, ra.name)
}
}

// trigger the message we want to test
_, err := s.msgServer.MarkVulnerableRollapps(s.Ctx, &types.MsgMarkVulnerableRollapps{
_, err := s.msgServer.MarkObsoleteRollapps(s.Ctx, &types.MsgMarkObsoleteRollapps{
Authority: tc.authority,
DrsVersions: tc.vulnVersions,
DrsVersions: tc.obsoleteVersions,
})

// validate results
Expand All @@ -148,49 +148,49 @@ func (s *RollappTestSuite) TestMarkVulnerableRollapps() {
s.ErrorContains(err, tc.expError.Error())

// check the event is not emitted
eventName := proto.MessageName(new(types.EventMarkVulnerableRollapps))
eventName := proto.MessageName(new(types.EventMarkObsoleteRollapps))
s.AssertEventEmitted(s.Ctx, eventName, 0)

// check non-vulnerable rollapps: all rollapps are still non-vulnerable
nonVulnRa := s.App.RollappKeeper.FilterRollapps(s.Ctx, keeper.FilterNonVulnerable)
actualNonVulnRollappIDs := uslice.Map(nonVulnRa, func(r types.Rollapp) string { return r.RollappId })
allRollapps := slices.Concat(expectedVulnRollappIDs, expectedNonVulnRollappIDs)
s.ElementsMatch(allRollapps, actualNonVulnRollappIDs)
// check non-obsolete rollapps: all rollapps are still non-obsolete
nonObsoleteRa := s.App.RollappKeeper.FilterRollapps(s.Ctx, keeper.FilterNonObsolete)
actualNonObsoleteRollappIDs := uslice.Map(nonObsoleteRa, func(r types.Rollapp) string { return r.RollappId })
allRollapps := slices.Concat(expectedObsoleteRollappIDs, expectedNonObsoleteRollappIDs)
s.ElementsMatch(allRollapps, actualNonObsoleteRollappIDs)

// check vulnerable rollapps: no vulnerable rollapps
vulnRa := s.App.RollappKeeper.FilterRollapps(s.Ctx, FilterVulnerable)
s.Empty(vulnRa)
// check obsolete rollapps: no obsolete rollapps
obsoleteRa := s.App.RollappKeeper.FilterRollapps(s.Ctx, FilterObsolete)
s.Empty(obsoleteRa)

// check the vulnerable version set is empty
actualVulnVersions, err := s.App.RollappKeeper.GetAllVulnerableDRSVersions(s.Ctx)
// check the obsolete version set is empty
actualObsoleteVersions, err := s.App.RollappKeeper.GetAllObsoleteDRSVersions(s.Ctx)
s.Require().NoError(err)
s.Require().Empty(actualVulnVersions)
s.Require().Empty(actualObsoleteVersions)
} else {
s.NoError(err)

// check the event is emitted
eventName := proto.MessageName(new(types.EventMarkVulnerableRollapps))
eventName := proto.MessageName(new(types.EventMarkObsoleteRollapps))
s.AssertEventEmitted(s.Ctx, eventName, 1)

// check non-vulnerable rollapps
nonVulnRa := s.App.RollappKeeper.FilterRollapps(s.Ctx, keeper.FilterNonVulnerable)
actualNonVulnRollappIDs := uslice.Map(nonVulnRa, func(r types.Rollapp) string { return r.RollappId })
s.ElementsMatch(expectedNonVulnRollappIDs, actualNonVulnRollappIDs)
// check non-obsolete rollapps
nonObsoleteRa := s.App.RollappKeeper.FilterRollapps(s.Ctx, keeper.FilterNonObsolete)
actualNonObsoleteRollappIDs := uslice.Map(nonObsoleteRa, func(r types.Rollapp) string { return r.RollappId })
s.ElementsMatch(expectedNonObsoleteRollappIDs, actualNonObsoleteRollappIDs)

// check vulnerable rollapps
vulnRa := s.App.RollappKeeper.FilterRollapps(s.Ctx, FilterVulnerable)
actualVulnRollappIDs := uslice.Map(vulnRa, func(r types.Rollapp) string { return r.RollappId })
s.ElementsMatch(expectedVulnRollappIDs, actualVulnRollappIDs)
// check obsolete rollapps
obsoleteRa := s.App.RollappKeeper.FilterRollapps(s.Ctx, FilterObsolete)
actualObsoleteRollappIDs := uslice.Map(obsoleteRa, func(r types.Rollapp) string { return r.RollappId })
s.ElementsMatch(expectedObsoleteRollappIDs, actualObsoleteRollappIDs)

// check the vulnerable version set
actualVulnVersions, err := s.App.RollappKeeper.GetAllVulnerableDRSVersions(s.Ctx)
// check the obsolete version set
actualObsoleteVersions, err := s.App.RollappKeeper.GetAllObsoleteDRSVersions(s.Ctx)
s.Require().NoError(err)
s.Require().ElementsMatch(tc.vulnVersions, actualVulnVersions)
s.Require().ElementsMatch(tc.obsoleteVersions, actualObsoleteVersions)
}
})
}
}

func FilterVulnerable(b types.Rollapp) bool {
func FilterObsolete(b types.Rollapp) bool {
return b.Frozen
}
Loading

0 comments on commit 23ad1e8

Please sign in to comment.