forked from TrueCloudLab/frostfs-node
Anton Nikiforov
802192cfef
To avoid conflicts with `util` packages in other imports. Signed-off-by: Anton Nikiforov <an.nikiforov@yadro.com>
174 lines
5.3 KiB
Go
174 lines
5.3 KiB
Go
package initialize
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"math/big"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/cmd/frostfs-adm/internal/modules/morph/constants"
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/cmd/frostfs-adm/internal/modules/morph/helper"
|
|
"github.com/nspcc-dev/neo-go/pkg/core/native"
|
|
"github.com/nspcc-dev/neo-go/pkg/core/state"
|
|
"github.com/nspcc-dev/neo-go/pkg/core/transaction"
|
|
"github.com/nspcc-dev/neo-go/pkg/io"
|
|
"github.com/nspcc-dev/neo-go/pkg/rpcclient"
|
|
"github.com/nspcc-dev/neo-go/pkg/rpcclient/actor"
|
|
"github.com/nspcc-dev/neo-go/pkg/rpcclient/invoker"
|
|
"github.com/nspcc-dev/neo-go/pkg/rpcclient/neo"
|
|
"github.com/nspcc-dev/neo-go/pkg/rpcclient/nep17"
|
|
"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"
|
|
)
|
|
|
|
// initialAlphabetNEOAmount represents the total amount of GAS distributed between alphabet nodes.
|
|
const (
|
|
initialAlphabetNEOAmount = native.NEOTotalSupply
|
|
registerBatchSize = transaction.MaxAttributes - 1
|
|
)
|
|
|
|
func registerCandidateRange(c *helper.InitializeContext, start, end int) error {
|
|
regPrice, err := getCandidateRegisterPrice(c)
|
|
if err != nil {
|
|
return fmt.Errorf("can't fetch registration price: %w", err)
|
|
}
|
|
|
|
w := io.NewBufBinWriter()
|
|
emit.AppCall(w.BinWriter, neo.Hash, "setRegisterPrice", callflag.States, 1)
|
|
for _, acc := range c.Accounts[start:end] {
|
|
emit.AppCall(w.BinWriter, neo.Hash, "registerCandidate", callflag.States, acc.PrivateKey().PublicKey().Bytes())
|
|
emit.Opcodes(w.BinWriter, opcode.ASSERT)
|
|
}
|
|
emit.AppCall(w.BinWriter, neo.Hash, "setRegisterPrice", callflag.States, regPrice)
|
|
if w.Err != nil {
|
|
panic(fmt.Sprintf("BUG: %v", w.Err))
|
|
}
|
|
|
|
signers := []actor.SignerAccount{{
|
|
Signer: c.GetSigner(false, c.CommitteeAcc),
|
|
Account: c.CommitteeAcc,
|
|
}}
|
|
for _, acc := range c.Accounts[start:end] {
|
|
signers = append(signers, actor.SignerAccount{
|
|
Signer: transaction.Signer{
|
|
Account: acc.Contract.ScriptHash(),
|
|
Scopes: transaction.CustomContracts,
|
|
AllowedContracts: []util.Uint160{neo.Hash},
|
|
},
|
|
Account: acc,
|
|
})
|
|
}
|
|
|
|
act, err := actor.New(c.Client, signers)
|
|
if err != nil {
|
|
return fmt.Errorf("can't create actor: %w", err)
|
|
}
|
|
tx, err := act.MakeRun(w.Bytes())
|
|
if err != nil {
|
|
return fmt.Errorf("can't create tx: %w", err)
|
|
}
|
|
if err := c.MultiSign(tx, constants.CommitteeAccountName); err != nil {
|
|
return fmt.Errorf("can't sign a transaction: %w", err)
|
|
}
|
|
|
|
network := c.CommitteeAct.GetNetwork()
|
|
for _, acc := range c.Accounts[start:end] {
|
|
if err := acc.SignTx(network, tx); err != nil {
|
|
return fmt.Errorf("can't sign a transaction: %w", err)
|
|
}
|
|
}
|
|
|
|
return c.SendTx(tx, c.Command, true)
|
|
}
|
|
|
|
func registerCandidates(c *helper.InitializeContext) error {
|
|
cc, err := unwrap.Array(c.ReadOnlyInvoker.Call(neo.Hash, "getCandidates"))
|
|
if err != nil {
|
|
return fmt.Errorf("`getCandidates`: %w", err)
|
|
}
|
|
|
|
need := len(c.Accounts)
|
|
have := len(cc)
|
|
|
|
if need == have {
|
|
c.Command.Println("Candidates are already registered.")
|
|
return nil
|
|
}
|
|
|
|
// Register candidates in batches in order to overcome the signers amount limit.
|
|
// See: https://github.com/nspcc-dev/neo-go/blob/master/pkg/core/transaction/transaction.go#L27
|
|
for i := 0; i < need; i += registerBatchSize {
|
|
start, end := i, i+registerBatchSize
|
|
if end > need {
|
|
end = need
|
|
}
|
|
// This check is sound because transactions are accepted/rejected atomically.
|
|
if have >= end {
|
|
continue
|
|
}
|
|
if err := registerCandidateRange(c, start, end); err != nil {
|
|
return fmt.Errorf("registering candidates %d..%d: %q", start, end-1, err)
|
|
}
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func transferNEOToAlphabetContracts(c *helper.InitializeContext) error {
|
|
neoHash := neo.Hash
|
|
|
|
ok, err := transferNEOFinished(c, neoHash)
|
|
if ok || err != nil {
|
|
return err
|
|
}
|
|
|
|
cs := c.GetContract(constants.AlphabetContract)
|
|
amount := initialAlphabetNEOAmount / len(c.Wallets)
|
|
|
|
bw := io.NewBufBinWriter()
|
|
for _, acc := range c.Accounts {
|
|
h := state.CreateContractHash(acc.Contract.ScriptHash(), cs.NEF.Checksum, cs.Manifest.Name)
|
|
emit.AppCall(bw.BinWriter, neoHash, "transfer", callflag.All,
|
|
c.CommitteeAcc.Contract.ScriptHash(), h, int64(amount), nil)
|
|
emit.Opcodes(bw.BinWriter, opcode.ASSERT)
|
|
}
|
|
|
|
if err := c.SendCommitteeTx(bw.Bytes(), false); err != nil {
|
|
return err
|
|
}
|
|
|
|
return c.AwaitTx()
|
|
}
|
|
|
|
func transferNEOFinished(c *helper.InitializeContext, neoHash util.Uint160) (bool, error) {
|
|
r := nep17.NewReader(c.ReadOnlyInvoker, neoHash)
|
|
bal, err := r.BalanceOf(c.CommitteeAcc.Contract.ScriptHash())
|
|
return bal.Cmp(big.NewInt(native.NEOTotalSupply)) == -1, err
|
|
}
|
|
|
|
var errGetPriceInvalid = errors.New("`getRegisterPrice`: invalid response")
|
|
|
|
func getCandidateRegisterPrice(c *helper.InitializeContext) (int64, error) {
|
|
switch c.Client.(type) {
|
|
case *rpcclient.Client:
|
|
inv := invoker.New(c.Client, nil)
|
|
reader := neo.NewReader(inv)
|
|
return reader.GetRegisterPrice()
|
|
default:
|
|
neoHash := neo.Hash
|
|
res, err := helper.InvokeFunction(c.Client, neoHash, "getRegisterPrice", nil, nil)
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
if len(res.Stack) == 0 {
|
|
return 0, errGetPriceInvalid
|
|
}
|
|
bi, err := res.Stack[0].TryInteger()
|
|
if err != nil || !bi.IsInt64() {
|
|
return 0, errGetPriceInvalid
|
|
}
|
|
return bi.Int64(), nil
|
|
}
|
|
}
|