Skip to content

Commit

Permalink
fix(rollapp): revert state update pruning feature (#1312)
Browse files Browse the repository at this point in the history
  • Loading branch information
zale144 authored Oct 14, 2024
1 parent 03ee4bd commit 2cd612a
Show file tree
Hide file tree
Showing 18 changed files with 71 additions and 484 deletions.
1 change: 0 additions & 1 deletion app/keepers/keepers.go
Original file line number Diff line number Diff line change
Expand Up @@ -587,7 +587,6 @@ func (a *AppKeepers) SetupHooks() {
a.IncentivesKeeper.Hooks(),
a.TxFeesKeeper.Hooks(),
a.DelayedAckKeeper.GetEpochHooks(),
a.RollappKeeper.GetEpochHooks(),
),
)

Expand Down
3 changes: 0 additions & 3 deletions proto/dymensionxyz/dymension/rollapp/params.proto
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,4 @@ message Params {
(gogoproto.nullable) = false,
(gogoproto.moretags) = "yaml:\"app_registration_fee\""
];
// state_info_deletion_epoch_identifier is used to control the interval at which the state info records will be deleted.
string state_info_deletion_epoch_identifier = 8
[ (gogoproto.moretags) = "yaml:\"state_info_deletion_epoch_identifier\"" ];
}
8 changes: 1 addition & 7 deletions proto/dymensionxyz/dymension/rollapp/state_info.proto
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,8 @@ message StateInfo {
// BDs is a list of block description objects (one per block)
// the list must be ordered by height, starting from startHeight to startHeight+numBlocks-1
BlockDescriptors BDs = 9 [(gogoproto.nullable) = false];
// created_at is the timestamp at which the StateInfo was created
google.protobuf.Timestamp created_at = 10 [
(gogoproto.stdtime) = true,
(gogoproto.nullable) = false,
(gogoproto.moretags) = "yaml:\"created_at\""
];
// DrsVersion is a DRS version used by the rollapp.
string drs_version = 11;
string drs_version = 10;
}

// StateInfoSummary is a compact representation of StateInfo
Expand Down
38 changes: 0 additions & 38 deletions x/rollapp/keeper/hooks.go

This file was deleted.

5 changes: 0 additions & 5 deletions x/rollapp/keeper/liveness_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"fmt"
"slices"
"testing"
"time"

sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/dymensionxyz/sdk-utils/utils/urand"
Expand Down Expand Up @@ -191,10 +190,6 @@ type livenessMockSequencerKeeper struct {
jails map[string]int
}

func (l livenessMockSequencerKeeper) UnbondingTime(sdk.Context) (res time.Duration) {
return time.Minute
}

func newLivenessMockSequencerKeeper() livenessMockSequencerKeeper {
return livenessMockSequencerKeeper{
make(map[string]int),
Expand Down
2 changes: 0 additions & 2 deletions x/rollapp/keeper/msg_server_update_state.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,6 @@ func (k msgServer) UpdateState(goCtx context.Context, msg *types.MsgUpdateState)
})

creationHeight := uint64(ctx.BlockHeight())
blockTime := ctx.BlockTime()
stateInfo := types.NewStateInfo(
msg.RollappId,
newIndex,
Expand All @@ -106,7 +105,6 @@ func (k msgServer) UpdateState(goCtx context.Context, msg *types.MsgUpdateState)
msg.DAPath,
creationHeight,
msg.BDs,
blockTime,
msg.DrsVersion,
)
// Write new state information to the store indexed by <RollappId,LatestStateInfoIndex>
Expand Down
6 changes: 0 additions & 6 deletions x/rollapp/keeper/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ func (k Keeper) GetParams(ctx sdk.Context) types.Params {
k.LivenessSlashInterval(ctx),
k.LivenessJailBlocks(ctx),
k.AppRegistrationFee(ctx),
k.StateInfoDeletionEpochIdentifier(ctx),
)
}

Expand Down Expand Up @@ -49,8 +48,3 @@ func (k Keeper) AppRegistrationFee(ctx sdk.Context) (res sdk.Coin) {
k.paramstore.Get(ctx, types.KeyAppRegistrationFee, &res)
return
}

