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]>
  • Loading branch information
3 people authored Dec 10, 2024
1 parent 6a7ecd8 commit 57b4d30
Show file tree
Hide file tree
Showing 69 changed files with 251 additions and 95 deletions.
1 change: 1 addition & 0 deletions client/v2/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,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"
}
2 changes: 1 addition & 1 deletion client/v2/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ require (
cosmossdk.io/errors v1.0.1
cosmossdk.io/log v1.5.0
cosmossdk.io/math v1.4.0
cosmossdk.io/schema v0.3.1-0.20241128094659-bd76b47e1d8b // indirect
cosmossdk.io/schema v0.4.0 // indirect
cosmossdk.io/store v1.1.1-0.20240418092142-896cdf1971bc // indirect
cosmossdk.io/x/staking v0.0.0-00010101000000-000000000000 // indirect
filippo.io/edwards25519 v1.1.0 // indirect
Expand Down
4 changes: 2 additions & 2 deletions client/v2/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ 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=
cosmossdk.io/schema v0.3.1-0.20241128094659-bd76b47e1d8b h1:svpFdulZRrYz+RTHu2u9CeKkMKrIHx5354vjiHerovo=
cosmossdk.io/schema v0.3.1-0.20241128094659-bd76b47e1d8b/go.mod h1:RDAhxIeNB4bYqAlF4NBJwRrgtnciMcyyg0DOKnhNZQQ=
cosmossdk.io/schema v0.4.0 h1:TrBs5BUnGqniAwEBVsjiisrAk3h3DK/zHLU1O8fRnO0=
cosmossdk.io/schema v0.4.0/go.mod h1:RDAhxIeNB4bYqAlF4NBJwRrgtnciMcyyg0DOKnhNZQQ=
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=
filippo.io/edwards25519 v1.1.0 h1:FNf4tywRC1HmFuKW5xopWpigGjJKiJSV0Cqo0cJWDaA=
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
3 changes: 1 addition & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ require (
cosmossdk.io/errors v1.0.1
cosmossdk.io/log v1.5.0
cosmossdk.io/math v1.4.0
cosmossdk.io/schema v0.3.1-0.20241128094659-bd76b47e1d8b
cosmossdk.io/schema v0.4.0
cosmossdk.io/store v1.1.1-0.20240418092142-896cdf1971bc
cosmossdk.io/x/bank v0.0.0-20240226161501-23359a0b6d91
cosmossdk.io/x/staking v0.0.0-00010101000000-000000000000
Expand Down Expand Up @@ -186,7 +186,6 @@ require (
// TODO remove after all modules have their own go.mods
replace (
cosmossdk.io/api => ./api
cosmossdk.io/schema => ./schema
cosmossdk.io/store => ./store
cosmossdk.io/x/bank => ./x/bank
cosmossdk.io/x/staking => ./x/staking
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ 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=
cosmossdk.io/schema v0.4.0 h1:TrBs5BUnGqniAwEBVsjiisrAk3h3DK/zHLU1O8fRnO0=
cosmossdk.io/schema v0.4.0/go.mod h1:RDAhxIeNB4bYqAlF4NBJwRrgtnciMcyyg0DOKnhNZQQ=
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
2 changes: 1 addition & 1 deletion indexer/postgres/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ go 1.12
// 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.3.0
require cosmossdk.io/schema v0.4.0

replace cosmossdk.io/schema => ../../schema
2 changes: 1 addition & 1 deletion indexer/postgres/tests/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ go 1.23

require (
cosmossdk.io/indexer/postgres v0.0.0-00010101000000-000000000000
cosmossdk.io/schema v0.3.0
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
Expand Down
3 changes: 1 addition & 2 deletions runtime/v2/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ go 1.23
replace (
cosmossdk.io/api => ../../api
cosmossdk.io/core/testing => ../../core/testing
cosmossdk.io/schema => ../../schema
cosmossdk.io/server/v2/appmanager => ../../server/v2/appmanager
cosmossdk.io/server/v2/stf => ../../server/v2/stf
cosmossdk.io/store/v2 => ../../store/v2
Expand All @@ -18,7 +17,7 @@ require (
cosmossdk.io/core v1.0.0-alpha.6
cosmossdk.io/depinject v1.1.0
cosmossdk.io/log v1.5.0
cosmossdk.io/schema v0.3.0
cosmossdk.io/schema v0.4.0
cosmossdk.io/server/v2/appmanager v0.0.0-00010101000000-000000000000
cosmossdk.io/server/v2/stf v0.0.0-00010101000000-000000000000
cosmossdk.io/store/v2 v2.0.0-00010101000000-000000000000
Expand Down
2 changes: 2 additions & 0 deletions runtime/v2/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ cosmossdk.io/errors/v2 v2.0.0-20240731132947-df72853b3ca5 h1:IQNdY2kB+k+1OM2DvqF
cosmossdk.io/errors/v2 v2.0.0-20240731132947-df72853b3ca5/go.mod h1:0CuYKkFHxc1vw2JC+t21THBCALJVROrWVR/3PQ1urpc=
cosmossdk.io/log v1.5.0 h1:dVdzPJW9kMrnAYyMf1duqacoidB9uZIl+7c6z0mnq0g=
cosmossdk.io/log v1.5.0/go.mod h1:Tr46PUJjiUthlwQ+hxYtUtPn4D/oCZXAkYevBeh5+FI=
cosmossdk.io/schema v0.4.0 h1:TrBs5BUnGqniAwEBVsjiisrAk3h3DK/zHLU1O8fRnO0=
cosmossdk.io/schema v0.4.0/go.mod h1:RDAhxIeNB4bYqAlF4NBJwRrgtnciMcyyg0DOKnhNZQQ=
github.com/DataDog/datadog-go v3.2.0+incompatible/go.mod h1:LButxg5PwREeZtORoXG3tL4fMGNddJ+vMq1mwgfaqoQ=
github.com/DataDog/zstd v1.5.5 h1:oWf5W7GtOLgp6bciQYDmhHHjdhYkALu6S/5Ni9ZgSvQ=
github.com/DataDog/zstd v1.5.5/go.mod h1:g4AWEaM3yOg3HYfnJ3YIawPnVdXJh9QME85blwSAmyw=
Expand Down
2 changes: 1 addition & 1 deletion schema/testing/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module cosmossdk.io/schema/testing
go 1.23

require (
cosmossdk.io/schema v0.0.0
cosmossdk.io/schema v0.4.0
github.com/cockroachdb/apd/v3 v3.2.1
github.com/stretchr/testify v1.10.0
github.com/tidwall/btree v1.7.0
Expand Down
3 changes: 1 addition & 2 deletions server/v2/cometbft/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ go 1.23.2
replace (
cosmossdk.io/api => ../../../api
cosmossdk.io/core/testing => ../../../core/testing
cosmossdk.io/schema => ../../../schema
cosmossdk.io/server/v2 => ../
cosmossdk.io/server/v2/appmanager => ../appmanager
cosmossdk.io/server/v2/stf => ../stf
Expand All @@ -25,7 +24,7 @@ require (
cosmossdk.io/errors v1.0.1
cosmossdk.io/errors/v2 v2.0.0-20240731132947-df72853b3ca5
cosmossdk.io/log v1.5.0
cosmossdk.io/schema v0.3.1-0.20241128094659-bd76b47e1d8b
cosmossdk.io/schema v0.4.0
cosmossdk.io/server/v2 v2.0.0-00010101000000-000000000000
cosmossdk.io/server/v2/appmanager v0.0.0-20240802110823-cffeedff643d
cosmossdk.io/server/v2/stf v0.0.0-20240708142107-25e99c54bac1
Expand Down
2 changes: 2 additions & 0 deletions server/v2/cometbft/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ 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=
cosmossdk.io/schema v0.4.0 h1:TrBs5BUnGqniAwEBVsjiisrAk3h3DK/zHLU1O8fRnO0=
cosmossdk.io/schema v0.4.0/go.mod h1:RDAhxIeNB4bYqAlF4NBJwRrgtnciMcyyg0DOKnhNZQQ=
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
3 changes: 1 addition & 2 deletions server/v2/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ go 1.23

replace (
cosmossdk.io/api => ../../api
cosmossdk.io/schema => ../../schema
cosmossdk.io/server/v2/appmanager => ./appmanager
cosmossdk.io/server/v2/stf => ./stf
cosmossdk.io/store/v2 => ../../store/v2
Expand Down Expand Up @@ -42,7 +41,7 @@ require (

require (
cosmossdk.io/errors/v2 v2.0.0-20240731132947-df72853b3ca5 // indirect
cosmossdk.io/schema v0.3.1-0.20241010135032-192601639cac // indirect
cosmossdk.io/schema v0.4.0 // indirect
github.com/DataDog/datadog-go v4.8.3+incompatible // indirect
github.com/DataDog/zstd v1.5.5 // indirect
github.com/Microsoft/go-winio v0.6.1 // indirect
Expand Down
2 changes: 2 additions & 0 deletions server/v2/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ cosmossdk.io/errors/v2 v2.0.0-20240731132947-df72853b3ca5 h1:IQNdY2kB+k+1OM2DvqF
cosmossdk.io/errors/v2 v2.0.0-20240731132947-df72853b3ca5/go.mod h1:0CuYKkFHxc1vw2JC+t21THBCALJVROrWVR/3PQ1urpc=
cosmossdk.io/log v1.5.0 h1:dVdzPJW9kMrnAYyMf1duqacoidB9uZIl+7c6z0mnq0g=
cosmossdk.io/log v1.5.0/go.mod h1:Tr46PUJjiUthlwQ+hxYtUtPn4D/oCZXAkYevBeh5+FI=
cosmossdk.io/schema v0.4.0 h1:TrBs5BUnGqniAwEBVsjiisrAk3h3DK/zHLU1O8fRnO0=
cosmossdk.io/schema v0.4.0/go.mod h1:RDAhxIeNB4bYqAlF4NBJwRrgtnciMcyyg0DOKnhNZQQ=
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/DataDog/datadog-go v3.2.0+incompatible/go.mod h1:LButxg5PwREeZtORoXG3tL4fMGNddJ+vMq1mwgfaqoQ=
github.com/DataDog/datadog-go v4.8.3+incompatible h1:fNGaYSuObuQb5nzeTQqowRAd9bpDIRRV4/gUtIBjh8Q=
Expand Down
4 changes: 1 addition & 3 deletions server/v2/stf/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,11 @@ go 1.23
require (
cosmossdk.io/core v1.0.0-alpha.6
cosmossdk.io/core/testing v0.0.0-20241108153815-606544c7be7e
cosmossdk.io/schema v0.3.0
cosmossdk.io/schema v0.4.0
github.com/cosmos/gogoproto v1.7.0
github.com/tidwall/btree v1.7.0
)

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

require (
github.com/google/go-cmp v0.6.0 // indirect
google.golang.org/protobuf v1.35.2 // indirect
Expand Down
2 changes: 2 additions & 0 deletions server/v2/stf/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ cosmossdk.io/core v1.0.0-alpha.6 h1:5ukC4JcQKmemLQXcAgu/QoOvJI50hpBkIIg4ZT2EN8E=
cosmossdk.io/core v1.0.0-alpha.6/go.mod h1:3u9cWq1FAVtiiCrDPpo4LhR+9V6k/ycSG4/Y/tREWCY=
cosmossdk.io/core/testing v0.0.0-20241108153815-606544c7be7e h1:F+ScucYxwrrDJU8guJXQXpGhdpziYSbxW6HMP2wCNxs=
cosmossdk.io/core/testing v0.0.0-20241108153815-606544c7be7e/go.mod h1:3YvVv9aJayjPhdX0DY1IMrGse4sR63hNBWx2VtDWjGQ=
cosmossdk.io/schema v0.4.0 h1:TrBs5BUnGqniAwEBVsjiisrAk3h3DK/zHLU1O8fRnO0=
cosmossdk.io/schema v0.4.0/go.mod h1:RDAhxIeNB4bYqAlF4NBJwRrgtnciMcyyg0DOKnhNZQQ=
github.com/cosmos/gogoproto v1.7.0 h1:79USr0oyXAbxg3rspGh/m4SWNyoz/GLaAh0QlCe2fro=
github.com/cosmos/gogoproto v1.7.0/go.mod h1:yWChEv5IUEYURQasfyBW5ffkMHR/90hiHgbNgrtp4j0=
github.com/golang/protobuf v1.5.3 h1:KhyjKVUg7Usr/dYsdSqoFveMYd5ko72D+zANwlG1mmg=
Expand Down
Loading

0 comments on commit 57b4d30

Please sign in to comment.