From f937443fa27945fa53c4c37199fb84d4868dc1f5 Mon Sep 17 00:00:00 2001 From: Amaury Martiny Date: Tue, 6 Oct 2020 17:19:17 +0200 Subject: [PATCH 1/2] Use nil instead of protobuf.Empty in genesis --- x/auth/legacy/v040/migrate.go | 20 ----------------- x/auth/legacy/v040/migrate_test.go | 35 ++++++------------------------ x/auth/types/codec.go | 2 +- 3 files changed, 8 insertions(+), 49 deletions(-) diff --git a/x/auth/legacy/v040/migrate.go b/x/auth/legacy/v040/migrate.go index 5e190ffeab5a..e4037facc484 100644 --- a/x/auth/legacy/v040/migrate.go +++ b/x/auth/legacy/v040/migrate.go @@ -1,37 +1,17 @@ package v040 import ( - pt "github.com/gogo/protobuf/types" - codectypes "github.com/cosmos/cosmos-sdk/codec/types" sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" v039auth "github.com/cosmos/cosmos-sdk/x/auth/legacy/v039" - "github.com/cosmos/cosmos-sdk/x/auth/tx" v040auth "github.com/cosmos/cosmos-sdk/x/auth/types" v040vesting "github.com/cosmos/cosmos-sdk/x/auth/vesting/types" ) // convertBaseAccount converts a 0.39 BaseAccount to a 0.40 BaseAccount. func convertBaseAccount(old *v039auth.BaseAccount) *v040auth.BaseAccount { - // In `x/auth/legacy/v038/migrate.go`, when creating a BaseAccount, we - // explicitly set the PublicKey field to nil. This propagates until 0.40, - // and we don't know the PubKey for those accounts, so we just put an empty - // string inside the Any. - var any *codectypes.Any - var err error - if old.PubKey != nil { - any, err = tx.PubKeyToAny(old.PubKey) - } else { - s := pt.Empty{} - any, err = codectypes.NewAnyWithValue(&s) - } - if err != nil { - panic(err) - } - return &v040auth.BaseAccount{ Address: old.Address.String(), - PubKey: any, AccountNumber: old.AccountNumber, Sequence: old.Sequence, } diff --git a/x/auth/legacy/v040/migrate_test.go b/x/auth/legacy/v040/migrate_test.go index f8994315b5ee..9d674da864f8 100644 --- a/x/auth/legacy/v040/migrate_test.go +++ b/x/auth/legacy/v040/migrate_test.go @@ -89,10 +89,7 @@ func TestMigrate(t *testing.T) { "@type": "/cosmos.auth.v1beta1.BaseAccount", "account_number": "1", "address": "cosmos13syh7de9xndv9wmklccpfvc0d8dcyvay4s6z6l", - "pub_key": { - "@type": "/cosmos.crypto.secp256k1.PubKey", - "key": "A8oWyJkohwy8XZ0Df92jFMBTtTPMvYJplYIrlEHTKPYk" - }, + "pub_key": null, "sequence": "0" }, { @@ -100,10 +97,7 @@ func TestMigrate(t *testing.T) { "base_account": { "account_number": "1", "address": "cosmos1v57fx2l2rt6ehujuu99u2fw05779m5e2ux4z2h", - "pub_key": { - "@type": "/cosmos.crypto.secp256k1.PubKey", - "key": "AruDygh5HprMOpHOEato85dLgAsybMJVyxBGUa3KuWCr" - }, + "pub_key": null, "sequence": "0" }, "name": "module2", @@ -116,10 +110,7 @@ func TestMigrate(t *testing.T) { "base_account": { "account_number": "1", "address": "cosmos18hnp9fjflrkeeqn4gmhjhzljusxzmjeartdckw", - "pub_key": { - "@type": "/cosmos.crypto.secp256k1.PubKey", - "key": "A5aEFDIdQHh0OYmNXNv1sHBNURDWWgVkXC2IALcWLLwJ" - }, + "pub_key": null, "sequence": "0" }, "delegated_free": [ @@ -148,10 +139,7 @@ func TestMigrate(t *testing.T) { "base_account": { "account_number": "1", "address": "cosmos1t9kvvejvk6hjtddx6antck39s206csqduq3ke3", - "pub_key": { - "@type": "/cosmos.crypto.secp256k1.PubKey", - "key": "AoXDzxwTnljemHxfnJcwrKqODBP6Q2l3K3U3UhVDzyah" - }, + "pub_key": null, "sequence": "0" }, "delegated_free": [], @@ -172,10 +160,7 @@ func TestMigrate(t *testing.T) { "base_account": { "account_number": "1", "address": "cosmos1s4ss9zquz7skvguechzlk3na635jdrecl0sgy2", - "pub_key": { - "@type": "/cosmos.crypto.secp256k1.PubKey", - "key": "A2a4P4TQ1OKzpfu0eKnCoEtmTvoiclSx0G9higenUGws" - }, + "pub_key": null, "sequence": "0" }, "delegated_free": [], @@ -207,10 +192,7 @@ func TestMigrate(t *testing.T) { "base_account": { "account_number": "1", "address": "cosmos1mcc6rwrj4hswf8p9ct82c7lmf77w9tuk07rha4", - "pub_key": { - "@type": "/cosmos.crypto.secp256k1.PubKey", - "key": "A4tuAfmZlhjK5cjp6ImR704miybHnITVNOyJORdDPFu3" - }, + "pub_key": null, "sequence": "0" }, "delegated_free": [], @@ -228,10 +210,7 @@ func TestMigrate(t *testing.T) { "@type": "/cosmos.auth.v1beta1.BaseAccount", "account_number": "1", "address": "cosmos16ydaqh0fcnh4qt7a3jme4mmztm2qel5axcpw00", - "pub_key": { - "@type": "/google.protobuf.Empty", - "value": {} - }, + "pub_key": null, "sequence": "0" } ], diff --git a/x/auth/types/codec.go b/x/auth/types/codec.go index 52b1eac4af39..beb14eab7ae7 100644 --- a/x/auth/types/codec.go +++ b/x/auth/types/codec.go @@ -19,7 +19,7 @@ func RegisterLegacyAminoCodec(cdc *codec.LegacyAmino) { legacytx.RegisterLegacyAminoCodec(cdc) } -// RegisterInterface associates protoName with AccountI interface +// RegisterInterfaces associates protoName with AccountI interface // and creates a registry of it's concrete implementations func RegisterInterfaces(registry types.InterfaceRegistry) { registry.RegisterInterface( From 744a822ef4a43454e3c95b2ed2b9aebc7dfd3a89 Mon Sep 17 00:00:00 2001 From: Amaury Martiny Date: Tue, 6 Oct 2020 17:32:17 +0200 Subject: [PATCH 2/2] Fix tests --- x/auth/legacy/v040/migrate.go | 13 +++++++++++++ x/auth/legacy/v040/migrate_test.go | 30 ++++++++++++++++++++++++------ 2 files changed, 37 insertions(+), 6 deletions(-) diff --git a/x/auth/legacy/v040/migrate.go b/x/auth/legacy/v040/migrate.go index e4037facc484..4fe663a48d48 100644 --- a/x/auth/legacy/v040/migrate.go +++ b/x/auth/legacy/v040/migrate.go @@ -4,14 +4,27 @@ import ( codectypes "github.com/cosmos/cosmos-sdk/codec/types" sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" v039auth "github.com/cosmos/cosmos-sdk/x/auth/legacy/v039" + "github.com/cosmos/cosmos-sdk/x/auth/tx" v040auth "github.com/cosmos/cosmos-sdk/x/auth/types" v040vesting "github.com/cosmos/cosmos-sdk/x/auth/vesting/types" ) // convertBaseAccount converts a 0.39 BaseAccount to a 0.40 BaseAccount. func convertBaseAccount(old *v039auth.BaseAccount) *v040auth.BaseAccount { + var any *codectypes.Any + // If the old genesis had a pubkey, we pack it inside an Any. Or else, we + // just leave it nil. + if old.PubKey != nil { + var err error + any, err = tx.PubKeyToAny(old.PubKey) + if err != nil { + panic(err) + } + } + return &v040auth.BaseAccount{ Address: old.Address.String(), + PubKey: any, AccountNumber: old.AccountNumber, Sequence: old.Sequence, } diff --git a/x/auth/legacy/v040/migrate_test.go b/x/auth/legacy/v040/migrate_test.go index 9d674da864f8..519b3cb48342 100644 --- a/x/auth/legacy/v040/migrate_test.go +++ b/x/auth/legacy/v040/migrate_test.go @@ -89,7 +89,10 @@ func TestMigrate(t *testing.T) { "@type": "/cosmos.auth.v1beta1.BaseAccount", "account_number": "1", "address": "cosmos13syh7de9xndv9wmklccpfvc0d8dcyvay4s6z6l", - "pub_key": null, + "pub_key": { + "@type": "/cosmos.crypto.secp256k1.PubKey", + "key": "A8oWyJkohwy8XZ0Df92jFMBTtTPMvYJplYIrlEHTKPYk" + }, "sequence": "0" }, { @@ -97,7 +100,10 @@ func TestMigrate(t *testing.T) { "base_account": { "account_number": "1", "address": "cosmos1v57fx2l2rt6ehujuu99u2fw05779m5e2ux4z2h", - "pub_key": null, + "pub_key": { + "@type": "/cosmos.crypto.secp256k1.PubKey", + "key": "AruDygh5HprMOpHOEato85dLgAsybMJVyxBGUa3KuWCr" + }, "sequence": "0" }, "name": "module2", @@ -110,7 +116,10 @@ func TestMigrate(t *testing.T) { "base_account": { "account_number": "1", "address": "cosmos18hnp9fjflrkeeqn4gmhjhzljusxzmjeartdckw", - "pub_key": null, + "pub_key": { + "@type": "/cosmos.crypto.secp256k1.PubKey", + "key": "A5aEFDIdQHh0OYmNXNv1sHBNURDWWgVkXC2IALcWLLwJ" + }, "sequence": "0" }, "delegated_free": [ @@ -139,7 +148,10 @@ func TestMigrate(t *testing.T) { "base_account": { "account_number": "1", "address": "cosmos1t9kvvejvk6hjtddx6antck39s206csqduq3ke3", - "pub_key": null, + "pub_key": { + "@type": "/cosmos.crypto.secp256k1.PubKey", + "key": "AoXDzxwTnljemHxfnJcwrKqODBP6Q2l3K3U3UhVDzyah" + }, "sequence": "0" }, "delegated_free": [], @@ -160,7 +172,10 @@ func TestMigrate(t *testing.T) { "base_account": { "account_number": "1", "address": "cosmos1s4ss9zquz7skvguechzlk3na635jdrecl0sgy2", - "pub_key": null, + "pub_key": { + "@type": "/cosmos.crypto.secp256k1.PubKey", + "key": "A2a4P4TQ1OKzpfu0eKnCoEtmTvoiclSx0G9higenUGws" + }, "sequence": "0" }, "delegated_free": [], @@ -192,7 +207,10 @@ func TestMigrate(t *testing.T) { "base_account": { "account_number": "1", "address": "cosmos1mcc6rwrj4hswf8p9ct82c7lmf77w9tuk07rha4", - "pub_key": null, + "pub_key": { + "@type": "/cosmos.crypto.secp256k1.PubKey", + "key": "A4tuAfmZlhjK5cjp6ImR704miybHnITVNOyJORdDPFu3" + }, "sequence": "0" }, "delegated_free": [],