func (k Keeper) StateInfoDeletionEpochIdentifier(ctx sdk.Context) (res string) {
k.paramstore.Get(ctx, types.KeyStateInfoDeletionEpochIdentifier, &res)
return
}
57 changes: 0 additions & 57 deletions x/rollapp/keeper/state_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package keeper

import (
"fmt"
"time"

"github.com/cosmos/cosmos-sdk/store/prefix"
sdk "github.com/cosmos/cosmos-sdk/types"
Expand All @@ -17,12 +16,6 @@ func (k Keeper) SetStateInfo(ctx sdk.Context, stateInfo types.StateInfo) {
store.Set(types.StateInfoKey(
stateInfo.StateInfoIndex,
), b)

// store a key prefixed with the creation timestamp
storeTS := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefix(types.TimestampedStateInfoKeyPrefix))
storeTS.Set(types.StateInfoTimestampKey(
stateInfo,
), []byte{})
}

// GetStateInfo returns a stateInfo from its index
Expand Down Expand Up @@ -93,53 +86,3 @@ func (k Keeper) GetAllStateInfo(ctx sdk.Context) (list []types.StateInfo) {

return
}

// DeleteStateInfoUntilTimestamp deletes all stateInfo until the given timestamp
func (k Keeper) DeleteStateInfoUntilTimestamp(ctx sdk.Context, endTimestampExcl time.Time) {
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefix(types.StateInfoKeyPrefix))
storeTS := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefix(types.TimestampedStateInfoKeyPrefix))

// Note that for the active sequencer, the latest state info index will not be within the range of
// the state updates to be deleted, as it will be more recent.
// For a sequencer that is inactive for 21 days or more, it will be within range, furthermore
// the latest state info index and the latest finalized state info index will be the same.
latestIndexes := k.GetAllLatestStateInfoIndex(ctx)
skipStateInfoIndexes := make(map[string]uint64, len(latestIndexes))
for _, index := range latestIndexes {
skipStateInfoIndexes[index.RollappId] = index.Index
}

k.IterateStateInfoWithTimestamp(storeTS, endTimestampExcl.UnixMicro(), func(keyTS []byte) bool {
key := types.StateInfoIndexKeyFromTimestampKey(keyTS)
// skip latest stateInfo and latest finalized stateInfo
stateInfoIndex := types.StateInfoIndexFromKey(key)
index := skipStateInfoIndexes[stateInfoIndex.RollappId]
if index == stateInfoIndex.Index {
return false
}

store.Delete(key)
storeTS.Delete(keyTS)
return false
})
}

// IterateStateInfoWithTimestamp iterates over stateInfo until timestamp
func (k Keeper) IterateStateInfoWithTimestamp(store prefix.Store, endTimestampUNIX int64, fn func(key []byte) (stop bool)) {
endKey := types.StateInfoTimestampKeyPrefix(endTimestampUNIX)
iterator := store.ReverseIterator(nil, endKey)

defer iterator.Close() // nolint: errcheck

for ; iterator.Valid(); iterator.Next() {
if fn(iterator.Key()) {
break
}
}
}

// HasStateInfoTimestampKey checks if the stateInfo has a timestamp key - used for testing
func (k Keeper) HasStateInfoTimestampKey(ctx sdk.Context, stateInfo types.StateInfo) bool {
storeTS := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefix(types.TimestampedStateInfoKeyPrefix))
return storeTS.Has(types.StateInfoTimestampKey(stateInfo))
}
68 changes: 0 additions & 68 deletions x/rollapp/keeper/state_info_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,8 @@ package keeper_test
import (
"strconv"
"testing"
"time"

sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

keepertest "github.com/dymensionxyz/dymension/v3/testutil/keeper"
Expand Down Expand Up @@ -81,69 +79,3 @@ func TestStateInfoGetAll(t *testing.T) {
nullify.Fill(k.GetAllStateInfo(ctx)),
)
}

