Skip to content

Commit

Permalink
fix: unfinalized block caching
Browse files Browse the repository at this point in the history
  • Loading branch information
alrxy committed Oct 21, 2024
1 parent 2794111 commit 615b6e0
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 2 deletions.
14 changes: 13 additions & 1 deletion x/symStaking/abci/proposal.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ func (h *ProposalHandler) PreBlocker() sdk.PreBlocker {

block, err := h.keeper.GetBlockByHash(ctx, blockHash)
if errors.Is(err, ethereum.NotFound) {
h.logger.Warn("Preblock: Block not found")
h.logger.Error("Preblock: Block not found", "hash", blockHash)
err := h.keeper.CacheBlockHash(ctx, skipBlockHash)
return err
}
Expand All @@ -87,6 +87,18 @@ func (h *ProposalHandler) PreBlocker() sdk.PreBlocker {
os.Exit(0) // panic recovers
}

block, err = h.keeper.GetBlockByNumber(ctx, block.Number())
if err != nil {
h.logger.Error("PreBlocker error get block by hash error", "err", err)
os.Exit(0) // panic recovers
}
// very specific error caused by finalized check bug, ideally this check shouldn't exist
if block.Hash().String() != blockHash {
h.logger.Error("Preblock: Block is not finalized", "hash", blockHash)
err := h.keeper.CacheBlockHash(ctx, skipBlockHash)
return err
}

if block.Time() < h.prevBlockTime || int64(block.Time()) >= ctx.HeaderInfo().Time.Unix() || block.Time() < h.keeper.GetMinBlockTimestamp(ctx) {
err := h.keeper.CacheBlockHash(ctx, skipBlockHash)
return err
Expand Down
32 changes: 31 additions & 1 deletion x/symStaking/keeper/symbiotic_state_change.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ import (

// Struct to unmarshal the response from the Beacon Chain API
type Block struct {
Data struct {
Finalized bool `json:"finalized"`
Data struct {
Message struct {
Body struct {
ExecutionPayload struct {
Expand Down Expand Up @@ -226,6 +227,10 @@ func (k *Keeper) GetFinalizedBlockHash(ctx context.Context) (string, error) {
return "", err
}

if !block.Finalized {
return INVALID_BLOCKHASH, nil
}

return block.Data.Message.Body.ExecutionPayload.BlockHash, nil
}

Expand Down Expand Up @@ -254,6 +259,31 @@ func (k *Keeper) GetBlockByHash(ctx context.Context, blockHash string) (*types.B
return block, nil
}

func (k *Keeper) GetBlockByNumber(ctx context.Context, number *big.Int) (*types.Block, error) {
var block *types.Block
client, err := ethclient.Dial(k.apiUrls.GetEthApiUrl())
if err != nil {
return nil, err
}
defer client.Close()

for i := 0; i < RETRIES; i++ {
block, err = client.BlockByNumber(ctx, number)
if err == nil {
break
}

k.apiUrls.RotateEthUrl()
time.Sleep(time.Millisecond * SLEEP_ON_RETRY)
}

if err != nil {
return nil, err
}

return block, nil
}

func (k Keeper) GetMinBlockTimestamp(ctx context.Context) uint64 {
return uint64(k.getSlot(ctx)-SLOTS_IN_EPOCH)*12 + BEACON_GENESIS_TIMESTAMP
}
Expand Down

0 comments on commit 615b6e0

Please sign in to comment.