From 43eff09944c40b762bd14d22d848c27f802d0a92 Mon Sep 17 00:00:00 2001 From: Leonard Lyubich Date: Wed, 23 Jun 2021 15:27:00 +0300 Subject: [PATCH] [#607] *: Do not use deprecated elements of code Signed-off-by: Leonard Lyubich --- cmd/neofs-cli/modules/netmap.go | 4 +- pkg/innerring/processors/audit/process.go | 3 +- pkg/innerring/rpc.go | 3 +- pkg/network/group.go | 40 ++++++++++++------- pkg/network/validation.go | 12 +----- pkg/network/validation_test.go | 2 +- pkg/services/audit/auditor/pdp.go | 3 +- pkg/services/audit/auditor/por.go | 3 +- pkg/services/object/get/get_test.go | 2 +- pkg/services/object/search/search_test.go | 2 +- .../placement/traverser_test.go | 2 +- 11 files changed, 42 insertions(+), 34 deletions(-) diff --git a/cmd/neofs-cli/modules/netmap.go b/cmd/neofs-cli/modules/netmap.go index 4cb7a3ac..a2538311 100644 --- a/cmd/neofs-cli/modules/netmap.go +++ b/cmd/neofs-cli/modules/netmap.go @@ -178,8 +178,10 @@ func prettyPrintNodeInfo(cmd *cobra.Command, i *netmap.NodeInfo, jsonEncoding bo } cmd.Println("key:", hex.EncodeToString(i.PublicKey())) - cmd.Println("address:", i.Address()) cmd.Println("state:", i.State()) + netmap.IterateAllAddresses(i, func(s string) { + cmd.Println("address:", s) + }) for _, attribute := range i.Attributes() { cmd.Printf("attribute: %s=%s\n", attribute.Key(), attribute.Value()) diff --git a/pkg/innerring/processors/audit/process.go b/pkg/innerring/processors/audit/process.go index 0138c40d..c9957aca 100644 --- a/pkg/innerring/processors/audit/process.go +++ b/pkg/innerring/processors/audit/process.go @@ -2,6 +2,7 @@ package audit import ( "context" + "encoding/hex" "github.com/nspcc-dev/neofs-api-go/pkg/client" cid "github.com/nspcc-dev/neofs-api-go/pkg/container/id" @@ -113,7 +114,7 @@ func (ap *Processor) findStorageGroups(cid *cid.ID, shuffled netmap.Nodes) []*ob for i := range shuffled { // consider iterating over some part of container log := ap.log.With( zap.Stringer("cid", cid), - zap.String("address", shuffled[0].Address()), + zap.String("key", hex.EncodeToString(shuffled[0].PublicKey())), zap.Int("try", i), zap.Int("total_tries", ln), ) diff --git a/pkg/innerring/rpc.go b/pkg/innerring/rpc.go index be0158d2..e78c2b1e 100644 --- a/pkg/innerring/rpc.go +++ b/pkg/innerring/rpc.go @@ -3,6 +3,7 @@ package innerring import ( "context" "crypto/ecdsa" + "encoding/hex" "fmt" "time" @@ -80,7 +81,7 @@ func (c *ClientCache) getSG(ctx context.Context, addr *object.Address, nm *netma err := netAddr.FromIterator(node) if err != nil { c.log.Warn("can't parse remote address", - zap.String("address", node.Address()), + zap.String("key", hex.EncodeToString(node.PublicKey())), zap.String("error", err.Error())) continue diff --git a/pkg/network/group.go b/pkg/network/group.go index d56b4c8d..8c2a8be8 100644 --- a/pkg/network/group.go +++ b/pkg/network/group.go @@ -2,6 +2,7 @@ package network import ( "errors" + "fmt" "sort" "github.com/nspcc-dev/neofs-api-go/pkg/netmap" @@ -78,13 +79,12 @@ type MultiAddressIterator interface { // The result is sorted with sort.Sort. // // Returns an error in the absence of addresses or if any of the addresses are incorrect. -func (x *AddressGroup) FromIterator(iter MultiAddressIterator) (err error) { +func (x *AddressGroup) FromIterator(iter MultiAddressIterator) error { as := *x addrNum := iter.NumberOfAddresses() if addrNum <= 0 { - err = errors.New("missing network addresses") - return + return errors.New("missing network addresses") } if cap(as) >= addrNum { @@ -93,17 +93,9 @@ func (x *AddressGroup) FromIterator(iter MultiAddressIterator) (err error) { as = make(AddressGroup, 0, addrNum) } - iter.IterateAddresses(func(s string) bool { - var a Address - - err = a.FromString(s) - - fail := err != nil - if !fail { - as = append(as, a) - } - - return fail + err := iterateParsedAddresses(iter, func(a Address) error { + as = append(as, a) + return nil }) if err == nil { @@ -111,6 +103,26 @@ func (x *AddressGroup) FromIterator(iter MultiAddressIterator) (err error) { *x = as } + return err +} + +// iterateParsedAddresses parses each address from MultiAddressIterator and passes it to f +// until 1st parsing failure or f's error. +func iterateParsedAddresses(iter MultiAddressIterator, f func(s Address) error) (err error) { + iter.IterateAddresses(func(s string) bool { + var a Address + + err = a.FromString(s) + if err != nil { + err = fmt.Errorf("could not parse address from string: %w", err) + return true + } + + err = f(a) + + return err != nil + }) + return } diff --git a/pkg/network/validation.go b/pkg/network/validation.go index 66dd5982..fb2aa153 100644 --- a/pkg/network/validation.go +++ b/pkg/network/validation.go @@ -2,7 +2,6 @@ package network import ( "errors" - "fmt" "github.com/nspcc-dev/neofs-api-go/pkg/netmap" ) @@ -47,16 +46,7 @@ var ( // 3. tls(optional, may be absent) // func VerifyMultiAddress(ni *netmap.NodeInfo) error { - // check if it can be parsed to network.Address - var netAddr Address - - err := netAddr.FromString(ni.Address()) - if err != nil { - return fmt.Errorf("could not parse multiaddr from NodeInfo: %w", err) - } - - // check amount of protocols and its order - return checkProtocols(netAddr) + return iterateParsedAddresses(ni, checkProtocols) } func checkProtocols(a Address) error { diff --git a/pkg/network/validation_test.go b/pkg/network/validation_test.go index 8409b291..d27517f7 100644 --- a/pkg/network/validation_test.go +++ b/pkg/network/validation_test.go @@ -58,7 +58,7 @@ func TestVerifyMultiAddress_Order(t *testing.T) { func constructNodeInfo(address string) *netmap.NodeInfo { ni := new(netmap.NodeInfo) - ni.SetAddress(address) + ni.SetAddresses(address) return ni } diff --git a/pkg/services/audit/auditor/pdp.go b/pkg/services/audit/auditor/pdp.go index 39d2e6bb..49e481c1 100644 --- a/pkg/services/audit/auditor/pdp.go +++ b/pkg/services/audit/auditor/pdp.go @@ -2,6 +2,7 @@ package auditor import ( "bytes" + "encoding/hex" "sync" "time" @@ -117,7 +118,7 @@ func (c *Context) collectHashes(p *gamePair) { if err != nil { c.log.Debug("could not get payload range hash", zap.Stringer("id", p.id), - zap.String("node", n.Address()), + zap.String("node", hex.EncodeToString(n.PublicKey())), zap.String("error", err.Error()), ) return diff --git a/pkg/services/audit/auditor/por.go b/pkg/services/audit/auditor/por.go index afb88278..a0c65a1e 100644 --- a/pkg/services/audit/auditor/por.go +++ b/pkg/services/audit/auditor/por.go @@ -2,6 +2,7 @@ package auditor import ( "bytes" + "encoding/hex" "sync" "github.com/nspcc-dev/neofs-api-go/pkg/object" @@ -80,7 +81,7 @@ func (c *Context) checkStorageGroupPoR(ind int, sg *object.ID) { hdr, err := c.cnrCom.GetHeader(c.task, flat[j], members[i], true) if err != nil { c.log.Debug("can't head object", - zap.String("remote_node", flat[j].Address()), + zap.String("remote_node", hex.EncodeToString(flat[j].PublicKey())), zap.Stringer("oid", members[i])) continue diff --git a/pkg/services/object/get/get_test.go b/pkg/services/object/get/get_test.go index 94c2a388..8708777e 100644 --- a/pkg/services/object/get/get_test.go +++ b/pkg/services/object/get/get_test.go @@ -407,7 +407,7 @@ func testNodeMatrix(t testing.TB, dim []int) ([]netmap.Nodes, [][]string) { ) ni := netmap.NewNodeInfo() - ni.SetAddress(a) + ni.SetAddresses(a) var na network.AddressGroup diff --git a/pkg/services/object/search/search_test.go b/pkg/services/object/search/search_test.go index 8ec231b9..ec8b32c2 100644 --- a/pkg/services/object/search/search_test.go +++ b/pkg/services/object/search/search_test.go @@ -201,7 +201,7 @@ func testNodeMatrix(t testing.TB, dim []int) ([]netmap.Nodes, [][]string) { ) ni := netmap.NewNodeInfo() - ni.SetAddress(a) + ni.SetAddresses(a) var na network.AddressGroup diff --git a/pkg/services/object_manager/placement/traverser_test.go b/pkg/services/object_manager/placement/traverser_test.go index ed25d066..59f83382 100644 --- a/pkg/services/object_manager/placement/traverser_test.go +++ b/pkg/services/object_manager/placement/traverser_test.go @@ -20,7 +20,7 @@ func (b testBuilder) BuildPlacement(*object.Address, *netmap.PlacementPolicy) ([ } func testNode(v uint32) (n netmap.NodeInfo) { - n.SetAddress("/ip4/0.0.0.0/tcp/" + strconv.Itoa(int(v))) + n.SetAddresses("/ip4/0.0.0.0/tcp/" + strconv.Itoa(int(v))) return n }