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"
|
2023-10-24 10:13:49 +00:00
|
|
|
"math/big"
|
2021-07-22 07:57:11 +00:00
|
|
|
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/native"
|
|
|
|
"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"
|
2022-07-28 16:22:32 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/rpcclient"
|
2023-04-12 10:58:54 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/rpcclient/invoker"
|
2022-11-23 14:44:03 +00:00
|
|
|
"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-09-05 12:45:38 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/rpcclient/unwrap"
|
2021-07-22 07:57:11 +00:00
|
|
|
"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"
|
|
|
|
)
|
|
|
|
|
2022-04-21 11:28:05 +00:00
|
|
|
// initialAlphabetNEOAmount represents the total amount of GAS distributed between alphabet nodes.
|
2023-05-15 11:46:17 +00:00
|
|
|
const (
|
|
|
|
initialAlphabetNEOAmount = native.NEOTotalSupply
|
|
|
|
registerBatchSize = transaction.MaxAttributes - 1
|
|
|
|
)
|
2021-07-22 07:57:11 +00:00
|
|
|
|
2023-05-15 11:46:17 +00:00
|
|
|
func (c *initializeContext) registerCandidateRange(start, end int) error {
|
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()
|
2023-05-15 11:46:17 +00:00
|
|
|
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())
|
2021-07-22 07:57:11 +00:00
|
|
|
emit.Opcodes(w.BinWriter, opcode.ASSERT)
|
2022-07-18 15:21:31 +00:00
|
|
|
}
|
2023-05-15 11:46:17 +00:00
|
|
|
emit.AppCall(w.BinWriter, neo.Hash, "setRegisterPrice", callflag.States, regPrice)
|
2022-07-18 15:21:31 +00:00
|
|
|
if w.Err != nil {
|
|
|
|
panic(fmt.Sprintf("BUG: %v", w.Err))
|
|
|
|
}
|
|
|
|
|
2022-07-28 16:22:32 +00:00
|
|
|
signers := []rpcclient.SignerAccount{{
|
2022-10-21 14:35:15 +00:00
|
|
|
Signer: c.getSigner(false, c.CommitteeAcc),
|
2022-07-18 15:21:31 +00:00
|
|
|
Account: c.CommitteeAcc,
|
|
|
|
}}
|
2023-05-15 11:46:17 +00:00
|
|
|
for _, acc := range c.Accounts[start:end] {
|
2022-07-28 16:22:32 +00:00
|
|
|
signers = append(signers, rpcclient.SignerAccount{
|
2022-07-18 15:21:31 +00:00
|
|
|
Signer: transaction.Signer{
|
2023-05-15 11:46:17 +00:00
|
|
|
Account: acc.Contract.ScriptHash(),
|
2022-07-18 15:21:31 +00:00
|
|
|
Scopes: transaction.CustomContracts,
|
2023-05-15 11:46:17 +00:00
|
|
|
AllowedContracts: []util.Uint160{neo.Hash},
|
2022-07-18 15:21:31 +00:00
|
|
|
},
|
2023-05-15 11:46:17 +00:00
|
|
|
Account: acc,
|
2022-07-18 15:21:31 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
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-09-05 12:45:38 +00:00
|
|
|
network := c.CommitteeAct.GetNetwork()
|
2023-05-15 11:46:17 +00:00
|
|
|
for _, acc := range c.Accounts[start:end] {
|
|
|
|
if err := acc.SignTx(network, tx); err != nil {
|
2022-07-18 15:21:31 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2023-05-15 11:46:17 +00:00
|
|
|
func (c *initializeContext) registerCandidates() 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 := c.registerCandidateRange(start, end); err != nil {
|
|
|
|
return fmt.Errorf("registering candidates %d..%d: %q", start, end-1, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-07-22 07:57:11 +00:00
|
|
|
func (c *initializeContext) transferNEOToAlphabetContracts() error {
|
2022-11-23 14:44:03 +00:00
|
|
|
neoHash := neo.Hash
|
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-08-29 19:31:32 +00:00
|
|
|
if err := c.sendCommitteeTx(bw.Bytes(), 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) {
|
2023-10-24 10:13:49 +00:00
|
|
|
r := nep17.NewReader(c.ReadOnlyInvoker, neoHash)
|
|
|
|
bal, err := r.BalanceOf(c.CommitteeAcc.Contract.ScriptHash())
|
|
|
|
return bal.Cmp(big.NewInt(native.NEOTotalSupply)) == -1, err
|
2021-07-22 07:57:11 +00:00
|
|
|
}
|
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) {
|
2023-04-12 10:58:54 +00:00
|
|
|
switch c.Client.(type) {
|
2022-07-28 16:22:32 +00:00
|
|
|
case *rpcclient.Client:
|
2023-04-12 10:58:54 +00:00
|
|
|
inv := invoker.New(c.Client, nil)
|
|
|
|
reader := neo.NewReader(inv)
|
|
|
|
return reader.GetRegisterPrice()
|
2022-04-07 08:10:35 +00:00
|
|
|
default:
|
2022-11-23 14:44:03 +00:00
|
|
|
neoHash := neo.Hash
|
2022-04-07 12:47:13 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|