2021-08-31 12:06:17 +00:00
|
|
|
package client
|
|
|
|
|
|
|
|
import (
|
2021-10-12 10:33:57 +00:00
|
|
|
"errors"
|
2021-08-31 12:06:17 +00:00
|
|
|
"fmt"
|
2022-06-04 12:03:30 +00:00
|
|
|
"math/big"
|
2021-08-31 12:06:17 +00:00
|
|
|
"strconv"
|
|
|
|
|
2023-03-07 13:38:26 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-contract/nns"
|
2022-01-29 12:42:48 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/transaction"
|
2022-01-29 11:15:05 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/crypto/keys"
|
2022-11-16 13:17:12 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/encoding/address"
|
2022-07-28 16:22:32 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/rpcclient"
|
2021-10-12 10:33:57 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/smartcontract"
|
2021-08-31 12:06:17 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/util"
|
2021-10-12 10:33:57 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/vm/stackitem"
|
2022-07-18 08:33:53 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/vm/vmstate"
|
2021-08-31 12:06:17 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2022-04-21 11:28:05 +00:00
|
|
|
nnsContractID = 1 // NNS contract must be deployed first in the sidechain
|
2021-08-31 12:06:17 +00:00
|
|
|
|
|
|
|
// NNSAuditContractName is a name of the audit contract in NNS.
|
2022-12-23 17:35:35 +00:00
|
|
|
NNSAuditContractName = "audit.frostfs"
|
2021-08-31 12:06:17 +00:00
|
|
|
// NNSBalanceContractName is a name of the balance contract in NNS.
|
2022-12-23 17:35:35 +00:00
|
|
|
NNSBalanceContractName = "balance.frostfs"
|
2021-08-31 12:06:17 +00:00
|
|
|
// NNSContainerContractName is a name of the container contract in NNS.
|
2022-12-23 17:35:35 +00:00
|
|
|
NNSContainerContractName = "container.frostfs"
|
2023-01-10 13:07:47 +00:00
|
|
|
// NNSFrostFSIDContractName is a name of the frostfsid contract in NNS.
|
|
|
|
NNSFrostFSIDContractName = "frostfsid.frostfs"
|
2021-08-31 12:06:17 +00:00
|
|
|
// NNSNetmapContractName is a name of the netmap contract in NNS.
|
2022-12-23 17:35:35 +00:00
|
|
|
NNSNetmapContractName = "netmap.frostfs"
|
2021-08-31 12:06:17 +00:00
|
|
|
// NNSProxyContractName is a name of the proxy contract in NNS.
|
2022-12-23 17:35:35 +00:00
|
|
|
NNSProxyContractName = "proxy.frostfs"
|
2021-08-31 12:06:17 +00:00
|
|
|
// NNSReputationContractName is a name of the reputation contract in NNS.
|
2022-12-23 17:35:35 +00:00
|
|
|
NNSReputationContractName = "reputation.frostfs"
|
2021-11-26 14:15:22 +00:00
|
|
|
// NNSSubnetworkContractName is a name of the subnet contract in NNS.
|
2022-12-23 17:35:35 +00:00
|
|
|
NNSSubnetworkContractName = "subnet.frostfs"
|
2023-02-05 15:59:38 +00:00
|
|
|
// NNSGroupKeyName is a name for the FrostFS group key record in NNS.
|
2022-12-23 17:35:35 +00:00
|
|
|
NNSGroupKeyName = "group.frostfs"
|
2021-08-31 12:06:17 +00:00
|
|
|
)
|
|
|
|
|
2021-10-20 13:17:01 +00:00
|
|
|
var (
|
|
|
|
// ErrNNSRecordNotFound means that there is no such record in NNS contract.
|
|
|
|
ErrNNSRecordNotFound = errors.New("record has not been found in NNS contract")
|
|
|
|
|
|
|
|
errEmptyResultStack = errors.New("returned result stack is empty")
|
|
|
|
)
|
|
|
|
|
2021-08-31 12:06:17 +00:00
|
|
|
// NNSAlphabetContractName returns contract name of the alphabet contract in NNS
|
|
|
|
// based on alphabet index.
|
|
|
|
func NNSAlphabetContractName(index int) string {
|
2022-12-23 17:35:35 +00:00
|
|
|
return "alphabet" + strconv.Itoa(index) + ".frostfs"
|
2021-08-31 12:06:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NNSContractAddress returns contract address script hash based on its name
|
|
|
|
// in NNS contract.
|
2021-10-20 13:17:01 +00:00
|
|
|
// If script hash has not been found, returns ErrNNSRecordNotFound.
|
2021-08-31 12:06:17 +00:00
|
|
|
func (c *Client) NNSContractAddress(name string) (sh util.Uint160, err error) {
|
2021-02-09 17:52:10 +00:00
|
|
|
c.switchLock.RLock()
|
|
|
|
defer c.switchLock.RUnlock()
|
|
|
|
|
|
|
|
if c.inactive {
|
|
|
|
return util.Uint160{}, ErrConnectionLost
|
2021-08-31 12:06:17 +00:00
|
|
|
}
|
|
|
|
|
2022-01-29 11:15:05 +00:00
|
|
|
nnsHash, err := c.NNSHash()
|
|
|
|
if err != nil {
|
|
|
|
return util.Uint160{}, err
|
2021-08-31 12:06:17 +00:00
|
|
|
}
|
|
|
|
|
2022-01-29 11:15:05 +00:00
|
|
|
sh, err = nnsResolve(c.client, nnsHash, name)
|
2021-08-31 12:06:17 +00:00
|
|
|
if err != nil {
|
|
|
|
return sh, fmt.Errorf("NNS.resolve: %w", err)
|
|
|
|
}
|
2021-10-12 10:33:57 +00:00
|
|
|
return sh, nil
|
|
|
|
}
|
2021-08-31 12:06:17 +00:00
|
|
|
|
2022-01-29 11:15:05 +00:00
|
|
|
// NNSHash returns NNS contract hash.
|
|
|
|
func (c *Client) NNSHash() (util.Uint160, error) {
|
2021-02-09 17:52:10 +00:00
|
|
|
c.switchLock.RLock()
|
|
|
|
defer c.switchLock.RUnlock()
|
|
|
|
|
|
|
|
if c.inactive {
|
|
|
|
return util.Uint160{}, ErrConnectionLost
|
2022-01-29 11:15:05 +00:00
|
|
|
}
|
|
|
|
|
2022-03-31 13:12:42 +00:00
|
|
|
nnsHash := c.cache.nns()
|
2022-03-15 11:21:24 +00:00
|
|
|
|
2022-03-31 13:12:42 +00:00
|
|
|
if nnsHash == nil {
|
2022-01-29 11:15:05 +00:00
|
|
|
cs, err := c.client.GetContractStateByID(nnsContractID)
|
|
|
|
if err != nil {
|
|
|
|
return util.Uint160{}, fmt.Errorf("NNS contract state: %w", err)
|
|
|
|
}
|
2022-03-31 13:12:42 +00:00
|
|
|
|
|
|
|
c.cache.setNNSHash(cs.Hash)
|
|
|
|
nnsHash = &cs.Hash
|
2022-01-29 11:15:05 +00:00
|
|
|
}
|
2022-03-31 13:12:42 +00:00
|
|
|
return *nnsHash, nil
|
2022-01-29 11:15:05 +00:00
|
|
|
}
|
|
|
|
|
2022-07-28 16:22:32 +00:00
|
|
|
func nnsResolveItem(c *rpcclient.WSClient, nnsHash util.Uint160, domain string) (stackitem.Item, error) {
|
2021-10-20 13:17:01 +00:00
|
|
|
found, err := exists(c, nnsHash, domain)
|
|
|
|
if err != nil {
|
2022-01-29 11:15:05 +00:00
|
|
|
return nil, fmt.Errorf("could not check presence in NNS contract for %s: %w", domain, err)
|
2021-10-20 13:17:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if !found {
|
2022-01-29 11:15:05 +00:00
|
|
|
return nil, ErrNNSRecordNotFound
|
2021-10-20 13:17:01 +00:00
|
|
|
}
|
|
|
|
|
2021-10-12 10:33:57 +00:00
|
|
|
result, err := c.InvokeFunction(nnsHash, "resolve", []smartcontract.Parameter{
|
|
|
|
{
|
|
|
|
Type: smartcontract.StringType,
|
|
|
|
Value: domain,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Type: smartcontract.IntegerType,
|
2022-06-04 12:03:30 +00:00
|
|
|
Value: big.NewInt(int64(nns.TXT)),
|
2021-10-12 10:33:57 +00:00
|
|
|
},
|
|
|
|
}, nil)
|
2021-08-31 12:06:17 +00:00
|
|
|
if err != nil {
|
2022-01-29 11:15:05 +00:00
|
|
|
return nil, err
|
2021-10-12 10:33:57 +00:00
|
|
|
}
|
2022-07-18 08:33:53 +00:00
|
|
|
if result.State != vmstate.Halt.String() {
|
2022-01-29 11:15:05 +00:00
|
|
|
return nil, fmt.Errorf("invocation failed: %s", result.FaultException)
|
2021-10-12 10:33:57 +00:00
|
|
|
}
|
|
|
|
if len(result.Stack) == 0 {
|
2022-01-29 11:15:05 +00:00
|
|
|
return nil, errEmptyResultStack
|
|
|
|
}
|
|
|
|
return result.Stack[0], nil
|
|
|
|
}
|
|
|
|
|
2022-07-28 16:22:32 +00:00
|
|
|
func nnsResolve(c *rpcclient.WSClient, nnsHash util.Uint160, domain string) (util.Uint160, error) {
|
2022-01-29 11:15:05 +00:00
|
|
|
res, err := nnsResolveItem(c, nnsHash, domain)
|
|
|
|
if err != nil {
|
|
|
|
return util.Uint160{}, err
|
2021-08-31 12:06:17 +00:00
|
|
|
}
|
|
|
|
|
2021-10-12 10:33:57 +00:00
|
|
|
// Parse the result of resolving NNS record.
|
|
|
|
// It works with multiple formats (corresponding to multiple NNS versions).
|
|
|
|
// If array of hashes is provided, it returns only the first one.
|
|
|
|
if arr, ok := res.Value().([]stackitem.Item); ok {
|
|
|
|
if len(arr) == 0 {
|
|
|
|
return util.Uint160{}, errors.New("NNS record is missing")
|
|
|
|
}
|
|
|
|
res = arr[0]
|
|
|
|
}
|
|
|
|
bs, err := res.TryBytes()
|
|
|
|
if err != nil {
|
|
|
|
return util.Uint160{}, fmt.Errorf("malformed response: %w", err)
|
|
|
|
}
|
2022-11-16 13:17:12 +00:00
|
|
|
|
|
|
|
// We support several formats for hash encoding, this logic should be maintained in sync
|
2022-12-23 17:35:35 +00:00
|
|
|
// with parseNNSResolveResult from cmd/frostfs-adm/internal/modules/morph/initialize_nns.go
|
2022-11-16 13:17:12 +00:00
|
|
|
h, err := util.Uint160DecodeStringLE(string(bs))
|
|
|
|
if err == nil {
|
|
|
|
return h, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
h, err = address.StringToUint160(string(bs))
|
|
|
|
if err == nil {
|
|
|
|
return h, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return util.Uint160{}, errors.New("no valid hashes are found")
|
2021-08-31 12:06:17 +00:00
|
|
|
}
|
2021-10-20 13:17:01 +00:00
|
|
|
|
2022-07-28 16:22:32 +00:00
|
|
|
func exists(c *rpcclient.WSClient, nnsHash util.Uint160, domain string) (bool, error) {
|
2021-10-20 13:17:01 +00:00
|
|
|
result, err := c.InvokeFunction(nnsHash, "isAvailable", []smartcontract.Parameter{
|
|
|
|
{
|
|
|
|
Type: smartcontract.StringType,
|
|
|
|
Value: domain,
|
|
|
|
},
|
|
|
|
}, nil)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(result.Stack) == 0 {
|
|
|
|
return false, errEmptyResultStack
|
|
|
|
}
|
|
|
|
|
|
|
|
res := result.Stack[0]
|
|
|
|
|
|
|
|
available, err := res.TryBool()
|
|
|
|
if err != nil {
|
|
|
|
return false, fmt.Errorf("malformed response: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// not available means that it is taken
|
|
|
|
// and, therefore, exists
|
|
|
|
return !available, nil
|
|
|
|
}
|
2022-01-29 11:15:05 +00:00
|
|
|
|
2023-02-05 15:59:38 +00:00
|
|
|
// SetGroupSignerScope makes the default signer scope include all FrostFS contracts.
|
2022-01-29 12:42:48 +00:00
|
|
|
// Should be called for side-chain client only.
|
|
|
|
func (c *Client) SetGroupSignerScope() error {
|
2021-02-09 17:52:10 +00:00
|
|
|
c.switchLock.RLock()
|
|
|
|
defer c.switchLock.RUnlock()
|
|
|
|
|
|
|
|
if c.inactive {
|
|
|
|
return ErrConnectionLost
|
2022-01-29 12:42:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub, err := c.contractGroupKey()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-02-17 19:32:31 +00:00
|
|
|
// Don't change c before everything is OK.
|
|
|
|
cfg := c.cfg
|
|
|
|
cfg.signer = &transaction.Signer{
|
2023-02-17 19:27:29 +00:00
|
|
|
Scopes: transaction.CustomGroups | transaction.CalledByEntry,
|
|
|
|
AllowedGroups: []*keys.PublicKey{pub},
|
|
|
|
}
|
2023-02-17 19:32:31 +00:00
|
|
|
rpcActor, err := newActor(c.client, c.acc, cfg)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
c.cfg = cfg
|
|
|
|
c.setActor(rpcActor)
|
2022-01-29 12:42:48 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-02-05 15:59:38 +00:00
|
|
|
// contractGroupKey returns public key designating FrostFS contract group.
|
2022-01-29 12:42:48 +00:00
|
|
|
func (c *Client) contractGroupKey() (*keys.PublicKey, error) {
|
2022-03-31 13:12:42 +00:00
|
|
|
if gKey := c.cache.groupKey(); gKey != nil {
|
|
|
|
return gKey, nil
|
2022-01-29 11:15:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
nnsHash, err := c.NNSHash()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
item, err := nnsResolveItem(c.client, nnsHash, NNSGroupKeyName)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-05-23 11:58:13 +00:00
|
|
|
arr, ok := item.Value().([]stackitem.Item)
|
|
|
|
if !ok || len(arr) == 0 {
|
|
|
|
return nil, errors.New("NNS record is missing")
|
|
|
|
}
|
|
|
|
|
|
|
|
bs, err := arr[0].TryBytes()
|
2022-01-29 11:15:05 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-03-31 13:12:42 +00:00
|
|
|
pub, err := keys.NewPublicKeyFromString(string(bs))
|
2022-01-29 11:15:05 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-03-31 13:12:42 +00:00
|
|
|
c.cache.setGroupKey(pub)
|
2022-01-29 11:15:05 +00:00
|
|
|
return pub, nil
|
|
|
|
}
|