-
Notifications
You must be signed in to change notification settings - Fork 124
/
statetool.go
78 lines (66 loc) · 1.95 KB
/
statetool.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
package state
import (
"errors"
"fmt"
cfg "github.com/spf13/viper"
pbtypes "github.com/dappledger/AnnChain/angine/protos/types"
dbm "github.com/dappledger/AnnChain/module/lib/go-db"
"github.com/dappledger/AnnChain/module/xlib/def"
)
var (
ErrBranchNameUsed = errors.New("state db:branch name has been used")
ErrStateIsNil = errors.New("the state has no data")
ErrRevertFromBackup = errors.New("state revert from backup, not find data")
)
type StateTool struct {
db dbm.DB
lastState *State
}
func (st *StateTool) Init(config *cfg.Viper) error {
st.db = StateDB(config)
st.lastState = LoadState(nil, st.db)
if st.lastState == nil || st.lastState.LastBlockHeight <= 0 {
return ErrStateIsNil
}
return nil
}
func (st *StateTool) ChangeToIntermidiate() {
}
func (st *StateTool) LastHeight() def.INT {
return st.lastState.LastBlockHeight
}
func (st *StateTool) backupName(branchName string) []byte {
return []byte(fmt.Sprintf("%s-%s", stateKey, branchName))
}
func (st *StateTool) BackupLastState(branchName string) error {
saveKey := st.backupName(branchName)
preBs := st.db.Get(saveKey)
if len(preBs) > 0 {
return ErrBranchNameUsed
}
st.lastState.SaveToKey(saveKey)
// SaveIntermediate()
return nil
}
func (st *StateTool) RevertFromBackup(branchName string) error {
preKeyName := st.backupName(branchName)
bs := st.db.Get(preKeyName)
if len(preKeyName) == 0 {
return ErrRevertFromBackup
}
st.db.Set(stateKey, bs)
return nil
}
func (st *StateTool) DelBackup(branchName string) {
st.db.Delete(st.backupName(branchName))
}
// back to height of lastBlock
func (st *StateTool) SaveNewState(lastBlock *pbtypes.Block, lastBlockMeta *pbtypes.BlockMeta, lastBlockID *pbtypes.BlockID) error {
newState := st.lastState.Copy()
newState.AppHash = lastBlock.Header.AppHash
newState.LastBlockHeight = lastBlock.Header.Height
newState.LastBlockID = *lastBlockID
newState.LastBlockTime = lastBlockMeta.Header.Time
newState.Save()
return nil
}