github.com/aquanetwork/aquachain@v1.7.8/core/tx_journal.go (about)

     1  // Copyright 2017 The aquachain Authors
     2  // This file is part of the aquachain library.
     3  //
     4  // The aquachain library is free software: you can redistribute it and/or modify
     5  // it under the terms of the GNU Lesser General Public License as published by
     6  // the Free Software Foundation, either version 3 of the License, or
     7  // (at your option) any later version.
     8  //
     9  // The aquachain library is distributed in the hope that it will be useful,
    10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    12  // GNU Lesser General Public License for more details.
    13  //
    14  // You should have received a copy of the GNU Lesser General Public License
    15  // along with the aquachain library. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  package core
    18  
    19  import (
    20  	"errors"
    21  	"io"
    22  	"os"
    23  
    24  	"gitlab.com/aquachain/aquachain/common"
    25  	"gitlab.com/aquachain/aquachain/common/log"
    26  	"gitlab.com/aquachain/aquachain/core/types"
    27  	"gitlab.com/aquachain/aquachain/rlp"
    28  )
    29  
    30  // errNoActiveJournal is returned if a transaction is attempted to be inserted
    31  // into the journal, but no such file is currently open.
    32  var errNoActiveJournal = errors.New("no active journal")
    33  
    34  // devNull is a WriteCloser that just discards anything written into it. Its
    35  // goal is to allow the transaction journal to write into a fake journal when
    36  // loading transactions on startup without printing warnings due to no file
    37  // being readt for write.
    38  type devNull struct{}
    39  
    40  func (*devNull) Write(p []byte) (n int, err error) { return len(p), nil }
    41  func (*devNull) Close() error                      { return nil }
    42  
    43  // txJournal is a rotating log of transactions with the aim of storing locally
    44  // created transactions to allow non-executed ones to survive node restarts.
    45  type txJournal struct {
    46  	path   string         // Filesystem path to store the transactions at
    47  	writer io.WriteCloser // Output stream to write new transactions into
    48  }
    49  
    50  // newTxJournal creates a new transaction journal to
    51  func newTxJournal(path string) *txJournal {
    52  	return &txJournal{
    53  		path: path,
    54  	}
    55  }
    56  
    57  // load parses a transaction journal dump from disk, loading its contents into
    58  // the specified pool.
    59  func (journal *txJournal) load(add func(*types.Transaction) error) error {
    60  	// Skip the parsing if the journal file doens't exist at all
    61  	if _, err := os.Stat(journal.path); os.IsNotExist(err) {
    62  		return nil
    63  	}
    64  	// Open the journal for loading any past transactions
    65  	input, err := os.Open(journal.path)
    66  	if err != nil {
    67  		return err
    68  	}
    69  	defer input.Close()
    70  
    71  	// Temporarily discard any journal additions (don't double add on load)
    72  	journal.writer = new(devNull)
    73  	defer func() { journal.writer = nil }()
    74  
    75  	// Inject all transactions from the journal into the pool
    76  	stream := rlp.NewStream(input, 0)
    77  	total, dropped := 0, 0
    78  
    79  	var failure error
    80  	for {
    81  		// Parse the next transaction and terminate on error
    82  		tx := new(types.Transaction)
    83  		if err = stream.Decode(tx); err != nil {
    84  			if err != io.EOF {
    85  				failure = err
    86  			}
    87  			break
    88  		}
    89  		// Import the transaction and bump the appropriate progress counters
    90  		total++
    91  		if err = add(tx); err != nil {
    92  			log.Debug("Failed to add journaled transaction", "err", err)
    93  			dropped++
    94  			continue
    95  		}
    96  	}
    97  	log.Info("Loaded local transaction journal", "transactions", total, "dropped", dropped)
    98  
    99  	return failure
   100  }
   101  
   102  // insert adds the specified transaction to the local disk journal.
   103  func (journal *txJournal) insert(tx *types.Transaction) error {
   104  	if journal.writer == nil {
   105  		return errNoActiveJournal
   106  	}
   107  	if err := rlp.Encode(journal.writer, tx); err != nil {
   108  		return err
   109  	}
   110  	return nil
   111  }
   112  
   113  // rotate regenerates the transaction journal based on the current contents of
   114  // the transaction pool.
   115  func (journal *txJournal) rotate(all map[common.Address]types.Transactions) error {
   116  	// Close the current journal (if any is open)
   117  	if journal.writer != nil {
   118  		if err := journal.writer.Close(); err != nil {
   119  			return err
   120  		}
   121  		journal.writer = nil
   122  	}
   123  	// Generate a new journal with the contents of the current pool
   124  	replacement, err := os.OpenFile(journal.path+".new", os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0755)
   125  	if err != nil {
   126  		return err
   127  	}
   128  	journaled := 0
   129  	for _, txs := range all {
   130  		for _, tx := range txs {
   131  			if err = rlp.Encode(replacement, tx); err != nil {
   132  				replacement.Close()
   133  				return err
   134  			}
   135  		}
   136  		journaled += len(txs)
   137  	}
   138  	replacement.Close()
   139  
   140  	// Replace the live journal with the newly generated one
   141  	if err = os.Rename(journal.path+".new", journal.path); err != nil {
   142  		return err
   143  	}
   144  	sink, err := os.OpenFile(journal.path, os.O_WRONLY|os.O_APPEND, 0755)
   145  	if err != nil {
   146  		return err
   147  	}
   148  	journal.writer = sink
   149  	log.Info("Regenerated local transaction journal", "transactions", journaled, "accounts", len(all))
   150  
   151  	return nil
   152  }
   153  
   154  // close flushes the transaction journal contents to disk and closes the file.
   155  func (journal *txJournal) close() error {
   156  	var err error
   157  
   158  	if journal.writer != nil {
   159  		err = journal.writer.Close()
   160  		journal.writer = nil
   161  	}
   162  	return err
   163  }