mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-07-23 05:06:43 +00:00
Refactor journal loading and add setWriter method
Refactor journal load method to use nil writer instead of original writer. Add setWriter method to handle journal file opening for writing new transactions.
This commit is contained in:
parent
397d750851
commit
77de963b5b
1 changed files with 19 additions and 3 deletions
|
|
@ -58,8 +58,6 @@ func newTxJournal(path string) *journal {
|
||||||
// load parses a transaction journal dump from disk, loading its contents into
|
// load parses a transaction journal dump from disk, loading its contents into
|
||||||
// the specified pool.
|
// the specified pool.
|
||||||
func (journal *journal) load(add func([]*types.Transaction) []error) error {
|
func (journal *journal) load(add func([]*types.Transaction) []error) error {
|
||||||
originalWriter := journal.writer
|
|
||||||
|
|
||||||
// Open the journal for loading any past transactions
|
// Open the journal for loading any past transactions
|
||||||
input, err := os.Open(journal.path)
|
input, err := os.Open(journal.path)
|
||||||
if errors.Is(err, fs.ErrNotExist) {
|
if errors.Is(err, fs.ErrNotExist) {
|
||||||
|
|
@ -73,7 +71,7 @@ func (journal *journal) load(add func([]*types.Transaction) []error) error {
|
||||||
|
|
||||||
// Temporarily discard any journal additions (don't double add on load)
|
// Temporarily discard any journal additions (don't double add on load)
|
||||||
journal.writer = new(devNull)
|
journal.writer = new(devNull)
|
||||||
defer func() { journal.writer = originalWriter }()
|
defer func() { journal.writer = nil }()
|
||||||
|
|
||||||
// Inject all transactions from the journal into the pool
|
// Inject all transactions from the journal into the pool
|
||||||
stream := rlp.NewStream(input, 0)
|
stream := rlp.NewStream(input, 0)
|
||||||
|
|
@ -119,6 +117,24 @@ func (journal *journal) load(add func([]*types.Transaction) []error) error {
|
||||||
return failure
|
return failure
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// setWriter opens the journal file for writing new transactions.
|
||||||
|
func (journal *journal) setWriter() error {
|
||||||
|
// Close the current writer if any
|
||||||
|
if journal.writer != nil {
|
||||||
|
journal.writer.Close()
|
||||||
|
journal.writer = nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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 {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
journal.writer = sink
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
// insert adds the specified transaction to the local disk journal.
|
// insert adds the specified transaction to the local disk journal.
|
||||||
func (journal *journal) insert(tx *types.Transaction) error {
|
func (journal *journal) insert(tx *types.Transaction) error {
|
||||||
if journal.writer == nil {
|
if journal.writer == nil {
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue