Skip to content

Commit

Permalink
feat: Add consensus peer client detection
Browse files Browse the repository at this point in the history
  • Loading branch information
samcm committed Sep 9, 2022
1 parent e8ea492 commit 66eb748
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 0 deletions.
60 changes: 60 additions & 0 deletions pkg/exporter/consensus/api/types/agents.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package types

import (
"strings"
)

type Agent string

const (
AgentUnknown Agent = "unknown"
AgentLighthouse Agent = "lighthouse"
AgentNimbus Agent = "nimbus"
AgentTeku Agent = "teku"
AgentPrysm Agent = "prysm"
AgentLodestar Agent = "lodestar"
)

var AllAgents = []Agent{
AgentUnknown,
AgentLighthouse,
AgentNimbus,
AgentTeku,
AgentPrysm,
AgentLodestar,
}

type AgentCount struct {
Unknown int `json:"unknown"`
Lighthouse int `json:"lighthouse"`
Nimbus int `json:"nimbus"`
Teku int `json:"teku"`
Prysm int `json:"prysm"`
Lodestar int `json:"lodestar"`
}

func AgentFromString(agent string) Agent {
asLower := strings.ToLower(agent)

if strings.Contains(asLower, "lighthouse") {
return AgentLighthouse
}

if strings.Contains(asLower, "nimbus") {
return AgentNimbus
}

if strings.Contains(asLower, "teku") {
return AgentTeku
}

if strings.Contains(asLower, "prysm") {
return AgentPrysm
}

if strings.Contains(asLower, "lodestar") {
return AgentLodestar
}

return AgentUnknown
}
27 changes: 27 additions & 0 deletions pkg/exporter/consensus/api/types/agents_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package types

import "testing"

func TestAgentParsing(t *testing.T) {
t.Parallel()

tests := []struct {
input string
expect Agent
}{
{"Prysm/v2.0.2/4a4a7e97dfd2285a5e48a178f693d870e9a4ff60", AgentPrysm},
{"Lighthouse/v3.1.0-aa022f4/x86_64-linux", AgentLighthouse},
{"nimbus", AgentNimbus},
{"teku/teku/v22.9.0/linux-x86_64/-privatebuild-openjdk64bitservervm-java-17", AgentTeku},
{"Lodestar/v0.32.0-rc.0-1-gc3b5b6a9/linux-x64/nodejs", AgentLodestar},
}

for _, test := range tests {
t.Run(test.input, func(t *testing.T) {
t.Parallel()
if actual := AgentFromString(test.input); actual != test.expect {
t.Errorf("Expected %s, got %s", test.expect, actual)
}
})
}
}

0 comments on commit 66eb748

Please sign in to comment.