Skip to content

Commit a5c5c91

Browse files
authored
fix consensus version bug (#285)
1 parent f7860e1 commit a5c5c91

1 file changed

Lines changed: 8 additions & 31 deletions

File tree

kernel/consensus/pluggable_consensus.go

Lines changed: 8 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"encoding/json"
55
"errors"
66
"strconv"
7-
"strings"
87
"sync"
98

109
"github.com/xuperchain/xupercore/kernel/common/xcontext"
@@ -43,7 +42,7 @@ var (
4342
ContractMngErr = errors.New("Contract manager is empty.")
4443

4544
ErrInvalidConfig = errors.New("config should be an empty JSON when rolling back an old one, or try an upper version")
46-
ErrInvalidVersion = errors.New("version should be an upper one when upgrading a new one, or try an empty config JSON when you need rollback")
45+
ErrInvalidVersion = errors.New("version should be an upper one when upgrading a new one")
4746
)
4847

4948
// PluggableConsensus 实现了consensus_interface接口
@@ -195,7 +194,7 @@ func (pc *PluggableConsensus) proposalArgsUnmarshal(ctxArgs map[string][]byte) (
195194

196195
// updateConsensus 共识升级,更新原有共识列表,向PluggableConsensus共识列表插入新共识,并暂停原共识实例
197196
// 该方法注册到kernel的延时调用合约中,在trigger高度时被调用,此时直接按照共识cfg新建新的共识实例
198-
// 若同名共识且version相同,则使用历史的配置,否则version需要递增序列
197+
// 共识version需要递增序列
199198
func (pc *PluggableConsensus) updateConsensus(contractCtx contract.KContext) (*contract.Response, error) {
200199
// 解析用户合约信息,包括待升级名称name、trigger高度height和待升级配置config
201200
cfg, err := pc.proposalArgsUnmarshal(contractCtx.Args())
@@ -410,13 +409,11 @@ func NewPluginConsensus(cCtx cctx.ConsensusCtx, cCfg def.ConsensusConfig) (base.
410409

411410
type configFilter struct {
412411
Version string `json:"version,omitempty"`
413-
index int `json:"-"`
414412
}
415413

416414
// CheckConsensusConfig 同名配置文件检查:
417-
// 1. 若历史相同version,则直接返回历史cfg
418-
// 2. 若不存在,需要比历史最大值大
419-
// 3. 将合法的配置写到map中
415+
// 1. 同一个链的共识版本只能增加,不能升级到旧版本
416+
// 2. 将合法的配置写到map中
420417
func CheckConsensusVersion(hisMap map[int]def.ConsensusConfig, cfg *def.ConsensusConfig) error {
421418
var err error
422419
var newConf configFilter
@@ -431,41 +428,21 @@ func CheckConsensusVersion(hisMap map[int]def.ConsensusConfig, cfg *def.Consensu
431428
return errors.New("wrong parameter version, version should an integer in string")
432429
}
433430
// 获取历史最近共识实例,初始状态下历史共识没有version字段的,需手动添加
434-
var configSlice []configFilter
435431
var maxVersion int64
436432
for i := len(hisMap) - 1; i >= 0; i-- {
437433
configItem := hisMap[i]
438-
if configItem.ConsensusName != cfg.ConsensusName {
439-
continue
440-
}
441434
var tmpItem configFilter
442-
json.Unmarshal([]byte(configItem.Config), &tmpItem)
443-
tmpItem.index = i
435+
err := json.Unmarshal([]byte(configItem.Config), &tmpItem)
436+
if err != nil {
437+
return errors.New("unmarshal config error")
438+
}
444439
if tmpItem.Version == "" {
445440
tmpItem.Version = "0"
446441
}
447-
if tmpItem.Version == newConf.Version {
448-
dec := json.NewDecoder(strings.NewReader(cfg.Config))
449-
dec.DisallowUnknownFields()
450-
var checkCfg configFilter
451-
err := dec.Decode(&checkCfg)
452-
if err != nil {
453-
return ErrInvalidConfig
454-
}
455-
hisConfig := hisMap[i]
456-
hisMap[len(hisMap)] = hisConfig
457-
return nil
458-
}
459-
configSlice = append(configSlice, tmpItem)
460442
v, _ := strconv.ParseInt(tmpItem.Version, 10, 64)
461443
if maxVersion < v {
462444
maxVersion = v
463445
}
464-
continue
465-
}
466-
if len(configSlice) == 0 {
467-
hisMap[len(hisMap)] = *cfg
468-
return nil
469446
}
470447
if maxVersion < newConfVersion {
471448
hisMap[len(hisMap)] = *cfg

0 commit comments

Comments
 (0)