Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/master'
Browse files Browse the repository at this point in the history
  • Loading branch information
springrain committed Feb 10, 2022
2 parents df57b67 + dcc1e24 commit 4423aa7
Show file tree
Hide file tree
Showing 54 changed files with 1,185 additions and 927 deletions.
17 changes: 17 additions & 0 deletions .github/workflows/backport.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
name: Backport
on:
pull_request:
types:
- closed
- labeled

jobs:
backport:
runs-on: ubuntu-18.04
name: Backport
steps:
- name: Backport
uses: tibdex/backport@v1
with:
github_token: ${{ secrets.GITHUB_TOKEN }}

4 changes: 3 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ jobs:
- name: Checkout code
uses: actions/checkout@v2
- name: Test
run: make && make test
run: |
diff -u <(echo -n) <(gofmt -d $(find . -type f -name '*.go'))
make && make test
- name: Upload coverage to Codecov
uses: codecov/codecov-action@v1
Expand Down
7 changes: 3 additions & 4 deletions bcs/consensus/pow/pow.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (

"github.com/xuperchain/xupercore/kernel/common/xcontext"
"github.com/xuperchain/xupercore/kernel/consensus"
"github.com/xuperchain/xupercore/kernel/consensus/base"
"github.com/xuperchain/xupercore/kernel/consensus/context"
"github.com/xuperchain/xupercore/kernel/consensus/def"
)
Expand Down Expand Up @@ -48,7 +47,7 @@ type PoWConsensus struct {
}

