Merge branch 'ethereum:master' into master

This commit is contained in:
juga1980 2025-07-15 11:55:11 +02:00 committed by GitHub
commit a8edce56e8
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
23 changed files with 427 additions and 101 deletions

View file

@ -2198,6 +2198,12 @@ func MakeChain(ctx *cli.Context, stack *node.Node, readonly bool) (*core.BlockCh
StateHistory: ctx.Uint64(StateHistoryFlag.Name), StateHistory: ctx.Uint64(StateHistoryFlag.Name),
// Disable transaction indexing/unindexing. // Disable transaction indexing/unindexing.
TxLookupLimit: -1, TxLookupLimit: -1,
// Enables file journaling for the trie database. The journal files will be stored
// within the data directory. The corresponding paths will be either:
// - DATADIR/triedb/merkle.journal
// - DATADIR/triedb/verkle.journal
TrieJournalDirectory: stack.ResolvePath("triedb"),
} }
if options.ArchiveMode && !options.Preimages { if options.ArchiveMode && !options.Preimages {
options.Preimages = true options.Preimages = true

View file

@ -23,7 +23,6 @@ import (
"github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/params" "github.com/ethereum/go-ethereum/params"
"github.com/ethereum/go-ethereum/params/forks"
) )
var ( var (
@ -100,42 +99,53 @@ func CalcExcessBlobGas(config *params.ChainConfig, parent *types.Header, headTim
// CalcBlobFee calculates the blobfee from the header's excess blob gas field. // CalcBlobFee calculates the blobfee from the header's excess blob gas field.
func CalcBlobFee(config *params.ChainConfig, header *types.Header) *big.Int { func CalcBlobFee(config *params.ChainConfig, header *types.Header) *big.Int {
var frac uint64 blobConfig := latestBlobConfig(config, header.Time)
switch config.LatestFork(header.Time) { if blobConfig == nil {
case forks.Osaka:
frac = config.BlobScheduleConfig.Osaka.UpdateFraction
case forks.Prague:
frac = config.BlobScheduleConfig.Prague.UpdateFraction
case forks.Cancun:
frac = config.BlobScheduleConfig.Cancun.UpdateFraction
default:
panic("calculating blob fee on unsupported fork") panic("calculating blob fee on unsupported fork")
} }
return fakeExponential(minBlobGasPrice, new(big.Int).SetUint64(*header.ExcessBlobGas), new(big.Int).SetUint64(frac)) return fakeExponential(minBlobGasPrice, new(big.Int).SetUint64(*header.ExcessBlobGas), new(big.Int).SetUint64(blobConfig.UpdateFraction))
} }
// MaxBlobsPerBlock returns the max blobs per block for a block at the given timestamp. // MaxBlobsPerBlock returns the max blobs per block for a block at the given timestamp.
func MaxBlobsPerBlock(cfg *params.ChainConfig, time uint64) int { func MaxBlobsPerBlock(cfg *params.ChainConfig, time uint64) int {
if cfg.BlobScheduleConfig == nil { blobConfig := latestBlobConfig(cfg, time)
if blobConfig == nil {
return 0 return 0
} }
return blobConfig.Max
}
func latestBlobConfig(cfg *params.ChainConfig, time uint64) *params.BlobConfig {
if cfg.BlobScheduleConfig == nil {
return nil
}
var ( var (
london = cfg.LondonBlock london = cfg.LondonBlock
s = cfg.BlobScheduleConfig s = cfg.BlobScheduleConfig
) )
switch { switch {
case cfg.IsBPO5(london, time) && s.BPO5 != nil:
return s.BPO5
case cfg.IsBPO4(london, time) && s.BPO4 != nil:
return s.BPO4
case cfg.IsBPO3(london, time) && s.BPO3 != nil:
return s.BPO3
case cfg.IsBPO2(london, time) && s.BPO2 != nil:
return s.BPO2
case cfg.IsBPO1(london, time) && s.BPO1 != nil:
return s.BPO1
case cfg.IsOsaka(london, time) && s.Osaka != nil: case cfg.IsOsaka(london, time) && s.Osaka != nil:
return s.Osaka.Max return s.Osaka
case cfg.IsPrague(london, time) && s.Prague != nil: case cfg.IsPrague(london, time) && s.Prague != nil:
return s.Prague.Max return s.Prague
case cfg.IsCancun(london, time) && s.Cancun != nil: case cfg.IsCancun(london, time) && s.Cancun != nil:
return s.Cancun.Max return s.Cancun
default: default:
return 0 return nil
} }
} }
// MaxBlobsPerBlock returns the maximum blob gas that can be spent in a block at the given timestamp. // MaxBlobGasPerBlock returns the maximum blob gas that can be spent in a block at the given timestamp.
func MaxBlobGasPerBlock(cfg *params.ChainConfig, time uint64) uint64 { func MaxBlobGasPerBlock(cfg *params.ChainConfig, time uint64) uint64 {
return uint64(MaxBlobsPerBlock(cfg, time)) * params.BlobTxBlobGasPerBlob return uint64(MaxBlobsPerBlock(cfg, time)) * params.BlobTxBlobGasPerBlob
} }
@ -148,6 +158,16 @@ func LatestMaxBlobsPerBlock(cfg *params.ChainConfig) int {
return 0 return 0
} }
switch { switch {
case s.BPO5 != nil:
return s.BPO5.Max
case s.BPO4 != nil:
return s.BPO4.Max
case s.BPO3 != nil:
return s.BPO3.Max
case s.BPO2 != nil:
return s.BPO2.Max
case s.BPO1 != nil:
return s.BPO1.Max
case s.Osaka != nil: case s.Osaka != nil:
return s.Osaka.Max return s.Osaka.Max
case s.Prague != nil: case s.Prague != nil:
@ -161,23 +181,11 @@ func LatestMaxBlobsPerBlock(cfg *params.ChainConfig) int {
// targetBlobsPerBlock returns the target number of blobs in a block at the given timestamp. // targetBlobsPerBlock returns the target number of blobs in a block at the given timestamp.
func targetBlobsPerBlock(cfg *params.ChainConfig, time uint64) int { func targetBlobsPerBlock(cfg *params.ChainConfig, time uint64) int {
if cfg.BlobScheduleConfig == nil { blobConfig := latestBlobConfig(cfg, time)
return 0 if blobConfig == nil {
}
var (
london = cfg.LondonBlock
s = cfg.BlobScheduleConfig
)
switch {
case cfg.IsOsaka(london, time) && s.Osaka != nil:
return s.Osaka.Target
case cfg.IsPrague(london, time) && s.Prague != nil:
return s.Prague.Target
case cfg.IsCancun(london, time) && s.Cancun != nil:
return s.Cancun.Target
default:
return 0 return 0
} }
return blobConfig.Target
} }
// fakeExponential approximates factor * e ** (numerator / denominator) using // fakeExponential approximates factor * e ** (numerator / denominator) using

View file

@ -166,6 +166,7 @@ type BlockChainConfig struct {
TrieDirtyLimit int // Memory limit (MB) at which to start flushing dirty trie nodes to disk TrieDirtyLimit int // Memory limit (MB) at which to start flushing dirty trie nodes to disk
TrieTimeLimit time.Duration // Time limit after which to flush the current in-memory trie to disk TrieTimeLimit time.Duration // Time limit after which to flush the current in-memory trie to disk
TrieNoAsyncFlush bool // Whether the asynchronous buffer flushing is disallowed TrieNoAsyncFlush bool // Whether the asynchronous buffer flushing is disallowed
TrieJournalDirectory string // Directory path to the journal used for persisting trie data across node restarts
Preimages bool // Whether to store preimage of trie key to the disk Preimages bool // Whether to store preimage of trie key to the disk
StateHistory uint64 // Number of blocks from head whose state histories are reserved. StateHistory uint64 // Number of blocks from head whose state histories are reserved.
@ -246,6 +247,7 @@ func (cfg *BlockChainConfig) triedbConfig(isVerkle bool) *triedb.Config {
EnableStateIndexing: cfg.ArchiveMode, EnableStateIndexing: cfg.ArchiveMode,
TrieCleanSize: cfg.TrieCleanLimit * 1024 * 1024, TrieCleanSize: cfg.TrieCleanLimit * 1024 * 1024,
StateCleanSize: cfg.SnapshotLimit * 1024 * 1024, StateCleanSize: cfg.SnapshotLimit * 1024 * 1024,
JournalDirectory: cfg.TrieJournalDirectory,
// TODO(rjl493456442): The write buffer represents the memory limit used // TODO(rjl493456442): The write buffer represents the memory limit used
// for flushing both trie data and state data to disk. The config name // for flushing both trie data and state data to disk. The config name

View file

@ -157,14 +157,6 @@ func WriteTrieJournal(db ethdb.KeyValueWriter, journal []byte) {
} }
} }
// DeleteTrieJournal deletes the serialized in-memory trie nodes of layers saved at
// the last shutdown.
func DeleteTrieJournal(db ethdb.KeyValueWriter) {
if err := db.Delete(trieJournalKey); err != nil {
log.Crit("Failed to remove tries journal", "err", err)
}
}
// ReadStateHistoryMeta retrieves the metadata corresponding to the specified // ReadStateHistoryMeta retrieves the metadata corresponding to the specified
// state history. Compute the position of state history in freezer by minus // state history. Compute the position of state history in freezer by minus
// one since the id of first state history starts from one(zero for initial // one since the id of first state history starts from one(zero for initial

View file

@ -25,9 +25,16 @@ import (
"github.com/golang/snappy" "github.com/golang/snappy"
) )
const (
// This is the maximum amount of data that will be buffered in memory // This is the maximum amount of data that will be buffered in memory
// for a single freezer table batch. // for a single freezer table batch.
const freezerBatchBufferLimit = 2 * 1024 * 1024 freezerBatchBufferLimit = 2 * 1024 * 1024
// freezerTableFlushThreshold defines the threshold for triggering a freezer
// table sync operation. If the number of accumulated uncommitted items exceeds
// this value, a sync will be scheduled.
freezerTableFlushThreshold = 512
)
// freezerBatch is a write operation of multiple items on a freezer. // freezerBatch is a write operation of multiple items on a freezer.
type freezerBatch struct { type freezerBatch struct {
@ -201,6 +208,7 @@ func (batch *freezerTableBatch) commit() error {
// Update headBytes of table. // Update headBytes of table.
batch.t.headBytes += dataSize batch.t.headBytes += dataSize
items := batch.curItem - batch.t.items.Load()
batch.t.items.Store(batch.curItem) batch.t.items.Store(batch.curItem)
// Update metrics. // Update metrics.
@ -208,7 +216,9 @@ func (batch *freezerTableBatch) commit() error {
batch.t.writeMeter.Mark(dataSize + indexSize) batch.t.writeMeter.Mark(dataSize + indexSize)
// Periodically sync the table, todo (rjl493456442) make it configurable? // Periodically sync the table, todo (rjl493456442) make it configurable?
if time.Since(batch.t.lastSync) > 30*time.Second { batch.t.uncommitted += items
if batch.t.uncommitted > freezerTableFlushThreshold && time.Since(batch.t.lastSync) > 30*time.Second {
batch.t.uncommitted = 0
batch.t.lastSync = time.Now() batch.t.lastSync = time.Now()
return batch.t.Sync() return batch.t.Sync()
} }

View file

@ -113,6 +113,7 @@ type freezerTable struct {
tailId uint32 // number of the earliest file tailId uint32 // number of the earliest file
metadata *freezerTableMeta // metadata of the table metadata *freezerTableMeta // metadata of the table
uncommitted uint64 // Count of items written without flushing to file
lastSync time.Time // Timestamp when the last sync was performed lastSync time.Time // Timestamp when the last sync was performed
headBytes int64 // Number of bytes written to the head file headBytes int64 // Number of bytes written to the head file

View file

@ -22,7 +22,6 @@ import (
"errors" "errors"
"fmt" "fmt"
"math/big" "math/big"
"slices" "slices"
"github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common"
@ -75,17 +74,19 @@ func (sc *BlobTxSidecar) BlobHashes() []common.Hash {
} }
// CellProofsAt returns the cell proofs for blob with index idx. // CellProofsAt returns the cell proofs for blob with index idx.
func (sc *BlobTxSidecar) CellProofsAt(idx int) []kzg4844.Proof { // This method is only valid for sidecars with version 1.
var cellProofs []kzg4844.Proof func (sc *BlobTxSidecar) CellProofsAt(idx int) ([]kzg4844.Proof, error) {
for i := range kzg4844.CellProofsPerBlob { if sc.Version != 1 {
index := idx*kzg4844.CellProofsPerBlob + i return nil, fmt.Errorf("cell proof unsupported, version: %d", sc.Version)
if index > len(sc.Proofs) {
return nil
} }
proof := sc.Proofs[index] if idx < 0 || idx >= len(sc.Blobs) {
cellProofs = append(cellProofs, proof) return nil, fmt.Errorf("cell proof out of bounds, index: %d, blobs: %d", idx, len(sc.Blobs))
} }
return cellProofs index := idx * kzg4844.CellProofsPerBlob
if len(sc.Proofs) < index+kzg4844.CellProofsPerBlob {
return nil, fmt.Errorf("cell proof is corrupted, index: %d, proofs: %d", idx, len(sc.Proofs))
}
return sc.Proofs[index : index+kzg4844.CellProofsPerBlob], nil
} }
// encodedSize computes the RLP size of the sidecar elements. This does NOT return the // encodedSize computes the RLP size of the sidecar elements. This does NOT return the
@ -217,6 +218,7 @@ func (tx *BlobTx) copy() TxData {
} }
if tx.Sidecar != nil { if tx.Sidecar != nil {
cpy.Sidecar = &BlobTxSidecar{ cpy.Sidecar = &BlobTxSidecar{
Version: tx.Sidecar.Version,
Blobs: slices.Clone(tx.Sidecar.Blobs), Blobs: slices.Clone(tx.Sidecar.Blobs),
Commitments: slices.Clone(tx.Sidecar.Commitments), Commitments: slices.Clone(tx.Sidecar.Commitments),
Proofs: slices.Clone(tx.Sidecar.Proofs), Proofs: slices.Clone(tx.Sidecar.Proofs),

View file

@ -236,9 +236,13 @@ func New(stack *node.Node, config *ethconfig.Config) (*Ethereum, error) {
VmConfig: vm.Config{ VmConfig: vm.Config{
EnablePreimageRecording: config.EnablePreimageRecording, EnablePreimageRecording: config.EnablePreimageRecording,
}, },
// Enables file journaling for the trie database. The journal files will be stored
// within the data directory. The corresponding paths will be either:
// - DATADIR/triedb/merkle.journal
// - DATADIR/triedb/verkle.journal
TrieJournalDirectory: stack.ResolvePath("triedb"),
} }
) )
if config.VMTrace != "" { if config.VMTrace != "" {
traceConfig := json.RawMessage("{}") traceConfig := json.RawMessage("{}")
if config.VMTraceJsonConfig != "" { if config.VMTraceJsonConfig != "" {

View file

@ -564,7 +564,12 @@ func (api *ConsensusAPI) GetBlobsV2(hashes []common.Hash) ([]*engine.BlobAndProo
blobHashes := sidecar.BlobHashes() blobHashes := sidecar.BlobHashes()
for bIdx, hash := range blobHashes { for bIdx, hash := range blobHashes {
if idxes, ok := index[hash]; ok { if idxes, ok := index[hash]; ok {
proofs := sidecar.CellProofsAt(bIdx) proofs, err := sidecar.CellProofsAt(bIdx)
if err != nil {
// TODO @rjl @marius we should return an error
log.Info("Failed to get cell proof", "err", err)
return nil, nil
}
var cellProofs []hexutil.Bytes var cellProofs []hexutil.Bytes
for _, proof := range proofs { for _, proof := range proofs {
cellProofs = append(cellProofs, proof[:]) cellProofs = append(cellProofs, proof[:])

View file

@ -439,8 +439,8 @@ func (f *TxFetcher) loop() {
if want > maxTxAnnounces { if want > maxTxAnnounces {
txAnnounceDOSMeter.Mark(int64(want - maxTxAnnounces)) txAnnounceDOSMeter.Mark(int64(want - maxTxAnnounces))
ann.hashes = ann.hashes[:want-maxTxAnnounces] ann.hashes = ann.hashes[:maxTxAnnounces-used]
ann.metas = ann.metas[:want-maxTxAnnounces] ann.metas = ann.metas[:maxTxAnnounces-used]
} }
// All is well, schedule the remainder of the transactions // All is well, schedule the remainder of the transactions
var ( var (

View file

@ -1179,6 +1179,24 @@ func TestTransactionFetcherDoSProtection(t *testing.T) {
size: 111, size: 111,
}) })
} }
var (
hashesC []common.Hash
typesC []byte
sizesC []uint32
announceC []announce
)
for i := 0; i < maxTxAnnounces+2; i++ {
hash := common.Hash{0x03, byte(i / 256), byte(i % 256)}
hashesC = append(hashesC, hash)
typesC = append(typesC, types.LegacyTxType)
sizesC = append(sizesC, 111)
announceC = append(announceC, announce{
hash: hash,
kind: types.LegacyTxType,
size: 111,
})
}
testTransactionFetcherParallel(t, txFetcherTest{ testTransactionFetcherParallel(t, txFetcherTest{
init: func() *TxFetcher { init: func() *TxFetcher {
return NewTxFetcher( return NewTxFetcher(
@ -1192,43 +1210,52 @@ func TestTransactionFetcherDoSProtection(t *testing.T) {
// Announce half of the transaction and wait for them to be scheduled // Announce half of the transaction and wait for them to be scheduled
doTxNotify{peer: "A", hashes: hashesA[:maxTxAnnounces/2], types: typesA[:maxTxAnnounces/2], sizes: sizesA[:maxTxAnnounces/2]}, doTxNotify{peer: "A", hashes: hashesA[:maxTxAnnounces/2], types: typesA[:maxTxAnnounces/2], sizes: sizesA[:maxTxAnnounces/2]},
doTxNotify{peer: "B", hashes: hashesB[:maxTxAnnounces/2-1], types: typesB[:maxTxAnnounces/2-1], sizes: sizesB[:maxTxAnnounces/2-1]}, doTxNotify{peer: "B", hashes: hashesB[:maxTxAnnounces/2-1], types: typesB[:maxTxAnnounces/2-1], sizes: sizesB[:maxTxAnnounces/2-1]},
doTxNotify{peer: "C", hashes: hashesC[:maxTxAnnounces/2-1], types: typesC[:maxTxAnnounces/2-1], sizes: sizesC[:maxTxAnnounces/2-1]},
doWait{time: txArriveTimeout, step: true}, doWait{time: txArriveTimeout, step: true},
// Announce the second half and keep them in the wait list // Announce the second half and keep them in the wait list
doTxNotify{peer: "A", hashes: hashesA[maxTxAnnounces/2 : maxTxAnnounces], types: typesA[maxTxAnnounces/2 : maxTxAnnounces], sizes: sizesA[maxTxAnnounces/2 : maxTxAnnounces]}, doTxNotify{peer: "A", hashes: hashesA[maxTxAnnounces/2 : maxTxAnnounces], types: typesA[maxTxAnnounces/2 : maxTxAnnounces], sizes: sizesA[maxTxAnnounces/2 : maxTxAnnounces]},
doTxNotify{peer: "B", hashes: hashesB[maxTxAnnounces/2-1 : maxTxAnnounces-1], types: typesB[maxTxAnnounces/2-1 : maxTxAnnounces-1], sizes: sizesB[maxTxAnnounces/2-1 : maxTxAnnounces-1]}, doTxNotify{peer: "B", hashes: hashesB[maxTxAnnounces/2-1 : maxTxAnnounces-1], types: typesB[maxTxAnnounces/2-1 : maxTxAnnounces-1], sizes: sizesB[maxTxAnnounces/2-1 : maxTxAnnounces-1]},
doTxNotify{peer: "C", hashes: hashesC[maxTxAnnounces/2-1 : maxTxAnnounces-1], types: typesC[maxTxAnnounces/2-1 : maxTxAnnounces-1], sizes: sizesC[maxTxAnnounces/2-1 : maxTxAnnounces-1]},
// Ensure the hashes are split half and half // Ensure the hashes are split half and half
isWaiting(map[string][]announce{ isWaiting(map[string][]announce{
"A": announceA[maxTxAnnounces/2 : maxTxAnnounces], "A": announceA[maxTxAnnounces/2 : maxTxAnnounces],
"B": announceB[maxTxAnnounces/2-1 : maxTxAnnounces-1], "B": announceB[maxTxAnnounces/2-1 : maxTxAnnounces-1],
"C": announceC[maxTxAnnounces/2-1 : maxTxAnnounces-1],
}), }),
isScheduled{ isScheduled{
tracking: map[string][]announce{ tracking: map[string][]announce{
"A": announceA[:maxTxAnnounces/2], "A": announceA[:maxTxAnnounces/2],
"B": announceB[:maxTxAnnounces/2-1], "B": announceB[:maxTxAnnounces/2-1],
"C": announceC[:maxTxAnnounces/2-1],
}, },
fetching: map[string][]common.Hash{ fetching: map[string][]common.Hash{
"A": hashesA[:maxTxRetrievals], "A": hashesA[:maxTxRetrievals],
"B": hashesB[:maxTxRetrievals], "B": hashesB[:maxTxRetrievals],
"C": hashesC[:maxTxRetrievals],
}, },
}, },
// Ensure that adding even one more hash results in dropping the hash // Ensure that adding even one more hash results in dropping the hash
doTxNotify{peer: "A", hashes: []common.Hash{hashesA[maxTxAnnounces]}, types: []byte{typesA[maxTxAnnounces]}, sizes: []uint32{sizesA[maxTxAnnounces]}}, doTxNotify{peer: "A", hashes: []common.Hash{hashesA[maxTxAnnounces]}, types: []byte{typesA[maxTxAnnounces]}, sizes: []uint32{sizesA[maxTxAnnounces]}},
doTxNotify{peer: "B", hashes: hashesB[maxTxAnnounces-1 : maxTxAnnounces+1], types: typesB[maxTxAnnounces-1 : maxTxAnnounces+1], sizes: sizesB[maxTxAnnounces-1 : maxTxAnnounces+1]}, doTxNotify{peer: "B", hashes: hashesB[maxTxAnnounces-1 : maxTxAnnounces+1], types: typesB[maxTxAnnounces-1 : maxTxAnnounces+1], sizes: sizesB[maxTxAnnounces-1 : maxTxAnnounces+1]},
doTxNotify{peer: "C", hashes: hashesC[maxTxAnnounces-1 : maxTxAnnounces+2], types: typesC[maxTxAnnounces-1 : maxTxAnnounces+2], sizes: sizesC[maxTxAnnounces-1 : maxTxAnnounces+2]},
isWaiting(map[string][]announce{ isWaiting(map[string][]announce{
"A": announceA[maxTxAnnounces/2 : maxTxAnnounces], "A": announceA[maxTxAnnounces/2 : maxTxAnnounces],
"B": announceB[maxTxAnnounces/2-1 : maxTxAnnounces], "B": announceB[maxTxAnnounces/2-1 : maxTxAnnounces],
"C": announceC[maxTxAnnounces/2-1 : maxTxAnnounces],
}), }),
isScheduled{ isScheduled{
tracking: map[string][]announce{ tracking: map[string][]announce{
"A": announceA[:maxTxAnnounces/2], "A": announceA[:maxTxAnnounces/2],
"B": announceB[:maxTxAnnounces/2-1], "B": announceB[:maxTxAnnounces/2-1],
"C": announceC[:maxTxAnnounces/2-1],
}, },
fetching: map[string][]common.Hash{ fetching: map[string][]common.Hash{
"A": hashesA[:maxTxRetrievals], "A": hashesA[:maxTxRetrievals],
"B": hashesB[:maxTxRetrievals], "B": hashesB[:maxTxRetrievals],
"C": hashesC[:maxTxRetrievals],
}, },
}, },
}, },

View file

@ -615,7 +615,7 @@ func (s *Syncer) Sync(root common.Hash, cancel chan struct{}) error {
s.statelessPeers = make(map[string]struct{}) s.statelessPeers = make(map[string]struct{})
s.lock.Unlock() s.lock.Unlock()
if s.startTime == (time.Time{}) { if s.startTime.IsZero() {
s.startTime = time.Now() s.startTime = time.Now()
} }
// Retrieve the previous sync status from LevelDB and abort if already synced // Retrieve the previous sync status from LevelDB and abort if already synced
@ -1987,9 +1987,7 @@ func (s *Syncer) processAccountResponse(res *accountResponse) {
func (s *Syncer) processBytecodeResponse(res *bytecodeResponse) { func (s *Syncer) processBytecodeResponse(res *bytecodeResponse) {
batch := s.db.NewBatch() batch := s.db.NewBatch()
var ( var codes uint64
codes uint64
)
for i, hash := range res.hashes { for i, hash := range res.hashes {
code := res.codes[i] code := res.codes[i]

View file

@ -1549,7 +1549,7 @@ func (api *TransactionAPI) SendRawTransaction(ctx context.Context, input hexutil
// //
// The account associated with addr must be unlocked. // The account associated with addr must be unlocked.
// //
// https://github.com/ethereum/wiki/wiki/JSON-RPC#eth_sign // https://ethereum.org/en/developers/docs/apis/json-rpc/#eth_sign
func (api *TransactionAPI) Sign(addr common.Address, data hexutil.Bytes) (hexutil.Bytes, error) { func (api *TransactionAPI) Sign(addr common.Address, data hexutil.Bytes) (hexutil.Bytes, error) {
// Look up the wallet containing the requested signer // Look up the wallet containing the requested signer
account := accounts.Account{Address: addr} account := accounts.Account{Address: addr}

View file

@ -34,7 +34,7 @@ type revertError struct {
} }
// ErrorCode returns the JSON error code for a revert. // ErrorCode returns the JSON error code for a revert.
// See: https://github.com/ethereum/wiki/wiki/JSON-RPC-Error-Codes-Improvement-Proposal // See: https://ethereum.org/en/developers/docs/apis/json-rpc/#error-codes
func (e *revertError) ErrorCode() int { func (e *revertError) ErrorCode() int {
return 3 return 3
} }
@ -71,7 +71,7 @@ func (e *TxIndexingError) Error() string {
} }
// ErrorCode returns the JSON error code for a revert. // ErrorCode returns the JSON error code for a revert.
// See: https://github.com/ethereum/wiki/wiki/JSON-RPC-Error-Codes-Improvement-Proposal // See: https://ethereum.org/en/developers/docs/apis/json-rpc/#error-codes
func (e *TxIndexingError) ErrorCode() int { func (e *TxIndexingError) ErrorCode() int {
return -32000 // to be decided return -32000 // to be decided
} }

View file

@ -440,6 +440,7 @@ func (miner *Miner) commitTransactions(env *environment, plainTxs, blobTxs *tran
} }
sidecar.Proofs = append(sidecar.Proofs, cellProofs...) sidecar.Proofs = append(sidecar.Proofs, cellProofs...)
} }
sidecar.Version = 1
} }
} }
} }

View file

@ -562,7 +562,7 @@ func (tab *Table) addReplacement(b *bucket, n *enode.Node) {
} }
func (tab *Table) nodeAdded(b *bucket, n *tableNode) { func (tab *Table) nodeAdded(b *bucket, n *tableNode) {
if n.addedToTable == (time.Time{}) { if n.addedToTable.IsZero() {
n.addedToTable = time.Now() n.addedToTable = time.Now()
} }
n.addedToBucket = time.Now() n.addedToBucket = time.Now()

View file

@ -411,6 +411,11 @@ type ChainConfig struct {
PragueTime *uint64 `json:"pragueTime,omitempty"` // Prague switch time (nil = no fork, 0 = already on prague) PragueTime *uint64 `json:"pragueTime,omitempty"` // Prague switch time (nil = no fork, 0 = already on prague)
OsakaTime *uint64 `json:"osakaTime,omitempty"` // Osaka switch time (nil = no fork, 0 = already on osaka) OsakaTime *uint64 `json:"osakaTime,omitempty"` // Osaka switch time (nil = no fork, 0 = already on osaka)
VerkleTime *uint64 `json:"verkleTime,omitempty"` // Verkle switch time (nil = no fork, 0 = already on verkle) VerkleTime *uint64 `json:"verkleTime,omitempty"` // Verkle switch time (nil = no fork, 0 = already on verkle)
BPO1Time *uint64 `json:"bpo1Time,omitempty"` // BPO1 switch time (nil = no fork, 0 = already on bpo1)
BPO2Time *uint64 `json:"bpo2Time,omitempty"` // BPO2 switch time (nil = no fork, 0 = already on bpo2)
BPO3Time *uint64 `json:"bpo3Time,omitempty"` // BPO3 switch time (nil = no fork, 0 = already on bpo3)
BPO4Time *uint64 `json:"bpo4Time,omitempty"` // BPO4 switch time (nil = no fork, 0 = already on bpo4)
BPO5Time *uint64 `json:"bpo5Time,omitempty"` // BPO5 switch time (nil = no fork, 0 = already on bpo5)
// TerminalTotalDifficulty is the amount of total difficulty reached by // TerminalTotalDifficulty is the amount of total difficulty reached by
// the network that triggers the consensus upgrade. // the network that triggers the consensus upgrade.
@ -531,6 +536,21 @@ func (c *ChainConfig) Description() string {
if c.VerkleTime != nil { if c.VerkleTime != nil {
banner += fmt.Sprintf(" - Verkle: @%-10v\n", *c.VerkleTime) banner += fmt.Sprintf(" - Verkle: @%-10v\n", *c.VerkleTime)
} }
if c.BPO1Time != nil {
banner += fmt.Sprintf(" - BPO1: @%-10v\n", *c.BPO1Time)
}
if c.BPO2Time != nil {
banner += fmt.Sprintf(" - BPO2: @%-10v\n", *c.BPO2Time)
}
if c.BPO3Time != nil {
banner += fmt.Sprintf(" - BPO3: @%-10v\n", *c.BPO3Time)
}
if c.BPO4Time != nil {
banner += fmt.Sprintf(" - BPO4: @%-10v\n", *c.BPO4Time)
}
if c.BPO5Time != nil {
banner += fmt.Sprintf(" - BPO5: @%-10v\n", *c.BPO5Time)
}
return banner return banner
} }
@ -547,6 +567,11 @@ type BlobScheduleConfig struct {
Prague *BlobConfig `json:"prague,omitempty"` Prague *BlobConfig `json:"prague,omitempty"`
Osaka *BlobConfig `json:"osaka,omitempty"` Osaka *BlobConfig `json:"osaka,omitempty"`
Verkle *BlobConfig `json:"verkle,omitempty"` Verkle *BlobConfig `json:"verkle,omitempty"`
BPO1 *BlobConfig `json:"bpo1,omitempty"`
BPO2 *BlobConfig `json:"bpo2,omitempty"`
BPO3 *BlobConfig `json:"bpo3,omitempty"`
BPO4 *BlobConfig `json:"bpo4,omitempty"`
BPO5 *BlobConfig `json:"bpo5,omitempty"`
} }
// IsHomestead returns whether num is either equal to the homestead block or greater. // IsHomestead returns whether num is either equal to the homestead block or greater.
@ -654,6 +679,31 @@ func (c *ChainConfig) IsVerkle(num *big.Int, time uint64) bool {
return c.IsLondon(num) && isTimestampForked(c.VerkleTime, time) return c.IsLondon(num) && isTimestampForked(c.VerkleTime, time)
} }
// IsBPO1 returns whether time is either equal to the BPO1 fork time or greater.
func (c *ChainConfig) IsBPO1(num *big.Int, time uint64) bool {
return c.IsLondon(num) && isTimestampForked(c.BPO1Time, time)
}
// IsBPO2 returns whether time is either equal to the BPO2 fork time or greater.
func (c *ChainConfig) IsBPO2(num *big.Int, time uint64) bool {
return c.IsLondon(num) && isTimestampForked(c.BPO2Time, time)
}
// IsBPO3 returns whether time is either equal to the BPO3 fork time or greater.
func (c *ChainConfig) IsBPO3(num *big.Int, time uint64) bool {
return c.IsLondon(num) && isTimestampForked(c.BPO3Time, time)
}
// IsBPO4 returns whether time is either equal to the BPO4 fork time or greater.
func (c *ChainConfig) IsBPO4(num *big.Int, time uint64) bool {
return c.IsLondon(num) && isTimestampForked(c.BPO4Time, time)
}
// IsBPO5 returns whether time is either equal to the BPO5 fork time or greater.
func (c *ChainConfig) IsBPO5(num *big.Int, time uint64) bool {
return c.IsLondon(num) && isTimestampForked(c.BPO5Time, time)
}
// IsVerkleGenesis checks whether the verkle fork is activated at the genesis block. // IsVerkleGenesis checks whether the verkle fork is activated at the genesis block.
// //
// Verkle mode is considered enabled if the verkle fork time is configured, // Verkle mode is considered enabled if the verkle fork time is configured,
@ -729,6 +779,11 @@ func (c *ChainConfig) CheckConfigForkOrder() error {
{name: "pragueTime", timestamp: c.PragueTime, optional: true}, {name: "pragueTime", timestamp: c.PragueTime, optional: true},
{name: "osakaTime", timestamp: c.OsakaTime, optional: true}, {name: "osakaTime", timestamp: c.OsakaTime, optional: true},
{name: "verkleTime", timestamp: c.VerkleTime, optional: true}, {name: "verkleTime", timestamp: c.VerkleTime, optional: true},
{name: "bpo1", timestamp: c.BPO1Time, optional: true},
{name: "bpo2", timestamp: c.BPO2Time, optional: true},
{name: "bpo3", timestamp: c.BPO3Time, optional: true},
{name: "bpo4", timestamp: c.BPO4Time, optional: true},
{name: "bpo5", timestamp: c.BPO5Time, optional: true},
} { } {
if lastFork.name != "" { if lastFork.name != "" {
switch { switch {
@ -778,6 +833,11 @@ func (c *ChainConfig) CheckConfigForkOrder() error {
{name: "cancun", timestamp: c.CancunTime, config: bsc.Cancun}, {name: "cancun", timestamp: c.CancunTime, config: bsc.Cancun},
{name: "prague", timestamp: c.PragueTime, config: bsc.Prague}, {name: "prague", timestamp: c.PragueTime, config: bsc.Prague},
{name: "osaka", timestamp: c.OsakaTime, config: bsc.Osaka}, {name: "osaka", timestamp: c.OsakaTime, config: bsc.Osaka},
{name: "bpo1", timestamp: c.BPO1Time, config: bsc.BPO1},
{name: "bpo2", timestamp: c.BPO2Time, config: bsc.BPO2},
{name: "bpo3", timestamp: c.BPO3Time, config: bsc.BPO3},
{name: "bpo4", timestamp: c.BPO4Time, config: bsc.BPO4},
{name: "bpo5", timestamp: c.BPO5Time, config: bsc.BPO5},
} { } {
if cur.config != nil { if cur.config != nil {
if err := cur.config.validate(); err != nil { if err := cur.config.validate(); err != nil {
@ -878,6 +938,21 @@ func (c *ChainConfig) checkCompatible(newcfg *ChainConfig, headNumber *big.Int,
if isForkTimestampIncompatible(c.VerkleTime, newcfg.VerkleTime, headTimestamp) { if isForkTimestampIncompatible(c.VerkleTime, newcfg.VerkleTime, headTimestamp) {
return newTimestampCompatError("Verkle fork timestamp", c.VerkleTime, newcfg.VerkleTime) return newTimestampCompatError("Verkle fork timestamp", c.VerkleTime, newcfg.VerkleTime)
} }
if isForkTimestampIncompatible(c.BPO1Time, newcfg.BPO1Time, headTimestamp) {
return newTimestampCompatError("BPO1 fork timestamp", c.BPO1Time, newcfg.BPO1Time)
}
if isForkTimestampIncompatible(c.BPO2Time, newcfg.BPO2Time, headTimestamp) {
return newTimestampCompatError("BPO2 fork timestamp", c.BPO2Time, newcfg.BPO2Time)
}
if isForkTimestampIncompatible(c.BPO3Time, newcfg.BPO3Time, headTimestamp) {
return newTimestampCompatError("BPO3 fork timestamp", c.BPO3Time, newcfg.BPO3Time)
}
if isForkTimestampIncompatible(c.BPO4Time, newcfg.BPO4Time, headTimestamp) {
return newTimestampCompatError("BPO4 fork timestamp", c.BPO4Time, newcfg.BPO4Time)
}
if isForkTimestampIncompatible(c.BPO5Time, newcfg.BPO5Time, headTimestamp) {
return newTimestampCompatError("BPO5 fork timestamp", c.BPO5Time, newcfg.BPO5Time)
}
return nil return nil
} }

View file

@ -21,6 +21,7 @@ import (
"errors" "errors"
"fmt" "fmt"
"io" "io"
"path/filepath"
"sync" "sync"
"time" "time"
@ -120,6 +121,7 @@ type Config struct {
StateCleanSize int // Maximum memory allowance (in bytes) for caching clean state data StateCleanSize int // Maximum memory allowance (in bytes) for caching clean state data
WriteBufferSize int // Maximum memory allowance (in bytes) for write buffer WriteBufferSize int // Maximum memory allowance (in bytes) for write buffer
ReadOnly bool // Flag whether the database is opened in read only mode ReadOnly bool // Flag whether the database is opened in read only mode
JournalDirectory string // Absolute path of journal directory (null means the journal data is persisted in key-value store)
// Testing configurations // Testing configurations
SnapshotNoBuild bool // Flag Whether the state generation is allowed SnapshotNoBuild bool // Flag Whether the state generation is allowed
@ -156,6 +158,9 @@ func (c *Config) fields() []interface{} {
} else { } else {
list = append(list, "history", fmt.Sprintf("last %d blocks", c.StateHistory)) list = append(list, "history", fmt.Sprintf("last %d blocks", c.StateHistory))
} }
if c.JournalDirectory != "" {
list = append(list, "journal-dir", c.JournalDirectory)
}
return list return list
} }
@ -493,7 +498,6 @@ func (db *Database) Enable(root common.Hash) error {
// Drop the stale state journal in persistent database and // Drop the stale state journal in persistent database and
// reset the persistent state id back to zero. // reset the persistent state id back to zero.
batch := db.diskdb.NewBatch() batch := db.diskdb.NewBatch()
rawdb.DeleteTrieJournal(batch)
rawdb.DeleteSnapshotRoot(batch) rawdb.DeleteSnapshotRoot(batch)
rawdb.WritePersistentStateID(batch, 0) rawdb.WritePersistentStateID(batch, 0)
if err := batch.Write(); err != nil { if err := batch.Write(); err != nil {
@ -573,8 +577,6 @@ func (db *Database) Recover(root common.Hash) error {
// disk layer won't be accessible from outside. // disk layer won't be accessible from outside.
db.tree.init(dl) db.tree.init(dl)
} }
rawdb.DeleteTrieJournal(db.diskdb)
// Explicitly sync the key-value store to ensure all recent writes are // Explicitly sync the key-value store to ensure all recent writes are
// flushed to disk. This step is crucial to prevent a scenario where // flushed to disk. This step is crucial to prevent a scenario where
// recent key-value writes are lost due to an application panic, while // recent key-value writes are lost due to an application panic, while
@ -680,6 +682,20 @@ func (db *Database) modifyAllowed() error {
return nil return nil
} }
// journalPath returns the absolute path of journal for persisting state data.
func (db *Database) journalPath() string {
if db.config.JournalDirectory == "" {
return ""
}
var fname string
if db.isVerkle {
fname = fmt.Sprintf("verkle.journal")
} else {
fname = fmt.Sprintf("merkle.journal")
}
return filepath.Join(db.config.JournalDirectory, fname)
}
// AccountHistory inspects the account history within the specified range. // AccountHistory inspects the account history within the specified range.
// //
// Start: State ID of the first history object for the query. 0 implies the first // Start: State ID of the first history object for the query. 0 implies the first

View file

@ -21,12 +21,16 @@ import (
"errors" "errors"
"fmt" "fmt"
"math/rand" "math/rand"
"os"
"path/filepath"
"strconv"
"testing" "testing"
"github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/rawdb" "github.com/ethereum/go-ethereum/core/rawdb"
"github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/crypto" "github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/ethdb"
"github.com/ethereum/go-ethereum/internal/testrand" "github.com/ethereum/go-ethereum/internal/testrand"
"github.com/ethereum/go-ethereum/rlp" "github.com/ethereum/go-ethereum/rlp"
"github.com/ethereum/go-ethereum/trie" "github.com/ethereum/go-ethereum/trie"
@ -121,7 +125,7 @@ type tester struct {
snapStorages map[common.Hash]map[common.Hash]map[common.Hash][]byte // Keyed by the hash of account address and the hash of storage key snapStorages map[common.Hash]map[common.Hash]map[common.Hash][]byte // Keyed by the hash of account address and the hash of storage key
} }
func newTester(t *testing.T, historyLimit uint64, isVerkle bool, layers int, enableIndex bool) *tester { func newTester(t *testing.T, historyLimit uint64, isVerkle bool, layers int, enableIndex bool, journalDir string) *tester {
var ( var (
disk, _ = rawdb.Open(rawdb.NewMemoryDatabase(), rawdb.OpenOptions{Ancient: t.TempDir()}) disk, _ = rawdb.Open(rawdb.NewMemoryDatabase(), rawdb.OpenOptions{Ancient: t.TempDir()})
db = New(disk, &Config{ db = New(disk, &Config{
@ -131,6 +135,7 @@ func newTester(t *testing.T, historyLimit uint64, isVerkle bool, layers int, ena
StateCleanSize: 256 * 1024, StateCleanSize: 256 * 1024,
WriteBufferSize: 256 * 1024, WriteBufferSize: 256 * 1024,
NoAsyncFlush: true, NoAsyncFlush: true,
JournalDirectory: journalDir,
}, isVerkle) }, isVerkle)
obj = &tester{ obj = &tester{
@ -466,7 +471,7 @@ func TestDatabaseRollback(t *testing.T) {
}() }()
// Verify state histories // Verify state histories
tester := newTester(t, 0, false, 32, false) tester := newTester(t, 0, false, 32, false, "")
defer tester.release() defer tester.release()
if err := tester.verifyHistory(); err != nil { if err := tester.verifyHistory(); err != nil {
@ -500,7 +505,7 @@ func TestDatabaseRecoverable(t *testing.T) {
}() }()
var ( var (
tester = newTester(t, 0, false, 12, false) tester = newTester(t, 0, false, 12, false, "")
index = tester.bottomIndex() index = tester.bottomIndex()
) )
defer tester.release() defer tester.release()
@ -544,7 +549,7 @@ func TestDisable(t *testing.T) {
maxDiffLayers = 128 maxDiffLayers = 128
}() }()
tester := newTester(t, 0, false, 32, false) tester := newTester(t, 0, false, 32, false, "")
defer tester.release() defer tester.release()
stored := crypto.Keccak256Hash(rawdb.ReadAccountTrieNode(tester.db.diskdb, nil)) stored := crypto.Keccak256Hash(rawdb.ReadAccountTrieNode(tester.db.diskdb, nil))
@ -586,7 +591,7 @@ func TestCommit(t *testing.T) {
maxDiffLayers = 128 maxDiffLayers = 128
}() }()
tester := newTester(t, 0, false, 12, false) tester := newTester(t, 0, false, 12, false, "")
defer tester.release() defer tester.release()
if err := tester.db.Commit(tester.lastHash(), false); err != nil { if err := tester.db.Commit(tester.lastHash(), false); err != nil {
@ -610,20 +615,25 @@ func TestCommit(t *testing.T) {
} }
func TestJournal(t *testing.T) { func TestJournal(t *testing.T) {
testJournal(t, "")
testJournal(t, filepath.Join(t.TempDir(), strconv.Itoa(rand.Intn(10000))))
}
func testJournal(t *testing.T, journalDir string) {
// Redefine the diff layer depth allowance for faster testing. // Redefine the diff layer depth allowance for faster testing.
maxDiffLayers = 4 maxDiffLayers = 4
defer func() { defer func() {
maxDiffLayers = 128 maxDiffLayers = 128
}() }()
tester := newTester(t, 0, false, 12, false) tester := newTester(t, 0, false, 12, false, journalDir)
defer tester.release() defer tester.release()
if err := tester.db.Journal(tester.lastHash()); err != nil { if err := tester.db.Journal(tester.lastHash()); err != nil {
t.Errorf("Failed to journal, err: %v", err) t.Errorf("Failed to journal, err: %v", err)
} }
tester.db.Close() tester.db.Close()
tester.db = New(tester.db.diskdb, nil, false) tester.db = New(tester.db.diskdb, tester.db.config, false)
// Verify states including disk layer and all diff on top. // Verify states including disk layer and all diff on top.
for i := 0; i < len(tester.roots); i++ { for i := 0; i < len(tester.roots); i++ {
@ -640,13 +650,30 @@ func TestJournal(t *testing.T) {
} }
func TestCorruptedJournal(t *testing.T) { func TestCorruptedJournal(t *testing.T) {
testCorruptedJournal(t, "", func(db ethdb.Database) {
// Mutate the journal in disk, it should be regarded as invalid
blob := rawdb.ReadTrieJournal(db)
blob[0] = 0xa
rawdb.WriteTrieJournal(db, blob)
})
directory := filepath.Join(t.TempDir(), strconv.Itoa(rand.Intn(10000)))
testCorruptedJournal(t, directory, func(_ ethdb.Database) {
f, _ := os.OpenFile(filepath.Join(directory, "merkle.journal"), os.O_WRONLY, 0644)
f.WriteAt([]byte{0xa}, 0)
f.Sync()
f.Close()
})
}
func testCorruptedJournal(t *testing.T, journalDir string, modifyFn func(database ethdb.Database)) {
// Redefine the diff layer depth allowance for faster testing. // Redefine the diff layer depth allowance for faster testing.
maxDiffLayers = 4 maxDiffLayers = 4
defer func() { defer func() {
maxDiffLayers = 128 maxDiffLayers = 128
}() }()
tester := newTester(t, 0, false, 12, false) tester := newTester(t, 0, false, 12, false, journalDir)
defer tester.release() defer tester.release()
if err := tester.db.Journal(tester.lastHash()); err != nil { if err := tester.db.Journal(tester.lastHash()); err != nil {
@ -655,13 +682,10 @@ func TestCorruptedJournal(t *testing.T) {
tester.db.Close() tester.db.Close()
root := crypto.Keccak256Hash(rawdb.ReadAccountTrieNode(tester.db.diskdb, nil)) root := crypto.Keccak256Hash(rawdb.ReadAccountTrieNode(tester.db.diskdb, nil))
// Mutate the journal in disk, it should be regarded as invalid modifyFn(tester.db.diskdb)
blob := rawdb.ReadTrieJournal(tester.db.diskdb)
blob[0] = 0xa
rawdb.WriteTrieJournal(tester.db.diskdb, blob)
// Verify states, all not-yet-written states should be discarded // Verify states, all not-yet-written states should be discarded
tester.db = New(tester.db.diskdb, nil, false) tester.db = New(tester.db.diskdb, tester.db.config, false)
for i := 0; i < len(tester.roots); i++ { for i := 0; i < len(tester.roots); i++ {
if tester.roots[i] == root { if tester.roots[i] == root {
if err := tester.verifyState(root); err != nil { if err := tester.verifyState(root); err != nil {
@ -694,7 +718,7 @@ func TestTailTruncateHistory(t *testing.T) {
maxDiffLayers = 128 maxDiffLayers = 128
}() }()
tester := newTester(t, 10, false, 12, false) tester := newTester(t, 10, false, 12, false, "")
defer tester.release() defer tester.release()
tester.db.Close() tester.db.Close()

View file

@ -0,0 +1,57 @@
// Copyright 2025 The go-ethereum Authors
// This file is part of the go-ethereum library.
//
// The go-ethereum library is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// The go-ethereum library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
//go:build !windows
// +build !windows
package pathdb
import (
"errors"
"os"
"syscall"
)
func isErrInvalid(err error) bool {
if errors.Is(err, os.ErrInvalid) {
return true
}
// Go >= 1.8 returns *os.PathError instead
if patherr, ok := err.(*os.PathError); ok && patherr.Err == syscall.EINVAL {
return true
}
return false
}
func syncDir(name string) error {
// As per fsync manpage, Linux seems to expect fsync on directory, however
// some system don't support this, so we will ignore syscall.EINVAL.
//
// From fsync(2):
// Calling fsync() does not necessarily ensure that the entry in the
// directory containing the file has also reached disk. For that an
// explicit fsync() on a file descriptor for the directory is also needed.
f, err := os.Open(name)
if err != nil {
return err
}
defer f.Close()
if err := f.Sync(); err != nil && !isErrInvalid(err) {
return err
}
return nil
}

View file

@ -0,0 +1,25 @@
// Copyright 2025 The go-ethereum Authors
// This file is part of the go-ethereum library.
//
// The go-ethereum library is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// The go-ethereum library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
//go:build windows
// +build windows
package pathdb
func syncDir(name string) error {
// On Windows, fsync on directories is not supported
return nil
}

View file

@ -126,7 +126,7 @@ func testHistoryReader(t *testing.T, historyLimit uint64) {
}() }()
//log.SetDefault(log.NewLogger(log.NewTerminalHandlerWithLevel(os.Stderr, log.LevelDebug, true))) //log.SetDefault(log.NewLogger(log.NewTerminalHandlerWithLevel(os.Stderr, log.LevelDebug, true)))
env := newTester(t, historyLimit, false, 64, true) env := newTester(t, historyLimit, false, 64, true, "")
defer env.release() defer env.release()
waitIndexing(env.db) waitIndexing(env.db)

View file

@ -21,6 +21,7 @@ import (
"errors" "errors"
"fmt" "fmt"
"io" "io"
"os"
"time" "time"
"github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common"
@ -31,6 +32,8 @@ import (
"github.com/ethereum/go-ethereum/rlp" "github.com/ethereum/go-ethereum/rlp"
) )
const tempJournalSuffix = ".tmp"
var ( var (
errMissJournal = errors.New("journal not found") errMissJournal = errors.New("journal not found")
errMissVersion = errors.New("version not found") errMissVersion = errors.New("version not found")
@ -51,11 +54,25 @@ const journalVersion uint64 = 3
// loadJournal tries to parse the layer journal from the disk. // loadJournal tries to parse the layer journal from the disk.
func (db *Database) loadJournal(diskRoot common.Hash) (layer, error) { func (db *Database) loadJournal(diskRoot common.Hash) (layer, error) {
var reader io.Reader
if path := db.journalPath(); path != "" && common.FileExist(path) {
// If a journal file is specified, read it from there
log.Info("Load database journal from file", "path", path)
f, err := os.OpenFile(path, os.O_RDONLY, 0644)
if err != nil {
return nil, fmt.Errorf("failed to read journal file %s: %w", path, err)
}
defer f.Close()
reader = f
} else {
log.Info("Load database journal from disk")
journal := rawdb.ReadTrieJournal(db.diskdb) journal := rawdb.ReadTrieJournal(db.diskdb)
if len(journal) == 0 { if len(journal) == 0 {
return nil, errMissJournal return nil, errMissJournal
} }
r := rlp.NewStream(bytes.NewReader(journal), 0) reader = bytes.NewReader(journal)
}
r := rlp.NewStream(reader, 0)
// Firstly, resolve the first element as the journal version // Firstly, resolve the first element as the journal version
version, err := r.Uint64() version, err := r.Uint64()
@ -297,9 +314,9 @@ func (db *Database) Journal(root common.Hash) error {
} }
disk := db.tree.bottom() disk := db.tree.bottom()
if l, ok := l.(*diffLayer); ok { if l, ok := l.(*diffLayer); ok {
log.Info("Persisting dirty state to disk", "head", l.block, "root", root, "layers", l.id-disk.id+disk.buffer.layers) log.Info("Persisting dirty state", "head", l.block, "root", root, "layers", l.id-disk.id+disk.buffer.layers)
} else { // disk layer only on noop runs (likely) or deep reorgs (unlikely) } else { // disk layer only on noop runs (likely) or deep reorgs (unlikely)
log.Info("Persisting dirty state to disk", "root", root, "layers", disk.buffer.layers) log.Info("Persisting dirty state", "root", root, "layers", disk.buffer.layers)
} }
// Block until the background flushing is finished and terminate // Block until the background flushing is finished and terminate
// the potential active state generator. // the potential active state generator.
@ -316,8 +333,37 @@ func (db *Database) Journal(root common.Hash) error {
if db.readOnly { if db.readOnly {
return errDatabaseReadOnly return errDatabaseReadOnly
} }
// Store the journal into the database and return
var (
file *os.File
journal io.Writer
journalPath = db.journalPath()
)
if journalPath != "" {
// Write into a temp file first
err := os.MkdirAll(db.config.JournalDirectory, 0755)
if err != nil {
return err
}
tmp := journalPath + tempJournalSuffix
file, err = os.OpenFile(tmp, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644)
if err != nil {
return fmt.Errorf("failed to open journal file %s: %w", tmp, err)
}
defer func() {
if file != nil {
file.Close()
os.Remove(tmp) // Clean up temp file if we didn't successfully rename it
log.Warn("Removed leftover temporary journal file", "path", tmp)
}
}()
journal = file
} else {
journal = new(bytes.Buffer)
}
// Firstly write out the metadata of journal // Firstly write out the metadata of journal
journal := new(bytes.Buffer)
if err := rlp.Encode(journal, journalVersion); err != nil { if err := rlp.Encode(journal, journalVersion); err != nil {
return err return err
} }
@ -334,11 +380,38 @@ func (db *Database) Journal(root common.Hash) error {
if err := l.journal(journal); err != nil { if err := l.journal(journal); err != nil {
return err return err
} }
// Store the journal into the database and return
rawdb.WriteTrieJournal(db.diskdb, journal.Bytes())
// Store the journal into the database and return
if file == nil {
data := journal.(*bytes.Buffer)
size := data.Len()
rawdb.WriteTrieJournal(db.diskdb, data.Bytes())
log.Info("Persisted dirty state to disk", "size", common.StorageSize(size), "elapsed", common.PrettyDuration(time.Since(start)))
} else {
stat, err := file.Stat()
if err != nil {
return err
}
size := int(stat.Size())
// Close the temporary file and atomically rename it
if err := file.Sync(); err != nil {
return fmt.Errorf("failed to fsync the journal, %v", err)
}
if err := file.Close(); err != nil {
return fmt.Errorf("failed to close the journal: %v", err)
}
// Replace the live journal with the newly generated one
if err := os.Rename(journalPath+tempJournalSuffix, journalPath); err != nil {
return fmt.Errorf("failed to rename the journal: %v", err)
}
if err := syncDir(db.config.JournalDirectory); err != nil {
return fmt.Errorf("failed to fsync the dir: %v", err)
}
file = nil
log.Info("Persisted dirty state to file", "path", journalPath, "size", common.StorageSize(size), "elapsed", common.PrettyDuration(time.Since(start)))
}
// Set the db in read only mode to reject all following mutations // Set the db in read only mode to reject all following mutations
db.readOnly = true db.readOnly = true
log.Info("Persisted dirty state to disk", "size", common.StorageSize(journal.Len()), "elapsed", common.PrettyDuration(time.Since(start)))
return nil return nil
} }