2021-07-22 09:52:44 +00:00
|
|
|
package morph
|
|
|
|
|
|
|
|
import (
|
2021-10-07 12:12:15 +00:00
|
|
|
"errors"
|
2021-07-22 09:52:44 +00:00
|
|
|
"fmt"
|
2021-07-28 14:36:15 +00:00
|
|
|
"strconv"
|
2021-07-22 09:52:44 +00:00
|
|
|
|
|
|
|
nns "github.com/nspcc-dev/neo-go/examples/nft-nd-nns"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/native"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/state"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/io"
|
2021-08-06 11:28:07 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/rpc/client"
|
2021-07-22 09:52:44 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/smartcontract"
|
|
|
|
"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"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/vm/emit"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/vm/opcode"
|
2021-10-07 12:12:15 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/vm/stackitem"
|
2021-07-22 09:52:44 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const defaultNameServiceDomainPrice = 10_0000_0000
|
|
|
|
const defaultNameServiceSysfee = 4000_0000
|
|
|
|
const defaultRegisterSysfee = 10_0000_0000 + defaultNameServiceDomainPrice
|
|
|
|
|
|
|
|
func (c *initializeContext) setNNS() error {
|
2021-10-07 12:25:34 +00:00
|
|
|
nnsCs, err := c.readContract(nnsContract)
|
2021-07-22 09:52:44 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-10-07 12:25:34 +00:00
|
|
|
ok, err := c.nnsRootRegistered(nnsCs.Hash)
|
2021-07-22 09:52:44 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
} else if !ok {
|
|
|
|
bw := io.NewBufBinWriter()
|
2021-10-07 12:25:34 +00:00
|
|
|
emit.AppCall(bw.BinWriter, nnsCs.Hash, "addRoot", callflag.All, "neofs")
|
2021-07-22 09:52:44 +00:00
|
|
|
if err := c.sendCommitteeTx(bw.Bytes(), -1); err != nil {
|
|
|
|
return fmt.Errorf("can't add domain root to NNS: %w", err)
|
|
|
|
}
|
|
|
|
if err := c.awaitTx(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-29 13:23:36 +00:00
|
|
|
alphaCs, err := c.readContract(alphabetContract)
|
2021-07-28 14:36:15 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("can't read alphabet contract: %w", err)
|
|
|
|
}
|
2021-07-30 11:26:44 +00:00
|
|
|
for i, acc := range c.Accounts {
|
2021-07-28 14:36:15 +00:00
|
|
|
alphaCs.Hash = state.CreateContractHash(acc.Contract.ScriptHash(), alphaCs.NEF.Checksum, alphaCs.Manifest.Name)
|
|
|
|
|
|
|
|
domain := getAlphabetNNSDomain(i)
|
2021-10-07 12:25:34 +00:00
|
|
|
if err := c.nnsRegisterDomain(nnsCs.Hash, alphaCs.Hash, domain); err != nil {
|
2021-07-28 14:36:15 +00:00
|
|
|
return err
|
|
|
|
}
|
2021-08-10 08:38:48 +00:00
|
|
|
c.Command.Printf("NNS: Set %s -> %s\n", domain, alphaCs.Hash.StringLE())
|
2021-07-28 14:36:15 +00:00
|
|
|
}
|
|
|
|
|
2021-07-22 09:52:44 +00:00
|
|
|
for _, ctrName := range contractList {
|
2021-07-29 13:23:36 +00:00
|
|
|
cs, err := c.readContract(ctrName)
|
2021-07-22 09:52:44 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
domain := ctrName + ".neofs"
|
2021-10-07 12:25:34 +00:00
|
|
|
if err := c.nnsRegisterDomain(nnsCs.Hash, cs.Hash, domain); err != nil {
|
2021-07-22 09:52:44 +00:00
|
|
|
return err
|
|
|
|
}
|
2021-08-10 08:38:48 +00:00
|
|
|
c.Command.Printf("NNS: Set %s -> %s\n", domain, cs.Hash.StringLE())
|
2021-08-06 11:28:07 +00:00
|
|
|
}
|
2021-07-22 09:52:44 +00:00
|
|
|
|
2021-08-06 11:28:07 +00:00
|
|
|
return c.awaitTx()
|
|
|
|
}
|
|
|
|
|
|
|
|
func getAlphabetNNSDomain(i int) string {
|
|
|
|
return alphabetContract + strconv.FormatUint(uint64(i), 10) + ".neofs"
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *initializeContext) nnsRegisterDomain(nnsHash, expectedHash util.Uint160, domain string) error {
|
|
|
|
ok, err := c.Client.NNSIsAvailable(nnsHash, domain)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
bw := io.NewBufBinWriter()
|
|
|
|
if ok {
|
|
|
|
emit.AppCall(bw.BinWriter, nnsHash, "register", callflag.All,
|
2021-10-07 12:12:15 +00:00
|
|
|
domain, c.CommitteeAcc.Contract.ScriptHash(),
|
|
|
|
"ops@nspcc.ru", int64(3600), int64(600), int64(604800), int64(3600))
|
2021-07-22 09:52:44 +00:00
|
|
|
emit.Opcodes(bw.BinWriter, opcode.ASSERT)
|
2021-08-06 11:28:07 +00:00
|
|
|
} else {
|
2021-10-07 12:12:15 +00:00
|
|
|
s, err := nnsResolveHash(c.Client, nnsHash, domain)
|
2021-08-06 11:28:07 +00:00
|
|
|
if err != nil {
|
2021-07-22 09:52:44 +00:00
|
|
|
return err
|
|
|
|
}
|
2021-10-07 12:12:15 +00:00
|
|
|
if s != expectedHash {
|
2021-08-06 11:28:07 +00:00
|
|
|
return nil
|
|
|
|
}
|
2021-07-22 09:52:44 +00:00
|
|
|
}
|
|
|
|
|
2021-10-07 12:12:15 +00:00
|
|
|
emit.AppCall(bw.BinWriter, nnsHash, "addRecord", callflag.All,
|
2021-08-06 11:28:07 +00:00
|
|
|
domain, int64(nns.TXT), expectedHash.StringLE())
|
|
|
|
|
|
|
|
if bw.Err != nil {
|
|
|
|
panic(bw.Err)
|
|
|
|
}
|
|
|
|
|
|
|
|
sysFee := int64(defaultRegisterSysfee + native.GASFactor)
|
|
|
|
return c.sendCommitteeTx(bw.Bytes(), sysFee)
|
2021-07-22 09:52:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *initializeContext) nnsRootRegistered(nnsHash util.Uint160) (bool, error) {
|
|
|
|
params := []smartcontract.Parameter{{Type: smartcontract.StringType, Value: "name.neofs"}}
|
|
|
|
res, err := c.Client.InvokeFunction(nnsHash, "isAvailable", params, nil)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
return res.State == vm.HaltState.String(), nil
|
|
|
|
}
|
|
|
|
|
2021-08-06 11:28:07 +00:00
|
|
|
func nnsResolveHash(c *client.Client, nnsHash util.Uint160, domain string) (util.Uint160, error) {
|
2021-10-07 12:12:15 +00:00
|
|
|
result, err := c.InvokeFunction(nnsHash, "resolve", []smartcontract.Parameter{
|
|
|
|
{
|
|
|
|
Type: smartcontract.StringType,
|
|
|
|
Value: domain,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Type: smartcontract.IntegerType,
|
|
|
|
Value: int64(nns.TXT),
|
|
|
|
},
|
|
|
|
}, nil)
|
2021-08-06 11:28:07 +00:00
|
|
|
if err != nil {
|
2021-10-07 12:12:15 +00:00
|
|
|
return util.Uint160{}, fmt.Errorf("`resolve`: %w", err)
|
2021-08-06 11:28:07 +00:00
|
|
|
}
|
2021-10-07 12:12:15 +00:00
|
|
|
if result.State != vm.HaltState.String() {
|
|
|
|
return util.Uint160{}, fmt.Errorf("invocation failed: %s", result.FaultException)
|
|
|
|
}
|
|
|
|
if len(result.Stack) == 0 {
|
|
|
|
return util.Uint160{}, errors.New("result stack is empty")
|
|
|
|
}
|
|
|
|
arr, ok := result.Stack[len(result.Stack)-1].Value().([]stackitem.Item)
|
|
|
|
if !ok || len(arr) == 0 {
|
|
|
|
return util.Uint160{}, errors.New("malformed response")
|
|
|
|
}
|
|
|
|
bs, err := arr[0].TryBytes()
|
|
|
|
if err != nil {
|
|
|
|
return util.Uint160{}, errors.New("malformed response")
|
|
|
|
}
|
|
|
|
return util.Uint160DecodeStringLE(string(bs))
|
2021-07-28 14:36:15 +00:00
|
|
|
}
|