// NewPoWConsensus 初始化实例
func NewPoWConsensus(cCtx context.ConsensusCtx, cCfg def.ConsensusConfig) base.ConsensusImplInterface {
func NewPoWConsensus(cCtx context.ConsensusCtx, cCfg def.ConsensusConfig) consensus.ConsensusImplInterface {
// 解析config中需要的字段
if cCtx.XLog == nil {
return nil
Expand Down Expand Up @@ -237,7 +236,7 @@ func (pow *PoWConsensus) CheckMinerMatch(ctx xcontext.XContext, block context.Bl
}

// ProcessBeforeMiner 更新下一次pow挖矿时的targetBits
func (pow *PoWConsensus) ProcessBeforeMiner(timestamp int64) ([]byte, []byte, error) {
func (pow *PoWConsensus) ProcessBeforeMiner(height, timestamp int64) ([]byte, []byte, error) {
tipHeight := pow.Ledger.QueryTipBlockHeader().GetHeight()
preBlock, err := pow.Ledger.QueryBlockHeaderByHeight(tipHeight)
if err != nil {
Expand Down Expand Up @@ -268,7 +267,7 @@ func (pow *PoWConsensus) ProcessConfirmBlock(block context.BlockInterface) error
}

// GetConsensusStatus 获取pow实例状态
func (pow *PoWConsensus) GetConsensusStatus() (base.ConsensusStatus, error) {
func (pow *PoWConsensus) GetConsensusStatus() (consensus.ConsensusStatus, error) {
return pow.status, nil
}

Expand Down
2 changes: 1 addition & 1 deletion bcs/consensus/pow/pow_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ func TestProcessBeforeMiner(t *testing.T) {
return
}
i := NewPoWConsensus(*cCtx, getConsensusConf(getPoWConsensusConf()))
_, _, err = i.ProcessBeforeMiner(time.Now().UnixNano())
_, _, err = i.ProcessBeforeMiner(0, time.Now().UnixNano())
if err != nil {
t.Error("ProcessBeforeMiner error.")
}
Expand Down
7 changes: 3 additions & 4 deletions bcs/consensus/single/single.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (

"github.com/xuperchain/xupercore/kernel/common/xcontext"
"github.com/xuperchain/xupercore/kernel/consensus"
"github.com/xuperchain/xupercore/kernel/consensus/base"
cctx "github.com/xuperchain/xupercore/kernel/consensus/context"
"github.com/xuperchain/xupercore/kernel/consensus/def"
)
Expand All @@ -32,7 +31,7 @@ type SingleConsensus struct {
}

// NewSingleConsensus 初始化实例
func NewSingleConsensus(cCtx cctx.ConsensusCtx, cCfg def.ConsensusConfig) base.ConsensusImplInterface {
func NewSingleConsensus(cCtx cctx.ConsensusCtx, cCfg def.ConsensusConfig) consensus.ConsensusImplInterface {
// 解析config中需要的字段
if cCtx.XLog == nil {
return nil
Expand Down Expand Up @@ -133,7 +132,7 @@ func (s *SingleConsensus) CheckMinerMatch(ctx xcontext.XContext, block cctx.Bloc
}

// ProcessBeforeMiner 开始挖矿前进行相应的处理, 返回是否需要truncate, 返回写consensusStorage, 返回err
func (s *SingleConsensus) ProcessBeforeMiner(timestamp int64) ([]byte, []byte, error) {
func (s *SingleConsensus) ProcessBeforeMiner(height, timestamp int64) ([]byte, []byte, error) {
return nil, nil, nil
}

Expand All @@ -148,7 +147,7 @@ func (s *SingleConsensus) ProcessConfirmBlock(block cctx.BlockInterface) error {
}

// GetStatus 获取区块链共识信息
func (s *SingleConsensus) GetConsensusStatus() (base.ConsensusStatus, error) {
func (s *SingleConsensus) GetConsensusStatus() (consensus.ConsensusStatus, error) {
return s.status, nil
}

Expand Down
4 changes: 2 additions & 2 deletions bcs/consensus/single/single_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ func TestNewSingleConsensus(t *testing.T) {
}
i.Stop()
i.Start()
i.ProcessBeforeMiner(time.Now().UnixNano())
i.ProcessBeforeMiner(0, time.Now().UnixNano())
cCtx.XLog = nil
i = NewSingleConsensus(*cCtx, conf)
if i != nil {
Expand Down Expand Up @@ -140,7 +140,7 @@ func TestCheckMinerMatch(t *testing.T) {
if !ok || err != nil {
t.Error("TestCheckMinerMatch error", "error", err, cCtx.Address.PrivateKey)
}
_, _, err = i.ProcessBeforeMiner(time.Now().UnixNano())
_, _, err = i.ProcessBeforeMiner(0, time.Now().UnixNano())
if err != nil {
t.Error("ProcessBeforeMiner error", "error", err)
}
Expand Down
11 changes: 11 additions & 0 deletions bcs/consensus/tdpos/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,17 @@ func buildConfigs(input []byte) (*tdposConfig, error) {
if err != nil {
return nil, fmt.Errorf("unmarshal to temp struct failed.err:%v", err)
}

// 校验是否输入初始候选人节点列表
if temp.InitProposer != nil {
// 二次校验
if addrs, ok := temp.InitProposer["1"]; !ok || len(addrs) <= 0 {
return nil, fmt.Errorf("init_proposer[\"1\"] is required")
}
} else {
return nil, fmt.Errorf("init_proposer is required")
}

tdposCfg.InitProposer = temp.InitProposer
tdposCfg.EnableBFT = temp.EnableBFT

Expand Down
11 changes: 7 additions & 4 deletions bcs/consensus/tdpos/schedule.go
Original file line number Diff line number Diff line change
Expand Up @@ -210,10 +210,11 @@ func (s *tdposSchedule) UpdateProposers(height int64) bool {
return false
}
if !common.AddressEqual(nextProposers, s.validators) {
s.log.Debug("tdpos::UpdateProposers", "origin", s.validators, "proposers", nextProposers)
s.log.Debug("tdpos::UpdateProposers", "origin", s.validators, "proposers", nextProposers, "height", height)
s.validators = nextProposers
return true
}
s.log.Debug("tdpos::UpdateProposers", "origin", s.validators, "height", height)
return false
}

Expand Down Expand Up @@ -352,7 +353,7 @@ func (s *tdposSchedule) calHisValidators(height int64) ([]string, error) {
term, pos, blockPos := s.minerScheduling(block.GetTimestamp())
// 往前回溯的最远距离为internal,即该轮term之前最多生产过多少个区块
internal := pos*s.blockNum + blockPos
begin := block.GetHeight() - internal
begin := block.GetHeight() - internal - 1
if begin <= s.startHeight {
begin = s.startHeight
}
Expand All @@ -361,7 +362,9 @@ func (s *tdposSchedule) calHisValidators(height int64) ([]string, error) {
if err != nil {
return nil, err
}
s.log.Debug("tdpos::CalculateProposers::target height.", "height", height, "targetHeight", targetHeight, "term", term)
s.log.Debug("tdpos::CalculateProposers::target height.", "inputHeight", height, "targetHeight", targetHeight,
"begin", begin, "end", block.GetHeight(), "term", term, "pos", pos, "blockPos", blockPos, "internal", internal,
"blockNum", s.blockNum, "block.Timestamp", block.GetTimestamp())
return s.calTopKNominator(targetHeight)
}

Expand All @@ -378,7 +381,7 @@ func (s *tdposSchedule) binarySearch(begin int64, end int64, term int64) (int64,
return -1, err
}
if midTerm < term && nextMidTerm == term {
return mid + 1, nil
return mid, nil
}
if midTerm < term {
begin = mid + 1
Expand Down
6 changes: 3 additions & 3 deletions bcs/consensus/tdpos/schedule_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,12 +178,12 @@ func TestCalHisValidators(t *testing.T) {
return
}
target, _ = s.binarySearch(int64(1), int64(5), int64(2))
if target != 4 {
if target != 3 {
t.Error("binarySearch cal err2.", "target", target)
return
}
target, _ = s.binarySearch(int64(5), int64(6), int64(3))
if target != 6 {
if target != 5 {
t.Error("binarySearch cal err.", "target", target)
return
}
Expand All @@ -198,7 +198,7 @@ func TestCalHisValidators(t *testing.T) {
return
}
target, _ = s.binarySearch(int64(5), int64(11), int64(5))
if target != 8 {
if target != 7 {
t.Error("binarySearch cal err.", "target", target)
return
}
Expand Down
Loading

0 comments on commit 4423aa7

Please sign in to comment.