Skip to content

Commit 8921133

Browse files
committed
core, ethdb: polish code
1 parent a64a85c commit 8921133

File tree

4 files changed

+22
-10
lines changed

4 files changed

+22
-10
lines changed

core/headerchain.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -620,7 +620,8 @@ func (hc *HeaderChain) setHead(headBlock uint64, headTime uint64, updateFn Updat
620620
// This step must be performed after synchronizing the key-value store;
621621
// otherwise, in the event of a panic, it's theoretically possible to
622622
// lose recent key-value store writes while the ancient store deletions
623-
// remain, leading to data inconsistency.
623+
// remain, leading to data inconsistency, e.g., the gap between the key
624+
// value store and ancient can be created due to unclean shutdown.
624625
if delFn != nil {
625626
// Ignore the error here since light client won't hit this path
626627
frozen, _ := hc.chainDb.Ancients()

ethdb/leveldb/leveldb.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -329,7 +329,8 @@ func (db *Database) Path() string {
329329
func (db *Database) Sync() error {
330330
// In theory, the WAL (Write-Ahead Log) can be explicitly synchronized using
331331
// a write operation with SYNC=true. However, there is no dedicated key reserved
332-
// for this purpose, and even a nil key (key=nil) is considered a valid database entry.
332+
// for this purpose, and even a nil key (key=nil) is considered a valid
333+
// database entry.
333334
//
334335
// In LevelDB, writes are blocked until the data is written to the WAL, meaning
335336
// recent writes won't be lost unless a power failure or system crash occurs.

ethdb/pebble/pebble.go

+13-6
Original file line numberDiff line numberDiff line change
@@ -182,10 +182,18 @@ func New(file string, cache int, handles int, namespace string, readonly bool) (
182182
memTableSize = maxMemTableSize - 1
183183
}
184184
db := &Database{
185-
fn: file,
186-
log: logger,
187-
quitChan: make(chan chan error),
188-
writeOptions: &pebble.WriteOptions{Sync: false},
185+
fn: file,
186+
log: logger,
187+
quitChan: make(chan chan error),
188+
189+
// Use asynchronous write mode by default. Otherwise, the overhead of frequent fsync
190+
// operations can be significant, especially on platforms with slow fsync performance
191+
// (e.g., macOS) or less capable SSDs.
192+
//
193+
// Note that enabling async writes means recent data may be lost in the event of an
194+
// application-level panic (writes will also be lost on a machine-level failure,
195+
// of course). Geth is expected to handle recovery from an unclean shutdown.
196+
writeOptions: pebble.NoSync,
189197
}
190198
opt := &pebble.Options{
191199
// Pebble has a single combined cache area and the write
@@ -426,12 +434,11 @@ func (d *Database) Path() string {
426434
// Sync flushes all pending writes in the write-ahead-log to disk, ensuring
427435
// data durability up to that point.
428436
func (d *Database) Sync() error {
429-
b := d.db.NewBatch()
430-
431437
// The entry (value=nil) is not written to the database; it is only
432438
// added to the WAL. Writing this special log entry in sync mode
433439
// automatically flushes all previous writes, ensuring database
434440
// durability up to this point.
441+
b := d.db.NewBatch()
435442
b.LogData(nil, nil)
436443
return d.db.Apply(b, pebble.Sync)
437444
}

triedb/pathdb/buffer.go

+5-2
Original file line numberDiff line numberDiff line change
@@ -135,8 +135,11 @@ func (b *buffer) flush(db ethdb.KeyValueStore, freezer ethdb.AncientWriter, node
135135
start = time.Now()
136136
batch = db.NewBatchWithSize(b.nodes.dbsize() * 11 / 10) // extra 10% for potential pebble internal stuff
137137
)
138-
// Explicitly sync the state freezer, ensuring that all written
139-
// data is transferred to disk before updating the key-value store.
138+
// Explicitly sync the state freezer to ensure all written data is persisted to disk
139+
// before updating the key-value store.
140+
//
141+
// This step is crucial to guarantee that the corresponding state history remains
142+
// available for state rollback.
140143
if freezer != nil {
141144
if err := freezer.SyncAncient(); err != nil {
142145
return err

0 commit comments

Comments
 (0)