@@ -8,11 +8,22 @@ import (
88)
99
1010type 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
1829func (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
7183func (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