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 23 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
7 changes: 1 addition & 6 deletions runtime/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,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 @@ -113,12 +114,6 @@ func ProvideApp() (
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 Down
70 changes: 70 additions & 0 deletions tests/integration/tx/context_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package tx

import (
"testing"

bankv1beta1 "cosmossdk.io/api/cosmos/bank/v1beta1"
"cosmossdk.io/depinject"
"cosmossdk.io/log"
"cosmossdk.io/x/tx/signing"
"github.com/stretchr/testify/require"

codectypes "github.com/cosmos/cosmos-sdk/codec/types"
"github.com/cosmos/cosmos-sdk/testutil/configurator"
simtestutil "github.com/cosmos/cosmos-sdk/testutil/sims"
)

func SetCustomGetSigners(registry codectypes.InterfaceRegistry) {
signing.DefineCustomGetSigners(registry.SigningContext(), func(msg *bankv1beta1.SendAuthorization) ([][]byte, error) {
// arbitrary logic
signer := msg.AllowList[1]
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.Invoke(SetCustomGetSigners),
),
&interfaceRegistry,
)
require.NoError(t, err)
require.NotNil(t, interfaceRegistry)

sendAuth := &bankv1beta1.SendAuthorization{
AllowList: []string{"foo", "bar"},
}
signers, err := interfaceRegistry.SigningContext().GetSigners(sendAuth)
require.NoError(t, err)
require.Equal(t, [][]byte{[]byte("bar")}, signers)

// reset without invoker, no custom signer.
_, err = simtestutil.SetupAtGenesis(
depinject.Configs(
configurator.NewAppConfig(
configurator.ParamsModule(),
configurator.AuthModule(),
configurator.StakingModule(),
configurator.BankModule(),
configurator.ConsensusModule(),
),
depinject.Supply(log.NewNopLogger()),
),
&interfaceRegistry,
)
require.NoError(t, err)
require.NotNil(t, interfaceRegistry)

_, err = interfaceRegistry.SigningContext().GetSigners(sendAuth)
require.ErrorContains(t, err, "use DefineCustomGetSigners")
}
1 change: 1 addition & 0 deletions x/nft/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ require (
// Fix upstream GHSA-h395-qcrw-5vmq and GHSA-3vp4-m3rf-835h vulnerabilities.
// TODO Remove it: https://github.com/cosmos/cosmos-sdk/issues/10409
replace (
cosmossdk.io/x/tx => ../../x/tx
// TODO: remove me after collections 0.2. is released.
cosmossdk.io/collections => ../../collections
cosmossdk.io/core => ../../core
Expand Down
3 changes: 3 additions & 0 deletions x/tx/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ Ref: https://keepachangelog.com/en/1.0.0/

## Unreleased

### Improvements
* [#16340](https://github.com/cosmos/cosmos-sdk/pull/16340): add `DefineCustomGetSigners` API function.

## v0.7.0

### API Breaking
Expand Down
2 changes: 1 addition & 1 deletion x/tx/decode/decode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func TestDecode(t *testing.T) {
{
name: "empty signer option",
msg: &testpb.A{},
error: "no cosmos.msg.v1.signer option found for message A: tx parse error",
error: "no cosmos.msg.v1.signer option found for message A; use DefineCustomGetSigners to specify a custom getter: tx parse error",
},
}

Expand Down
2 changes: 1 addition & 1 deletion x/tx/internal/testpb/1.proto
Original file line number Diff line number Diff line change
Expand Up @@ -104,4 +104,4 @@ message Ballot {
string voter = 2;
reserved 3;
repeated WeightedBallotOption options = 4;
}
}
22 changes: 19 additions & 3 deletions x/tx/signing/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@ import (
"google.golang.org/protobuf/reflect/protoreflect"
"google.golang.org/protobuf/reflect/protoregistry"

msgv1 "cosmossdk.io/api/cosmos/msg/v1"
"cosmossdk.io/core/address"

msgv1 "cosmossdk.io/api/cosmos/msg/v1"
)

// Context is a context for retrieving the list of signers from a
Expand Down Expand Up @@ -85,7 +86,7 @@ type getSignersFunc func(proto.Message) ([][]byte, error)
func getSignersFieldNames(descriptor protoreflect.MessageDescriptor) ([]string, error) {
signersFields := proto.GetExtension(descriptor.Options(), msgv1.E_Signer).([]string)
if len(signersFields) == 0 {
return nil, fmt.Errorf("no cosmos.msg.v1.signer option found for message %s", descriptor.FullName())
return nil, fmt.Errorf("no cosmos.msg.v1.signer option found for message %s; use DefineCustomGetSigners to specify a custom getter", descriptor.FullName())
}

return signersFields, nil
Expand Down Expand Up @@ -316,11 +317,26 @@ func (c *Context) ValidatorAddressCodec() address.Codec {
return c.validatorAddressCodec
}

// FileResolver returns the proto file resolver used by the context.
// FileResolver returns the protobuf file resolver used by the context.
func (c *Context) FileResolver() ProtoFileResolver {
return c.fileResolver
}

// TypeResolver returns the protobuf type resolver used by the context.
func (c *Context) TypeResolver() protoregistry.MessageTypeResolver {
return c.typeResolver
}

// DefineCustomGetSigners defines a custom GetSigners function for a given
// message type. It is defined as a function rather than a method on Context
// because of how go generics work.
//
// NOTE: if a custom signers function is defined, the message type used to
// define this function MUST be the concrete type passed to GetSigners,
// otherwise a runtime type error will occur.
func DefineCustomGetSigners[T proto.Message](ctx *Context, getSigners func(T) ([][]byte, error)) {
Copy link
Member

Choose a reason for hiding this comment

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

I wonder if this should be done when constructing a context by setting options. We shouldn't really allow this after initialization

Copy link
Member Author

Choose a reason for hiding this comment

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

I explored how to do this with depinject, with CustomGetSignersFunc as a ManyPerContainerType, but couldn't come up with anything too good. Maybe we can change the API for DefineCustomGetSigners. I think we'd need to drop the Generic and possibly pass the type name in.

Copy link
Member

Choose a reason for hiding this comment

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

Yeah we can refactor the API. If that sounds like the right direction, let's first figure out how this works with the signing context options struct and then we can figure out depinject

t := *new(T)
ctx.getSignersFuncs[t.ProtoReflect().Descriptor().FullName()] = func(msg proto.Message) ([][]byte, error) {
return getSigners(msg.(T))
}
}
22 changes: 22 additions & 0 deletions x/tx/signing/context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,28 @@ func TestGetSigners(t *testing.T) {
}
}

func TestDefineCustomGetSigners(t *testing.T) {
customMsg := &testpb.Ballot{}
signers := [][]byte{[]byte("foo")}

context, err := NewContext(Options{
AddressCodec: dummyAddressCodec{},
ValidatorAddressCodec: dummyValidatorAddressCodec{},
})
require.NoError(t, err)

_, err = context.GetSigners(customMsg)
// before calling DefineCustomGetSigners, we should get an error
require.ErrorContains(t, err, "use DefineCustomGetSigners to specify")
DefineCustomGetSigners(context, func(msg *testpb.Ballot) ([][]byte, error) {
return signers, nil
})
// after calling DefineCustomGetSigners, we should get the signers
gotSigners, err := context.GetSigners(customMsg)
require.NoError(t, err)
require.Equal(t, signers, gotSigners)
}

type dummyAddressCodec struct{}

func (d dummyAddressCodec) StringToBytes(text string) ([]byte, error) {
Expand Down