diff --git a/cli/candidate_test.go b/cli/candidate_test.go new file mode 100644 index 000000000..4600e2644 --- /dev/null +++ b/cli/candidate_test.go @@ -0,0 +1,65 @@ +package main + +import ( + "encoding/hex" + "math/big" + "testing" + + "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) { + e := newExecutor(t, true) + defer e.Close(t) + + e.In.WriteString("one\r") + e.Run(t, "neo-go", "wallet", "nep5", "multitransfer", + "--unittest", "--rpc-endpoint", "http://"+e.RPC.Addr, + "--wallet", validatorWallet, + "--from", validatorAddr, + "neo:"+validatorPriv.Address()+":10", + "gas:"+validatorPriv.Address()+":100") + e.checkTxPersisted(t) + + e.In.WriteString("one\r") + e.Run(t, "neo-go", "wallet", "candidate", "register", + "--unittest", "--rpc-endpoint", "http://"+e.RPC.Addr, + "--wallet", validatorWallet, + "--address", validatorPriv.Address()) + e.checkTxPersisted(t) + + vs, err := e.Chain.GetEnrollments() + require.NoError(t, err) + require.Equal(t, 1, len(vs)) + require.Equal(t, validatorPriv.PublicKey(), vs[0].Key) + require.Equal(t, big.NewInt(0), vs[0].Votes) + + t.Run("Vote", func(t *testing.T) { + e.In.WriteString("one\r") + e.Run(t, "neo-go", "wallet", "candidate", "vote", + "--unittest", "--rpc-endpoint", "http://"+e.RPC.Addr, + "--wallet", validatorWallet, + "--address", validatorPriv.Address(), + "--candidate", hex.EncodeToString(validatorPriv.PublicKey().Bytes())) + e.checkTxPersisted(t) + + vs, err = e.Chain.GetEnrollments() + require.Equal(t, 1, len(vs)) + require.Equal(t, validatorPriv.PublicKey(), vs[0].Key) + b, _ := e.Chain.GetGoverningTokenBalance(validatorPriv.GetScriptHash()) + require.Equal(t, b, vs[0].Votes) + }) + + e.In.WriteString("one\r") + e.Run(t, "neo-go", "wallet", "candidate", "unregister", + "--unittest", "--rpc-endpoint", "http://"+e.RPC.Addr, + "--wallet", validatorWallet, + "--address", validatorPriv.Address()) + e.checkTxPersisted(t) + + vs, err = e.Chain.GetEnrollments() + require.Equal(t, 0, len(vs)) +} diff --git a/cli/wallet/validator.go b/cli/wallet/validator.go index 0d9d7ad97..671f16d8f 100644 --- a/cli/wallet/validator.go +++ b/cli/wallet/validator.go @@ -6,6 +6,7 @@ import ( "github.com/nspcc-dev/neo-go/cli/flags" "github.com/nspcc-dev/neo-go/cli/input" "github.com/nspcc-dev/neo-go/cli/options" + "github.com/nspcc-dev/neo-go/pkg/core/transaction" "github.com/nspcc-dev/neo-go/pkg/crypto/keys" "github.com/nspcc-dev/neo-go/pkg/encoding/address" "github.com/nspcc-dev/neo-go/pkg/io" @@ -81,6 +82,7 @@ func handleCandidate(ctx *cli.Context, method string) error { if err != nil { return cli.NewExitError(err, 1) } + defer wall.Close() addrFlag := ctx.Generic("address").(*flags.Address) addr := addrFlag.Uint160() @@ -101,7 +103,10 @@ func handleCandidate(ctx *cli.Context, method string) error { w := io.NewBufBinWriter() emit.AppCallWithOperationAndArgs(w.BinWriter, client.NeoContractHash, method, acc.PrivateKey().PublicKey().Bytes()) emit.Opcode(w.BinWriter, opcode.ASSERT) - tx, err := c.CreateTxFromScript(w.Bytes(), acc, -1, int64(gas)) + tx, err := c.CreateTxFromScript(w.Bytes(), acc, -1, int64(gas), transaction.Signer{ + Account: acc.Contract.ScriptHash(), + Scopes: transaction.CalledByEntry, + }) if err != nil { return cli.NewExitError(err, 1) } else if err = acc.SignTx(tx); err != nil { @@ -121,6 +126,7 @@ func handleVote(ctx *cli.Context) error { if err != nil { return cli.NewExitError(err, 1) } + defer wall.Close() addrFlag := ctx.Generic("address").(*flags.Address) addr := addrFlag.Uint160() @@ -156,7 +162,10 @@ func handleVote(ctx *cli.Context) error { emit.AppCallWithOperationAndArgs(w.BinWriter, client.NeoContractHash, "vote", addr.BytesBE(), pubArg) emit.Opcode(w.BinWriter, opcode.ASSERT) - tx, err := c.CreateTxFromScript(w.Bytes(), acc, -1, int64(gas)) + tx, err := c.CreateTxFromScript(w.Bytes(), acc, -1, int64(gas), transaction.Signer{ + Account: acc.Contract.ScriptHash(), + Scopes: transaction.CalledByEntry, + }) if err != nil { return cli.NewExitError(err, 1) }