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

refactor(client/v2): use address codec instead of global #19026

Merged
merged 3 commits into from
Jan 11, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
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"
authtx "cosmossdk.io/x/auth/tx"

"github.com/cosmos/cosmos-sdk/client"
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"))
}
Loading