Skip to content

Commit e9bc306

Browse files
authored
xpoa: parameter version works compatibly with int and string type (#286)
* xpoa: parameter version works compatibly with int and string type.
1 parent f35d472 commit e9bc306

4 files changed

Lines changed: 66 additions & 11 deletions

File tree

bcs/consensus/xpoa/common.go

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package xpoa
33
import (
44
"encoding/json"
55
"errors"
6+
"strconv"
67
)
78

89
var (
@@ -30,7 +31,6 @@ const (
3031
)
3132

3233
type xpoaConfig struct {
33-
Version string `json:"version,omitempty"`
3434
// 每个候选人每轮出块个数
3535
BlockNum int64 `json:"block_num"`
3636
// 单位为毫秒
@@ -103,3 +103,28 @@ func (a aksSlice) Less(i, j int) bool {
103103
}
104104
return a[j].Weight < a[i].Weight
105105
}
106+
107+
type xpoaStringConfig struct {
108+
Version string `json:"version,omitempty"`
109+
}
110+
111+
type xpoaIntConfig struct {
112+
Version int64 `json:"version,omitempty"`
113+
}
114+
115+
// ParseVersion 支持string格式和int格式的version type
116+
func ParseVersion(cfg string) (int64, error) {
117+
intVersion := xpoaIntConfig{}
118+
if err := json.Unmarshal([]byte(cfg), &intVersion); err == nil {
119+
return intVersion.Version, nil
120+
}
121+
strVersion := xpoaStringConfig{}
122+
if err := json.Unmarshal([]byte(cfg), &strVersion); err != nil {
123+
return 0, err
124+
}
125+
version, err := strconv.ParseInt(strVersion.Version, 10, 64)
126+
if err != nil {
127+
return 0, err
128+
}
129+
return version, nil
130+
}

bcs/consensus/xpoa/common_test.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,3 +42,40 @@ func TestLoadValidatorsMultiInfo(t *testing.T) {
4242
t.Error("TestLoadValidatorsMultiInfo error 2.")
4343
}
4444
}
45+
46+
func TestParseVersion(t *testing.T) {
47+
strVersion := `{
48+
"version": "2"
49+
}`
50+
v, err := ParseVersion(strVersion)
51+
if err != nil {
52+
t.Error("ParseVersion err, err: ", err)
53+
return
54+
}
55+
if v != 2 {
56+
t.Error("ParseVersion err, v: ", v)
57+
return
58+
}
59+
intVersion := `{
60+
"version": 3
61+
}`
62+
v, err = ParseVersion(intVersion)
63+
if err != nil {
64+
t.Error("ParseVersion err, err: ", err)
65+
return
66+
}
67+
if v != 3 {
68+
t.Error("ParseVersion err, v: ", v)
69+
return
70+
}
71+
empryVersion := `{}`
72+
v, err = ParseVersion(empryVersion)
73+
if err != nil {
74+
t.Error("ParseVersion err, err: ", err)
75+
return
76+
}
77+
if v != 0 {
78+
t.Error("ParseVersion err, v: ", v)
79+
return
80+
}
81+
}

bcs/consensus/xpoa/schedule.go

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package xpoa
22

33
import (
44
"fmt"
5-
"strconv"
65
"time"
76

87
common "github.com/xuperchain/xupercore/kernel/consensus/base/common"
@@ -35,12 +34,7 @@ type xpoaSchedule struct {
3534
ledger cctx.LedgerRely
3635
}
3736

38-
func NewXpoaSchedule(xconfig *xpoaConfig, cCtx context.ConsensusCtx, startHeight int64) *xpoaSchedule {
39-
version, err := strconv.ParseInt(xconfig.Version, 10, 64)
40-
if err != nil {
41-
cCtx.XLog.Error("Xpoa::NewXpoaSchedule::Parse version error.", "err", err)
42-
return nil
43-
}
37+
func NewXpoaSchedule(xconfig *xpoaConfig, cCtx context.ConsensusCtx, startHeight, version int64) *xpoaSchedule {
4438
s := xpoaSchedule{
4539
address: cCtx.Network.PeerInfo().Account,
4640
period: xconfig.Period,

bcs/consensus/xpoa/xpoa.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package xpoa
33
import (
44
"bytes"
55
"encoding/json"
6-
"strconv"
76
"time"
87

98
"github.com/xuperchain/xupercore/kernel/common/xcontext"
@@ -66,13 +65,13 @@ func NewXpoaConsensus(cCtx cctx.ConsensusCtx, cCfg def.ConsensusConfig) base.Con
6665
cCtx.XLog.Error("consensus:xpoa:NewXpoaConsensus: xpoa struct unmarshal error", "error", err)
6766
return nil
6867
}
69-
version, err := strconv.ParseInt(xconfig.Version, 10, 64)
68+
version, err := ParseVersion(cCfg.Config)
7069
if err != nil {
7170
cCtx.XLog.Error("consensus:xpoa:NewXpoaConsensus: version error", "error", err)
7271
return nil
7372
}
7473
// create xpoaSchedule
75-
schedule := NewXpoaSchedule(xconfig, cCtx, cCfg.StartHeight)
74+
schedule := NewXpoaSchedule(xconfig, cCtx, cCfg.StartHeight, version)
7675
if schedule == nil {
7776
cCtx.XLog.Error("consensus:xpoa:NewXpoaSchedule error")
7877
return nil

0 commit comments

Comments
 (0)