2022-02-18 11:12:53 +00:00
|
|
|
package snapshot
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/csv"
|
|
|
|
"fmt"
|
|
|
|
"os"
|
2022-03-11 14:28:32 +00:00
|
|
|
"os/signal"
|
|
|
|
"syscall"
|
2022-02-18 11:12:53 +00:00
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/core/state"
|
|
|
|
"github.com/ethereum/go-ethereum/trie"
|
2022-03-11 14:28:32 +00:00
|
|
|
log "github.com/sirupsen/logrus"
|
2022-02-18 11:12:53 +00:00
|
|
|
|
|
|
|
iter "github.com/vulcanize/go-eth-state-node-iterator"
|
|
|
|
)
|
|
|
|
|
|
|
|
type trackedIter struct {
|
|
|
|
trie.NodeIterator
|
|
|
|
tracker *iteratorTracker
|
|
|
|
}
|
|
|
|
|
|
|
|
func (it *trackedIter) Next(descend bool) bool {
|
|
|
|
ret := it.NodeIterator.Next(descend)
|
|
|
|
if !ret {
|
2022-06-08 12:02:43 +00:00
|
|
|
if it.tracker.running {
|
|
|
|
it.tracker.stopChan <- it
|
|
|
|
} else {
|
|
|
|
log.Errorf("iterator stopped after tracker halted: path=%x", it.Path())
|
|
|
|
}
|
2022-02-18 11:12:53 +00:00
|
|
|
}
|
|
|
|
return ret
|
|
|
|
}
|
|
|
|
|
|
|
|
type iteratorTracker struct {
|
2022-03-11 14:28:32 +00:00
|
|
|
recoveryFile string
|
|
|
|
|
2022-02-18 11:12:53 +00:00
|
|
|
startChan chan *trackedIter
|
|
|
|
stopChan chan *trackedIter
|
|
|
|
started map[*trackedIter]struct{}
|
|
|
|
stopped []*trackedIter
|
2022-06-08 12:02:43 +00:00
|
|
|
running bool
|
2022-02-18 11:12:53 +00:00
|
|
|
}
|
|
|
|
|
2022-03-11 14:28:32 +00:00
|
|
|
func newTracker(file string, buf int) iteratorTracker {
|
2022-02-18 11:12:53 +00:00
|
|
|
return iteratorTracker{
|
2022-03-11 14:28:32 +00:00
|
|
|
recoveryFile: file,
|
|
|
|
startChan: make(chan *trackedIter, buf),
|
|
|
|
stopChan: make(chan *trackedIter, buf),
|
|
|
|
started: map[*trackedIter]struct{}{},
|
2022-06-08 12:02:43 +00:00
|
|
|
running: true,
|
2022-02-18 11:12:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-11 14:28:32 +00:00
|
|
|
func (tr *iteratorTracker) captureSignal() {
|
|
|
|
sigChan := make(chan os.Signal, 1)
|
|
|
|
|
2022-03-16 10:48:51 +00:00
|
|
|
signal.Notify(sigChan, syscall.SIGINT, syscall.SIGTERM)
|
2022-03-11 14:28:32 +00:00
|
|
|
go func() {
|
|
|
|
sig := <-sigChan
|
|
|
|
log.Errorf("Signal received (%v), stopping", sig)
|
|
|
|
tr.haltAndDump()
|
|
|
|
os.Exit(1)
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
2022-06-08 12:02:43 +00:00
|
|
|
// Wraps an iterator in a trackedIter. This should not be called once halts are possible.
|
2022-02-18 11:12:53 +00:00
|
|
|
func (tr *iteratorTracker) tracked(it trie.NodeIterator) (ret *trackedIter) {
|
|
|
|
ret = &trackedIter{it, tr}
|
|
|
|
tr.startChan <- ret
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// dumps iterator path and bounds to a text file so it can be restored later
|
2022-03-11 14:28:32 +00:00
|
|
|
func (tr *iteratorTracker) dump() error {
|
2022-04-04 19:35:18 +00:00
|
|
|
log.Info("Dumping recovery state to: ", tr.recoveryFile)
|
2022-02-18 11:12:53 +00:00
|
|
|
var rows [][]string
|
|
|
|
for it, _ := range tr.started {
|
|
|
|
var endPath []byte
|
|
|
|
if impl, ok := it.NodeIterator.(*iter.PrefixBoundIterator); ok {
|
|
|
|
endPath = impl.EndPath
|
|
|
|
}
|
|
|
|
rows = append(rows, []string{
|
|
|
|
fmt.Sprintf("%x", it.Path()),
|
|
|
|
fmt.Sprintf("%x", endPath),
|
|
|
|
})
|
|
|
|
}
|
2022-03-11 14:28:32 +00:00
|
|
|
file, err := os.Create(tr.recoveryFile)
|
2022-02-18 11:12:53 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer file.Close()
|
|
|
|
out := csv.NewWriter(file)
|
|
|
|
return out.WriteAll(rows)
|
|
|
|
}
|
|
|
|
|
|
|
|
// attempts to read iterator state from file
|
|
|
|
// if file doesn't exist, returns an empty slice with no error
|
2022-03-11 14:28:32 +00:00
|
|
|
func (tr *iteratorTracker) restore(tree state.Trie) ([]trie.NodeIterator, error) {
|
|
|
|
file, err := os.Open(tr.recoveryFile)
|
2022-02-18 11:12:53 +00:00
|
|
|
if err != nil {
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer file.Close()
|
|
|
|
in := csv.NewReader(file)
|
|
|
|
in.FieldsPerRecord = 2
|
|
|
|
rows, err := in.ReadAll()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
var ret []trie.NodeIterator
|
|
|
|
for _, row := range rows {
|
|
|
|
// pick up where each interval left off
|
|
|
|
var paths [2][]byte
|
|
|
|
for i, val := range row {
|
|
|
|
if len(val) != 0 {
|
|
|
|
if _, err = fmt.Sscanf(val, "%x", &paths[i]); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-31 15:34:02 +00:00
|
|
|
// Force the lower bound path to an even length
|
|
|
|
if len(paths[0])&0b1 == 1 {
|
2022-06-06 12:08:38 +00:00
|
|
|
decrementPath(paths[0]) // decrement first to avoid skipped nodes
|
2022-05-31 15:34:02 +00:00
|
|
|
paths[0] = append(paths[0], 0)
|
|
|
|
}
|
|
|
|
it := iter.NewPrefixBoundIterator(tree.NodeIterator(iter.HexToKeyBytes(paths[0])), paths[1])
|
2022-02-18 11:12:53 +00:00
|
|
|
ret = append(ret, tr.tracked(it))
|
|
|
|
}
|
|
|
|
return ret, nil
|
|
|
|
}
|
|
|
|
|
2022-03-11 14:28:32 +00:00
|
|
|
func (tr *iteratorTracker) haltAndDump() error {
|
2022-06-08 12:02:43 +00:00
|
|
|
tr.running = false
|
2022-02-18 11:12:53 +00:00
|
|
|
|
|
|
|
// drain any pending events
|
|
|
|
close(tr.startChan)
|
|
|
|
for start := range tr.startChan {
|
|
|
|
tr.started[start] = struct{}{}
|
|
|
|
}
|
|
|
|
close(tr.stopChan)
|
|
|
|
for stop := range tr.stopChan {
|
|
|
|
tr.stopped = append(tr.stopped, stop)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, stop := range tr.stopped {
|
|
|
|
delete(tr.started, stop)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(tr.started) == 0 {
|
|
|
|
// if the tracker state is empty, erase any existing recovery file
|
2022-03-11 14:28:32 +00:00
|
|
|
err := os.Remove(tr.recoveryFile)
|
2022-02-18 11:12:53 +00:00
|
|
|
if os.IsNotExist(err) {
|
|
|
|
err = nil
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
2022-03-11 14:28:32 +00:00
|
|
|
return tr.dump()
|
2022-02-18 11:12:53 +00:00
|
|
|
}
|