From 2e4a1cb6df2707bd8b197382602e9b07c8bcea24 Mon Sep 17 00:00:00 2001 From: Leonard Lyubich Date: Wed, 15 Jun 2022 11:08:10 +0300 Subject: [PATCH] [#1518] Upgrade NeoFS SDK Go with changed `subnet` package Signed-off-by: Leonard Lyubich --- .../internal/modules/morph/subnet.go | 65 ++++-------------- cmd/neofs-cli/modules/container.go | 16 ++--- cmd/neofs-node/netmap.go | 2 +- go.mod | 2 +- go.sum | Bin 98718 -> 98718 bytes .../processors/container/process_container.go | 7 +- .../netmap/nodevalidation/subnet/calls.go | 7 +- .../processors/netmap/process_peers.go | 4 +- pkg/innerring/processors/subnet/put.go | 4 +- pkg/innerring/processors/subnet/put_test.go | 4 +- pkg/innerring/subnet.go | 2 +- pkg/morph/event/subnet/remove_node_test.go | 5 +- 12 files changed, 30 insertions(+), 88 deletions(-) diff --git a/cmd/neofs-adm/internal/modules/morph/subnet.go b/cmd/neofs-adm/internal/modules/morph/subnet.go index e8dc67c8df..e41e478fdb 100644 --- a/cmd/neofs-adm/internal/modules/morph/subnet.go +++ b/cmd/neofs-adm/internal/modules/morph/subnet.go @@ -140,18 +140,13 @@ var cmdSubnetCreate = &cobra.Command{ for { num = rand.Uint32() - id.SetNumber(num) + id.SetNumeric(num) if !subnetid.IsZero(id) { break } } - binID, err := id.Marshal() - if err != nil { - return fmt.Errorf("marshal subnet ID: %w", err) - } - // declare creator ID and encode it var creator user.ID user.IDFromKey(&creator, key.PrivateKey.PublicKey) @@ -162,12 +157,7 @@ var cmdSubnetCreate = &cobra.Command{ info.SetID(id) info.SetOwner(creator) - binInfo, err := info.Marshal() - if err != nil { - return fmt.Errorf("marshal subnet info: %w", err) - } - - err = invokeMethod(key, true, "put", binID, key.PublicKey().Bytes(), binInfo) + err = invokeMethod(key, true, "put", id.Marshal(), key.PublicKey().Bytes(), info.Marshal()) if err != nil { return fmt.Errorf("morph invocation: %w", err) } @@ -210,7 +200,7 @@ var cmdSubnetRemove = &cobra.Command{ // read ID and encode it var id subnetid.ID - err = id.UnmarshalText([]byte(viper.GetString(flagSubnetRemoveID))) + err = id.DecodeString(viper.GetString(flagSubnetRemoveID)) if err != nil { return fmt.Errorf("decode ID text: %w", err) } @@ -219,12 +209,7 @@ var cmdSubnetRemove = &cobra.Command{ return errZeroSubnet } - binID, err := id.Marshal() - if err != nil { - return fmt.Errorf("marshal subnet ID: %w", err) - } - - err = invokeMethod(key, false, "delete", binID) + err = invokeMethod(key, false, "delete", id.Marshal()) if err != nil { return fmt.Errorf("morph invocation: %w", err) } @@ -254,7 +239,7 @@ var cmdSubnetGet = &cobra.Command{ // read ID and encode it var id subnetid.ID - err := id.UnmarshalText([]byte(viper.GetString(flagSubnetGetID))) + err := id.DecodeString(viper.GetString(flagSubnetGetID)) if err != nil { return fmt.Errorf("decode ID text: %w", err) } @@ -263,11 +248,6 @@ var cmdSubnetGet = &cobra.Command{ return errZeroSubnet } - binID, err := id.Marshal() - if err != nil { - return fmt.Errorf("marshal subnet ID: %w", err) - } - // use random key to fetch the data // we could use raw neo-go client to perform testInvoke // without keys, as it is done in other commands @@ -276,7 +256,7 @@ var cmdSubnetGet = &cobra.Command{ return fmt.Errorf("init subnet client: %w", err) } - res, err := testInvokeMethod(*key, "get", binID) + res, err := testInvokeMethod(*key, "get", id.Marshal()) if err != nil { return fmt.Errorf("morph invocation: %w", err) } @@ -297,11 +277,7 @@ var cmdSubnetGet = &cobra.Command{ } // print information - var ownerID user.ID - - info.ReadOwner(&ownerID) - - cmd.Printf("Owner: %s\n", &ownerID) + cmd.Printf("Owner: %s\n", info.Owner()) return nil }, @@ -350,7 +326,7 @@ func manageSubnetAdmins(cmd *cobra.Command, rm bool) error { // read ID and encode it var id subnetid.ID - err = id.UnmarshalText([]byte(viper.GetString(flagSubnetAdminSubnet))) + err = id.DecodeString(viper.GetString(flagSubnetAdminSubnet)) if err != nil { return fmt.Errorf("decode ID text: %w", err) } @@ -359,11 +335,6 @@ func manageSubnetAdmins(cmd *cobra.Command, rm bool) error { return errZeroSubnet } - binID, err := id.Marshal() - if err != nil { - return fmt.Errorf("marshal subnet ID: %w", err) - } - // read admin key and decode it binAdminKey, err := hex.DecodeString(viper.GetString(flagSubnetAdminID)) if err != nil { @@ -377,7 +348,7 @@ func manageSubnetAdmins(cmd *cobra.Command, rm bool) error { // prepare call parameters prm := make([]interface{}, 0, 3) - prm = append(prm, binID) + prm = append(prm, id.Marshal()) var method string @@ -497,7 +468,7 @@ func manageSubnetClients(cmd *cobra.Command, rm bool) error { // read ID and encode it var id subnetid.ID - err = id.UnmarshalText([]byte(viper.GetString(flagSubnetClientSubnet))) + err = id.DecodeString(viper.GetString(flagSubnetClientSubnet)) if err != nil { return fmt.Errorf("decode ID text: %w", err) } @@ -506,11 +477,6 @@ func manageSubnetClients(cmd *cobra.Command, rm bool) error { return errZeroSubnet } - binID, err := id.Marshal() - if err != nil { - return fmt.Errorf("marshal subnet ID: %w", err) - } - // read client ID and encode it var clientID user.ID @@ -539,7 +505,7 @@ func manageSubnetClients(cmd *cobra.Command, rm bool) error { method = "addUser" } - err = invokeMethod(key, false, method, binID, binGroupID, clientID.WalletBytes()) + err = invokeMethod(key, false, method, id.Marshal(), binGroupID, clientID.WalletBytes()) if err != nil { return fmt.Errorf("morph invocation: %w", err) } @@ -596,7 +562,7 @@ func manageSubnetNodes(cmd *cobra.Command, rm bool) error { // read ID and encode it var id subnetid.ID - err = id.UnmarshalText([]byte(viper.GetString(flagSubnetNodeSubnet))) + err = id.DecodeString(viper.GetString(flagSubnetNodeSubnet)) if err != nil { return fmt.Errorf("decode ID text: %w", err) } @@ -605,11 +571,6 @@ func manageSubnetNodes(cmd *cobra.Command, rm bool) error { return errZeroSubnet } - binID, err := id.Marshal() - if err != nil { - return fmt.Errorf("marshal subnet ID: %w", err) - } - // read node ID and encode it binNodeID, err := hex.DecodeString(viper.GetString(flagSubnetNode)) if err != nil { @@ -628,7 +589,7 @@ func manageSubnetNodes(cmd *cobra.Command, rm bool) error { method = "addNode" } - err = invokeMethod(key, false, method, binID, binNodeID) + err = invokeMethod(key, false, method, id.Marshal(), binNodeID) if err != nil { return fmt.Errorf("morph invocation: %w", err) } diff --git a/cmd/neofs-cli/modules/container.go b/cmd/neofs-cli/modules/container.go index 142b454c5b..0acc2ea04c 100644 --- a/cmd/neofs-cli/modules/container.go +++ b/cmd/neofs-cli/modules/container.go @@ -141,10 +141,12 @@ It will be stored in sidechain when inner ring will accepts it.`, placementPolicy, err := parseContainerPolicy(containerPolicy) common.ExitOnErr(cmd, "", err) - subnetID, err := parseSubnetID(containerSubnet) + var subnetID subnetid.ID + + err = subnetID.DecodeString(containerSubnet) common.ExitOnErr(cmd, "could not parse subnetID: %w", err) - placementPolicy.SetSubnetID(subnetID) + placementPolicy.SetSubnetID(&subnetID) attributes, err := parseAttributes(containerAttributes) common.ExitOnErr(cmd, "", err) @@ -596,16 +598,6 @@ func prettyPrintContainerList(cmd *cobra.Command, list []cid.ID) { } } -func parseSubnetID(val string) (sub *subnetid.ID, err error) { - sub = &subnetid.ID{} - - if val != "" { - err = sub.UnmarshalText([]byte(val)) - } - - return -} - func parseContainerPolicy(policyString string) (*netmap.PlacementPolicy, error) { _, err := os.Stat(policyString) // check if `policyString` is a path to file with placement policy if err == nil { diff --git a/cmd/neofs-node/netmap.go b/cmd/neofs-node/netmap.go index ac8879f87c..f79d7145f1 100644 --- a/cmd/neofs-node/netmap.go +++ b/cmd/neofs-node/netmap.go @@ -186,7 +186,7 @@ func readSubnetCfg(c *cfg) { ) subnetCfg.IterateSubnets(func(idTxt string) { - err = id.UnmarshalText([]byte(idTxt)) + err = id.DecodeString(idTxt) fatalOnErrDetails("parse subnet entry", err) c.cfgNodeInfo.localInfo.EnterSubnet(id) diff --git a/go.mod b/go.mod index 7012823f07..e582d4826b 100644 --- a/go.mod +++ b/go.mod @@ -19,7 +19,7 @@ require ( github.com/nspcc-dev/neo-go/pkg/interop v0.0.0-20220601120906-3bec6657f5c5 // indirect github.com/nspcc-dev/neofs-api-go/v2 v2.12.2 github.com/nspcc-dev/neofs-contract v0.15.1 - github.com/nspcc-dev/neofs-sdk-go v1.0.0-rc.4.0.20220609064532-517d7a1e4a0f + github.com/nspcc-dev/neofs-sdk-go v1.0.0-rc.4.0.20220615085207-eb3b99081235 github.com/nspcc-dev/tzhash v1.5.2 github.com/panjf2000/ants/v2 v2.4.0 github.com/paulmach/orb v0.2.2 diff --git a/go.sum b/go.sum index 6c1c756e50e8a6993ff94aeb81b1790c1adf2128..9eb96d5984f404f36c01b7fc2859936e62f562d7 100644 GIT binary patch delta 113 zcmbQ&%r>u?ZG*yG7eiA63sWNlbKTS=<0MN<0}DeVV^f6;Lo1KmFoVz#|I*?BpD67} zx6p9y9G|p;M7M08uu3D};Lu{f^7Mj`#0Yoiu?ZG*yGX9G(EGZRx|BVAKN^Az($!&H+*gEWN^cNq+GKI_ZsR_$&T|RCjXzKD2mmr%^T;weGCBRT_w5z diff --git a/pkg/innerring/processors/container/process_container.go b/pkg/innerring/processors/container/process_container.go index bb464da58d..d761f15d36 100644 --- a/pkg/innerring/processors/container/process_container.go +++ b/pkg/innerring/processors/container/process_container.go @@ -247,12 +247,7 @@ func checkSubnet(subCli *morphsubnet.Client, cnr *containerSDK.Container) error return nil } - rawSubID, err := subID.Marshal() - if err != nil { - return fmt.Errorf("could not marshal container subnetwork: %w", err) - } - - prm.SetID(rawSubID) + prm.SetID(subID.Marshal()) prm.SetClient(owner.WalletBytes()) res, err := subCli.UserAllowed(prm) diff --git a/pkg/innerring/processors/netmap/nodevalidation/subnet/calls.go b/pkg/innerring/processors/netmap/nodevalidation/subnet/calls.go index 70438de1ba..399cf865fb 100644 --- a/pkg/innerring/processors/netmap/nodevalidation/subnet/calls.go +++ b/pkg/innerring/processors/netmap/nodevalidation/subnet/calls.go @@ -20,12 +20,7 @@ func (v *Validator) VerifyAndUpdate(n *netmap.NodeInfo) error { return nil } - rawSubnetID, err := id.Marshal() - if err != nil { - return fmt.Errorf("could not marshal subnetwork ID: %w", err) - } - - prm.SetID(rawSubnetID) + prm.SetID(id.Marshal()) prm.SetNode(n.PublicKey()) res, err := v.subnetClient.NodeAllowed(prm) diff --git a/pkg/innerring/processors/netmap/process_peers.go b/pkg/innerring/processors/netmap/process_peers.go index 4777700b7b..9edb87d51b 100644 --- a/pkg/innerring/processors/netmap/process_peers.go +++ b/pkg/innerring/processors/netmap/process_peers.go @@ -166,7 +166,7 @@ func (np *Processor) processRemoveSubnetNode(ev subnetEvent.RemoveNode) { } rawSubnet := ev.SubnetworkID() - subnetToRemoveFrom := &subnetid.ID{} + var subnetToRemoveFrom subnetid.ID err = subnetToRemoveFrom.Unmarshal(rawSubnet) if err != nil { @@ -176,7 +176,7 @@ func (np *Processor) processRemoveSubnetNode(ev subnetEvent.RemoveNode) { return } - if subnetid.IsZero(*subnetToRemoveFrom) { + if subnetid.IsZero(subnetToRemoveFrom) { np.log.Warn("got zero subnet in remove node notification") return } diff --git a/pkg/innerring/processors/subnet/put.go b/pkg/innerring/processors/subnet/put.go index 7d4dc25d6a..1b7f4905b0 100644 --- a/pkg/innerring/processors/subnet/put.go +++ b/pkg/innerring/processors/subnet/put.go @@ -69,12 +69,12 @@ func (x PutValidator) Assert(event Put) error { } // check if the explicit ID equals to the one from info - if !subnet.IDEquals(info, id) { + if !subnet.AssertReference(info, id) { return errDiffID } // check if the explicit creator equals to the one from info - if !subnet.IsOwner(info, creator) { + if !subnet.AssertOwnership(info, creator) { return errDiffOwner } diff --git a/pkg/innerring/processors/subnet/put_test.go b/pkg/innerring/processors/subnet/put_test.go index 62452230b6..d91521e2d3 100644 --- a/pkg/innerring/processors/subnet/put_test.go +++ b/pkg/innerring/processors/subnet/put_test.go @@ -68,7 +68,7 @@ func TestPutValidator_Assert(t *testing.T) { require.ErrorAs(t, err, new(zeroSubnetOp)) const idNum = 13 - e.id.SetNumber(idNum) + e.id.SetNumeric(idNum) // read creator error e.creatorErr = errors.New("creator err") @@ -89,7 +89,7 @@ func TestPutValidator_Assert(t *testing.T) { // diff explicit ID and the one in info var id2 subnetid.ID - id2.SetNumber(idNum + 1) + id2.SetNumeric(idNum + 1) e.info.SetID(id2) diff --git a/pkg/innerring/subnet.go b/pkg/innerring/subnet.go index 5d040ab23f..f60d736a04 100644 --- a/pkg/innerring/subnet.go +++ b/pkg/innerring/subnet.go @@ -314,7 +314,7 @@ func (s *Server) processCandidate(txHash neogoutil.Uint256, removedID subnetid.I ) err := c.NodeInfo.IterateSubnets(func(id subnetid.ID) error { - if removedID.Equals(&id) { + if removedID.Equals(id) { removeSubnet = true return netmap.ErrRemoveSubnet } diff --git a/pkg/morph/event/subnet/remove_node_test.go b/pkg/morph/event/subnet/remove_node_test.go index 17410a0129..dcad25e9e7 100644 --- a/pkg/morph/event/subnet/remove_node_test.go +++ b/pkg/morph/event/subnet/remove_node_test.go @@ -25,10 +25,9 @@ func TestParseRemoveNode(t *testing.T) { }) subnetID := subnetid.ID{} - subnetID.SetNumber(123) + subnetID.SetNumeric(123) - rawSubnetID, err := subnetID.Marshal() - require.NoError(t, err) + rawSubnetID := subnetID.Marshal() priv, err := keys.NewPrivateKey() require.NoError(t, err)