2022-10-05 06:44:10 +00:00
|
|
|
package wallet_test
|
2020-09-01 13:55:00 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/hex"
|
|
|
|
"math/big"
|
2021-05-28 15:05:19 +00:00
|
|
|
"strconv"
|
2020-09-01 13:55:00 +00:00
|
|
|
"testing"
|
|
|
|
|
2022-10-05 06:44:10 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/internal/testcli"
|
2020-09-01 13:55:00 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Register standby validator and vote for it.
|
|
|
|
// We don't create a new account here, because chain will
|
|
|
|
// stop working after validator will change.
|
|
|
|
func TestRegisterCandidate(t *testing.T) {
|
2022-10-05 06:44:10 +00:00
|
|
|
e := testcli.NewExecutor(t, true)
|
2020-09-01 13:55:00 +00:00
|
|
|
|
2022-10-05 06:44:10 +00:00
|
|
|
validatorAddress := testcli.ValidatorPriv.Address()
|
|
|
|
validatorPublic := testcli.ValidatorPriv.PublicKey()
|
|
|
|
validatorHex := hex.EncodeToString(validatorPublic.Bytes())
|
2021-07-22 11:40:18 +00:00
|
|
|
|
2020-09-01 13:55:00 +00:00
|
|
|
e.In.WriteString("one\r")
|
2020-11-24 08:14:25 +00:00
|
|
|
e.Run(t, "neo-go", "wallet", "nep17", "multitransfer",
|
2020-10-14 15:13:20 +00:00
|
|
|
"--rpc-endpoint", "http://"+e.RPC.Addr,
|
2022-10-05 06:44:10 +00:00
|
|
|
"--wallet", testcli.ValidatorWallet,
|
|
|
|
"--from", testcli.ValidatorAddr,
|
2021-09-15 09:40:30 +00:00
|
|
|
"--force",
|
2022-10-05 06:44:10 +00:00
|
|
|
"NEO:"+validatorAddress+":10",
|
|
|
|
"GAS:"+validatorAddress+":10000")
|
|
|
|
e.CheckTxPersisted(t)
|
2020-09-01 13:55:00 +00:00
|
|
|
|
2021-07-22 11:40:18 +00:00
|
|
|
e.Run(t, "neo-go", "query", "committee",
|
|
|
|
"--rpc-endpoint", "http://"+e.RPC.Addr)
|
2022-10-05 06:44:10 +00:00
|
|
|
e.CheckNextLine(t, "^\\s*"+validatorHex)
|
2021-07-22 11:40:18 +00:00
|
|
|
|
|
|
|
e.Run(t, "neo-go", "query", "candidates",
|
|
|
|
"--rpc-endpoint", "http://"+e.RPC.Addr)
|
2022-10-05 06:44:10 +00:00
|
|
|
e.CheckNextLine(t, "^\\s*Key.+$") // Header.
|
|
|
|
e.CheckEOF(t)
|
2021-07-22 11:40:18 +00:00
|
|
|
|
2021-04-16 09:03:03 +00:00
|
|
|
// missing address
|
|
|
|
e.RunWithError(t, "neo-go", "wallet", "candidate", "register",
|
|
|
|
"--rpc-endpoint", "http://"+e.RPC.Addr,
|
2022-10-05 06:44:10 +00:00
|
|
|
"--wallet", testcli.ValidatorWallet)
|
2021-04-16 09:03:03 +00:00
|
|
|
|
2022-08-05 10:32:37 +00:00
|
|
|
// additional parameter
|
|
|
|
e.RunWithError(t, "neo-go", "wallet", "candidate", "register",
|
|
|
|
"--rpc-endpoint", "http://"+e.RPC.Addr,
|
2022-10-05 06:44:10 +00:00
|
|
|
"--wallet", testcli.ValidatorWallet,
|
|
|
|
"--address", validatorAddress,
|
2022-08-05 10:32:37 +00:00
|
|
|
"error")
|
|
|
|
|
2020-09-01 13:55:00 +00:00
|
|
|
e.In.WriteString("one\r")
|
|
|
|
e.Run(t, "neo-go", "wallet", "candidate", "register",
|
2020-10-14 15:13:20 +00:00
|
|
|
"--rpc-endpoint", "http://"+e.RPC.Addr,
|
2022-10-05 06:44:10 +00:00
|
|
|
"--wallet", testcli.ValidatorWallet,
|
|
|
|
"--address", validatorAddress)
|
|
|
|
e.CheckTxPersisted(t)
|
2020-09-01 13:55:00 +00:00
|
|
|
|
|
|
|
vs, err := e.Chain.GetEnrollments()
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, 1, len(vs))
|
2022-10-05 06:44:10 +00:00
|
|
|
require.Equal(t, validatorPublic, vs[0].Key)
|
2020-09-01 13:55:00 +00:00
|
|
|
require.Equal(t, big.NewInt(0), vs[0].Votes)
|
|
|
|
|
2021-05-28 12:15:37 +00:00
|
|
|
t.Run("VoteUnvote", func(t *testing.T) {
|
2022-08-05 10:32:37 +00:00
|
|
|
// positional instead of a flag.
|
|
|
|
e.RunWithError(t, "neo-go", "wallet", "candidate", "vote",
|
|
|
|
"--rpc-endpoint", "http://"+e.RPC.Addr,
|
2022-10-05 06:44:10 +00:00
|
|
|
"--wallet", testcli.ValidatorWallet,
|
|
|
|
"--address", validatorAddress,
|
2022-08-05 10:32:37 +00:00
|
|
|
validatorHex) // not "--candidate hex", but "hex".
|
|
|
|
|
2020-09-01 13:55:00 +00:00
|
|
|
e.In.WriteString("one\r")
|
|
|
|
e.Run(t, "neo-go", "wallet", "candidate", "vote",
|
2020-10-14 15:13:20 +00:00
|
|
|
"--rpc-endpoint", "http://"+e.RPC.Addr,
|
2022-10-05 06:44:10 +00:00
|
|
|
"--wallet", testcli.ValidatorWallet,
|
|
|
|
"--address", validatorAddress,
|
2021-07-22 11:40:18 +00:00
|
|
|
"--candidate", validatorHex)
|
2022-10-05 06:44:10 +00:00
|
|
|
_, index := e.CheckTxPersisted(t)
|
2020-09-01 13:55:00 +00:00
|
|
|
|
|
|
|
vs, err = e.Chain.GetEnrollments()
|
|
|
|
require.Equal(t, 1, len(vs))
|
2022-10-05 06:44:10 +00:00
|
|
|
require.Equal(t, validatorPublic, vs[0].Key)
|
|
|
|
b, _ := e.Chain.GetGoverningTokenBalance(testcli.ValidatorPriv.GetScriptHash())
|
2020-09-01 13:55:00 +00:00
|
|
|
require.Equal(t, b, vs[0].Votes)
|
2021-05-28 12:15:37 +00:00
|
|
|
|
2021-07-22 11:40:18 +00:00
|
|
|
e.Run(t, "neo-go", "query", "committee",
|
|
|
|
"--rpc-endpoint", "http://"+e.RPC.Addr)
|
2022-10-05 06:44:10 +00:00
|
|
|
e.CheckNextLine(t, "^\\s*"+validatorHex)
|
2021-07-22 11:40:18 +00:00
|
|
|
|
|
|
|
e.Run(t, "neo-go", "query", "candidates",
|
|
|
|
"--rpc-endpoint", "http://"+e.RPC.Addr)
|
2022-10-05 06:44:10 +00:00
|
|
|
e.CheckNextLine(t, "^\\s*Key.+$") // Header.
|
|
|
|
e.CheckNextLine(t, "^\\s*"+validatorHex+"\\s*"+b.String()+"\\s*true\\s*true$")
|
|
|
|
e.CheckEOF(t)
|
2021-07-22 11:40:18 +00:00
|
|
|
|
2021-05-28 15:05:19 +00:00
|
|
|
// check state
|
2021-07-22 11:57:29 +00:00
|
|
|
e.Run(t, "neo-go", "query", "voter",
|
2021-05-28 15:05:19 +00:00
|
|
|
"--rpc-endpoint", "http://"+e.RPC.Addr,
|
2022-10-05 06:44:10 +00:00
|
|
|
validatorAddress)
|
|
|
|
e.CheckNextLine(t, "^\\s*Voted:\\s+"+validatorHex+"\\s+\\("+validatorAddress+"\\)$")
|
|
|
|
e.CheckNextLine(t, "^\\s*Amount\\s*:\\s*"+b.String()+"$")
|
|
|
|
e.CheckNextLine(t, "^\\s*Block\\s*:\\s*"+strconv.FormatUint(uint64(index), 10))
|
|
|
|
e.CheckEOF(t)
|
2021-05-28 15:05:19 +00:00
|
|
|
|
2021-05-28 12:15:37 +00:00
|
|
|
// unvote
|
|
|
|
e.In.WriteString("one\r")
|
|
|
|
e.Run(t, "neo-go", "wallet", "candidate", "vote",
|
|
|
|
"--rpc-endpoint", "http://"+e.RPC.Addr,
|
2022-10-05 06:44:10 +00:00
|
|
|
"--wallet", testcli.ValidatorWallet,
|
|
|
|
"--address", validatorAddress)
|
|
|
|
_, index = e.CheckTxPersisted(t)
|
2021-05-28 12:15:37 +00:00
|
|
|
|
|
|
|
vs, err = e.Chain.GetEnrollments()
|
|
|
|
require.Equal(t, 1, len(vs))
|
2022-10-05 06:44:10 +00:00
|
|
|
require.Equal(t, validatorPublic, vs[0].Key)
|
2021-05-28 12:15:37 +00:00
|
|
|
require.Equal(t, big.NewInt(0), vs[0].Votes)
|
2021-05-28 15:05:19 +00:00
|
|
|
|
|
|
|
// check state
|
2021-07-22 11:57:29 +00:00
|
|
|
e.Run(t, "neo-go", "query", "voter",
|
2021-05-28 15:05:19 +00:00
|
|
|
"--rpc-endpoint", "http://"+e.RPC.Addr,
|
2022-10-05 06:44:10 +00:00
|
|
|
validatorAddress)
|
|
|
|
e.CheckNextLine(t, "^\\s*Voted:\\s+"+"null") // no vote.
|
|
|
|
e.CheckNextLine(t, "^\\s*Amount\\s*:\\s*"+b.String()+"$")
|
|
|
|
e.CheckNextLine(t, "^\\s*Block\\s*:\\s*"+strconv.FormatUint(uint64(index), 10))
|
|
|
|
e.CheckEOF(t)
|
2020-09-01 13:55:00 +00:00
|
|
|
})
|
|
|
|
|
2021-04-16 09:03:03 +00:00
|
|
|
// missing address
|
|
|
|
e.RunWithError(t, "neo-go", "wallet", "candidate", "unregister",
|
|
|
|
"--rpc-endpoint", "http://"+e.RPC.Addr,
|
2022-10-05 06:44:10 +00:00
|
|
|
"--wallet", testcli.ValidatorWallet)
|
2022-08-05 10:32:37 +00:00
|
|
|
// additional argument
|
|
|
|
e.RunWithError(t, "neo-go", "wallet", "candidate", "unregister",
|
|
|
|
"--rpc-endpoint", "http://"+e.RPC.Addr,
|
2022-10-05 06:44:10 +00:00
|
|
|
"--wallet", testcli.ValidatorWallet,
|
|
|
|
"--address", validatorAddress,
|
2022-08-05 10:32:37 +00:00
|
|
|
"argument")
|
2021-04-16 09:03:03 +00:00
|
|
|
|
2020-09-01 13:55:00 +00:00
|
|
|
e.In.WriteString("one\r")
|
|
|
|
e.Run(t, "neo-go", "wallet", "candidate", "unregister",
|
2020-10-14 15:13:20 +00:00
|
|
|
"--rpc-endpoint", "http://"+e.RPC.Addr,
|
2022-10-05 06:44:10 +00:00
|
|
|
"--wallet", testcli.ValidatorWallet,
|
|
|
|
"--address", validatorAddress)
|
|
|
|
e.CheckTxPersisted(t)
|
2020-09-01 13:55:00 +00:00
|
|
|
|
|
|
|
vs, err = e.Chain.GetEnrollments()
|
|
|
|
require.Equal(t, 0, len(vs))
|
2021-05-28 15:05:19 +00:00
|
|
|
|
2021-07-22 11:57:29 +00:00
|
|
|
// query voter: missing address
|
|
|
|
e.RunWithError(t, "neo-go", "query", "voter")
|
2022-08-05 10:32:37 +00:00
|
|
|
// Excessive parameters.
|
2022-10-05 06:44:10 +00:00
|
|
|
e.RunWithError(t, "neo-go", "query", "voter", "--rpc-endpoint", "http://"+e.RPC.Addr, validatorAddress, validatorAddress)
|
2022-08-05 10:32:37 +00:00
|
|
|
e.RunWithError(t, "neo-go", "query", "committee", "--rpc-endpoint", "http://"+e.RPC.Addr, "something")
|
|
|
|
e.RunWithError(t, "neo-go", "query", "candidates", "--rpc-endpoint", "http://"+e.RPC.Addr, "something")
|
2020-09-01 13:55:00 +00:00
|
|
|
}
|