Skip to content

Commit

Permalink
fix gas cost of insert header (#309)
Browse files Browse the repository at this point in the history
* fix gas cost of insert header
  • Loading branch information
KonradStaniec authored Feb 20, 2023
1 parent c92e948 commit 093ebe5
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 7 deletions.
4 changes: 3 additions & 1 deletion app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,8 @@ func NewBabylonApp(
ibctransfertypes.StoreKey,
zctypes.StoreKey,
)
tkeys := sdk.NewTransientStoreKeys(paramstypes.TStoreKey)
tkeys := sdk.NewTransientStoreKeys(
paramstypes.TStoreKey, btccheckpointtypes.TStoreKey)
// NOTE: The testingkey is just mounted for testing purposes. Actual applications should
// not include this key.
memKeys := sdk.NewMemoryStoreKeys(capabilitytypes.MemStoreKey, "testingkey")
Expand Down Expand Up @@ -518,6 +519,7 @@ func NewBabylonApp(
btccheckpointkeeper.NewKeeper(
appCodec,
keys[btccheckpointtypes.StoreKey],
tkeys[btccheckpointtypes.TStoreKey],
keys[btccheckpointtypes.MemStoreKey],
app.GetSubspace(btccheckpointtypes.ModuleName),
&btclightclientKeeper,
Expand Down
2 changes: 2 additions & 0 deletions testutil/keeper/btccheckpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ func NewBTCCheckpointKeeper(
ek btcctypes.CheckpointingKeeper,
powLimit *big.Int) (*keeper.Keeper, sdk.Context) {
storeKey := sdk.NewKVStoreKey(btcctypes.StoreKey)
tstoreKey := sdk.NewTransientStoreKey(btcctypes.TStoreKey)
memStoreKey := storetypes.NewMemoryStoreKey(btcctypes.MemStoreKey)

db := tmdb.NewMemDB()
Expand All @@ -47,6 +48,7 @@ func NewBTCCheckpointKeeper(
k := keeper.NewKeeper(
cdc,
storeKey,
tstoreKey,
memStoreKey,
paramsSubspace,
lk,
Expand Down
16 changes: 16 additions & 0 deletions x/btccheckpoint/abci.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package btccheckpoint

import (
"github.com/babylonchain/babylon/x/btccheckpoint/keeper"
sdk "github.com/cosmos/cosmos-sdk/types"
abci "github.com/tendermint/tendermint/abci/types"
)

// EndBlocker checks if during block execution btc light client head had been
// updated. If the head had been updated, status of all available checkpoints
// is checked to determine if any of them became confirmed/finalized/abandonded.
func EndBlocker(ctx sdk.Context, k keeper.Keeper, req abci.RequestEndBlock) {
if k.BtcLightClientUpdated(ctx) {
k.OnTipChange(ctx)
}
}
4 changes: 2 additions & 2 deletions x/btccheckpoint/keeper/hooks.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@ var _ HandledHooks = Hooks{}
func (k Keeper) Hooks() Hooks { return Hooks{k} }

func (h Hooks) AfterBTCRollBack(ctx sdk.Context, headerInfo *ltypes.BTCHeaderInfo) {
h.k.OnTipChange(ctx)
h.k.setBtcLightClientUpdated(ctx)
}

func (h Hooks) AfterBTCRollForward(ctx sdk.Context, headerInfo *ltypes.BTCHeaderInfo) {
h.k.OnTipChange(ctx)
h.k.setBtcLightClientUpdated(ctx)
}

func (h Hooks) AfterBTCHeaderInserted(ctx sdk.Context, headerInfo *ltypes.BTCHeaderInfo) {}
Expand Down
18 changes: 18 additions & 0 deletions x/btccheckpoint/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ type (
Keeper struct {
cdc codec.BinaryCodec
storeKey storetypes.StoreKey
tstoreKey storetypes.StoreKey
memKey storetypes.StoreKey
paramstore paramtypes.Subspace
btcLightClientKeeper types.BTCLightClientKeeper
Expand Down Expand Up @@ -60,6 +61,7 @@ const (
func NewKeeper(
cdc codec.BinaryCodec,
storeKey,
tstoreKey,
memKey storetypes.StoreKey,
ps paramtypes.Subspace,
bk types.BTCLightClientKeeper,
Expand All @@ -75,6 +77,7 @@ func NewKeeper(
return Keeper{
cdc: cdc,
storeKey: storeKey,
tstoreKey: tstoreKey,
memKey: memKey,
paramstore: ps,
btcLightClientKeeper: bk,
Expand Down Expand Up @@ -347,6 +350,21 @@ func (k Keeper) OnTipChange(ctx sdk.Context) {
k.checkCheckpoints(ctx)
}

func (k Keeper) setBtcLightClientUpdated(ctx sdk.Context) {
store := ctx.TransientStore(k.tstoreKey)
store.Set(types.GetBtcLightClientUpdatedKey(), []byte{1})
}

// BtcLightClientUpdated checks if btc light client was updated during block execution
func (k Keeper) BtcLightClientUpdated(ctx sdk.Context) bool {
// transient store is cleared after each block execution, therfore if
// BtcLightClientKey is set, it means setBtcLightClientUpdated was called during
// current block execution
store := ctx.TransientStore(k.tstoreKey)
lcUpdated := store.Get(types.GetBtcLightClientUpdatedKey())
return len(lcUpdated) > 0
}

func (k Keeper) getLastFinalizedEpochNumber(ctx sdk.Context) uint64 {
store := ctx.KVStore(k.storeKey)
epoch := store.Get(types.GetLatestFinalizedEpochKey())
Expand Down
3 changes: 2 additions & 1 deletion x/btccheckpoint/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,7 @@ func (am AppModule) BeginBlock(_ sdk.Context, _ abci.RequestBeginBlock) {}

// EndBlock executes all ABCI EndBlock logic respective to the capability module. It
// returns no validator updates.
func (am AppModule) EndBlock(_ sdk.Context, _ abci.RequestEndBlock) []abci.ValidatorUpdate {
func (am AppModule) EndBlock(ctx sdk.Context, req abci.RequestEndBlock) []abci.ValidatorUpdate {
EndBlocker(ctx, am.keeper, req)
return []abci.ValidatorUpdate{}
}
15 changes: 12 additions & 3 deletions x/btccheckpoint/types/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,21 @@ const (
// QuerierRoute defines the module's query routing key
QuerierRoute = ModuleName

TStoreKey = "transient_btccheckpoint"

// MemStoreKey defines the in-memory store key
MemStoreKey = "mem_btccheckpoint"

LatestFinalizedEpochKey = "latestFinalizedEpoch"

btcLightClientUpdated = "btcLightClientUpdated"
)

var (
SubmisionKeyPrefix = []byte{3}
EpochDataPrefix = []byte{4}
LastFinalizedEpochKey = append([]byte{5}, []byte(LatestFinalizedEpochKey)...)
SubmisionKeyPrefix = []byte{3}
EpochDataPrefix = []byte{4}
LastFinalizedEpochKey = append([]byte{5}, []byte(LatestFinalizedEpochKey)...)
BtcLightClientUpdatedKey = append([]byte{6}, []byte(btcLightClientUpdated)...)
)

func KeyPrefix(p string) []byte {
Expand All @@ -45,3 +50,7 @@ func GetEpochIndexKey(e uint64) []byte {
func GetLatestFinalizedEpochKey() []byte {
return LastFinalizedEpochKey
}

func GetBtcLightClientUpdatedKey() []byte {
return BtcLightClientUpdatedKey
}

0 comments on commit 093ebe5

Please sign in to comment.