[#48] frostfsid: add GetSubjectByName method

Signed-off-by: Denis Kirillov <d.kirillov@yadro.com>
This commit is contained in:
Denis Kirillov 2023-12-07 15:02:57 +03:00 committed by Denis Kirillov
parent 3fb511ac15
commit 897f538a3c
5 changed files with 53 additions and 0 deletions

View file

@ -95,6 +95,7 @@ const (
addSubjectKeyMethod = "addSubjectKey" addSubjectKeyMethod = "addSubjectKey"
removeSubjectKeyMethod = "removeSubjectKey" removeSubjectKeyMethod = "removeSubjectKey"
getSubjectByKeyMethod = "getSubjectByKey" getSubjectByKeyMethod = "getSubjectByKey"
getSubjectByNameMethod = "getSubjectByName"
getSubjectKeyByNameMethod = "getSubjectKeyByName" getSubjectKeyByNameMethod = "getSubjectKeyByName"
setSubjectKVMethod = "setSubjectKV" setSubjectKVMethod = "setSubjectKV"
setSubjectNameMethod = "setSubjectName" setSubjectNameMethod = "setSubjectName"
@ -305,6 +306,16 @@ func (c Client) GetSubjectByKey(key *keys.PublicKey) (*Subject, error) {
return parseSubject(items) return parseSubject(items)
} }
// GetSubjectByName gets subject by its name (namespace scope).
func (c Client) GetSubjectByName(namespace, subjectName string) (*Subject, error) {
items, err := unwrap.Array(c.act.Call(c.contract, getSubjectByNameMethod, namespace, subjectName))
if err != nil {
return nil, err
}
return parseSubject(items)
}
// GetSubjectKeyByName gets subject public key by its name (namespace scope). // GetSubjectKeyByName gets subject public key by its name (namespace scope).
func (c Client) GetSubjectKeyByName(namespace, subjectName string) (*keys.PublicKey, error) { func (c Client) GetSubjectKeyByName(namespace, subjectName string) (*keys.PublicKey, error) {
return unwrap.PublicKey(c.act.Call(c.contract, getSubjectKeyByNameMethod, namespace, subjectName)) return unwrap.PublicKey(c.act.Call(c.contract, getSubjectKeyByNameMethod, namespace, subjectName))
@ -518,10 +529,19 @@ func (c Client) ListNonEmptyNamespaces() ([]string, error) {
} }
// Wait invokes underlying wait method on actor.Actor. // Wait invokes underlying wait method on actor.Actor.
// Notice that "already exists" err value is treated as an error by this routine unlike actor.Waiter.
func (c Client) Wait(tx util.Uint256, vub uint32, err error) (*state.AppExecResult, error) { func (c Client) Wait(tx util.Uint256, vub uint32, err error) (*state.AppExecResult, error) {
if err != nil {
return nil, err
}
return c.act.Wait(tx, vub, err) return c.act.Wait(tx, vub, err)
} }
// Waiter returns underlying actor.Waiter.
func (c Client) Waiter() actor.Waiter {
return c.act
}
// ParseGroupID fetch groupID from stack after creating group method invocation. // ParseGroupID fetch groupID from stack after creating group method invocation.
func (c Client) ParseGroupID(res *state.AppExecResult, err error) (int64, error) { func (c Client) ParseGroupID(res *state.AppExecResult, err error) (int64, error) {
if err != nil { if err != nil {

View file

@ -9,6 +9,7 @@ safemethods:
- "getSubject" - "getSubject"
- "getSubjectExtended" - "getSubjectExtended"
- "getSubjectByKey" - "getSubjectByKey"
- "getSubjectByName"
- "getSubjectKeyByName" - "getSubjectKeyByName"
- "listGroups" - "listGroups"
- "listGroupSubjects" - "listGroupSubjects"

View file

@ -407,6 +407,12 @@ func GetSubjectByKey(key interop.PublicKey) Subject {
panic("subject not found") panic("subject not found")
} }
func GetSubjectByName(ns, name string) Subject {
key := GetSubjectKeyByName(ns, name)
addr := contract.CreateStandardAccount(key)
return GetSubject(addr)
}
func GetSubjectKeyByName(ns, name string) interop.PublicKey { func GetSubjectKeyByName(ns, name string) interop.PublicKey {
if name == "" { if name == "" {
panic("invalid or name") panic("invalid or name")

View file

@ -207,6 +207,11 @@ func (c *ContractReader) GetSubjectByKey(key *keys.PublicKey) ([]stackitem.Item,
return unwrap.Array(c.invoker.Call(c.hash, "getSubjectByKey", key)) return unwrap.Array(c.invoker.Call(c.hash, "getSubjectByKey", key))
} }
// GetSubjectByName invokes `getSubjectByName` method of contract.
func (c *ContractReader) GetSubjectByName(ns string, name string) ([]stackitem.Item, error) {
return unwrap.Array(c.invoker.Call(c.hash, "getSubjectByName", ns, name))
}
// GetSubjectExtended invokes `getSubjectExtended` method of contract. // GetSubjectExtended invokes `getSubjectExtended` method of contract.
func (c *ContractReader) GetSubjectExtended(addr util.Uint160) ([]stackitem.Item, error) { func (c *ContractReader) GetSubjectExtended(addr util.Uint160) ([]stackitem.Item, error) {
return unwrap.Array(c.invoker.Call(c.hash, "getSubjectExtended", addr)) return unwrap.Array(c.invoker.Call(c.hash, "getSubjectExtended", addr))

View file

@ -516,6 +516,27 @@ func TestFrostFSID_Client_UseCaseListNSSubjects(t *testing.T) {
prettyPrintExtendedSubjects(res) prettyPrintExtendedSubjects(res)
} }
func TestFrostFSID_Client_GetSubjectByName(t *testing.T) {
ffsid, cancel := initFrostfsIFClientTest(t)
defer cancel()
key, addr := newKey(t)
subjName := "name"
tx := ffsid.cli.StartTx()
err := tx.WrapCall(ffsid.cli.CreateSubjectCall(defaultNamespace, key.PublicKey()))
require.NoError(t, err)
err = tx.WrapCall(ffsid.cli.SetSubjectNameCall(addr, subjName))
require.NoError(t, err)
ffsid.a.await(ffsid.cli.SendTx(tx))
subj, err := ffsid.cli.GetSubjectByName(defaultNamespace, subjName)
require.NoError(t, err)
require.Equal(t, subjName, subj.Name)
require.True(t, key.PublicKey().Equal(subj.PrimaryKey))
require.Equal(t, defaultNamespace, subj.Namespace)
}
func prettyPrintExtendedSubjects(subjects []*client.SubjectExtended) { func prettyPrintExtendedSubjects(subjects []*client.SubjectExtended) {
for _, subj := range subjects { for _, subj := range subjects {
var sb strings.Builder var sb strings.Builder