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: unfinalized block caching #75

Merged
merged 2 commits into from
Oct 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions x/symStaking/abci/proposal.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,8 @@ func (h *ProposalHandler) PreBlocker() sdk.PreBlocker {
}

block, err := h.keeper.GetBlockByHash(ctx, blockHash)
if errors.Is(err, ethereum.NotFound) || block.Hash().String() != blockHash {
h.logger.Warn("Preblock: Block not found")
if errors.Is(err, ethereum.NotFound) {
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 @@ -232,6 +233,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 @@ -260,6 +265,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