Merge PR #3523: Add tx/encode endpoint and CLI command
This commit is contained in:
committed by
Jack Zampolin
parent
d759bef4d1
commit
9348750eb4
@@ -1,12 +1,14 @@
|
||||
package clitest
|
||||
|
||||
import (
|
||||
"encoding/base64"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"path"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
@@ -18,6 +20,7 @@ import (
|
||||
"github.com/cosmos/cosmos-sdk/cmd/gaia/app"
|
||||
"github.com/cosmos/cosmos-sdk/tests"
|
||||
sdk "github.com/cosmos/cosmos-sdk/types"
|
||||
"github.com/cosmos/cosmos-sdk/x/auth"
|
||||
"github.com/cosmos/cosmos-sdk/x/gov"
|
||||
"github.com/cosmos/cosmos-sdk/x/staking"
|
||||
)
|
||||
@@ -587,7 +590,7 @@ func TestGaiaCLIValidateSignatures(t *testing.T) {
|
||||
require.Empty(t, stderr)
|
||||
|
||||
// write unsigned tx to file
|
||||
unsignedTxFile := writeToNewTempFile(t, stdout)
|
||||
unsignedTxFile := WriteToNewTempFile(t, stdout)
|
||||
defer os.Remove(unsignedTxFile.Name())
|
||||
|
||||
// validate we can successfully sign
|
||||
@@ -600,7 +603,7 @@ func TestGaiaCLIValidateSignatures(t *testing.T) {
|
||||
require.Equal(t, fooAddr.String(), stdTx.GetSigners()[0].String())
|
||||
|
||||
// write signed tx to file
|
||||
signedTxFile := writeToNewTempFile(t, stdout)
|
||||
signedTxFile := WriteToNewTempFile(t, stdout)
|
||||
defer os.Remove(signedTxFile.Name())
|
||||
|
||||
// validate signatures
|
||||
@@ -610,7 +613,7 @@ func TestGaiaCLIValidateSignatures(t *testing.T) {
|
||||
// modify the transaction
|
||||
stdTx.Memo = "MODIFIED-ORIGINAL-TX-BAD"
|
||||
bz := marshalStdTx(t, stdTx)
|
||||
modSignedTxFile := writeToNewTempFile(t, string(bz))
|
||||
modSignedTxFile := WriteToNewTempFile(t, string(bz))
|
||||
defer os.Remove(modSignedTxFile.Name())
|
||||
|
||||
// validate signature validation failure due to different transaction sig bytes
|
||||
@@ -659,7 +662,7 @@ func TestGaiaCLISendGenerateSignAndBroadcast(t *testing.T) {
|
||||
require.Equal(t, len(msg.Msgs), 1)
|
||||
|
||||
// Write the output to disk
|
||||
unsignedTxFile := writeToNewTempFile(t, stdout)
|
||||
unsignedTxFile := WriteToNewTempFile(t, stdout)
|
||||
defer os.Remove(unsignedTxFile.Name())
|
||||
|
||||
// Test sign --validate-signatures
|
||||
@@ -676,7 +679,7 @@ func TestGaiaCLISendGenerateSignAndBroadcast(t *testing.T) {
|
||||
require.Equal(t, fooAddr.String(), msg.GetSigners()[0].String())
|
||||
|
||||
// Write the output to disk
|
||||
signedTxFile := writeToNewTempFile(t, stdout)
|
||||
signedTxFile := WriteToNewTempFile(t, stdout)
|
||||
defer os.Remove(signedTxFile.Name())
|
||||
|
||||
// Test sign --validate-signatures
|
||||
@@ -732,7 +735,7 @@ func TestGaiaCLIMultisignInsufficientCosigners(t *testing.T) {
|
||||
require.True(t, success)
|
||||
|
||||
// Write the output to disk
|
||||
unsignedTxFile := writeToNewTempFile(t, stdout)
|
||||
unsignedTxFile := WriteToNewTempFile(t, stdout)
|
||||
defer os.Remove(unsignedTxFile.Name())
|
||||
|
||||
// Sign with foo's key
|
||||
@@ -740,7 +743,7 @@ func TestGaiaCLIMultisignInsufficientCosigners(t *testing.T) {
|
||||
require.True(t, success)
|
||||
|
||||
// Write the output to disk
|
||||
fooSignatureFile := writeToNewTempFile(t, stdout)
|
||||
fooSignatureFile := WriteToNewTempFile(t, stdout)
|
||||
defer os.Remove(fooSignatureFile.Name())
|
||||
|
||||
// Multisign, not enough signatures
|
||||
@@ -748,7 +751,7 @@ func TestGaiaCLIMultisignInsufficientCosigners(t *testing.T) {
|
||||
require.True(t, success)
|
||||
|
||||
// Write the output to disk
|
||||
signedTxFile := writeToNewTempFile(t, stdout)
|
||||
signedTxFile := WriteToNewTempFile(t, stdout)
|
||||
defer os.Remove(signedTxFile.Name())
|
||||
|
||||
// Validate the multisignature
|
||||
@@ -760,6 +763,42 @@ func TestGaiaCLIMultisignInsufficientCosigners(t *testing.T) {
|
||||
require.False(t, success)
|
||||
}
|
||||
|
||||
func TestGaiaCLIEncode(t *testing.T) {
|
||||
t.Parallel()
|
||||
f := InitFixtures(t)
|
||||
|
||||
// start gaiad server
|
||||
proc := f.GDStart()
|
||||
defer proc.Stop(false)
|
||||
|
||||
cdc := app.MakeCodec()
|
||||
|
||||
// Build a testing transaction and write it to disk
|
||||
barAddr := f.KeyAddress(keyBar)
|
||||
sendTokens := staking.TokensFromTendermintPower(10)
|
||||
success, stdout, stderr := f.TxSend(keyFoo, barAddr, sdk.NewCoin(denom, sendTokens), "--generate-only", "--memo", "deadbeef")
|
||||
require.True(t, success)
|
||||
require.Empty(t, stderr)
|
||||
|
||||
// Write it to disk
|
||||
jsonTxFile := WriteToNewTempFile(t, stdout)
|
||||
defer os.Remove(jsonTxFile.Name())
|
||||
|
||||
// Run the encode command, and trim the extras from the stdout capture
|
||||
success, base64Encoded, _ := f.TxEncode(jsonTxFile.Name())
|
||||
require.True(t, success)
|
||||
trimmedBase64 := strings.Trim(base64Encoded, "\"\n")
|
||||
|
||||
// Decode the base64
|
||||
decodedBytes, err := base64.StdEncoding.DecodeString(trimmedBase64)
|
||||
require.Nil(t, err)
|
||||
|
||||
// Check that the transaction decodes as epxceted
|
||||
var decodedTx auth.StdTx
|
||||
require.Nil(t, cdc.UnmarshalBinaryLengthPrefixed(decodedBytes, &decodedTx))
|
||||
require.Equal(t, "deadbeef", decodedTx.Memo)
|
||||
}
|
||||
|
||||
func TestGaiaCLIMultisignSortSignatures(t *testing.T) {
|
||||
t.Parallel()
|
||||
f := InitFixtures(t)
|
||||
@@ -785,7 +824,7 @@ func TestGaiaCLIMultisignSortSignatures(t *testing.T) {
|
||||
require.True(t, success)
|
||||
|
||||
// Write the output to disk
|
||||
unsignedTxFile := writeToNewTempFile(t, stdout)
|
||||
unsignedTxFile := WriteToNewTempFile(t, stdout)
|
||||
defer os.Remove(unsignedTxFile.Name())
|
||||
|
||||
// Sign with foo's key
|
||||
@@ -793,7 +832,7 @@ func TestGaiaCLIMultisignSortSignatures(t *testing.T) {
|
||||
require.True(t, success)
|
||||
|
||||
// Write the output to disk
|
||||
fooSignatureFile := writeToNewTempFile(t, stdout)
|
||||
fooSignatureFile := WriteToNewTempFile(t, stdout)
|
||||
defer os.Remove(fooSignatureFile.Name())
|
||||
|
||||
// Sign with baz's key
|
||||
@@ -801,7 +840,7 @@ func TestGaiaCLIMultisignSortSignatures(t *testing.T) {
|
||||
require.True(t, success)
|
||||
|
||||
// Write the output to disk
|
||||
bazSignatureFile := writeToNewTempFile(t, stdout)
|
||||
bazSignatureFile := WriteToNewTempFile(t, stdout)
|
||||
defer os.Remove(bazSignatureFile.Name())
|
||||
|
||||
// Multisign, keys in different order
|
||||
@@ -810,7 +849,7 @@ func TestGaiaCLIMultisignSortSignatures(t *testing.T) {
|
||||
require.True(t, success)
|
||||
|
||||
// Write the output to disk
|
||||
signedTxFile := writeToNewTempFile(t, stdout)
|
||||
signedTxFile := WriteToNewTempFile(t, stdout)
|
||||
defer os.Remove(signedTxFile.Name())
|
||||
|
||||
// Validate the multisignature
|
||||
@@ -848,7 +887,7 @@ func TestGaiaCLIMultisign(t *testing.T) {
|
||||
require.Empty(t, stderr)
|
||||
|
||||
// Write the output to disk
|
||||
unsignedTxFile := writeToNewTempFile(t, stdout)
|
||||
unsignedTxFile := WriteToNewTempFile(t, stdout)
|
||||
defer os.Remove(unsignedTxFile.Name())
|
||||
|
||||
// Sign with foo's key
|
||||
@@ -856,7 +895,7 @@ func TestGaiaCLIMultisign(t *testing.T) {
|
||||
require.True(t, success)
|
||||
|
||||
// Write the output to disk
|
||||
fooSignatureFile := writeToNewTempFile(t, stdout)
|
||||
fooSignatureFile := WriteToNewTempFile(t, stdout)
|
||||
defer os.Remove(fooSignatureFile.Name())
|
||||
|
||||
// Sign with bar's key
|
||||
@@ -864,7 +903,7 @@ func TestGaiaCLIMultisign(t *testing.T) {
|
||||
require.True(t, success)
|
||||
|
||||
// Write the output to disk
|
||||
barSignatureFile := writeToNewTempFile(t, stdout)
|
||||
barSignatureFile := WriteToNewTempFile(t, stdout)
|
||||
defer os.Remove(barSignatureFile.Name())
|
||||
|
||||
// Multisign
|
||||
@@ -873,7 +912,7 @@ func TestGaiaCLIMultisign(t *testing.T) {
|
||||
require.True(t, success)
|
||||
|
||||
// Write the output to disk
|
||||
signedTxFile := writeToNewTempFile(t, stdout)
|
||||
signedTxFile := WriteToNewTempFile(t, stdout)
|
||||
defer os.Remove(signedTxFile.Name())
|
||||
|
||||
// Validate the multisignature
|
||||
|
||||
@@ -294,12 +294,18 @@ func (f *Fixtures) TxSign(signer, fileName string, flags ...string) (bool, strin
|
||||
return executeWriteRetStdStreams(f.T, addFlags(cmd, flags), app.DefaultKeyPass)
|
||||
}
|
||||
|
||||
// TxBroadcast is gaiacli tx sign
|
||||
// TxBroadcast is gaiacli tx broadcast
|
||||
func (f *Fixtures) TxBroadcast(fileName string, flags ...string) (bool, string, string) {
|
||||
cmd := fmt.Sprintf("gaiacli tx broadcast %v %v", f.Flags(), fileName)
|
||||
return executeWriteRetStdStreams(f.T, addFlags(cmd, flags), app.DefaultKeyPass)
|
||||
}
|
||||
|
||||
// TxEncode is gaiacli tx encode
|
||||
func (f *Fixtures) TxEncode(fileName string, flags ...string) (bool, string, string) {
|
||||
cmd := fmt.Sprintf("gaiacli tx encode %v %v", f.Flags(), fileName)
|
||||
return executeWriteRetStdStreams(f.T, addFlags(cmd, flags), app.DefaultKeyPass)
|
||||
}
|
||||
|
||||
// TxMultisign is gaiacli tx multisign
|
||||
func (f *Fixtures) TxMultisign(fileName, name string, signaturesFiles []string,
|
||||
flags ...string) (bool, string, string) {
|
||||
@@ -640,7 +646,8 @@ func queryTags(tags []string) (out string) {
|
||||
return strings.TrimSuffix(out, "&")
|
||||
}
|
||||
|
||||
func writeToNewTempFile(t *testing.T, s string) *os.File {
|
||||
// Write the given string to a new temporary file
|
||||
func WriteToNewTempFile(t *testing.T, s string) *os.File {
|
||||
fp, err := ioutil.TempFile(os.TempDir(), "cosmos_cli_test_")
|
||||
require.Nil(t, err)
|
||||
_, err = fp.WriteString(s)
|
||||
|
||||
@@ -141,7 +141,8 @@ func txCmd(cdc *amino.Codec, mc []sdk.ModuleClients) *cobra.Command {
|
||||
client.LineBreak,
|
||||
authcmd.GetSignCommand(cdc),
|
||||
authcmd.GetMultiSignCommand(cdc),
|
||||
bankcmd.GetBroadcastCommand(cdc),
|
||||
authcmd.GetBroadcastCommand(cdc),
|
||||
authcmd.GetEncodeCommand(cdc),
|
||||
client.LineBreak,
|
||||
)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user