[#1518] Upgrade NeoFS SDK Go with changed subnet package

Signed-off-by: Leonard Lyubich <leonard@nspcc.ru>
This commit is contained in:
Leonard Lyubich 2022-06-15 11:08:10 +03:00 committed by LeL
parent f602d05b0a
commit 2e4a1cb6df
12 changed files with 32 additions and 90 deletions

View file

@ -140,18 +140,13 @@ var cmdSubnetCreate = &cobra.Command{
for { for {
num = rand.Uint32() num = rand.Uint32()
id.SetNumber(num) id.SetNumeric(num)
if !subnetid.IsZero(id) { if !subnetid.IsZero(id) {
break break
} }
} }
binID, err := id.Marshal()
if err != nil {
return fmt.Errorf("marshal subnet ID: %w", err)
}
// declare creator ID and encode it // declare creator ID and encode it
var creator user.ID var creator user.ID
user.IDFromKey(&creator, key.PrivateKey.PublicKey) user.IDFromKey(&creator, key.PrivateKey.PublicKey)
@ -162,12 +157,7 @@ var cmdSubnetCreate = &cobra.Command{
info.SetID(id) info.SetID(id)
info.SetOwner(creator) info.SetOwner(creator)
binInfo, err := info.Marshal() err = invokeMethod(key, true, "put", id.Marshal(), key.PublicKey().Bytes(), info.Marshal())
if err != nil {
return fmt.Errorf("marshal subnet info: %w", err)
}
err = invokeMethod(key, true, "put", binID, key.PublicKey().Bytes(), binInfo)
if err != nil { if err != nil {
return fmt.Errorf("morph invocation: %w", err) return fmt.Errorf("morph invocation: %w", err)
} }
@ -210,7 +200,7 @@ var cmdSubnetRemove = &cobra.Command{
// read ID and encode it // read ID and encode it
var id subnetid.ID var id subnetid.ID
err = id.UnmarshalText([]byte(viper.GetString(flagSubnetRemoveID))) err = id.DecodeString(viper.GetString(flagSubnetRemoveID))
if err != nil { if err != nil {
return fmt.Errorf("decode ID text: %w", err) return fmt.Errorf("decode ID text: %w", err)
} }
@ -219,12 +209,7 @@ var cmdSubnetRemove = &cobra.Command{
return errZeroSubnet return errZeroSubnet
} }
binID, err := id.Marshal() err = invokeMethod(key, false, "delete", id.Marshal())
if err != nil {
return fmt.Errorf("marshal subnet ID: %w", err)
}
err = invokeMethod(key, false, "delete", binID)
if err != nil { if err != nil {
return fmt.Errorf("morph invocation: %w", err) return fmt.Errorf("morph invocation: %w", err)
} }
@ -254,7 +239,7 @@ var cmdSubnetGet = &cobra.Command{
// read ID and encode it // read ID and encode it
var id subnetid.ID var id subnetid.ID
err := id.UnmarshalText([]byte(viper.GetString(flagSubnetGetID))) err := id.DecodeString(viper.GetString(flagSubnetGetID))
if err != nil { if err != nil {
return fmt.Errorf("decode ID text: %w", err) return fmt.Errorf("decode ID text: %w", err)
} }
@ -263,11 +248,6 @@ var cmdSubnetGet = &cobra.Command{
return errZeroSubnet return errZeroSubnet
} }
binID, err := id.Marshal()
if err != nil {
return fmt.Errorf("marshal subnet ID: %w", err)
}
// use random key to fetch the data // use random key to fetch the data
// we could use raw neo-go client to perform testInvoke // we could use raw neo-go client to perform testInvoke
// without keys, as it is done in other commands // 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) return fmt.Errorf("init subnet client: %w", err)
} }
res, err := testInvokeMethod(*key, "get", binID) res, err := testInvokeMethod(*key, "get", id.Marshal())
if err != nil { if err != nil {
return fmt.Errorf("morph invocation: %w", err) return fmt.Errorf("morph invocation: %w", err)
} }
@ -297,11 +277,7 @@ var cmdSubnetGet = &cobra.Command{
} }
// print information // print information
var ownerID user.ID cmd.Printf("Owner: %s\n", info.Owner())
info.ReadOwner(&ownerID)
cmd.Printf("Owner: %s\n", &ownerID)
return nil return nil
}, },
@ -350,7 +326,7 @@ func manageSubnetAdmins(cmd *cobra.Command, rm bool) error {
// read ID and encode it // read ID and encode it
var id subnetid.ID var id subnetid.ID
err = id.UnmarshalText([]byte(viper.GetString(flagSubnetAdminSubnet))) err = id.DecodeString(viper.GetString(flagSubnetAdminSubnet))
if err != nil { if err != nil {
return fmt.Errorf("decode ID text: %w", err) return fmt.Errorf("decode ID text: %w", err)
} }
@ -359,11 +335,6 @@ func manageSubnetAdmins(cmd *cobra.Command, rm bool) error {
return errZeroSubnet return errZeroSubnet
} }
binID, err := id.Marshal()
if err != nil {
return fmt.Errorf("marshal subnet ID: %w", err)
}
// read admin key and decode it // read admin key and decode it
binAdminKey, err := hex.DecodeString(viper.GetString(flagSubnetAdminID)) binAdminKey, err := hex.DecodeString(viper.GetString(flagSubnetAdminID))
if err != nil { if err != nil {
@ -377,7 +348,7 @@ func manageSubnetAdmins(cmd *cobra.Command, rm bool) error {
// prepare call parameters // prepare call parameters
prm := make([]interface{}, 0, 3) prm := make([]interface{}, 0, 3)
prm = append(prm, binID) prm = append(prm, id.Marshal())
var method string var method string
@ -497,7 +468,7 @@ func manageSubnetClients(cmd *cobra.Command, rm bool) error {
// read ID and encode it // read ID and encode it
var id subnetid.ID var id subnetid.ID
err = id.UnmarshalText([]byte(viper.GetString(flagSubnetClientSubnet))) err = id.DecodeString(viper.GetString(flagSubnetClientSubnet))
if err != nil { if err != nil {
return fmt.Errorf("decode ID text: %w", err) return fmt.Errorf("decode ID text: %w", err)
} }
@ -506,11 +477,6 @@ func manageSubnetClients(cmd *cobra.Command, rm bool) error {
return errZeroSubnet return errZeroSubnet
} }
binID, err := id.Marshal()
if err != nil {
return fmt.Errorf("marshal subnet ID: %w", err)
}
// read client ID and encode it // read client ID and encode it
var clientID user.ID var clientID user.ID
@ -539,7 +505,7 @@ func manageSubnetClients(cmd *cobra.Command, rm bool) error {
method = "addUser" method = "addUser"
} }
err = invokeMethod(key, false, method, binID, binGroupID, clientID.WalletBytes()) err = invokeMethod(key, false, method, id.Marshal(), binGroupID, clientID.WalletBytes())
if err != nil { if err != nil {
return fmt.Errorf("morph invocation: %w", err) return fmt.Errorf("morph invocation: %w", err)
} }
@ -596,7 +562,7 @@ func manageSubnetNodes(cmd *cobra.Command, rm bool) error {
// read ID and encode it // read ID and encode it
var id subnetid.ID var id subnetid.ID
err = id.UnmarshalText([]byte(viper.GetString(flagSubnetNodeSubnet))) err = id.DecodeString(viper.GetString(flagSubnetNodeSubnet))
if err != nil { if err != nil {
return fmt.Errorf("decode ID text: %w", err) return fmt.Errorf("decode ID text: %w", err)
} }
@ -605,11 +571,6 @@ func manageSubnetNodes(cmd *cobra.Command, rm bool) error {
return errZeroSubnet return errZeroSubnet
} }
binID, err := id.Marshal()
if err != nil {
return fmt.Errorf("marshal subnet ID: %w", err)
}
// read node ID and encode it // read node ID and encode it
binNodeID, err := hex.DecodeString(viper.GetString(flagSubnetNode)) binNodeID, err := hex.DecodeString(viper.GetString(flagSubnetNode))
if err != nil { if err != nil {
@ -628,7 +589,7 @@ func manageSubnetNodes(cmd *cobra.Command, rm bool) error {
method = "addNode" method = "addNode"
} }
err = invokeMethod(key, false, method, binID, binNodeID) err = invokeMethod(key, false, method, id.Marshal(), binNodeID)
if err != nil { if err != nil {
return fmt.Errorf("morph invocation: %w", err) return fmt.Errorf("morph invocation: %w", err)
} }

View file

@ -141,10 +141,12 @@ It will be stored in sidechain when inner ring will accepts it.`,
placementPolicy, err := parseContainerPolicy(containerPolicy) placementPolicy, err := parseContainerPolicy(containerPolicy)
common.ExitOnErr(cmd, "", err) 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) common.ExitOnErr(cmd, "could not parse subnetID: %w", err)
placementPolicy.SetSubnetID(subnetID) placementPolicy.SetSubnetID(&subnetID)
attributes, err := parseAttributes(containerAttributes) attributes, err := parseAttributes(containerAttributes)
common.ExitOnErr(cmd, "", err) 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) { func parseContainerPolicy(policyString string) (*netmap.PlacementPolicy, error) {
_, err := os.Stat(policyString) // check if `policyString` is a path to file with placement policy _, err := os.Stat(policyString) // check if `policyString` is a path to file with placement policy
if err == nil { if err == nil {

View file

@ -186,7 +186,7 @@ func readSubnetCfg(c *cfg) {
) )
subnetCfg.IterateSubnets(func(idTxt string) { subnetCfg.IterateSubnets(func(idTxt string) {
err = id.UnmarshalText([]byte(idTxt)) err = id.DecodeString(idTxt)
fatalOnErrDetails("parse subnet entry", err) fatalOnErrDetails("parse subnet entry", err)
c.cfgNodeInfo.localInfo.EnterSubnet(id) c.cfgNodeInfo.localInfo.EnterSubnet(id)

2
go.mod
View file

@ -19,7 +19,7 @@ require (
github.com/nspcc-dev/neo-go/pkg/interop v0.0.0-20220601120906-3bec6657f5c5 // indirect 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-api-go/v2 v2.12.2
github.com/nspcc-dev/neofs-contract v0.15.1 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/nspcc-dev/tzhash v1.5.2
github.com/panjf2000/ants/v2 v2.4.0 github.com/panjf2000/ants/v2 v2.4.0
github.com/paulmach/orb v0.2.2 github.com/paulmach/orb v0.2.2

4
go.sum
View file

@ -409,8 +409,8 @@ github.com/nspcc-dev/neofs-crypto v0.3.0 h1:zlr3pgoxuzrmGCxc5W8dGVfA9Rro8diFvVnB
github.com/nspcc-dev/neofs-crypto v0.3.0/go.mod h1:8w16GEJbH6791ktVqHN9YRNH3s9BEEKYxGhlFnp0cDw= github.com/nspcc-dev/neofs-crypto v0.3.0/go.mod h1:8w16GEJbH6791ktVqHN9YRNH3s9BEEKYxGhlFnp0cDw=
github.com/nspcc-dev/neofs-sdk-go v0.0.0-20211201182451-a5b61c4f6477/go.mod h1:dfMtQWmBHYpl9Dez23TGtIUKiFvCIxUZq/CkSIhEpz4= github.com/nspcc-dev/neofs-sdk-go v0.0.0-20211201182451-a5b61c4f6477/go.mod h1:dfMtQWmBHYpl9Dez23TGtIUKiFvCIxUZq/CkSIhEpz4=
github.com/nspcc-dev/neofs-sdk-go v0.0.0-20220113123743-7f3162110659/go.mod h1:/jay1lr3w7NQd/VDBkEhkJmDmyPNsu4W+QV2obsUV40= github.com/nspcc-dev/neofs-sdk-go v0.0.0-20220113123743-7f3162110659/go.mod h1:/jay1lr3w7NQd/VDBkEhkJmDmyPNsu4W+QV2obsUV40=
github.com/nspcc-dev/neofs-sdk-go v1.0.0-rc.4.0.20220609064532-517d7a1e4a0f h1:xqmVaOsc0CPXWVLqtVFfIHfHQtbm6XGlOwVNYCmOeYo= github.com/nspcc-dev/neofs-sdk-go v1.0.0-rc.4.0.20220615085207-eb3b99081235 h1:HmV0UTOusPLZ+YFUW+lLfpaFkLVy2MSUsNwgpTaXGCc=
github.com/nspcc-dev/neofs-sdk-go v1.0.0-rc.4.0.20220609064532-517d7a1e4a0f/go.mod h1:k58jgszGX3pws2yiOXu9m0i32BzRgi1T6Bpd/L1KrJU= github.com/nspcc-dev/neofs-sdk-go v1.0.0-rc.4.0.20220615085207-eb3b99081235/go.mod h1:k58jgszGX3pws2yiOXu9m0i32BzRgi1T6Bpd/L1KrJU=
github.com/nspcc-dev/rfc6979 v0.1.0/go.mod h1:exhIh1PdpDC5vQmyEsGvc4YDM/lyQp/452QxGq/UEso= github.com/nspcc-dev/rfc6979 v0.1.0/go.mod h1:exhIh1PdpDC5vQmyEsGvc4YDM/lyQp/452QxGq/UEso=
github.com/nspcc-dev/rfc6979 v0.2.0 h1:3e1WNxrN60/6N0DW7+UYisLeZJyfqZTNOjeV/toYvOE= github.com/nspcc-dev/rfc6979 v0.2.0 h1:3e1WNxrN60/6N0DW7+UYisLeZJyfqZTNOjeV/toYvOE=
github.com/nspcc-dev/rfc6979 v0.2.0/go.mod h1:exhIh1PdpDC5vQmyEsGvc4YDM/lyQp/452QxGq/UEso= github.com/nspcc-dev/rfc6979 v0.2.0/go.mod h1:exhIh1PdpDC5vQmyEsGvc4YDM/lyQp/452QxGq/UEso=

View file

@ -247,12 +247,7 @@ func checkSubnet(subCli *morphsubnet.Client, cnr *containerSDK.Container) error
return nil return nil
} }
rawSubID, err := subID.Marshal() prm.SetID(subID.Marshal())
if err != nil {
return fmt.Errorf("could not marshal container subnetwork: %w", err)
}
prm.SetID(rawSubID)
prm.SetClient(owner.WalletBytes()) prm.SetClient(owner.WalletBytes())
res, err := subCli.UserAllowed(prm) res, err := subCli.UserAllowed(prm)

View file

@ -20,12 +20,7 @@ func (v *Validator) VerifyAndUpdate(n *netmap.NodeInfo) error {
return nil return nil
} }
rawSubnetID, err := id.Marshal() prm.SetID(id.Marshal())
if err != nil {
return fmt.Errorf("could not marshal subnetwork ID: %w", err)
}
prm.SetID(rawSubnetID)
prm.SetNode(n.PublicKey()) prm.SetNode(n.PublicKey())
res, err := v.subnetClient.NodeAllowed(prm) res, err := v.subnetClient.NodeAllowed(prm)

View file

@ -166,7 +166,7 @@ func (np *Processor) processRemoveSubnetNode(ev subnetEvent.RemoveNode) {
} }
rawSubnet := ev.SubnetworkID() rawSubnet := ev.SubnetworkID()
subnetToRemoveFrom := &subnetid.ID{} var subnetToRemoveFrom subnetid.ID
err = subnetToRemoveFrom.Unmarshal(rawSubnet) err = subnetToRemoveFrom.Unmarshal(rawSubnet)
if err != nil { if err != nil {
@ -176,7 +176,7 @@ func (np *Processor) processRemoveSubnetNode(ev subnetEvent.RemoveNode) {
return return
} }
if subnetid.IsZero(*subnetToRemoveFrom) { if subnetid.IsZero(subnetToRemoveFrom) {
np.log.Warn("got zero subnet in remove node notification") np.log.Warn("got zero subnet in remove node notification")
return return
} }

View file

@ -69,12 +69,12 @@ func (x PutValidator) Assert(event Put) error {
} }
// check if the explicit ID equals to the one from info // check if the explicit ID equals to the one from info
if !subnet.IDEquals(info, id) { if !subnet.AssertReference(info, id) {
return errDiffID return errDiffID
} }
// check if the explicit creator equals to the one from info // check if the explicit creator equals to the one from info
if !subnet.IsOwner(info, creator) { if !subnet.AssertOwnership(info, creator) {
return errDiffOwner return errDiffOwner
} }

View file

@ -68,7 +68,7 @@ func TestPutValidator_Assert(t *testing.T) {
require.ErrorAs(t, err, new(zeroSubnetOp)) require.ErrorAs(t, err, new(zeroSubnetOp))
const idNum = 13 const idNum = 13
e.id.SetNumber(idNum) e.id.SetNumeric(idNum)
// read creator error // read creator error
e.creatorErr = errors.New("creator err") e.creatorErr = errors.New("creator err")
@ -89,7 +89,7 @@ func TestPutValidator_Assert(t *testing.T) {
// diff explicit ID and the one in info // diff explicit ID and the one in info
var id2 subnetid.ID var id2 subnetid.ID
id2.SetNumber(idNum + 1) id2.SetNumeric(idNum + 1)
e.info.SetID(id2) e.info.SetID(id2)

View file

@ -314,7 +314,7 @@ func (s *Server) processCandidate(txHash neogoutil.Uint256, removedID subnetid.I
) )
err := c.NodeInfo.IterateSubnets(func(id subnetid.ID) error { err := c.NodeInfo.IterateSubnets(func(id subnetid.ID) error {
if removedID.Equals(&id) { if removedID.Equals(id) {
removeSubnet = true removeSubnet = true
return netmap.ErrRemoveSubnet return netmap.ErrRemoveSubnet
} }

View file

@ -25,10 +25,9 @@ func TestParseRemoveNode(t *testing.T) {
}) })
subnetID := subnetid.ID{} subnetID := subnetid.ID{}
subnetID.SetNumber(123) subnetID.SetNumeric(123)
rawSubnetID, err := subnetID.Marshal() rawSubnetID := subnetID.Marshal()
require.NoError(t, err)
priv, err := keys.NewPrivateKey() priv, err := keys.NewPrivateKey()
require.NoError(t, err) require.NoError(t, err)