Skip to content

Commit 794a496

Browse files
authored
Merge PR #5533: Protobuf: x/auth & x/supply
1 parent c0a4222 commit 794a496

File tree

108 files changed

+5235
-1013
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

108 files changed

+5235
-1013
lines changed

CHANGELOG.md

+19-2
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@ flushed to disk or kept as a snapshot. Note, `KeepRecent` is automatically infer
5757
and provided directly the IAVL store.
5858
* (modules) [\#5555](https://github.com/cosmos/cosmos-sdk/pull/5555) Move x/auth/client/utils/ types and functions to x/auth/client/.
5959
* (modules) [\#5572](https://github.com/cosmos/cosmos-sdk/pull/5572) Move account balance logic and APIs from `x/auth` to `x/bank`.
60+
* (types) [\#5533](https://github.com/cosmos/cosmos-sdk/pull/5533) Refactored `AppModuleBasic` and `AppModuleGenesis`
61+
to now accept a `codec.JSONMarshaler` for modular serialization of genesis state.
6062

6163
### Bug Fixes
6264

@@ -72,15 +74,15 @@ and provided directly the IAVL store.
7274
* Callers to `NewBaseVestingAccount` are responsible for verifying account balance in relation to
7375
the original vesting amount.
7476
* The `SendKeeper` and `ViewKeeper` interfaces in `x/bank` have been modified to account for changes.
75-
* (staking) [\#5600](https://github.com/cosmos/cosmos-sdk/pull/5600) Migrate the `x/staking` module to use Protocol Buffer for state
77+
* (x/staking) [\#5600](https://github.com/cosmos/cosmos-sdk/pull/5600) Migrate the `x/staking` module to use Protocol Buffers for state
7678
serialization instead of Amino. The exact codec used is `codec.HybridCodec` which utilizes Protobuf for binary encoding and Amino
7779
for JSON encoding.
7880
* `BondStatus` is now of type `int32` instead of `byte`.
7981
* Types of `int16` in the `Params` type are now of type `int32`.
8082
* Every reference of `crypto.Pubkey` in context of a `Validator` is now of type string. `GetPubKeyFromBech32` must be used to get the `crypto.Pubkey`.
8183
* The `Keeper` constructor now takes a `codec.Marshaler` instead of a concrete Amino codec. This exact type
8284
provided is specified by `ModuleCdc`.
83-
* (distr) [\#5610](https://github.com/cosmos/cosmos-sdk/pull/5610) Migrate the `x/distribution` module to use Protocol Buffer for state
85+
* (x/distribution) [\#5610](https://github.com/cosmos/cosmos-sdk/pull/5610) Migrate the `x/distribution` module to use Protocol Buffers for state
8486
serialization instead of Amino. The exact codec used is `codec.HybridCodec` which utilizes Protobuf for binary encoding and Amino
8587
for JSON encoding.
8688
* `ValidatorHistoricalRewards.ReferenceCount` is now of types `uint32` instead of `uint16`.
@@ -89,6 +91,21 @@ for JSON encoding.
8991
* `ValidatorAccumulatedCommission` is now a struct with `commission`.
9092
* The `Keeper` constructor now takes a `codec.Marshaler` instead of a concrete Amino codec. This exact type
9193
provided is specified by `ModuleCdc`.
94+
* (x/auth) [\#5533](https://github.com/cosmos/cosmos-sdk/pull/5533) Migrate the `x/auth` module to use Protocol Buffers for state
95+
serialization instead of Amino.
96+
* The `BaseAccount.PubKey` field is now represented as a Bech32 string instead of a `crypto.Pubkey`.
97+
* `NewBaseAccountWithAddress` now returns a reference to a `BaseAccount`.
98+
* The `x/auth` module now accepts a `Codec` interface which extends the `codec.Marshaler` interface by
99+
requiring a concrete codec to know how to serialize accounts.
100+
* The `AccountRetriever` type now accepts a `Codec` in its constructor in order to know how to
101+
serialize accounts.
102+
* (x/supply) [\#5533](https://github.com/cosmos/cosmos-sdk/pull/5533) Migrate the `x/supply` module to use Protocol Buffers for state
103+
serialization instead of Amino.
104+
* The `internal` sub-package has been removed in order to expose the types proto file.
105+
* The `x/supply` module now accepts a `Codec` interface which extends the `codec.Marshaler` interface by
106+
requiring a concrete codec to know how to serialize `SupplyI` types.
107+
* The `SupplyI` interface has been modified to no longer return `SupplyI` on methods. Instead the
108+
concrete type's receiver should modify the type.
92109

93110
### Improvements
94111

go.mod

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ require (
1616
github.com/pelletier/go-toml v1.6.0
1717
github.com/pkg/errors v0.9.1
1818
github.com/rakyll/statik v0.1.6
19-
github.com/regen-network/cosmos-proto v0.1.0
19+
github.com/regen-network/cosmos-proto v0.1.1-0.20200213154359-02baa11ea7c2
2020
github.com/spf13/afero v1.2.1 // indirect
2121
github.com/spf13/cobra v0.0.5
2222
github.com/spf13/jwalterweatherman v1.1.0 // indirect

go.sum

+2-5
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,6 @@ github.com/golang/mock v1.3.1-0.20190508161146-9fa652df1129/go.mod h1:sBzyDLLjw3
9191
github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
9292
github.com/golang/protobuf v1.3.0/go.mod h1:Qd/q+1AKNOZr9uGQzbzCmRO6sUih6GTPZv6a1/R87v0=
9393
github.com/golang/protobuf v1.3.1/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
94-
github.com/golang/protobuf v1.3.2 h1:6nsPYzhq5kReh6QImI3k5qWzO4PEbvbIW2cwSfR/6xs=
9594
github.com/golang/protobuf v1.3.2/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
9695
github.com/golang/protobuf v1.3.3 h1:gyjaxf+svBWX08ZjK86iN9geUJF0H6gp2IRKX6Nf6/I=
9796
github.com/golang/protobuf v1.3.3/go.mod h1:vzj43D7+SQXF/4pzW/hwtAqwc6iTitCiVSaWz5lYuqw=
@@ -196,8 +195,8 @@ github.com/rakyll/statik v0.1.6 h1:uICcfUXpgqtw2VopbIncslhAmE5hwc4g20TEyEENBNs=
196195
github.com/rakyll/statik v0.1.6/go.mod h1:OEi9wJV/fMUAGx1eNjq75DKDsJVuEv1U0oYdX6GX8Zs=
197196
github.com/rcrowley/go-metrics v0.0.0-20180503174638-e2704e165165 h1:nkcn14uNmFEuGCb2mBZbBb24RdNRL08b/wb+xBOYpuk=
198197
github.com/rcrowley/go-metrics v0.0.0-20180503174638-e2704e165165/go.mod h1:bCqnVzQkZxMG4s8nGwiZ5l3QUCyqpo9Y+/ZMZ9VjZe4=
199-
github.com/regen-network/cosmos-proto v0.1.0 h1:gsV+YO2kMvY430zQn8ioPXRxEJgb/ms0iMPeWo3VEyY=
200-
github.com/regen-network/cosmos-proto v0.1.0/go.mod h1:+r7jN10xXCypD4yBgzKOa+vgLz0riqYMHeDcKekxPvA=
198+
github.com/regen-network/cosmos-proto v0.1.1-0.20200213154359-02baa11ea7c2 h1:jQK1YoH972Aptd22YKgtNu5jM2X2xMGkyIENOAc71to=
199+
github.com/regen-network/cosmos-proto v0.1.1-0.20200213154359-02baa11ea7c2/go.mod h1:+r7jN10xXCypD4yBgzKOa+vgLz0riqYMHeDcKekxPvA=
201200
github.com/regen-network/protobuf v1.3.2-alpha.regen.1 h1:YdeZbBS0lG1D13COb7b57+nM/RGgIs8WF9DwitU6EBM=
202201
github.com/regen-network/protobuf v1.3.2-alpha.regen.1/go.mod h1:lye6mqhOn/GCw1zRl3uPD5VP8rC+LPMyTyPAyQV872U=
203202
github.com/rogpeppe/fastuuid v0.0.0-20150106093220-6724a57986af/go.mod h1:XWv6SoW27p1b0cqNHllgS5HIMJraePCO15w5zCzIWYg=
@@ -253,7 +252,6 @@ github.com/tendermint/go-amino v0.15.1 h1:D2uk35eT4iTsvJd9jWIetzthE5C0/k2QmMFkCN
253252
github.com/tendermint/go-amino v0.15.1/go.mod h1:TQU0M1i/ImAo+tYpZi73AU3V/dKeCoMC9Sphe2ZwGME=
254253
github.com/tendermint/iavl v0.13.0 h1:r2sINvNFlJsLlLhGoqlqPlREWYkuK26BvMfkBt+XQnA=
255254
github.com/tendermint/iavl v0.13.0/go.mod h1:7nSUPdrsHEZ2nNZa+9gaIrcJciWd1jCQZXtcyARU82k=
256-
github.com/tendermint/tendermint v0.33.0 h1:TW1g9sQs3YSqKM8o1+opL3/VmBy4Ke/VKV9MxYpqNbI=
257255
github.com/tendermint/tendermint v0.33.0/go.mod h1:s5UoymnPIY+GcA3mMte4P9gpMP8vS7UH7HBXikT1pHI=
258256
github.com/tendermint/tendermint v0.33.1 h1:8f68LUBz8yhISZvaLFP4siXXrLWsWeoYfelbdNtmvm4=
259257
github.com/tendermint/tendermint v0.33.1/go.mod h1:fBOKyrlXOETqQ+heL8x/TZgSdmItON54csyabvktBp0=
@@ -341,7 +339,6 @@ google.golang.org/genproto v0.0.0-20200108215221-bd8f9a0ef82f/go.mod h1:n3cpQtvx
341339
google.golang.org/grpc v1.19.0/go.mod h1:mqu4LbDTu4XGKhr4mRzUsmM4RtVoemTSY81AxZiDr8c=
342340
google.golang.org/grpc v1.21.0/go.mod h1:oYelfM1adQP15Ek0mdvEgi9Df8B9CZIaU1084ijfRaM=
343341
google.golang.org/grpc v1.23.0/go.mod h1:Y5yQAOtifL1yxbo5wqy6BxZv8vAUGQwXBOALyacEbxg=
344-
google.golang.org/grpc v1.26.0 h1:2dTRdpdFEEhJYQD8EMLB61nnrzSCTbG38PhqdhvOltg=
345342
google.golang.org/grpc v1.26.0/go.mod h1:qbnxyOmOxrQa7FizSgH+ReBfzJrCY1pSN7KXBS8abTk=
346343
google.golang.org/grpc v1.27.1 h1:zvIju4sqAGvwKspUQOhwnpcqSbzi7/H6QomNNjTL4sk=
347344
google.golang.org/grpc v1.27.1/go.mod h1:qbnxyOmOxrQa7FizSgH+ReBfzJrCY1pSN7KXBS8abTk=

simapp/app.go

+10-10
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111

1212
bam "github.com/cosmos/cosmos-sdk/baseapp"
1313
"github.com/cosmos/cosmos-sdk/codec"
14+
simappcodec "github.com/cosmos/cosmos-sdk/simapp/codec"
1415
sdk "github.com/cosmos/cosmos-sdk/types"
1516
"github.com/cosmos/cosmos-sdk/types/module"
1617
"github.com/cosmos/cosmos-sdk/version"
@@ -78,7 +79,6 @@ var (
7879
}
7980
)
8081

81-
// Verify app interface at compile time
8282
var _ App = (*SimApp)(nil)
8383

8484
// SimApp extends an ABCI application, but with most of its parameters exported.
@@ -124,10 +124,10 @@ func NewSimApp(
124124
invCheckPeriod uint, baseAppOptions ...func(*bam.BaseApp),
125125
) *SimApp {
126126

127-
appCodec := NewAppCodec()
128-
129127
// TODO: Remove cdc in favor of appCodec once all modules are migrated.
130-
cdc := MakeCodec()
128+
cdc := simappcodec.MakeCodec(ModuleBasics)
129+
130+
appCodec := simappcodec.NewAppCodec(cdc)
131131

132132
bApp := bam.NewBaseApp(appName, logger, db, auth.DefaultTxDecoder(cdc), baseAppOptions...)
133133
bApp.SetCommitMultiStoreTracer(traceStore)
@@ -150,7 +150,7 @@ func NewSimApp(
150150
}
151151

152152
// init params keeper and subspaces
153-
app.ParamsKeeper = params.NewKeeper(appCodec.Params, keys[params.StoreKey], tkeys[params.TStoreKey])
153+
app.ParamsKeeper = params.NewKeeper(appCodec, keys[params.StoreKey], tkeys[params.TStoreKey])
154154
app.subspaces[auth.ModuleName] = app.ParamsKeeper.Subspace(auth.DefaultParamspace)
155155
app.subspaces[bank.ModuleName] = app.ParamsKeeper.Subspace(bank.DefaultParamspace)
156156
app.subspaces[staking.ModuleName] = app.ParamsKeeper.Subspace(staking.DefaultParamspace)
@@ -163,23 +163,23 @@ func NewSimApp(
163163

164164
// add keepers
165165
app.AccountKeeper = auth.NewAccountKeeper(
166-
app.cdc, keys[auth.StoreKey], app.subspaces[auth.ModuleName], auth.ProtoBaseAccount,
166+
appCodec, keys[auth.StoreKey], app.subspaces[auth.ModuleName], auth.ProtoBaseAccount,
167167
)
168168
app.BankKeeper = bank.NewBaseKeeper(
169169
app.cdc, keys[bank.StoreKey], app.AccountKeeper, app.subspaces[bank.ModuleName], app.BlacklistedAccAddrs(),
170170
)
171171
app.SupplyKeeper = supply.NewKeeper(
172-
app.cdc, keys[supply.StoreKey], app.AccountKeeper, app.BankKeeper, maccPerms,
172+
appCodec, keys[supply.StoreKey], app.AccountKeeper, app.BankKeeper, maccPerms,
173173
)
174174
stakingKeeper := staking.NewKeeper(
175-
appCodec.Staking, keys[staking.StoreKey], app.BankKeeper, app.SupplyKeeper, app.subspaces[staking.ModuleName],
175+
appCodec, keys[staking.StoreKey], app.BankKeeper, app.SupplyKeeper, app.subspaces[staking.ModuleName],
176176
)
177177
app.MintKeeper = mint.NewKeeper(
178178
app.cdc, keys[mint.StoreKey], app.subspaces[mint.ModuleName], &stakingKeeper,
179179
app.SupplyKeeper, auth.FeeCollectorName,
180180
)
181181
app.DistrKeeper = distr.NewKeeper(
182-
appCodec.Distribution, keys[distr.StoreKey], app.subspaces[distr.ModuleName], app.BankKeeper, &stakingKeeper,
182+
appCodec, keys[distr.StoreKey], app.subspaces[distr.ModuleName], app.BankKeeper, &stakingKeeper,
183183
app.SupplyKeeper, auth.FeeCollectorName, app.ModuleAccountAddrs(),
184184
)
185185
app.SlashingKeeper = slashing.NewKeeper(
@@ -305,7 +305,7 @@ func (app *SimApp) EndBlocker(ctx sdk.Context, req abci.RequestEndBlock) abci.Re
305305
func (app *SimApp) InitChainer(ctx sdk.Context, req abci.RequestInitChain) abci.ResponseInitChain {
306306
var genesisState GenesisState
307307
app.cdc.MustUnmarshalJSON(req.AppStateBytes, &genesisState)
308-
return app.mm.InitGenesis(ctx, genesisState)
308+
return app.mm.InitGenesis(ctx, app.cdc, genesisState)
309309
}
310310

311311
// LoadHeight loads a particular height

simapp/codec.go

-46
This file was deleted.

simapp/codec/codec.go

+128
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
package codec
2+
3+
import (
4+
"github.com/cosmos/cosmos-sdk/codec"
5+
sdk "github.com/cosmos/cosmos-sdk/types"
6+
"github.com/cosmos/cosmos-sdk/types/module"
7+
"github.com/cosmos/cosmos-sdk/x/auth"
8+
authexported "github.com/cosmos/cosmos-sdk/x/auth/exported"
9+
"github.com/cosmos/cosmos-sdk/x/auth/vesting"
10+
"github.com/cosmos/cosmos-sdk/x/supply"
11+
"github.com/cosmos/cosmos-sdk/x/supply/exported"
12+
)
13+
14+
var (
15+
_ auth.Codec = (*Codec)(nil)
16+
_ supply.Codec = (*Codec)(nil)
17+
)
18+
19+
// Codec defines the application-level codec. This codec contains all the
20+
// required module-specific codecs that are to be provided upon initialization.
21+
type Codec struct {
22+
codec.Marshaler
23+
24+
// Keep reference to the amino codec to allow backwards compatibility along
25+
// with type, and interface registration.
26+
amino *codec.Codec
27+
}
28+
29+
func NewAppCodec(amino *codec.Codec) *Codec {
30+
return &Codec{Marshaler: codec.NewHybridCodec(amino), amino: amino}
31+
}
32+
33+
// MarshalAccount marshals an Account interface. If the given type implements
34+
// the Marshaler interface, it is treated as a Proto-defined message and
35+
// serialized that way. Otherwise, it falls back on the internal Amino codec.
36+
func (c *Codec) MarshalAccount(accI authexported.Account) ([]byte, error) {
37+
acc := &Account{}
38+
if err := acc.SetAccount(accI); err != nil {
39+
return nil, err
40+
}
41+
42+
return c.Marshaler.MarshalBinaryLengthPrefixed(acc)
43+
}
44+
45+
// UnmarshalAccount returns an Account interface from raw encoded account bytes
46+
// of a Proto-based Account type. An error is returned upon decoding failure.
47+
func (c *Codec) UnmarshalAccount(bz []byte) (authexported.Account, error) {
48+
acc := &Account{}
49+
if err := c.Marshaler.UnmarshalBinaryLengthPrefixed(bz, acc); err != nil {
50+
return nil, err
51+
}
52+
53+
return acc.GetAccount(), nil
54+
}
55+
56+
// MarshalAccountJSON JSON encodes an account object implementing the Account
57+
// interface.
58+
func (c *Codec) MarshalAccountJSON(acc authexported.Account) ([]byte, error) {
59+
return c.Marshaler.MarshalJSON(acc)
60+
}
61+
62+
// UnmarshalAccountJSON returns an Account from JSON encoded bytes.
63+
func (c *Codec) UnmarshalAccountJSON(bz []byte) (authexported.Account, error) {
64+
acc := &Account{}
65+
if err := c.Marshaler.UnmarshalJSON(bz, acc); err != nil {
66+
return nil, err
67+
}
68+
69+
return acc.GetAccount(), nil
70+
}
71+
72+
// MarshalSupply marshals a SupplyI interface. If the given type implements
73+
// the Marshaler interface, it is treated as a Proto-defined message and
74+
// serialized that way. Otherwise, it falls back on the internal Amino codec.
75+
func (c *Codec) MarshalSupply(supplyI exported.SupplyI) ([]byte, error) {
76+
supply := &Supply{}
77+
if err := supply.SetSupplyI(supplyI); err != nil {
78+
return nil, err
79+
}
80+
81+
return c.Marshaler.MarshalBinaryLengthPrefixed(supply)
82+
}
83+
84+
// UnmarshalSupply returns a SupplyI interface from raw encoded account bytes
85+
// of a Proto-based SupplyI type. An error is returned upon decoding failure.
86+
func (c *Codec) UnmarshalSupply(bz []byte) (exported.SupplyI, error) {
87+
supply := &Supply{}
88+
if err := c.Marshaler.UnmarshalBinaryLengthPrefixed(bz, supply); err != nil {
89+
return nil, err
90+
}
91+
92+
return supply.GetSupplyI(), nil
93+
}
94+
95+
// MarshalSupplyJSON JSON encodes a supply object implementing the SupplyI
96+
// interface.
97+
func (c *Codec) MarshalSupplyJSON(supply exported.SupplyI) ([]byte, error) {
98+
return c.Marshaler.MarshalJSON(supply)
99+
}
100+
101+
// UnmarshalSupplyJSON returns a SupplyI from JSON encoded bytes.
102+
func (c *Codec) UnmarshalSupplyJSON(bz []byte) (exported.SupplyI, error) {
103+
supply := &Supply{}
104+
if err := c.Marshaler.UnmarshalJSON(bz, supply); err != nil {
105+
return nil, err
106+
}
107+
108+
return supply.GetSupplyI(), nil
109+
}
110+
111+
// ----------------------------------------------------------------------------
112+
113+
// MakeCodec creates and returns a reference to an Amino codec that has all the
114+
// necessary types and interfaces registered. This codec is provided to all the
115+
// modules the application depends on.
116+
//
117+
// NOTE: This codec will be deprecated in favor of AppCodec once all modules are
118+
// migrated.
119+
func MakeCodec(bm module.BasicManager) *codec.Codec {
120+
cdc := codec.New()
121+
122+
bm.RegisterCodec(cdc)
123+
vesting.RegisterCodec(cdc)
124+
sdk.RegisterCodec(cdc)
125+
codec.RegisterCrypto(cdc)
126+
127+
return cdc
128+
}

0 commit comments

Comments
 (0)