forked from TrueCloudLab/frostfs-node
[#908] adm/frostfsid: Use client for write operations
Signed-off-by: Denis Kirillov <d.kirillov@yadro.com>
This commit is contained in:
parent
6ebd61298e
commit
136acdba21
1 changed files with 93 additions and 61 deletions
|
@ -1,15 +1,18 @@
|
|||
package morph
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"sort"
|
||||
|
||||
frostfsidclient "git.frostfs.info/TrueCloudLab/frostfs-contract/frostfsid/client"
|
||||
commonCmd "git.frostfs.info/TrueCloudLab/frostfs-node/cmd/internal/common"
|
||||
"github.com/nspcc-dev/neo-go/pkg/core/state"
|
||||
"github.com/nspcc-dev/neo-go/pkg/encoding/address"
|
||||
"github.com/nspcc-dev/neo-go/pkg/io"
|
||||
"github.com/nspcc-dev/neo-go/pkg/rpcclient/management"
|
||||
"github.com/nspcc-dev/neo-go/pkg/smartcontract/callflag"
|
||||
"github.com/nspcc-dev/neo-go/pkg/util"
|
||||
"github.com/nspcc-dev/neo-go/pkg/vm/emit"
|
||||
"github.com/spf13/cobra"
|
||||
"github.com/spf13/viper"
|
||||
|
@ -216,23 +219,23 @@ func initFrostfsIDListGroupSubjectsCmd() {
|
|||
frostfsidListGroupSubjectsCmd.Flags().Bool(includeNamesFlag, false, "Whether include subject name (require additional requests)")
|
||||
}
|
||||
|
||||
type ffsidMethodArgs struct {
|
||||
name string
|
||||
args []any
|
||||
}
|
||||
|
||||
func frostfsidCreateNamespace(cmd *cobra.Command, _ []string) {
|
||||
ns, _ := cmd.Flags().GetString(namespaceFlag)
|
||||
|
||||
err := sendFrostfsIDTx(cmd, ffsidMethodArgs{name: "createNamespace", args: []any{ns}})
|
||||
commonCmd.ExitOnErr(cmd, "processing error: %w", err)
|
||||
ffsid, err := newFrostfsIDClient(cmd)
|
||||
commonCmd.ExitOnErr(cmd, "init contract client: %w", err)
|
||||
|
||||
ffsid.addCall(ffsid.roCli.CreateNamespaceCall(ns))
|
||||
|
||||
err = ffsid.sendWait()
|
||||
commonCmd.ExitOnErr(cmd, "create namespace error: %w", err)
|
||||
}
|
||||
|
||||
func frostfsidListNamespaces(cmd *cobra.Command, _ []string) {
|
||||
ffsid, err := frostfsIDClient(cmd)
|
||||
ffsid, err := newFrostfsIDClient(cmd)
|
||||
commonCmd.ExitOnErr(cmd, "init contract invoker: %w", err)
|
||||
|
||||
namespaces, err := ffsid.ListNamespaces()
|
||||
namespaces, err := ffsid.roCli.ListNamespaces()
|
||||
commonCmd.ExitOnErr(cmd, "list namespaces: %w", err)
|
||||
|
||||
sort.Slice(namespaces, func(i, j int) bool { return namespaces[i].Name < namespaces[j].Name })
|
||||
|
@ -247,22 +250,27 @@ func frostfsidCreateSubject(cmd *cobra.Command, _ []string) {
|
|||
subjName, _ := cmd.Flags().GetString(subjectNameFlag)
|
||||
subjKey := getFrostfsIDSubjectKey(cmd)
|
||||
|
||||
args := []ffsidMethodArgs{
|
||||
{name: "createSubject", args: []any{ns, subjKey.Bytes()}},
|
||||
}
|
||||
ffsid, err := newFrostfsIDClient(cmd)
|
||||
commonCmd.ExitOnErr(cmd, "init contract client: %w", err)
|
||||
|
||||
ffsid.addCall(ffsid.roCli.CreateSubjectCall(ns, subjKey))
|
||||
if subjName != "" {
|
||||
args = append(args, ffsidMethodArgs{name: "setSubjectName", args: []any{subjKey.GetScriptHash(), subjName}})
|
||||
ffsid.addCall(ffsid.roCli.SetSubjectNameCall(subjKey.GetScriptHash(), subjName))
|
||||
}
|
||||
|
||||
err := sendFrostfsIDTx(cmd, args...)
|
||||
commonCmd.ExitOnErr(cmd, "processing error: %w", err)
|
||||
err = ffsid.sendWait()
|
||||
commonCmd.ExitOnErr(cmd, "create subject: %w", err)
|
||||
}
|
||||
|
||||
func frostfsidDeleteSubject(cmd *cobra.Command, _ []string) {
|
||||
subjectAddress := getFrostfsIDSubjectAddress(cmd)
|
||||
|
||||
err := sendFrostfsIDTx(cmd, ffsidMethodArgs{name: "deleteSubject", args: []any{subjectAddress}})
|
||||
ffsid, err := newFrostfsIDClient(cmd)
|
||||
commonCmd.ExitOnErr(cmd, "init contract client: %w", err)
|
||||
|
||||
ffsid.addCall(ffsid.roCli.DeleteSubjectCall(subjectAddress))
|
||||
|
||||
err = ffsid.sendWait()
|
||||
commonCmd.ExitOnErr(cmd, "delete subject error: %w", err)
|
||||
}
|
||||
|
||||
|
@ -270,10 +278,10 @@ func frostfsidListSubjects(cmd *cobra.Command, _ []string) {
|
|||
ns, _ := cmd.Flags().GetString(namespaceFlag)
|
||||
includeNames, _ := cmd.Flags().GetBool(includeNamesFlag)
|
||||
|
||||
ffsid, err := frostfsIDClient(cmd)
|
||||
ffsid, err := newFrostfsIDClient(cmd)
|
||||
commonCmd.ExitOnErr(cmd, "init contract invoker: %w", err)
|
||||
|
||||
subAddresses, err := ffsid.ListNamespaceSubjects(ns)
|
||||
subAddresses, err := ffsid.roCli.ListNamespaceSubjects(ns)
|
||||
commonCmd.ExitOnErr(cmd, "list subjects: %w", err)
|
||||
|
||||
sort.Slice(subAddresses, func(i, j int) bool { return subAddresses[i].Less(subAddresses[j]) })
|
||||
|
@ -284,7 +292,7 @@ func frostfsidListSubjects(cmd *cobra.Command, _ []string) {
|
|||
continue
|
||||
}
|
||||
|
||||
subj, err := ffsid.GetSubject(addr)
|
||||
subj, err := ffsid.roCli.GetSubject(addr)
|
||||
commonCmd.ExitOnErr(cmd, "get subject: %w", err)
|
||||
|
||||
cmd.Printf("%s (%s)\n", address.Uint160ToString(addr), subj.Name)
|
||||
|
@ -295,10 +303,12 @@ func frostfsidCreateGroup(cmd *cobra.Command, _ []string) {
|
|||
ns, _ := cmd.Flags().GetString(namespaceFlag)
|
||||
groupName, _ := cmd.Flags().GetString(groupNameFlag)
|
||||
|
||||
ffsid, err := frostfsIDClient(cmd)
|
||||
ffsid, err := newFrostfsIDClient(cmd)
|
||||
commonCmd.ExitOnErr(cmd, "init contract client: %w", err)
|
||||
|
||||
groupID, err := ffsid.ParseGroupID(ffsid.Wait(ffsid.CreateGroup(ns, groupName)))
|
||||
ffsid.addCall(ffsid.roCli.CreateGroupCall(ns, groupName))
|
||||
|
||||
groupID, err := ffsid.roCli.ParseGroupID(ffsid.sendWaitRes())
|
||||
commonCmd.ExitOnErr(cmd, "create group: %w", err)
|
||||
|
||||
cmd.Printf("group '%s' created with id: %d\n", groupName, groupID)
|
||||
|
@ -308,20 +318,22 @@ func frostfsidDeleteGroup(cmd *cobra.Command, _ []string) {
|
|||
ns, _ := cmd.Flags().GetString(namespaceFlag)
|
||||
groupID, _ := cmd.Flags().GetInt64(groupIDFlag)
|
||||
|
||||
ffsid, err := frostfsIDClient(cmd)
|
||||
ffsid, err := newFrostfsIDClient(cmd)
|
||||
commonCmd.ExitOnErr(cmd, "init contract client: %w", err)
|
||||
|
||||
_, err = ffsid.Wait(ffsid.DeleteGroup(ns, groupID))
|
||||
ffsid.addCall(ffsid.roCli.DeleteGroupCall(ns, groupID))
|
||||
|
||||
err = ffsid.sendWait()
|
||||
commonCmd.ExitOnErr(cmd, "delete group error: %w", err)
|
||||
}
|
||||
|
||||
func frostfsidListGroups(cmd *cobra.Command, _ []string) {
|
||||
ns, _ := cmd.Flags().GetString(namespaceFlag)
|
||||
|
||||
ffsid, err := frostfsIDClient(cmd)
|
||||
ffsid, err := newFrostfsIDClient(cmd)
|
||||
commonCmd.ExitOnErr(cmd, "init contract invoker: %w", err)
|
||||
|
||||
groups, err := ffsid.ListGroups(ns)
|
||||
groups, err := ffsid.roCli.ListGroups(ns)
|
||||
commonCmd.ExitOnErr(cmd, "list groups: %w", err)
|
||||
|
||||
sort.Slice(groups, func(i, j int) bool { return groups[i].Name < groups[j].Name })
|
||||
|
@ -335,16 +347,26 @@ func frostfsidAddSubjectToGroup(cmd *cobra.Command, _ []string) {
|
|||
subjectAddress := getFrostfsIDSubjectAddress(cmd)
|
||||
groupID, _ := cmd.Flags().GetInt64(groupIDFlag)
|
||||
|
||||
err := sendFrostfsIDTx(cmd, ffsidMethodArgs{name: "addSubjectToGroup", args: []any{subjectAddress, groupID}})
|
||||
commonCmd.ExitOnErr(cmd, "add subject to namespace error: %w", err)
|
||||
ffsid, err := newFrostfsIDClient(cmd)
|
||||
commonCmd.ExitOnErr(cmd, "init contract client: %w", err)
|
||||
|
||||
ffsid.addCall(ffsid.roCli.AddSubjectToGroupCall(subjectAddress, groupID))
|
||||
|
||||
err = ffsid.sendWait()
|
||||
commonCmd.ExitOnErr(cmd, "add subject to group error: %w", err)
|
||||
}
|
||||
|
||||
func frostfsidRemoveSubjectFromGroup(cmd *cobra.Command, _ []string) {
|
||||
subjectAddress := getFrostfsIDSubjectAddress(cmd)
|
||||
groupID, _ := cmd.Flags().GetInt64(groupIDFlag)
|
||||
|
||||
err := sendFrostfsIDTx(cmd, ffsidMethodArgs{name: "removeSubjectFromGroup", args: []any{subjectAddress, groupID}})
|
||||
commonCmd.ExitOnErr(cmd, "remove subject to namespace error: %w", err)
|
||||
ffsid, err := newFrostfsIDClient(cmd)
|
||||
commonCmd.ExitOnErr(cmd, "init contract client: %w", err)
|
||||
|
||||
ffsid.addCall(ffsid.roCli.RemoveSubjectFromGroupCall(subjectAddress, groupID))
|
||||
|
||||
err = ffsid.sendWait()
|
||||
commonCmd.ExitOnErr(cmd, "remove subject from group error: %w", err)
|
||||
}
|
||||
|
||||
func frostfsidListGroupSubjects(cmd *cobra.Command, _ []string) {
|
||||
|
@ -352,10 +374,10 @@ func frostfsidListGroupSubjects(cmd *cobra.Command, _ []string) {
|
|||
groupID, _ := cmd.Flags().GetInt64(groupIDFlag)
|
||||
includeNames, _ := cmd.Flags().GetBool(includeNamesFlag)
|
||||
|
||||
ffsid, err := frostfsIDClient(cmd)
|
||||
ffsid, err := newFrostfsIDClient(cmd)
|
||||
commonCmd.ExitOnErr(cmd, "init contract client: %w", err)
|
||||
|
||||
subjects, err := ffsid.ListGroupSubjects(ns, groupID)
|
||||
subjects, err := ffsid.roCli.ListGroupSubjects(ns, groupID)
|
||||
commonCmd.ExitOnErr(cmd, "list group subjects: %w", err)
|
||||
|
||||
sort.Slice(subjects, func(i, j int) bool { return subjects[i].Less(subjects[j]) })
|
||||
|
@ -366,43 +388,21 @@ func frostfsidListGroupSubjects(cmd *cobra.Command, _ []string) {
|
|||
continue
|
||||
}
|
||||
|
||||
subj, err := ffsid.GetSubject(subjAddr)
|
||||
subj, err := ffsid.roCli.GetSubject(subjAddr)
|
||||
commonCmd.ExitOnErr(cmd, "get subject: %w", err)
|
||||
|
||||
cmd.Printf("%s (%s)\n", address.Uint160ToString(subjAddr), subj.Name)
|
||||
}
|
||||
}
|
||||
|
||||
func sendFrostfsIDTx(cmd *cobra.Command, methodArgs ...ffsidMethodArgs) error {
|
||||
wCtx, err := newInitializeContext(cmd, viper.GetViper())
|
||||
if err != nil {
|
||||
return fmt.Errorf("can't to initialize context: %w", err)
|
||||
type frostfsidClient struct {
|
||||
bw *io.BufBinWriter
|
||||
contractHash util.Uint160
|
||||
roCli *frostfsidclient.Client // client can be used only for waiting tx, parsing and forming method params
|
||||
wCtx *initializeContext
|
||||
}
|
||||
|
||||
r := management.NewReader(wCtx.ReadOnlyInvoker)
|
||||
cs, err := r.GetContractByID(1)
|
||||
if err != nil {
|
||||
return fmt.Errorf("can't get NNS contract info: %w", err)
|
||||
}
|
||||
|
||||
ffsidHash, err := nnsResolveHash(wCtx.ReadOnlyInvoker, cs.Hash, domainOf(frostfsIDContract))
|
||||
if err != nil {
|
||||
return fmt.Errorf("can't get proxy contract hash: %w", err)
|
||||
}
|
||||
|
||||
bw := io.NewBufBinWriter()
|
||||
for _, method := range methodArgs {
|
||||
emit.AppCall(bw.BinWriter, ffsidHash, method.name, callflag.All, method.args...)
|
||||
}
|
||||
|
||||
if err = wCtx.sendConsensusTx(bw.Bytes()); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return wCtx.awaitTx()
|
||||
}
|
||||
|
||||
func frostfsIDClient(cmd *cobra.Command) (*frostfsidclient.Client, error) {
|
||||
func newFrostfsIDClient(cmd *cobra.Command) (*frostfsidClient, error) {
|
||||
wCtx, err := newInitializeContext(cmd, viper.GetViper())
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("can't to initialize context: %w", err)
|
||||
|
@ -419,5 +419,37 @@ func frostfsIDClient(cmd *cobra.Command) (*frostfsidclient.Client, error) {
|
|||
return nil, fmt.Errorf("can't get proxy contract hash: %w", err)
|
||||
}
|
||||
|
||||
return frostfsidclient.NewSimple(wCtx.CommitteeAct, ffsidHash), nil
|
||||
return &frostfsidClient{
|
||||
bw: io.NewBufBinWriter(),
|
||||
contractHash: ffsidHash,
|
||||
roCli: frostfsidclient.NewSimple(wCtx.CommitteeAct, ffsidHash),
|
||||
wCtx: wCtx,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (f *frostfsidClient) addCall(method string, args []any) {
|
||||
emit.AppCall(f.bw.BinWriter, f.contractHash, method, callflag.All, args...)
|
||||
}
|
||||
|
||||
func (f *frostfsidClient) sendWait() error {
|
||||
if err := f.wCtx.sendConsensusTx(f.bw.Bytes()); err != nil {
|
||||
return err
|
||||
}
|
||||
f.bw.Reset()
|
||||
|
||||
return f.wCtx.awaitTx()
|
||||
}
|
||||
|
||||
func (f *frostfsidClient) sendWaitRes() (*state.AppExecResult, error) {
|
||||
if err := f.wCtx.sendConsensusTx(f.bw.Bytes()); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
f.bw.Reset()
|
||||
|
||||
if len(f.wCtx.SentTxs) == 0 {
|
||||
return nil, errors.New("no transactions to wait")
|
||||
}
|
||||
|
||||
f.wCtx.Command.Println("Waiting for transactions to persist...")
|
||||
return f.roCli.Wait(f.wCtx.SentTxs[0].hash, f.wCtx.SentTxs[0].vub, nil)
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue