MCPcopy Create free account
hub / github.com/CPChain/chain / load

Method load

core/tx_journal.go:59–117  ·  view source on GitHub ↗

load parses a transaction journal dump from disk, loading its contents into the specified pool.

(add func([]*types.Transaction) []error)

Source from the content-addressed store, hash-verified

57// load parses a transaction journal dump from disk, loading its contents into
58// the specified pool.
59func (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 // Create a method to load a limited batch of transactions and bump the
80 // appropriate progress counters. Then use this method to load all the
81 // journalled transactions in small-ish batches.
82 loadBatch := func(txs types.Transactions) {
83 for _, err := range add(txs) {
84 if err != nil {
85 log.Debug("Failed to add journaled transaction", "err", err)
86 dropped++
87 }
88 }
89 }
90 var (
91 failure error
92 batch types.Transactions
93 )
94 for {
95 // Parse the next transaction and terminate on error
96 tx := new(types.Transaction)
97 if err = stream.Decode(tx); err != nil {
98 if err != io.EOF {
99 failure = err
100 }
101 if batch.Len() > 0 {
102 loadBatch(batch)
103 }
104 break
105 }
106 // New transaction parsed, queue up for later, import if threnshold is reached
107 total++
108
109 if batch = append(batch, tx); batch.Len() > 1024 {
110 loadBatch(batch)
111 batch = batch[:0]
112 }
113 }
114 log.Info("Loaded local transaction journal", "transactions", total, "dropped", dropped)
115
116 return failure

Callers 1

NewTxPoolFunction · 0.80

Calls 5

DebugMethod · 0.80
OpenMethod · 0.65
CloseMethod · 0.65
LenMethod · 0.65
InfoMethod · 0.65

Tested by

no test coverage detected