cli: add tests for wallet claim

Fix a NEO contract hash used in tx.
This commit is contained in:
Evgenii Stratonikov 2020-09-02 11:43:25 +03:00
parent dff2ac1387
commit 583ef546f9
3 changed files with 30 additions and 11 deletions

View file

@ -116,15 +116,15 @@ func (e *executor) Close(t *testing.T) {
// GetTransaction returns tx with hash h after it has persisted. // GetTransaction returns tx with hash h after it has persisted.
// If it is in mempool, we can just wait for the next block, otherwise // If it is in mempool, we can just wait for the next block, otherwise
// it must be already in chain. 1 second is time per block in a unittest chain. // it must be already in chain. 1 second is time per block in a unittest chain.
func (e *executor) GetTransaction(t *testing.T, h util.Uint256) *transaction.Transaction { func (e *executor) GetTransaction(t *testing.T, h util.Uint256) (*transaction.Transaction, uint32) {
var tx *transaction.Transaction var tx *transaction.Transaction
require.Eventually(t, func() bool {
var height uint32 var height uint32
require.Eventually(t, func() bool {
var err error var err error
tx, height, err = e.Chain.GetTransaction(h) tx, height, err = e.Chain.GetTransaction(h)
return err == nil && height != 0 return err == nil && height != 0
}, time.Second*2, time.Millisecond*100, "too long time waiting for block") }, time.Second*2, time.Millisecond*100, "too long time waiting for block")
return tx return tx, height
} }
func (e *executor) checkNextLine(t *testing.T, expected string) { func (e *executor) checkNextLine(t *testing.T, expected string) {
@ -180,7 +180,7 @@ func (e *executor) run(args ...string) error {
return e.CLI.Run(args) return e.CLI.Run(args)
} }
func (e *executor) checkTxPersisted(t *testing.T) { func (e *executor) checkTxPersisted(t *testing.T) (*transaction.Transaction, uint32) {
line, err := e.Out.ReadString('\n') line, err := e.Out.ReadString('\n')
require.NoError(t, err) require.NoError(t, err)
@ -188,10 +188,11 @@ func (e *executor) checkTxPersisted(t *testing.T) {
h, err := util.Uint256DecodeStringLE(line) h, err := util.Uint256DecodeStringLE(line)
require.NoError(t, err, "can't decode tx hash: %s", line) require.NoError(t, err, "can't decode tx hash: %s", line)
tx := e.GetTransaction(t, h) tx, height := e.GetTransaction(t, h)
aer, err := e.Chain.GetAppExecResult(tx.Hash()) aer, err := e.Chain.GetAppExecResult(tx.Hash())
require.NoError(t, err) require.NoError(t, err)
require.Equal(t, vm.HaltState, aer.VMState) require.Equal(t, vm.HaltState, aer.VMState)
return tx, height
} }
func generateKeys(t *testing.T, n int) ([]*keys.PrivateKey, keys.PublicKeys) { func generateKeys(t *testing.T, n int) ([]*keys.PrivateKey, keys.PublicKeys) {

View file

@ -12,6 +12,7 @@ import (
"github.com/nspcc-dev/neo-go/cli/options" "github.com/nspcc-dev/neo-go/cli/options"
"github.com/nspcc-dev/neo-go/pkg/crypto/keys" "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/encoding/address"
"github.com/nspcc-dev/neo-go/pkg/rpc/client"
"github.com/nspcc-dev/neo-go/pkg/smartcontract/manifest" "github.com/nspcc-dev/neo-go/pkg/smartcontract/manifest"
"github.com/nspcc-dev/neo-go/pkg/util" "github.com/nspcc-dev/neo-go/pkg/util"
"github.com/nspcc-dev/neo-go/pkg/wallet" "github.com/nspcc-dev/neo-go/pkg/wallet"
@ -232,13 +233,8 @@ func claimGas(ctx *cli.Context) error {
if err != nil { if err != nil {
return err return err
} }
// Temporary.
neoHash, err := util.Uint160DecodeStringLE("3b7d3711c6f0ccf9b1dca903d1bfa1d896f1238c")
if err != nil {
return cli.NewExitError(err, 1)
}
hash, err := c.TransferNEP5(acc, scriptHash, neoHash, 0, 0) hash, err := c.TransferNEP5(acc, scriptHash, client.NeoContractHash, 0, 0)
if err != nil { if err != nil {
return cli.NewExitError(err, 1) return cli.NewExitError(err, 1)
} }

View file

@ -2,6 +2,7 @@ package main
import ( import (
"encoding/hex" "encoding/hex"
"math/big"
"os" "os"
"path" "path"
"strings" "strings"
@ -163,3 +164,24 @@ func TestWalletExport(t *testing.T) {
require.Equal(t, validatorWIF, strings.TrimSpace(line)) require.Equal(t, validatorWIF, strings.TrimSpace(line))
}) })
} }
func TestClaimGas(t *testing.T) {
e := newExecutor(t, true)
defer e.Close(t)
start := e.Chain.BlockHeight()
balanceBefore := e.Chain.GetUtilityTokenBalance(validatorHash)
e.In.WriteString("one\r")
e.Run(t, "neo-go", "wallet", "claim",
"--unittest", "--rpc-endpoint", "http://"+e.RPC.Addr,
"--wallet", validatorWallet,
"--address", validatorAddr)
tx, end := e.checkTxPersisted(t)
b, _ := e.Chain.GetGoverningTokenBalance(validatorHash)
cl := e.Chain.CalculateClaimable(b, start, end)
require.True(t, cl.Sign() > 0)
cl.Sub(cl, big.NewInt(tx.NetworkFee+tx.SystemFee))
balanceAfter := e.Chain.GetUtilityTokenBalance(validatorHash)
require.Equal(t, 0, balanceAfter.Cmp(balanceBefore.Add(balanceBefore, cl)))
}