forked from ava-labs/spacesvm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
compactor.go
67 lines (53 loc) · 1.47 KB
/
compactor.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
// Copyright (C) 2019-2021, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package vm
import (
"time"
log "github.com/inconshreveable/log15"
"github.com/MetalBlockchain/spacesvm/chain"
)
func (vm *VM) compactCall(r *chain.CompactRange) {
// Lock to prevent concurrent modification of state
vm.ctx.Lock.Lock()
defer vm.ctx.Lock.Unlock()
start := time.Now()
if err := vm.db.Compact(r.Start, r.Limit); err != nil {
log.Error("unable to compact range", "start", r.Start, "stop", r.Limit)
return
}
log.Debug("compacted range", "start", r.Start, "stop", r.Limit, "t", time.Since(start))
// Make sure to update children or else won't be persisted
if err := vm.lastAccepted.SetChildrenDB(vm.db); err != nil {
log.Error("unable to update child databases of last accepted block", "error", err)
return
}
}
func (vm *VM) compact() {
log.Debug("starting compaction loops")
defer close(vm.doneCompact)
t := time.NewTimer(vm.config.CompactInterval)
defer t.Stop()
// Ensure there is something to compact
ranges := chain.CompactRanges
currentRange := 0
if len(ranges) == 0 {
log.Debug("exiting compactor because nothing to compact")
return
}
for {
select {
case <-t.C:
case <-vm.stop:
return
}
// Compact next range
prefix := ranges[currentRange]
vm.compactCall(prefix)
// Update range compaction index
currentRange++
if currentRange > len(ranges)-1 {
currentRange = 0
}
t.Reset(vm.config.CompactInterval)
}
}