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

Enable go-lint for local pre-commit #401

Merged
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
16 changes: 16 additions & 0 deletions .golangci.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# see: https://golangci-lint.run/usage/configuration/
run:
skip-dirs: # TODO: update after corrected
- bcs
- example
- lib
- kernel/contract
- kernel/common
- kernel/consensus
- kernel/network
- kernel/permission
- kernel/engines/xuperos/common
- kernel/engines/xuperos/event
- kernel/engines/xuperos/miner
- kernel/engines/xuperos/net
- kernel/engines/xuperos/parachain
5 changes: 5 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,8 @@ repos:
.*/address
)$
- id: trailing-whitespace

- repo: https://github.com/golangci/golangci-lint
rev: v1.50.1
hooks:
- id: golangci-lint
2 changes: 1 addition & 1 deletion bcs/consensus/tdpos/kernel_contract_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ func TestRunRevokeVote(t *testing.T) {
l.SetSnapshot(tdposBucket, []byte(vote_prefix+"akf7qunmeaqb51Wu418d6TyPKp4jdLdpV"), VoteKey3())

i := NewTdposConsensus(*cCtx, getConfig(getTdposConsensusConf()))
tdpos, _ := i.(*tdposConsensus)
tdpos := i.(*tdposConsensus)
fakeCtx := mock.NewFakeKContext(NewNominateArgs(), NewM())
tdpos.runRevokeVote(fakeCtx)
}
35 changes: 15 additions & 20 deletions kernel/engines/xuperos/asyncworker/asyncworker_impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ import (
"sync"
"time"

"github.com/golang/protobuf/proto"
"github.com/golang/protobuf/proto" //nolint:staticcheck

"github.com/xuperchain/xupercore/bcs/ledger/xledger/def"
"github.com/xuperchain/xupercore/kernel/engines/xuperos/common"
"github.com/xuperchain/xupercore/kernel/engines/xuperos/event"
Expand Down Expand Up @@ -140,12 +141,9 @@ func (aw *AsyncWorkerImpl) Start() (err error) {
}

go func() {
select {
case <-aw.close:
iter.Close()
aw.log.Warn("async task loop shut down.")
return
}
<-aw.close
iter.Close()
aw.log.Warn("async task loop shut down.")
}()

go func() {
Expand All @@ -161,11 +159,12 @@ func (aw *AsyncWorkerImpl) Start() (err error) {
break
}
// 当且仅当断点有效,且当前高度为断点存储高度时,需要过滤部分已做异步任务
var curBlockCursor *asyncWorkerCursor
if cursor != nil && block.BlockHeight == cursor.BlockHeight {
aw.doAsyncTasks(block.Txs, block.BlockHeight, cursor)
continue
curBlockCursor = cursor
}
aw.doAsyncTasks(block.Txs, block.BlockHeight, nil)
// TODO: deal with error
_ = aw.doAsyncTasks(block.Txs, block.BlockHeight, curBlockCursor)
}
}()
return
Expand Down Expand Up @@ -201,15 +200,14 @@ func (aw *AsyncWorkerImpl) doAsyncTasks(txs []*protos.FilteredTransaction, heigh
}
aw.log.Info("do async task success", "contract", event.Contract, "event", event.Name,
"txIndex", index, "eventIndex", eventIndex)

// 执行完毕后进行持久化
newCursor := asyncWorkerCursor{
BlockHeight: height,
TxIndex: int64(index),
EventIndex: int64(eventIndex),
}
if err := aw.storeCursor(newCursor); err != nil {
continue
}
_ = aw.storeCursor(newCursor) // ignore error which logged inside method
}
}
// 该block已经处理完毕,此时需要记录到游标里,避免后续事件遍历负担
Expand All @@ -218,10 +216,7 @@ func (aw *AsyncWorkerImpl) doAsyncTasks(txs []*protos.FilteredTransaction, heigh
TxIndex: lastTxIndex,
EventIndex: lastEventIndex,
}
if err := aw.storeCursor(newCursor); err != nil {
return err
}
return nil
return aw.storeCursor(newCursor)
}

