/
config.go
89 lines (73 loc) · 2.26 KB
/
config.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
package convert
import (
"encoding/json"
"io/ioutil"
"log"
"github.com/commis/tm-tools/libs/op/hold"
otp "github.com/commis/tm-tools/oldver/types"
"github.com/tendermint/tendermint/privval"
"github.com/tendermint/tendermint/types"
cmn "github.com/tendermint/tmlibs/common"
dbm "github.com/tendermint/tmlibs/db"
)
func UpgradeGenesisJSON(oFile, nFile string) string {
jsonBytes, err := ioutil.ReadFile(oFile)
if err != nil {
cmn.Exit(err.Error())
}
oGen, err := otp.GenesisDocFromJSON(jsonBytes)
if err == nil {
nGen := toNewGenesisDoc(oGen)
if err := nGen.SaveAs(nFile); err != nil {
cmn.Exit(err.Error())
}
return nGen.ChainID
}
return ""
}
func UpgradePrivValidatorJson(oFile, nFile string) (string, *privval.FilePV) {
old := otp.LoadPrivValidator(oFile)
newPrivVal := privval.GenFilePV(nFile)
/*后续根据cs.wal消息更新*/
newPrivVal.LastHeight = old.LastHeight
newPrivVal.LastRound = old.LastRound
newPrivVal.LastStep = old.LastStep
newPrivVal.LastSignature = CvtNewSignature(old.LastSignature)
newPrivVal.LastSignBytes = old.LastSignBytes.Bytes()
newPrivVal.PubKey = CvtNewPubKey(old.PubKey)
newPrivVal.PrivKey = CvtNewPrivKey(old.PrivKey)
newPrivVal.Address = newPrivVal.PubKey.Address()
newPrivVal.Save()
return old.Address.String(), newPrivVal
}
func LoadOldGenesisDoc(db dbm.DB) *otp.GenesisDoc {
bytes := db.Get([]byte(hold.GenesisDoc))
if len(bytes) == 0 {
return nil
}
var genDoc *otp.GenesisDoc
err := json.Unmarshal(bytes, &genDoc)
if err != nil {
log.Printf("Failed to load genesis doc due to unmarshaling error: %v (bytes: %X)", err, bytes)
}
return genDoc
}
func toNewGenesisDoc(old *otp.GenesisDoc) *types.GenesisDoc {
newGenesisDoc := new(types.GenesisDoc)
newGenesisDoc.AppHash = old.AppHash.Bytes()
newGenesisDoc.ChainID = old.ChainID
newGenesisDoc.GenesisTime = old.GenesisTime
newGenesisDoc.Validators = []types.GenesisValidator{}
// 修正老版本错误的时间
/*if newGenesisDoc.GenesisTime.IsZero() {
newGenesisDoc.GenesisTime = time.Now().UTC()
}*/
for _, val := range old.Validators {
one := types.GenesisValidator{}
one.Power = val.Power
one.Name = val.Name
one.PubKey = CvtNewPubKey(val.PubKey)
newGenesisDoc.Validators = append(newGenesisDoc.Validators, one)
}
return newGenesisDoc
}