diff --git a/.golangci.yaml b/.golangci.yaml new file mode 100644 index 00000000..520871ea --- /dev/null +++ b/.golangci.yaml @@ -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 diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 792ec2ab..f3d431bb 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -12,3 +12,8 @@ repos: .*/address )$ - id: trailing-whitespace + + - repo: https://github.com/golangci/golangci-lint + rev: v1.50.1 + hooks: + - id: golangci-lint diff --git a/bcs/consensus/tdpos/kernel_contract_test.go b/bcs/consensus/tdpos/kernel_contract_test.go index 2fbbad60..824372c3 100644 --- a/bcs/consensus/tdpos/kernel_contract_test.go +++ b/bcs/consensus/tdpos/kernel_contract_test.go @@ -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) } diff --git a/kernel/engines/xuperos/asyncworker/asyncworker_impl.go b/kernel/engines/xuperos/asyncworker/asyncworker_impl.go index fcddd3c0..57a87623 100644 --- a/kernel/engines/xuperos/asyncworker/asyncworker_impl.go +++ b/kernel/engines/xuperos/asyncworker/asyncworker_impl.go @@ -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" @@ -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() { @@ -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 @@ -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已经处理完毕,此时需要记录到游标里,避免后续事件遍历负担 @@ -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 { @@ -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 } diff --git a/kernel/engines/xuperos/asyncworker/asyncworker_test.go b/kernel/engines/xuperos/asyncworker/asyncworker_test.go index 7854e4c2..47520128 100644 --- a/kernel/engines/xuperos/asyncworker/asyncworker_test.go +++ b/kernel/engines/xuperos/asyncworker/asyncworker_test.go @@ -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) @@ -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) { @@ -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") } @@ -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() } @@ -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() diff --git a/kernel/engines/xuperos/chain.go b/kernel/engines/xuperos/chain.go index 0adc0139..85cc59d0 100644 --- a/kernel/engines/xuperos/chain.go +++ b/kernel/engines/xuperos/chain.go @@ -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) @@ -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()) } @@ -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) diff --git a/kernel/engines/xuperos/engine.go b/kernel/engines/xuperos/engine.go index 3284c37c..8bee2c7d 100644 --- a/kernel/engines/xuperos/engine.go +++ b/kernel/engines/xuperos/engine.go @@ -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) diff --git a/kernel/engines/xuperos/engine_test.go b/kernel/engines/xuperos/engine_test.go index 8fd29bd3..b05f1080 100644 --- a/kernel/engines/xuperos/engine_test.go +++ b/kernel/engines/xuperos/engine_test.go @@ -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) {