Usage of actor, rpc client and waiter for async methods

This commit is contained in:
Marina Biryukova 2023-07-20 16:14:51 +03:00
parent 08f91efd08
commit e58d56e46c
3 changed files with 68 additions and 27 deletions

View file

@ -1,8 +1,12 @@
package main
import (
"context"
"git.frostfs.info/TrueCloudLab/nns-connection/ns"
"github.com/nspcc-dev/neo-go/pkg/encoding/address"
"github.com/nspcc-dev/neo-go/pkg/rpcclient"
"github.com/nspcc-dev/neo-go/pkg/rpcclient/actor"
"github.com/nspcc-dev/neo-go/pkg/wallet"
)
@ -20,14 +24,32 @@ func main() {
acc := w.Accounts[1]
acc.Decrypt("one", w.Scrypt)
domain := "domain.frostfs"
cli, err := rpcclient.New(context.Background(), "http://localhost:30333", rpcclient.Options{})
if err != nil {
panic(err.Error())
}
cli.Init()
act, err := actor.NewSimple(cli, acc)
if err != nil {
panic(err.Error())
}
// Registering new top-level domain
domain := "newroot"
owner, _ := address.StringToUint160(acc.Address)
ok, err := nns.Register(domain, owner, "my.email@yadro.com", 100, 100, 100, 100)
ok, err := nns.Register(cli, act, domain, owner, "my.email@yadro.com", 100, 100, 100, 100)
if !ok {
println(err.Error())
}
err = nns.AddTXTRecord(domain, "my-record")
// Registering new second-level domain
domain = "domain.newroot"
ok, err = nns.Register(cli, act, domain, owner, "my.email@yadro.com", 100, 100, 100, 100)
if !ok {
println(err.Error())
}
err = nns.AddTXTRecord(cli, act, domain, "my-record")
if err != nil {
println(err.Error())
}
@ -41,8 +63,17 @@ func main() {
}
}
err = nns.DeleteTXTRecords(domain)
err = nns.DeleteTXTRecords(cli, act, domain)
if err != nil {
println(err.Error())
}
res, err = nns.GetTXTRecords(domain)
if err != nil {
println(err.Error())
} else {
for _, record := range res {
println(record)
}
}
}

2
go.mod
View file

