Skip to content

Commit 6894864

Browse files
fjlkaralabe
authored andcommitted
build: use golangci-lint (ethereum#20295)
* build: use golangci-lint This changes build/ci.go to download and run golangci-lint instead of gometalinter. * core/state: fix unnecessary conversion * p2p/simulations: fix lock copying (found by go vet) * signer/core: fix unnecessary conversions * crypto/ecies: remove unused function cmpPublic * core/rawdb: remove unused function print * core/state: remove unused function xTestFuzzCutter * core/vm: disable TestWriteExpectedValues in a different way * core/forkid: remove unused function checksum * les: remove unused type proofsData * cmd/utils: remove unused functions prefixedNames, prefixFor * crypto/bn256: run goimports * p2p/nat: fix goimports lint issue * cmd/clef: avoid using unkeyed struct fields * les: cancel context in testRequest * rlp: delete unreachable code * core: gofmt * internal/build: simplify DownloadFile for Go 1.11 compatibility * build: remove go test --short flag * .travis.yml: disable build cache * whisper/whisperv6: fix ineffectual assignment in TestWhisperIdentityManagement * .golangci.yml: enable goconst and ineffassign linters * build: print message when there are no lint issues * internal/build: refactor download a bit
1 parent 7c4a4eb commit 6894864

29 files changed

+354
-316
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ build/_vendor/pkg
2424

2525
# used by the Makefile
2626
/build/_workspace/
27+
/build/cache/
2728
/build/bin/
2829
/geth*.zip
2930

.golangci.yml

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# This file configures github.com/golangci/golangci-lint.
2+
3+
run:
4+
timeout: 2m
5+
tests: true
6+
# default is true. Enables skipping of directories:
7+
# vendor$, third_party$, testdata$, examples$, Godeps$, builtin$
8+
skip-dirs-use-default: true
9+
10+
linters:
11+
disable-all: true
12+
enable:
13+
- deadcode
14+
- goconst
15+
- goimports
16+
- gosimple
17+
- govet
18+
- ineffassign
19+
- misspell
20+
# - staticcheck
21+
- unconvert
22+
# - unused
23+
- varcheck
24+
25+
linters-settings:
26+
gofmt:
27+
simplify: true
28+
goconst:
29+
min-len: 3 # minimum length of string constant
30+
min-occurrences: 6 # minimum number of occurrences
31+
32+
issues:
33+
exclude-rules:
34+
- path: crypto/blake2b/
35+
linters:
36+
- deadcode
37+
- path: crypto/bn256/cloudflare
38+
linters:
39+
- deadcode
40+
- path: p2p/discv5/
41+
linters:
42+
- deadcode
43+
- path: core/vm/instructions_test.go
44+
linters:
45+
- goconst

.travis.yml

+1-4
Original file line numberDiff line numberDiff line change
@@ -87,12 +87,9 @@ jobs:
8787
- fakeroot
8888
- python-bzrlib
8989
- python-paramiko
90-
cache:
91-
directories:
92-
- $HOME/.gobundle
9390
script:
9491
- echo '|1|7SiYPr9xl3uctzovOTj4gMwAC1M=|t6ReES75Bo/PxlOPJ6/GsGbTrM0= ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEA0aKz5UTUndYgIGG7dQBV+HaeuEZJ2xPHo2DS2iSKvUL4xNMSAY4UguNW+pX56nAQmZKIZZ8MaEvSj6zMEDiq6HFfn5JcTlM80UwlnyKe8B8p7Nk06PPQLrnmQt5fh0HmEcZx+JU9TZsfCHPnX7MNz4ELfZE6cFsclClrKim3BHUIGq//t93DllB+h4O9LHjEUsQ1Sr63irDLSutkLJD6RXchjROXkNirlcNVHH/jwLWR5RcYilNX7S5bIkK8NlWPjsn/8Ua5O7I9/YoE97PpO6i73DTGLh5H9JN/SITwCKBkgSDWUt61uPK3Y11Gty7o2lWsBjhBUm2Y38CBsoGmBw==' >> ~/.ssh/known_hosts
95-
- go run build/ci.go debsrc -upload ethereum/ethereum -sftp-user geth-ci -signer "Go Ethereum Linux Builder <[email protected]>" -goversion 1.13.4 -gohash 95dbeab442ee2746b9acf0934c8e2fc26414a0565c008631b04addb8c02e7624 -gobundle $HOME/.gobundle/go.tar.gz
92+
- go run build/ci.go debsrc -goversion 1.13.4 -upload ethereum/ethereum -sftp-user geth-ci -signer "Go Ethereum Linux Builder <[email protected]>"
9693

9794
# This builder does the Linux Azure uploads
9895
- stage: build

build/checksums.txt

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# This file contains sha256 checksums of optional build dependencies.
2+
3+
95dbeab442ee2746b9acf0934c8e2fc26414a0565c008631b04addb8c02e7624 go1.13.4.src.tar.gz
4+
5+
1fcbc9e36f4319eeed02beb8cfd1b3d425ffc2f90ddf09a80f18d5064c51e0cb golangci-lint-1.21.0-linux-386.tar.gz
6+
267b4066e67139a38d29499331a002d6a29ad5be7aafc83db3b1e88f1b027f90 golangci-lint-1.21.0-linux-armv6.tar.gz
7+
a602c1f25f90e46e621019cff0a8cb3f4e1837011f3537f15e730d6a9ebf507b golangci-lint-1.21.0-freebsd-armv7.tar.gz
8+
2c861f8dc56b560474aa27cab0c075991628cc01af3451e27ac82f5d10d5106b golangci-lint-1.21.0-linux-amd64.tar.gz
9+
a1c39e055280e755acaa906e7abfc20b99a5c28be8af541c57fbc44abbb20dde golangci-lint-1.21.0-linux-arm64.tar.gz
10+
a8f8bda8c6a4136acf858091077830b1e83ad5612606cb69d5dced869ce00bd8 golangci-lint-1.21.0-linux-ppc64le.tar.gz
11+
0a8a8c3bc660ccbca668897ab520f7ee9878f16cc8e4dd24fe46236ceec97ba3 golangci-lint-1.21.0-freebsd-armv6.tar.gz
12+
699b07f45e216571f54002bcbd83b511c4801464a422162158e299587b095b18 golangci-lint-1.21.0-freebsd-amd64.tar.gz
13+
980fb4993942154bb5c8129ea3b86de09574fe81b24384ebb58cd7a9d2f04483 golangci-lint-1.21.0-linux-armv7.tar.gz
14+
f15b689088a47f20d5d3c1d945e9ee7c6238f2b84ea468b5f886cf8713dce62e golangci-lint-1.21.0-windows-386.zip
15+
2e40ded7adcf11e59013cb15c24438b15a86526ca241edfcfdf1abd73a5280a8 golangci-lint-1.21.0-windows-amd64.zip
16+
6052c7cfea4d6dc2fc722f6c12792a5ec087420198db495afffbc22052653bf7 golangci-lint-1.21.0-freebsd-386.tar.gz
17+
ca00b8eacf9af14a71b908b4149606c762aa5c0eac781e74ca0abedfdfdf6c8c golangci-lint-1.21.0-linux-s390x.tar.gz
18+
1365455940c342f95718159d89d66ad2eef19f0846c3e87023e915a3527b929f golangci-lint-1.21.0-darwin-386.tar.gz
19+
2b2713ec5007e67883aa501eebb81f22abfab0cf0909134ba90f60a066db3760 golangci-lint-1.21.0-darwin-amd64.tar.gz

build/ci.go

+47-38
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,6 @@ import (
5858
"strings"
5959
"time"
6060

61-
"github.com/ethereum/go-ethereum/common/hexutil"
6261
"github.com/ethereum/go-ethereum/internal/build"
6362
"github.com/ethereum/go-ethereum/params"
6463
)
@@ -331,7 +330,7 @@ func doTest(cmdline []string) {
331330
// Test a single package at a time. CI builders are slow
332331
// and some tests run into timeouts under load.
333332
gotest := goTool("test", buildFlags(env)...)
334-
gotest.Args = append(gotest.Args, "-p", "1", "-timeout", "5m", "--short")
333+
gotest.Args = append(gotest.Args, "-p", "1", "-timeout", "5m")
335334
if *coverage {
336335
gotest.Args = append(gotest.Args, "-covermode=atomic", "-cover")
337336
}
@@ -340,39 +339,38 @@ func doTest(cmdline []string) {
340339
build.MustRun(gotest)
341340
}
342341

343-
// runs gometalinter on requested packages
342+
// doLint runs golangci-lint on requested packages.
344343
func doLint(cmdline []string) {
344+
var (
345+
cachedir = flag.String("cachedir", "./build/cache", "directory for caching golangci-lint binary.")
346+
)
345347
flag.CommandLine.Parse(cmdline)
346-
347348
packages := []string{"./..."}
348349
if len(flag.CommandLine.Args()) > 0 {
349350
packages = flag.CommandLine.Args()
350351
}
351-
// Get metalinter and install all supported linters
352-
build.MustRun(goTool("get", "gopkg.in/alecthomas/gometalinter.v2"))
353-
build.MustRunCommand(filepath.Join(GOBIN, "gometalinter.v2"), "--install")
354-
355-
// Run fast linters batched together
356-
configs := []string{
357-
"--vendor",
358-
"--tests",
359-
"--deadline=2m",
360-
"--disable-all",
361-
"--enable=goimports",
362-
"--enable=varcheck",
363-
"--enable=vet",
364-
"--enable=gofmt",
365-
"--enable=misspell",
366-
"--enable=goconst",
367-
"--min-occurrences=6", // for goconst
368-
}
369-
build.MustRunCommand(filepath.Join(GOBIN, "gometalinter.v2"), append(configs, packages...)...)
370-
371-
// Run slow linters one by one
372-
for _, linter := range []string{"unconvert", "gosimple"} {
373-
configs = []string{"--vendor", "--tests", "--deadline=10m", "--disable-all", "--enable=" + linter}
374-
build.MustRunCommand(filepath.Join(GOBIN, "gometalinter.v2"), append(configs, packages...)...)
352+
353+
linter := downloadLinter(*cachedir)
354+
lflags := []string{"run", "--config", ".golangci.yml"}
355+
build.MustRunCommand(linter, append(lflags, packages...)...)
356+
fmt.Println("You have achieved perfection.")
357+
}
358+
359+
// downloadLinter downloads and unpacks golangci-lint.
360+
func downloadLinter(cachedir string) string {
361+
const version = "1.21.0"
362+
363+
csdb := build.MustLoadChecksums("build/checksums.txt")
364+
base := fmt.Sprintf("golangci-lint-%s-%s-%s", version, runtime.GOOS, runtime.GOARCH)
365+
url := fmt.Sprintf("https://github.com/golangci/golangci-lint/releases/download/v%s/%s.tar.gz", version, base)
366+
archivePath := filepath.Join(cachedir, base+".tar.gz")
367+
if err := csdb.DownloadFile(url, archivePath); err != nil {
368+
log.Fatal(err)
369+
}
370+
if err := build.ExtractTarballArchive(archivePath, cachedir); err != nil {
371+
log.Fatal(err)
375372
}
373+
return filepath.Join(cachedir, base, "golangci-lint")
376374
}
377375

378376
// Release Packaging
@@ -476,8 +474,7 @@ func maybeSkipArchive(env build.Environment) {
476474
func doDebianSource(cmdline []string) {
477475
var (
478476
goversion = flag.String("goversion", "", `Go version to build with (will be included in the source package)`)
479-
gobundle = flag.String("gobundle", "/tmp/go.tar.gz", `Filesystem path to cache the downloaded Go bundles at`)
480-
gohash = flag.String("gohash", "", `SHA256 checksum of the Go sources requested to build with`)
477+
cachedir = flag.String("cachedir", "./build/cache", `Filesystem path to cache the downloaded Go bundles at`)
481478
signer = flag.String("signer", "", `Signing key name, also used as package author`)
482479
upload = flag.String("upload", "", `Where to upload the source package (usually "ethereum/ethereum")`)
483480
sshUser = flag.String("sftp-user", "", `Username for SFTP upload (usually "geth-ci")`)
@@ -495,24 +492,25 @@ func doDebianSource(cmdline []string) {
495492
gpg.Stdin = bytes.NewReader(key)
496493
build.MustRun(gpg)
497494
}
498-
// Download and verify the Go source package
499-
if err := build.EnsureGoSources(*goversion, hexutil.MustDecode("0x"+*gohash), *gobundle); err != nil {
500-
log.Fatalf("Failed to ensure Go source package: %v", err)
501-
}
502-
// Create Debian packages and upload them
495+
496+
// Download and verify the Go source package.
497+
gobundle := downloadGoSources(*goversion, *cachedir)
498+
499+
// Create Debian packages and upload them.
503500
for _, pkg := range debPackages {
504501
for distro, goboot := range debDistroGoBoots {
505-
// Prepare the debian package with the go-ethereum sources
502+
// Prepare the debian package with the go-ethereum sources.
506503
meta := newDebMetadata(distro, goboot, *signer, env, now, pkg.Name, pkg.Version, pkg.Executables)
507504
pkgdir := stageDebianSource(*workdir, meta)
508505

509-
// Ship the Go sources along so we have a proper thing to build with
510-
if err := build.ExtractTarballArchive(*gobundle, pkgdir); err != nil {
506+
// Add Go source code.
507+
if err := build.ExtractTarballArchive(gobundle, pkgdir); err != nil {
511508
log.Fatalf("Failed to extract Go sources: %v", err)
512509
}
513510
if err := os.Rename(filepath.Join(pkgdir, "go"), filepath.Join(pkgdir, ".go")); err != nil {
514511
log.Fatalf("Failed to rename Go source folder: %v", err)
515512
}
513+
516514
// Run the packaging and upload to the PPA
517515
debuild := exec.Command("debuild", "-S", "-sa", "-us", "-uc", "-d", "-Zxz")
518516
debuild.Dir = pkgdir
@@ -534,6 +532,17 @@ func doDebianSource(cmdline []string) {
534532
}
535533
}
536534

535+
func downloadGoSources(version string, cachedir string) string {
536+
csdb := build.MustLoadChecksums("build/checksums.txt")
537+
file := fmt.Sprintf("go%s.src.tar.gz", version)
538+
url := "https://dl.google.com/go/" + file
539+
dst := filepath.Join(cachedir, file)
540+
if err := csdb.DownloadFile(url, dst); err != nil {
541+
log.Fatal(err)
542+
}
543+
return dst
544+
}
545+
537546
func ppaUpload(workdir, ppa, sshUser string, files []string) {
538547
p := strings.Split(ppa, "/")
539548
if len(p) != 2 {

cmd/clef/main.go

+25-22
Original file line numberDiff line numberDiff line change
@@ -760,21 +760,19 @@ func testExternalUI(api *core.SignerAPI) {
760760
api.UI.ShowInfo("Please approve the next request for signing a clique header")
761761
time.Sleep(delay)
762762
cliqueHeader := types.Header{
763-
common.HexToHash("0000H45H"),
764-
common.HexToHash("0000H45H"),
765-
common.HexToAddress("0000H45H"),
766-
common.HexToHash("0000H00H"),
767-
common.HexToHash("0000H45H"),
768-
common.HexToHash("0000H45H"),
769-
types.Bloom{},
770-
big.NewInt(1337),
771-
big.NewInt(1337),
772-
1338,
773-
1338,
774-
1338,
775-
[]byte("Extra data Extra data Extra data Extra data Extra data Extra data Extra data Extra data"),
776-
common.HexToHash("0x0000H45H"),
777-
types.BlockNonce{},
763+
ParentHash: common.HexToHash("0000H45H"),
764+
UncleHash: common.HexToHash("0000H45H"),
765+
Coinbase: common.HexToAddress("0000H45H"),
766+
Root: common.HexToHash("0000H00H"),
767+
TxHash: common.HexToHash("0000H45H"),
768+
ReceiptHash: common.HexToHash("0000H45H"),
769+
Difficulty: big.NewInt(1337),
770+
Number: big.NewInt(1337),
771+
GasLimit: 1338,
772+
GasUsed: 1338,
773+
Time: 1338,
774+
Extra: []byte("Extra data Extra data Extra data Extra data Extra data Extra data Extra data Extra data"),
775+
MixDigest: common.HexToHash("0x0000H45H"),
778776
}
779777
cliqueRlp, err := rlp.EncodeToBytes(cliqueHeader)
780778
if err != nil {
@@ -938,7 +936,7 @@ func GenDoc(ctx *cli.Context) {
938936
"of the work in canonicalizing and making sense of the data, and it's up to the UI to present" +
939937
"the user with the contents of the `message`"
940938
sighash, msg := accounts.TextAndHash([]byte("hello world"))
941-
messages := []*core.NameValueType{{"message", msg, accounts.MimetypeTextPlain}}
939+
messages := []*core.NameValueType{{Name: "message", Value: msg, Typ: accounts.MimetypeTextPlain}}
942940

943941
add("SignDataRequest", desc, &core.SignDataRequest{
944942
Address: common.NewMixedcaseAddress(a),
@@ -969,8 +967,8 @@ func GenDoc(ctx *cli.Context) {
969967
add("SignTxRequest", desc, &core.SignTxRequest{
970968
Meta: meta,
971969
Callinfo: []core.ValidationInfo{
972-
{"Warning", "Something looks odd, show this message as a warning"},
973-
{"Info", "User should see this aswell"},
970+
{Typ: "Warning", Message: "Something looks odd, show this message as a warning"},
971+
{Typ: "Info", Message: "User should see this as well"},
974972
},
975973
Transaction: core.SendTxArgs{
976974
Data: &data,
@@ -1036,16 +1034,21 @@ func GenDoc(ctx *cli.Context) {
10361034
&core.ListRequest{
10371035
Meta: meta,
10381036
Accounts: []accounts.Account{
1039-
{a, accounts.URL{Scheme: "keystore", Path: "/path/to/keyfile/a"}},
1040-
{b, accounts.URL{Scheme: "keystore", Path: "/path/to/keyfile/b"}}},
1037+
{Address: a, URL: accounts.URL{Scheme: "keystore", Path: "/path/to/keyfile/a"}},
1038+
{Address: b, URL: accounts.URL{Scheme: "keystore", Path: "/path/to/keyfile/b"}}},
10411039
})
10421040

10431041
add("ListResponse", "Response to list request. The response contains a list of all addresses to show to the caller. "+
10441042
"Note: the UI is free to respond with any address the caller, regardless of whether it exists or not",
10451043
&core.ListResponse{
10461044
Accounts: []accounts.Account{
1047-
{common.HexToAddress("0xcowbeef000000cowbeef00000000000000000c0w"), accounts.URL{Path: ".. ignored .."}},
1048-
{common.HexToAddress("0xffffffffffffffffffffffffffffffffffffffff"), accounts.URL{}},
1045+
{
1046+
Address: common.HexToAddress("0xcowbeef000000cowbeef00000000000000000c0w"),
1047+
URL: accounts.URL{Path: ".. ignored .."},
1048+
},
1049+
{
1050+
Address: common.HexToAddress("0xffffffffffffffffffffffffffffffffffffffff"),
1051+
},
10491052
}})
10501053
}
10511054

cmd/utils/customflags.go

-22
Original file line numberDiff line numberDiff line change
@@ -185,28 +185,6 @@ func GlobalBig(ctx *cli.Context, name string) *big.Int {
185185
return (*big.Int)(val.(*bigValue))
186186
}
187187

188-
func prefixFor(name string) (prefix string) {
189-
if len(name) == 1 {
190-
prefix = "-"
191-
} else {
192-
prefix = "--"
193-
}
194-
195-
return
196-
}
197-
198-
func prefixedNames(fullName string) (prefixed string) {
199-
parts := strings.Split(fullName, ",")
200-
for i, name := range parts {
201-
name = strings.Trim(name, " ")
202-
prefixed += prefixFor(name) + name
203-
if i < len(parts)-1 {
204-
prefixed += ", "
205-
}
206-
}
207-
return
208-
}
209-
210188
// Expands a file path
211189
// 1. replace tilde with users home dir
212190
// 2. expands embedded environment variables

core/blockchain.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ import (
4242
"github.com/ethereum/go-ethereum/params"
4343
"github.com/ethereum/go-ethereum/rlp"
4444
"github.com/ethereum/go-ethereum/trie"
45-
"github.com/hashicorp/golang-lru"
45+
lru "github.com/hashicorp/golang-lru"
4646
)
4747

4848
var (

core/forkid/forkid.go

-7
Original file line numberDiff line numberDiff line change
@@ -186,13 +186,6 @@ func newFilter(config *params.ChainConfig, genesis common.Hash, headfn func() ui
186186
}
187187
}
188188

189-
// checksum calculates the IEEE CRC32 checksum of a block number.
190-
func checksum(fork uint64) uint32 {
191-
var blob [8]byte
192-
binary.BigEndian.PutUint64(blob[:], fork)
193-
return crc32.ChecksumIEEE(blob[:])
194-
}
195-
196189
// checksumUpdate calculates the next IEEE CRC32 checksum based on the previous
197190
// one and a fork block number (equivalent to CRC32(original-blob || fork)).
198191
func checksumUpdate(hash uint32, fork uint64) uint32 {

core/rawdb/freezer_table_test.go

-8
Original file line numberDiff line numberDiff line change
@@ -41,14 +41,6 @@ func getChunk(size int, b int) []byte {
4141
return data
4242
}
4343

44-
func print(t *testing.T, f *freezerTable, item uint64) {
45-
a, err := f.Retrieve(item)
46-
if err != nil {
47-
t.Fatal(err)
48-
}
49-
t.Logf("db[%d] = %x\n", item, a)
50-
}
51-
5244
// TestFreezerBasics test initializing a freezertable from scratch, writing to the table,
5345
// and reading it back.
5446
func TestFreezerBasics(t *testing.T) {

0 commit comments

Comments
 (0)