Skip to content

Commit

Permalink
fix: fix some validation issues & ancient reset;
Browse files Browse the repository at this point in the history
  • Loading branch information
galaio committed Mar 21, 2024
1 parent 420147c commit a4620fe
Show file tree
Hide file tree
Showing 13 changed files with 50 additions and 21 deletions.
3 changes: 3 additions & 0 deletions cmd/geth/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ import (
"strings"
"unicode"

"github.com/ethereum/go-ethereum/eth/downloader"

"github.com/ethereum/go-ethereum/accounts"
"github.com/ethereum/go-ethereum/accounts/external"
"github.com/ethereum/go-ethereum/accounts/keystore"
Expand Down Expand Up @@ -201,6 +203,7 @@ func makeFullNode(ctx *cli.Context) (*node.Node, ethapi.Backend) {
}
if ctx.IsSet(utils.OverrideFullImmutabilityThreshold.Name) {
params.FullImmutabilityThreshold = ctx.Uint64(utils.OverrideFullImmutabilityThreshold.Name)
downloader.FullMaxForkAncestry = ctx.Uint64(utils.OverrideFullImmutabilityThreshold.Name)
}
if ctx.IsSet(utils.OverrideMinBlocksForBlobRequests.Name) {
params.MinBlocksForBlobRequests = ctx.Uint64(utils.OverrideMinBlocksForBlobRequests.Name)
Expand Down
2 changes: 1 addition & 1 deletion core/block_validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ func (v *BlockValidator) ValidateBody(block *types.Block) error {
},
func() error {
// Withdrawals are present after the Shanghai fork.
if (header.WithdrawalsHash != nil && *header.WithdrawalsHash != common.Hash{}) {
if !header.EmptyWithdrawalsHash() {
// Withdrawals list must be present in body after Shanghai.
if block.Withdrawals() == nil {
return errors.New("missing withdrawals in block body")
Expand Down
1 change: 1 addition & 0 deletions core/blockchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -1409,6 +1409,7 @@ func (bc *BlockChain) InsertReceiptChain(blockChain types.Blocks, receiptChain [
err error
)
if !bc.chainConfig.IsCancun(last.Number(), last.Time()) {
log.Info("WriteAncientBlocks", "startAt", blockChain[0].Number(), "last", last.Number())
writeSize, err = rawdb.WriteAncientBlocks(bc.db, blockChain, receiptChain, td)
} else {
writeSize, err = rawdb.WriteAncientBlocksAfterCancun(bc.db, bc.chainConfig, blockChain, receiptChain, td)
Expand Down
3 changes: 2 additions & 1 deletion core/rawdb/chain_freezer.go
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,8 @@ func (f *chainFreezer) tryPruneBlobAncient(env *ethdb.FreezerEnv, num uint64) {
}
start := time.Now()
if err = f.ResetTable(ChainFreezerBlobSidecarTable, expectTail, h, false); err != nil {
log.Error("Cannot prune blob ancient", "block", num, "expectTail", expectTail)
log.Error("Cannot prune blob ancient", "block", num, "expectTail", expectTail, "err", err)
return
}
log.Info("Chain freezer prune useless blobs, now ancient data is", "from", expectTail, "to", num, "cost", common.PrettyDuration(time.Since(start)))
}
Expand Down
24 changes: 20 additions & 4 deletions core/rawdb/freezer.go
Original file line number Diff line number Diff line change
Expand Up @@ -342,8 +342,16 @@ func (f *Freezer) TruncateHead(items uint64) (uint64, error) {
if oitems <= items {
return oitems, nil
}
for _, table := range f.tables {
if err := table.truncateHead(items - f.offset); err != nil {
for kind, table := range f.tables {
err := table.truncateHead(items - f.offset)
// if truncate below tail, try reset to it
if err == errTruncationBelowTail {
nt, err := table.resetItems(items-f.offset, items-f.offset)
if err != nil {
return 0, err
}
f.tables[kind] = nt
} else {
return 0, err
}
}
Expand Down Expand Up @@ -469,7 +477,15 @@ func (f *Freezer) repair() error {
if slices.Contains(f.additionTableKinds, kind) && EmptyTable(table) {
continue
}
if err := table.truncateHead(head); err != nil {
err := table.truncateHead(head)
// if truncate below tail, try reset to it
if err == errTruncationBelowTail {
nt, err := table.resetItems(head, head)
if err != nil {
return err
}
f.tables[kind] = nt
} else {
return err
}
if err := table.truncateTail(tail); err != nil {
Expand Down Expand Up @@ -708,7 +724,7 @@ func (f *Freezer) ResetTable(kind string, tail, head uint64, onlyEmpty bool) err
f.frozen.Add(f.offset)
f.tail.Add(f.offset)
f.writeBatch = newFreezerBatch(f)
log.Info("Reset Table", "tail", f.tail.Load(), "frozen", f.frozen.Load())
log.Info("Reset Table", "kind", kind, "tail", f.tables[kind].itemHidden.Load(), "frozen", f.tables[kind].items.Load())
return nil
}

Expand Down
4 changes: 3 additions & 1 deletion core/rawdb/freezer_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ var (

// errNotSupported is returned if the database doesn't support the required operation.
errNotSupported = errors.New("this operation is not supported")

errTruncationBelowTail = errors.New("truncation below tail")
)

// indexEntry contains the number/id of the file that the data resides in, as well as the
Expand Down Expand Up @@ -406,7 +408,7 @@ func (t *freezerTable) truncateHead(items uint64) error {
return nil
}
if items < t.itemHidden.Load() {
return fmt.Errorf("truncation below tail, name: %v, tail: %v, head: %v, truncate to: %v", t.name, t.itemHidden.Load(), t.items.Load(), items)
return errTruncationBelowTail
}
// We need to truncate, save the old size for metrics tracking
oldSize, err := t.sizeNolock()
Expand Down
6 changes: 6 additions & 0 deletions core/types/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,12 @@ func (h *Header) EmptyReceipts() bool {
return h.ReceiptHash == EmptyReceiptsHash
}

// EmptyWithdrawalsHash returns true if there are no WithdrawalsHash for this header/block.
func (h *Header) EmptyWithdrawalsHash() bool {
// TODO(GalaIO): if check EmptyWithdrawalsHash in here?
return h.WithdrawalsHash == nil || *h.WithdrawalsHash == common.Hash{}
}

// Body is a simple (mutable, non-safe) data container for storing and moving
// a block's data contents (transactions and uncles) together.
type Body struct {
Expand Down
2 changes: 1 addition & 1 deletion eth/catalyst/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -874,7 +874,7 @@ func getBody(block *types.Block) *engine.ExecutionPayloadBodyV1 {
}

// Post-shanghai withdrawals MUST be set to empty slice instead of nil
if withdrawals == nil && block.Header().WithdrawalsHash != nil {
if withdrawals == nil && !block.Header().EmptyWithdrawalsHash() {
withdrawals = make([]*types.Withdrawal, 0)
}

Expand Down
8 changes: 4 additions & 4 deletions eth/downloader/downloader.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ var (
maxQueuedHeaders = 32 * 1024 // [eth/62] Maximum number of headers to queue for import (DOS protection)
maxHeadersProcess = 2048 // Number of header download results to import at once into the chain
maxResultsProcess = 2048 // Number of content download results to import at once into the chain
fullMaxForkAncestry uint64 = params.FullImmutabilityThreshold // Maximum chain reorganisation (locally redeclared so tests can reduce it)
FullMaxForkAncestry uint64 = params.FullImmutabilityThreshold // Maximum chain reorganisation (locally redeclared so tests can reduce it)
lightMaxForkAncestry uint64 = params.LightImmutabilityThreshold // Maximum chain reorganisation (locally redeclared so tests can reduce it)

reorgProtThreshold = 48 // Threshold number of recent blocks to disable mini reorg protection
Expand Down Expand Up @@ -550,8 +550,8 @@ func (d *Downloader) syncWithPeer(p *peerConnection, hash common.Hash, td, ttd *

// Legacy sync, use the best announcement we have from the remote peer.
// TODO(karalabe): Drop this pathway.
if remoteHeight > fullMaxForkAncestry+1 {
d.ancientLimit = remoteHeight - fullMaxForkAncestry - 1
if remoteHeight > FullMaxForkAncestry+1 {
d.ancientLimit = remoteHeight - FullMaxForkAncestry - 1
} else {
d.ancientLimit = 0
}
Expand Down Expand Up @@ -789,7 +789,7 @@ func (d *Downloader) findAncestor(p *peerConnection, localHeight uint64, remoteH
p.log.Debug("Looking for common ancestor", "local", localHeight, "remote", remoteHeight)

// Recap floor value for binary search
maxForkAncestry := fullMaxForkAncestry
maxForkAncestry := FullMaxForkAncestry
if d.getMode() == LightSync {
maxForkAncestry = lightMaxForkAncestry
}
Expand Down
6 changes: 3 additions & 3 deletions eth/downloader/queue.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ func newFetchResult(header *types.Header, fastSync bool, pid string) *fetchResul
}
if !header.EmptyBody() {
item.pending.Store(item.pending.Load() | (1 << bodyType))
} else if header.WithdrawalsHash != nil {
} else if !header.EmptyWithdrawalsHash() {
item.Withdrawals = make(types.Withdrawals, 0)
}
if fastSync && !header.EmptyReceipts() {
Expand Down Expand Up @@ -788,9 +788,9 @@ func (q *queue) DeliverBodies(id string, txLists [][]*types.Transaction, txListH
if uncleListHashes[index] != header.UncleHash {
return errInvalidBody
}
if header.WithdrawalsHash == nil {
if header.EmptyWithdrawalsHash() {
// nil hash means that withdrawals should not be present in body
if withdrawalLists[index] != nil {
if len(withdrawalLists[index]) != 0 {
return errInvalidBody
}
} else { // non-nil hash: body must have withdrawals
Expand Down
4 changes: 2 additions & 2 deletions eth/downloader/testchain_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,15 +57,15 @@ var pregenerated bool

func init() {
// Reduce some of the parameters to make the tester faster
fullMaxForkAncestry = 10000
FullMaxForkAncestry = 10000
lightMaxForkAncestry = 10000
blockCacheMaxItems = 1024
fsHeaderSafetyNet = 256
fsHeaderContCheck = 500 * time.Millisecond

testChainBase = newTestChain(blockCacheMaxItems+200, testGenesis)

var forkLen = int(fullMaxForkAncestry + 50)
var forkLen = int(FullMaxForkAncestry + 50)
var wg sync.WaitGroup

// Generate the test chains to seed the peers with
Expand Down
4 changes: 2 additions & 2 deletions graphql/graphql.go
Original file line number Diff line number Diff line change
Expand Up @@ -1041,7 +1041,7 @@ func (b *Block) WithdrawalsRoot(ctx context.Context) (*common.Hash, error) {
return nil, err
}
// Pre-shanghai blocks
if header.WithdrawalsHash == nil {
if header.EmptyWithdrawalsHash() {
return nil, nil
}
return header.WithdrawalsHash, nil
Expand All @@ -1053,7 +1053,7 @@ func (b *Block) Withdrawals(ctx context.Context) (*[]*Withdrawal, error) {
return nil, err
}
// Pre-shanghai blocks
if block.Header().WithdrawalsHash == nil {
if block.Header().EmptyWithdrawalsHash() {
return nil, nil
}
ret := make([]*Withdrawal, 0, len(block.Withdrawals()))
Expand Down
4 changes: 2 additions & 2 deletions internal/ethapi/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -1534,7 +1534,7 @@ func RPCMarshalHeader(head *types.Header) map[string]interface{} {
if head.BaseFee != nil {
result["baseFeePerGas"] = (*hexutil.Big)(head.BaseFee)
}
if head.WithdrawalsHash != nil {
if !head.EmptyWithdrawalsHash() {
result["withdrawalsRoot"] = head.WithdrawalsHash
}
if head.BlobGasUsed != nil {
Expand Down Expand Up @@ -1578,7 +1578,7 @@ func RPCMarshalBlock(block *types.Block, inclTx bool, fullTx bool, config *param
uncleHashes[i] = uncle.Hash()
}
fields["uncles"] = uncleHashes
if block.Header().WithdrawalsHash != nil {
if !block.Header().EmptyWithdrawalsHash() {
fields["withdrawals"] = block.Withdrawals()
}
return fields
Expand Down

0 comments on commit a4620fe

Please sign in to comment.