2023-11-20 14:03:19 +00:00
|
|
|
package frostfsid
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
frostfsidclient "git.frostfs.info/TrueCloudLab/frostfs-contract/frostfsid/client"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/morph/client"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/util"
|
2024-07-03 12:31:39 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/vm/stackitem"
|
2023-11-20 14:03:19 +00:00
|
|
|
)
|
|
|
|
|
2024-05-02 16:45:09 +00:00
|
|
|
const (
|
|
|
|
methodGetSubject = "getSubject"
|
|
|
|
methodGetSubjectExtended = "getSubjectExtended"
|
|
|
|
)
|
2023-11-20 14:03:19 +00:00
|
|
|
|
|
|
|
func (c *Client) GetSubject(addr util.Uint160) (*frostfsidclient.Subject, error) {
|
|
|
|
prm := client.TestInvokePrm{}
|
|
|
|
prm.SetMethod(methodGetSubject)
|
|
|
|
prm.SetArgs(addr)
|
|
|
|
|
|
|
|
res, err := c.client.TestInvoke(prm)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("could not perform test invocation (%s): %w", methodGetSubject, err)
|
|
|
|
}
|
|
|
|
|
2024-07-12 14:56:04 +00:00
|
|
|
structArr, err := checkStackItem(res)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("invalid test invocation result (%s): %w", methodGetSubjectExtended, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
subj, err := frostfsidclient.ParseSubject(structArr)
|
2023-11-20 14:03:19 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("could not parse test invocation result (%s): %w", methodGetSubject, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return subj, nil
|
|
|
|
}
|
|
|
|
|
2024-05-02 16:45:09 +00:00
|
|
|
func (c *Client) GetSubjectExtended(addr util.Uint160) (*frostfsidclient.SubjectExtended, error) {
|
|
|
|
prm := client.TestInvokePrm{}
|
|
|
|
prm.SetMethod(methodGetSubjectExtended)
|
|
|
|
prm.SetArgs(addr)
|
|
|
|
|
|
|
|
res, err := c.client.TestInvoke(prm)
|
|
|
|
if err != nil {
|
2024-07-12 14:56:04 +00:00
|
|
|
return nil, fmt.Errorf("could not perform test invocation (%s): %w", methodGetSubjectExtended, err)
|
2024-07-03 12:31:39 +00:00
|
|
|
}
|
|
|
|
|
2024-07-12 14:56:04 +00:00
|
|
|
structArr, err := checkStackItem(res)
|
2024-07-03 12:31:39 +00:00
|
|
|
if err != nil {
|
2024-07-12 14:56:04 +00:00
|
|
|
return nil, fmt.Errorf("invalid test invocation result (%s): %w", methodGetSubjectExtended, err)
|
2024-07-03 12:31:39 +00:00
|
|
|
}
|
|
|
|
|
2024-07-12 14:56:04 +00:00
|
|
|
subj, err := frostfsidclient.ParseSubjectExtended(structArr)
|
2024-07-03 12:31:39 +00:00
|
|
|
if err != nil {
|
2024-07-12 14:56:04 +00:00
|
|
|
return nil, fmt.Errorf("could not parse test invocation result (%s): %w", methodGetSubject, err)
|
2024-07-03 12:31:39 +00:00
|
|
|
}
|
|
|
|
|
2024-07-12 14:56:04 +00:00
|
|
|
return subj, nil
|
2024-07-03 12:31:39 +00:00
|
|
|
}
|
|
|
|
|
2024-07-12 14:56:04 +00:00
|
|
|
func checkStackItem(res []stackitem.Item) (structArr []stackitem.Item, err error) {
|
2024-07-03 12:31:39 +00:00
|
|
|
if ln := len(res); ln != 1 {
|
|
|
|
return nil, fmt.Errorf("unexpected stack item count (%s): %d", methodGetSubject, ln)
|
|
|
|
}
|
|
|
|
|
2024-07-12 14:56:04 +00:00
|
|
|
structArr, err = client.ArrayFromStackItem(res[0])
|
2024-07-03 12:31:39 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("could not get item array of container (%s): %w", methodGetSubject, err)
|
|
|
|
}
|
2024-07-12 14:56:04 +00:00
|
|
|
return
|
2024-07-03 12:31:39 +00:00
|
|
|
}
|