Skip to content

Commit 5f59ca7

Browse files
Test go file
1 parent efe7eb4 commit 5f59ca7

157 files changed

Lines changed: 924 additions & 912 deletions

File tree

Some content is hidden

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

bcs/consensus/mock/mock_consensus.go

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package mock
22

33
import (
4-
"errors"
54
"path/filepath"
65
"time"
76

@@ -17,12 +16,9 @@ import (
1716

1817
var (
1918
BcName = "xuper5"
20-
nodeIp = "/ip4/127.0.0.1/tcp/47101/p2p/QmVcSF4F7rTdsvUJqsik98tXRXMBUqL5DSuBpyYKVhjuG4"
2119
priKey = `{"Curvname":"P-256","X":74695617477160058757747208220371236837474210247114418775262229497812962582435,"Y":51348715319124770392993866417088542497927816017012182211244120852620959209571,"D":29079635126530934056640915735344231956621504557963207107451663058887647996601}`
2220
PubKey = `{"Curvname":"P-256","X":74695617477160058757747208220371236837474210247114418775262229497812962582435,"Y":51348715319124770392993866417088542497927816017012182211244120852620959209571}`
2321
Miner = "dpzuVdosQrF2kmzumhVeFQZa1aYcdgFpN"
24-
25-
blockSetItemErr = errors.New("item invalid")
2622
)
2723

2824
func NewFakeLogger() logs.Logger {

bcs/consensus/pow/common.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ func GetCompact(number *big.Int) (uint32, bool) {
6262
func SetCompact(nCompact uint32) (*big.Int, bool, bool) {
6363
nSize := nCompact >> 24
6464
nWord := new(big.Int)
65-
u := new(big.Int)
65+
var u *big.Int
6666
nCompactInt := big.NewInt(int64(nCompact))
6767
// 0x00800000是一个符号位,故nWord仅为后23位
6868
lowBits := big.NewInt(0x007fffff)
@@ -99,14 +99,14 @@ func unmarshalPowConfig(input []byte) (*PoWConfig, error) {
9999
"defaultTarget": 0,
100100
"maxTarget": 0,
101101
}
102-
for k, _ := range int32Map {
102+
for k := range int32Map {
103103
value, err := strconv.ParseInt(consCfg[k].(string), 10, 32)
104104
if err != nil {
105105
return nil, fmt.Errorf("marshal consensus config failed key %s set error", k)
106106
}
107107
int32Map[k] = int32(value)
108108
}
109-
for k, _ := range uint32Map {
109+
for k := range uint32Map {
110110
value, err := strconv.ParseInt(consCfg[k].(string), 10, 64)
111111
if err != nil {
112112
return nil, fmt.Errorf("marshal consensus config failed key %s set error", k)

bcs/consensus/pow/pow.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ func (pow *PoWConsensus) CheckMinerMatch(ctx xcontext.XContext, block context.Bl
219219
}
220220
// 跟address比较
221221
chkResult, _ := pow.Crypto.VerifyAddressUsingPublicKey(string(block.GetProposer()), k)
222-
if chkResult == false {
222+
if !chkResult {
223223
ctx.GetLog().Warn("PoW::CheckMinerMatch::address is not match publickey", "miner", string(block.GetProposer()))
224224
return false, err
225225
}
@@ -245,7 +245,8 @@ func (pow *PoWConsensus) ProcessBeforeMiner(height, timestamp int64) ([]byte, []
245245
}
246246
bits, err := pow.refreshDifficulty(preBlock.GetBlockid(), tipHeight+1)
247247
if err != nil {
248-
pow.Stop()
248+
// TODO: deal with error
249+
_ = pow.Stop()
249250
}
250251
pow.targetBits = bits
251252
store := &PoWStorage{
@@ -409,10 +410,7 @@ func (pow *PoWConsensus) IsProofed(blockID []byte, targetBits uint32) bool {
409410
// 原xuperchain逻辑
410411
target := big.NewInt(1)
411412
target.Lsh(target, uint(256-targetBits))
412-
if hash.Cmp(target) == 1 {
413-
return false
414-
}
415-
return true
413+
return hash.Cmp(target) != 1
416414
}
417415

418416
// mining 为带副作用的函数,将直接对block进行操作,更改其原始值
@@ -440,14 +438,16 @@ func (pow *PoWConsensus) mining(task *mineTask) {
440438
return
441439
}
442440
if pow.IsProofed(bid, pow.targetBits) {
443-
task.block.SetItem("blockid", bid)
441+
// TODO: deal with error
442+
_ = task.block.SetItem("blockid", bid)
444443
// 签名重置
445444
s, err := pow.Crypto.SignECDSA(pow.Address.PrivateKey, bid)
446445
if err != nil {
447446
task.doDone(BlockSignErr)
448447
return
449448
}
450-
task.block.SetItem("sign", s)
449+
// TODO: deal with error
450+
_ = task.block.SetItem("sign", s)
451451
task.doDone(nil)
452452
return
453453
}

bcs/consensus/pow/pow_test.go

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -276,8 +276,12 @@ func TestMining(t *testing.T) {
276276
return
277277
}
278278
powC.targetBits = minTarget
279-
powC.Start()
280-
defer powC.Stop()
279+
if err := powC.Start(); err != nil {
280+
t.Fatal(err)
281+
}
282+
defer func() {
283+
_ = powC.Stop()
284+
}()
281285
ps := PoWStorage{
282286
TargetBits: minTarget,
283287
}
@@ -315,7 +319,7 @@ func TestRefreshDifficulty(t *testing.T) {
315319
t.Error("NewBlock error", err)
316320
return
317321
}
318-
l, ok := powC.Ledger.(*kmock.FakeLedger)
322+
l := powC.Ledger.(*kmock.FakeLedger)
319323
err = l.Put(genesisB)
320324
if err != nil {
321325
t.Error("TestRefreshDifficulty put genesis err", "err", err)
@@ -409,6 +413,9 @@ func TestCheckMinerMatch(t *testing.T) {
409413
}
410414
by, _ := json.Marshal(ps)
411415
b3, err := bmock.NewBlockWithStorage(3, cCtx.Crypto, cCtx.Address, by)
416+
if err != nil {
417+
t.Fatal(err)
418+
}
412419
c := cCtx.BaseCtx
413420
_, err = i.CheckMinerMatch(&c, b3)
414421
if err != nil {
@@ -423,5 +430,7 @@ func TestCompeteMaster(t *testing.T) {
423430
return
424431
}
425432
i := NewPoWConsensus(*cCtx, getConsensusConf(getPoWConsensusConf()))
426-
i.CompeteMaster(3)
433+
if _, _, err := i.CompeteMaster(3); err != nil {
434+
t.Fatal(err)
435+
}
427436
}

bcs/consensus/single/single.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ func (s *SingleConsensus) CheckMinerMatch(ctx xcontext.XContext, block cctx.Bloc
110110
return false, err
111111
}
112112
chkResult, _ := s.ctx.Crypto.VerifyAddressUsingPublicKey(string(block.GetProposer()), k)
113-
if chkResult == false {
113+
if !chkResult {
114114
ctx.GetLog().Warn("Single::CheckMinerMatch::address is not match publickey")
115115
return false, err
116116
}

bcs/consensus/single/single_test.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,15 @@ func TestNewSingleConsensus(t *testing.T) {
6262
if i := NewSingleConsensus(*cCtx, getWrongConsensusConf()); i != nil {
6363
t.Error("NewSingleConsensus check name error")
6464
}
65-
i.Stop()
66-
i.Start()
67-
i.ProcessBeforeMiner(0, time.Now().UnixNano())
65+
if err := i.Stop(); err != nil {
66+
t.Fatal(err)
67+
}
68+
if err := i.Start(); err != nil {
69+
t.Fatal(err)
70+
}
71+
if _, _, err := i.ProcessBeforeMiner(0, time.Now().UnixNano()); err != nil {
72+
t.Fatal(err)
73+
}
6874
cCtx.XLog = nil
6975
i = NewSingleConsensus(*cCtx, conf)
7076
if i != nil {

bcs/consensus/tdpos/common.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ func buildConfigs(input []byte) (*tdposConfig, error) {
114114
"block_num": 0,
115115
"timestamp": 0,
116116
}
117-
for k, _ := range int64Map {
117+
for k := range int64Map {
118118
if _, ok := consCfg[k]; !ok {
119119
if k == "version" {
120120
continue

bcs/consensus/tdpos/kernel_contract.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -364,7 +364,7 @@ func (tp *tdposConsensus) runGetTdposInfos(contractCtx contract.KContext) (*cont
364364

365365
// vote信息
366366
voteMap := make(map[string]voteValue)
367-
for candidate, _ := range nominateValue {
367+
for candidate := range nominateValue {
368368
// 读取投票存储
369369
voteKey := fmt.Sprintf("%s_%d_%s%s", tp.status.Name, tp.status.Version, voteKeyPrefix, candidate)
370370
res, err = contractCtx.Get(tp.election.bindContractBucket, []byte(voteKey))

bcs/consensus/tdpos/kernel_contract_test.go

Lines changed: 27 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -80,10 +80,10 @@ func TestRunNominateCandidate(t *testing.T) {
8080
}
8181
// 1. 构造term存储
8282
l, _ := cCtx.Ledger.(*kmock.FakeLedger)
83-
l.Put(kmock.NewBlock(3))
84-
l.Put(kmock.NewBlock(4))
85-
l.Put(kmock.NewBlock(5))
86-
l.Put(kmock.NewBlock(6))
83+
_ = l.Put(kmock.NewBlock(3))
84+
_ = l.Put(kmock.NewBlock(4))
85+
_ = l.Put(kmock.NewBlock(5))
86+
_ = l.Put(kmock.NewBlock(6))
8787
// 2. 整理Block的共识存储
8888
l.SetConsensusStorage(1, SetTdposStorage(1, nil))
8989
l.SetConsensusStorage(2, SetTdposStorage(1, nil))
@@ -116,10 +116,10 @@ func TestRunRevokeCandidate(t *testing.T) {
116116
}
117117
// 1. 构造term存储
118118
l, _ := cCtx.Ledger.(*kmock.FakeLedger)
119-
l.Put(kmock.NewBlock(3))
120-
l.Put(kmock.NewBlock(4))
121-
l.Put(kmock.NewBlock(5))
122-
l.Put(kmock.NewBlock(6))
119+
_ = l.Put(kmock.NewBlock(3))
120+
_ = l.Put(kmock.NewBlock(4))
121+
_ = l.Put(kmock.NewBlock(5))
122+
_ = l.Put(kmock.NewBlock(6))
123123
// 2. 整理Block的共识存储
124124
l.SetConsensusStorage(1, SetTdposStorage(1, nil))
125125
l.SetConsensusStorage(2, SetTdposStorage(1, nil))
@@ -137,7 +137,9 @@ func TestRunRevokeCandidate(t *testing.T) {
137137
i := NewTdposConsensus(*cCtx, getConfig(getTdposConsensusConf()))
138138
tdpos, _ := i.(*tdposConsensus)
139139
fakeCtx := mock.NewFakeKContext(NewRevokeNominateArgs(), NewM())
140-
tdpos.runRevokeCandidate(fakeCtx)
140+
if _, err := tdpos.runRevokeCandidate(fakeCtx); err != nil {
141+
t.Fatal(err)
142+
}
141143
}
142144

143145
func TestRunVote(t *testing.T) {
@@ -148,10 +150,10 @@ func TestRunVote(t *testing.T) {
148150
}
149151
// 1. 构造term存储
150152
l, _ := cCtx.Ledger.(*kmock.FakeLedger)
151-
l.Put(kmock.NewBlock(3))
152-
l.Put(kmock.NewBlock(4))
153-
l.Put(kmock.NewBlock(5))
154-
l.Put(kmock.NewBlock(6))
153+
_ = l.Put(kmock.NewBlock(3))
154+
_ = l.Put(kmock.NewBlock(4))
155+
_ = l.Put(kmock.NewBlock(5))
156+
_ = l.Put(kmock.NewBlock(6))
155157
// 2. 整理Block的共识存储
156158
l.SetConsensusStorage(1, SetTdposStorage(1, nil))
157159
l.SetConsensusStorage(2, SetTdposStorage(1, nil))
@@ -169,7 +171,10 @@ func TestRunVote(t *testing.T) {
169171
i := NewTdposConsensus(*cCtx, getConfig(getTdposConsensusConf()))
170172
tdpos, _ := i.(*tdposConsensus)
171173
fakeCtx := mock.NewFakeKContext(NewVoteArgs(), NewM())
172-
tdpos.runVote(fakeCtx)
174+
_, err = tdpos.runVote(fakeCtx)
175+
if err != nil {
176+
t.Fatal(err)
177+
}
173178
}
174179
func TestRunRevokeVote(t *testing.T) {
175180
cCtx, err := prepare(getTdposConsensusConf())
@@ -178,11 +183,11 @@ func TestRunRevokeVote(t *testing.T) {
178183
return
179184
}
180185
// 1. 构造term存储
181-
l, _ := cCtx.Ledger.(*kmock.FakeLedger)
182-
l.Put(kmock.NewBlock(3))
183-
l.Put(kmock.NewBlock(4))
184-
l.Put(kmock.NewBlock(5))
185-
l.Put(kmock.NewBlock(6))
186+
l := cCtx.Ledger.(*kmock.FakeLedger)
187+
_ = l.Put(kmock.NewBlock(3))
188+
_ = l.Put(kmock.NewBlock(4))
189+
_ = l.Put(kmock.NewBlock(5))
190+
_ = l.Put(kmock.NewBlock(6))
186191
// 2. 整理Block的共识存储
187192
l.SetConsensusStorage(1, SetTdposStorage(1, nil))
188193
l.SetConsensusStorage(2, SetTdposStorage(1, nil))
@@ -200,5 +205,7 @@ func TestRunRevokeVote(t *testing.T) {
200205
i := NewTdposConsensus(*cCtx, getConfig(getTdposConsensusConf()))
201206
tdpos, _ := i.(*tdposConsensus)
202207
fakeCtx := mock.NewFakeKContext(NewNominateArgs(), NewM())
203-
tdpos.runRevokeVote(fakeCtx)
208+
if _, err := tdpos.runRevokeVote(fakeCtx); err != nil {
209+
t.Fatal(err)
210+
}
204211
}

bcs/consensus/tdpos/schedule.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,7 @@ func (s *tdposSchedule) calTopKNominator(height int64) ([]string, error) {
259259
return nil, err
260260
}
261261
var termBallotSli termBallotsSlice
262-
for candidate, _ := range nominateValue {
262+
for candidate := range nominateValue {
263263
candidateBallot := &termBallots{
264264
Address: candidate,
265265
}

0 commit comments

Comments
 (0)