func (aw *AsyncWorkerImpl) storeCursor(cursor asyncWorkerCursor) error {
Expand Down Expand Up @@ -281,7 +276,7 @@ func (aw *AsyncWorkerImpl) Stop() {
}

type asyncWorkerCursor struct {
BlockHeight int64 `json:"block_height,required"`
TxIndex int64 `json:"tx_index,required"`
EventIndex int64 `json:"event_index,required"`
BlockHeight int64 `json:"block_height,required"` //nolint:staticcheck
TxIndex int64 `json:"tx_index,required"` //nolint:staticcheck
EventIndex int64 `json:"event_index,required"` //nolint:staticcheck
}
18 changes: 12 additions & 6 deletions kernel/engines/xuperos/asyncworker/asyncworker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ func TestCursor(t *testing.T) {
t.Errorf("marshal cursor failed when doAsyncTasks, err=%v", err)
return
}
aw.finishTable.Put([]byte(testBcName), cursorBuf)
_ = aw.finishTable.Put([]byte(testBcName), cursorBuf)
cursor, err = aw.reloadCursor()
if err != nil {
t.Errorf("reloadCursor err=%v", err)
Expand All @@ -153,9 +153,12 @@ func TestCursor(t *testing.T) {
t.Errorf("reloadCursor value error")
return
}
aw.storeCursor(asyncWorkerCursor{
err = aw.storeCursor(asyncWorkerCursor{
BlockHeight: 10,
})
if err != nil {
t.Fatal(err)
}
}

func TestDoAsyncTasks(t *testing.T) {
Expand Down Expand Up @@ -189,8 +192,10 @@ func TestDoAsyncTasks(t *testing.T) {
EventIndex: int64(0),
}
cursorBuf, _ := json.Marshal(cursor)
aw.finishTable.Put([]byte(testBcName), cursorBuf)
aw.doAsyncTasks(newTxs(), 5, cursor)
_ = aw.finishTable.Put([]byte(testBcName), cursorBuf)
if err := aw.doAsyncTasks(newTxs(), 5, cursor); err != nil {
t.Fatal(err)
}
if cursor.BlockHeight != 5 || cursor.TxIndex != 1 || cursor.EventIndex != 0 {
t.Errorf("doAsyncTasks block break cursor error")
}
Expand All @@ -206,7 +211,8 @@ func TestStartAsyncTask(t *testing.T) {
aw.finishTable = th.db
aw.log = th.log
aw.RegisterHandler("$parachain", "CreateBlockChain", handleCreateChain)
aw.Start()
// TODO: deal with error
_ = aw.Start()
aw.Stop()
}

Expand All @@ -219,7 +225,7 @@ type TestHelper struct {
func NewTestHelper() (*TestHelper, error) {
basedir, err := ioutil.TempDir("", "asyncworker-test")
if err != nil {
panic(err)
return nil, err
}

dir := utils.GetCurFileDir()
Expand Down
12 changes: 8 additions & 4 deletions kernel/engines/xuperos/chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,14 +201,16 @@ func (t *Chain) PreExec(ctx xctx.XContext, reqs []*protos.InvokeRequest, initiat

resp, err := context.Invoke(req.MethodName, req.Args)
if err != nil {
context.Release()
// TODO: deal with error
_ = context.Release()
ctx.GetLog().Error("PreExec Invoke error", "error", err, "contractName", req.ContractName)
metrics.ContractInvokeCounter.WithLabelValues(t.ctx.BCName, req.ModuleName, req.ContractName, req.MethodName, "InvokeError").Inc()
return nil, common.ErrContractInvokeFailed.More("%v", err)
}

if resp.Status >= 400 && i < len(reservedRequests) {
context.Release()
// TODO: deal with error
_ = context.Release()
ctx.GetLog().Error("PreExec Invoke error", "status", resp.Status, "contractName", req.ContractName)
metrics.ContractInvokeCounter.WithLabelValues(t.ctx.BCName, req.ModuleName, req.ContractName, req.MethodName, "InvokeError").Inc()
return nil, common.ErrContractInvokeFailed.More("%v", resp.Message)
Expand All @@ -234,7 +236,8 @@ func (t *Chain) PreExec(ctx xctx.XContext, reqs []*protos.InvokeRequest, initiat
responses = append(responses, response)
responseBodes = append(responseBodes, resp.Body)

context.Release()
// TODO: deal with error
_ = context.Release()
metrics.ContractInvokeHistogram.WithLabelValues(t.ctx.BCName, req.ModuleName, req.ContractName, req.MethodName).Observe(time.Since(beginTime).Seconds())
}

Expand Down Expand Up @@ -319,7 +322,8 @@ func (t *Chain) ProcBlock(ctx xctx.XContext, block *lpb.InternalBlock) error {
}

log := ctx.GetLog()
err := t.miner.ProcBlock(ctx, block)
// TODO: replace deprecated method
err := t.miner.ProcBlock(ctx, block) //nolint:staticcheck
if err != nil {
if common.CastError(err).Equal(common.ErrForbidden) {
log.Trace("forbidden process block", "blockid", utils.F(block.GetBlockid()), "err", err)
Expand Down
4 changes: 3 additions & 1 deletion kernel/engines/xuperos/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,9 @@ func (t *Engine) loadChains() error {
t.log.Error("create parachain mgmt error", "bcName", rootChain, "err", err)
return fmt.Errorf("create parachain error")
}
aw.Start()
if err = aw.Start(); err != nil {
return err
}
}

t.log.Trace("load chain succeeded", "chain", fInfo.Name(), "dir", chainDir)
Expand Down
4 changes: 1 addition & 3 deletions kernel/engines/xuperos/engine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,11 @@ func CreateLedger(conf *xconf.EnvConf) error {
return nil
}

func RemoveLedger(conf *xconf.EnvConf) error {
func RemoveLedger(conf *xconf.EnvConf) {
path := conf.GenDataAbsPath("blockchain")
if err := os.RemoveAll(path); err != nil {
log.Printf("remove ledger failed.err:%v\n", err)
return err
}
return nil
}

func MockEngine(path string) (common.Engine, error) {
Expand Down