Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

go/common/node/address: use custom type instead of internal net.TCPAddr #4744

Merged
merged 1 commit into from
May 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .changelog/4744.internal.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
go/common/node/address: use custom type instead of `net.TCPAddr`
25 changes: 21 additions & 4 deletions go/common/node/address.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,18 @@ var (

// Address represents a TCP address for the purpose of node descriptors.
type Address struct {
net.TCPAddr
IP net.IP `json:"IP"`
Port int64 `json:"Port"`
Zone string `json:"Zone"`
}

// ToTCPAddr returns a net TCP address.
func (a *Address) ToTCPAddr() *net.TCPAddr {
return &net.TCPAddr{
IP: a.IP,
Port: int(a.Port),
Zone: a.Zone,
}
}

// Equal compares vs another address for equality.
Expand Down Expand Up @@ -58,7 +69,9 @@ func (a *Address) UnmarshalText(text []byte) error {
return err
}

a.TCPAddr = *tcpAddr
a.IP = tcpAddr.IP
a.Port = int64(tcpAddr.Port)
a.Zone = tcpAddr.Zone

return nil
}
Expand All @@ -73,7 +86,7 @@ func (a *Address) FromIP(ip net.IP, port uint16) error {
return ErrInvalidAddress
}

a.Port = int(port)
a.Port = int64(port)
a.Zone = ""

