Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support go lint #395

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .github/workflows/backport.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,3 @@ jobs:
uses: tibdex/backport@v1
with:
github_token: ${{ secrets.GITHUB_TOKEN }}

34 changes: 17 additions & 17 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -1,26 +1,26 @@
on: [push, pull_request]
on: [ push, pull_request ]
name: CI
jobs:
test:
strategy:
matrix:
go-version: [1.13.x]
platform: [ubuntu-latest, macos-latest]
go-version: [ 1.13.x ]
platform: [ ubuntu-latest, macos-latest ]
runs-on: ${{ matrix.platform }}
steps:
- name: Install Go
uses: actions/setup-go@v1
with:
go-version: ${{ matrix.go-version }}
- name: Install Go
uses: actions/setup-go@v1
with:
go-version: ${{ matrix.go-version }}

- name: Checkout code
uses: actions/checkout@v2
- name: Test
run: |
diff -u <(echo -n) <(gofmt -d $(find . -type f -name '*.go'))
make && make test
- name: Checkout code
uses: actions/checkout@v2
- name: 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
with:
file: ./coverage.txt
- name: Upload coverage to Codecov
uses: codecov/codecov-action@v1
with:
file: ./coverage.txt
20 changes: 20 additions & 0 deletions .github/workflows/pre-commit.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
name: pre-commit

on:
pull_request:
push:
branches: [master]

jobs:
pre-commit:
strategy:
matrix:
go-version: [ 1.19.x ]
platform: [ ubuntu-latest ]
runs-on: ${{ matrix.platform }}
steps:
- uses: actions/checkout@v3
- uses: actions/setup-python@v3
- uses: pre-commit/[email protected]
with:
extra_args: -c .pre-commit-config.yaml
19 changes: 19 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.4.0
hooks:
- id: check-yaml
- id: end-of-file-fixer
exclude: |
(?x)^(
.*.key|
.*.bin|
.*.abi|
.*/address
)$
- id: trailing-whitespace

- repo: https://github.com/golangci/golangci-lint
rev: v1.50.1
hooks:
- id: golangci-lint
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
## 特点

#### 1. 广域适用

XuperCore通过极致的可扩展性,做到“广域场景适用”的区块链技术。基于XuperCore可以多纬度的自由扩展订制,非常便捷、快速的构建起适用于各类场景的区块链。开发者可以根据自己实际场景的需要,非常自由、多纬度的扩展。可选择对部分内核组件做订制;也可以基于标准组件订制自己的区块链引擎;也可以基于标准引擎,轻量级的订制自己的区块链实现。从而满足开发者的各纬度的需要,做到广域场景适用。

#### 2. 高可扩展

XuperCore通过“动态内核技术”,做到了区块链内核核心流程和核心组件,都可以没有内核框架代码侵入的自由扩展替换,支持多纬度的自由扩展,让整个内核具备极好的可扩展性。通过对共识、账本、合约等这些内核组件做抽象,制定了区块链内核组件编程规范,这些规范就像主板上的插槽,起到承上启下的作用,让内核各核心组件可以自由的扩展替换,同时让引擎订制变得非常的轻量级。再在内核核心组件编程规范的基础上,设计了多引擎架构,让内核核心处理流程和技术选型也可以无内核代码侵入的自由扩展替换。
Expand Down
2 changes: 0 additions & 2 deletions bcs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,3 @@

负责区块链核心组件具体实现,支持无内核代码侵入插拔,可自由扩展替换。
新组件开发只需要满该子领域足内核定义的组件编程规范即可。


4 changes: 0 additions & 4 deletions bcs/consensus/mock/mock_consensus.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package mock

import (
"errors"
"path/filepath"
"time"

Expand All @@ -17,12 +16,9 @@ import (

var (
BcName = "xuper5"
nodeIp = "/ip4/127.0.0.1/tcp/47101/p2p/QmVcSF4F7rTdsvUJqsik98tXRXMBUqL5DSuBpyYKVhjuG4"
priKey = `{"Curvname":"P-256","X":74695617477160058757747208220371236837474210247114418775262229497812962582435,"Y":51348715319124770392993866417088542497927816017012182211244120852620959209571,"D":29079635126530934056640915735344231956621504557963207107451663058887647996601}`
PubKey = `{"Curvname":"P-256","X":74695617477160058757747208220371236837474210247114418775262229497812962582435,"Y":51348715319124770392993866417088542497927816017012182211244120852620959209571}`
Miner = "dpzuVdosQrF2kmzumhVeFQZa1aYcdgFpN"

blockSetItemErr = errors.New("item invalid")
)

func NewFakeLogger() logs.Logger {
Expand Down
15 changes: 8 additions & 7 deletions bcs/consensus/pow/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@ import (
)

// PoWConfig pow需要解析的创始块解析格式
// 根据Bitcoin推荐
// AdjustHeightGap: 2016,
// MaxTarget: 0x1d00FFFF,
// DefaultTarget: 0x207FFFFF
//
// 根据Bitcoin推荐
// AdjustHeightGap: 2016,
// MaxTarget: 0x1d00FFFF,
// DefaultTarget: 0x207FFFFF
type PoWConfig struct {
DefaultTarget uint32 `json:"defaultTarget"`
AdjustHeightGap int32 `json:"adjustHeightGap"`
Expand Down Expand Up @@ -62,7 +63,7 @@ func GetCompact(number *big.Int) (uint32, bool) {
func SetCompact(nCompact uint32) (*big.Int, bool, bool) {
nSize := nCompact >> 24
nWord := new(big.Int)
u := new(big.Int)
var u *big.Int
nCompactInt := big.NewInt(int64(nCompact))
// 0x00800000是一个符号位,故nWord仅为后23位
lowBits := big.NewInt(0x007fffff)
Expand Down Expand Up @@ -99,14 +100,14 @@ func unmarshalPowConfig(input []byte) (*PoWConfig, error) {
"defaultTarget": 0,
"maxTarget": 0,
}
for k, _ := range int32Map {
for k := range int32Map {
value, err := strconv.ParseInt(consCfg[k].(string), 10, 32)
if err != nil {
return nil, fmt.Errorf("marshal consensus config failed key %s set error", k)
}
int32Map[k] = int32(value)
}
for k, _ := range uint32Map {
for k := range uint32Map {
value, err := strconv.ParseInt(consCfg[k].(string), 10, 64)
if err != nil {
return nil, fmt.Errorf("marshal consensus config failed key %s set error", k)
Expand Down
18 changes: 9 additions & 9 deletions bcs/consensus/pow/pow.go
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ func (pow *PoWConsensus) CheckMinerMatch(ctx xcontext.XContext, block context.Bl
}
// 跟address比较
chkResult, _ := pow.Crypto.VerifyAddressUsingPublicKey(string(block.GetProposer()), k)
if chkResult == false {
if !chkResult {
ctx.GetLog().Warn("PoW::CheckMinerMatch::address is not match publickey", "miner", string(block.GetProposer()))
return false, err
}
Expand All @@ -245,7 +245,8 @@ func (pow *PoWConsensus) ProcessBeforeMiner(height, timestamp int64) ([]byte, []
}
bits, err := pow.refreshDifficulty(preBlock.GetBlockid(), tipHeight+1)
if err != nil {
pow.Stop()
// TODO: deal with error
_ = pow.Stop()
}
pow.targetBits = bits
store := &PoWStorage{
Expand Down Expand Up @@ -392,7 +393,7 @@ func (pow *PoWConsensus) refreshDifficulty(tipHash []byte, nextHeight int64) (ui
return newTargetBits, nil
}

//IsProofed check workload proof
// IsProofed check workload proof
func (pow *PoWConsensus) IsProofed(blockID []byte, targetBits uint32) bool {
hash := new(big.Int)
hash.SetBytes(blockID)
Expand All @@ -409,10 +410,7 @@ func (pow *PoWConsensus) IsProofed(blockID []byte, targetBits uint32) bool {
// 原xuperchain逻辑
target := big.NewInt(1)
target.Lsh(target, uint(256-targetBits))
if hash.Cmp(target) == 1 {
return false
}
return true
return hash.Cmp(target) != 1
}

// mining 为带副作用的函数,将直接对block进行操作,更改其原始值
Expand Down Expand Up @@ -440,14 +438,16 @@ func (pow *PoWConsensus) mining(task *mineTask) {
return
}
if pow.IsProofed(bid, pow.targetBits) {
task.block.SetItem("blockid", bid)
// TODO: deal with error
_ = task.block.SetItem("blockid", bid)
// 签名重置
s, err := pow.Crypto.SignECDSA(pow.Address.PrivateKey, bid)
if err != nil {
task.doDone(BlockSignErr)
return
}
task.block.SetItem("sign", s)
// TODO: deal with error
_ = task.block.SetItem("sign", s)
task.doDone(nil)
return
}
Expand Down
17 changes: 13 additions & 4 deletions bcs/consensus/pow/pow_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -276,8 +276,12 @@ func TestMining(t *testing.T) {
return
}
powC.targetBits = minTarget
powC.Start()
defer powC.Stop()
if err := powC.Start(); err != nil {
t.Fatal(err)
}
defer func() {
_ = powC.Stop()
}()
ps := PoWStorage{
TargetBits: minTarget,
}
Expand Down Expand Up @@ -315,7 +319,7 @@ func TestRefreshDifficulty(t *testing.T) {
t.Error("NewBlock error", err)
return
}
l, ok := powC.Ledger.(*kmock.FakeLedger)
l := powC.Ledger.(*kmock.FakeLedger)
err = l.Put(genesisB)
if err != nil {
t.Error("TestRefreshDifficulty put genesis err", "err", err)
Expand Down Expand Up @@ -409,6 +413,9 @@ func TestCheckMinerMatch(t *testing.T) {
}
by, _ := json.Marshal(ps)
b3, err := bmock.NewBlockWithStorage(3, cCtx.Crypto, cCtx.Address, by)
if err != nil {
t.Fatal(err)
}
c := cCtx.BaseCtx
_, err = i.CheckMinerMatch(&c, b3)
if err != nil {
Expand All @@ -423,5 +430,7 @@ func TestCompeteMaster(t *testing.T) {
return
}
i := NewPoWConsensus(*cCtx, getConsensusConf(getPoWConsensusConf()))
i.CompeteMaster(3)
if _, _, err := i.CompeteMaster(3); err != nil {
t.Fatal(err)
}
}
2 changes: 1 addition & 1 deletion bcs/consensus/single/single.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ func (s *SingleConsensus) CheckMinerMatch(ctx xcontext.XContext, block cctx.Bloc
return false, err
}
chkResult, _ := s.ctx.Crypto.VerifyAddressUsingPublicKey(string(block.GetProposer()), k)
if chkResult == false {
if !chkResult {
ctx.GetLog().Warn("Single::CheckMinerMatch::address is not match publickey")
return false, err
}
Expand Down
12 changes: 9 additions & 3 deletions bcs/consensus/single/single_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,15 @@ func TestNewSingleConsensus(t *testing.T) {
if i := NewSingleConsensus(*cCtx, getWrongConsensusConf()); i != nil {
t.Error("NewSingleConsensus check name error")
}
i.Stop()
i.Start()
i.ProcessBeforeMiner(0, time.Now().UnixNano())
if err := i.Stop(); err != nil {
t.Fatal(err)
}
if err := i.Start(); err != nil {
t.Fatal(err)
}
if _, _, err := i.ProcessBeforeMiner(0, time.Now().UnixNano()); err != nil {
t.Fatal(err)
}
cCtx.XLog = nil
i = NewSingleConsensus(*cCtx, conf)
if i != nil {
Expand Down
2 changes: 1 addition & 1 deletion bcs/consensus/tdpos/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ func buildConfigs(input []byte) (*tdposConfig, error) {
"block_num": 0,
"timestamp": 0,
}
for k, _ := range int64Map {
for k := range int64Map {
if _, ok := consCfg[k]; !ok {
if k == "version" {
continue
Expand Down
8 changes: 5 additions & 3 deletions bcs/consensus/tdpos/kernel_contract.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,8 @@ func (tp *tdposConsensus) runRevokeCandidate(contractCtx contract.KContext) (*co

// runVote 执行投票
// Args: candidate::候选人钱包地址
// amount::投票者票数
//
// amount::投票者票数
func (tp *tdposConsensus) runVote(contractCtx contract.KContext) (*contract.Response, error) {
// 1.1 验证合约参数是否正确
candidateName, err := tp.checkArgs(contractCtx.Args())
Expand Down Expand Up @@ -253,7 +254,8 @@ func (tp *tdposConsensus) runVote(contractCtx contract.KContext) (*contract.Resp
// runRevokeVote 执行选票撤销
// 重构后的候选人撤销
// Args: candidate::候选人钱包地址
// amount: 投票数
//
// amount: 投票数
func (tp *tdposConsensus) runRevokeVote(contractCtx contract.KContext) (*contract.Response, error) {
// 1.1 验证合约参数
candidateName, err := tp.checkArgs(contractCtx.Args())
Expand Down Expand Up @@ -364,7 +366,7 @@ func (tp *tdposConsensus) runGetTdposInfos(contractCtx contract.KContext) (*cont

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