Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix gas cost of insert header #309

Merged
merged 2 commits into from
Feb 20, 2023
Merged

Conversation

KonradStaniec
Copy link
Collaborator

Currently most costly babylon msg is Insert Header msg from btc light client. The difference is in order of magnitude range. The main reason is that btclightclient exposes hooks for header events to which btccheckpoint subscribes. With each header update it check status of every checkpoint of every non finalized epoch. This mean that gas cost of this operation is around O(numberOfNonFinlizedEpochs * numberOfCheckpointsInEachNonFinalizedEpoch) (+ some some cost of light client itself).

This pr proposes to move all checking logic into EndBlock callback and to use local transient store to check if btc light client have been updated during current block execution.

This:

  • reduces gas cost of inserting btc header to only the gas costs of btc light client itself (operations on transient store are super cheap)
  • reduces gas cost of transactions which have multiple Insert Header messages i.e if one inserts 50 btc headers at once, ultimately what matters is the state at the end of Babylon block.

Gas cost of inserting headers in e2e tests have been reduced almost two times.

Copy link
Member

@vitsalis vitsalis left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's a very clean fix, thanks!

@@ -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.GetBtcLigtClientUpdatedKey(), []byte{1})
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
store.Set(types.GetBtcLigtClientUpdatedKey(), []byte{1})
store.Set(types.GetBtcLightClientUpdatedKey(), []byte{1})

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

LatestFinalizedEpochKey = "latestFinalizedEpoch"

btcLighClientUpdated = "btcLightClientUpdated"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
btcLighClientUpdated = "btcLightClientUpdated"
btcLightClientUpdated = "btcLightClientUpdated"

Copy link
Member

@SebastianElvis SebastianElvis left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a pretty good use case for transient store!

@KonradStaniec KonradStaniec merged commit 093ebe5 into dev Feb 20, 2023
@KonradStaniec KonradStaniec deleted the fix-insert-header-gas-price branch February 20, 2023 07:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CONSENSUS BREAKING Breaks consensus
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants