Skip to content

Commit 4423aa7

Browse files
committed
Merge remote-tracking branch 'upstream/master'
2 parents df57b67 + dcc1e24 commit 4423aa7

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

54 files changed

+1185
-927
lines changed

.github/workflows/backport.yml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
name: Backport
2+
on:
3+
pull_request:
4+
types:
5+
- closed
6+
- labeled
7+
8+
jobs:
9+
backport:
10+
runs-on: ubuntu-18.04
11+
name: Backport
12+
steps:
13+
- name: Backport
14+
uses: tibdex/backport@v1
15+
with:
16+
github_token: ${{ secrets.GITHUB_TOKEN }}
17+

.github/workflows/ci.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@ jobs:
1616
- name: Checkout code
1717
uses: actions/checkout@v2
1818
- name: Test
19-
run: make && make test
19+
run: |
20+
diff -u <(echo -n) <(gofmt -d $(find . -type f -name '*.go'))
21+
make && make test
2022
2123
- name: Upload coverage to Codecov
2224
uses: codecov/codecov-action@v1

bcs/consensus/pow/pow.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import (
99

1010
"github.com/xuperchain/xupercore/kernel/common/xcontext"
1111
"github.com/xuperchain/xupercore/kernel/consensus"
12-
"github.com/xuperchain/xupercore/kernel/consensus/base"
1312
"github.com/xuperchain/xupercore/kernel/consensus/context"
1413
"github.com/xuperchain/xupercore/kernel/consensus/def"
1514
)
@@ -48,7 +47,7 @@ type PoWConsensus struct {
4847
}
4948

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

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

270269
// GetConsensusStatus 获取pow实例状态
271-
func (pow *PoWConsensus) GetConsensusStatus() (base.ConsensusStatus, error) {
270+
func (pow *PoWConsensus) GetConsensusStatus() (consensus.ConsensusStatus, error) {
272271
return pow.status, nil
273272
}
274273

bcs/consensus/pow/pow_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ func TestProcessBeforeMiner(t *testing.T) {
107107
return
108108
}
109109
i := NewPoWConsensus(*cCtx, getConsensusConf(getPoWConsensusConf()))
110-
_, _, err = i.ProcessBeforeMiner(time.Now().UnixNano())
110+
_, _, err = i.ProcessBeforeMiner(0, time.Now().UnixNano())
111111
if err != nil {
112112
t.Error("ProcessBeforeMiner error.")
113113
}

bcs/consensus/single/single.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import (
1010

1111
"github.com/xuperchain/xupercore/kernel/common/xcontext"
1212
"github.com/xuperchain/xupercore/kernel/consensus"
13-
"github.com/xuperchain/xupercore/kernel/consensus/base"
1413
cctx "github.com/xuperchain/xupercore/kernel/consensus/context"
1514
"github.com/xuperchain/xupercore/kernel/consensus/def"
1615
)
@@ -32,7 +31,7 @@ type SingleConsensus struct {
3231
}
3332

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

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

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

150149
// GetStatus 获取区块链共识信息
151-
func (s *SingleConsensus) GetConsensusStatus() (base.ConsensusStatus, error) {
150+
func (s *SingleConsensus) GetConsensusStatus() (consensus.ConsensusStatus, error) {
152151
return s.status, nil
153152
}
154153

bcs/consensus/single/single_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ func TestNewSingleConsensus(t *testing.T) {
6464
}
6565
i.Stop()
6666
i.Start()
67-
i.ProcessBeforeMiner(time.Now().UnixNano())
67+
i.ProcessBeforeMiner(0, time.Now().UnixNano())
6868
cCtx.XLog = nil
6969
i = NewSingleConsensus(*cCtx, conf)
7070
if i != nil {
@@ -140,7 +140,7 @@ func TestCheckMinerMatch(t *testing.T) {
140140
if !ok || err != nil {
141141
t.Error("TestCheckMinerMatch error", "error", err, cCtx.Address.PrivateKey)
142142
}
143-
_, _, err = i.ProcessBeforeMiner(time.Now().UnixNano())
143+
_, _, err = i.ProcessBeforeMiner(0, time.Now().UnixNano())
144144
if err != nil {
145145
t.Error("ProcessBeforeMiner error", "error", err)
146146
}

bcs/consensus/tdpos/common.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,17 @@ func buildConfigs(input []byte) (*tdposConfig, error) {
152152
if err != nil {
153153
return nil, fmt.Errorf("unmarshal to temp struct failed.err:%v", err)
154154
}
155+
156+
// 校验是否输入初始候选人节点列表
157+
if temp.InitProposer != nil {
158+
// 二次校验
159+
if addrs, ok := temp.InitProposer["1"]; !ok || len(addrs) <= 0 {
160+
return nil, fmt.Errorf("init_proposer[\"1\"] is required")
161+
}
162+
} else {
163+
return nil, fmt.Errorf("init_proposer is required")
164+
}
165+
155166
tdposCfg.InitProposer = temp.InitProposer
156167
tdposCfg.EnableBFT = temp.EnableBFT
157168

bcs/consensus/tdpos/schedule.go

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -210,10 +210,11 @@ func (s *tdposSchedule) UpdateProposers(height int64) bool {
210210
return false
211211
}
212212
if !common.AddressEqual(nextProposers, s.validators) {
213-
s.log.Debug("tdpos::UpdateProposers", "origin", s.validators, "proposers", nextProposers)
213+
s.log.Debug("tdpos::UpdateProposers", "origin", s.validators, "proposers", nextProposers, "height", height)
214214
s.validators = nextProposers
215215
return true
216216
}
217+
s.log.Debug("tdpos::UpdateProposers", "origin", s.validators, "height", height)
217218
return false
218219
}
219220

@@ -352,7 +353,7 @@ func (s *tdposSchedule) calHisValidators(height int64) ([]string, error) {
352353
term, pos, blockPos := s.minerScheduling(block.GetTimestamp())
353354
// 往前回溯的最远距离为internal,即该轮term之前最多生产过多少个区块
354355
internal := pos*s.blockNum + blockPos
355-
begin := block.GetHeight() - internal
356+
begin := block.GetHeight() - internal - 1
356357
if begin <= s.startHeight {
357358
begin = s.startHeight
358359
}
@@ -361,7 +362,9 @@ func (s *tdposSchedule) calHisValidators(height int64) ([]string, error) {
361362
if err != nil {
362363
return nil, err
363364
}
364-
s.log.Debug("tdpos::CalculateProposers::target height.", "height", height, "targetHeight", targetHeight, "term", term)
365+
s.log.Debug("tdpos::CalculateProposers::target height.", "inputHeight", height, "targetHeight", targetHeight,
366+
"begin", begin, "end", block.GetHeight(), "term", term, "pos", pos, "blockPos", blockPos, "internal", internal,
367+
"blockNum", s.blockNum, "block.Timestamp", block.GetTimestamp())
365368
return s.calTopKNominator(targetHeight)
366369
}
367370

@@ -378,7 +381,7 @@ func (s *tdposSchedule) binarySearch(begin int64, end int64, term int64) (int64,
378381
return -1, err
379382
}
380383
if midTerm < term && nextMidTerm == term {
381-
return mid + 1, nil
384+
return mid, nil
382385
}
383386
if midTerm < term {
384387
begin = mid + 1

bcs/consensus/tdpos/schedule_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -178,12 +178,12 @@ func TestCalHisValidators(t *testing.T) {
178178
return
179179
}
180180
target, _ = s.binarySearch(int64(1), int64(5), int64(2))
181-
if target != 4 {
181+
if target != 3 {
182182
t.Error("binarySearch cal err2.", "target", target)
183183
return
184184
}
185185
target, _ = s.binarySearch(int64(5), int64(6), int64(3))
186-
if target != 6 {
186+
if target != 5 {
187187
t.Error("binarySearch cal err.", "target", target)
188188
return
189189
}
@@ -198,7 +198,7 @@ func TestCalHisValidators(t *testing.T) {
198198
return
199199
}
200200
target, _ = s.binarySearch(int64(5), int64(11), int64(5))
201-
if target != 8 {
201+
if target != 7 {
202202
t.Error("binarySearch cal err.", "target", target)
203203
return
204204
}

0 commit comments

Comments
 (0)