core/txpool/locals: fix the writer setup

This commit is contained in:
Gary Rong 2025-10-21 09:17:56 +08:00
parent 0a8a981a58
commit dba43fcdd2
2 changed files with 23 additions and 2 deletions

View file

@ -112,8 +112,18 @@ func (journal *journal) load(add func([]*types.Transaction) []error) error {
}
}
log.Info("Loaded local transaction journal", "transactions", total, "dropped", dropped)
return failure
}
// Open the journal file for appending
func (journal *journal) setupWriter() error {
if journal.writer != nil {
if err := journal.writer.Close(); err != nil {
return err
}
journal.writer = nil
}
// Re-open the journal file for appending
// Use O_APPEND to ensure we always write to the end of the file
sink, err := os.OpenFile(journal.path, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0644)
if err != nil {
@ -121,7 +131,7 @@ func (journal *journal) load(add func([]*types.Transaction) []error) error {
}
journal.writer = sink
return failure
return nil
}
// insert adds the specified transaction to the local disk journal.

View file

@ -185,6 +185,17 @@ func (tracker *TxTracker) loop() {
tracker.TrackAll(transactions)
return nil
})
// Setup the writer for the upcoming transactions. Lock to prevent
// journal.setupWriter <-> journal.insert (via TrackAll) conflicts.
tracker.mu.Lock()
if err := tracker.journal.setupWriter(); err != nil {
log.Error("Failed to setup the journal writer", "err", err)
tracker.mu.Unlock()
return
}
tracker.mu.Unlock()
defer tracker.journal.close()
}
var (