Skip to content

Commit

Permalink
Provide client info in block info output.
Browse files Browse the repository at this point in the history
  • Loading branch information
mcdee committed Jul 28, 2024
1 parent e50b4f0 commit 6117904
Show file tree
Hide file tree
Showing 4 changed files with 153 additions and 10 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
1.35.4:
- provide consensus and execution client info in block info output

1.35.3:
- provide better error message on context deadlline exceeded
- update launchpad output to match latest version
Expand Down
103 changes: 94 additions & 9 deletions cmd/block/info/output.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"encoding/hex"
"fmt"
"math/big"
"regexp"
"sort"
"strconv"
"strings"
Expand Down Expand Up @@ -54,7 +55,7 @@ func output(_ context.Context, data *dataOut) (string, error) {
return "", nil
}

func outputBlockGeneral(_ context.Context,
func outputBlockGeneral(ctx context.Context,
verbose bool,
slot phase0.Slot,
proposerIndex phase0.ValidatorIndex,
Expand Down Expand Up @@ -82,14 +83,7 @@ func outputBlockGeneral(_ context.Context,
res.WriteString(fmt.Sprintf("Parent root: %#x\n", parentRoot))
res.WriteString(fmt.Sprintf("State root: %#x\n", stateRoot))
}
if len(graffiti) > 0 && hex.EncodeToString(graffiti) != "0000000000000000000000000000000000000000000000000000000000000000" {
graffiti = bytes.TrimRight(graffiti, "\u0000")
if utf8.Valid(graffiti) {
res.WriteString(fmt.Sprintf("Graffiti: %s\n", string(graffiti)))
} else {
res.WriteString(fmt.Sprintf("Graffiti: %#x\n", graffiti))
}
}
res.WriteString(blockGraffiti(ctx, graffiti))

return res.String(), nil
}
Expand Down Expand Up @@ -1062,3 +1056,94 @@ func attestingIndices(input bitfield.Bitlist, indices []phase0.ValidatorIndex) s
}
return strings.TrimSpace(res)
}

func blockGraffiti(_ context.Context, graffiti []byte) string {
if len(graffiti) == 0 || hex.EncodeToString(graffiti) == "0000000000000000000000000000000000000000000000000000000000000000" {
// No graffiti.
return ""
}

// Remove any trailing null characters.
graffiti = bytes.TrimRight(graffiti, "\u0000")

if !utf8.Valid(graffiti) {
// Graffiti is not valid UTF-8, return hex.
return fmt.Sprintf("Graffiti: %#x\n", graffiti)
}

// See if there is client identification information present in the graffiti.
// The client identification will always be the last entry in the graffiti, with a space beforehand.
parts := bytes.Split(graffiti, []byte{' '})

// Consensus and execution client values come from
// https://github.com/ethereum/execution-apis/blob/main/src/engine/identification.md
consensusClients := map[string]string{
"GR": "grandine",
"LH": "lighthouse",
"LS": "lodestar",
"NB": "nimbus",
"PM": "prysm",
"TK": "teku",
}
consensusRegex := regexp.MustCompile(`(GR|LH|LS|NB|PM|TK)([0-9a-f]*)`)
consensusData := consensusRegex.Find(parts[len(parts)-1])

executionClients := map[string]string{
"BU": "besu",
"EG": "erigon",
"EJ": "ethereumJS",
"GE": "go-ethereum",
"NM": "nethermind",
"RH": "reth",
}
executionRegex := regexp.MustCompile(`(BU|EG|EJ|GE|NM|RH)([0-9a-f]*)`)
executionData := executionRegex.Find(parts[len(parts)-1])

if len(consensusData) == 0 && len(executionData) == 0 {
// There is no identifier; return the graffiti as-is.
return fmt.Sprintf("Graffiti: %s\n", string(graffiti))
}

res := strings.Builder{}

truncatedGraffiti := bytes.Join(parts[0:len(parts)-1], []byte(" "))
if len(truncatedGraffiti) > 0 {
res.WriteString(fmt.Sprintf("Graffiti: %s\n", string(truncatedGraffiti)))
}

if len(consensusData) > 0 {
consensusClient := consensusData[0:2]
consensusHash := ""
if len(consensusData) > 2 {
consensusHash = string(consensusData[2:])
}

res.WriteString("Consensus client: ")
res.WriteString(consensusClients[string(consensusClient)])
if consensusHash != "" {
res.WriteString(" (version hash ")
res.WriteString(consensusHash)
res.WriteString(")")
}
res.WriteString("\n")
}

if len(executionData) > 0 {
executionClient := executionData[0:2]
executionHash := ""
if len(executionData) > 2 {
executionHash = string(executionData[2:])
}

res.WriteString("Execution client: ")
res.WriteString(executionClients[string(executionClient)])
if executionHash != "" {
res.WriteString(" (version hash ")
res.WriteString(executionHash)
res.WriteString(")")
}
res.WriteString("\n")
}

return res.String()
}
55 changes: 55 additions & 0 deletions cmd/block/info/output_internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,3 +175,58 @@ func TestOutputBlockETH1Data(t *testing.T) {
})
}
}

func TestBlockGraffiti(t *testing.T) {
tests := []struct {
name string
graffiti []byte
res string
}{
{
name: "Empty",
graffiti: []byte(""),
},
{
name: "NoID",
graffiti: []byte("No identifier"),
res: "Graffiti: No identifier\n",
},
{
name: "SingleClient",
graffiti: []byte("Graffiti TK"),
res: "Graffiti: Graffiti\nConsensus client: teku\n",
},
{
name: "SingleClientImmediate",
graffiti: []byte("TK"),
res: "Consensus client: teku\n",
},
{
name: "SingleClientAndHashImmediate",
graffiti: []byte("TKa9f98260"),
res: "Consensus client: teku (version hash a9f98260)\n",
},
{
name: "DualClients",
graffiti: []byte("LHGE"),
res: "Consensus client: lighthouse\nExecution client: go-ethereum\n",
},
{
name: "DualClientsReverseOrder",
graffiti: []byte("GELH"),
res: "Consensus client: lighthouse\nExecution client: go-ethereum\n",
},
{
name: "DualClientsTruncatedHash",
graffiti: []byte("Freedom To Transact TKa9f9NM220b"),
res: "Graffiti: Freedom To Transact\nConsensus client: teku (version hash a9f9)\nExecution client: nethermind (version hash 220b)\n",
},
}

for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
res := blockGraffiti(context.Background(), test.graffiti)
require.Equal(t, test.res, res)
})
}
}
2 changes: 1 addition & 1 deletion cmd/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import (

// ReleaseVersion is the release version of the codebase.
// Usually overridden by tag names when building binaries.
var ReleaseVersion = "local build (latest release 1.35.3)"
var ReleaseVersion = "local build (latest release 1.35.4)"

// versionCmd represents the version command.
var versionCmd = &cobra.Command{
Expand Down

0 comments on commit 6117904

Please sign in to comment.