Skip to content

Commit

Permalink
fix(client/v2/autocli): add CoinDec flag (#22817)
Browse files Browse the repository at this point in the history
Co-authored-by: Marko <[email protected]>
Co-authored-by: Julien Robert <[email protected]>
(cherry picked from commit 57b4d30)

# Conflicts:
#	client/v2/go.mod
#	client/v2/go.sum
#	go.mod
#	go.sum
#	indexer/postgres/go.mod
#	indexer/postgres/tests/go.mod
#	runtime/v2/go.mod
#	runtime/v2/go.sum
#	schema/testing/go.mod
#	server/v2/cometbft/go.mod
#	server/v2/cometbft/go.sum
#	server/v2/go.mod
#	server/v2/go.sum
#	server/v2/stf/go.mod
#	server/v2/stf/go.sum
#	simapp/go.mod
#	simapp/go.sum
#	simapp/v2/go.mod
#	simapp/v2/go.sum
#	tests/go.mod
#	tests/go.sum
#	x/accounts/defaults/base/go.mod
#	x/accounts/defaults/base/go.sum
#	x/accounts/defaults/lockup/go.mod
#	x/accounts/defaults/lockup/go.sum
#	x/accounts/defaults/multisig/go.mod
#	x/accounts/defaults/multisig/go.sum
#	x/accounts/go.mod
#	x/accounts/go.sum
#	x/authz/go.mod
#	x/authz/go.sum
#	x/bank/go.mod
#	x/bank/go.sum
#	x/circuit/go.mod
#	x/circuit/go.sum
#	x/consensus/go.mod
#	x/consensus/go.sum
#	x/distribution/go.mod
#	x/distribution/go.sum
#	x/epochs/go.mod
#	x/epochs/go.sum
#	x/evidence/go.mod
#	x/evidence/go.sum
#	x/feegrant/go.mod
#	x/feegrant/go.sum
#	x/gov/go.mod
#	x/gov/go.sum
#	x/group/go.mod
#	x/group/go.sum
#	x/mint/go.mod
#	x/mint/go.sum
#	x/nft/go.mod
#	x/nft/go.sum
#	x/params/go.mod
#	x/params/go.sum
#	x/protocolpool/go.mod
#	x/protocolpool/go.sum
#	x/slashing/go.mod
#	x/slashing/go.sum
#	x/staking/go.mod
#	x/staking/go.sum
#	x/upgrade/go.mod
#	x/upgrade/go.sum
  • Loading branch information
JulianToledano authored and mergify[bot] committed Dec 10, 2024
1 parent 0cf6cd8 commit f7f07fb
Show file tree
Hide file tree
Showing 69 changed files with 1,710 additions and 11 deletions.
1 change: 1 addition & 0 deletions client/v2/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ Ref: https://keepachangelog.com/en/1.0.0/

* [#21853](https://github.com/cosmos/cosmos-sdk/pull/21853) Fix `*big.Int` unmarshalling in txs.
* [#22576](https://github.com/cosmos/cosmos-sdk/pull/22576) Fix duplicate command addition in `autocli` when custom enhanced command has a different name than module name
* [#22817](https://github.com/cosmos/cosmos-sdk/pull/22817) Add DecCoin support in autocli flag builder.

## [v2.0.0-beta.5] - 2024-09-18

Expand Down
1 change: 1 addition & 0 deletions client/v2/autocli/flag/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ func (b *Builder) init() {
b.messageFlagTypes["google.protobuf.Timestamp"] = timestampType{}
b.messageFlagTypes["google.protobuf.Duration"] = durationType{}
b.messageFlagTypes["cosmos.base.v1beta1.Coin"] = coinType{}
b.messageFlagTypes["cosmos.base.v1beta1.DecCoin"] = decCoinType{}
}

if b.scalarFlagTypes == nil {
Expand Down
4 changes: 4 additions & 0 deletions client/v2/autocli/flag/coin.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ func (c *coinValue) Get(protoreflect.Value) (protoreflect.Value, error) {
}

func (c *coinValue) String() string {
if c.value == nil {
return ""
}

return c.value.String()
}

Expand Down
58 changes: 58 additions & 0 deletions client/v2/autocli/flag/dec_coin.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package flag

import (
"context"
"errors"
"strings"

"google.golang.org/protobuf/reflect/protoreflect"

basev1beta1 "cosmossdk.io/api/cosmos/base/v1beta1"
"cosmossdk.io/client/v2/internal/coins"
)

type decCoinType struct{}

type decCoinValue struct {
value *basev1beta1.DecCoin
}

func (c decCoinType) NewValue(*context.Context, *Builder) Value {
return &decCoinValue{}
}

func (c decCoinType) DefaultValue() string {
return "zero"
}

func (c *decCoinValue) Get(protoreflect.Value) (protoreflect.Value, error) {
if c.value == nil {
return protoreflect.Value{}, nil
}
return protoreflect.ValueOfMessage(c.value.ProtoReflect()), nil
}

func (c *decCoinValue) String() string {
if c.value == nil {
return ""
}

return c.value.String()
}

func (c *decCoinValue) Set(stringValue string) error {
if strings.Contains(stringValue, ",") {
return errors.New("coin flag must be a single coin, specific multiple coins with multiple flags or spaces")
}

coin, err := coins.ParseDecCoin(stringValue)
if err != nil {
return err
}
c.value = coin
return nil
}

func (c *decCoinValue) Type() string {
return "cosmos.base.v1beta1.DecCoin"
}
5 changes: 5 additions & 0 deletions client/v2/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,13 @@ require (
cosmossdk.io/errors v1.0.1 // indirect
cosmossdk.io/log v1.5.0
cosmossdk.io/math v1.4.0
<<<<<<< HEAD
cosmossdk.io/schema v0.3.1-0.20241209183624-332d0b106d1b // indirect
cosmossdk.io/store v1.1.1-0.20240909133312-50288938d1b6 // indirect
=======
cosmossdk.io/schema v0.4.0 // indirect
cosmossdk.io/store v1.1.1-0.20240418092142-896cdf1971bc // indirect
>>>>>>> 57b4d3003 (fix(client/v2/autocli): add CoinDec flag (#22817))
cosmossdk.io/x/staking v0.0.0-00010101000000-000000000000 // indirect
filippo.io/edwards25519 v1.1.0 // indirect
github.com/99designs/go-keychain v0.0.0-20191008050251-8e49817e8af4 // indirect
Expand Down
5 changes: 5 additions & 0 deletions client/v2/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,15 @@ cosmossdk.io/log v1.5.0 h1:dVdzPJW9kMrnAYyMf1duqacoidB9uZIl+7c6z0mnq0g=
cosmossdk.io/log v1.5.0/go.mod h1:Tr46PUJjiUthlwQ+hxYtUtPn4D/oCZXAkYevBeh5+FI=
cosmossdk.io/math v1.4.0 h1:XbgExXFnXmF/CccPPEto40gOO7FpWu9yWNAZPN3nkNQ=
cosmossdk.io/math v1.4.0/go.mod h1:O5PkD4apz2jZs4zqFdTr16e1dcaQCc5z6lkEnrrppuk=
<<<<<<< HEAD
cosmossdk.io/schema v0.3.1-0.20241209183624-332d0b106d1b h1:ymo4PrM7Q4tPpEtce6IdH/kurphOAE6DmzHux1tniNY=
cosmossdk.io/schema v0.3.1-0.20241209183624-332d0b106d1b/go.mod h1:RDAhxIeNB4bYqAlF4NBJwRrgtnciMcyyg0DOKnhNZQQ=
cosmossdk.io/store v1.0.0-rc.0.0.20241204123127-eb3bf8b0469d h1:KQM4Q6kjwlM4HuDZRV8/ZDXX3whjfStndYNTsRrbboQ=
cosmossdk.io/store v1.0.0-rc.0.0.20241204123127-eb3bf8b0469d/go.mod h1:oZBBY4BrkYnghr6MFL0MP5mGqpkPedHcWkXwXddd6tU=
=======
cosmossdk.io/schema v0.4.0 h1:TrBs5BUnGqniAwEBVsjiisrAk3h3DK/zHLU1O8fRnO0=
cosmossdk.io/schema v0.4.0/go.mod h1:RDAhxIeNB4bYqAlF4NBJwRrgtnciMcyyg0DOKnhNZQQ=
>>>>>>> 57b4d3003 (fix(client/v2/autocli): add CoinDec flag (#22817))
cosmossdk.io/x/protocolpool v0.0.0-20230925135524-a1bc045b3190 h1:XQJj9Dv9Gtze0l2TF79BU5lkP6MkUveTUuKICmxoz+o=
cosmossdk.io/x/protocolpool v0.0.0-20230925135524-a1bc045b3190/go.mod h1:7WUGupOvmlHJoIMBz1JbObQxeo6/TDiuDBxmtod8HRg=
cosmossdk.io/x/tx v1.0.0-alpha.2 h1:UW80FMm7B0fiAMsrfe5+HabSJ3XBg+tQa6/GK9prqWk=
Expand Down
39 changes: 33 additions & 6 deletions client/v2/internal/coins/format.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,47 @@ var coinRegex = regexp.MustCompile(`^(\d+(\.\d+)?)([a-zA-Z][a-zA-Z0-9\/\:\._\-]{
// ParseCoin parses a coin from a string. The string must be in the format
// <amount><denom>, where <amount> is a number and <denom> is a valid denom.
func ParseCoin(input string) (*basev1beta1.Coin, error) {
amount, denom, err := parseCoin(input)
if err != nil {
return nil, err
}

return &basev1beta1.Coin{
Amount: amount,
Denom: denom,
}, nil
}

// ParseDecCoin parses a decCoin from a string. The string must be in the format
// <amount><denom>, where <amount> is a number and <denom> is a valid denom.
func ParseDecCoin(input string) (*basev1beta1.DecCoin, error) {
amount, denom, err := parseCoin(input)
if err != nil {
return nil, err
}

return &basev1beta1.DecCoin{
Amount: amount,
Denom: denom,
}, nil
}

// parseCoin parses a coin string into its amount and denom components.
// The input string must be in the format <amount><denom>.
// It returns the amount string, denom string, and any error encountered.
// Returns an error if the input is empty or doesn't match the expected format.
func parseCoin(input string) (amount, denom string, err error) {
input = strings.TrimSpace(input)

if input == "" {
return nil, errors.New("empty input when parsing coin")
return "", "", errors.New("empty input when parsing coin")
}

matches := coinRegex.FindStringSubmatch(input)

if len(matches) == 0 {
return nil, errors.New("invalid input format")
return "", "", errors.New("invalid input format")
}

return &basev1beta1.Coin{
Amount: matches[1],
Denom: matches[3],
}, nil
return matches[1], matches[3], nil
}
66 changes: 61 additions & 5 deletions client/v2/internal/coins/format_test.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,72 @@
package coins_test
package coins

import (
"testing"

"github.com/stretchr/testify/require"

"cosmossdk.io/client/v2/internal/coins"
)

func TestDecodeCoin(t *testing.T) {
func Test_parseCoin(t *testing.T) {
tests := []struct {
name string
input string
amount string
denom string
err string
}{
{
name: "ok",
input: "1000stake",
amount: "1000",
denom: "stake",
},
{
name: "empty",
input: "",
err: "empty input when parsing coin",
},
{
name: "empty denom",
input: "1000",
err: "invalid input format",
},
{
name: "empty amount",
input: "stake",
err: "invalid input format",
},
{
name: "<denom><amount> format",
input: "stake1000",
err: "invalid input format",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
amount, denom, err := parseCoin(tt.input)
if tt.err != "" {
require.Error(t, err)
require.Contains(t, err.Error(), tt.err)
} else {
require.NoError(t, err)
require.Equal(t, tt.amount, amount)
require.Equal(t, tt.denom, denom)
}
})
}
}

func TestParseCoin(t *testing.T) {
encodedCoin := "1000000000foo"
coin, err := ParseCoin(encodedCoin)
require.NoError(t, err)
require.Equal(t, "1000000000", coin.Amount)
require.Equal(t, "foo", coin.Denom)
}

func TestParseDecCoin(t *testing.T) {
encodedCoin := "1000000000foo"
coin, err := coins.ParseCoin(encodedCoin)
coin, err := ParseDecCoin(encodedCoin)
require.NoError(t, err)
require.Equal(t, "1000000000", coin.Amount)
require.Equal(t, "foo", coin.Denom)
Expand Down
10 changes: 10 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,13 @@ require (
cosmossdk.io/errors v1.0.1
cosmossdk.io/log v1.5.0
cosmossdk.io/math v1.4.0
<<<<<<< HEAD
cosmossdk.io/schema v0.3.1-0.20241209183624-332d0b106d1b
cosmossdk.io/store v1.1.1-0.20240909133312-50288938d1b6
=======
cosmossdk.io/schema v0.4.0
cosmossdk.io/store v1.1.1-0.20240418092142-896cdf1971bc
>>>>>>> 57b4d3003 (fix(client/v2/autocli): add CoinDec flag (#22817))
cosmossdk.io/x/bank v0.0.0-20240226161501-23359a0b6d91
cosmossdk.io/x/staking v0.0.0-00010101000000-000000000000
cosmossdk.io/x/tx v1.0.0-alpha.2 // main
Expand Down Expand Up @@ -184,10 +189,15 @@ require (
// )
// TODO remove after all modules have their own go.mods
replace (
<<<<<<< HEAD
// pseudo version lower than the latest tag
cosmossdk.io/api => cosmossdk.io/api v0.7.3-0.20241127063259-f296a5005ce8 // main
// pseudo version lower than the latest tag
cosmossdk.io/store => cosmossdk.io/store v1.0.0-rc.0.0.20241204123127-eb3bf8b0469d // main
=======
cosmossdk.io/api => ./api
cosmossdk.io/store => ./store
>>>>>>> 57b4d3003 (fix(client/v2/autocli): add CoinDec flag (#22817))
cosmossdk.io/x/bank => ./x/bank
cosmossdk.io/x/staking => ./x/staking
)
Expand Down
5 changes: 5 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,17 @@ cosmossdk.io/log v1.5.0 h1:dVdzPJW9kMrnAYyMf1duqacoidB9uZIl+7c6z0mnq0g=
cosmossdk.io/log v1.5.0/go.mod h1:Tr46PUJjiUthlwQ+hxYtUtPn4D/oCZXAkYevBeh5+FI=
cosmossdk.io/math v1.4.0 h1:XbgExXFnXmF/CccPPEto40gOO7FpWu9yWNAZPN3nkNQ=
cosmossdk.io/math v1.4.0/go.mod h1:O5PkD4apz2jZs4zqFdTr16e1dcaQCc5z6lkEnrrppuk=
<<<<<<< HEAD
cosmossdk.io/schema v0.3.1-0.20241209183624-332d0b106d1b h1:ymo4PrM7Q4tPpEtce6IdH/kurphOAE6DmzHux1tniNY=
cosmossdk.io/schema v0.3.1-0.20241209183624-332d0b106d1b/go.mod h1:RDAhxIeNB4bYqAlF4NBJwRrgtnciMcyyg0DOKnhNZQQ=
cosmossdk.io/store v1.0.0-rc.0.0.20241204123127-eb3bf8b0469d h1:KQM4Q6kjwlM4HuDZRV8/ZDXX3whjfStndYNTsRrbboQ=
cosmossdk.io/store v1.0.0-rc.0.0.20241204123127-eb3bf8b0469d/go.mod h1:oZBBY4BrkYnghr6MFL0MP5mGqpkPedHcWkXwXddd6tU=
cosmossdk.io/x/tx v1.0.0-alpha.2 h1:UW80FMm7B0fiAMsrfe5+HabSJ3XBg+tQa6/GK9prqWk=
cosmossdk.io/x/tx v1.0.0-alpha.2/go.mod h1:r4yTKSJ7ZCCR95YbBfY3nfvbgNw6m9F6f25efWYYQWo=
=======
cosmossdk.io/schema v0.4.0 h1:TrBs5BUnGqniAwEBVsjiisrAk3h3DK/zHLU1O8fRnO0=
cosmossdk.io/schema v0.4.0/go.mod h1:RDAhxIeNB4bYqAlF4NBJwRrgtnciMcyyg0DOKnhNZQQ=
>>>>>>> 57b4d3003 (fix(client/v2/autocli): add CoinDec flag (#22817))
filippo.io/edwards25519 v1.1.0 h1:FNf4tywRC1HmFuKW5xopWpigGjJKiJSV0Cqo0cJWDaA=
filippo.io/edwards25519 v1.1.0/go.mod h1:BxyFTGdWcka3PhytdK4V28tE5sGfRvvvRV7EaN4VDT4=
github.com/99designs/go-keychain v0.0.0-20191008050251-8e49817e8af4 h1:/vQbFIOMbk2FiG/kXiLl8BRyzTWDw7gX/Hz7Dd5eDMs=
Expand Down
13 changes: 13 additions & 0 deletions indexer/postgres/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
module cosmossdk.io/indexer/postgres

// NOTE: we are staying on an earlier version of golang to avoid problems building
// with older codebases.
go 1.12

// NOTE: cosmossdk.io/schema should be the only dependency here
// so there are no problems building this with any version of the SDK.
// This module should only use the golang standard library (database/sql)
// and cosmossdk.io/indexer/base.
require cosmossdk.io/schema v0.4.0

replace cosmossdk.io/schema => ../../schema
41 changes: 41 additions & 0 deletions indexer/postgres/tests/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
module cosmossdk.io/indexer/postgres/testing

go 1.23

require (
cosmossdk.io/indexer/postgres v0.0.0-00010101000000-000000000000
cosmossdk.io/schema v0.4.0
cosmossdk.io/schema/testing v0.0.0
github.com/fergusstrange/embedded-postgres v1.29.0
github.com/hashicorp/consul/sdk v0.16.1
github.com/jackc/pgx/v5 v5.7.1
github.com/stretchr/testify v1.10.0
gotest.tools/v3 v3.5.1
)

require (
github.com/cockroachdb/apd/v3 v3.2.1 // indirect
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
github.com/google/go-cmp v0.6.0 // indirect
github.com/jackc/pgpassfile v1.0.0 // indirect
github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 // indirect
github.com/jackc/puddle/v2 v2.2.2 // indirect
github.com/kr/text v0.2.0 // indirect
github.com/lib/pq v1.10.9 // indirect
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
github.com/rogpeppe/go-internal v1.12.0 // indirect
github.com/tidwall/btree v1.7.0 // indirect
github.com/xi2/xz v0.0.0-20171230120015-48954b6210f8 // indirect
golang.org/x/crypto v0.29.0 // indirect
golang.org/x/sync v0.9.0 // indirect
golang.org/x/sys v0.27.0 // indirect
golang.org/x/text v0.20.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
pgregory.net/rapid v1.1.0 // indirect
)

replace cosmossdk.io/indexer/postgres => ../.

replace cosmossdk.io/schema => ../../../schema

replace cosmossdk.io/schema/testing => ../../../schema/testing
Loading

0 comments on commit f7f07fb

Please sign in to comment.