Skip to content

Commit

Permalink
refactor(client/v2): use address codec instead of global (backport #1…
Browse files Browse the repository at this point in the history
…9026) (#19029)

Co-authored-by: Julien Robert <[email protected]>
Co-authored-by: marbar3778 <[email protected]>
  • Loading branch information
3 people authored Jan 11, 2024
1 parent b3ec532 commit c0beb19
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 33 deletions.
31 changes: 1 addition & 30 deletions client/v2/autocli/builder.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
package autocli

import (
"errors"

"github.com/spf13/cobra"
"google.golang.org/grpc"

"cosmossdk.io/client/v2/autocli/flag"
"cosmossdk.io/client/v2/autocli/keyring"

"github.com/cosmos/cosmos-sdk/client"
authtx "github.com/cosmos/cosmos-sdk/x/auth/tx"
Expand Down Expand Up @@ -35,32 +32,6 @@ type Builder struct {

// ValidateAndComplete the builder fields.
// It returns an error if any of the required fields are missing.
// If the Logger is nil, it will be set to a nop logger.
// If the keyring is nil, it will be set to a no keyring.
func (b *Builder) ValidateAndComplete() error {
if b.Builder.AddressCodec == nil {
return errors.New("address codec is required in flag builder")
}

if b.Builder.ValidatorAddressCodec == nil {
return errors.New("validator address codec is required in flag builder")
}

if b.Builder.ConsensusAddressCodec == nil {
return errors.New("consensus address codec is required in flag builder")
}

if b.Builder.Keyring == nil {
b.Keyring = keyring.NoKeyring{}
}

if b.Builder.TypeResolver == nil {
return errors.New("type resolver is required in flag builder")
}

if b.Builder.FileResolver == nil {
return errors.New("file resolver is required in flag builder")
}

return nil
return b.Builder.ValidateAndComplete()
}
7 changes: 5 additions & 2 deletions client/v2/autocli/flag/address.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import (
"github.com/cosmos/cosmos-sdk/codec/types"
cryptocodec "github.com/cosmos/cosmos-sdk/crypto/codec"
cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types"
sdk "github.com/cosmos/cosmos-sdk/types"
)

type addressStringType struct{}
Expand Down Expand Up @@ -133,6 +132,10 @@ func (a *consensusAddressValue) Set(s string) error {
return fmt.Errorf("input isn't a pubkey %w or is an invalid account address: %w", err, err2)
}

a.value = sdk.ConsAddress(pk.Address()).String()
a.value, err = a.addressCodec.BytesToString(pk.Address())
if err != nil {
return fmt.Errorf("invalid pubkey address: %w", err)
}

return nil
}
32 changes: 32 additions & 0 deletions client/v2/autocli/flag/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package flag

import (
"context"
"errors"
"fmt"
"strconv"

Expand Down Expand Up @@ -73,6 +74,37 @@ func (b *Builder) init() {
}
}

// ValidateAndComplete the flag builder fields.
// It returns an error if any of the required fields are missing.
// If the keyring is nil, it will be set to a no keyring.
func (b *Builder) ValidateAndComplete() error {
if b.AddressCodec == nil {
return errors.New("address codec is required in flag builder")
}

if b.ValidatorAddressCodec == nil {
return errors.New("validator address codec is required in flag builder")
}

if b.ConsensusAddressCodec == nil {
return errors.New("consensus address codec is required in flag builder")
}

if b.Keyring == nil {
b.Keyring = keyring.NoKeyring{}
}

if b.TypeResolver == nil {
return errors.New("type resolver is required in flag builder")
}

if b.FileResolver == nil {
return errors.New("file resolver is required in flag builder")
}

return nil
}

// DefineMessageFlagType allows to extend custom protobuf message type handling for flags (and positional arguments).
func (b *Builder) DefineMessageFlagType(messageName protoreflect.FullName, flagType Type) {
b.init()
Expand Down
1 change: 0 additions & 1 deletion client/v2/autocli/query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -729,6 +729,5 @@ func TestDurationMarshal(t *testing.T) {

out, err := runCmd(fixture.conn, fixture.b, buildModuleQueryCommand, "echo", "1", "abc", "--duration", "1s")
assert.NilError(t, err)
fmt.Println(out.String())
assert.Assert(t, strings.Contains(out.String(), "duration: 1s"))
}

0 comments on commit c0beb19

Please sign in to comment.