2024-02-01 14:36:26 +00:00
|
|
|
package container
|
2021-08-10 08:45:01 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
2022-03-18 11:12:58 +00:00
|
|
|
"os"
|
2024-04-10 11:15:56 +00:00
|
|
|
"slices"
|
2021-08-10 09:34:27 +00:00
|
|
|
"sort"
|
2021-08-10 08:45:01 +00:00
|
|
|
|
2024-02-02 12:26:57 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/cmd/frostfs-adm/internal/modules/morph/constants"
|
2024-02-02 12:36:14 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/cmd/frostfs-adm/internal/modules/morph/helper"
|
2023-03-07 13:38:26 +00:00
|
|
|
cid "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/container/id"
|
2021-08-10 08:46:06 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/crypto/hash"
|
2021-08-10 08:45:01 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/io"
|
2022-09-05 12:45:38 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/rpcclient/invoker"
|
2023-10-24 10:06:30 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/rpcclient/management"
|
2022-09-05 12:45:38 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/rpcclient/unwrap"
|
2021-08-10 08:45:01 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/smartcontract/callflag"
|
2021-08-10 08:51:11 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/util"
|
2021-08-10 08:45:01 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/vm/emit"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/vm/stackitem"
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
"github.com/spf13/viper"
|
|
|
|
)
|
|
|
|
|
|
|
|
var errInvalidContainerResponse = errors.New("invalid response from container contract")
|
|
|
|
|
2023-10-24 10:06:30 +00:00
|
|
|
func getContainerContractHash(cmd *cobra.Command, inv *invoker.Invoker) (util.Uint160, error) {
|
2022-10-26 10:58:11 +00:00
|
|
|
s, err := cmd.Flags().GetString(containerContractFlag)
|
|
|
|
var ch util.Uint160
|
|
|
|
if err == nil {
|
|
|
|
ch, err = util.Uint160DecodeStringLE(s)
|
|
|
|
}
|
|
|
|
if err != nil {
|
2023-10-24 10:06:30 +00:00
|
|
|
r := management.NewReader(inv)
|
2024-06-28 13:35:18 +00:00
|
|
|
nnsCs, err := helper.GetContractByID(r, 1)
|
2022-10-26 10:58:11 +00:00
|
|
|
if err != nil {
|
|
|
|
return util.Uint160{}, fmt.Errorf("can't get NNS contract state: %w", err)
|
|
|
|
}
|
2024-02-02 12:36:14 +00:00
|
|
|
ch, err = helper.NNSResolveHash(inv, nnsCs.Hash, helper.DomainOf(constants.ContainerContract))
|
2022-10-26 10:58:11 +00:00
|
|
|
if err != nil {
|
|
|
|
return util.Uint160{}, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ch, nil
|
|
|
|
}
|
|
|
|
|
2023-04-03 11:37:05 +00:00
|
|
|
func iterateContainerList(inv *invoker.Invoker, ch util.Uint160, f func([]byte) error) error {
|
2023-04-03 11:06:57 +00:00
|
|
|
sid, r, err := unwrap.SessionIterator(inv.Call(ch, "containersOf", ""))
|
2022-10-26 10:58:11 +00:00
|
|
|
if err != nil {
|
2023-04-03 11:37:05 +00:00
|
|
|
return fmt.Errorf("%w: %v", errInvalidContainerResponse, err)
|
2022-10-26 10:58:11 +00:00
|
|
|
}
|
2023-04-03 11:06:57 +00:00
|
|
|
// Nothing bad, except live session on the server, do not report to the user.
|
|
|
|
defer func() { _ = inv.TerminateSession(sid) }()
|
|
|
|
|
|
|
|
items, err := inv.TraverseIterator(sid, &r, 0)
|
|
|
|
for err == nil && len(items) != 0 {
|
|
|
|
for j := range items {
|
|
|
|
b, err := items[j].TryBytes()
|
|
|
|
if err != nil {
|
2023-04-03 11:37:05 +00:00
|
|
|
return fmt.Errorf("%w: %v", errInvalidContainerResponse, err)
|
|
|
|
}
|
|
|
|
if err := f(b); err != nil {
|
|
|
|
return err
|
2023-04-03 11:06:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
items, err = inv.TraverseIterator(sid, &r, 0)
|
2022-10-26 10:58:11 +00:00
|
|
|
}
|
2023-04-03 11:37:05 +00:00
|
|
|
return err
|
2022-10-26 10:58:11 +00:00
|
|
|
}
|
|
|
|
|
2021-08-10 08:45:01 +00:00
|
|
|
func dumpContainers(cmd *cobra.Command, _ []string) error {
|
|
|
|
filename, err := cmd.Flags().GetString(containerDumpFlag)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("invalid filename: %w", err)
|
|
|
|
}
|
|
|
|
|
2024-11-22 07:53:09 +00:00
|
|
|
c, err := helper.NewRemoteClient(viper.GetViper())
|
2021-08-10 08:45:01 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("can't create N3 client: %w", err)
|
|
|
|
}
|
|
|
|
|
2022-09-05 12:45:38 +00:00
|
|
|
inv := invoker.New(c, nil)
|
|
|
|
|
2023-10-24 10:06:30 +00:00
|
|
|
ch, err := getContainerContractHash(cmd, inv)
|
2021-08-10 08:45:01 +00:00
|
|
|
if err != nil {
|
2022-10-26 10:58:11 +00:00
|
|
|
return fmt.Errorf("unable to get contaract hash: %w", err)
|
2021-08-10 08:45:01 +00:00
|
|
|
}
|
|
|
|
|
2023-04-03 11:37:05 +00:00
|
|
|
isOK, err := getCIDFilterFunc(cmd)
|
2021-08-10 08:45:01 +00:00
|
|
|
if err != nil {
|
2023-04-03 11:37:05 +00:00
|
|
|
return err
|
2021-08-10 08:45:01 +00:00
|
|
|
}
|
|
|
|
|
2023-04-03 11:37:05 +00:00
|
|
|
f, err := os.OpenFile(filename, os.O_RDWR|os.O_TRUNC|os.O_CREATE, 0o660)
|
2021-08-10 09:34:27 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-04-03 11:37:05 +00:00
|
|
|
defer f.Close()
|
2021-08-10 09:34:27 +00:00
|
|
|
|
2023-04-03 11:37:05 +00:00
|
|
|
_, err = f.Write([]byte{'['})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
written := 0
|
|
|
|
enc := json.NewEncoder(f)
|
2021-08-10 08:45:01 +00:00
|
|
|
bw := io.NewBufBinWriter()
|
2023-04-03 11:37:05 +00:00
|
|
|
iterErr := iterateContainerList(inv, ch, func(id []byte) error {
|
2021-08-10 09:34:27 +00:00
|
|
|
if !isOK(id) {
|
2023-04-03 11:37:05 +00:00
|
|
|
return nil
|
2021-08-10 08:45:01 +00:00
|
|
|
}
|
|
|
|
|
2023-04-03 11:37:05 +00:00
|
|
|
cnt, err := dumpSingleContainer(bw, ch, inv, id)
|
2021-08-10 08:45:01 +00:00
|
|
|
if err != nil {
|
2023-04-03 11:37:05 +00:00
|
|
|
return err
|
2021-08-10 08:45:01 +00:00
|
|
|
}
|
|
|
|
|
2023-04-03 11:37:05 +00:00
|
|
|
// Writing directly to the file is ok, because json.Encoder does no internal buffering.
|
|
|
|
if written != 0 {
|
|
|
|
_, err = f.Write([]byte{','})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-08-10 08:45:01 +00:00
|
|
|
}
|
|
|
|
|
2023-04-03 11:37:05 +00:00
|
|
|
written++
|
|
|
|
return enc.Encode(cnt)
|
|
|
|
})
|
|
|
|
if iterErr != nil {
|
|
|
|
return iterErr
|
2021-08-10 08:45:01 +00:00
|
|
|
}
|
|
|
|
|
2023-04-03 11:37:05 +00:00
|
|
|
_, err = f.Write([]byte{']'})
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func dumpSingleContainer(bw *io.BufBinWriter, ch util.Uint160, inv *invoker.Invoker, id []byte) (*Container, error) {
|
|
|
|
bw.Reset()
|
|
|
|
emit.AppCall(bw.BinWriter, ch, "get", callflag.All, id)
|
|
|
|
res, err := inv.Run(bw.Bytes())
|
2021-08-10 08:45:01 +00:00
|
|
|
if err != nil {
|
2023-04-03 11:37:05 +00:00
|
|
|
return nil, fmt.Errorf("can't get container info: %w", err)
|
|
|
|
}
|
2024-10-09 07:03:58 +00:00
|
|
|
if len(res.Stack) != 1 {
|
|
|
|
return nil, fmt.Errorf("%w: expected 1 items on stack", errInvalidContainerResponse)
|
2023-04-03 11:37:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
cnt := new(Container)
|
|
|
|
err = cnt.FromStackItem(res.Stack[0])
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("%w: %v", errInvalidContainerResponse, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return cnt, nil
|
2021-08-10 08:45:01 +00:00
|
|
|
}
|
|
|
|
|
2022-10-26 10:58:11 +00:00
|
|
|
func listContainers(cmd *cobra.Command, _ []string) error {
|
2024-11-22 07:53:09 +00:00
|
|
|
c, err := helper.NewRemoteClient(viper.GetViper())
|
2022-10-26 10:58:11 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("can't create N3 client: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
inv := invoker.New(c, nil)
|
|
|
|
|
2023-10-24 10:06:30 +00:00
|
|
|
ch, err := getContainerContractHash(cmd, inv)
|
2022-10-26 10:58:11 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("unable to get contaract hash: %w", err)
|
|
|
|
}
|
|
|
|
|
2023-04-03 11:37:05 +00:00
|
|
|
return iterateContainerList(inv, ch, func(id []byte) error {
|
2022-10-26 10:58:11 +00:00
|
|
|
var idCnr cid.ID
|
|
|
|
err = idCnr.Decode(id)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("unable to decode container id: %w", err)
|
|
|
|
}
|
|
|
|
cmd.Println(idCnr)
|
2023-04-03 11:37:05 +00:00
|
|
|
return nil
|
|
|
|
})
|
2022-10-26 10:58:11 +00:00
|
|
|
}
|
|
|
|
|
2021-08-10 08:46:06 +00:00
|
|
|
func restoreContainers(cmd *cobra.Command, _ []string) error {
|
|
|
|
filename, err := cmd.Flags().GetString(containerDumpFlag)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("invalid filename: %w", err)
|
|
|
|
}
|
|
|
|
|
2024-02-02 12:36:14 +00:00
|
|
|
wCtx, err := helper.NewInitializeContext(cmd, viper.GetViper())
|
2021-08-10 08:46:06 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2024-02-01 11:30:59 +00:00
|
|
|
defer wCtx.Close()
|
2021-08-10 08:46:06 +00:00
|
|
|
|
2023-03-22 14:25:35 +00:00
|
|
|
containers, err := parseContainers(filename)
|
2021-08-10 08:46:06 +00:00
|
|
|
if err != nil {
|
2023-03-22 14:25:35 +00:00
|
|
|
return err
|
2021-08-10 08:46:06 +00:00
|
|
|
}
|
|
|
|
|
2023-03-22 14:25:35 +00:00
|
|
|
ch, err := fetchContainerContractHash(wCtx)
|
2021-08-10 08:46:06 +00:00
|
|
|
if err != nil {
|
2023-03-22 14:25:35 +00:00
|
|
|
return err
|
2021-08-10 08:46:06 +00:00
|
|
|
}
|
|
|
|
|
2023-03-22 14:25:35 +00:00
|
|
|
isOK, err := getCIDFilterFunc(cmd)
|
2021-08-10 08:46:06 +00:00
|
|
|
if err != nil {
|
2023-03-22 14:25:35 +00:00
|
|
|
return err
|
2021-08-10 08:46:06 +00:00
|
|
|
}
|
|
|
|
|
2023-03-22 14:25:35 +00:00
|
|
|
err = restoreOrPutContainers(containers, isOK, cmd, wCtx, ch)
|
2021-08-10 09:34:27 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-02-01 11:30:59 +00:00
|
|
|
return wCtx.AwaitTx()
|
2023-03-22 14:25:35 +00:00
|
|
|
}
|
|
|
|
|
2024-02-02 12:36:14 +00:00
|
|
|
func restoreOrPutContainers(containers []Container, isOK func([]byte) bool, cmd *cobra.Command, wCtx *helper.InitializeContext, ch util.Uint160) error {
|
2021-08-10 08:46:06 +00:00
|
|
|
bw := io.NewBufBinWriter()
|
|
|
|
for _, cnt := range containers {
|
|
|
|
hv := hash.Sha256(cnt.Value)
|
2021-08-10 09:34:27 +00:00
|
|
|
if !isOK(hv[:]) {
|
|
|
|
continue
|
|
|
|
}
|
2021-08-10 08:46:06 +00:00
|
|
|
bw.Reset()
|
2023-03-22 14:25:35 +00:00
|
|
|
restored, err := isContainerRestored(cmd, wCtx, ch, bw, hv)
|
2021-08-10 08:46:06 +00:00
|
|
|
if err != nil {
|
2023-03-22 14:25:35 +00:00
|
|
|
return err
|
2021-08-10 08:46:06 +00:00
|
|
|
}
|
2023-03-22 14:25:35 +00:00
|
|
|
if restored {
|
2021-08-10 08:46:06 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
bw.Reset()
|
2023-03-22 14:25:35 +00:00
|
|
|
|
|
|
|
putContainer(bw, ch, cnt)
|
|
|
|
|
2021-08-10 08:46:06 +00:00
|
|
|
if bw.Err != nil {
|
|
|
|
panic(bw.Err)
|
|
|
|
}
|
|
|
|
|
2024-02-01 11:30:59 +00:00
|
|
|
if err := wCtx.SendConsensusTx(bw.Bytes()); err != nil {
|
2021-08-10 08:46:06 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2023-03-22 14:25:35 +00:00
|
|
|
return nil
|
|
|
|
}
|
2021-08-10 08:46:06 +00:00
|
|
|
|
2023-03-22 14:25:35 +00:00
|
|
|
func putContainer(bw *io.BufBinWriter, ch util.Uint160, cnt Container) {
|
|
|
|
emit.AppCall(bw.BinWriter, ch, "put", callflag.All,
|
|
|
|
cnt.Value, cnt.Signature, cnt.PublicKey, cnt.Token)
|
|
|
|
}
|
|
|
|
|
2024-02-02 12:36:14 +00:00
|
|
|
func isContainerRestored(cmd *cobra.Command, wCtx *helper.InitializeContext, containerHash util.Uint160, bw *io.BufBinWriter, hashValue util.Uint256) (bool, error) {
|
2023-03-22 14:25:35 +00:00
|
|
|
emit.AppCall(bw.BinWriter, containerHash, "get", callflag.All, hashValue.BytesBE())
|
|
|
|
res, err := wCtx.Client.InvokeScript(bw.Bytes(), nil)
|
|
|
|
if err != nil {
|
|
|
|
return false, fmt.Errorf("can't check if container is already restored: %w", err)
|
|
|
|
}
|
|
|
|
if len(res.Stack) == 0 {
|
|
|
|
return false, errors.New("empty stack")
|
|
|
|
}
|
|
|
|
|
|
|
|
old := new(Container)
|
|
|
|
if err := old.FromStackItem(res.Stack[0]); err != nil {
|
|
|
|
return false, fmt.Errorf("%w: %v", errInvalidContainerResponse, err)
|
|
|
|
}
|
|
|
|
if len(old.Value) != 0 {
|
|
|
|
var id cid.ID
|
|
|
|
id.SetSHA256(hashValue)
|
|
|
|
cmd.Printf("Container %s is already deployed.\n", id)
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func parseContainers(filename string) ([]Container, error) {
|
|
|
|
data, err := os.ReadFile(filename)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("can't read dump file: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
var containers []Container
|
|
|
|
err = json.Unmarshal(data, &containers)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("can't parse dump file: %w", err)
|
|
|
|
}
|
|
|
|
return containers, nil
|
|
|
|
}
|
|
|
|
|
2024-02-02 12:36:14 +00:00
|
|
|
func fetchContainerContractHash(wCtx *helper.InitializeContext) (util.Uint160, error) {
|
2023-10-24 10:06:30 +00:00
|
|
|
r := management.NewReader(wCtx.ReadOnlyInvoker)
|
2024-06-28 13:35:18 +00:00
|
|
|
nnsCs, err := helper.GetContractByID(r, 1)
|
2023-03-22 14:25:35 +00:00
|
|
|
if err != nil {
|
|
|
|
return util.Uint160{}, fmt.Errorf("can't get NNS contract state: %w", err)
|
|
|
|
}
|
|
|
|
|
2024-02-02 12:36:14 +00:00
|
|
|
ch, err := helper.NNSResolveHash(wCtx.ReadOnlyInvoker, nnsCs.Hash, helper.DomainOf(constants.ContainerContract))
|
2023-03-22 14:25:35 +00:00
|
|
|
if err != nil {
|
|
|
|
return util.Uint160{}, fmt.Errorf("can't fetch container contract hash: %w", err)
|
|
|
|
}
|
|
|
|
return ch, nil
|
2021-08-10 08:46:06 +00:00
|
|
|
}
|
|
|
|
|
2021-08-10 08:45:01 +00:00
|
|
|
// Container represents container struct in contract storage.
|
|
|
|
type Container struct {
|
|
|
|
Value []byte `json:"value"`
|
|
|
|
Signature []byte `json:"signature"`
|
|
|
|
PublicKey []byte `json:"public_key"`
|
|
|
|
Token []byte `json:"token"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// ToStackItem implements stackitem.Convertible.
|
|
|
|
func (c *Container) ToStackItem() (stackitem.Item, error) {
|
|
|
|
return stackitem.NewStruct([]stackitem.Item{
|
|
|
|
stackitem.NewByteArray(c.Value),
|
|
|
|
stackitem.NewByteArray(c.Signature),
|
|
|
|
stackitem.NewByteArray(c.PublicKey),
|
|
|
|
stackitem.NewByteArray(c.Token),
|
|
|
|
}), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// FromStackItem implements stackitem.Convertible.
|
|
|
|
func (c *Container) FromStackItem(item stackitem.Item) error {
|
|
|
|
arr, ok := item.Value().([]stackitem.Item)
|
|
|
|
if !ok || len(arr) != 4 {
|
|
|
|
return errors.New("invalid stack item type")
|
|
|
|
}
|
|
|
|
|
|
|
|
value, err := arr[0].TryBytes()
|
|
|
|
if err != nil {
|
|
|
|
return errors.New("invalid container value")
|
|
|
|
}
|
|
|
|
|
|
|
|
sig, err := arr[1].TryBytes()
|
|
|
|
if err != nil {
|
|
|
|
return errors.New("invalid container signature")
|
|
|
|
}
|
|
|
|
|
|
|
|
pub, err := arr[2].TryBytes()
|
|
|
|
if err != nil {
|
|
|
|
return errors.New("invalid container public key")
|
|
|
|
}
|
|
|
|
|
|
|
|
tok, err := arr[3].TryBytes()
|
|
|
|
if err != nil {
|
|
|
|
return errors.New("invalid container token")
|
|
|
|
}
|
|
|
|
|
|
|
|
c.Value = value
|
|
|
|
c.Signature = sig
|
|
|
|
c.PublicKey = pub
|
|
|
|
c.Token = tok
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-08-10 09:34:27 +00:00
|
|
|
// getCIDFilterFunc returns filtering function for container IDs.
|
|
|
|
// Raw byte slices are used because it works with structures returned
|
|
|
|
// from contract.
|
|
|
|
func getCIDFilterFunc(cmd *cobra.Command) (func([]byte) bool, error) {
|
|
|
|
rawIDs, err := cmd.Flags().GetStringSlice(containerIDsFlag)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if len(rawIDs) == 0 {
|
|
|
|
return func([]byte) bool { return true }, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := range rawIDs {
|
2022-05-12 16:37:46 +00:00
|
|
|
err := new(cid.ID).DecodeString(rawIDs[i])
|
2021-08-10 09:34:27 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("can't parse CID %s: %w", rawIDs[i], err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
sort.Strings(rawIDs)
|
|
|
|
return func(rawID []byte) bool {
|
|
|
|
var v [32]byte
|
|
|
|
copy(v[:], rawID)
|
|
|
|
|
2022-05-12 16:37:46 +00:00
|
|
|
var id cid.ID
|
2021-08-10 09:34:27 +00:00
|
|
|
id.SetSHA256(v)
|
2022-05-12 16:37:46 +00:00
|
|
|
idStr := id.EncodeToString()
|
2024-04-10 11:15:56 +00:00
|
|
|
_, found := slices.BinarySearch(rawIDs, idStr)
|
|
|
|
return found
|
2021-08-10 09:34:27 +00:00
|
|
|
}, nil
|
|
|
|
}
|