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

feat: custom get signers #16340

Merged
merged 34 commits into from
Jun 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
b20a1df
feat: get signers extension interface
aaronc May 16, 2023
badd826
WIP on defining custom get signers
aaronc May 16, 2023
00eefcf
add DefineCustomGetSigners
aaronc May 16, 2023
d8290fe
make vulncheck fix
kocubinski May 30, 2023
7a6bc71
add a custom signers test
kocubinski May 30, 2023
a275687
Merge branch 'main' of github.com:cosmos/cosmos-sdk into kocubinski/1…
kocubinski May 31, 2023
eefa8ae
Merge branch 'main' of github.com:cosmos/cosmos-sdk into kocubinski/1…
kocubinski May 31, 2023
ccdb3c0
remove replace
kocubinski May 31, 2023
bbfe4aa
still need replace for new annotation
kocubinski May 31, 2023
d8022de
no custom signers annotation
kocubinski May 31, 2023
a44534a
Merge branch 'main' of github.com:cosmos/cosmos-sdk into kocubinski/1…
kocubinski May 31, 2023
a1db0df
go mod tidy
kocubinski May 31, 2023
3cac445
rm unused import
kocubinski May 31, 2023
c088db2
specify CustomGetSignersFuncs as inputs
kocubinski May 31, 2023
ee6fa69
fix x/tx test
kocubinski May 31, 2023
ea17470
need x/tx replace in x/nft/go.mod
kocubinski May 31, 2023
5dce52b
works, but not how I want it to
kocubinski Jun 1, 2023
49a9013
simplify and use invoker
kocubinski Jun 1, 2023
2f4772e
remove signingContext validation from ProvideApp
kocubinski Jun 1, 2023
c6af01c
Merge branch 'main' into kocubinski/16112-custom-signers
kocubinski Jun 1, 2023
b01d359
changelog
kocubinski Jun 1, 2023
49d6ccd
Merge branch 'kocubinski/16112-custom-signers' of github.com:cosmos/c…
kocubinski Jun 1, 2023
66aedff
lint fixg
kocubinski Jun 1, 2023
9e7a2bb
seems better
kocubinski Jun 1, 2023
de3816c
add comments
kocubinski Jun 2, 2023
eb72031
refactor moving files
kocubinski Jun 2, 2023
9138f2c
adjust depinject test to fail during startup
kocubinski Jun 2, 2023
410935f
fix comment
kocubinski Jun 2, 2023
bdbace3
Merge branch 'main' of github.com:cosmos/cosmos-sdk into kocubinski/1…
kocubinski Jun 5, 2023
0d6f08f
disallow custom signer with proto annotation in Validate
kocubinski Jun 5, 2023
6955df2
Merge branch 'main' of github.com:cosmos/cosmos-sdk into kocubinski/1…
kocubinski Jun 5, 2023
5445754
fix validate logic
kocubinski Jun 5, 2023
e3dd1c3
linting fix
kocubinski Jun 5, 2023
3947ced
Merge branch 'main' of github.com:cosmos/cosmos-sdk into kocubinski/1…
kocubinski Jun 5, 2023
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
9 changes: 6 additions & 3 deletions codec/proto_codec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

