2024-02-02 07:25:04 +00:00
|
|
|
package initialize
|
2021-07-19 11:04:19 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2023-10-24 10:13:49 +00:00
|
|
|
"math/big"
|
2024-09-20 10:24:40 +00:00
|
|
|
"strings"
|
2021-07-19 11:04:19 +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"
|
2021-07-19 11:04:19 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/native"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/transaction"
|
2022-04-07 08:10:35 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/io"
|
2023-10-24 12:12:59 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/rpcclient/actor"
|
2022-11-23 14:44:03 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/rpcclient/gas"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/rpcclient/neo"
|
2023-10-24 10:13:49 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/rpcclient/nep17"
|
2022-04-07 08:10:35 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/smartcontract/callflag"
|
2023-10-24 12:20:38 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/util"
|
2022-04-07 08:10:35 +00:00
|
|
|
"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/wallet"
|
2021-07-19 11:04:19 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
gasInitialTotalSupply = 30000000 * native.GASFactor
|
2022-04-21 11:28:05 +00:00
|
|
|
// initialAlphabetGASAmount represents the amount of GAS given to each alphabet node.
|
2021-07-19 11:04:19 +00:00
|
|
|
initialAlphabetGASAmount = 10_000 * native.GASFactor
|
2022-04-21 11:28:05 +00:00
|
|
|
// initialProxyGASAmount represents the amount of GAS given to a proxy contract.
|
2021-07-23 12:07:44 +00:00
|
|
|
initialProxyGASAmount = 50_000 * native.GASFactor
|
2024-07-11 15:28:14 +00:00
|
|
|
// alphabetGasRatio is a coefficient that defines the threshold below which
|
|
|
|
// the balance of the alphabet node is considered not replenished. The value
|
|
|
|
// of this coefficient is determined empirically.
|
|
|
|
alphabetGasRatio = 5
|
2021-07-19 11:04:19 +00:00
|
|
|
)
|
|
|
|
|
2024-02-02 12:36:14 +00:00
|
|
|
func transferFunds(c *helper.InitializeContext) error {
|
2024-02-01 11:30:59 +00:00
|
|
|
ok, err := transferFundsFinished(c)
|
2021-07-20 14:51:45 +00:00
|
|
|
if ok || err != nil {
|
|
|
|
if err == nil {
|
|
|
|
c.Command.Println("Stage 1: already performed.")
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-10-24 12:20:38 +00:00
|
|
|
var transfers []transferTarget
|
2021-07-30 11:26:44 +00:00
|
|
|
for _, acc := range c.Accounts {
|
2021-07-19 11:04:19 +00:00
|
|
|
to := acc.Contract.ScriptHash()
|
|
|
|
transfers = append(transfers,
|
2023-10-24 12:20:38 +00:00
|
|
|
transferTarget{
|
2022-11-23 14:44:03 +00:00
|
|
|
Token: gas.Hash,
|
2021-07-19 11:04:19 +00:00
|
|
|
Address: to,
|
|
|
|
Amount: initialAlphabetGASAmount,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
// It is convenient to have all funds at the committee account.
|
|
|
|
transfers = append(transfers,
|
2023-10-24 12:20:38 +00:00
|
|
|
transferTarget{
|
2022-11-23 14:44:03 +00:00
|
|
|
Token: gas.Hash,
|
2021-07-19 11:04:19 +00:00
|
|
|
Address: c.CommitteeAcc.Contract.ScriptHash(),
|
2021-07-20 14:51:45 +00:00
|
|
|
Amount: (gasInitialTotalSupply - initialAlphabetGASAmount*int64(len(c.Wallets))) / 2,
|
2021-07-19 11:04:19 +00:00
|
|
|
},
|
2023-10-24 12:20:38 +00:00
|
|
|
transferTarget{
|
2022-11-23 14:44:03 +00:00
|
|
|
Token: neo.Hash,
|
2021-07-19 11:04:19 +00:00
|
|
|
Address: c.CommitteeAcc.Contract.ScriptHash(),
|
|
|
|
Amount: native.NEOTotalSupply,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
2023-10-24 12:12:59 +00:00
|
|
|
tx, err := createNEP17MultiTransferTx(c.Client, c.ConsensusAcc, transfers)
|
2021-07-19 11:04:19 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("can't create transfer transaction: %w", err)
|
|
|
|
}
|
|
|
|
|
2024-02-02 12:26:57 +00:00
|
|
|
if err := c.MultiSignAndSend(tx, constants.ConsensusAccountName); err != nil {
|
2021-07-19 11:04:19 +00:00
|
|
|
return fmt.Errorf("can't send transfer transaction: %w", err)
|
|
|
|
}
|
|
|
|
|
2024-02-01 11:30:59 +00:00
|
|
|
return c.AwaitTx()
|
2021-07-19 11:04:19 +00:00
|
|
|
}
|
|
|
|
|
2024-02-02 12:36:14 +00:00
|
|
|
func transferFundsFinished(c *helper.InitializeContext) (bool, error) {
|
2021-07-30 11:26:44 +00:00
|
|
|
acc := c.Accounts[0]
|
2021-07-20 14:51:45 +00:00
|
|
|
|
2023-10-24 10:13:49 +00:00
|
|
|
r := nep17.NewReader(c.ReadOnlyInvoker, gas.Hash)
|
|
|
|
res, err := r.BalanceOf(acc.Contract.ScriptHash())
|
2024-07-11 15:28:14 +00:00
|
|
|
return res.Cmp(big.NewInt(alphabetGasRatio*native.GASFactor)) == 1, err
|
2021-07-20 14:51:45 +00:00
|
|
|
}
|
|
|
|
|
2024-02-02 12:36:14 +00:00
|
|
|
func transferGASToProxy(c *helper.InitializeContext) error {
|
2024-02-02 12:26:57 +00:00
|
|
|
proxyCs := c.GetContract(constants.ProxyContract)
|
2021-07-23 12:07:44 +00:00
|
|
|
|
2023-10-24 10:13:49 +00:00
|
|
|
r := nep17.NewReader(c.ReadOnlyInvoker, gas.Hash)
|
|
|
|
bal, err := r.BalanceOf(proxyCs.Hash)
|
|
|
|
if err != nil || bal.Sign() > 0 {
|
2021-07-23 12:07:44 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-10-24 12:20:38 +00:00
|
|
|
tx, err := createNEP17MultiTransferTx(c.Client, c.CommitteeAcc, []transferTarget{{
|
2022-11-23 14:44:03 +00:00
|
|
|
Token: gas.Hash,
|
2022-04-07 08:10:35 +00:00
|
|
|
Address: proxyCs.Hash,
|
|
|
|
Amount: initialProxyGASAmount,
|
2023-10-24 12:12:59 +00:00
|
|
|
}})
|
2021-07-23 12:07:44 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-02-02 12:26:57 +00:00
|
|
|
if err := c.MultiSignAndSend(tx, constants.CommitteeAccountName); err != nil {
|
2021-07-23 12:07:44 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-02-01 11:30:59 +00:00
|
|
|
return c.AwaitTx()
|
2021-07-23 12:07:44 +00:00
|
|
|
}
|
2022-04-07 08:10:35 +00:00
|
|
|
|
2023-10-24 12:20:38 +00:00
|
|
|
type transferTarget struct {
|
|
|
|
Token util.Uint160
|
|
|
|
Address util.Uint160
|
|
|
|
Amount int64
|
|
|
|
Data any
|
|
|
|
}
|
|
|
|
|
2024-02-02 12:36:14 +00:00
|
|
|
func createNEP17MultiTransferTx(c helper.Client, acc *wallet.Account, recipients []transferTarget) (*transaction.Transaction, error) {
|
2022-04-07 08:10:35 +00:00
|
|
|
from := acc.Contract.ScriptHash()
|
|
|
|
|
|
|
|
w := io.NewBufBinWriter()
|
|
|
|
for i := range recipients {
|
|
|
|
emit.AppCall(w.BinWriter, recipients[i].Token, "transfer", callflag.All,
|
|
|
|
from, recipients[i].Address, recipients[i].Amount, recipients[i].Data)
|
|
|
|
emit.Opcodes(w.BinWriter, opcode.ASSERT)
|
|
|
|
}
|
|
|
|
if w.Err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to create transfer script: %w", w.Err)
|
|
|
|
}
|
2023-10-24 12:12:59 +00:00
|
|
|
|
|
|
|
signers := []actor.SignerAccount{{
|
2022-04-07 08:10:35 +00:00
|
|
|
Signer: transaction.Signer{
|
2023-10-24 12:12:59 +00:00
|
|
|
Account: acc.Contract.ScriptHash(),
|
2022-04-07 08:10:35 +00:00
|
|
|
Scopes: transaction.CalledByEntry,
|
|
|
|
},
|
|
|
|
Account: acc,
|
2023-10-24 12:12:59 +00:00
|
|
|
}}
|
|
|
|
|
|
|
|
act, err := actor.New(c, signers)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("can't create actor: %w", err)
|
|
|
|
}
|
2024-09-20 10:24:40 +00:00
|
|
|
tx, err := act.MakeRun(w.Bytes())
|
|
|
|
if err != nil {
|
|
|
|
sum := make(map[util.Uint160]int64)
|
|
|
|
for _, recipient := range recipients {
|
|
|
|
sum[recipient.Token] += recipient.Amount
|
|
|
|
}
|
|
|
|
detail := make([]string, 0, len(sum))
|
|
|
|
for _, value := range sum {
|
|
|
|
detail = append(detail, fmt.Sprintf("amount=%v", value))
|
|
|
|
}
|
|
|
|
err = fmt.Errorf("transfer failed: from=%s(%s) %s: %w", acc.Label, acc.Address, strings.Join(detail, " "), err)
|
|
|
|
}
|
|
|
|
return tx, err
|
2022-04-07 08:10:35 +00:00
|
|
|
}
|