Skip to content

Commit

Permalink
refactor: changed user UserAgent name (#3379)
Browse files Browse the repository at this point in the history
<!--
Thank you for submitting a pull request!

Please make sure you have reviewed our contributors guide before
submitting your
first PR.

Please ensure you've addressed or included references to any related
issues.

Tips:
- Use keywords like "closes" or "fixes" followed by an issue number to
automatically close related issues when the PR is merged (e.g., "closes
#123" or "fixes #123").
- Describe the changes made in the PR.
- Ensure the PR has one of the required tags (kind:fix, kind:misc,
kind:break!, kind:refactor, kind:feat, kind:deps, kind:docs, kind:ci,
kind:chore, kind:testing)

-->

Opening as a replacement for
#2956 which has gone
stale. I had added some tests to the original refactoring but original
author vanished without allowing admin's to modify the original
PR/branch.

From original note:

fixes: #2932 ,
#2908

---------

Co-authored-by: Atreay Kukanur <66585295+ATREAY@users.noreply.github.com>
  • Loading branch information
ramin and ATREAY committed May 10, 2024
1 parent 93dfedf commit 86f4b2a
Show file tree
Hide file tree
Showing 4 changed files with 206 additions and 1 deletion.
24 changes: 24 additions & 0 deletions nodebuilder/node/buildInfo.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ var (
golangVersion = runtime.Version()
)

const (
emptyValue = "unknown"
)

// BuildInfo represents all necessary information about current build.
type BuildInfo struct {
BuildTime string
Expand All @@ -23,6 +27,26 @@ type BuildInfo struct {
GolangVersion string
}

func (b *BuildInfo) GetSemanticVersion() string {
if b.SemanticVersion == "" {
return emptyValue
}

return fmt.Sprintf("v%s", b.SemanticVersion)
}

func (b *BuildInfo) CommitShortSha() string {
if b.LastCommit == "" {
return emptyValue
}

if len(b.LastCommit) < 7 {
return b.LastCommit
}

return b.LastCommit[:7]
}

// GetBuildInfo returns information about current build.
func GetBuildInfo() *BuildInfo {
return &BuildInfo{
Expand Down
86 changes: 86 additions & 0 deletions nodebuilder/node/buildinfo_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package node

import (
"testing"

"github.com/stretchr/testify/require"
)

func TestEmptyValue(t *testing.T) {
want := "unknown"
got := emptyValue

require.Equal(t, want, got)
}

func TestGetSemanticVersion(t *testing.T) {
tests := []struct {
name string
buildInfo BuildInfo
expectedVersion string
}{
{
name: "Empty Semantic Version",
buildInfo: BuildInfo{
SemanticVersion: "",
},
expectedVersion: emptyValue,
},
{
name: "Non-empty Semantic Version",
buildInfo: BuildInfo{
SemanticVersion: "1.2.3",
},
expectedVersion: "v1.2.3",
},
// Add more test cases as needed
}

for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
actualVersion := tc.buildInfo.GetSemanticVersion()
if actualVersion != tc.expectedVersion {
t.Errorf("Test %s failed: expected %s, got %s", tc.name, tc.expectedVersion, actualVersion)
}
})
}
}

func TestBuildInfo_CommitShortSha(t *testing.T) {
tests := []struct {
name string
lastCommit string
want string
}{
{
name: "Empty lastCommit",
lastCommit: "",
want: "unknown",
},
{
name: "Short lastCommit",
lastCommit: "abc123",
want: "abc123",
},
{
name: "Valid lastCommit",
lastCommit: "abcdefg",
want: "abcdefg",
},
{
name: "Long lastCommit",
lastCommit: "abcdefghijk",
want: "abcdefg",
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
b := &BuildInfo{
LastCommit: tt.lastCommit,
}
got := b.CommitShortSha()
require.Equal(t, tt.want, got)
})
}
}
39 changes: 38 additions & 1 deletion nodebuilder/p2p/host.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package p2p
import (
"context"
"fmt"
"strings"

"github.com/libp2p/go-libp2p"
p2pconfig "github.com/libp2p/go-libp2p/config"
Expand All @@ -28,16 +29,52 @@ func routedHost(base HostBase, r routing.PeerRouting) hst.Host {
return routedhost.Wrap(base, r)
}

func newUserAgent() *UserAgent {
return &UserAgent{
network: "",
nodeType: 0,
build: node.GetBuildInfo(),
}
}

func (ua *UserAgent) WithNetwork(net Network) *UserAgent {
ua.network = net
return ua
}

func (ua *UserAgent) WithNodeType(tp node.Type) *UserAgent {
ua.nodeType = tp
return ua
}

type UserAgent struct {
network Network
nodeType node.Type
build *node.BuildInfo
}

func (ua *UserAgent) String() string {
return fmt.Sprintf(
"celestia-node/%s/%s/%s/%s",
ua.network,
strings.ToLower(ua.nodeType.String()),
ua.build.GetSemanticVersion(),
ua.build.CommitShortSha(),
)
}

// host returns constructor for Host.
func host(params hostParams) (HostBase, error) {
ua := newUserAgent().WithNetwork(params.Net).WithNodeType(params.Tp)

opts := []libp2p.Option{
libp2p.NoListenAddrs, // do not listen automatically
libp2p.AddrsFactory(params.AddrF),
libp2p.Identity(params.Key),
libp2p.Peerstore(params.PStore),
libp2p.ConnectionManager(params.ConnMngr),
libp2p.ConnectionGater(params.ConnGater),
libp2p.UserAgent(fmt.Sprintf("celestia-%s", params.Net)),
libp2p.UserAgent(ua.String()),
libp2p.NATPortMap(), // enables upnp
libp2p.DisableRelay(),
libp2p.BandwidthReporter(params.Bandwidth),
Expand Down
58 changes: 58 additions & 0 deletions nodebuilder/p2p/host_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package p2p

import (
"testing"

"github.com/celestiaorg/celestia-node/nodebuilder/node"
)

func TestUserAgent(t *testing.T) {
tests := []struct {
name string
net Network
tp node.Type
build *node.BuildInfo
expected string
}{
{
name: "Testnet",
net: "testnet",
tp: node.Full,
expected: "celestia-node/testnet/full/v1.0.0/abcdefg",
build: &node.BuildInfo{
SemanticVersion: "1.0.0",
LastCommit: "abcdefg",
},
},
{
name: "Mainnet",
net: "mainnet",
expected: "celestia-node/mainnet/light/v1.0.0/abcdefg",
tp: node.Light,
build: &node.BuildInfo{
SemanticVersion: "1.0.0",
LastCommit: "abcdefg",
},
},
{
name: "Empty LastCommit, Empty NodeType",
net: "testnet",
expected: "celestia-node/testnet/unknown/unknown/unknown",
build: &node.BuildInfo{
SemanticVersion: "",
LastCommit: "",
},
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
userAgent := newUserAgent().WithNetwork(tt.net).WithNodeType(tt.tp)
userAgent.build = tt.build

if userAgent.String() != tt.expected {
t.Errorf("Unexpected user agent. Got: %s, want: %s", userAgent, tt.expected)
}
})
}
}

0 comments on commit 86f4b2a

Please sign in to comment.