2024-02-02 12:36:14 +00:00
|
|
|
package helper
|
2023-12-20 12:22:17 +00:00
|
|
|
|
|
|
|
import (
|
2024-02-01 12:21:51 +00:00
|
|
|
"archive/tar"
|
|
|
|
"compress/gzip"
|
2023-12-20 12:22:17 +00:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
2024-02-01 12:21:51 +00:00
|
|
|
"io"
|
2023-12-20 12:22:17 +00:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2024-02-01 13:38:20 +00:00
|
|
|
"strconv"
|
2024-02-01 12:21:51 +00:00
|
|
|
"strings"
|
2023-12-20 12:22:17 +00:00
|
|
|
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/cmd/frostfs-adm/internal/modules/config"
|
2024-02-02 12:26:57 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/cmd/frostfs-adm/internal/modules/morph/constants"
|
2023-12-20 12:22:17 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/innerring"
|
2024-06-28 13:35:18 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/state"
|
2023-12-20 12:22:17 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/transaction"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/crypto/keys"
|
2024-02-01 13:58:48 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/encoding/fixedn"
|
2023-12-20 12:22:17 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/rpcclient/actor"
|
2024-06-28 13:35:18 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/rpcclient/management"
|
2023-12-20 12:22:17 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/wallet"
|
|
|
|
"github.com/spf13/viper"
|
|
|
|
)
|
|
|
|
|
2024-01-31 14:26:26 +00:00
|
|
|
func GetAlphabetWallets(v *viper.Viper, walletDir string) ([]*wallet.Wallet, error) {
|
2024-02-01 13:00:45 +00:00
|
|
|
wallets, err := openAlphabetWallets(v, walletDir)
|
2023-12-20 12:22:17 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2024-02-02 12:26:57 +00:00
|
|
|
if len(wallets) > constants.MaxAlphabetNodes {
|
2023-12-20 12:22:17 +00:00
|
|
|
return nil, ErrTooManyAlphabetNodes
|
|
|
|
}
|
|
|
|
return wallets, nil
|
|
|
|
}
|
|
|
|
|
2024-02-01 13:00:45 +00:00
|
|
|
func openAlphabetWallets(v *viper.Viper, walletDir string) ([]*wallet.Wallet, error) {
|
2023-12-20 12:22:17 +00:00
|
|
|
walletFiles, err := os.ReadDir(walletDir)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("can't read alphabet wallets dir: %w", err)
|
|
|
|
}
|
|
|
|
|
2024-06-26 11:30:31 +00:00
|
|
|
var wallets []*wallet.Wallet
|
|
|
|
var letter string
|
2024-08-30 16:20:55 +00:00
|
|
|
for i := range constants.MaxAlphabetNodes {
|
2024-06-26 11:30:31 +00:00
|
|
|
letter = innerring.GlagoliticLetter(i).String()
|
2023-12-20 12:22:17 +00:00
|
|
|
p := filepath.Join(walletDir, letter+".json")
|
2024-06-26 11:30:31 +00:00
|
|
|
var w *wallet.Wallet
|
|
|
|
w, err = wallet.NewWalletFromFile(p)
|
2023-12-20 12:22:17 +00:00
|
|
|
if err != nil {
|
2024-06-26 11:30:31 +00:00
|
|
|
if errors.Is(err, os.ErrNotExist) {
|
|
|
|
err = nil
|
|
|
|
} else {
|
|
|
|
err = fmt.Errorf("can't open wallet: %w", err)
|
|
|
|
}
|
|
|
|
break
|
2023-12-20 12:22:17 +00:00
|
|
|
}
|
|
|
|
|
2024-06-26 11:30:31 +00:00
|
|
|
var password string
|
|
|
|
password, err = config.GetPassword(v, letter)
|
2023-12-20 12:22:17 +00:00
|
|
|
if err != nil {
|
2024-06-26 11:30:31 +00:00
|
|
|
err = fmt.Errorf("can't fetch password: %w", err)
|
|
|
|
break
|
2023-12-20 12:22:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for i := range w.Accounts {
|
2024-06-26 11:30:31 +00:00
|
|
|
if err = w.Accounts[i].Decrypt(password, keys.NEP2ScryptParams()); err != nil {
|
|
|
|
err = fmt.Errorf("can't unlock wallet: %w", err)
|
|
|
|
break
|
2023-12-20 12:22:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-06-26 11:30:31 +00:00
|
|
|
wallets = append(wallets, w)
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("can't read wallet for letter '%s': %w", letter, err)
|
|
|
|
}
|
|
|
|
if len(wallets) == 0 {
|
|
|
|
err = errors.New("there are no alphabet wallets in dir (run `generate-alphabet` command first)")
|
|
|
|
if len(walletFiles) > 0 {
|
|
|
|
err = fmt.Errorf("use glagolitic names for wallets(run `print-alphabet`): %w", err)
|
|
|
|
}
|
|
|
|
return nil, err
|
2023-12-20 12:22:17 +00:00
|
|
|
}
|
|
|
|
return wallets, nil
|
|
|
|
}
|
|
|
|
|
2024-01-31 14:26:26 +00:00
|
|
|
func NewActor(c actor.RPCActor, committeeAcc *wallet.Account) (*actor.Actor, error) {
|
2023-12-20 12:22:17 +00:00
|
|
|
return actor.New(c, []actor.SignerAccount{{
|
|
|
|
Signer: transaction.Signer{
|
|
|
|
Account: committeeAcc.Contract.ScriptHash(),
|
|
|
|
Scopes: transaction.Global,
|
|
|
|
},
|
|
|
|
Account: committeeAcc,
|
|
|
|
}})
|
|
|
|
}
|
2024-02-01 12:21:51 +00:00
|
|
|
|
|
|
|
func ReadContract(ctrPath, ctrName string) (*ContractState, error) {
|
|
|
|
rawNef, err := os.ReadFile(filepath.Join(ctrPath, ctrName+"_contract.nef"))
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("can't read NEF file for %s contract: %w", ctrName, err)
|
|
|
|
}
|
|
|
|
rawManif, err := os.ReadFile(filepath.Join(ctrPath, "config.json"))
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("can't read manifest file for %s contract: %w", ctrName, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
cs := &ContractState{
|
|
|
|
RawNEF: rawNef,
|
|
|
|
RawManifest: rawManif,
|
|
|
|
}
|
|
|
|
|
|
|
|
return cs, cs.Parse()
|
|
|
|
}
|
|
|
|
|
2024-02-01 13:00:45 +00:00
|
|
|
func readContractsFromArchive(file io.Reader, names []string) (map[string]*ContractState, error) {
|
2024-02-01 12:21:51 +00:00
|
|
|
m := make(map[string]*ContractState, len(names))
|
|
|
|
for i := range names {
|
|
|
|
m[names[i]] = new(ContractState)
|
|
|
|
}
|
|
|
|
|
|
|
|
gr, err := gzip.NewReader(file)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("contracts file must be tar.gz archive: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
r := tar.NewReader(gr)
|
2024-06-06 12:25:51 +00:00
|
|
|
var h *tar.Header
|
|
|
|
for h, err = r.Next(); err == nil && h != nil; h, err = r.Next() {
|
|
|
|
if h.Typeflag != tar.TypeReg {
|
|
|
|
continue
|
2024-02-01 12:21:51 +00:00
|
|
|
}
|
|
|
|
dir, _ := filepath.Split(h.Name)
|
|
|
|
ctrName := filepath.Base(dir)
|
|
|
|
|
|
|
|
cs, ok := m[ctrName]
|
|
|
|
if !ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
switch {
|
|
|
|
case strings.HasSuffix(h.Name, filepath.Join(ctrName, ctrName+"_contract.nef")):
|
|
|
|
cs.RawNEF, err = io.ReadAll(r)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("can't read NEF file for %s contract: %w", ctrName, err)
|
|
|
|
}
|
|
|
|
case strings.HasSuffix(h.Name, "config.json"):
|
|
|
|
cs.RawManifest, err = io.ReadAll(r)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("can't read manifest file for %s contract: %w", ctrName, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
m[ctrName] = cs
|
|
|
|
}
|
2024-06-06 12:25:51 +00:00
|
|
|
if err != nil && err != io.EOF {
|
|
|
|
return nil, fmt.Errorf("can't read contracts from archive: %w", err)
|
|
|
|
}
|
2024-02-01 12:21:51 +00:00
|
|
|
|
|
|
|
for ctrName, cs := range m {
|
|
|
|
if cs.RawNEF == nil {
|
|
|
|
return nil, fmt.Errorf("NEF for %s contract wasn't found", ctrName)
|
|
|
|
}
|
|
|
|
if cs.RawManifest == nil {
|
|
|
|
return nil, fmt.Errorf("manifest for %s contract wasn't found", ctrName)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return m, nil
|
|
|
|
}
|
2024-02-01 13:38:20 +00:00
|
|
|
|
|
|
|
func GetAlphabetNNSDomain(i int) string {
|
2024-02-02 12:26:57 +00:00
|
|
|
return constants.AlphabetContract + strconv.FormatUint(uint64(i), 10) + ".frostfs"
|
2024-02-01 13:38:20 +00:00
|
|
|
}
|
2024-02-01 13:58:48 +00:00
|
|
|
|
|
|
|
func ParseGASAmount(s string) (fixedn.Fixed8, error) {
|
|
|
|
gasAmount, err := fixedn.Fixed8FromString(s)
|
|
|
|
if err != nil {
|
|
|
|
return 0, fmt.Errorf("invalid GAS amount %s: %w", s, err)
|
|
|
|
}
|
|
|
|
if gasAmount <= 0 {
|
|
|
|
return 0, fmt.Errorf("GAS amount must be positive (got %d)", gasAmount)
|
|
|
|
}
|
|
|
|
return gasAmount, nil
|
|
|
|
}
|
2024-06-28 13:35:18 +00:00
|
|
|
|
|
|
|
// GetContractByID retrieves a contract by its ID using the standard GetContractByID method.
|
|
|
|
// However, if the returned state.Contract is nil, it returns an error indicating that the contract was not found.
|
|
|
|
// See https://git.frostfs.info/TrueCloudLab/frostfs-node/issues/1210
|
|
|
|
func GetContractByID(r *management.ContractReader, id int32) (*state.Contract, error) {
|
|
|
|
cs, err := r.GetContractByID(id)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if cs == nil {
|
|
|
|
return nil, errors.New("contract not found")
|
|
|
|
}
|
|
|
|
return cs, nil
|
|
|
|
}
|