This removes the memory block node (header) pruning. It should be noted that this does not apply to stake node pruning. This is being done for two primary reasons: - The goal is to ultimately have all block nodes in memory in the same way the upstream code has done since it provides significant optimization and code simplification opportunities - Upcoming code that deals with calculating sequence locks on inputs far in the past requires the ability to quickly calculate the median time for arbitrarily old nodes and consequently pruning the memory block nodes could lead to significant performance implications under those conditions
40 lines
1.1 KiB
Go
40 lines
1.1 KiB
Go
package blockchain
|
|
|
|
import (
|
|
"time"
|
|
)
|
|
|
|
// pruningIntervalInMinutes is the interval in which to prune the blockchain's
|
|
// nodes and restore memory to the garbage collector.
|
|
const pruningIntervalInMinutes = 5
|
|
|
|
// chainPruner is used to occasionally prune the blockchain of old nodes that
|
|
// can be freed to the garbage collector.
|
|
type chainPruner struct {
|
|
chain *BlockChain
|
|
lastNodeInsertTime time.Time
|
|
}
|
|
|
|
// newChainPruner returns a new chain pruner.
|
|
func newChainPruner(chain *BlockChain) *chainPruner {
|
|
return &chainPruner{
|
|
chain: chain,
|
|
lastNodeInsertTime: time.Now(),
|
|
}
|
|
}
|
|
|
|
// pruneChainIfNeeded checks the current time versus the time of the last pruning.
|
|
// If the blockchain hasn't been pruned in this time, it initiates a new pruning.
|
|
//
|
|
// pruneChainIfNeeded must be called with the chainLock held for writes.
|
|
func (c *chainPruner) pruneChainIfNeeded() {
|
|
now := time.Now()
|
|
duration := now.Sub(c.lastNodeInsertTime)
|
|
if duration < time.Minute*pruningIntervalInMinutes {
|
|
return
|
|
}
|
|
|
|
c.lastNodeInsertTime = now
|
|
c.chain.pruneStakeNodes()
|
|
}
|