func TestKeeper_DeleteStateInfoUntilTimestamp(t *testing.T) {
k, ctx := keepertest.RollappKeeper(t)

ts1 := time.Date(2020, time.May, 1, 10, 22, 0, 0, time.UTC)
ts2 := ts1.Add(9 * time.Second)
ts3 := ts2.Add(11 * time.Second)
ts4 := ts3.Add(13 * time.Second)

items := []types.StateInfo{
{CreatedAt: ts1},
{CreatedAt: ts2},
{CreatedAt: ts3},
{CreatedAt: ts4},
}
for i := range items {
items[i].StateInfoIndex.RollappId = strconv.Itoa(i + 1)
items[i].StateInfoIndex.Index = 1 + uint64(i)

k.SetStateInfo(ctx, items[i])
}

lastItem := items[len(items)-1]
latestStateInfoIndex := types.StateInfoIndex{
RollappId: lastItem.StateInfoIndex.RollappId,
Index: lastItem.StateInfoIndex.Index,
}
k.SetLatestStateInfoIndex(ctx, latestStateInfoIndex)

// delete all before ts3: only ts3 and ts4 should be found
k.DeleteStateInfoUntilTimestamp(ctx, ts2.Add(time.Second))

for _, item := range items {
_, found := k.GetStateInfo(ctx,
item.StateInfoIndex.RollappId,
item.StateInfoIndex.Index,
)

foundTSKey := k.HasStateInfoTimestampKey(ctx, item)

if item.CreatedAt.After(ts2) {
assert.True(t, found)
assert.True(t, foundTSKey)
continue
}
assert.Falsef(t, found, "item %v", item)
assert.False(t, foundTSKey)
}

// delete all: only ts4 should be found, as it's the latest and has an index
k.DeleteStateInfoUntilTimestamp(ctx, ts4.Add(time.Second))

info3 := items[2]
_, found := k.GetStateInfo(ctx,
info3.StateInfoIndex.RollappId,
info3.StateInfoIndex.Index,
)
assert.False(t, found)

info4 := items[3]
_, found = k.GetStateInfo(ctx,
info4.StateInfoIndex.RollappId,
info4.StateInfoIndex.Index,
)
assert.True(t, found)
}
1 change: 0 additions & 1 deletion x/rollapp/types/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ const (
AttributeKeyNumBlocks = "num_blocks"
AttributeKeyDAPath = "da_path"
AttributeKeyStatus = "status"
AttributeKeyCreatedAt = "created_at"

// EventTypeFraud is emitted when a fraud evidence is submitted
EventTypeFraud = "fraud_proposal"
Expand Down
3 changes: 0 additions & 3 deletions x/rollapp/types/expected_keepers.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package types

import (
"time"

sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/ibc-go/v7/modules/core/exported"
)
Expand All @@ -23,7 +21,6 @@ type ChannelKeeper interface {
type SequencerKeeper interface {
SlashLiveness(ctx sdk.Context, rollappID string) error
JailLiveness(ctx sdk.Context, rollappID string) error
UnbondingTime(ctx sdk.Context) (res time.Duration)
}

// BankKeeper defines the expected interface needed to retrieve account balances.
Expand Down
21 changes: 1 addition & 20 deletions x/rollapp/types/key_state_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@ var _ binary.ByteOrder

const (
// StateInfoKeyPrefix is the prefix to retrieve all StateInfo
StateInfoKeyPrefix = "StateInfo/value/"
StateInfoIndexKeyPartLength = 8 + 1 + 1 // BigEndian + "/" + "/"
StateInfoKeyPrefix = "StateInfo/value/"
)

// StateInfoKey returns the store key to retrieve a StateInfo from the index fields
Expand All @@ -30,21 +29,3 @@ func StateInfoKey(

return key
}

// StateInfoIndexFromKey returns the StateInfoIndex from a store key.
// The value of StateInfoIndexKeyPartLength will always be shorter than the key itself,
// because the key contains the rollappId and the BigEndian representation of the index,
// which is always 8 bytes long.
func StateInfoIndexFromKey(key []byte) StateInfoIndex {
l := len(key)
rollappId := string(key[:l-StateInfoIndexKeyPartLength])
return StateInfoIndex{
RollappId: rollappId,
Index: sdk.BigEndianToUint64(key[len(rollappId)+1 : l-1]),
}
}

// StateInfoIndexKeyFromTimestampKey returns the StateInfoIndex key from a timestamp key by removing the timestamp prefix.
func StateInfoIndexKeyFromTimestampKey(keyTS []byte) []byte {
return keyTS[TimestampPrefixLen:] // remove the timestamp prefix
}
42 changes: 0 additions & 42 deletions x/rollapp/types/key_state_info_test.go

This file was deleted.

Loading

0 comments on commit 2cd612a

Please sign in to comment.