@ -21,7 +21,7 @@ require (
github.com/hashicorp/golang-lru v0.6.0 // indirect
github.com/mr-tron/base58 v1.2.0 // indirect
github.com/nspcc-dev/go-ordered-json v0.0.0-20220111165707-25110be27d22 // indirect
github.com/nspcc-dev/neo-go/pkg/interop v0.0.0-20230615193820-9185820289ce
github.com/nspcc-dev/neo-go/pkg/interop v0.0.0-20230615193820-9185820289ce // indirect
github.com/nspcc-dev/rfc6979 v0.2.0 // indirect
github.com/twmb/murmur3 v1.1.8 // indirect
go.uber.org/atomic v1.10.0 // indirect

View file

@ -13,6 +13,7 @@ import (
"github.com/nspcc-dev/neo-go/pkg/encoding/address"
"github.com/nspcc-dev/neo-go/pkg/neorpc/result"
"github.com/nspcc-dev/neo-go/pkg/rpcclient"
"github.com/nspcc-dev/neo-go/pkg/rpcclient/actor"
"github.com/nspcc-dev/neo-go/pkg/rpcclient/invoker"
"github.com/nspcc-dev/neo-go/pkg/rpcclient/unwrap"
"github.com/nspcc-dev/neo-go/pkg/util"
@ -161,6 +162,7 @@ func (n *NNS) ResolveContractHash(domain container.Domain) (util.Uint160, error)
return util.Uint160{}, errNotFound
}
// Version returns version of NNS contract by calling `version` method of this contract.
func (n *NNS) Version() (int64, error) {
item, err := unwrap.Item(n.invoker.Call(n.nnsContract, "version"))
if err != nil {
@ -178,6 +180,7 @@ func (n *NNS) Version() (int64, error) {
return 0, errNotFound
}
// Symbol returns symbol of NNS contract by calling `symbol` method of this contract.
func (n *NNS) Symbol() (string, error) {
item, err := unwrap.Item(n.invoker.Call(n.nnsContract, "symbol"))
if err != nil {
@ -196,14 +199,20 @@ func (n *NNS) Symbol() (string, error) {
return "", errNotFound
}
func (n *NNS) Register(name string, owner util.Uint160, email string, refresh, retry, expire, ttl int) (bool, error) {
item, err := unwrap.Item(n.invoker.Call(n.nnsContract, "register", name, owner, email, refresh, retry, expire, ttl))
// Register registers a new domain with the specified owner and name by calling `register` method of NNS contract.
func (n *NNS) Register(cli *rpcclient.Client, act *actor.Actor, name string, owner util.Uint160, email string,
refresh, retry, expire, ttl int) (bool, error) {
waiter, err := actor.NewPollingWaiter(cli)
if err != nil {
return false, fmt.Errorf("waiter creation: %w", err)
}
execRes, err := waiter.Wait(act.SendCall(n.nnsContract, "register", name, owner, email, refresh, retry, expire, ttl))
if err != nil {
return false, fmt.Errorf("contract invocation: %w", err)
}
if _, ok := item.(stackitem.Null); !ok {
recordValue, err := item.TryBool()
if len(execRes.Stack) != 0 {
recordValue, err := execRes.Stack[0].TryBool()
if err != nil {
return false, fmt.Errorf("convert stack item to bool: %w", err)
}
@ -214,17 +223,22 @@ func (n *NNS) Register(name string, owner util.Uint160, email string, refresh, r
return false, errNotFound
}
func (n *NNS) AddTXTRecord(name string, data string) error {
item, err := unwrap.Item(n.invoker.Call(n.nnsContract, "addRecord", name, int64(nns.TXT), data))
// AddTXTRecord adds a new TXT record with the specified data to the provided domain by calling `addRecord` method
// of NNS contract.
func (n *NNS) AddTXTRecord(cli *rpcclient.Client, act *actor.Actor, name string, data string) error {
waiter, err := actor.NewPollingWaiter(cli)
if err != nil {
return fmt.Errorf("waiter creation: %w", err)
}
_, err = waiter.Wait(act.SendCall(n.nnsContract, "addRecord", name, int64(nns.TXT), data))
if err != nil {
return fmt.Errorf("contract invocation: %w", err)
}
println(item.Type())
return nil
}
// GetTXTRecords returns TXT records of the provided domain by calling `getRecords` method of NNS contract.
func (n *NNS) GetTXTRecords(name string) ([]string, error) {
item, err := unwrap.Item(n.invoker.Call(n.nnsContract, "getRecords", name, int64(nns.TXT)))
if err != nil {
@ -238,7 +252,7 @@ func (n *NNS) GetTXTRecords(name string) ([]string, error) {
return make([]string, 0), errors.New("invalid cast to stack item slice")
}
var result = make([]string, len(arr))
var result = make([]string, 0, len(arr))
for i := range arr {
recordValue, err := arr[i].TryBytes()
if err != nil {
@ -252,26 +266,21 @@ func (n *NNS) GetTXTRecords(name string) ([]string, error) {
return make([]string, 0), errNotFound
}
func (n *NNS) DeleteTXTRecords(name string) error {
item, err := unwrap.Item(n.invoker.Call(n.nnsContract, "deleteRecords", name, int64(nns.TXT)))
// DeleteTXTRecords removes TXT records from the provided domain by calling `deleteRecords` method of NNS contract.
func (n *NNS) DeleteTXTRecords(cli *rpcclient.Client, act *actor.Actor, name string) error {
waiter, err := actor.NewPollingWaiter(cli)
if err != nil {
return fmt.Errorf("waiter creation: %w", err)
}
_, err = waiter.Wait(act.SendCall(n.nnsContract, "deleteRecords", name, int64(nns.TXT)))
if err != nil {
return fmt.Errorf("contract invocation: %w", err)
}
println(item.Type())
return nil
}
func (n *NNS) SetAdmin(name string, admin util.Uint160) error {
item, err := unwrap.Item(n.invoker.Call(n.nnsContract, "setAdmin", name, admin))
if err != nil {
return fmt.Errorf("contract invocation: %w", err)
}
println(item.Type())
return nil
}
// IsAvailable checks whether the provided domain name is available by calling `isAvailable` method of NNS contract.
func (n *NNS) IsAvailable(name string) (bool, error) {
item, err := unwrap.Item(n.invoker.Call(n.nnsContract, "isAvailable", name))
if err != nil {
@ -290,6 +299,7 @@ func (n *NNS) IsAvailable(name string) (bool, error) {
return false, errNotFound
}
// OwnerOf returns the owner of the provided domain by calling `ownerOf` method of NNS contract.
func (n *NNS) OwnerOf(name []byte) ([]byte, error) {
item, err := unwrap.Item(n.invoker.Call(n.nnsContract, "ownerOf", name))
if err != nil {