forked from FactomProject/factomd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
stateSaver.go
151 lines (132 loc) · 4.26 KB
/
stateSaver.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
// Copyright 2017 Factom Foundation
// Use of this source code is governed by the MIT
// license that can be found in the LICENSE file.
package state
import (
"errors"
"fmt"
"io/ioutil"
"os"
"sync"
"github.com/FactomProject/factomd/common/constants"
"github.com/FactomProject/factomd/common/primitives"
)
type StateSaverStruct struct {
FastBoot bool
FastBootLocation string
TmpDBHt uint32
TmpState []byte
Mutex sync.Mutex
Stop bool
}
func (sss *StateSaverStruct) StopSaving() {
sss.Mutex.Lock()
defer sss.Mutex.Unlock()
sss.Stop = true
}
func (sss *StateSaverStruct) SaveDBStateList(s *State, ss *DBStateList, networkName string) error {
//For now, to file. Later - to DB
if sss.Stop == true {
return nil // if we have closed the database then don't save
}
//Save only every FastSaveRate states
if int(s.LLeaderHeight)%ss.State.FastSaveRate != 0 || int(s.LLeaderHeight) < ss.State.FastSaveRate {
return nil
}
sss.Mutex.Lock()
defer sss.Mutex.Unlock()
//Actually save data from previous cached state to prevent dealing with rollbacks
// Save the N block old state and then make a new savestate for the next save
if sss.TmpDBHt != ss.State.LLeaderHeight && len(sss.TmpState) > 0 {
filename := NetworkIDToFilename(networkName, sss.FastBootLocation)
s.LogPrintf("executeMsg", "%d-:-%d %20s Saving %s for dbht %d", s.LLeaderHeight, s.CurrentMinute, s.FactomNodeName, filename, sss.TmpDBHt)
err := SaveToFile(s, sss.TmpDBHt, sss.TmpState, filename)
if err != nil {
fmt.Fprintln(os.Stderr, "SaveState SaveToFile Failed", err)
return err
}
}
if sss.TmpDBHt != ss.State.LLeaderHeight {
//Marshal state for future saving
b, err := ss.MarshalBinary()
if err != nil {
fmt.Fprintln(os.Stderr, "SaveState MarshalBinary Failed", err)
return err
}
//adding an integrity check
h := primitives.Sha(b)
b = append(h.Bytes(), b...)
sss.TmpState = b
sss.TmpDBHt = ss.State.LLeaderHeight
}
return nil
}
func (sss *StateSaverStruct) DeleteSaveState(networkName string) error {
return DeleteFile(NetworkIDToFilename(networkName, sss.FastBootLocation))
}
func (sss *StateSaverStruct) LoadDBStateList(s *State, statelist *DBStateList, networkName string) error {
filename := NetworkIDToFilename(networkName, sss.FastBootLocation)
fmt.Println(statelist.State.FactomNodeName, "Loading from", filename)
b, err := LoadFromFile(s, filename)
if err != nil {
fmt.Fprintln(os.Stderr, "LoadDBStateList error:", err)
return err
}
if b == nil {
fmt.Fprintln(os.Stderr, "LoadDBStateList LoadFromFile returned nil")
return errors.New("failed to load from file")
}
h := primitives.NewZeroHash()
b, err = h.UnmarshalBinaryData(b)
if err != nil {
return err
}
h2 := primitives.Sha(b)
if h.IsSameAs(h2) == false {
fmt.Fprintf(os.Stderr, "LoadDBStateList - Integrity hashes do not match!")
return errors.New("fastboot file does not match its hash")
//return fmt.Errorf("Integrity hashes do not match")
}
statelist.UnmarshalBinary(b)
var i int
for i = len(statelist.DBStates) - 1; i >= 0; i-- {
if statelist.DBStates[i].SaveStruct != nil {
break
}
}
statelist.DBStates[i].SaveStruct.RestoreFactomdState(statelist.State)
return nil
}
func NetworkIDToFilename(networkName string, fileLocation string) string {
file := fmt.Sprintf("FastBoot_%s_v%v.db", networkName, constants.SaveStateVersion)
if fileLocation != "" {
// Trim optional trailing / from file path
i := len(fileLocation) - 1
if fileLocation[i] == byte('/') {
fileLocation = fileLocation[:i]
}
return fmt.Sprintf("%v/%v", fileLocation, file)
}
return file
}
func SaveToFile(s *State, dbht uint32, b []byte, filename string) error {
fmt.Fprintf(os.Stderr, "%20s Saving %s for dbht %d\n", s.FactomNodeName, filename, dbht)
err := ioutil.WriteFile(filename, b, 0644)
if err != nil {
fmt.Fprintf(os.Stderr, "%20s Saving FailrueError: %v\n", s.FactomNodeName, err)
return err
}
return nil
}
func LoadFromFile(s *State, filename string) ([]byte, error) {
fmt.Fprintf(os.Stderr, "%20s Load state from %s\n", s.FactomNodeName, filename)
b, err := ioutil.ReadFile(filename)
if err != nil {
fmt.Fprintf(os.Stderr, "%20s LoadFromFile error: %v\n", s.FactomNodeName, err)
return nil, err
}
return b, nil
}
func DeleteFile(filename string) error {
return os.Remove(filename)
}