bankv1beta1 "cosmossdk.io/api/cosmos/bank/v1beta1"
basev1beta1 "cosmossdk.io/api/cosmos/base/v1beta1"
"cosmossdk.io/x/tx/signing"
"github.com/cosmos/gogoproto/proto"
"github.com/stretchr/testify/require"
"google.golang.org/grpc/codes"
Expand Down Expand Up @@ -176,9 +177,11 @@ func BenchmarkProtoCodecMarshalLengthPrefixed(b *testing.B) {

func TestGetSigners(t *testing.T) {
interfaceRegistry, err := types.NewInterfaceRegistryWithOptions(types.InterfaceRegistryOptions{
AddressCodec: testAddressCodec{},
ValidatorAddressCodec: testAddressCodec{},
ProtoFiles: protoregistry.GlobalFiles,
SigningOptions: signing.Options{
AddressCodec: testAddressCodec{},
ValidatorAddressCodec: testAddressCodec{},
},
ProtoFiles: protoregistry.GlobalFiles,
})
require.NoError(t, err)
cdc := codec.NewProtoCodec(interfaceRegistry)
Expand Down
9 changes: 6 additions & 3 deletions codec/testutil/codec.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package testutil

import (
"cosmossdk.io/x/tx/signing"
"github.com/cosmos/gogoproto/proto"

"github.com/cosmos/cosmos-sdk/codec"
Expand All @@ -27,9 +28,11 @@ func (o CodecOptions) NewInterfaceRegistry() codectypes.InterfaceRegistry {
}

ir, err := codectypes.NewInterfaceRegistryWithOptions(codectypes.InterfaceRegistryOptions{
ProtoFiles: proto.HybridResolver,
AddressCodec: address.NewBech32Codec(accAddressPrefix),
ValidatorAddressCodec: address.NewBech32Codec(valAddressPrefix),
ProtoFiles: proto.HybridResolver,
SigningOptions: signing.Options{
AddressCodec: address.NewBech32Codec(accAddressPrefix),
ValidatorAddressCodec: address.NewBech32Codec(valAddressPrefix),
},
})
if err != nil {
panic(err)
Expand Down
25 changes: 9 additions & 16 deletions codec/types/interface_registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ import (
"google.golang.org/protobuf/reflect/protodesc"
"google.golang.org/protobuf/reflect/protoreflect"

"cosmossdk.io/core/address"

"cosmossdk.io/x/tx/signing"
)

Expand Down Expand Up @@ -114,9 +112,11 @@ type interfaceMap = map[string]reflect.Type
// NewInterfaceRegistry returns a new InterfaceRegistry
func NewInterfaceRegistry() InterfaceRegistry {
registry, err := NewInterfaceRegistryWithOptions(InterfaceRegistryOptions{
ProtoFiles: proto.HybridResolver,
AddressCodec: failingAddressCodec{},
ValidatorAddressCodec: failingAddressCodec{},
ProtoFiles: proto.HybridResolver,
SigningOptions: signing.Options{
AddressCodec: failingAddressCodec{},
ValidatorAddressCodec: failingAddressCodec{},
},
})
if err != nil {
panic(err)
Expand All @@ -129,11 +129,8 @@ type InterfaceRegistryOptions struct {
// ProtoFiles is the set of files to use for the registry. It is required.
ProtoFiles signing.ProtoFileResolver

// AddressCodec is the address codec to use for the registry. It is required.
AddressCodec address.Codec

// ValidatorAddressCodec is the validator address codec to use for the registry. It is required.
ValidatorAddressCodec address.Codec
// SigningOptions are the signing options to use for the registry.
SigningOptions signing.Options
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Still we just require a signing context to be passed in? Seems like signing options already takes proto files already so there's some redundancy here

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could but signing.Options permits an nil ProtoFileResolver while interface registry does not. What do you think we ought to do?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe just enforce the check at the interface registry level, or allow a nil resolver and default to hybrid resolver. But duplicated fields in both options structures might be confusing right?

}

// NewInterfaceRegistryWithOptions returns a new InterfaceRegistry with the given options.
Expand All @@ -142,12 +139,8 @@ func NewInterfaceRegistryWithOptions(options InterfaceRegistryOptions) (Interfac
return nil, fmt.Errorf("proto files must be provided")
}

signingCtx, err := signing.NewContext(signing.Options{
FileResolver: options.ProtoFiles,
TypeResolver: nil,
AddressCodec: options.AddressCodec,
ValidatorAddressCodec: options.ValidatorAddressCodec,
})
options.SigningOptions.FileResolver = options.ProtoFiles
signingCtx, err := signing.NewContext(options.SigningOptions)
if err != nil {
return nil, err
}
Expand Down
58 changes: 34 additions & 24 deletions runtime/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
appv1alpha1 "cosmossdk.io/api/cosmos/app/v1alpha1"
"cosmossdk.io/depinject"
"cosmossdk.io/log"
"cosmossdk.io/x/tx/signing"
"github.com/cosmos/gogoproto/proto"
"google.golang.org/protobuf/reflect/protodesc"
"google.golang.org/protobuf/reflect/protoregistry"
Expand All @@ -19,6 +20,7 @@ import (
"cosmossdk.io/core/header"
"cosmossdk.io/core/store"
storetypes "cosmossdk.io/store/types"

"github.com/cosmos/cosmos-sdk/codec/address"

"github.com/cosmos/cosmos-sdk/baseapp"
Expand Down Expand Up @@ -60,6 +62,7 @@ func init() {
appmodule.Register(&runtimev1alpha1.Module{},
appmodule.Provide(
ProvideApp,
ProvideInterfaceRegistry,
ProvideKVStoreKey,
ProvideTransientStoreKey,
ProvideMemoryStoreKey,
Expand All @@ -76,8 +79,7 @@ func init() {
)
}

func ProvideApp() (
codectypes.InterfaceRegistry,
func ProvideApp(interfaceRegistry codectypes.InterfaceRegistry) (
codec.Codec,
*codec.LegacyAmino,
*AppBuilder,
Expand All @@ -98,27 +100,6 @@ func ProvideApp() (
_, _ = fmt.Fprintln(os.Stderr, err.Error())
}

interfaceRegistry, err := codectypes.NewInterfaceRegistryWithOptions(codectypes.InterfaceRegistryOptions{
ProtoFiles: proto.HybridResolver,
// using the global prefixes is a temporary solution until we refactor this
// to get the address.Codec's from the container
AddressCodec: address.Bech32Codec{
Bech32Prefix: sdk.GetConfig().GetBech32AccountAddrPrefix(),
},
ValidatorAddressCodec: address.Bech32Codec{
Bech32Prefix: sdk.GetConfig().GetBech32ValidatorAddrPrefix(),
},
})
if err != nil {
return nil, nil, nil, nil, nil, nil, nil, nil, nil, err
}

// validate the signing context to make sure that messages are properly configured
// with cosmos.msg.v1.signer
if err := interfaceRegistry.SigningContext().Validate(); err != nil {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why no validation?

Copy link
Member Author

@kocubinski kocubinski Jun 1, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With current app wiring and depinject, I couldn't find anyway to call Validate after an invoker is called. If it's called before the app will error during start up.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interesting... can we call validate in an invoker?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes but DefineCustomGetSigners is also called in an invoker and order matters, right?

return nil, nil, nil, nil, nil, nil, nil, nil, nil, err
}

amino := codec.NewLegacyAmino()

std.RegisterInterfaces(interfaceRegistry)
Expand All @@ -136,7 +117,7 @@ func ProvideApp() (
}
appBuilder := &AppBuilder{app}

return interfaceRegistry, cdc, amino, appBuilder, cdc, msgServiceRouter, appModule{app}, protoFiles, protoTypes, nil
return cdc, amino, appBuilder, cdc, msgServiceRouter, appModule{app}, protoFiles, protoTypes, nil
}

type AppInputs struct {
Expand Down Expand Up @@ -176,6 +157,35 @@ func SetupAppBuilder(inputs AppInputs) {
}
}

func ProvideInterfaceRegistry(customGetSigners []signing.CustomGetSigner) (codectypes.InterfaceRegistry, error) {
signingOptions := signing.Options{
// using the global prefixes is a temporary solution until we refactor this
// to get the address.Codec's from the container
AddressCodec: address.Bech32Codec{
Bech32Prefix: sdk.GetConfig().GetBech32AccountAddrPrefix(),
},
ValidatorAddressCodec: address.Bech32Codec{
Bech32Prefix: sdk.GetConfig().GetBech32ValidatorAddrPrefix(),
},
}
for _, signer := range customGetSigners {
signingOptions.DefineCustomGetSigners(signer.MsgType, signer.Fn)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This code does nothing I'm pretty sure. The receiver is a struct not a pointer

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This code does something, registers a custom signer fn. If you'd like to prove to this yourself, comment it out and run TestDefineCustomGetSigners in the x/tx integration tests.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because map is a pointer type?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yea. I'm doing an iteration and copy of customGetSignerFuncs in in construction of signing.Context so that changing options after construction won't alter context.

}

interfaceRegistry, err := codectypes.NewInterfaceRegistryWithOptions(codectypes.InterfaceRegistryOptions{
ProtoFiles: proto.HybridResolver,
SigningOptions: signingOptions,
})
if err != nil {
return nil, err
}
err = interfaceRegistry.SigningContext().Validate()
if err != nil {
return nil, err
}
return interfaceRegistry, nil
}

func registerStoreKey(wrapper *AppBuilder, key storetypes.StoreKey) {
wrapper.app.storeKeys = append(wrapper.app.storeKeys, key)
}
Expand Down
14 changes: 9 additions & 5 deletions simapp/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,13 @@ import (
"path/filepath"

"cosmossdk.io/log"
"cosmossdk.io/x/tx/signing"

autocliv1 "cosmossdk.io/api/cosmos/autocli/v1"
reflectionv1 "cosmossdk.io/api/cosmos/reflection/v1"
"cosmossdk.io/client/v2/autocli"
"cosmossdk.io/core/appmodule"

"github.com/cosmos/cosmos-sdk/codec/address"

authcodec "github.com/cosmos/cosmos-sdk/x/auth/codec"
Expand Down Expand Up @@ -229,11 +231,13 @@ func NewSimApp(
) *SimApp {
interfaceRegistry, _ := types.NewInterfaceRegistryWithOptions(types.InterfaceRegistryOptions{
ProtoFiles: proto.HybridResolver,
AddressCodec: address.Bech32Codec{
Bech32Prefix: sdk.GetConfig().GetBech32AccountAddrPrefix(),
},
ValidatorAddressCodec: address.Bech32Codec{
Bech32Prefix: sdk.GetConfig().GetBech32ValidatorAddrPrefix(),
SigningOptions: signing.Options{
AddressCodec: address.Bech32Codec{
Bech32Prefix: sdk.GetConfig().GetBech32AccountAddrPrefix(),
},
ValidatorAddressCodec: address.Bech32Codec{
Bech32Prefix: sdk.GetConfig().GetBech32ValidatorAddrPrefix(),
},
},
})
appCodec := codec.NewProtoCodec(interfaceRegistry)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,9 @@ import (
ed25519types "github.com/cosmos/cosmos-sdk/crypto/keys/ed25519"
"github.com/cosmos/cosmos-sdk/crypto/keys/multisig"
secp256k1types "github.com/cosmos/cosmos-sdk/crypto/keys/secp256k1"
gogo_testpb "github.com/cosmos/cosmos-sdk/tests/integration/aminojson/internal/gogo/testpb"
pulsar_testpb "github.com/cosmos/cosmos-sdk/tests/integration/aminojson/internal/pulsar/testpb"
"github.com/cosmos/cosmos-sdk/tests/integration/rapidgen"
gogo_testpb "github.com/cosmos/cosmos-sdk/tests/integration/tx/internal/gogo/testpb"
pulsar_testpb "github.com/cosmos/cosmos-sdk/tests/integration/tx/internal/pulsar/testpb"
"github.com/cosmos/cosmos-sdk/testutil/testdata"
"github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/bech32"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@ import (
"github.com/stretchr/testify/require"

"cosmossdk.io/x/tx/signing/aminojson"

"github.com/cosmos/cosmos-sdk/codec"
gogopb "github.com/cosmos/cosmos-sdk/tests/integration/aminojson/internal/gogo/testpb"
pulsarpb "github.com/cosmos/cosmos-sdk/tests/integration/aminojson/internal/pulsar/testpb"
gogopb "github.com/cosmos/cosmos-sdk/tests/integration/tx/internal/gogo/testpb"
pulsarpb "github.com/cosmos/cosmos-sdk/tests/integration/tx/internal/pulsar/testpb"
)

func TestRepeatedFields(t *testing.T) {
Expand Down
74 changes: 74 additions & 0 deletions tests/integration/tx/context_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package tx

import (
"testing"

"cosmossdk.io/depinject"
"cosmossdk.io/log"
"cosmossdk.io/x/tx/signing"
"github.com/stretchr/testify/require"
"google.golang.org/protobuf/proto"

codectypes "github.com/cosmos/cosmos-sdk/codec/types"
"github.com/cosmos/cosmos-sdk/tests/integration/tx/internal/pulsar/testpb"
"github.com/cosmos/cosmos-sdk/testutil/configurator"
simtestutil "github.com/cosmos/cosmos-sdk/testutil/sims"
)

func ProvideCustomGetSigners() signing.CustomGetSigner {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice, it will be useful if this could be displayed in the docs ref: #16294

return signing.CustomGetSigner{
MsgType: proto.MessageName(&testpb.TestRepeatedFields{}),
Fn: func(msg proto.Message) ([][]byte, error) {
testMsg := msg.(*testpb.TestRepeatedFields)
// arbitrary logic
signer := testMsg.NullableDontOmitempty[1].Value
return [][]byte{[]byte(signer)}, nil
},
}
}

func TestDefineCustomGetSigners(t *testing.T) {
var interfaceRegistry codectypes.InterfaceRegistry
_, err := simtestutil.SetupAtGenesis(
depinject.Configs(
configurator.NewAppConfig(
configurator.ParamsModule(),
configurator.AuthModule(),
configurator.StakingModule(),
configurator.BankModule(),
configurator.ConsensusModule(),
),
depinject.Supply(log.NewNopLogger()),
depinject.Provide(ProvideCustomGetSigners),
),
&interfaceRegistry,
)
require.NoError(t, err)
require.NotNil(t, interfaceRegistry)

msg := &testpb.TestRepeatedFields{
NullableDontOmitempty: []*testpb.Streng{
{Value: "foo"},
{Value: "bar"},
},
}
signers, err := interfaceRegistry.SigningContext().GetSigners(msg)
require.NoError(t, err)
require.Equal(t, [][]byte{[]byte("bar")}, signers)

// Reset and provider no CustomGetSigners. Consequently, validation will fail and depinject will return an error
_, err = simtestutil.SetupAtGenesis(
depinject.Configs(
configurator.NewAppConfig(
configurator.ParamsModule(),
configurator.AuthModule(),
configurator.StakingModule(),
configurator.BankModule(),
configurator.ConsensusModule(),
),
depinject.Supply(log.NewNopLogger()),
),
&interfaceRegistry,
)
require.ErrorContains(t, err, "use DefineCustomGetSigners")
}
Loading