Skip to content

Commit

Permalink
Merge branch 'main' of https://github.com/initia-labs/OPinit into fea…
Browse files Browse the repository at this point in the history
…t/sdk
  • Loading branch information
JSHan94 committed Nov 29, 2023
2 parents eba3c54 + 1cf667f commit 89c41da
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 186 deletions.
69 changes: 0 additions & 69 deletions x/opchild/keeper/abci_listener.go

This file was deleted.

60 changes: 13 additions & 47 deletions x/opchild/keeper/historical_info.go
Original file line number Diff line number Diff line change
@@ -1,59 +1,44 @@
package keeper

import (
"encoding/binary"

"cosmossdk.io/math"
"github.com/cosmos/cosmos-sdk/store/prefix"
sdk "github.com/cosmos/cosmos-sdk/types"
cosmostypes "github.com/cosmos/cosmos-sdk/x/staking/types"

"github.com/initia-labs/OPinit/x/opchild/types"
)

// GetHistoricalInfo fetch height historical info that is equal or lower than the given height.
// GetHistoricalInfo gets the historical info at a given height
func (k Keeper) GetHistoricalInfo(ctx sdk.Context, height int64) (cosmostypes.HistoricalInfo, bool) {
store := ctx.KVStore(k.storeKey)
key := cosmostypes.GetHistoricalInfoKey(height)

// increase height by 1 because iterator is exclusive.
height += 1

prefixStore := prefix.NewStore(store, types.HistoricalInfoKey)

end := make([]byte, 8)
binary.BigEndian.PutUint64(end, uint64(height))

iterator := prefixStore.ReverseIterator(nil, end)
defer iterator.Close()

if !iterator.Valid() {
value := store.Get(key)
if value == nil {
return cosmostypes.HistoricalInfo{}, false
}

value := iterator.Value()
return cosmostypes.MustUnmarshalHistoricalInfo(k.cdc, value), true
}

// SetHistoricalInfo sets the historical info at a given height
func (k Keeper) SetHistoricalInfo(ctx sdk.Context, height int64, hi *cosmostypes.HistoricalInfo) {
store := ctx.KVStore(k.storeKey)
key := types.GetHistoricalInfoKey(uint64(height))
key := cosmostypes.GetHistoricalInfoKey(height)
value := k.cdc.MustMarshal(hi)
store.Set(key, value)
}

// DeleteHistoricalInfo deletes the historical info at a given height
func (k Keeper) DeleteHistoricalInfo(ctx sdk.Context, height int64) {
store := ctx.KVStore(k.storeKey)
key := types.GetHistoricalInfoKey(uint64(height))
key := cosmostypes.GetHistoricalInfoKey(height)

store.Delete(key)
}

// TrackHistoricalInfo saves the latest historical-info and deletes the oldest
// heights that are below pruning height
func (k Keeper) TrackHistoricalInfo(ctx sdk.Context) {
entryNum := uint64(k.HistoricalEntries(ctx))
entryNum := k.HistoricalEntries(ctx)

// Prune store to ensure we only have parameter-defined historical entries.
// In most cases, this will involve removing a single historical entry.
Expand All @@ -62,31 +47,12 @@ func (k Keeper) TrackHistoricalInfo(ctx sdk.Context) {
// Since the entries to be deleted are always in a continuous range, we can iterate
// over the historical entries starting from the most recent version to be pruned
// and then return at the first empty entry.
height := uint64(ctx.BlockHeight())
if height > entryNum {
store := ctx.KVStore(k.storeKey)
prefixStore := prefix.NewStore(store, types.HistoricalInfoKey)

end := make([]byte, 8)
binary.BigEndian.PutUint64(end, height-entryNum)

iterator := prefixStore.ReverseIterator(nil, end)
defer iterator.Close()

// our historical info does not exist for every block to allow
// empty block, so it is possible when ibc request deleted block
// historical info. Then opchild module returns height historical
// historical info that is lower than the given height.
//
// Whenever we delete historical info, we have to leave first info
// for safety.
if iterator.Valid() {
iterator.Next()
}

for ; iterator.Valid(); iterator.Next() {
key := iterator.Key()
prefixStore.Delete(key)
for i := ctx.BlockHeight() - int64(entryNum); i >= 0; i-- {
_, found := k.GetHistoricalInfo(ctx, i)
if found {
k.DeleteHistoricalInfo(ctx, i)
} else {
break
}
}

Expand Down
67 changes: 19 additions & 48 deletions x/opchild/keeper/historical_info_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,65 +3,36 @@ package keeper_test
import (
"testing"

tmtypes "github.com/cometbft/cometbft/proto/tendermint/types"
cosmostypes "github.com/cosmos/cosmos-sdk/x/staking/types"
"github.com/stretchr/testify/require"

cosmostypes "github.com/cosmos/cosmos-sdk/x/staking/types"
)

func Test_HistoricalInfo(t *testing.T) {
ctx, input := createDefaultTestInput(t)

historicalInfo := cosmostypes.HistoricalInfo{
Header: tmtypes.Header{
Height: 100,
ChainID: "testnet",
},
Valset: []cosmostypes.Validator{{
OperatorAddress: "hihi",
}},
}

input.OPChildKeeper.SetHistoricalInfo(ctx, 100, &historicalInfo)

_historicalInfo, found := input.OPChildKeeper.GetHistoricalInfo(ctx, 101)
require.True(t, found)
require.Equal(t, historicalInfo.Header.Height, _historicalInfo.Header.Height)
require.Equal(t, historicalInfo.Header.ChainID, _historicalInfo.Header.ChainID)
require.Equal(t, historicalInfo.Valset[0].OperatorAddress, _historicalInfo.Valset[0].OperatorAddress)

_, found = input.OPChildKeeper.GetHistoricalInfo(ctx, 99)
require.False(t, found)
}

func Test_TrackHistoricalInfo(t *testing.T) {
ctx, input := createDefaultTestInput(t)
emptyHistoricalInfo := cosmostypes.HistoricalInfo{}
input.OPChildKeeper.SetHistoricalInfo(ctx, 100, &emptyHistoricalInfo)
input.OPChildKeeper.SetHistoricalInfo(ctx, 101, &emptyHistoricalInfo)
input.OPChildKeeper.SetHistoricalInfo(ctx, 102, &emptyHistoricalInfo)
input.OPChildKeeper.SetHistoricalInfo(ctx, 103, &emptyHistoricalInfo)

ctx = ctx.WithBlockHeight(104)
params := input.OPChildKeeper.GetParams(ctx)
params.HistoricalEntries = 1
params.HistoricalEntries = 2
input.OPChildKeeper.SetParams(ctx, params)

input.OPChildKeeper.TrackHistoricalInfo(ctx)
input.OPChildKeeper.TrackHistoricalInfo(ctx.WithBlockHeight(1))
input.OPChildKeeper.TrackHistoricalInfo(ctx.WithBlockHeight(2))
input.OPChildKeeper.TrackHistoricalInfo(ctx.WithBlockHeight(3))

_, found := input.OPChildKeeper.GetHistoricalInfo(ctx, 102)
require.True(t, found)
_, found = input.OPChildKeeper.GetHistoricalInfo(ctx, 101)
_, found := input.OPChildKeeper.GetHistoricalInfo(ctx, 1)
require.False(t, found)
_, found = input.OPChildKeeper.GetHistoricalInfo(ctx, 100)
require.False(t, found)
}

func Test_DeleteHistoricalInfo(t *testing.T) {
ctx, input := createDefaultTestInput(t)
emptyHistoricalInfo := cosmostypes.HistoricalInfo{}
input.OPChildKeeper.SetHistoricalInfo(ctx, 100, &emptyHistoricalInfo)
historicalInfo, found := input.OPChildKeeper.GetHistoricalInfo(ctx, 2)
require.True(t, found)
require.Equal(t, cosmostypes.HistoricalInfo{
Header: ctx.WithBlockHeight(2).BlockHeader(),
Valset: nil,
}, historicalInfo)

input.OPChildKeeper.DeleteHistoricalInfo(ctx, 100)
_, found := input.OPChildKeeper.GetHistoricalInfo(ctx, 100)
require.False(t, found)
historicalInfo, found = input.OPChildKeeper.GetHistoricalInfo(ctx, 3)
require.True(t, found)
require.Equal(t, cosmostypes.HistoricalInfo{
Header: ctx.WithBlockHeight(3).BlockHeader(),
Valset: nil,
}, historicalInfo)
}
31 changes: 9 additions & 22 deletions x/opchild/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@ type Keeper struct {
bridgeHook types.BridgeHook

// Msg server router
router *baseapp.MsgServiceRouter
abciListener *ABCIListener
router *baseapp.MsgServiceRouter

// Legacy Proposal router
legacyRouter govv1beta1.Router
Expand All @@ -45,22 +44,15 @@ func NewKeeper(
panic("authority is not a valid acc address")
}

abciListener := &ABCIListener{}
k := Keeper{
cdc: cdc,
storeKey: key,
authKeeper: ak,
bankKeeper: bk,
bridgeHook: bh,
router: router,
authority: authority,
abciListener: abciListener,
return Keeper{
cdc: cdc,
storeKey: key,
authKeeper: ak,
bankKeeper: bk,
bridgeHook: bh,
router: router,
authority: authority,
}

_abciListener := newABCIListener(&k)
*abciListener = _abciListener

return k
}

// GetAuthority returns the x/move module's authority.
Expand All @@ -73,11 +65,6 @@ func (k Keeper) Logger(ctx sdk.Context) log.Logger {
return ctx.Logger().With("module", "x/"+types.ModuleName)
}

// GetABCIListener return ABCIListener pointer
func (k Keeper) GetABCIListener() *ABCIListener {
return k.abciListener
}

// Router returns the gov keeper's router
func (keeper Keeper) Router() *baseapp.MsgServiceRouter {
return keeper.router
Expand Down
1 change: 1 addition & 0 deletions x/opchild/types/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,5 @@ var (
ErrDepositAlreadyFinalized = errorsmod.Register(ModuleName, 9, "deposit already finalized")
ErrInvalidAmount = errorsmod.Register(ModuleName, 10, "invalid amount")
ErrInvalidSequence = errorsmod.Register(ModuleName, 11, "invalid sequence")
ErrZeroMaxValidators = errorsmod.Register(ModuleName, 12, "max validators must be non-zero")
)
4 changes: 4 additions & 0 deletions x/opchild/types/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,5 +48,9 @@ func (p Params) Validate() error {
return err
}

if p.MaxValidators == 0 {
return ErrZeroMaxValidators
}

return nil
}

0 comments on commit 89c41da

Please sign in to comment.