From 95fe7781d5ee33c61109175b20e5ad6201b38c1a Mon Sep 17 00:00:00 2001 From: Denis Kirillov Date: Tue, 7 Nov 2023 13:58:16 +0300 Subject: [PATCH] [#48] frostfsid: Add user-friendly client Signed-off-by: Denis Kirillov --- frostfsid/client/client.go | 776 +++++++++++++++++++++++++++++++++++++ go.mod | 11 +- go.sum | 23 +- tests/util.go | 205 ++++++++++ 4 files changed, 999 insertions(+), 16 deletions(-) create mode 100644 frostfsid/client/client.go diff --git a/frostfsid/client/client.go b/frostfsid/client/client.go new file mode 100644 index 0000000..e92fa33 --- /dev/null +++ b/frostfsid/client/client.go @@ -0,0 +1,776 @@ +package client + +import ( + "errors" + "fmt" + + "git.frostfs.info/TrueCloudLab/frostfs-contract/commonclient" + "github.com/nspcc-dev/neo-go/pkg/core/state" + "github.com/nspcc-dev/neo-go/pkg/crypto/keys" + "github.com/nspcc-dev/neo-go/pkg/neorpc/result" + "github.com/nspcc-dev/neo-go/pkg/rpcclient/actor" + "github.com/nspcc-dev/neo-go/pkg/rpcclient/unwrap" + "github.com/nspcc-dev/neo-go/pkg/util" + "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 + acc *wallet.Account + contract util.Uint160 + } + + Options struct { + // todo add proxy params + } +) + +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 { + Name string + Namespace string + } + + GroupExtended struct { + Name string + Namespace string + SubjectsCount int64 + } +) + +const ( + SubjectIAMPathKey = "iam-path" + SubjectCreatedTimeKey = "ctime" + SubjectModifiedTimeKey = "mtime" +) + +const iteratorBatchSize = 100 + +const ( + addOwnerMethod = "addOwner" + deleteOwnerMethod = "deleteOwner" + listOwnersMethod = "listOwners" + + versionMethod = "version" + + createSubjectMethod = "createSubject" + getSubjectMethod = "getSubject" + getSubjectExtendedMethod = "getSubjectExtended" + listSubjectsMethod = "listSubjects" + addSubjectKeyMethod = "addSubjectKey" + removeSubjectKeyMethod = "removeSubjectKey" + getSubjectByKeyMethod = "getSubjectByKey" + getSubjectKeyByNameMethod = "getSubjectKeyByName" + setSubjectKVMethod = "setSubjectKV" + setSubjectNameMethod = "setSubjectName" + deleteSubjectKVMethod = "deleteSubjectKV" + deleteSubjectMethod = "deleteSubject" + + createNamespaceMethod = "createNamespace" + getNamespaceMethod = "getNamespace" + getNamespaceExtendedMethod = "getNamespaceExtended" + listNamespacesMethod = "listNamespaces" + addSubjectToNamespaceMethod = "addSubjectToNamespace" + removeSubjectFromNamespaceMethod = "removeSubjectFromNamespace" + listNamespaceSubjectsMethod = "listNamespaceSubjects" + + createGroupMethod = "createGroup" + getGroupMethod = "getGroup" + getGroupExtendedMethod = "getGroupExtended" + listGroupsMethod = "listGroups" + addSubjectToGroupMethod = "addSubjectToGroup" + removeSubjectFromGroupMethod = "removeSubjectFromGroup" + listGroupSubjectsMethod = "listGroupSubjects" + deleteGroupMethod = "deleteGroup" +) + +// New creates a new Client. Options can be nil. +func New(ra actor.RPCActor, acc *wallet.Account, contract util.Uint160, _ *Options) (*Client, error) { + act, err := actor.NewSimple(ra, acc) + if err != nil { + return nil, fmt.Errorf("init actor: %w", err) + } + + return &Client{ + act: act, + acc: acc, + contract: contract, + }, nil +} + +// 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)) +} + +// AddOwner adds new address that can perform write operations on contract. +// Must be invoked by committee. +func (c Client) AddOwner(owner util.Uint160) (tx util.Uint256, vub uint32, err error) { + method, args := c.AddOwnerCall(owner) + return c.act.SendCall(c.contract, method, args...) +} + +// AddOwnerCall provides args for AddOwner to use in commonclient.Transaction. +func (c Client) AddOwnerCall(owner util.Uint160) (method string, args []any) { + return addOwnerMethod, []any{owner} +} + +// DeleteOwner removes address from list of that can perform write operations on contract. +// Must be invoked by committee. +func (c Client) DeleteOwner(owner util.Uint160) (tx util.Uint256, vub uint32, err error) { + method, args := c.DeleteOwnerCall(owner) + return c.act.SendCall(c.contract, method, args...) +} + +// DeleteOwnerCall provides args for DeleteOwner to use in commonclient.Transaction. +func (c Client) DeleteOwnerCall(owner util.Uint160) (method string, args []any) { + return deleteOwnerMethod, []any{owner} +} + +// ListOwners returns list of address that can perform write operations on contract. +func (c Client) ListOwners() ([]util.Uint160, error) { + return unwrapArrayOfUint160(commonclient.ReadIteratorItems(c.act, iteratorBatchSize, c.contract, listOwnersMethod)) +} + +// CreateSubject creates new subject using public key. +// Must be invoked by contract owner. +func (c Client) CreateSubject(key *keys.PublicKey) (tx util.Uint256, vub uint32, err error) { + method, args := c.CreateSubjectCall(key) + return c.act.SendCall(c.contract, method, args...) +} + +// CreateSubjectCall provides args for CreateSubject to use in commonclient.Transaction. +func (c Client) CreateSubjectCall(key *keys.PublicKey) (method string, args []any) { + return createSubjectMethod, []any{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)) +} + +// 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: SubjectLoginKey, SubjectIAMPathKey, SubjectCreatedTimeKey, SubjectModifiedTimeKey. +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 SetSubjectLogin 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 SetSubjectLogin 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 SetSubjectLogin 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) +} + +// 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) +} + +// AddSubjectToNamespace adds a subject to namespace. +// Must be invoked by contract owner. +func (c Client) AddSubjectToNamespace(addr util.Uint160, namespace string) (tx util.Uint256, vub uint32, err error) { + method, args := c.AddSubjectToNamespaceCall(addr, namespace) + return c.act.SendCall(c.contract, method, args...) +} + +// AddSubjectToNamespaceCall provides args for AddSubjectToNamespace to use in commonclient.Transaction. +func (c Client) AddSubjectToNamespaceCall(addr util.Uint160, namespace string) (method string, args []any) { + return addSubjectToNamespaceMethod, []any{addr, namespace} +} + +// RemoveSubjectFromNamespace removes a subject from namespace. +// Must be invoked by contract owner. +func (c Client) RemoveSubjectFromNamespace(addr util.Uint160) (tx util.Uint256, vub uint32, err error) { + method, args := c.RemoveSubjectFromNamespaceCall(addr) + return c.act.SendCall(c.contract, method, args...) +} + +// RemoveSubjectFromNamespaceCall provides args for RemoveSubjectFromNamespace to use in commonclient.Transaction. +func (c Client) RemoveSubjectFromNamespaceCall(addr util.Uint160) (method string, args []any) { + return removeSubjectFromNamespaceMethod, []any{addr} +} + +// 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, group string) (*Group, error) { + items, err := unwrap.Array(c.act.Call(c.contract, getGroupMethod, namespace, group)) + if err != nil { + return nil, err + } + + return parseGroup(items) +} + +// GetGroupExtended gets extended group. +func (c Client) GetGroupExtended(namespace, group string) (*GroupExtended, error) { + items, err := unwrap.Array(c.act.Call(c.contract, getGroupExtendedMethod, namespace, group)) + if err != nil { + return nil, err + } + + return parseGroupExtended(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, group string) (tx util.Uint256, vub uint32, err error) { + method, args := c.AddSubjectToGroupCall(addr, group) + 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, group string) (method string, args []any) { + return addSubjectToGroupMethod, []any{addr, group} +} + +// RemoveSubjectFromGroup removes subject from group. +// Must be invoked by contract owner. +func (c Client) RemoveSubjectFromGroup(addr util.Uint160, namespace, group string) (tx util.Uint256, vub uint32, err error) { + method, args := c.RemoveSubjectFromGroupCall(addr, namespace, group) + 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, namespace, group string) (method string, args []any) { + return removeSubjectFromGroupMethod, []any{addr, namespace, group} +} + +// ListGroupSubjects gets all subjects in specific group. +func (c Client) ListGroupSubjects(namespace, group string) ([]util.Uint160, error) { + return unwrapArrayOfUint160(commonclient.ReadIteratorItems(c.act, iteratorBatchSize, c.contract, listGroupSubjectsMethod, namespace, group)) +} + +// DeleteGroup deletes group. +// Must be invoked by contract owner. +func (c Client) DeleteGroup(namespace, group string) (tx util.Uint256, vub uint32, err error) { + method, args := c.DeleteGroupCall(namespace, group) + return c.act.SendCall(c.contract, method, args...) +} + +// DeleteGroupCall provides args for DeleteGroup to use in commonclient.Transaction. +func (c Client) DeleteGroupCall(namespace, group string) (method string, args []any) { + return deleteGroupMethod, []any{namespace, group} +} + +// 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 invokes underlying wait method on actor.Actor. +func (c Client) Wait(tx util.Uint256, vub uint32, err error) (*state.AppExecResult, error) { + return c.act.Wait(tx, vub, err) +} + +// 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.Name) + if err != nil { + return nil, err + } + + if groupExt.SubjectsCount > 0 { + res = append(res, groupExt.Name) + } + } + + return res, nil +} + +func unwrapArrayOfUint160(items []stackitem.Item, err error) ([]util.Uint160, error) { + if err != nil { + return nil, err + } + + return unwrap.ArrayOfUint160(makeValidRes(stackitem.NewArray(items))) +} + +func makeValidRes(item stackitem.Item) (*result.Invoke, error) { + return &result.Invoke{ + Stack: []stackitem.Item{item}, + State: vmstate.Halt.String(), + }, nil +} + +func parseSubject(structArr []stackitem.Item) (*Subject, error) { + if len(structArr) < 5 { + return nil, errors.New("invalid response subject struct") + } + + var ( + err error + subj Subject + ) + + subj.PrimaryKey, err = unwrap.PublicKey(makeValidRes(structArr[0])) + if err != nil { + return nil, err + } + + if !structArr[1].Equals(stackitem.Null{}) { + subj.AdditionalKeys, err = unwrap.ArrayOfPublicKeys(makeValidRes(structArr[1])) + if err != nil { + return nil, err + } + } + + if !structArr[2].Equals(stackitem.Null{}) { + subj.Namespace, err = unwrap.UTF8String(makeValidRes(structArr[2])) + if err != nil { + return nil, err + } + } + + if !structArr[2].Equals(stackitem.Null{}) { + subj.Name, err = unwrap.UTF8String(makeValidRes(structArr[3])) + if err != nil { + return nil, err + } + } + + subj.KV, err = parseMap(structArr[4]) + if err != nil { + return nil, err + } + + return &subj, nil +} + +func parseSubjectExtended(structArr []stackitem.Item) (*SubjectExtended, error) { + if len(structArr) < 6 { + return nil, errors.New("invalid response subject extended struct") + } + + var ( + err error + subj SubjectExtended + ) + + subj.PrimaryKey, err = unwrap.PublicKey(makeValidRes(structArr[0])) + if err != nil { + return nil, err + } + + if !structArr[1].Equals(stackitem.Null{}) { + subj.AdditionalKeys, err = unwrap.ArrayOfPublicKeys(makeValidRes(structArr[1])) + if err != nil { + return nil, err + } + } + + nsBytes, err := structArr[2].TryBytes() + if err != nil { + return nil, err + } + subj.Namespace = string(nsBytes) + + nameBytes, err := structArr[3].TryBytes() + if err != nil { + return nil, err + } + subj.Name = string(nameBytes) + + subj.KV, err = parseMap(structArr[4]) + if err != nil { + return nil, err + } + + if !structArr[5].Equals(stackitem.Null{}) { + groupItems, ok := structArr[5].Value().([]stackitem.Item) + if !ok { + return nil, fmt.Errorf("invalid groups field") + } + + subj.Groups, err = parseGroups(groupItems) + if err != nil { + return nil, err + } + } + + return &subj, nil +} + +func parseMap(item stackitem.Item) (map[string]string, error) { + if item.Equals(stackitem.Null{}) { + return nil, nil + } + + metaMap, err := unwrap.Map(makeValidRes(item)) + if err != nil { + return nil, err + } + + meta, ok := metaMap.Value().([]stackitem.MapElement) + if !ok { + return nil, errors.New("invalid map type") + } + + res := make(map[string]string, len(meta)) + for _, element := range meta { + key, err := element.Key.TryBytes() + if err != nil { + return nil, err + } + val, err := element.Value.TryBytes() + if err != nil { + return nil, err + } + res[string(key)] = string(val) + } + + return res, nil +} + +func parseNamespace(structArr []stackitem.Item) (*Namespace, error) { + if len(structArr) < 1 { + return nil, errors.New("invalid response namespace struct") + } + + name, err := structArr[0].TryBytes() + if err != nil { + return nil, err + } + + return &Namespace{Name: string(name)}, nil +} + +func parseNamespaceExtended(structArr []stackitem.Item) (*NamespaceExtended, error) { + if len(structArr) < 3 { + return nil, errors.New("invalid response namespace extended struct") + } + + name, err := structArr[0].TryBytes() + if err != nil { + return nil, err + } + + groupCount, err := structArr[1].TryInteger() + if err != nil { + return nil, err + } + + subjectCount, err := structArr[2].TryInteger() + if err != nil { + return nil, err + } + + return &NamespaceExtended{ + Name: string(name), + GroupsCount: groupCount.Int64(), + SubjectsCount: subjectCount.Int64(), + }, nil +} + +func parseNamespaces(items []stackitem.Item) ([]*Namespace, error) { + var err error + res := make([]*Namespace, len(items)) + + for i := 0; i < len(items); i++ { + arr, ok := items[i].Value().([]stackitem.Item) + if !ok { + return nil, fmt.Errorf("invalid namespace type") + } + res[i], err = parseNamespace(arr) + if err != nil { + return nil, err + } + } + + return res, nil +} + +func parseGroup(structArr []stackitem.Item) (*Group, error) { + if len(structArr) < 2 { + return nil, errors.New("invalid response group struct") + } + + name, err := structArr[0].TryBytes() + if err != nil { + return nil, err + } + + namespace, err := structArr[1].TryBytes() + if err != nil { + return nil, err + } + + return &Group{ + Name: string(name), + Namespace: string(namespace), + }, nil +} + +func parseGroupExtended(structArr []stackitem.Item) (*GroupExtended, error) { + if len(structArr) < 3 { + return nil, errors.New("invalid response group extended struct") + } + + name, err := structArr[0].TryBytes() + if err != nil { + return nil, err + } + + namespace, err := structArr[1].TryBytes() + if err != nil { + return nil, err + } + + subjectCount, err := structArr[2].TryInteger() + if err != nil { + return nil, err + } + + return &GroupExtended{ + Name: string(name), + Namespace: string(namespace), + SubjectsCount: subjectCount.Int64(), + }, nil +} + +func parseGroups(items []stackitem.Item) ([]*Group, error) { + var err error + res := make([]*Group, len(items)) + + for i := 0; i < len(items); i++ { + arr, ok := items[i].Value().([]stackitem.Item) + if !ok { + return nil, fmt.Errorf("invalid group type") + } + res[i], err = parseGroup(arr) + if err != nil { + return nil, err + } + } + + return res, nil +} diff --git a/go.mod b/go.mod index a674370..fc4ad2e 100644 --- a/go.mod +++ b/go.mod @@ -8,10 +8,10 @@ require ( github.com/nspcc-dev/neo-go v0.103.0 github.com/nspcc-dev/neo-go/pkg/interop v0.0.0-20231020160724-c3955f87d1b5 github.com/stretchr/testify v1.8.4 + go.uber.org/zap v1.26.0 ) require ( - github.com/benbjohnson/clock v1.1.0 // indirect github.com/beorn7/perks v1.0.1 // indirect github.com/bits-and-blooms/bitset v1.8.0 // indirect github.com/cespare/xxhash/v2 v2.2.0 // indirect @@ -28,8 +28,12 @@ require ( github.com/holiman/uint256 v1.2.0 // indirect github.com/matttproud/golang_protobuf_extensions v1.0.1 // indirect github.com/mmcloughlin/addchain v0.4.0 // indirect + github.com/nspcc-dev/dbft v0.0.0-20230515113611-25db6ba61d5c // indirect github.com/nspcc-dev/go-ordered-json v0.0.0-20220111165707-25110be27d22 // indirect + github.com/nspcc-dev/neofs-crypto v0.4.0 // indirect github.com/nspcc-dev/rfc6979 v0.2.0 // indirect + github.com/pierrec/lz4 v2.6.1+incompatible // indirect + github.com/pkg/errors v0.9.1 // indirect github.com/pmezard/go-difflib v1.0.0 // indirect github.com/prometheus/client_golang v1.13.0 // indirect github.com/prometheus/client_model v0.2.0 // indirect @@ -42,9 +46,7 @@ require ( github.com/twmb/murmur3 v1.1.5 // indirect github.com/urfave/cli v1.22.5 // indirect go.etcd.io/bbolt v1.3.7 // indirect - go.uber.org/atomic v1.10.0 // indirect - go.uber.org/multierr v1.9.0 // indirect - go.uber.org/zap v1.24.0 // indirect + go.uber.org/multierr v1.10.0 // indirect golang.org/x/crypto v0.14.0 // indirect golang.org/x/mod v0.12.0 // indirect golang.org/x/sync v0.3.0 // indirect @@ -52,7 +54,6 @@ require ( golang.org/x/term v0.13.0 // indirect golang.org/x/text v0.13.0 // indirect golang.org/x/tools v0.12.1-0.20230815132531-74c255bcf846 // indirect - google.golang.org/genproto/googleapis/rpc v0.0.0-20230803162519-f966b187b2e5 // indirect google.golang.org/protobuf v1.31.0 // indirect gopkg.in/yaml.v3 v3.0.1 // indirect rsc.io/tmplfunc v0.0.3 // indirect diff --git a/go.sum b/go.sum index 3e8a56f..d5eccca 100644 --- a/go.sum +++ b/go.sum @@ -39,8 +39,6 @@ github.com/alecthomas/units v0.0.0-20151022065526-2efee857e7cf/go.mod h1:ybxpYRF github.com/alecthomas/units v0.0.0-20190717042225-c3de453c63f4/go.mod h1:ybxpYRFXyAe+OPACYpWeL0wqObRcbAqCMya13uyzqw0= github.com/alecthomas/units v0.0.0-20190924025748-f65c72e2690d/go.mod h1:rBZYJk541a8SKzHPHnH3zbiI+7dagKZ0cgpgrD7Fyho= github.com/antlr/antlr4/runtime/Go/antlr/v4 v4.0.0-20221202181307-76fa05c21b12 h1:npHgfD4Tl2WJS3AJaMUi5ynGDPUBfkg3U3fCzDyXZ+4= -github.com/benbjohnson/clock v1.1.0 h1:Q92kusRqC1XV2MjkWETPvjJVqKetz1OzxZB7mHJLju8= -github.com/benbjohnson/clock v1.1.0/go.mod h1:J11/hYXuz8f4ySSvYwY0FKfm+ezbsZBKZxNJlLklBHA= github.com/beorn7/perks v0.0.0-20180321164747-3a771d992973/go.mod h1:Dwedo/Wpr24TaqPxmxbtue+5NUziq4I4S80YR8gNf3Q= github.com/beorn7/perks v1.0.0/go.mod h1:KWe93zE9D1o94FZ5RNwFwVgaQK1VOXiVxmqh+CedLV8= github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM= @@ -74,6 +72,7 @@ github.com/envoyproxy/go-control-plane v0.9.0/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymF github.com/envoyproxy/go-control-plane v0.9.1-0.20191026205805-5f8ba28d4473/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4= github.com/envoyproxy/go-control-plane v0.9.4/go.mod h1:6rpuAdCZL397s3pYoYcLgu1mIlRU8Am5FuJP05cCM98= github.com/envoyproxy/protoc-gen-validate v0.1.0/go.mod h1:iSmxcyjqTsJpI2R4NaDN7+kN2VEUnK/pcBlmesArF7c= +github.com/frankban/quicktest v1.14.5 h1:dfYrrRyLtiqT9GyKXgdh+k4inNeTvmGbuSgZ3lx3GhA= github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo= github.com/fsnotify/fsnotify v1.4.9 h1:hsms1Qyu0jgnwNXIxa+/V/PDsU6CfLf6CNO8H7IWoS4= github.com/fsnotify/fsnotify v1.4.9/go.mod h1:znqG4EE+3YCdAaPaxE2ZRY/06pZUdp0tY4IgpuI1SZQ= @@ -177,8 +176,8 @@ github.com/kr/logfmt v0.0.0-20140226030751-b84e30acd515/go.mod h1:+0opPa2QZZtGFB github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo= github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE= github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= -github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE= github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= +github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= github.com/leanovate/gopter v0.2.9 h1:fQjYxZaynp97ozCzfOyOuAGOU4aU/z37zf/tOujFk7c= github.com/matttproud/golang_protobuf_extensions v1.0.1 h1:4hp9jkHxhMHkqkrB3Ix0jegS5sx/RkqARlsWZ6pIwiU= github.com/matttproud/golang_protobuf_extensions v1.0.1/go.mod h1:D8He9yQNgCq6Z5Ld7szi9bcBfOoFv/3dc6xSMkL2PC0= @@ -195,6 +194,7 @@ github.com/mr-tron/base58 v1.2.0/go.mod h1:BinMc/sQntlIE1frQmRFPUoPA1Zkr8VRgBdjW github.com/mwitkow/go-conntrack v0.0.0-20161129095857-cc309e4a2223/go.mod h1:qRWi+5nqEBWmkhHvq77mSJWrCKwh8bxhgT7d/eI7P4U= github.com/mwitkow/go-conntrack v0.0.0-20190716064945-2f068394615f/go.mod h1:qRWi+5nqEBWmkhHvq77mSJWrCKwh8bxhgT7d/eI7P4U= github.com/nspcc-dev/dbft v0.0.0-20230515113611-25db6ba61d5c h1:uyK5aLbAhrnZtnvobJLN24gGUrlxIJAAFqiWl+liZuo= +github.com/nspcc-dev/dbft v0.0.0-20230515113611-25db6ba61d5c/go.mod h1:kjBC9F8L25GR+kIHy/1KgG/KfcoGnVwIiyovgq1uszk= github.com/nspcc-dev/go-ordered-json v0.0.0-20220111165707-25110be27d22 h1:n4ZaFCKt1pQJd7PXoMJabZWK9ejjbLOVrkl/lOUmshg= github.com/nspcc-dev/go-ordered-json v0.0.0-20220111165707-25110be27d22/go.mod h1:79bEUDEviBHJMFV6Iq6in57FEOCMcRhfQnfaf0ETA5U= github.com/nspcc-dev/hrw v1.0.9 h1:17VcAuTtrstmFppBjfRiia4K2wA/ukXZhLFS8Y8rz5Y= @@ -204,6 +204,7 @@ github.com/nspcc-dev/neo-go/pkg/interop v0.0.0-20231020160724-c3955f87d1b5 h1:09 github.com/nspcc-dev/neo-go/pkg/interop v0.0.0-20231020160724-c3955f87d1b5/go.mod h1:J/Mk6+nKeKSW4wygkZQFLQ6SkLOSGX5Ga0RuuuktEag= github.com/nspcc-dev/neofs-api-go/v2 v2.14.0 h1:jhuN8Ldqz7WApvUJRFY0bjRXE1R3iCkboMX5QVZhHVk= github.com/nspcc-dev/neofs-crypto v0.4.0 h1:5LlrUAM5O0k1+sH/sktBtrgfWtq1pgpDs09fZo+KYi4= +github.com/nspcc-dev/neofs-crypto v0.4.0/go.mod h1:6XJ8kbXgOfevbI2WMruOtI+qUJXNwSGM/E9eClXxPHs= github.com/nspcc-dev/neofs-sdk-go v1.0.0-rc.11 h1:QOc8ZRN5DXlAeRPh5QG9u8rMLgoeRNiZF5/vL7QupWg= 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= @@ -218,6 +219,7 @@ github.com/onsi/gomega v1.7.1/go.mod h1:XdKZgCCFLUoM/7CFJVPcG8C1xQ1AJ0vpAezJrB7J github.com/onsi/gomega v1.10.1 h1:o0+MgICZLuZ7xjH7Vx6zS/zcu93/BEp1VwkIW1mEXCE= github.com/onsi/gomega v1.10.1/go.mod h1:iN09h71vgCQne3DLsj+A5owkum+a2tYe+TOCB1ybHNo= github.com/pierrec/lz4 v2.6.1+incompatible h1:9UY3+iC23yxF0UfGaYrGplQ+79Rg+h/q9FV9ix19jjM= +github.com/pierrec/lz4 v2.6.1+incompatible/go.mod h1:pdkljMzZIN41W+lC3N2tnIh5sFi+IEE17M5jbnwPHcY= github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= @@ -265,6 +267,7 @@ github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+ github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= +github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk= github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= github.com/syndtr/goleveldb v1.0.1-0.20210305035536-64b5b1c73954 h1:xQdMZ1WLrgkkvOZ/LDQxjVxMLdby7osSh4ZEVa5sIjs= @@ -285,13 +288,11 @@ go.opencensus.io v0.22.0/go.mod h1:+kGneAE2xo2IficOXnaByMWTGM9T73dGwxeWcUqIpI8= go.opencensus.io v0.22.2/go.mod h1:yxeiOL68Rb0Xd1ddK5vPZ/oVn4vY4Ynel7k9FzqtOIw= go.opencensus.io v0.22.3/go.mod h1:yxeiOL68Rb0Xd1ddK5vPZ/oVn4vY4Ynel7k9FzqtOIw= go.opencensus.io v0.22.4/go.mod h1:yxeiOL68Rb0Xd1ddK5vPZ/oVn4vY4Ynel7k9FzqtOIw= -go.uber.org/atomic v1.10.0 h1:9qC72Qh0+3MqyJbAn8YU5xVq1frD8bn3JtD2oXtafVQ= -go.uber.org/atomic v1.10.0/go.mod h1:LUxbIzbOniOlMKjJjyPfpl4v+PKK2cNJn91OQbhoJI0= -go.uber.org/goleak v1.1.11 h1:wy28qYRKZgnJTxGxvye5/wgWr1EKjmUDGYox5mGlRlI= -go.uber.org/multierr v1.9.0 h1:7fIwc/ZtS0q++VgcfqFDxSBZVv/Xo49/SYnDFupUwlI= -go.uber.org/multierr v1.9.0/go.mod h1:X2jQV1h+kxSjClGpnseKVIxpmcjrj7MNnI0bnlfKTVQ= -go.uber.org/zap v1.24.0 h1:FiJd5l1UOLj0wCgbSE0rwwXHzEdAZS6hiiSnxJN/D60= -go.uber.org/zap v1.24.0/go.mod h1:2kMP+WWQ8aoFoedH3T2sq6iJ2yDWpHbP0f6MQbS9Gkg= +go.uber.org/goleak v1.2.0 h1:xqgm/S+aQvhWFTtR0XK3Jvg7z8kGV8P4X14IzwN3Eqk= +go.uber.org/multierr v1.10.0 h1:S0h4aNzvfcFsC3dRF1jLoaov7oRaKqRGC/pUEJ2yvPQ= +go.uber.org/multierr v1.10.0/go.mod h1:20+QtiLqy0Nd6FdQB9TLXag12DsQkrbs3htMFfDN80Y= +go.uber.org/zap v1.26.0 h1:sI7k6L95XOKS281NhVKOFCUNIvv9e0w4BF8N3u+tCRo= +go.uber.org/zap v1.26.0/go.mod h1:dtElttAiwGvoJ/vj4IwHBS/gXsEu/pZ50mUIRWuG0so= golang.org/x/crypto v0.0.0-20180904163835-0709b304e793/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= golang.org/x/crypto v0.0.0-20190510104115-cbcb75029529/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= @@ -548,7 +549,6 @@ google.golang.org/genproto v0.0.0-20200729003335-053ba62fc06f/go.mod h1:FWY/as6D google.golang.org/genproto v0.0.0-20200804131852-c06518451d9c/go.mod h1:FWY/as6DDZQgahTzZj3fqbO1CbirC29ZNUFHwi0/+no= google.golang.org/genproto v0.0.0-20200825200019-8632dd797987/go.mod h1:FWY/as6DDZQgahTzZj3fqbO1CbirC29ZNUFHwi0/+no= google.golang.org/genproto/googleapis/rpc v0.0.0-20230803162519-f966b187b2e5 h1:eSaPbMR4T7WfH9FvABk36NBMacoTUKdWCvV0dx+KfOg= -google.golang.org/genproto/googleapis/rpc v0.0.0-20230803162519-f966b187b2e5/go.mod h1:zBEcrKX2ZOcEkHWxBPAIvYUWOKKMIhYcmNiUIu2ji3I= google.golang.org/grpc v1.19.0/go.mod h1:mqu4LbDTu4XGKhr4mRzUsmM4RtVoemTSY81AxZiDr8c= google.golang.org/grpc v1.20.1/go.mod h1:10oTOabMzJvdu6/UiuZezV6QK5dSlG84ov/aaiqXj38= google.golang.org/grpc v1.21.1/go.mod h1:oYelfM1adQP15Ek0mdvEgi9Df8B9CZIaU1084ijfRaM= @@ -592,6 +592,7 @@ gopkg.in/yaml.v2 v2.2.5/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.3.0/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY= gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4= diff --git a/tests/util.go b/tests/util.go index 48f5f5d..ed7c119 100644 --- a/tests/util.go +++ b/tests/util.go @@ -1,14 +1,76 @@ package tests import ( + "context" + "net/http" + "os" "testing" + "time" + "github.com/nspcc-dev/neo-go/pkg/config" + "github.com/nspcc-dev/neo-go/pkg/consensus" + "github.com/nspcc-dev/neo-go/pkg/core" "github.com/nspcc-dev/neo-go/pkg/core/interop/storage" + "github.com/nspcc-dev/neo-go/pkg/core/state" + corestate "github.com/nspcc-dev/neo-go/pkg/core/stateroot" + "github.com/nspcc-dev/neo-go/pkg/core/storage/dbconfig" "github.com/nspcc-dev/neo-go/pkg/neotest" "github.com/nspcc-dev/neo-go/pkg/neotest/chain" + "github.com/nspcc-dev/neo-go/pkg/network" + "github.com/nspcc-dev/neo-go/pkg/rpcclient" + "github.com/nspcc-dev/neo-go/pkg/services/rpcsrv" + "github.com/nspcc-dev/neo-go/pkg/services/stateroot" + "github.com/nspcc-dev/neo-go/pkg/smartcontract/trigger" + "github.com/nspcc-dev/neo-go/pkg/util" "github.com/nspcc-dev/neo-go/pkg/vm/stackitem" + "github.com/stretchr/testify/require" + "go.uber.org/zap" ) +type awaiter struct { + ctx context.Context + t *testing.T + rpc *rpcclient.Client +} + +func (a awaiter) await(tx util.Uint256, vub uint32, err error) *state.AppExecResult { + require.NoError(a.t, err) + return await(a.ctx, a.t, a.rpc, tx, vub) +} + +const nodeWallet = ` +{ + "version": "3.0", + "accounts": [ + { + "address": "Nhfg3TbpwogLvDGVvAvqyThbsHgoSUKwtn", + "key": "6PYM8VdX2BSm7BSXKzV4Fz6S3R9cDLLWNrD9nMjxW352jEv3fsC8N3wNLY", + "label": "", + "contract": { + "script": "DCECs2Ir9AF73+MXxYrtX0x1PyBrfbiWBG+n13S7xL9/jcJBVuezJw==", + "parameters": [ + { + "name": "parameter0", + "type": "Signature" + } + ], + "deployed": false + }, + "lock": false, + "isdefault": false + } + ], + "scrypt": { + "n": 16384, + "r": 8, + "p": 8 + }, + "extra": { + "Tokens": null + } +} +` + func iteratorToArray(iter *storage.Iterator) []stackitem.Item { stackItems := make([]stackitem.Item, 0) for iter.Next() { @@ -21,3 +83,146 @@ func newExecutor(t *testing.T) *neotest.Executor { bc, acc := chain.NewSingle(t) return neotest.NewExecutor(t, bc, acc, acc) } + +func initTmpWallet(t *testing.T) string { + f, err := os.CreateTemp("", "tmp-neo-go-wallet") + require.NoError(t, err) + + _, err = f.WriteString(nodeWallet) + require.NoError(t, err) + + err = f.Close() + require.NoError(t, err) + return f.Name() +} + +func await(ctx context.Context, t *testing.T, rpc *rpcclient.Client, tx util.Uint256, vub uint32) *state.AppExecResult { + waitCtx, waitCancel := context.WithTimeout(ctx, 5*time.Second) + defer waitCancel() + + for { + select { + case <-waitCtx.Done(): + require.NoError(t, waitCtx.Err()) + return nil + default: + bc, err := rpc.GetBlockCount() + require.NoError(t, err) + + tr := trigger.Application + if log, err := rpc.GetApplicationLog(tx, &tr); err == nil { + return &state.AppExecResult{ + Container: log.Container, + Execution: log.Executions[0], + } + } + + require.LessOrEqual(t, bc, vub, "vub is expired") + time.Sleep(100 * time.Millisecond) + } + } +} + +func neogoCfg() config.Config { + return config.Config{ + ProtocolConfiguration: config.ProtocolConfiguration{}, + ApplicationConfiguration: config.ApplicationConfiguration{ + RPC: config.RPC{ + BasicService: config.BasicService{ + Enabled: true, + // See how tests are done in the neo-go itself: + // https://github.com/nspcc-dev/neo-go/blob/5fc61be5f6c5349d8de8b61967380feee6b51c55/config/protocol.unit_testnet.single.yml#L61 + Addresses: []string{"localhost:0"}, + }, + MaxGasInvoke: 200_000_000, + SessionEnabled: true, + MaxIteratorResultItems: 100, + SessionPoolSize: 20, + SessionExpirationTime: 15, + }, + DBConfiguration: dbconfig.DBConfiguration{ + Type: dbconfig.InMemoryDB, + }, + }, + } +} + +func consensusCfg(chain *core.Blockchain, walletAddress string, srv *network.Server) consensus.Config { + return consensus.Config{ + Logger: zap.NewExample(), + Broadcast: srv.BroadcastExtensible, + BlockQueue: srv.GetBlockQueue(), + Chain: chain, + ProtocolConfiguration: chain.GetConfig().ProtocolConfiguration, + RequestTx: srv.RequestTx, + StopTxFlow: srv.StopTxFlow, + Wallet: config.Wallet{ + Path: walletAddress, + Password: "one", + }, + TimePerBlock: 100 * time.Millisecond, + } +} + +func runRPC(ctx context.Context, t *testing.T, chain *core.Blockchain, walletPath string) string { + log := zap.NewExample() + cfg := neogoCfg() + + srvCfg, err := network.NewServerConfig(cfg) + require.NoError(t, err) + srv, err := network.NewServer(srvCfg, chain, chain.GetStateSyncModule(), log) + require.NoError(t, err) + + t.Cleanup(srv.Shutdown) + + errCh := make(chan error) + go func() { + for err := range errCh { + require.NoError(t, err) + return + } + }() + + srMod := chain.GetStateModule().(*corestate.Module) + sr, err := stateroot.New(srvCfg.StateRootCfg, srMod, log, chain, srv.BroadcastExtensible) + require.NoError(t, err) + srv.AddExtensibleService(sr, stateroot.Category, sr.OnPayload) + + consens, err := consensus.NewService(consensusCfg(chain, walletPath, srv)) + require.NoError(t, err) + srv.AddConsensusService(consens, consens.OnPayload, consens.OnTransaction) + + rpcSrv := rpcsrv.New(chain, cfg.ApplicationConfiguration.RPC, srv, nil, log, errCh) + srv.AddService(&rpcSrv) + + initialAddr := rpcSrv.Addresses()[0] + + go srv.Start() + + // wait until RPC server is started + startTimeout, cancel := context.WithTimeout(ctx, 5*time.Second) + defer cancel() + + ticker := time.NewTicker(time.Millisecond * 100) + defer ticker.Stop() + + var actualAddr string + for { + select { + case <-startTimeout.Done(): + t.Fatalf("Waiting for server start: %v", startTimeout.Err()) + case <-ticker.C: + if actualAddr == "" { + newAddr := rpcSrv.Addresses()[0] + if initialAddr == newAddr { + continue + } + actualAddr = newAddr + t.Logf("RPC server is listening at %s, checking health", actualAddr) + } + if _, err = http.Get("http://" + actualAddr); err == nil { + return actualAddr + } + } + } +}