From 7bcd645ecd9f5aad9fd1852f1e33c1b11e413638 Mon Sep 17 00:00:00 2001 From: coinplug-jhjin Date: Thu, 29 Sep 2022 11:50:35 +0900 Subject: [PATCH] build: upgrade to golangci-lint 1.49, improved code with lint checks --- .golangci.yml | 6 ++- build/checksums.txt | 71 +++++++++++++++++----------------- build/ci.go | 23 ++++++----- common/fdlimit/fdlimit_test.go | 3 +- core/vm/contracts_test.go | 2 +- miner/worker.go | 15 +++---- wemix/admin.go | 51 ++++++++++++------------ wemix/etcdutil.go | 15 ++++--- wemix/sync.go | 35 ++++++++--------- 9 files changed, 110 insertions(+), 111 deletions(-) diff --git a/.golangci.yml b/.golangci.yml index 4950b98c21ba..1a51f2a52463 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -8,11 +8,13 @@ run: skip-dirs-use-default: true skip-files: - core/genesis_alloc.go + - wemix/contracts/WemixGovernance.js + - wemix/governance_abi.go linters: disable-all: true enable: - - deadcode + # - deadcode - goconst - goimports - gosimple @@ -22,7 +24,7 @@ linters: # - staticcheck - unconvert # - unused - - varcheck + # - varcheck linters-settings: gofmt: diff --git a/build/checksums.txt b/build/checksums.txt index 5df27bbf6173..4b7056e119dd 100644 --- a/build/checksums.txt +++ b/build/checksums.txt @@ -1,37 +1,36 @@ -# This file contains sha256 checksums of optional build dependencies. +27871baa490f3401414ad793fba49086f6c855b1c584385ed7771e1204c7e179 go1.19.1.src.tar.gz +b2828a2b05f0d2169afc74c11ed010775bf7cf0061822b275697b2f470495fb7 go1.19.1.darwin-amd64.tar.gz +e46aecce83a9289be16ce4ba9b8478a5b89b8aa0230171d5c6adbc0c66640548 go1.19.1.darwin-arm64.tar.gz +cfaca8c1d5784d2bc21e12d8893cfd2dc885a60db4c1a9a95e4ffc694d0925ce go1.19.1.freebsd-386.tar.gz +db5b8f232e12c655cc6cde6af1adf4d27d842541807802d747c86161e89efa0a go1.19.1.freebsd-amd64.tar.gz +9acc57342400c5b0c2da07b5b01b50da239dd4a7fad41a1fb56af8363ef4133f go1.19.1.linux-386.tar.gz +acc512fbab4f716a8f97a8b3fbaa9ddd39606a28be6c2515ef7c6c6311acffde go1.19.1.linux-amd64.tar.gz +49960821948b9c6b14041430890eccee58c76b52e2dbaafce971c3c38d43df9f go1.19.1.linux-arm64.tar.gz +efe93f5671621ee84ce5e262e1e21acbc72acefbaba360f21778abd083d4ad16 go1.19.1.linux-armv6l.tar.gz +4137984aa353de9c5ec1bd8fb3cd00a0624b75eafa3d4ec13d2f3f48261dba2e go1.19.1.linux-ppc64le.tar.gz +ca1005cc80a3dd726536b4c6ea5fef0318939351ff273eff420bd66a377c74eb go1.19.1.linux-s390x.tar.gz +bc7043e7a9a8d34aacd06f8c2f70e166d1d148f6800814cff790c45b9ab31cee go1.19.1.windows-386.zip +b33584c1d93b0e9c783de876b7aa99d3018bdeccd396aeb6d516a74e9d88d55f go1.19.1.windows-amd64.zip +d8cf3f04762fa7d5d9c82dfa15b5adaae2404463af3bc8dcd7f89837512501fe go1.19.1.windows-arm64.zip -3defb9a09bed042403195e872dcbc8c6fae1485963332279668ec52e80a95a2d go1.17.5.src.tar.gz -2db6a5d25815b56072465a2cacc8ed426c18f1d5fc26c1fc8c4f5a7188658264 go1.17.5.darwin-amd64.tar.gz -111f71166de0cb8089bb3e8f9f5b02d76e1bf1309256824d4062a47b0e5f98e0 go1.17.5.darwin-arm64.tar.gz -443c1cd9768df02085014f1eb034ebc7dbe032ffc8a9bb9f2e6617d037eee23c go1.17.5.freebsd-386.tar.gz -17180bdc4126acffd0ebf86d66ef5cbc3488b6734e93374fb00eb09494e006d3 go1.17.5.freebsd-amd64.tar.gz -4f4914303bc18f24fd137a97e595735308f5ce81323c7224c12466fd763fc59f go1.17.5.linux-386.tar.gz -bd78114b0d441b029c8fe0341f4910370925a4d270a6a590668840675b0c653e go1.17.5.linux-amd64.tar.gz -6f95ce3da40d9ce1355e48f31f4eb6508382415ca4d7413b1e7a3314e6430e7e go1.17.5.linux-arm64.tar.gz -aa1fb6c53b4fe72f159333362a10aca37ae938bde8adc9c6eaf2a8e87d1e47de go1.17.5.linux-armv6l.tar.gz -3d4be616e568f0a02cb7f7769bcaafda4b0969ed0f9bb4277619930b96847e70 go1.17.5.linux-ppc64le.tar.gz -8087d4fe991e82804e6485c26568c2e0ee0bfde00ceb9015dc86cb6bf84ef40b go1.17.5.linux-s390x.tar.gz -6d7b9948ee14a906b14f5cbebdfab63cd6828b0b618160847ecd3cc3470a26fe go1.17.5.windows-386.zip -671faf99cd5d81cd7e40936c0a94363c64d654faa0148d2af4bbc262555620b9 go1.17.5.windows-amd64.zip -45e88676b68e9cf364be469b5a27965397f4e339aa622c2f52c10433c56e5030 go1.17.5.windows-arm64.zip - -d4bd25b9814eeaa2134197dd2c7671bb791eae786d42010d9d788af20dee4bfa golangci-lint-1.42.0-darwin-amd64.tar.gz -e56859c04a2ad5390c6a497b1acb1cc9329ecb1010260c6faae9b5a4c35b35ea golangci-lint-1.42.0-darwin-arm64.tar.gz -14d912a3fa856830339472fc4dc341933adf15f37bdb7130bbbfcf960ecf4809 golangci-lint-1.42.0-freebsd-386.tar.gz -337257fccc9baeb5ee1cd7e70c153e9d9f59d3afde46d631659500048afbdf80 golangci-lint-1.42.0-freebsd-amd64.tar.gz -6debcc266b629359fdd8eef4f4abb05a621604079d27016265afb5b4593b0eff golangci-lint-1.42.0-freebsd-armv6.tar.gz -878f0e190169db2ce9dde8cefbd99adc4fe28b90b68686bbfcfcc2085e6d693e golangci-lint-1.42.0-freebsd-armv7.tar.gz -42c78e31faf62b225363eff1b1d2aa74f9dbcb75686c8914aa3e90d6af65cece golangci-lint-1.42.0-linux-386.tar.gz -6937f62f8e2329e94822dc11c10b871ace5557ae1fcc4ee2f9980cd6aecbc159 golangci-lint-1.42.0-linux-amd64.tar.gz -2cf8d23d96cd854a537b355dab2962b960b88a06b615232599f066afd233f246 golangci-lint-1.42.0-linux-arm64.tar.gz -08b003d1ed61367473886defc957af5301066e62338e5d96a319c34dadc4c1d1 golangci-lint-1.42.0-linux-armv6.tar.gz -c7c00ec4845e806a1f32685f5b150219e180bd6d6a9d584be8d27f0c41d7a1bf golangci-lint-1.42.0-linux-armv7.tar.gz -3650fcf29eb3d8ee326d77791a896b15259eb2d5bf77437dc72e7efe5af6bd40 golangci-lint-1.42.0-linux-mips64.tar.gz -f51ae003fdbca4fef78ba73e2eb736a939c8eaa178cd452234213b489da5a420 golangci-lint-1.42.0-linux-mips64le.tar.gz -1b0bb7b8b22cc4ea7da44fd5ad5faaf6111d0677e01cc6f961b62a96537de2c6 golangci-lint-1.42.0-linux-ppc64le.tar.gz -8cb56927eb75e572450efbe0ff0f9cf3f56dc9faa81d9e8d30d6559fc1d06e6d golangci-lint-1.42.0-linux-riscv64.tar.gz -5ac41cd31825a176b21505a371a7b307cd9cdf17df0f35bbb3bf1466f9356ccc golangci-lint-1.42.0-linux-s390x.tar.gz -e1cebd2af621ac4b64c20937df92c3819264f2174c92f51e196db1e64ae097e0 golangci-lint-1.42.0-windows-386.zip -7e70fcde8e87a17cae0455df07d257ebc86669f3968d568e12727fa24bbe9883 golangci-lint-1.42.0-windows-amd64.zip -59da7ce1bda432616bfc28ae663e52c3675adee8d9bf5959fafd657c159576ab golangci-lint-1.42.0-windows-armv6.zip -65f62dda937bfcede0326ac77abe947ce1548931e6e13298ca036cb31f224db5 golangci-lint-1.42.0-windows-armv7.zip +20cd1215e0420db8cfa94a6cd3c9d325f7b39c07f2415a02d111568d8bc9e271 golangci-lint-1.49.0-darwin-amd64.tar.gz +cabb1a4c35fe1dadbe5a81550a00871281a331e7660cd85ae16e936a7f0f6cfc golangci-lint-1.49.0-darwin-arm64.tar.gz +f834c3b09580cf763b5d30b0c33c83cb13d7a822b5ed5d724143f121ffe28c97 golangci-lint-1.49.0-freebsd-386.tar.gz +4ca91c9f3aa79a71da441b7220a3e799365ff7a24caf9f04fcda12066c5ab0f7 golangci-lint-1.49.0-freebsd-amd64.tar.gz +37de789245248eea375d05080e11b4662a08762c353752575167611e65658454 golangci-lint-1.49.0-freebsd-armv6.tar.gz +3abed2bd3a8134b501fdc9cc9a0e60d616c86389e4fcdd1f79ceae7458974378 golangci-lint-1.49.0-freebsd-armv7.tar.gz +ef2860d90d83aee6713f697f23372cd93ac41a16439fdcb3c4ac86ba0f306860 golangci-lint-1.49.0-linux-386.tar.gz +5badc6e9fee2003621efa07e385910d9a88c89b38f6c35aded153193c5125178 golangci-lint-1.49.0-linux-amd64.tar.gz +b57ed03d29b8ca69be9925edd67ea305b6013cd5c97507d205fbe2979f71f2b5 golangci-lint-1.49.0-linux-arm64.tar.gz +4a41cff3af7f5304751f7bbf4ea617c14ebc1f88481a28a013e61b06d1f7102c golangci-lint-1.49.0-linux-armv6.tar.gz +14a9683af483ee7052dd0ce7d6140e0b502d6001bea3de606b8e7cce2c673539 golangci-lint-1.49.0-linux-armv7.tar.gz +33edf757bc2611204fdb40b212900866a57ded4eea62c1b19c10bfc375359afa golangci-lint-1.49.0-linux-mips64.tar.gz +280f7902f90d162566f1691a300663dd8db6e225e65384fe66b6fb2362e0b314 golangci-lint-1.49.0-linux-mips64le.tar.gz +103bcb7ce6c668e0a7e95e5c5355892d74f5d15391443430472e66d652906a15 golangci-lint-1.49.0-linux-ppc64le.tar.gz +4636ff9b01ddb18a2c1a953fc134207711b0a5d874d04ac66f915e9cfff0e8e0 golangci-lint-1.49.0-linux-riscv64.tar.gz +029e0844931a2d3edc771d67e17fe17928f04f80c1a9aa165160a543e8a7e8d4 golangci-lint-1.49.0-linux-s390x.tar.gz +e9cb6f691e62a4d8b28dd52d2eab57cca72acfd5083b3c5417a72d2eb64def09 golangci-lint-1.49.0-windows-386.zip +d058dfb0c7fbd73be70f285d3f8d4d424192fe9b19760ddbb0b2c4b743b8656c golangci-lint-1.49.0-windows-amd64.zip +c049d80297228db7065eabeac5114f77f04415dcd9b944e8d7c6426d9dd6e9dd golangci-lint-1.49.0-windows-arm64.zip +ec9164bab7134ddb94f51c17fd37c109b0801ecd5494b6c0e27ca7898fbd7469 golangci-lint-1.49.0-windows-armv6.zip +68fd9e880d98073f436c58b6f6d2c141881ef49b06ca31137bc19da4e4e3b996 golangci-lint-1.49.0-windows-armv7.zip \ No newline at end of file diff --git a/build/ci.go b/build/ci.go index 381f6115b385..dec9a6aa99de 100644 --- a/build/ci.go +++ b/build/ci.go @@ -24,19 +24,18 @@ Usage: go run build/ci.go Available commands are: - install [ -arch architecture ] [ -cc compiler ] [ packages... ] -- builds packages and executables - test [ -coverage ] [ packages... ] -- runs the tests - lint -- runs certain pre-selected linters - archive [ -arch architecture ] [ -type zip|tar ] [ -signer key-envvar ] [ -signify key-envvar ] [ -upload dest ] -- archives build artifacts - importkeys -- imports signing keys from env - debsrc [ -signer key-id ] [ -upload dest ] -- creates a debian source package - nsis -- creates a Windows NSIS installer - aar [ -local ] [ -sign key-id ] [-deploy repo] [ -upload dest ] -- creates an Android archive - xcode [ -local ] [ -sign key-id ] [-deploy repo] [ -upload dest ] -- creates an iOS XCode framework - purge [ -store blobstore ] [ -days threshold ] -- purges old archives from the blobstore + install [ -arch architecture ] [ -cc compiler ] [ packages... ] -- builds packages and executables + test [ -coverage ] [ packages... ] -- runs the tests + lint -- runs certain pre-selected linters + archive [ -arch architecture ] [ -type zip|tar ] [ -signer key-envvar ] [ -signify key-envvar ] [ -upload dest ] -- archives build artifacts + importkeys -- imports signing keys from env + debsrc [ -signer key-id ] [ -upload dest ] -- creates a debian source package + nsis -- creates a Windows NSIS installer + aar [ -local ] [ -sign key-id ] [-deploy repo] [ -upload dest ] -- creates an Android archive + xcode [ -local ] [ -sign key-id ] [-deploy repo] [ -upload dest ] -- creates an iOS XCode framework + purge [ -store blobstore ] [ -days threshold ] -- purges old archives from the blobstore For all commands, -n prevents execution of external programs (dry run mode). - */ package main @@ -337,7 +336,7 @@ func doLint(cmdline []string) { // downloadLinter downloads and unpacks golangci-lint. func downloadLinter(cachedir string) string { - const version = "1.42.0" + const version = "1.49.0" csdb := build.MustLoadChecksums("build/checksums.txt") arch := runtime.GOARCH diff --git a/common/fdlimit/fdlimit_test.go b/common/fdlimit/fdlimit_test.go index 21362b8463a3..9fd5e9fc3cbd 100644 --- a/common/fdlimit/fdlimit_test.go +++ b/common/fdlimit/fdlimit_test.go @@ -17,7 +17,6 @@ package fdlimit import ( - "fmt" "testing" ) @@ -30,7 +29,7 @@ func TestFileDescriptorLimits(t *testing.T) { t.Fatal(err) } if hardlimit < target { - t.Skip(fmt.Sprintf("system limit is less than desired test target: %d < %d", hardlimit, target)) + t.Skipf("system limit is less than desired test target: %d < %d", hardlimit, target) } if limit, err := Current(); err != nil || limit <= 0 { diff --git a/core/vm/contracts_test.go b/core/vm/contracts_test.go index 30d9b49f719b..aa8d2f1eb38b 100644 --- a/core/vm/contracts_test.go +++ b/core/vm/contracts_test.go @@ -185,7 +185,7 @@ func benchmarkPrecompiled(addr string, test precompiledTest, bench *testing.B) { return } if common.Bytes2Hex(res) != test.Expected { - bench.Error(fmt.Sprintf("Expected %v, got %v", test.Expected, common.Bytes2Hex(res))) + bench.Errorf("Expected %v, got %v", test.Expected, common.Bytes2Hex(res)) return } }) diff --git a/miner/worker.go b/miner/worker.go index 7a0dbdcd1ea3..13347f789989 100644 --- a/miner/worker.go +++ b/miner/worker.go @@ -1192,11 +1192,12 @@ func (w *worker) ancestorTimes(num *big.Int) []int64 { // returns throttle delay if necessary in seconds & seconds from the parent // blocks seconds seconds per -// 10 1 0.1 -// 50 10 0.2 -// 100 50 0.5 -// 500 500 1 -// 1000 2000 2 +// +// 10 1 0.1 +// 50 10 0.2 +// 100 50 0.5 +// 500 500 1 +// 1000 2000 2 func (w *worker) throttleMining(ts []int64) (int64, int64) { t := time.Now().Unix() dt, pt := int64(0), t-ts[0] @@ -1310,7 +1311,7 @@ func (w *worker) prepareWork(genParams *generateParams) (*environment, error) { if parent == nil { return nil, fmt.Errorf("missing parent") } - blockInterval, _, blockGasLimit, baseFeeMaxChangeRate, gasTargetPercentage, err := wemixminer.GetBlockBuildParameters(parent.Number()) + blockInterval, _, blockGasLimit, baseFeeMaxChangeRate, gasTargetPercentage, _ := wemixminer.GetBlockBuildParameters(parent.Number()) // Sanity check the timestamp correctness, recap the timestamp // to parent+1 if the mutation is allowed. timestamp := genParams.timestamp @@ -1326,7 +1327,7 @@ func (w *worker) prepareWork(genParams *generateParams) (*environment, error) { // wemix if !wemixminer.IsPoW() { - timestamp, till = w.timeIt(int64(blockInterval)) + timestamp, till = w.timeIt(blockInterval) } header := &types.Header{ ParentHash: parent.Hash(), diff --git a/wemix/admin.go b/wemix/admin.go index 14fb43f467f1..a8cfbdfd2c49 100644 --- a/wemix/admin.go +++ b/wemix/admin.go @@ -177,10 +177,10 @@ func toIdv4(id string) (string, error) { } // returns -// 1) extradata of genesis block, which is the id of the node that is allowed -// to generated blocks before admin contract is established. -// 2) returns the coinbase of genesis block, which should be the admin -// contract creator +// 1. extradata of genesis block, which is the id of the node that is allowed +// to generated blocks before admin contract is established. +// 2. returns the coinbase of genesis block, which should be the admin +// contract creator func (ma *wemixAdmin) getGenesisInfo() (string, common.Address, error) { ctx, cancel := context.WithCancel(context.Background()) defer cancel() @@ -341,11 +341,12 @@ func (ma *wemixAdmin) getNodes() []*wemixNode { } // returns -// 1. currentMiner *wemixNode: the current leader -// 2. nextMiner *wemixNode: the most eligible miner for the given height, -// which is up and running -// 3. nodes []*wemixNode: copies of map[string]*wemixNode, not references, -// sorted by id, i.e. mining order +// 1. currentMiner *wemixNode: the current leader +// 2. nextMiner *wemixNode: the most eligible miner for the given height, +// which is up and running +// 3. nodes []*wemixNode: copies of map[string]*wemixNode, not references, +// sorted by id, i.e. mining order +// // 'locked' indicates whether ma.lock is held by the caller or not func (ma *wemixAdmin) getMinerNodes(height int64, locked bool) (*wemixNode, *wemixNode, []*wemixNode) { var nodes []*wemixNode @@ -412,7 +413,7 @@ func (ma *wemixAdmin) getWemixNodes(ctx context.Context, block *big.Int) ([]*wem count, err = ma.getInt(ctx, ma.gov, block, "getNodeLength") for i := int64(1); i <= count; i++ { - input = []interface{}{big.NewInt(int64(i))} + input = []interface{}{big.NewInt(i)} output = []interface{}{&name, &enode, &ip, &port} if err = metclient.CallContract(ctx, ma.gov, "getNode", input, &output, block); err != nil { return nil, err @@ -453,7 +454,7 @@ func (ma *wemixAdmin) getRewardParams(ctx context.Context, height *big.Int) (*re return nil, err } - rp.distributionMethod = make([]*big.Int, 4, 4) + rp.distributionMethod = make([]*big.Int, 4) if err = metclient.CallContract(ctx, env, "getBlockRewardDistributionMethod", nil, &rp.distributionMethod, height); err != nil { return nil, err } @@ -489,11 +490,10 @@ func (ma *wemixAdmin) getRewardParams(ctx context.Context, height *big.Int) (*re return nil, err } else { for i := int64(1); i <= count; i++ { - input = []interface{}{big.NewInt(int64(i))} + input = []interface{}{big.NewInt(i)} if err = metclient.CallContract(ctx, gov, "getReward", input, &addr, height); err != nil { return nil, err } - input = []interface{}{addr} // NB. no staking consideration // if err = metclient.CallContract(ctx, staking, "lockedBalanceOf", input, &stake, height); err != nil { // return nil, err @@ -540,7 +540,7 @@ func (ma *wemixAdmin) getRewardAccounts(ctx context.Context, block *big.Int) (re } for i := int64(1); i <= count; i++ { - input = []interface{}{big.NewInt(int64(i))} + input = []interface{}{big.NewInt(i)} err = metclient.CallContract(ctx, ma.gov, "getReward", input, &addr, block) if err != nil { @@ -622,7 +622,7 @@ func (ma *wemixAdmin) getGovData(refresh bool) (data *govdata, err error) { if err != nil { return } - gasLimitAndBaseFee := make([]*big.Int, 3, 3) + gasLimitAndBaseFee := make([]*big.Int, 3) err = metclient.CallContract(ctx, ma.envStorage, "getGasLimitAndBaseFee", nil, &gasLimitAndBaseFee, block.Number) if err != nil { return @@ -1153,10 +1153,7 @@ func verifyBlockSig(height *big.Int, coinbase common.Address, nodeId []byte, has num := new(big.Int).Sub(height, common.Big1) _, gov, _, err := admin.getRegGovEnvContracts(ctx, num) if err != nil { - if err == wemixminer.ErrNotInitialized { - return true - } - return false + return err == wemixminer.ErrNotInitialized } // if minerNodeId is given, i.e. present in block header, use it, // otherwise, derive it from the codebase @@ -1348,7 +1345,7 @@ func getBlockBuildParameters(height *big.Int) (blockInterval int64, maxBaseFee, } blockInterval = v.Int64() - gasLimitAndBaseFee := make([]*big.Int, 3, 3) + gasLimitAndBaseFee := make([]*big.Int, 3) if err = metclient.CallContract(ctx, env, "getGasLimitAndBaseFee", nil, &gasLimitAndBaseFee, height); err != nil { err = wemixminer.ErrNotInitialized return @@ -1648,13 +1645,13 @@ func requirePendingTxs() bool { } // checks -// 1. fees total and per governance accounts are accurate -// 2. sum(rewards) == fees + block reward -// 3. rewards distribution is correct -// 4. reward members, reward pool and maintenance account are correct -// 5. balances of governance accounts are accurate. -// Note that it doesn't take account of internal transactions, -// so balance checks won't be accurate if there are contract transactions. +// 1. fees total and per governance accounts are accurate +// 2. sum(rewards) == fees + block reward +// 3. rewards distribution is correct +// 4. reward members, reward pool and maintenance account are correct +// 5. balances of governance accounts are accurate. +// Note that it doesn't take account of internal transactions, +// so balance checks won't be accurate if there are contract transactions. func verifyBlockRewards(height *big.Int) interface{} { type result struct { Status bool `json:"status"` diff --git a/wemix/etcdutil.go b/wemix/etcdutil.go index 268cccc14f6f..db861772246d 100644 --- a/wemix/etcdutil.go +++ b/wemix/etcdutil.go @@ -474,7 +474,7 @@ func (ma *wemixAdmin) etcdAutoJoin() error { if states := getMiners("", 0); len(states) > 0 { var tobes []*wemixapi.WemixMinerStatus for _, state := range states { - if state.NodeName == admin.self.Name || strings.Index(state.MiningPeers, "*") < 0 { + if state.NodeName == admin.self.Name || !strings.Contains(state.MiningPeers, "*") { tobes = append(tobes, state) } } @@ -522,7 +522,7 @@ func (ma *wemixAdmin) etcdAutoJoin() error { var state *wemixapi.WemixMinerStatus for _, s := range getMiners("", 0) { - if s.NodeName != admin.self.Name && s.Status == "up" && strings.Index(s.MiningPeers, "*") >= 0 { + if s.NodeName != admin.self.Name && s.Status == "up" && strings.Contains(s.MiningPeers, "*") { state = s break } @@ -879,7 +879,7 @@ again: ) } txresp, err := txIf.Then( - clientv3.OpPut(key, string(value)), + clientv3.OpPut(key, value), clientv3.OpGet(key), ).Else( clientv3.OpGet(key), @@ -935,9 +935,12 @@ func (ma *wemixAdmin) ttl2(ctx context.Context, key string) (int64, error) { // acquire token iff we're in sync with the latest block // lock is expected not to be present -// if expired, delete & try again +// +// if expired, delete & try again +// // work is expected to be there -// if not present, put an empty string & try again +// +// if not present, put an empty string & try again func (ma *wemixAdmin) acquireTokenSync(ctx context.Context, height *big.Int, parentHash common.Hash, ttl int64) (*WemixToken, error) { if !ma.etcdIsReady() { return nil, wemixminer.ErrNotInitialized @@ -994,7 +997,7 @@ again: ).Commit() if err == nil && !txresp.Succeeded { - err = ErrExists + var ( tokenFound, workFound bool = false, false foundToken []byte diff --git a/wemix/sync.go b/wemix/sync.go index cda867edad50..b2c7d87a8163 100644 --- a/wemix/sync.go +++ b/wemix/sync.go @@ -54,7 +54,7 @@ var ( latestWemixWork atomic.Value // governance data at modifiedBlock height - // cached coinbase -> enode & enode existance check at given modifiedBlock + // cached coinbase -> enode & enode existence check at given modifiedBlock // sync.Map[int]*cointbaseEnodeEntry coinbaseEnodeCache = &sync.Map{} @@ -79,7 +79,6 @@ func (ma *wemixAdmin) handleNewBlocks() { defer sub.Unsubscribe() for { - _ = <-ch latestUpdateTime.Store(time.Now()) if header, err := admin.cli.HeaderByNumber(context.Background(), nil); err == nil { latestBlock.Store(header) @@ -226,18 +225,18 @@ func findConsensusBlock(states []*wemixapi.WemixMinerStatus) (height *big.Int, h } // handles inconsistencies if any -// 0. updates of 'latestBlock' of 'latestWemixWork' hasn't occurred over 30 seconds -// -> i.e. the system is likely stuck -// 1. the token is invalid, i.e. present but json.Unmarshal fails -// -> remove -// 2. our latest block is ahead of the recorded 'work' -// -> if recorded 'work' exists and valid, -// then revert back to the 'work' block using 'debug.setHead()' -// 3. the recorded 'work' doesn't exist, i.e. no mining peers has it as their -// latest block, or -// the 'work' is invalid, i.e. present but json.Unmarshal fails -// -> find the consensus block, a block that the majority of the miners have -// as the latest block, sets it to the 'work' +// 0. updates of 'latestBlock' of 'latestWemixWork' hasn't occurred over 30 seconds +// -> i.e. the system is likely stuck +// 1. the token is invalid, i.e. present but json.Unmarshal fails +// -> remove +// 2. our latest block is ahead of the recorded 'work' +// -> if recorded 'work' exists and valid, +// then revert back to the 'work' block using 'debug.setHead()' +// 3. the recorded 'work' doesn't exist, i.e. no mining peers has it as their +// latest block, or +// the 'work' is invalid, i.e. present but json.Unmarshal fails +// -> find the consensus block, a block that the majority of the miners have +// as the latest block, sets it to the 'work' func syncCheck() error { if admin == nil || !admin.amPartner() || admin.self == nil || !admin.etcdIsRunning() { return nil @@ -269,7 +268,7 @@ func syncCheck() error { if err = json.Unmarshal([]byte(tokenData), token); err != nil { // invalid token string err = admin.etcdDelete(wemixTokenKey) - log.Error("sync check: reset the invalid token", "token", string(tokenData), "error", err) + log.Error("sync check: reset the invalid token", "token", tokenData, "error", err) } } @@ -311,7 +310,7 @@ func syncCheck() error { } else { if err = json.Unmarshal([]byte(workData), work); err != nil { // invalid work data - log.Error("sync check: ignoring invalid work", "work", string(workData)) + log.Error("sync check: ignoring invalid work", "work", workData) work = nil } } @@ -343,7 +342,7 @@ func syncCheck() error { log.Error("sync check: ahead of work, reverting", "height", work.Height, "was", header.Number) hexNum := hexutil.EncodeUint64(uint64(work.Height)) t := time.Now() - err = admin.rpcCli.CallContext(ctx, nil, "debug_setHead", hexNum) + _ = admin.rpcCli.CallContext(ctx, nil, "debug_setHead", hexNum) log.Error("sync check: ahead of work, reverted", "height", work.Height, "was", header.Number, "ellapsed", time.Since(t)) return nil } @@ -394,7 +393,7 @@ func syncCheck() error { if newWorkData, err := json.Marshal(newWork); err != nil { panic("failed to marshal work data") } else { - _, err = admin.etcdPut(wemixWorkKey, string(newWorkData)) + admin.etcdPut(wemixWorkKey, string(newWorkData)) } log.Error("sync check: found consensus block, setting work", "height", consensusHeight, "hash", consensusHash, "error", err) return err