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

R4R: Add fees and memo to REST, updated CLI to receive coins as fees #3069

Merged
merged 62 commits into from
Dec 20, 2018
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
Show all changes
62 commits
Select commit Hold shift + click to select a range
b55e748
pending
Dec 4, 2018
f3d5279
Split endpoints and use utils
Dec 5, 2018
9bea121
Merge branch 'develop' of https://github.com/cosmos/cosmos-sdk into f…
Dec 5, 2018
dcd7ecd
fix lcd tests
Dec 5, 2018
5af8fd6
change input msg types
Dec 5, 2018
b19d9ed
update and rearrange swagger file
Dec 5, 2018
0ee236a
Updated swagger
Dec 5, 2018
5bd2544
update delegations to use POST
Dec 6, 2018
8b794d2
update pending
Dec 6, 2018
9286098
Update tx.go
fedekunze Dec 7, 2018
c4765b2
Merge branch 'develop' of https://github.com/cosmos/cosmos-sdk into f…
Dec 10, 2018
3f744f8
fix genOnly REST, closes #3020
Dec 10, 2018
6fd34a9
fixes based on tests
Dec 10, 2018
03771a3
fix lint
Dec 10, 2018
7959e0b
Add fees to REST, update CLI to receive multiple coins as fees, added…
Dec 10, 2018
41d5cae
update swagger
Dec 10, 2018
6f6a4f8
add helper example to fee flag
Dec 10, 2018
58d4b8f
remove sdk.Coin from fees
Dec 10, 2018
e4e537a
fix conflicts and update functions based on Alessio's PR
Dec 11, 2018
b4a307b
fix comments on cli_tests and added simulation update
Dec 11, 2018
a90fca5
Update cli_test.go
fedekunze Dec 11, 2018
db07298
fix conflicts
Dec 11, 2018
9c82e31
Merge branch 'jack/dry-rest-tests' of https://github.com/cosmos/cosmo…
Dec 11, 2018
91f0818
commit to pull split changes
Dec 11, 2018
e3c0eb3
use req.BaseReq everywhere
Dec 11, 2018
64e7e6b
cleanup test_helper
Dec 11, 2018
e76c5c5
Merge develop
Dec 11, 2018
e36daa7
update tests
Dec 11, 2018
1835560
fixed some tests
Dec 11, 2018
8049985
flags
Dec 12, 2018
714c659
fix conflicts from develop
Dec 12, 2018
c2a4444
add balance checks on tests
Dec 12, 2018
017c305
fixes and modifications in gas
Dec 12, 2018
6484813
more balance checks
Dec 12, 2018
d429779
add TxEncoder changes
Dec 14, 2018
64b2b3f
fix tests
Dec 14, 2018
4b59216
cleanup
Dec 14, 2018
aaeee73
PENDING
Dec 14, 2018
4358e63
fix lint
Dec 14, 2018
e687ac9
Apply suggestions from @alexanderbez
alexanderbez Dec 14, 2018
5d033eb
Update rest.go
fedekunze Dec 14, 2018
d20d170
review fixes
Dec 14, 2018
1be6ade
rename flag '--fee' to '--fees'
Dec 14, 2018
b2190ff
Merge branch 'develop' into fedekunze/3067-fee-rest-txs
fedekunze Dec 14, 2018
525d290
merge develop
Dec 17, 2018
bc8b60f
pending update
Dec 17, 2018
655014c
Merge branch 'develop' into fedekunze/3067-fee-rest-txs
fedekunze Dec 18, 2018
d5785c0
Merge branch 'develop' into fedekunze/3067-fee-rest-txs
jackzampolin Dec 18, 2018
0fcc8b4
Merge branch 'develop' of https://github.com/cosmos/cosmos-sdk into f…
Dec 18, 2018
370ea59
fix tx builder
Dec 18, 2018
4dace37
Merge branch 'develop' into fedekunze/3067-fee-rest-txs
fedekunze Dec 18, 2018
c192cbc
update flag fee to fees
Dec 18, 2018
98797ab
Merge branch 'fedekunze/3067-fee-rest-txs' of https://github.com/cosm…
Dec 18, 2018
2809403
delete panics when no chain-id is provided
Dec 18, 2018
6e7db9d
make txBuilder's fields private
Dec 19, 2018
bc35c17
simulateAndExecute transaction
Dec 19, 2018
b376604
update swagger
Dec 19, 2018
0d4ee31
Update PENDING.md
fedekunze Dec 19, 2018
82dae0e
minor fix
Dec 19, 2018
93356dd
Merge branch 'fedekunze/3067-fee-rest-txs' of https://github.com/cosm…
Dec 19, 2018
c907f3b
Merge branch 'develop' into fedekunze/3067-fee-rest-txs
fedekunze Dec 19, 2018
cfa723a
Update sign.go
fedekunze Dec 19, 2018
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
2 changes: 1 addition & 1 deletion client/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ func PostCommands(cmds ...*cobra.Command) []*cobra.Command {
c.Flags().Uint64(FlagAccountNumber, 0, "AccountNumber number to sign the tx")
c.Flags().Uint64(FlagSequence, 0, "Sequence number to sign the tx")
c.Flags().String(FlagMemo, "", "Memo to send along with transaction")
c.Flags().String(FlagFee, "", "Fee to pay along with transaction")
c.Flags().String(FlagFee, "", "fees to pay along with transaction; eg: 10stake,1atom")
c.Flags().String(FlagChainID, "", "Chain ID of tendermint node")
c.Flags().String(FlagNode, "tcp://localhost:26657", "<host>:<port> to tendermint rpc interface for this chain")
c.Flags().Bool(FlagUseLedger, false, "Use a connected Ledger device")
Expand Down
17 changes: 13 additions & 4 deletions client/lcd/swagger-ui/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1871,23 +1871,32 @@ definitions:
properties:
name:
type: string
example: "my_name"
password:
type: string
example: "12345678"
memo:
type: string
example: "Sent via Cosmos Voyager 🚀"
chain_id:
type: string
example: "Cosmos-Hub"
account_number:
type: string
example: "0"
type: integer
fedekunze marked this conversation as resolved.
Show resolved Hide resolved
example: 0
sequence:
type: string
example: "0"
type: integer
example: 1
gas:
type: string
example: "200000"
gas_adjustment:
type: string
example: "1.2"
fees:
type: array
items:
$ref: "#/definitions/Coin"
TendermintValidator:
type: object
properties:
Expand Down
32 changes: 16 additions & 16 deletions client/utils/rest.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,25 +121,29 @@ func urlQueryHasArg(url *url.URL, arg string) bool { return url.Query().Get(arg)
// BaseReq defines a structure that can be embedded in other request structures
// that all share common "base" fields.
type BaseReq struct {
Name string `json:"name"`
Password string `json:"password"`
ChainID string `json:"chain_id"`
AccountNumber uint64 `json:"account_number"`
Sequence uint64 `json:"sequence"`
Gas string `json:"gas"`
GasAdjustment string `json:"gas_adjustment"`
Name string `json:"name"`
Password string `json:"password"`
Memo string `json:"memo"`
ChainID string `json:"chain_id"`
AccountNumber uint64 `json:"account_number"`
Sequence uint64 `json:"sequence"`
Gas string `json:"gas"`
GasAdjustment string `json:"gas_adjustment"`
Fees sdk.Coins `json:"fees"`
}

// Sanitize performs basic sanitization on a BaseReq object.
func (br BaseReq) Sanitize() BaseReq {
return BaseReq{
Name: strings.TrimSpace(br.Name),
Password: strings.TrimSpace(br.Password),
Memo: strings.TrimSpace(br.Memo),
ChainID: strings.TrimSpace(br.ChainID),
Gas: strings.TrimSpace(br.Gas),
GasAdjustment: strings.TrimSpace(br.GasAdjustment),
AccountNumber: br.AccountNumber,
Sequence: br.Sequence,
Fees: br.Fees,
}
}

Expand Down Expand Up @@ -188,6 +192,10 @@ func (br BaseReq) ValidateBasic(w http.ResponseWriter) bool {
case len(br.ChainID) == 0:
WriteErrorResponse(w, http.StatusUnauthorized, "chainID required but not specified")
return false

case !br.Fees.IsValid():
WriteErrorResponse(w, http.StatusPaymentRequired, sdk.ErrInvalidCoins("").Error())
fedekunze marked this conversation as resolved.
Show resolved Hide resolved
return false
}

return true
Expand All @@ -213,15 +221,7 @@ func CompleteAndBroadcastTxREST(w http.ResponseWriter, r *http.Request, cliCtx c
return
}

txBldr := authtxb.TxBuilder{
Codec: cdc,
Gas: gas,
GasAdjustment: adjustment,
SimulateGas: simulateGas,
ChainID: baseReq.ChainID,
AccountNumber: baseReq.AccountNumber,
Sequence: baseReq.Sequence,
}
txBldr := authtxb.NewTxBuilder(cdc, baseReq.AccountNumber, baseReq.Sequence, gas, adjustment, simulateGas, baseReq.ChainID, baseReq.Memo, baseReq.Fees)

if HasDryRunArg(r) || txBldr.SimulateGas {
newBldr, err := EnrichCtxWithGas(txBldr, cliCtx, baseReq.Name, msgs)
Expand Down
4 changes: 2 additions & 2 deletions cmd/gaia/app/benchmarks/txsize_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@ func ExampleTxSendSize() {
addr1 := sdk.AccAddress(priv1.PubKey().Address())
priv2 := secp256k1.GenPrivKeySecp256k1([]byte{1})
addr2 := sdk.AccAddress(priv2.PubKey().Address())
coins := []sdk.Coin{sdk.NewCoin("denom", sdk.NewInt(10))}
coins := sdk.Coins{sdk.NewCoin("denom", sdk.NewInt(10))}
msg1 := bank.MsgSend{
Inputs: []bank.Input{bank.NewInput(addr1, coins)},
Outputs: []bank.Output{bank.NewOutput(addr2, coins)},
}
fee := auth.NewStdFee(gas, coins...)
fee := auth.NewStdFee(gas, coins)
signBytes := auth.StdSignBytes("example-chain-ID",
1, 1, fee, []sdk.Msg{msg1}, "")
sig, _ := priv1.Sign(signBytes)
Expand Down
10 changes: 5 additions & 5 deletions x/auth/ante_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func newTestMsg(addrs ...sdk.AccAddress) *sdk.TestMsg {

func newStdFee() StdFee {
return NewStdFee(50000,
sdk.NewInt64Coin("atom", 150),
sdk.Coins{sdk.NewInt64Coin("atom", 150)},
)
}

Expand Down Expand Up @@ -426,24 +426,24 @@ func TestAnteHandlerMemoGas(t *testing.T) {
var tx sdk.Tx
msg := newTestMsg(addr1)
privs, accnums, seqs := []crypto.PrivKey{priv1}, []uint64{0}, []uint64{0}
fee := NewStdFee(0, sdk.NewInt64Coin("atom", 0))
fee := NewStdFee(0, sdk.Coins{sdk.NewInt64Coin("atom", 0)})

// tx does not have enough gas
tx = newTestTx(ctx, []sdk.Msg{msg}, privs, accnums, seqs, fee)
checkInvalidTx(t, anteHandler, ctx, tx, false, sdk.CodeOutOfGas)

// tx with memo doesn't have enough gas
fee = NewStdFee(801, sdk.NewInt64Coin("atom", 0))
fee = NewStdFee(801, sdk.Coins{sdk.NewInt64Coin("atom", 0)})
tx = newTestTxWithMemo(ctx, []sdk.Msg{msg}, privs, accnums, seqs, fee, "abcininasidniandsinasindiansdiansdinaisndiasndiadninsd")
checkInvalidTx(t, anteHandler, ctx, tx, false, sdk.CodeOutOfGas)

// memo too large
fee = NewStdFee(9000, sdk.NewInt64Coin("atom", 0))
fee = NewStdFee(9000, sdk.Coins{sdk.NewInt64Coin("atom", 0)})
tx = newTestTxWithMemo(ctx, []sdk.Msg{msg}, privs, accnums, seqs, fee, strings.Repeat("01234567890", 500))
checkInvalidTx(t, anteHandler, ctx, tx, false, sdk.CodeMemoTooLarge)

// tx with memo has enough gas
fee = NewStdFee(9000, sdk.NewInt64Coin("atom", 0))
fee = NewStdFee(9000, sdk.Coins{sdk.NewInt64Coin("atom", 0)})
tx = newTestTxWithMemo(ctx, []sdk.Msg{msg}, privs, accnums, seqs, fee, strings.Repeat("0123456789", 10))
checkValidTx(t, anteHandler, ctx, tx, false)
}
Expand Down
58 changes: 42 additions & 16 deletions x/auth/client/txbuilder/txbuilder.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package context

import (
"fmt"

"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/keys"
"github.com/cosmos/cosmos-sdk/codec"
Expand All @@ -21,7 +23,31 @@ type TxBuilder struct {
SimulateGas bool
ChainID string
Memo string
Fee string
Fees sdk.Coins
}

// NewTxBuilder returns a new initialized TxBuilder
func NewTxBuilder(cdc *codec.Codec, accNumber, seq, gas uint64, gasAdj float64, simulate bool, chainID, memo string, fees sdk.Coins) TxBuilder {
// if chain ID is not specified manually, read default chain ID
if chainID == "" {
defaultChainID, err := sdk.DefaultChainID()
if err != nil {
panic(err)
}
chainID = defaultChainID
}

return TxBuilder{
Codec: cdc,
ChainID: chainID,
AccountNumber: accNumber,
Gas: gas,
GasAdjustment: gasAdj,
Sequence: seq,
SimulateGas: simulate,
Fees: fees,
Memo: memo,
}
}

// NewTxBuilderFromCLI returns a new initialized TxBuilder with parameters from
Expand All @@ -32,8 +58,14 @@ func NewTxBuilderFromCLI() TxBuilder {
if chainID == "" {
defaultChainID, err := sdk.DefaultChainID()
if err != nil {
chainID = defaultChainID
panic(err)
}
chainID = defaultChainID
}
feesStr := viper.GetString(client.FlagFee)
parsedFees, err := sdk.ParseCoins(feesStr)
fedekunze marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
panic(fmt.Sprintf("invalid coins: %s", feesStr))
}

return TxBuilder{
Expand All @@ -43,7 +75,7 @@ func NewTxBuilderFromCLI() TxBuilder {
GasAdjustment: viper.GetFloat64(client.FlagGasAdjustment),
Sequence: uint64(viper.GetInt64(client.FlagSequence)),
SimulateGas: client.GasFlagVar.Simulate,
Fee: viper.GetString(client.FlagFee),
Fees: parsedFees,
Memo: viper.GetString(client.FlagMemo),
}
}
Expand All @@ -67,8 +99,12 @@ func (bldr TxBuilder) WithGas(gas uint64) TxBuilder {
}

// WithFee returns a copy of the context with an updated fee.
func (bldr TxBuilder) WithFee(fee string) TxBuilder {
bldr.Fee = fee
func (bldr TxBuilder) WithFees(fee string) TxBuilder {
parsedFees, err := sdk.ParseCoins(fee)
if err != nil {
panic(fmt.Sprintf("invalid coins: %s", fee))
}
bldr.Fees = parsedFees
return bldr
}

Expand Down Expand Up @@ -98,23 +134,13 @@ func (bldr TxBuilder) Build(msgs []sdk.Msg) (StdSignMsg, error) {
return StdSignMsg{}, errors.Errorf("chain ID required but not specified")
}

fee := sdk.Coin{}
if bldr.Fee != "" {
parsedFee, err := sdk.ParseCoin(bldr.Fee)
if err != nil {
return StdSignMsg{}, err
}

fee = parsedFee
}

return StdSignMsg{
ChainID: bldr.ChainID,
AccountNumber: bldr.AccountNumber,
Sequence: bldr.Sequence,
Memo: bldr.Memo,
Msgs: msgs,
Fee: auth.NewStdFee(bldr.Gas, fee),
Fee: auth.NewStdFee(bldr.Gas, bldr.Fees),
}, nil
}

Expand Down
22 changes: 6 additions & 16 deletions x/auth/client/txbuilder/txbuilder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func TestTxBuilderBuild(t *testing.T) {
SimulateGas bool
ChainID string
Memo string
Fee string
Fees sdk.Coins
}
defaultMsg := []sdk.Msg{sdk.NewTestMsg(addr)}
tests := []struct {
Expand All @@ -46,33 +46,23 @@ func TestTxBuilderBuild(t *testing.T) {
GasAdjustment: 1.1,
SimulateGas: false,
ChainID: "test-chain",
Memo: "hello",
Fee: "1" + stakeTypes.DefaultBondDenom,
Memo: "hello from Voyager !",
Fees: sdk.Coins{sdk.NewCoin(stakeTypes.DefaultBondDenom, sdk.NewInt(1))},
},
defaultMsg,
StdSignMsg{
ChainID: "test-chain",
AccountNumber: 1,
Sequence: 1,
Memo: "hello",
Memo: "hello from Voyager !",
Msgs: defaultMsg,
Fee: auth.NewStdFee(100, sdk.NewCoin(stakeTypes.DefaultBondDenom, sdk.NewInt(1))),
Fee: auth.NewStdFee(100, sdk.Coins{sdk.NewCoin(stakeTypes.DefaultBondDenom, sdk.NewInt(1))}),
},
false,
},
}
for i, tc := range tests {
bldr := TxBuilder{
Codec: tc.fields.Codec,
AccountNumber: tc.fields.AccountNumber,
Sequence: tc.fields.Sequence,
Gas: tc.fields.Gas,
GasAdjustment: tc.fields.GasAdjustment,
SimulateGas: tc.fields.SimulateGas,
ChainID: tc.fields.ChainID,
Memo: tc.fields.Memo,
Fee: tc.fields.Fee,
}
bldr := NewTxBuilder(tc.fields.Codec, tc.fields.AccountNumber, tc.fields.Sequence, tc.fields.Gas, tc.fields.GasAdjustment, tc.fields.SimulateGas, tc.fields.ChainID, tc.fields.Memo, tc.fields.Fees)
got, err := bldr.Build(tc.msgs)
require.Equal(t, tc.wantErr, (err != nil), "TxBuilder.Build() error = %v, wantErr %v, tc %d", err, tc.wantErr, i)
if !reflect.DeepEqual(got, tc.want) {
Expand Down
2 changes: 1 addition & 1 deletion x/auth/stdtx.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ type StdFee struct {
Gas uint64 `json:"gas"`
}

func NewStdFee(gas uint64, amount ...sdk.Coin) StdFee {
func NewStdFee(gas uint64, amount sdk.Coins) StdFee {
return StdFee{
Amount: amount,
Gas: gas,
Expand Down
2 changes: 1 addition & 1 deletion x/bank/app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ var (
coins = sdk.Coins{sdk.NewInt64Coin("foocoin", 10)}
halfCoins = sdk.Coins{sdk.NewInt64Coin("foocoin", 5)}
manyCoins = sdk.Coins{sdk.NewInt64Coin("foocoin", 1), sdk.NewInt64Coin("barcoin", 1)}
freeFee = auth.NewStdFee(100000, sdk.Coins{sdk.NewInt64Coin("foocoin", 0)}...)
freeFee = auth.NewStdFee(100000, sdk.Coins{sdk.NewInt64Coin("foocoin", 0)})

sendMsg1 = MsgSend{
Inputs: []Input{NewInput(addr1, coins)},
Expand Down
2 changes: 1 addition & 1 deletion x/stake/test_common.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ var (
coins = sdk.Coins{sdk.NewCoin("foocoin", sdk.NewInt(10))}
fee = auth.NewStdFee(
100000,
sdk.Coins{sdk.NewCoin("foocoin", sdk.NewInt(0))}...,
sdk.Coins{sdk.NewCoin("foocoin", sdk.NewInt(0))},
)

commissionMsg = NewCommissionMsg(sdk.ZeroDec(), sdk.ZeroDec(), sdk.ZeroDec())
Expand Down