Skip to content

Commit dadd639

Browse files
fjlblakehhuynh
authored andcommitted
core: move build version reading to its own package (ethereum#25806)
This fixes the build with Go 1.17, which does not have BuildInfo.Settings yet.
1 parent 3a4aba6 commit dadd639

4 files changed

Lines changed: 123 additions & 72 deletions

File tree

core/blockchain.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"fmt"
2323
"io"
2424
"math/big"
25+
"runtime"
2526
"sort"
2627
"strings"
2728
"sync"
@@ -40,6 +41,7 @@ import (
4041
"github.com/ethereum/go-ethereum/ethdb"
4142
"github.com/ethereum/go-ethereum/event"
4243
"github.com/ethereum/go-ethereum/internal/syncx"
44+
"github.com/ethereum/go-ethereum/internal/version"
4345
"github.com/ethereum/go-ethereum/log"
4446
"github.com/ethereum/go-ethereum/metrics"
4547
"github.com/ethereum/go-ethereum/params"
@@ -2378,6 +2380,31 @@ func (bc *BlockChain) reportBlock(block *types.Block, receipts types.Receipts, e
23782380
log.Error(summarizeBadBlock(block, receipts, bc.Config(), err))
23792381
}
23802382

2383+
// summarizeBadBlock returns a string summarizing the bad block and other
2384+
// relevant information.
2385+
func summarizeBadBlock(block *types.Block, receipts []*types.Receipt, config *params.ChainConfig, err error) string {
2386+
var receiptString string
2387+
for i, receipt := range receipts {
2388+
receiptString += fmt.Sprintf("\n %d: cumulative: %v gas: %v contract: %v status: %v tx: %v logs: %v bloom: %x state: %x",
2389+
i, receipt.CumulativeGasUsed, receipt.GasUsed, receipt.ContractAddress.Hex(),
2390+
receipt.Status, receipt.TxHash.Hex(), receipt.Logs, receipt.Bloom, receipt.PostState)
2391+
}
2392+
version, vcs := version.Info()
2393+
platform := fmt.Sprintf("%s %s %s %s", version, runtime.Version(), runtime.GOARCH, runtime.GOOS)
2394+
if vcs != "" {
2395+
vcs = fmt.Sprintf("\nVCS: %s", vcs)
2396+
}
2397+
return fmt.Sprintf(`
2398+
########## BAD BLOCK #########
2399+
Block: %v (%#x)
2400+
Error: %v
2401+
Platform: %v%v
2402+
Chain config: %#v
2403+
Receipts: %v
2404+
##############################
2405+
`, block.Number(), block.Hash(), err, platform, vcs, config, receiptString)
2406+
}
2407+
23812408
// InsertHeaderChain attempts to insert the given header chain in to the local
23822409
// chain, possibly creating a reorg. If an error is returned, it will return the
23832410
// index number of the failing header as well an error describing what went wrong.

internal/version/vcs_fallback.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// Copyright 2022 The go-ethereum Authors
2+
// This file is part of the go-ethereum library.
3+
//
4+
// The go-ethereum library is free software: you can redistribute it and/or modify
5+
// it under the terms of the GNU Lesser General Public License as published by
6+
// the Free Software Foundation, either version 3 of the License, or
7+
// (at your option) any later version.
8+
//
9+
// The go-ethereum library is distributed in the hope that it will be useful,
10+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
// GNU Lesser General Public License for more details.
13+
//
14+
// You should have received a copy of the GNU Lesser General Public License
15+
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
16+
17+
//go:build !go1.18
18+
// +build !go1.18
19+
20+
package version
21+
22+
import "runtime/debug"
23+
24+
// In Go versions before 1.18, VCS information is not available.
25+
26+
func vcsInfo(info *debug.BuildInfo) (gitStatus, bool) {
27+
return gitStatus{}, false
28+
}

internal/version/vcs_go1.18.go

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
// Copyright 2022 The go-ethereum Authors
2+
// This file is part of the go-ethereum library.
3+
//
4+
// The go-ethereum library is free software: you can redistribute it and/or modify
5+
// it under the terms of the GNU Lesser General Public License as published by
6+
// the Free Software Foundation, either version 3 of the License, or
7+
// (at your option) any later version.
8+
//
9+
// The go-ethereum library is distributed in the hope that it will be useful,
10+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
// GNU Lesser General Public License for more details.
13+
//
14+
// You should have received a copy of the GNU Lesser General Public License
15+
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
16+
17+
//go:build go1.18
18+
// +build go1.18
19+
20+
package version
21+
22+
import "runtime/debug"
23+
24+
// In go 1.18 and beyond, the go tool embeds VCS information into the build.
25+
26+
// vcsInfo returns VCS information of the build.
27+
func vcsInfo(info *debug.BuildInfo) (s gitStatus, ok bool) {
28+
for _, v := range info.Settings {
29+
switch v.Key {
30+
case "vcs.revision":
31+
if len(v.Value) < 8 {
32+
s.revision = v.Value
33+
} else {
34+
s.revision = v.Value[:8]
35+
}
36+
case "vcs.modified":
37+
if v.Value == "true" {
38+
s.modified = true
39+
}
40+
case "vcs.time":
41+
s.time = v.Value
42+
}
43+
}
44+
if s.revision != "" && s.time != "" {
45+
ok = true
46+
}
47+
return
48+
}
Lines changed: 20 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -14,70 +14,48 @@
1414
// You should have received a copy of the GNU Lesser General Public License
1515
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
1616

17-
package core
17+
// Package version implements reading of build version information.
18+
package version
1819

1920
import (
2021
"fmt"
21-
"runtime"
2222
"runtime/debug"
2323
"strings"
2424

25-
"github.com/ethereum/go-ethereum/core/types"
2625
"github.com/ethereum/go-ethereum/params"
2726
)
2827

2928
const ourPath = "github.com/ethereum/go-ethereum" // Path to our module
3029

31-
// summarizeBadBlock returns a string summarizing the bad block and other
32-
// relevant information.
33-
func summarizeBadBlock(block *types.Block, receipts []*types.Receipt, config *params.ChainConfig, err error) string {
34-
var receiptString string
35-
for i, receipt := range receipts {
36-
receiptString += fmt.Sprintf("\n %d: cumulative: %v gas: %v contract: %v status: %v tx: %v logs: %v bloom: %x state: %x",
37-
i, receipt.CumulativeGasUsed, receipt.GasUsed, receipt.ContractAddress.Hex(),
38-
receipt.Status, receipt.TxHash.Hex(), receipt.Logs, receipt.Bloom, receipt.PostState)
39-
}
40-
version, vcs := runtimeInfo()
41-
platform := fmt.Sprintf("%s %s %s %s", version, runtime.Version(), runtime.GOARCH, runtime.GOOS)
42-
if vcs != "" {
43-
vcs = fmt.Sprintf("\nVCS: %s", vcs)
44-
}
45-
return fmt.Sprintf(`
46-
########## BAD BLOCK #########
47-
Block: %v (%#x)
48-
Error: %v
49-
Platform: %v%v
50-
Chain config: %#v
51-
Receipts: %v
52-
##############################
53-
`, block.Number(), block.Hash(), err, platform, vcs, config, receiptString)
54-
}
55-
5630
// runtimeInfo returns build and platform information about the current binary.
5731
//
5832
// If the package that is currently executing is a prefixed by our go-ethereum
5933
// module path, it will print out commit and date VCS information. Otherwise,
6034
// it will assume it's imported by a third-party and will return the imported
6135
// version and whether it was replaced by another module.
62-
func runtimeInfo() (string, string) {
63-
var (
64-
version = params.VersionWithMeta
65-
vcs = ""
66-
buildInfo, ok = debug.ReadBuildInfo()
67-
)
68-
if ok {
69-
version = versionInfo(buildInfo)
70-
if status, ok := vcsInfo(buildInfo); ok {
71-
modified := ""
72-
if status.modified {
73-
modified = " (dirty)"
74-
}
75-
vcs = status.revision + "-" + status.time + modified
36+
func Info() (version, vcs string) {
37+
version = params.VersionWithMeta
38+
buildInfo, ok := debug.ReadBuildInfo()
39+
if !ok {
40+
return version, ""
41+
}
42+
version = versionInfo(buildInfo)
43+
if status, ok := vcsInfo(buildInfo); ok {
44+
modified := ""
45+
if status.modified {
46+
modified = " (dirty)"
7647
}
48+
vcs = status.revision + "-" + status.time + modified
7749
}
7850
return version, vcs
7951
}
8052

53+
type gitStatus struct {
54+
revision string
55+
time string
56+
modified bool
57+
}
58+
8159
// versionInfo returns version information for the currently executing
8260
// implementation.
8361
//
@@ -113,36 +91,6 @@ func versionInfo(info *debug.BuildInfo) string {
11391
return version
11492
}
11593

116-
type status struct {
117-
revision string
118-
time string
119-
modified bool
120-
}
121-
122-
// vcsInfo returns VCS information of the build.
123-
func vcsInfo(info *debug.BuildInfo) (s status, ok bool) {
124-
for _, v := range info.Settings {
125-
switch v.Key {
126-
case "vcs.revision":
127-
if len(v.Value) < 8 {
128-
s.revision = v.Value
129-
} else {
130-
s.revision = v.Value[:8]
131-
}
132-
case "vcs.modified":
133-
if v.Value == "true" {
134-
s.modified = true
135-
}
136-
case "vcs.time":
137-
s.time = v.Value
138-
}
139-
}
140-
if s.revision != "" && s.time != "" {
141-
ok = true
142-
}
143-
return
144-
}
145-
14694
// findModule returns the module at path.
14795
func findModule(info *debug.BuildInfo, path string) *debug.Module {
14896
if info.Path == ourPath {

0 commit comments

Comments
 (0)