Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/cosmos/cosmos-sdk into aa…
Browse files Browse the repository at this point in the history
…ronc/6213-cli-encode
  • Loading branch information
aaronc committed Jun 18, 2020
2 parents e0cb1d4 + 257354d commit 2621815
Show file tree
Hide file tree
Showing 134 changed files with 1,706 additions and 1,189 deletions.
24 changes: 24 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -213,3 +213,27 @@ jobs:
run: |
make test-integration
if: "env.GIT_DIFF != ''"

liveness-test:
runs-on: ubuntu-latest
timeout-minutes: 10
steps:
- uses: actions/checkout@v2
- uses: technote-space/get-diff-action@v1
id: git_diff
with:
SUFFIX_FILTER: |
.go
.mod
.sum
- name: build image
run: |
make build-docker-local-simapp
- name: start localnet
run: |
make clean build-sim-linux localnet-start
if: "env.GIT_DIFF != ''"
- name: test liveness
run: |
./contrib/localnet_liveness.sh 100 5 50 localhost
if: "env.GIT_DIFF != ''"
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ older clients.

### API Breaking Changes

* (x/staking) [\#6451](https://github.com/cosmos/cosmos-sdk/pull/6451) `DefaultParamspace` and `ParamKeyTable` in staking module are moved from keeper to types to enforce consistency.
* [\#6409](https://github.com/cosmos/cosmos-sdk/pull/6409) Rename all IsEmpty methods to Empty across the codebase and enforce consistency.
* [\#6231](https://github.com/cosmos/cosmos-sdk/pull/6231) Simplify `AppModule` interface, `Route` and `NewHandler` methods become only `Route`
and returns a new `Route` type.
Expand Down Expand Up @@ -122,6 +123,8 @@ be used to retrieve the actual proposal `Content`. Also the `NewMsgSubmitProposa
* (modules) [\#6326](https://github.com/cosmos/cosmos-sdk/pull/6326) `AppModuleBasic.GetQueryCmd` now takes a single `CLIContext` parameter.
* (modules) [\#6336](https://github.com/cosmos/cosmos-sdk/pull/6336) `AppModuleBasic.RegisterQueryService` method was added to support gRPC queries, and `QuerierRoute` and `NewQuerierHandler` were deprecated.
* (modules) [\#6311](https://github.com/cosmos/cosmos-sdk/issues/6311) Remove `alias.go` usage
* (x/auth) [\#6443](https://github.com/cosmos/cosmos-sdk/issues/6443) Move `FeeTx` and `TxWithMemo` interfaces from `x/auth/ante` to `types`.
* (modules) [\#6447](https://github.com/cosmos/cosmos-sdk/issues/6447) Rename `blacklistedAddrs` to `blockedAddrs`.

Migration guide:

Expand Down
79 changes: 35 additions & 44 deletions client/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package client

import (
"bufio"
"encoding/json"
"fmt"
"io"
"os"
Expand Down Expand Up @@ -321,67 +322,57 @@ func (ctx Context) WithAccountRetriever(retriever AccountRetriever) Context {
return ctx
}

// Println outputs toPrint to the ctx.Output based on ctx.OutputFormat which is
// PrintOutput outputs toPrint to the ctx.Output based on ctx.OutputFormat which is
// either text or json. If text, toPrint will be YAML encoded. Otherwise, toPrint
// will be JSON encoded using ctx.JSONMarshaler. An error is returned upon failure.
func (ctx Context) Println(toPrint interface{}) error {
var (
out []byte
err error
)

switch ctx.OutputFormat {
case "text":
out, err = yaml.Marshal(&toPrint)
func (ctx Context) PrintOutput(toPrint interface{}) error {
// always serialize JSON initially because proto json can't be directly YAML encoded
out, err := ctx.JSONMarshaler.MarshalJSON(toPrint)
if err != nil {
return err
}

case "json":
out, err = ctx.JSONMarshaler.MarshalJSON(toPrint)
if ctx.OutputFormat == "text" {
// handle text format by decoding and re-encoding JSON as YAML
var j interface{}
err = json.Unmarshal(out, &j)
if err != nil {
return err
}

out, err = yaml.Marshal(j)
if err != nil {
return err
}
} else if ctx.Indent {
// To JSON indent, we re-encode the already encoded JSON given there is no
// error. The re-encoded JSON uses the standard library as the initial encoded
// JSON should have the correct output produced by ctx.JSONMarshaler.
if ctx.Indent && err == nil {
out, err = codec.MarshalIndentFromJSON(out)
out, err = codec.MarshalIndentFromJSON(out)
if err != nil {
return err
}
}

writer := ctx.Output
// default to stdout
if writer == nil {
writer = os.Stdout
}

_, err = writer.Write(out)
if err != nil {
return err
}

_, err = fmt.Fprintf(ctx.Output, "%s\n", out)
return err
}

// PrintOutput prints output while respecting output and indent flags
// NOTE: pass in marshalled structs that have been unmarshaled
// because this function will panic on marshaling errors.
//
// TODO: Remove once client-side Protobuf migration has been completed.
// ref: https://github.com/cosmos/cosmos-sdk/issues/5864
func (ctx Context) PrintOutput(toPrint interface{}) error {
var (
out []byte
err error
)

switch ctx.OutputFormat {
case "text":
out, err = yaml.Marshal(&toPrint)

case "json":
if ctx.Indent {
out, err = ctx.Codec.MarshalJSONIndent(toPrint, "", " ")
} else {
out, err = ctx.Codec.MarshalJSON(toPrint)
if ctx.OutputFormat != "text" {
// append new-line for formats besides YAML
_, err = writer.Write([]byte("\n"))
if err != nil {
return err
}
}

if err != nil {
return err
}

fmt.Println(string(out))
return nil
}

Expand Down
128 changes: 128 additions & 0 deletions client/context_test.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
package client_test

import (
"bytes"
"os"
"testing"

"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/codec/testdata"
"github.com/cosmos/cosmos-sdk/codec/types"

"github.com/cosmos/cosmos-sdk/crypto/keyring"
sdk "github.com/cosmos/cosmos-sdk/types"

Expand Down Expand Up @@ -76,3 +81,126 @@ func TestMain(m *testing.M) {
viper.Set(flags.FlagKeyringBackend, keyring.BackendMemory)
os.Exit(m.Run())
}

func TestContext_PrintOutput(t *testing.T) {
ctx := client.Context{}

animal := &testdata.Dog{
Size_: "big",
Name: "Spot",
}
any, err := types.NewAnyWithValue(animal)
require.NoError(t, err)
hasAnimal := &testdata.HasAnimal{
Animal: any,
X: 10,
}

//
// proto
//
registry := testdata.NewTestInterfaceRegistry()
ctx = ctx.WithJSONMarshaler(codec.NewProtoCodec(registry))

// json
buf := &bytes.Buffer{}
ctx = ctx.WithOutput(buf)
ctx.OutputFormat = "json"
ctx.Indent = false
err = ctx.PrintOutput(hasAnimal)
require.NoError(t, err)
require.Equal(t,
`{"animal":{"@type":"/cosmos_sdk.codec.v1.Dog","size":"big","name":"Spot"},"x":"10"}
`, string(buf.Bytes()))

// json indent
buf = &bytes.Buffer{}
ctx = ctx.WithOutput(buf)
ctx.OutputFormat = "json"
ctx.Indent = true
err = ctx.PrintOutput(hasAnimal)
require.NoError(t, err)
require.Equal(t,
`{
"animal": {
"@type": "/cosmos_sdk.codec.v1.Dog",
"name": "Spot",
"size": "big"
},
"x": "10"
}
`, string(buf.Bytes()))

// yaml
buf = &bytes.Buffer{}
ctx = ctx.WithOutput(buf)
ctx.OutputFormat = "text"
ctx.Indent = false
err = ctx.PrintOutput(hasAnimal)
require.NoError(t, err)
require.Equal(t,
`animal:
'@type': /cosmos_sdk.codec.v1.Dog
name: Spot
size: big
x: "10"
`, string(buf.Bytes()))

//
// amino
//
amino := testdata.NewTestAmino()
ctx = ctx.WithJSONMarshaler(codec.NewAminoCodec(&codec.Codec{Amino: amino}))

// json
buf = &bytes.Buffer{}
ctx = ctx.WithOutput(buf)
ctx.OutputFormat = "json"
ctx.Indent = false
err = ctx.PrintOutput(hasAnimal)
require.NoError(t, err)
require.Equal(t,
`{"type":"testdata/HasAnimal","value":{"animal":{"type":"testdata/Dog","value":{"size":"big","name":"Spot"}},"x":"10"}}
`, string(buf.Bytes()))

// json indent
buf = &bytes.Buffer{}
ctx = ctx.WithOutput(buf)
ctx.OutputFormat = "json"
ctx.Indent = true
err = ctx.PrintOutput(hasAnimal)
require.NoError(t, err)
require.Equal(t,
`{
"type": "testdata/HasAnimal",
"value": {
"animal": {
"type": "testdata/Dog",
"value": {
"name": "Spot",
"size": "big"
}
},
"x": "10"
}
}
`, string(buf.Bytes()))

// yaml
buf = &bytes.Buffer{}
ctx = ctx.WithOutput(buf)
ctx.OutputFormat = "text"
ctx.Indent = false
err = ctx.PrintOutput(hasAnimal)
require.NoError(t, err)
require.Equal(t,
`type: testdata/HasAnimal
value:
animal:
type: testdata/Dog
value:
name: Spot
size: big
x: "10"
`, string(buf.Bytes()))
}
2 changes: 2 additions & 0 deletions client/flags/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ const (
FlagKeyringBackend = "keyring-backend"
FlagPage = "page"
FlagLimit = "limit"
FlagSignMode = "sign-mode"
)

// LineBreak can be included in a command list to provide a blank line
Expand Down Expand Up @@ -116,6 +117,7 @@ func PostCommands(cmds ...*cobra.Command) []*cobra.Command {
c.Flags().Bool(FlagOffline, false, "Offline mode (does not allow any online functionality")
c.Flags().BoolP(FlagSkipConfirmation, "y", false, "Skip tx broadcasting prompt confirmation")
c.Flags().String(FlagKeyringBackend, DefaultKeyringBackend, "Select keyring's backend (os|file|kwallet|pass|test)")
c.Flags().String(FlagSignMode, "", "Choose sign mode (direct|amino-json), this is an advanced feature")

// --gas can accept integers and "simulate"
c.Flags().Var(&GasFlagVar, "gas", fmt.Sprintf(
Expand Down
2 changes: 1 addition & 1 deletion client/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ func (ctx Context) queryABCI(req abci.RequestQuery) (abci.ResponseQuery, error)
}

// data from trusted node or subspace query doesn't need verification
if ctx.TrustNode || !isQueryStoreWithProof(req.Path) {
if !opts.Prove || !isQueryStoreWithProof(req.Path) {
return result.Response, nil
}

Expand Down
19 changes: 18 additions & 1 deletion client/tx/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/cosmos/cosmos-sdk/crypto/keyring"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/tx/signing"
)

// Factory defines a client transaction factory that facilitates generating and
Expand All @@ -21,13 +22,19 @@ type Factory struct {
sequence uint64
gas uint64
gasAdjustment float64
simulateAndExecute bool
chainID string
memo string
fees sdk.Coins
gasPrices sdk.DecCoins
signMode signing.SignMode
simulateAndExecute bool
}

const (
signModeDirect = "direct"
signModeAminoJSON = "amino-json"
)

func NewFactoryFromCLI(input io.Reader) Factory {
kb, err := keyring.New(
sdk.KeyringServiceName(),
Expand All @@ -39,6 +46,15 @@ func NewFactoryFromCLI(input io.Reader) Factory {
panic(err)
}

signModeStr := viper.GetString(flags.FlagSignMode)
signMode := signing.SignMode_SIGN_MODE_UNSPECIFIED
switch signModeStr {
case signModeDirect:
signMode = signing.SignMode_SIGN_MODE_DIRECT
case signModeAminoJSON:
signMode = signing.SignMode_SIGN_MODE_LEGACY_AMINO_JSON
}

f := Factory{
keybase: kb,
accountNumber: viper.GetUint64(flags.FlagAccountNumber),
Expand All @@ -48,6 +64,7 @@ func NewFactoryFromCLI(input io.Reader) Factory {
simulateAndExecute: flags.GasFlagVar.Simulate,
chainID: viper.GetString(flags.FlagChainID),
memo: viper.GetString(flags.FlagMemo),
signMode: signMode,
}

f = f.WithFees(viper.GetString(flags.FlagFees))
Expand Down
Loading

0 comments on commit 2621815

Please sign in to comment.