Skip to content
This repository has been archived by the owner on Oct 18, 2023. It is now read-only.

Commit

Permalink
better ancestor check (ledgerwatch#4617)
Browse files Browse the repository at this point in the history
  • Loading branch information
Giulio2002 committed Jul 3, 2022
1 parent 3fc51f5 commit c422b8c
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 2 deletions.
22 changes: 20 additions & 2 deletions cmd/rpcdaemon/commands/engine_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/ledgerwatch/erigon-lib/kv"
"github.com/ledgerwatch/erigon/common"
"github.com/ledgerwatch/erigon/common/hexutil"
"github.com/ledgerwatch/erigon/core/rawdb"
"github.com/ledgerwatch/erigon/core/types"
"github.com/ledgerwatch/erigon/turbo/rpchelper"
"github.com/ledgerwatch/log/v3"
Expand Down Expand Up @@ -127,6 +128,13 @@ func (e *EngineImpl) ForkchoiceUpdatedV1(ctx context.Context, forkChoiceState *F
func (e *EngineImpl) NewPayloadV1(ctx context.Context, payload *ExecutionPayload) (map[string]interface{}, error) {
log.Trace("Received NewPayload", "height", uint64(payload.BlockNumber), "hash", payload.BlockHash)

tx, err := e.db.BeginRo(ctx)
if err != nil {
return nil, err
}

defer tx.Rollback()

var baseFee *uint256.Int
if payload.BaseFeePerGas != nil {
var overflow bool
Expand Down Expand Up @@ -162,8 +170,18 @@ func (e *EngineImpl) NewPayloadV1(ctx context.Context, payload *ExecutionPayload
log.Warn("NewPayload", "err", err)
return nil, err
}

return convertPayloadStatus(res), nil
payloadStatus := convertPayloadStatus(res)
if payloadStatus["latestValidHash"] != nil {
latestValidHash := payloadStatus["latestValidHash"].(common.Hash)
isValidHashPos, err := rawdb.IsPosBlock(tx, latestValidHash)
if err != nil {
return nil, err
}
if !isValidHashPos {
payloadStatus["latestValidHash"] = common.Hash{}
}
}
return payloadStatus, nil
}

func (e *EngineImpl) GetPayloadV1(ctx context.Context, payloadID hexutil.Bytes) (*ExecutionPayload, error) {
Expand Down
10 changes: 10 additions & 0 deletions core/rawdb/accessors_chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -1603,3 +1603,13 @@ func Transitioned(db kv.Getter, blockNum uint64, terminalTotalDifficulty *big.In

return headerTd.Cmp(terminalTotalDifficulty) >= 0, nil
}

// Transitioned returns true if the block number comes after POS transition or is the last POW block
func IsPosBlock(db kv.Getter, blockHash common.Hash) (trans bool, err error) {
header, err := ReadHeaderByHash(db, blockHash)
if err != nil {
return false, err
}

return header.Difficulty.Cmp(common.Big0) == 0, nil
}

0 comments on commit c422b8c

Please sign in to comment.