diff --git a/x/symStaking/abci/proposal.go b/x/symStaking/abci/proposal.go index 06be9aeb8..7323c55c6 100644 --- a/x/symStaking/abci/proposal.go +++ b/x/symStaking/abci/proposal.go @@ -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 } @@ -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 diff --git a/x/symStaking/keeper/symbiotic_state_change.go b/x/symStaking/keeper/symbiotic_state_change.go index 14764b100..358bd131b 100644 --- a/x/symStaking/keeper/symbiotic_state_change.go +++ b/x/symStaking/keeper/symbiotic_state_change.go @@ -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 { @@ -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 } @@ -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 }