frostfs-node/cmd/neofs-adm/internal/modules/morph/initialize_register.go
Evgenii Stratonikov 37f9d083fb [#722] neofs-adm: Hide N3 client behind an interface
Signed-off-by: Evgenii Stratonikov <evgeniy@nspcc.ru>
2022-05-16 15:07:31 +03:00

94 lines
2.7 KiB
Go

package morph
import (
"fmt"
"github.com/nspcc-dev/neo-go/pkg/core/native"
"github.com/nspcc-dev/neo-go/pkg/core/native/nativenames"
"github.com/nspcc-dev/neo-go/pkg/core/state"
"github.com/nspcc-dev/neo-go/pkg/io"
"github.com/nspcc-dev/neo-go/pkg/rpc/client"
"github.com/nspcc-dev/neo-go/pkg/smartcontract"
"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"
"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/vm/stackitem"
)
// initialAlphabetNEOAmount represents the total amount of GAS distributed between alphabet nodes.
const initialAlphabetNEOAmount = native.NEOTotalSupply
func (c *initializeContext) registerCandidates() error {
neoHash := c.nativeHash(nativenames.Neo)
res, err := c.Client.InvokeFunction(neoHash, "getCandidates", []smartcontract.Parameter{}, nil)
if err != nil {
return err
}
if res.State == vm.HaltState.String() && len(res.Stack) > 0 {
arr, ok := res.Stack[0].Value().([]stackitem.Item)
if ok && len(arr) > 0 {
return nil
}
}
regPrice, err := getCandidateRegisterPrice(c.Client)
if err != nil {
return fmt.Errorf("can't fetch registration price: %w", err)
}
sysGas := regPrice + native.GASFactor // + 1 GAS
for _, acc := range c.Accounts {
w := io.NewBufBinWriter()
emit.AppCall(w.BinWriter, neoHash, "registerCandidate", callflag.States, acc.PrivateKey().PublicKey().Bytes())
emit.Opcodes(w.BinWriter, opcode.ASSERT)
if err := c.sendSingleTx(w.Bytes(), sysGas, acc); err != nil {
return err
}
}
return c.awaitTx()
}
func (c *initializeContext) transferNEOToAlphabetContracts() error {
neoHash := c.nativeHash(nativenames.Neo)
ok, err := c.transferNEOFinished(neoHash)
if ok || err != nil {
return err
}
cs := c.getContract(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(), -1, false); err != nil {
return err
}
return c.awaitTx()
}
func (c *initializeContext) transferNEOFinished(neoHash util.Uint160) (bool, error) {
bal, err := c.Client.NEP17BalanceOf(neoHash, c.CommitteeAcc.Contract.ScriptHash())
return bal < native.NEOTotalSupply, err
}
func getCandidateRegisterPrice(c Client) (int64, error) {
switch ct := c.(type) {
case *client.Client:
return ct.GetCandidateRegisterPrice()
default:
panic("unimplemented")
}
}