2021-07-22 07:57:11 +00:00
|
|
|
package morph
|
|
|
|
|
|
|
|
import (
|
2022-04-07 12:47:13 +00:00
|
|
|
"errors"
|
2021-07-22 07:57:11 +00:00
|
|
|
"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"
|
2022-07-18 15:21:31 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/transaction"
|
2021-07-22 07:57:11 +00:00
|
|
|
"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/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"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/vm/stackitem"
|
2022-07-18 08:33:53 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/vm/vmstate"
|
2021-07-22 07:57:11 +00:00
|
|
|
)
|
|
|
|
|
2022-04-21 11:28:05 +00:00
|
|
|
// initialAlphabetNEOAmount represents the total amount of GAS distributed between alphabet nodes.
|
2021-07-22 07:57:11 +00:00
|
|
|
const initialAlphabetNEOAmount = native.NEOTotalSupply
|
|
|
|
|
|
|
|
func (c *initializeContext) registerCandidates() error {
|
2021-07-29 13:35:57 +00:00
|
|
|
neoHash := c.nativeHash(nativenames.Neo)
|
2021-07-22 07:57:11 +00:00
|
|
|
|
2022-04-07 12:47:13 +00:00
|
|
|
res, err := invokeFunction(c.Client, neoHash, "getCandidates", nil, nil)
|
2021-07-22 07:57:11 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-07-18 08:33:53 +00:00
|
|
|
if res.State == vmstate.Halt.String() && len(res.Stack) > 0 {
|
2021-07-22 07:57:11 +00:00
|
|
|
arr, ok := res.Stack[0].Value().([]stackitem.Item)
|
|
|
|
if ok && len(arr) > 0 {
|
2022-07-18 14:52:41 +00:00
|
|
|
c.Command.Println("Candidates are already registered.")
|
2021-07-22 07:57:11 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-07 12:47:13 +00:00
|
|
|
regPrice, err := c.getCandidateRegisterPrice()
|
2021-07-22 07:57:11 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("can't fetch registration price: %w", err)
|
|
|
|
}
|
|
|
|
|
2022-07-18 15:21:31 +00:00
|
|
|
w := io.NewBufBinWriter()
|
|
|
|
emit.AppCall(w.BinWriter, neoHash, "setRegisterPrice", callflag.States, 1)
|
2021-07-30 11:26:44 +00:00
|
|
|
for _, acc := range c.Accounts {
|
2021-07-22 07:57:11 +00:00
|
|
|
emit.AppCall(w.BinWriter, neoHash, "registerCandidate", callflag.States, acc.PrivateKey().PublicKey().Bytes())
|
|
|
|
emit.Opcodes(w.BinWriter, opcode.ASSERT)
|
2022-07-18 15:21:31 +00:00
|
|
|
}
|
|
|
|
emit.AppCall(w.BinWriter, neoHash, "setRegisterPrice", callflag.States, regPrice)
|
|
|
|
if w.Err != nil {
|
|
|
|
panic(fmt.Sprintf("BUG: %v", w.Err))
|
|
|
|
}
|
|
|
|
|
|
|
|
signers := []client.SignerAccount{{
|
|
|
|
Signer: c.getSigner(false),
|
|
|
|
Account: c.CommitteeAcc,
|
|
|
|
}}
|
|
|
|
for i := range c.Accounts {
|
|
|
|
signers = append(signers, client.SignerAccount{
|
|
|
|
Signer: transaction.Signer{
|
|
|
|
Account: c.Accounts[i].Contract.ScriptHash(),
|
|
|
|
Scopes: transaction.CustomContracts,
|
|
|
|
AllowedContracts: []util.Uint160{neoHash},
|
|
|
|
},
|
|
|
|
Account: c.Accounts[i],
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
tx, err := c.Client.CreateTxFromScript(w.Bytes(), c.CommitteeAcc, -1, 0, signers)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("can't create tx: %w", err)
|
|
|
|
}
|
|
|
|
if err := c.multiSign(tx, committeeAccountName); err != nil {
|
|
|
|
return fmt.Errorf("can't sign a transaction: %w", err)
|
|
|
|
}
|
2021-07-22 07:57:11 +00:00
|
|
|
|
2022-07-18 15:21:31 +00:00
|
|
|
network, _ := c.Client.GetNetwork()
|
|
|
|
for i := range c.Accounts {
|
|
|
|
if err := c.Accounts[i].SignTx(network, tx); err != nil {
|
|
|
|
return fmt.Errorf("can't sign a transaction: %w", err)
|
2021-07-22 07:57:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-18 15:21:31 +00:00
|
|
|
return c.sendTx(tx, c.Command, true)
|
2021-07-22 07:57:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *initializeContext) transferNEOToAlphabetContracts() error {
|
2021-07-29 13:35:57 +00:00
|
|
|
neoHash := c.nativeHash(nativenames.Neo)
|
2021-07-22 07:57:11 +00:00
|
|
|
|
|
|
|
ok, err := c.transferNEOFinished(neoHash)
|
|
|
|
if ok || err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-10-25 13:19:56 +00:00
|
|
|
cs := c.getContract(alphabetContract)
|
2021-07-22 07:57:11 +00:00
|
|
|
amount := initialAlphabetNEOAmount / len(c.Wallets)
|
|
|
|
|
|
|
|
bw := io.NewBufBinWriter()
|
2021-07-30 11:26:44 +00:00
|
|
|
for _, acc := range c.Accounts {
|
2021-07-22 07:57:11 +00:00
|
|
|
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)
|
2022-04-05 14:05:37 +00:00
|
|
|
emit.Opcodes(bw.BinWriter, opcode.ASSERT)
|
2021-07-22 07:57:11 +00:00
|
|
|
}
|
|
|
|
|
2022-04-05 14:21:11 +00:00
|
|
|
if err := c.sendCommitteeTx(bw.Bytes(), -1, false); err != nil {
|
2021-07-22 07:57:11 +00:00
|
|
|
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
|
|
|
|
}
|
2022-04-07 08:10:35 +00:00
|
|
|
|
2022-04-07 12:47:13 +00:00
|
|
|
var errGetPriceInvalid = errors.New("`getRegisterPrice`: invalid response")
|
|
|
|
|
|
|
|
func (c *initializeContext) getCandidateRegisterPrice() (int64, error) {
|
|
|
|
switch ct := c.Client.(type) {
|
2022-04-07 08:10:35 +00:00
|
|
|
case *client.Client:
|
|
|
|
return ct.GetCandidateRegisterPrice()
|
|
|
|
default:
|
2022-04-07 12:47:13 +00:00
|
|
|
neoHash := c.nativeHash(nativenames.Neo)
|
|
|
|
res, err := 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
|
2022-04-07 08:10:35 +00:00
|
|
|
}
|
|
|
|
}
|