return nil
Expand All @@ -86,7 +99,11 @@ func (a *Address) IsRoutable() bool {

// String returns the string representation of an address.
func (a Address) String() string {
return a.TCPAddr.String()
ip := a.IP.String()
if a.Zone != "" {
return net.JoinHostPort(ip+"%"+a.Zone, fmt.Sprintf("%d", a.Port))
}
return net.JoinHostPort(ip, fmt.Sprintf("%d", a.Port))
}

// ConsensusAddress represents a Tendermint consensus address that includes an
Expand Down
4 changes: 2 additions & 2 deletions go/common/node/node_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -223,10 +223,10 @@ func TestNodeForTestSerialization(t *testing.T) {
Addresses: []TLSAddress{
{
PubKey: signature.NewPublicKey("fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff4"),
Address: Address{TCPAddr: net.TCPAddr{
Address: Address{
IP: net.IPv4(127, 0, 0, 1),
Port: 1111,
}},
},
},
},
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,10 @@ func InitializeTestRegistryState(ctx context.Context, mkvs mkvs.Tree) error {
Addresses: []node.TLSAddress{
{
PubKey: signature.NewPublicKey("fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff2"),
Address: node.Address{TCPAddr: net.TCPAddr{
Address: node.Address{
IP: net.IPv4(127, 0, 0, 1),
Port: 1111,
}},
},
},
},
},
Expand Down
6 changes: 2 additions & 4 deletions go/oasis-node/cmd/debug/byzantine/grpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,8 @@ import (
func getGrpcAddress() []node.Address {
return []node.Address{
{
TCPAddr: net.TCPAddr{
IP: net.IPv4(127, 0, 0, 1),
Port: viper.GetInt(cmdGrpc.CfgServerPort),
},
IP: net.IPv4(127, 0, 0, 1),
Port: viper.GetInt64(cmdGrpc.CfgServerPort),
},
}
}
8 changes: 3 additions & 5 deletions go/oasis-node/cmd/debug/txsource/workload/registration.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,11 +89,9 @@ func getRuntime(entityID signature.PublicKey, id common.Namespace, epoch beacon.

func getNodeDesc(rng *rand.Rand, nodeIdentity *identity.Identity, entityID signature.PublicKey, runtimeID common.Namespace) *node.Node {
nodeAddr := node.Address{
TCPAddr: net.TCPAddr{
IP: net.IPv4(127, 0, 0, 1),
Port: 12345,
Zone: "",
},
IP: net.IPv4(127, 0, 0, 1),
Port: 12345,
Zone: "",
}

// NOTE: we shouldn't be registering validators, as that would lead to
Expand Down
8 changes: 4 additions & 4 deletions go/oasis-test-runner/scenario/e2e/registry_cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -357,16 +357,16 @@ func (sc *registryCLIImpl) isRegistered(childEnv *env.Env, nodeName, nodeDataDir
func (sc *registryCLIImpl) newTestNode(entityID signature.PublicKey) (*node.Node, []string, []string, []string, error) {
// Addresses.
testAddresses := []node.Address{
{TCPAddr: net.TCPAddr{
{
IP: net.IPv4(127, 0, 0, 1),
Port: 12345,
Zone: "",
}},
{TCPAddr: net.TCPAddr{
},
{
IP: net.IPv4(127, 0, 0, 2),
Port: 54321,
Zone: "",
}},
},
}
testAddressesStr := []string{}
for _, a := range testAddresses {
Expand Down
13 changes: 4 additions & 9 deletions go/registry/tests/tester.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"errors"
"fmt"
"math"
"net"
"testing"
"time"

Expand Down Expand Up @@ -1080,10 +1079,8 @@ func (ent *TestEntity) NewTestNodes(nCompute int, idNonce []byte, runtimes []*no
Roles: role,
}
addr := node.Address{
TCPAddr: net.TCPAddr{
IP: []byte{192, 0, 2, byte(i + 1)},
Port: 451,
},
IP: []byte{192, 0, 2, byte(i + 1)},
Port: 451,
}
nod.Node.P2P.ID = nodeIdentity.P2PSigner.Public()
nod.Node.P2P.Addresses = append(nod.Node.P2P.Addresses, addr)
Expand Down Expand Up @@ -1368,10 +1365,8 @@ func (ent *TestEntity) NewTestNodes(nCompute int, idNonce []byte, runtimes []*no
VRF: nod.Node.VRF,
}
addr = node.Address{
TCPAddr: net.TCPAddr{
IP: []byte{192, 0, 2, byte(i + 1)},
Port: 452,
},
IP: []byte{192, 0, 2, byte(i + 1)},
Port: 452,
}
nod.UpdatedNode.P2P.ID = nod.Node.P2P.ID
nod.UpdatedNode.P2P.Addresses = append(nod.UpdatedNode.P2P.Addresses, addr)
Expand Down
9 changes: 7 additions & 2 deletions go/worker/common/p2p/p2p.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,12 @@ func (p *P2P) Addresses() []node.Address {
panic(err)
}
tcpAddr := (netAddr).(*net.TCPAddr)
nodeAddr := node.Address{TCPAddr: *tcpAddr}
nodeAddr := node.Address{
IP: tcpAddr.IP,
Port: int64(tcpAddr.Port),
Zone: tcpAddr.Zone,
}

if err := registryAPI.VerifyAddress(nodeAddr, allowUnroutable); err != nil {
continue
}
Expand Down Expand Up @@ -329,7 +334,7 @@ func New(identity *identity.Identity, consensus consensus.Backend) (*P2P, error)
var registerAddresses []multiaddr.Multiaddr
for _, addr := range addresses {
var mAddr multiaddr.Multiaddr
mAddr, err = manet.FromNetAddr(&addr.TCPAddr)
mAddr, err = manet.FromNetAddr(addr.ToTCPAddr())
if err != nil {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion go/worker/common/p2p/peermgmt.go
Original file line number Diff line number Diff line change
Expand Up @@ -458,7 +458,7 @@ func nodeToAddrInfo(node *node.Node) (*peer.AddrInfo, error) {
return nil, fmt.Errorf("failed to extract public key from node P2P ID: %w", err)
}
for _, nodeAddr := range node.P2P.Addresses {
addr, err := manet.FromNetAddr(&nodeAddr.TCPAddr)
addr, err := manet.FromNetAddr(nodeAddr.ToTCPAddr())
if err != nil {
return nil, fmt.Errorf("failed to convert address to libp2p format: %w", err)
}
Expand Down