Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

REST bank transfers fail due to encoding. #6536

Merged
merged 22 commits into from
Jul 2, 2020
Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,7 @@ be used to retrieve the actual proposal `Content`. Also the `NewMsgSubmitProposa

### Bug Fixes

* (x/bank) [\#6536](https://github.com/cosmos/cosmos-sdk/pull/6536) When calling bank REST endpoint to generate StdTx now returns correct JSON format.
jgimeno marked this conversation as resolved.
Show resolved Hide resolved
* (x/staking) [\#6529](https://github.com/cosmos/cosmos-sdk/pull/6529) Export validator addresses (previously was empty).
* (export) [\#6510](https://github.com/cosmos/cosmos-sdk/pull/6510/) Field TimeIotaMs now is included in genesis file while exporting.
* (client) [\#6402](https://github.com/cosmos/cosmos-sdk/issues/6402) Fix `keys add` `--algo` flag which only worked for Tendermint's `secp256k1` default key signing algorithm.
Expand Down
2 changes: 1 addition & 1 deletion client/tx/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ func WriteGeneratedTxResponse(
return
}

output, err := ctx.JSONMarshaler.MarshalJSON(tx)
output, err := ctx.JSONMarshaler.MarshalJSON(tx.GetTx())
if rest.CheckInternalServerError(w, err) {
return
}
Expand Down
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ require (
github.com/tendermint/iavl v0.14.0-rc1
github.com/tendermint/tendermint v0.33.5
github.com/tendermint/tm-db v0.5.1
golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e
google.golang.org/grpc v1.30.0
gopkg.in/yaml.v2 v2.3.0
)
Expand Down
1 change: 1 addition & 0 deletions testutil/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ type (
}
)

// NewTestNetwork runs a test network used for test.
jgimeno marked this conversation as resolved.
Show resolved Hide resolved
func NewTestNetwork(t *testing.T, cfg Config) *Network {
// only one caller/test can create and use a network at a time
t.Log("acquiring test network lock")
Expand Down
3 changes: 3 additions & 0 deletions testutil/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"path/filepath"
"time"

"github.com/cosmos/cosmos-sdk/simapp"

tmos "github.com/tendermint/tendermint/libs/os"
"github.com/tendermint/tendermint/node"
"github.com/tendermint/tendermint/p2p"
Expand Down Expand Up @@ -64,6 +66,7 @@ func startInProcess(cfg Config, val *Validator) error {
WithHomeDir(tmCfg.RootDir).
WithChainID(cfg.ChainID).
WithJSONMarshaler(cdc).
WithTxGenerator(simapp.MakeEncodingConfig().TxGenerator).
jgimeno marked this conversation as resolved.
Show resolved Hide resolved
WithClient(val.RPCClient).
WithTrustNode(true)

Expand Down
142 changes: 142 additions & 0 deletions x/bank/client/rest/tx_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
package rest_test

import (
"bytes"
"fmt"
"io/ioutil"
"net/http"

"github.com/cosmos/cosmos-sdk/simapp"
"github.com/cosmos/cosmos-sdk/testutil"
"github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/errors"
"github.com/cosmos/cosmos-sdk/types/rest"
authclient "github.com/cosmos/cosmos-sdk/x/auth/client"
types2 "github.com/cosmos/cosmos-sdk/x/auth/types"
rest2 "github.com/cosmos/cosmos-sdk/x/bank/client/rest"
jgimeno marked this conversation as resolved.
Show resolved Hide resolved
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
)

func (s *IntegrationTestSuite) TestCoinSend() {
encodingConfig := simapp.MakeEncodingConfig()
authclient.Codec = encodingConfig.Marshaler

val := s.network.Validators[0]

initValidatorCoins, err := getCoinsFromValidator(val)
s.Require().NoError(err)
s.Require().Equal(
types.NewCoins(
types.NewCoin(fmt.Sprintf("%stoken", val.Moniker), s.cfg.AccountTokens),
types.NewCoin(s.cfg.BondDenom, s.cfg.StakingTokens.Sub(s.cfg.BondedTokens)),
),
initValidatorCoins,
)

account, err := getAccountInfo(val)
s.Require().NoError(err)

sendReq := generateSendReq(
account,
types.Coins{types.NewCoin(s.cfg.BondDenom, types.TokensFromConsensusPower(1))},
)

stdTx, err := submitSendReq(val, sendReq)
s.Require().NoError(err)

s.Require().Nil(stdTx.Signatures)
s.Require().Equal([]types.Msg{
&banktypes.MsgSend{
FromAddress: account.GetAddress(),
ToAddress: account.GetAddress(),
Amount: sendReq.Amount,
},
}, stdTx.GetMsgs())
}

func submitSendReq(val *testutil.Validator, req rest2.SendReq) (types2.StdTx, error) {
url := fmt.Sprintf("%s/bank/accounts/%s/transfers", val.APIAddress, val.Address)

bz, err := val.ClientCtx.JSONMarshaler.MarshalJSON(req)
if err != nil {
return types2.StdTx{}, errors.Wrap(err, "error encoding SendReq to json")
}

resp, err := http.Post(url, "", bytes.NewBuffer(bz))
if err != nil {
return types2.StdTx{}, errors.Wrap(err, "error while sending post request")
}

bz, err = ioutil.ReadAll(resp.Body)
if err != nil {
return types2.StdTx{}, errors.Wrap(err, "error reading SendReq response body")
}

var tx types2.StdTx
err = val.ClientCtx.JSONMarshaler.UnmarshalJSON(bz, &tx)
if err != nil {
return types2.StdTx{}, errors.Wrap(err, "error unmarshaling to StdTx SendReq response")
}
jgimeno marked this conversation as resolved.
Show resolved Hide resolved

return tx, nil
}

func generateSendReq(from types2.AccountI, amount types.Coins) rest2.SendReq {
baseReq := rest.NewBaseReq(
from.GetAddress().String(),
"someMemo",
"some-id",
"10000",
fmt.Sprintf("%f", 1.0),
from.GetAccountNumber(),
from.GetSequence(),
types.NewCoins(),
nil,
false,
)

return rest2.SendReq{
BaseReq: baseReq,
Amount: amount,
}
}

func getAccountInfo(val *testutil.Validator) (types2.AccountI, error) {
url := fmt.Sprintf("%s/auth/accounts/%s", val.APIAddress, val.Address)

resp, err := rest.GetRequest(url)
if err != nil {
return nil, err
}

bz, err := rest.ParseResponseWithHeight(val.ClientCtx.JSONMarshaler, resp)
if err != nil {
return nil, err
}

var acc types2.AccountI
err = val.ClientCtx.JSONMarshaler.UnmarshalJSON(bz, &acc)
if err != nil {
return nil, err
}

return acc, nil
}

func getCoinsFromValidator(val *testutil.Validator) (types.Coins, error) {
url := fmt.Sprintf("%s/bank/balances/%s", val.APIAddress, val.Address)

resp, err := rest.GetRequest(url)
if err != nil {
return nil, err
}

bz, err := rest.ParseResponseWithHeight(val.ClientCtx.JSONMarshaler, resp)
var coins types.Coins
err = val.ClientCtx.JSONMarshaler.UnmarshalJSON(bz, &coins)
if err != nil {
return nil, err
}

return coins, nil
}