Skip to content

Commit

Permalink
xpoa: parameter version works compatibly with int and string type (#286)
Browse files Browse the repository at this point in the history
* xpoa: parameter version works compatibly with int and string type.
  • Loading branch information
aucusaga authored Nov 17, 2021
1 parent f35d472 commit e9bc306
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 11 deletions.
27 changes: 26 additions & 1 deletion bcs/consensus/xpoa/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package xpoa
import (
"encoding/json"
"errors"
"strconv"
)

var (
Expand Down Expand Up @@ -30,7 +31,6 @@ const (
)

type xpoaConfig struct {
Version string `json:"version,omitempty"`
// 每个候选人每轮出块个数
BlockNum int64 `json:"block_num"`
// 单位为毫秒
Expand Down Expand Up @@ -103,3 +103,28 @@ func (a aksSlice) Less(i, j int) bool {
}
return a[j].Weight < a[i].Weight
}

type xpoaStringConfig struct {
Version string `json:"version,omitempty"`
}

type xpoaIntConfig struct {
Version int64 `json:"version,omitempty"`
}

// ParseVersion 支持string格式和int格式的version type
func ParseVersion(cfg string) (int64, error) {
intVersion := xpoaIntConfig{}
if err := json.Unmarshal([]byte(cfg), &intVersion); err == nil {
return intVersion.Version, nil
}
strVersion := xpoaStringConfig{}
if err := json.Unmarshal([]byte(cfg), &strVersion); err != nil {
return 0, err
}
version, err := strconv.ParseInt(strVersion.Version, 10, 64)
if err != nil {
return 0, err
}
return version, nil
}
37 changes: 37 additions & 0 deletions bcs/consensus/xpoa/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,40 @@ func TestLoadValidatorsMultiInfo(t *testing.T) {
t.Error("TestLoadValidatorsMultiInfo error 2.")
}
}

func TestParseVersion(t *testing.T) {
strVersion := `{
"version": "2"
}`
v, err := ParseVersion(strVersion)
if err != nil {
t.Error("ParseVersion err, err: ", err)
return
}
if v != 2 {
t.Error("ParseVersion err, v: ", v)
return
}
intVersion := `{
"version": 3
}`
v, err = ParseVersion(intVersion)
if err != nil {
t.Error("ParseVersion err, err: ", err)
return
}
if v != 3 {
t.Error("ParseVersion err, v: ", v)
return
}
empryVersion := `{}`
v, err = ParseVersion(empryVersion)
if err != nil {
t.Error("ParseVersion err, err: ", err)
return
}
if v != 0 {
t.Error("ParseVersion err, v: ", v)
return
}
}
8 changes: 1 addition & 7 deletions bcs/consensus/xpoa/schedule.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package xpoa

import (
"fmt"
"strconv"
"time"

common "github.com/xuperchain/xupercore/kernel/consensus/base/common"
Expand Down Expand Up @@ -35,12 +34,7 @@ type xpoaSchedule struct {
ledger cctx.LedgerRely
}

func NewXpoaSchedule(xconfig *xpoaConfig, cCtx context.ConsensusCtx, startHeight int64) *xpoaSchedule {
version, err := strconv.ParseInt(xconfig.Version, 10, 64)
if err != nil {
cCtx.XLog.Error("Xpoa::NewXpoaSchedule::Parse version error.", "err", err)
return nil
}
func NewXpoaSchedule(xconfig *xpoaConfig, cCtx context.ConsensusCtx, startHeight, version int64) *xpoaSchedule {
s := xpoaSchedule{
address: cCtx.Network.PeerInfo().Account,
period: xconfig.Period,
Expand Down
5 changes: 2 additions & 3 deletions bcs/consensus/xpoa/xpoa.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package xpoa
import (
"bytes"
"encoding/json"
"strconv"
"time"

"github.com/xuperchain/xupercore/kernel/common/xcontext"
Expand Down Expand Up @@ -66,13 +65,13 @@ func NewXpoaConsensus(cCtx cctx.ConsensusCtx, cCfg def.ConsensusConfig) base.Con
cCtx.XLog.Error("consensus:xpoa:NewXpoaConsensus: xpoa struct unmarshal error", "error", err)
return nil
}
version, err := strconv.ParseInt(xconfig.Version, 10, 64)
version, err := ParseVersion(cCfg.Config)
if err != nil {
cCtx.XLog.Error("consensus:xpoa:NewXpoaConsensus: version error", "error", err)
return nil
}
// create xpoaSchedule
schedule := NewXpoaSchedule(xconfig, cCtx, cCfg.StartHeight)
schedule := NewXpoaSchedule(xconfig, cCtx, cCfg.StartHeight, version)
if schedule == nil {
cCtx.XLog.Error("consensus:xpoa:NewXpoaSchedule error")
return nil
Expand Down

0 comments on commit e9bc306

Please sign in to comment.