Skip to content

Commit 1b25303

Browse files
committed
minor improvement
1 parent bfa1187 commit 1b25303

File tree

3 files changed

+43
-10
lines changed

3 files changed

+43
-10
lines changed

cmd/gen_start_web.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,7 @@ func GetGenStartWebCmd() *cobra.Command {
256256
sb.WriteString(" --")
257257
sb.WriteString(flagChainDescription)
258258
sb.WriteString(" '")
259-
sb.WriteString(chainDescription)
259+
sb.WriteString(strings.ReplaceAll(chainDescription, "\\n", "\\\\n"))
260260
sb.WriteString("'")
261261
}
262262
{

cmd/node/prune_data.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ func GetPruneNodeDataCmd() *cobra.Command {
130130
return
131131
}
132132

133-
if !pvs.Equals(pvsBackup) {
133+
if !pvs.Equals(*pvsBackup) {
134134
utils.ExitWithErrorMsg("ERR: backup file at", backupPrivValStateJson, "has different content with", filePathPrivValState)
135135
return
136136
}
@@ -158,7 +158,7 @@ func GetPruneNodeDataCmd() *cobra.Command {
158158
utils.ExitWithErrorMsg("ERR: failed to load additional backup file", additionalBackupFile, ":", err)
159159
return
160160
}
161-
if !pvs.Equals(backupPsv2) {
161+
if !pvs.Equals(*backupPsv2) {
162162
utils.ExitWithErrorMsg("ERR: additional backup file at", additionalBackupFile, "has different content with", filePathPrivValState)
163163
return
164164
}
@@ -199,7 +199,7 @@ func GetPruneNodeDataCmd() *cobra.Command {
199199
return
200200
}
201201

202-
if !pvs.Equals(pvsBackup) {
202+
if !pvs.Equals(*pvsBackup) {
203203
utils.ExitWithErrorMsg("ERR: content of", fileNamePrivValState, "is changed after additional backup file created")
204204
return
205205
}

types/priv_validator_state.go

Lines changed: 39 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,22 @@ import (
88
)
99

1010
type PrivateValidatorState struct {
11+
originBz []byte
1112
Height string `json:"height"`
1213
Round int `json:"round"`
1314
Step int `json:"step"`
14-
Signature string `json:"signature"`
15-
SignBytes string `json:"signbytes"`
15+
Signature string `json:"signature,omitempty"`
16+
SignBytes string `json:"signbytes,omitempty"`
17+
}
18+
19+
func NewEmptyPrivateValidatorState() PrivateValidatorState {
20+
return PrivateValidatorState{
21+
Height: "0",
22+
Round: 0,
23+
Step: 0,
24+
Signature: "",
25+
SignBytes: "",
26+
}
1627
}
1728

1829
func (pvs PrivateValidatorState) IsEmpty() bool {
@@ -23,15 +34,15 @@ func (pvs PrivateValidatorState) IsEmpty() bool {
2334
pvs.SignBytes == ""
2435
}
2536

26-
func (pvs *PrivateValidatorState) Equals(other *PrivateValidatorState) bool {
37+
func (pvs PrivateValidatorState) Equals(other PrivateValidatorState) bool {
2738
return pvs.Height == other.Height &&
2839
pvs.Round == other.Round &&
2940
pvs.Step == other.Step &&
3041
pvs.Signature == other.Signature &&
3142
pvs.SignBytes == other.SignBytes
3243
}
3344

34-
func (pvs *PrivateValidatorState) CompareState(other *PrivateValidatorState) (cmp int, differentSigns bool) {
45+
func (pvs PrivateValidatorState) CompareState(other PrivateValidatorState) (cmp int, differentSigns bool) {
3546
var heightPvs, heightOther int64
3647
var err error
3748
if pvs.Height != "0" {
@@ -61,11 +72,12 @@ func (pvs *PrivateValidatorState) CompareState(other *PrivateValidatorState) (cm
6172

6273
if pvs.Step < other.Step {
6374
return -1, true
64-
} else {
75+
} else if pvs.Step > other.Step {
6576
return 1, true
6677
}
6778

68-
return 0, pvs.Signature == other.Signature && pvs.SignBytes == other.SignBytes
79+
sameSign := pvs.Signature == other.Signature && pvs.SignBytes == other.SignBytes
80+
return 0, !sameSign
6981
}
7082

7183
func (pvs *PrivateValidatorState) LoadFromJSONFile(filePath string) error {
@@ -81,5 +93,26 @@ func (pvs *PrivateValidatorState) LoadFromJSON(bz []byte) error {
8193
if err != nil {
8294
return errors.Wrap(err, "failed to unmarshal JSON")
8395
}
96+
pvs.originBz = bz
8497
return nil
8598
}
99+
100+
func (pvs *PrivateValidatorState) SaveToJSONFile(filePath string) error {
101+
jsonStr := pvs.Json()
102+
err := os.WriteFile(filePath, []byte(jsonStr), 0644)
103+
if err != nil {
104+
return errors.Wrap(err, "failed to write file")
105+
}
106+
return nil
107+
}
108+
109+
func (pvs PrivateValidatorState) Json() string {
110+
if len(pvs.originBz) > 0 {
111+
return string(pvs.originBz)
112+
}
113+
bz, err := json.MarshalIndent(pvs, "", " ")
114+
if err != nil {
115+
panic(errors.Wrap(err, "failed to marshal JSON"))
116+
}
117+
return string(bz)
118+
}

0 commit comments

Comments
 (0)