forked from TrueCloudLab/frostfs-node
261 lines
8.4 KiB
Go
261 lines
8.4 KiB
Go
package morph
|
|
|
|
import (
|
|
"encoding/hex"
|
|
"errors"
|
|
"fmt"
|
|
"strconv"
|
|
"time"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-contract/nns"
|
|
morphUtil "git.frostfs.info/TrueCloudLab/frostfs-node/cmd/frostfs-adm/internal/modules/morph/util"
|
|
morphClient "git.frostfs.info/TrueCloudLab/frostfs-node/pkg/morph/client"
|
|
"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/encoding/address"
|
|
"github.com/nspcc-dev/neo-go/pkg/io"
|
|
"github.com/nspcc-dev/neo-go/pkg/rpcclient"
|
|
"github.com/nspcc-dev/neo-go/pkg/rpcclient/invoker"
|
|
"github.com/nspcc-dev/neo-go/pkg/rpcclient/management"
|
|
nnsClient "github.com/nspcc-dev/neo-go/pkg/rpcclient/nns"
|
|
"github.com/nspcc-dev/neo-go/pkg/rpcclient/unwrap"
|
|
"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/opcode"
|
|
"github.com/nspcc-dev/neo-go/pkg/vm/stackitem"
|
|
"github.com/nspcc-dev/neo-go/pkg/vm/vmstate"
|
|
)
|
|
|
|
const defaultExpirationTime = 10 * 365 * 24 * time.Hour / time.Second
|
|
|
|
const frostfsOpsEmail = "ops@frostfs.info"
|
|
|
|
func (c *initializeContext) setNNS() error {
|
|
r := management.NewReader(c.ReadOnlyInvoker)
|
|
nnsCs, err := r.GetContractByID(1)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
ok, err := c.nnsRootRegistered(nnsCs.Hash, "frostfs")
|
|
if err != nil {
|
|
return err
|
|
} else if !ok {
|
|
bw := io.NewBufBinWriter()
|
|
emit.AppCall(bw.BinWriter, nnsCs.Hash, "register", callflag.All,
|
|
"frostfs", c.CommitteeAcc.Contract.ScriptHash(),
|
|
frostfsOpsEmail, int64(3600), int64(600), int64(defaultExpirationTime), int64(3600))
|
|
emit.Opcodes(bw.BinWriter, opcode.ASSERT)
|
|
if err := c.sendCommitteeTx(bw.Bytes(), true); err != nil {
|
|
return fmt.Errorf("can't add domain root to NNS: %w", err)
|
|
}
|
|
if err := c.awaitTx(); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
|
|
alphaCs := c.getContract(morphUtil.AlphabetContract)
|
|
for i, acc := range c.Accounts {
|
|
alphaCs.Hash = state.CreateContractHash(acc.Contract.ScriptHash(), alphaCs.NEF.Checksum, alphaCs.Manifest.Name)
|
|
|
|
domain := getAlphabetNNSDomain(i)
|
|
if err := c.nnsRegisterDomain(nnsCs.Hash, alphaCs.Hash, domain); err != nil {
|
|
return err
|
|
}
|
|
c.Command.Printf("NNS: Set %s -> %s\n", domain, alphaCs.Hash.StringLE())
|
|
}
|
|
|
|
for _, ctrName := range contractList {
|
|
cs := c.getContract(ctrName)
|
|
|
|
domain := ctrName + ".frostfs"
|
|
if err := c.nnsRegisterDomain(nnsCs.Hash, cs.Hash, domain); err != nil {
|
|
return err
|
|
}
|
|
c.Command.Printf("NNS: Set %s -> %s\n", domain, cs.Hash.StringLE())
|
|
}
|
|
|
|
groupKey := c.ContractWallet.Accounts[0].PrivateKey().PublicKey()
|
|
err = c.updateNNSGroup(nnsCs.Hash, groupKey)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
c.Command.Printf("NNS: Set %s -> %s\n", morphClient.NNSGroupKeyName, hex.EncodeToString(groupKey.Bytes()))
|
|
|
|
return c.awaitTx()
|
|
}
|
|
|
|
func (c *initializeContext) updateNNSGroup(nnsHash util.Uint160, pub *keys.PublicKey) error {
|
|
bw := io.NewBufBinWriter()
|
|
keyAlreadyAdded, domainRegCodeEmitted, err := c.emitUpdateNNSGroupScript(bw, nnsHash, pub)
|
|
if keyAlreadyAdded || err != nil {
|
|
return err
|
|
}
|
|
|
|
script := bw.Bytes()
|
|
if domainRegCodeEmitted {
|
|
w := io.NewBufBinWriter()
|
|
emit.Instruction(w.BinWriter, opcode.INITSSLOT, []byte{1})
|
|
wrapRegisterScriptWithPrice(w, nnsHash, script)
|
|
script = w.Bytes()
|
|
}
|
|
|
|
return c.sendCommitteeTx(script, true)
|
|
}
|
|
|
|
// emitUpdateNNSGroupScript emits script for updating group key stored in NNS.
|
|
// First return value is true iff the key is already there and nothing should be done.
|
|
// Second return value is true iff a domain registration code was emitted.
|
|
func (c *initializeContext) emitUpdateNNSGroupScript(bw *io.BufBinWriter, nnsHash util.Uint160, pub *keys.PublicKey) (bool, bool, error) {
|
|
isAvail, err := nnsIsAvailable(c.Client, nnsHash, morphClient.NNSGroupKeyName)
|
|
if err != nil {
|
|
return false, false, err
|
|
}
|
|
|
|
if !isAvail {
|
|
currentPub, err := nnsResolveKey(c.ReadOnlyInvoker, nnsHash, morphClient.NNSGroupKeyName)
|
|
if err != nil {
|
|
return false, false, err
|
|
}
|
|
|
|
if pub.Equal(currentPub) {
|
|
return true, false, nil
|
|
}
|
|
}
|
|
|
|
if isAvail {
|
|
emit.AppCall(bw.BinWriter, nnsHash, "register", callflag.All,
|
|
morphClient.NNSGroupKeyName, c.CommitteeAcc.Contract.ScriptHash(),
|
|
frostfsOpsEmail, int64(3600), int64(600), int64(defaultExpirationTime), int64(3600))
|
|
emit.Opcodes(bw.BinWriter, opcode.ASSERT)
|
|
}
|
|
|
|
emit.AppCall(bw.BinWriter, nnsHash, "deleteRecords", callflag.All, "group.frostfs", int64(nns.TXT))
|
|
emit.AppCall(bw.BinWriter, nnsHash, "addRecord", callflag.All,
|
|
"group.frostfs", int64(nns.TXT), hex.EncodeToString(pub.Bytes()))
|
|
|
|
return false, isAvail, nil
|
|
}
|
|
|
|
func getAlphabetNNSDomain(i int) string {
|
|
return morphUtil.AlphabetContract + strconv.FormatUint(uint64(i), 10) + ".frostfs"
|
|
}
|
|
|
|
// wrapRegisterScriptWithPrice wraps a given script with `getPrice`/`setPrice` calls for NNS.
|
|
// It is intended to be used for a single transaction, and not as a part of other scripts.
|
|
// It is assumed that script already contains static slot initialization code, the first one
|
|
// (with index 0) is used to store the price.
|
|
func wrapRegisterScriptWithPrice(w *io.BufBinWriter, nnsHash util.Uint160, s []byte) {
|
|
if len(s) == 0 {
|
|
return
|
|
}
|
|
|
|
emit.AppCall(w.BinWriter, nnsHash, "getPrice", callflag.All)
|
|
emit.Opcodes(w.BinWriter, opcode.STSFLD0)
|
|
emit.AppCall(w.BinWriter, nnsHash, "setPrice", callflag.All, 1)
|
|
|
|
w.WriteBytes(s)
|
|
|
|
emit.Opcodes(w.BinWriter, opcode.LDSFLD0, opcode.PUSH1, opcode.PACK)
|
|
emit.AppCallNoArgs(w.BinWriter, nnsHash, "setPrice", callflag.All)
|
|
|
|
if w.Err != nil {
|
|
panic(fmt.Errorf("BUG: can't wrap register script: %w", w.Err))
|
|
}
|
|
}
|
|
|
|
func (c *initializeContext) nnsRegisterDomainScript(nnsHash, expectedHash util.Uint160, domain string) ([]byte, bool, error) {
|
|
ok, err := nnsIsAvailable(c.Client, nnsHash, domain)
|
|
if err != nil {
|
|
return nil, false, err
|
|
}
|
|
|
|
if ok {
|
|
bw := io.NewBufBinWriter()
|
|
emit.AppCall(bw.BinWriter, nnsHash, "register", callflag.All,
|
|
domain, c.CommitteeAcc.Contract.ScriptHash(),
|
|
frostfsOpsEmail, int64(3600), int64(600), int64(defaultExpirationTime), int64(3600))
|
|
emit.Opcodes(bw.BinWriter, opcode.ASSERT)
|
|
|
|
if bw.Err != nil {
|
|
panic(bw.Err)
|
|
}
|
|
return bw.Bytes(), false, nil
|
|
}
|
|
|
|
s, err := morphUtil.NNSResolveHash(c.ReadOnlyInvoker, nnsHash, domain)
|
|
if err != nil {
|
|
return nil, false, err
|
|
}
|
|
return nil, s == expectedHash, nil
|
|
}
|
|
|
|
func (c *initializeContext) nnsRegisterDomain(nnsHash, expectedHash util.Uint160, domain string) error {
|
|
script, ok, err := c.nnsRegisterDomainScript(nnsHash, expectedHash, domain)
|
|
if ok || err != nil {
|
|
return err
|
|
}
|
|
|
|
w := io.NewBufBinWriter()
|
|
emit.Instruction(w.BinWriter, opcode.INITSSLOT, []byte{1})
|
|
wrapRegisterScriptWithPrice(w, nnsHash, script)
|
|
|
|
emit.AppCall(w.BinWriter, nnsHash, "deleteRecords", callflag.All, domain, int64(nns.TXT))
|
|
emit.AppCall(w.BinWriter, nnsHash, "addRecord", callflag.All,
|
|
domain, int64(nns.TXT), expectedHash.StringLE())
|
|
emit.AppCall(w.BinWriter, nnsHash, "addRecord", callflag.All,
|
|
domain, int64(nns.TXT), address.Uint160ToString(expectedHash))
|
|
return c.sendCommitteeTx(w.Bytes(), true)
|
|
}
|
|
|
|
func (c *initializeContext) nnsRootRegistered(nnsHash util.Uint160, zone string) (bool, error) {
|
|
res, err := c.CommitteeAct.Call(nnsHash, "isAvailable", "name."+zone)
|
|
if err != nil {
|
|
return false, err
|
|
}
|
|
|
|
return res.State == vmstate.Halt.String(), nil
|
|
}
|
|
|
|
var errMissingNNSRecord = errors.New("missing NNS record")
|
|
|
|
func nnsResolveKey(inv *invoker.Invoker, nnsHash util.Uint160, domain string) (*keys.PublicKey, error) {
|
|
res, err := morphUtil.NNSResolve(inv, nnsHash, domain)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if _, ok := res.Value().(stackitem.Null); ok {
|
|
return nil, errors.New("NNS record is missing")
|
|
}
|
|
arr, ok := res.Value().([]stackitem.Item)
|
|
if !ok {
|
|
return nil, errors.New("API of the NNS contract method `resolve` has changed")
|
|
}
|
|
for i := range arr {
|
|
var bs []byte
|
|
bs, err = arr[i].TryBytes()
|
|
if err != nil {
|
|
continue
|
|
}
|
|
|
|
return keys.NewPublicKeyFromString(string(bs))
|
|
}
|
|
return nil, errors.New("no valid keys are found")
|
|
}
|
|
|
|
func nnsIsAvailable(c morphUtil.Client, nnsHash util.Uint160, name string) (bool, error) {
|
|
switch c.(type) {
|
|
case *rpcclient.Client:
|
|
inv := invoker.New(c, nil)
|
|
reader := nnsClient.NewReader(inv, nnsHash)
|
|
return reader.IsAvailable(name)
|
|
default:
|
|
b, err := unwrap.Bool(morphUtil.InvokeFunction(c, nnsHash, "isAvailable", []any{name}, nil))
|
|
if err != nil {
|
|
return false, fmt.Errorf("`isAvailable`: invalid response: %w", err)
|
|
}
|
|
|
|
return b, nil
|
|
}
|
|
}
|