frostfs-contract/frostfsid/client/client.go
Vladimir Domnich f7794b06f6
All checks were successful
DCO action / DCO (pull_request) Successful in 40s
Code generation / Generate wrappers (pull_request) Successful in 1m21s
Tests / Tests (pull_request) Successful in 1m28s
[#120] Add waiter to frostfsid client
Signed-off-by: Vladimir Domnich <v.domnich@yadro.com>
2024-10-21 15:46:22 +03:00

650 lines
22 KiB
Go

package client
import (
"fmt"
"git.frostfs.info/TrueCloudLab/frostfs-contract/commonclient"
"github.com/nspcc-dev/neo-go/pkg/core/state"
"github.com/nspcc-dev/neo-go/pkg/core/transaction"
"github.com/nspcc-dev/neo-go/pkg/crypto/keys"
"github.com/nspcc-dev/neo-go/pkg/io"
"github.com/nspcc-dev/neo-go/pkg/rpcclient/actor"
"github.com/nspcc-dev/neo-go/pkg/rpcclient/notary"
"github.com/nspcc-dev/neo-go/pkg/rpcclient/unwrap"
"github.com/nspcc-dev/neo-go/pkg/rpcclient/waiter"
"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/nspcc-dev/neo-go/pkg/vm/stackitem"
"github.com/nspcc-dev/neo-go/pkg/vm/vmstate"
"github.com/nspcc-dev/neo-go/pkg/wallet"
)
type (
Client struct {
act *actor.Actor
waiter waiter.Waiter
contract util.Uint160
}
Options struct {
ProxyContract util.Uint160
Waiter commonclient.WaiterOptions
}
)
type (
Subject struct {
PrimaryKey *keys.PublicKey
AdditionalKeys keys.PublicKeys
Namespace string
Name string
KV map[string]string
}
SubjectExtended struct {
PrimaryKey *keys.PublicKey
AdditionalKeys keys.PublicKeys
Namespace string
Name string
KV map[string]string
Groups []*Group
}
Namespace struct {
Name string
}
NamespaceExtended struct {
Name string
GroupsCount int64
SubjectsCount int64
}
Group struct {
ID int64
Name string
Namespace string
KV map[string]string
}
GroupExtended struct {
ID int64
Name string
Namespace string
KV map[string]string
SubjectsCount int64
}
)
const (
IAMPathKey = "iam-path"
IAMARNKey = "iam-arn"
IAMCreatedTimeKey = "ctime"
IAMModifiedTimeKey = "mtime"
)
const iteratorBatchSize = 100
const (
getAdminMethod = "getAdmin"
setAdminMethod = "setAdmin"
clearAdminMethod = "clearAdmin"
versionMethod = "version"
createSubjectMethod = "createSubject"
getSubjectMethod = "getSubject"
getSubjectExtendedMethod = "getSubjectExtended"
listSubjectsMethod = "listSubjects"
addSubjectKeyMethod = "addSubjectKey"
removeSubjectKeyMethod = "removeSubjectKey"
getSubjectByKeyMethod = "getSubjectByKey"
getSubjectByNameMethod = "getSubjectByName"
getSubjectKeyByNameMethod = "getSubjectKeyByName"
setSubjectKVMethod = "setSubjectKV"
setSubjectNameMethod = "setSubjectName"
deleteSubjectKVMethod = "deleteSubjectKV"
deleteSubjectMethod = "deleteSubject"
createNamespaceMethod = "createNamespace"
getNamespaceMethod = "getNamespace"
getNamespaceExtendedMethod = "getNamespaceExtended"
listNamespacesMethod = "listNamespaces"
listNamespaceSubjectsMethod = "listNamespaceSubjects"
createGroupMethod = "createGroup"
getGroupMethod = "getGroup"
getGroupExtendedMethod = "getGroupExtended"
getGroupIDByNameMethod = "getGroupIDByName"
getGroupByNameMethod = "getGroupByName"
setGroupNameMethod = "setGroupName"
setGroupKVMethod = "setGroupKV"
deleteGroupKVMethod = "deleteGroupKV"
listGroupsMethod = "listGroups"
addSubjectToGroupMethod = "addSubjectToGroup"
removeSubjectFromGroupMethod = "removeSubjectFromGroup"
listGroupSubjectsMethod = "listGroupSubjects"
deleteGroupMethod = "deleteGroup"
)
// New creates a new Client. Options can be empty.
func New(ra actor.RPCActor, acc *wallet.Account, contract util.Uint160, opt Options) (*Client, error) {
signers := []actor.SignerAccount{{
Signer: transaction.Signer{
Account: acc.Contract.ScriptHash(),
Scopes: transaction.CalledByEntry,
},
Account: acc,
}}
if !opt.ProxyContract.Equals(util.Uint160{}) {
signers = append([]actor.SignerAccount{{
Signer: transaction.Signer{
Account: opt.ProxyContract,
Scopes: transaction.CustomContracts,
AllowedContracts: []util.Uint160{contract},
},
Account: notary.FakeContractAccount(opt.ProxyContract),
}}, signers...)
}
act, err := actor.New(ra, signers)
if err != nil {
return nil, fmt.Errorf("init actor: %w", err)
}
waiter := commonclient.NewWaiter(act, opt.Waiter)
return &Client{
act: act,
waiter: waiter,
contract: contract,
}, nil
}
// NewSimple creates a new Client using existing actor.Actor and default waiter options.
func NewSimple(act *actor.Actor, contract util.Uint160) *Client {
waiter := commonclient.NewWaiter(act, commonclient.WaiterOptions{})
return &Client{
act: act,
waiter: waiter,
contract: contract,
}
}
// StartTx inits transaction.
func (c Client) StartTx() *commonclient.Transaction {
return commonclient.NewTransaction(c.contract)
}
// SendTx sends provided transaction to blockchain.
func (c Client) SendTx(txn *commonclient.Transaction) (tx util.Uint256, vub uint32, err error) {
txBytes, err := txn.Bytes()
if err != nil {
return util.Uint256{}, 0, err
}
return c.act.SendRun(txBytes)
}
// Version returns version of contract.
func (c Client) Version() (int64, error) {
return unwrap.Int64(c.act.Call(c.contract, versionMethod))
}
// SetAdmin sets address that can perform write operations on contract.
// Must be invoked by committee.
func (c Client) SetAdmin(owner util.Uint160) (tx util.Uint256, vub uint32, err error) {
method, args := c.SetAdminCall(owner)
return c.act.SendCall(c.contract, method, args...)
}
// SetAdminCall provides args for SetAdmin to use in commonclient.Transaction.
func (c Client) SetAdminCall(owner util.Uint160) (method string, args []any) {
return setAdminMethod, []any{owner}
}
// ClearAdmin removes address that can perform write operations on contract.
// Must be invoked by committee.
func (c Client) ClearAdmin() (tx util.Uint256, vub uint32, err error) {
method, args := c.ClearAdminCall()
return c.act.SendCall(c.contract, method, args...)
}
// ClearAdminCall provides args for ClearAdmin to use in commonclient.Transaction.
func (c Client) ClearAdminCall() (method string, args []any) {
return clearAdminMethod, nil
}
// GetAdmin returns address that can perform write operations on contract.
// Second return values is true iff admin is set.
func (c Client) GetAdmin() (util.Uint160, bool, error) {
item, err := unwrap.Item(c.act.Call(c.contract, getAdminMethod))
if err != nil {
return util.Uint160{}, false, err
}
if item.Value() == nil {
return util.Uint160{}, false, nil
}
bs, err := item.TryBytes()
if err != nil {
return util.Uint160{}, true, err
}
u, err := util.Uint160DecodeBytesBE(bs)
return u, true, err
}
// CreateSubject creates new subject using public key and namespace.
// Must be invoked by contract owner.
func (c Client) CreateSubject(ns string, key *keys.PublicKey) (tx util.Uint256, vub uint32, err error) {
method, args := c.CreateSubjectCall(ns, key)
return c.act.SendCall(c.contract, method, args...)
}
// CreateSubjectCall provides args for CreateSubject to use in commonclient.Transaction.
func (c Client) CreateSubjectCall(ns string, key *keys.PublicKey) (method string, args []any) {
return createSubjectMethod, []any{ns, key.Bytes()}
}
// GetSubject gets subject by address.
func (c Client) GetSubject(addr util.Uint160) (*Subject, error) {
items, err := unwrap.Array(c.act.Call(c.contract, getSubjectMethod, addr))
if err != nil {
return nil, err
}
return ParseSubject(items)
}
// GetSubjectExtended gets extended subject by address.
func (c Client) GetSubjectExtended(addr util.Uint160) (*SubjectExtended, error) {
items, err := unwrap.Array(c.act.Call(c.contract, getSubjectExtendedMethod, addr))
if err != nil {
return nil, err
}
return ParseSubjectExtended(items)
}
// ListSubjects gets all subjects.
func (c Client) ListSubjects() ([]util.Uint160, error) {
return UnwrapArrayOfUint160(commonclient.ReadIteratorItems(c.act, iteratorBatchSize, c.contract, listSubjectsMethod))
}
// ListFullSubjects gets list of subjects.
func (c Client) ListFullSubjects(hashes []util.Uint160) ([]*Subject, error) {
w := io.NewBufBinWriter()
for _, hash := range hashes {
emit.AppCall(w.BinWriter, c.contract, getSubjectMethod, callflag.All, hash)
}
invoker, err := c.act.Run(w.Bytes())
if err != nil {
return nil, err
}
if invoker.State != vmstate.Halt.String() {
return nil, fmt.Errorf("invocation failed: %s", invoker.FaultException)
}
subjects := make([]*Subject, 0, len(invoker.Stack))
for _, item := range invoker.Stack {
arr, ok := item.Value().([]stackitem.Item)
if !ok {
return nil, fmt.Errorf("invalid subject")
}
subject, err := ParseSubject(arr)
if err != nil {
return nil, err
}
subjects = append(subjects, subject)
}
return subjects, nil
}
// AddSubjectKey adds extra public key to subject.
// Must be invoked by contract owner.
func (c Client) AddSubjectKey(addr util.Uint160, key *keys.PublicKey) (tx util.Uint256, vub uint32, err error) {
method, args := c.AddSubjectKeyCall(addr, key)
return c.act.SendCall(c.contract, method, args...)
}
// AddSubjectKeyCall provides args for AddSubjectKey to use in commonclient.Transaction.
func (c Client) AddSubjectKeyCall(addr util.Uint160, key *keys.PublicKey) (method string, args []any) {
return addSubjectKeyMethod, []any{addr, key.Bytes()}
}
// RemoveSubjectKey removes extra public key from subject.
// Must be invoked by contract owner.
func (c Client) RemoveSubjectKey(addr util.Uint160, key *keys.PublicKey) (tx util.Uint256, vub uint32, err error) {
method, args := c.RemoveSubjectKeyCall(addr, key)
return c.act.SendCall(c.contract, method, args...)
}
// RemoveSubjectKeyCall provides args for RemoveSubjectKey to use in commonclient.Transaction.
func (c Client) RemoveSubjectKeyCall(addr util.Uint160, key *keys.PublicKey) (method string, args []any) {
return removeSubjectKeyMethod, []any{addr, key.Bytes()}
}
// SetSubjectKV updates subject kv map.
// Must be invoked by contract owner.
// You can use some predefined key constants: IAMPathKey, IAMARNKey, IAMCreatedTimeKey, IAMModifiedTimeKey.
func (c Client) SetSubjectKV(addr util.Uint160, key, val string) (tx util.Uint256, vub uint32, err error) {
method, args := c.SetSubjectKVCall(addr, key, val)
return c.act.SendCall(c.contract, method, args...)
}
// SetSubjectKVCall provides args for SetSubjectKV to use in commonclient.Transaction.
func (c Client) SetSubjectKVCall(addr util.Uint160, key, val string) (method string, args []any) {
return setSubjectKVMethod, []any{addr, key, val}
}
// SetSubjectName updates subject name.
// Must be invoked by contract owner.
func (c Client) SetSubjectName(addr util.Uint160, name string) (tx util.Uint256, vub uint32, err error) {
method, args := c.SetSubjectNameCall(addr, name)
return c.act.SendCall(c.contract, method, args...)
}
// SetSubjectNameCall provides args for SetSubjectName to use in commonclient.Transaction.
func (c Client) SetSubjectNameCall(addr util.Uint160, name string) (method string, args []any) {
return setSubjectNameMethod, []any{addr, name}
}
// DeleteSubjectKV removes subject kv map.
// Must be invoked by contract owner.
func (c Client) DeleteSubjectKV(addr util.Uint160, key string) (tx util.Uint256, vub uint32, err error) {
method, args := c.DeleteSubjectKVCall(addr, key)
return c.act.SendCall(c.contract, method, args...)
}
// DeleteSubjectKVCall provides args for DeleteSubjectKV to use in commonclient.Transaction.
func (c Client) DeleteSubjectKVCall(addr util.Uint160, key string) (method string, args []any) {
return deleteSubjectKVMethod, []any{addr, key}
}
// GetSubjectByKey gets subject by its primary or additional keys.
func (c Client) GetSubjectByKey(key *keys.PublicKey) (*Subject, error) {
items, err := unwrap.Array(c.act.Call(c.contract, getSubjectByKeyMethod, key.Bytes()))
if err != nil {
return nil, err
}
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).
func (c Client) GetSubjectKeyByName(namespace, subjectName string) (*keys.PublicKey, error) {
return unwrap.PublicKey(c.act.Call(c.contract, getSubjectKeyByNameMethod, namespace, subjectName))
}
// DeleteSubject delete subject and removes it from related namespaces and groups.
// Must be invoked by contract owner.
func (c Client) DeleteSubject(addr util.Uint160) (tx util.Uint256, vub uint32, err error) {
method, args := c.DeleteSubjectCall(addr)
return c.act.SendCall(c.contract, method, args...)
}
// DeleteSubjectCall provides args for DeleteSubject to use in commonclient.Transaction.
func (c Client) DeleteSubjectCall(addr util.Uint160) (method string, args []any) {
return deleteSubjectMethod, []any{addr}
}
// CreateNamespace create new namespace.
// Must be invoked by contract owner.
func (c Client) CreateNamespace(namespace string) (tx util.Uint256, vub uint32, err error) {
method, args := c.CreateNamespaceCall(namespace)
return c.act.SendCall(c.contract, method, args...)
}
// CreateNamespaceCall provides args for CreateNamespace to use in commonclient.Transaction.
func (c Client) CreateNamespaceCall(namespace string) (method string, args []any) {
return createNamespaceMethod, []any{namespace}
}
// GetNamespace gets namespace.
func (c Client) GetNamespace(namespace string) (*Namespace, error) {
items, err := unwrap.Array(c.act.Call(c.contract, getNamespaceMethod, namespace))
if err != nil {
return nil, err
}
return ParseNamespace(items)
}
// GetNamespaceExtended gets extended namespace.
func (c Client) GetNamespaceExtended(namespace string) (*NamespaceExtended, error) {
items, err := unwrap.Array(c.act.Call(c.contract, getNamespaceExtendedMethod, namespace))
if err != nil {
return nil, err
}
return ParseNamespaceExtended(items)
}
// ListNamespaces gets all namespaces.
func (c Client) ListNamespaces() ([]*Namespace, error) {
items, err := commonclient.ReadIteratorItems(c.act, iteratorBatchSize, c.contract, listNamespacesMethod)
if err != nil {
return nil, err
}
return ParseNamespaces(items)
}
// ListNamespaceSubjects gets all subjects from namespace.
func (c Client) ListNamespaceSubjects(namespace string) ([]util.Uint160, error) {
return UnwrapArrayOfUint160(commonclient.ReadIteratorItems(c.act, iteratorBatchSize, c.contract, listNamespaceSubjectsMethod, namespace))
}
// CreateGroup creates a new group in specific namespace.
// Must be invoked by contract owner.
func (c Client) CreateGroup(namespace, group string) (tx util.Uint256, vub uint32, err error) {
method, args := c.CreateGroupCall(namespace, group)
return c.act.SendCall(c.contract, method, args...)
}
// CreateGroupCall provides args for CreateGroup to use in commonclient.Transaction.
func (c Client) CreateGroupCall(namespace, group string) (method string, args []any) {
return createGroupMethod, []any{namespace, group}
}
// GetGroup gets group.
func (c Client) GetGroup(namespace string, groupID int64) (*Group, error) {
items, err := unwrap.Array(c.act.Call(c.contract, getGroupMethod, namespace, groupID))
if err != nil {
return nil, err
}
return ParseGroup(items)
}
// GetGroupExtended gets extended group.
func (c Client) GetGroupExtended(namespace string, groupID int64) (*GroupExtended, error) {
items, err := unwrap.Array(c.act.Call(c.contract, getGroupExtendedMethod, namespace, groupID))
if err != nil {
return nil, err
}
return ParseGroupExtended(items)
}
// SetGroupName updates subject name.
// Must be invoked by contract owner.
func (c Client) SetGroupName(namespace string, groupID int64, name string) (tx util.Uint256, vub uint32, err error) {
method, args := c.SetGroupNameCall(namespace, groupID, name)
return c.act.SendCall(c.contract, method, args...)
}
// SetGroupNameCall provides args for SetGroupName to use in commonclient.Transaction.
func (c Client) SetGroupNameCall(namespace string, groupID int64, name string) (method string, args []any) {
return setGroupNameMethod, []any{namespace, groupID, name}
}
// SetGroupKV updates group kv map.
// Must be invoked by contract owner.
// You can use some predefined key constants: IAMPathKey, IAMARNKey, IAMCreatedTimeKey, IAMModifiedTimeKey.
func (c Client) SetGroupKV(namespace string, groupID int64, key, val string) (tx util.Uint256, vub uint32, err error) {
method, args := c.SetGroupKVCall(namespace, groupID, key, val)
return c.act.SendCall(c.contract, method, args...)
}
// SetGroupKVCall provides args for SetGroupKV to use in commonclient.Transaction.
func (c Client) SetGroupKVCall(namespace string, groupID int64, key, val string) (method string, args []any) {
return setGroupKVMethod, []any{namespace, groupID, key, val}
}
// DeleteGroupKV removes group kv map.
// Must be invoked by contract owner.
func (c Client) DeleteGroupKV(namespace string, groupID int64, key string) (tx util.Uint256, vub uint32, err error) {
method, args := c.DeleteGroupKVCall(namespace, groupID, key)
return c.act.SendCall(c.contract, method, args...)
}
// DeleteGroupKVCall provides args for DeleteGroupKV to use in commonclient.Transaction.
func (c Client) DeleteGroupKVCall(namespace string, groupID int64, key string) (method string, args []any) {
return deleteGroupKVMethod, []any{namespace, groupID, key}
}
// GetGroupIDByName gets group id its name (namespace scope).
func (c Client) GetGroupIDByName(namespace, groupName string) (int64, error) {
return unwrap.Int64(c.act.Call(c.contract, getGroupIDByNameMethod, namespace, groupName))
}
// GetGroupByName gets group by its name (namespace scope).
func (c Client) GetGroupByName(namespace, groupName string) (*Group, error) {
items, err := unwrap.Array(c.act.Call(c.contract, getGroupByNameMethod, namespace, groupName))
if err != nil {
return nil, err
}
return ParseGroup(items)
}
// ListGroups gets all groups in specific namespace.
func (c Client) ListGroups(namespace string) ([]*Group, error) {
items, err := commonclient.ReadIteratorItems(c.act, iteratorBatchSize, c.contract, listGroupsMethod, namespace)
if err != nil {
return nil, err
}
return ParseGroups(items)
}
// AddSubjectToGroup adds a new subject to group.
// Must be invoked by contract owner.
func (c Client) AddSubjectToGroup(addr util.Uint160, groupID int64) (tx util.Uint256, vub uint32, err error) {
method, args := c.AddSubjectToGroupCall(addr, groupID)
return c.act.SendCall(c.contract, method, args...)
}
// AddSubjectToGroupCall provides args for AddSubjectToGroup to use in commonclient.Transaction.
func (c Client) AddSubjectToGroupCall(addr util.Uint160, groupID int64) (method string, args []any) {
return addSubjectToGroupMethod, []any{addr, groupID}
}
// RemoveSubjectFromGroup removes subject from group.
// Must be invoked by contract owner.
func (c Client) RemoveSubjectFromGroup(addr util.Uint160, groupID int64) (tx util.Uint256, vub uint32, err error) {
method, args := c.RemoveSubjectFromGroupCall(addr, groupID)
return c.act.SendCall(c.contract, method, args...)
}
// RemoveSubjectFromGroupCall provides args for RemoveSubjectFromGroup to use in commonclient.Transaction.
func (c Client) RemoveSubjectFromGroupCall(addr util.Uint160, groupID int64) (method string, args []any) {
return removeSubjectFromGroupMethod, []any{addr, groupID}
}
// ListGroupSubjects gets all subjects in specific group.
func (c Client) ListGroupSubjects(namespace string, groupID int64) ([]util.Uint160, error) {
return UnwrapArrayOfUint160(commonclient.ReadIteratorItems(c.act, iteratorBatchSize, c.contract, listGroupSubjectsMethod, namespace, groupID))
}
// DeleteGroup deletes group.
// Must be invoked by contract owner.
func (c Client) DeleteGroup(namespace string, groupID int64) (tx util.Uint256, vub uint32, err error) {
method, args := c.DeleteGroupCall(namespace, groupID)
return c.act.SendCall(c.contract, method, args...)
}
// DeleteGroupCall provides args for DeleteGroup to use in commonclient.Transaction.
func (c Client) DeleteGroupCall(namespace string, groupID int64) (method string, args []any) {
return deleteGroupMethod, []any{namespace, groupID}
}
// ListNonEmptyNamespaces gets namespaces that contain at least one subject.
func (c Client) ListNonEmptyNamespaces() ([]string, error) {
namespaces, err := c.ListNamespaces()
if err != nil {
return nil, err
}
var res []string
for _, namespace := range namespaces {
nsExt, err := c.GetNamespaceExtended(namespace.Name)
if err != nil {
return nil, err
}
if nsExt.SubjectsCount > 0 {
res = append(res, nsExt.Name)
}
}
return res, nil
}
// Wait waits until the specified transaction is accepted to the chain.
func (c Client) Wait(tx util.Uint256, vub uint32, err error) (*state.AppExecResult, error) {
return c.Waiter().Wait(tx, vub, err)
}
// Waiter returns underlying waiter.Waiter.
func (c Client) Waiter() waiter.Waiter {
return c.waiter
}
// ParseGroupID fetch groupID from stack after creating group method invocation.
func (c Client) ParseGroupID(res *state.AppExecResult, err error) (int64, error) {
if err != nil {
return 0, err
}
return unwrap.Int64(makeResFromAppExec(res))
}
// ListNonEmptyGroups gets groups that contain at least one subject.
func (c Client) ListNonEmptyGroups(namespace string) ([]string, error) {
groups, err := c.ListGroups(namespace)
if err != nil {
return nil, err
}
var res []string
for _, group := range groups {
groupExt, err := c.GetGroupExtended(namespace, group.ID)
if err != nil {
return nil, err
}
if groupExt.SubjectsCount > 0 {
res = append(res, groupExt.Name)
}
}
return res, nil
}