2024-01-31 14:26:26 +00:00
|
|
|
package util
|
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"
|
|
|
|
"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"
|
|
|
|
"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)
|
|
|
|
}
|
|
|
|
|
|
|
|
var size int
|
|
|
|
loop:
|
|
|
|
for i := 0; i < len(walletFiles); i++ {
|
|
|
|
name := innerring.GlagoliticLetter(i).String() + ".json"
|
|
|
|
for j := range walletFiles {
|
|
|
|
if walletFiles[j].Name() == name {
|
|
|
|
size++
|
|
|
|
continue loop
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break
|
|
|
|
}
|
|
|
|
if size == 0 {
|
|
|
|
return nil, errors.New("alphabet wallets dir is empty (run `generate-alphabet` command first)")
|
|
|
|
}
|
|
|
|
|
|
|
|
wallets := make([]*wallet.Wallet, size)
|
|
|
|
for i := 0; i < size; i++ {
|
|
|
|
letter := innerring.GlagoliticLetter(i).String()
|
|
|
|
p := filepath.Join(walletDir, letter+".json")
|
|
|
|
w, err := wallet.NewWalletFromFile(p)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("can't open wallet: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
password, err := config.GetPassword(v, letter)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("can't fetch password: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := range w.Accounts {
|
|
|
|
if err := w.Accounts[i].Decrypt(password, keys.NEP2ScryptParams()); err != nil {
|
|
|
|
return nil, fmt.Errorf("can't unlock wallet: %w", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
wallets[i] = w
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
for h, err := r.Next(); ; h, err = r.Next() {
|
|
|
|
if err != nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|