Skip to content

Commit

Permalink
Merge PR cosmos#5600: Migrate x/staking to Protobuf
Browse files Browse the repository at this point in the history
  • Loading branch information
alexanderbez authored Feb 6, 2020
1 parent b0c6c75 commit 53bf227
Show file tree
Hide file tree
Showing 57 changed files with 8,796 additions and 850 deletions.
1 change: 1 addition & 0 deletions .codecov.yml
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ ignore:
- "docs"
- "*.md"
- "*.rst"
- "*.pb.go"
- "x/**/test_common.go"
- "scripts/"
- "contrib"
2 changes: 1 addition & 1 deletion .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ issues:
- text: "ST1003:"
linters:
- stylecheck
# FIXME disabled until golangci-lint updates stylecheck with this fix:
# FIXME: Disabled until golangci-lint updates stylecheck with this fix:
# https://github.com/dominikh/go-tools/issues/389
- text: "ST1016:"
linters:
Expand Down
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,14 @@ balances or a single balance by denom when the `denom` query parameter is presen
* Callers to `NewBaseVestingAccount` are responsible for verifying account balance in relation to
the original vesting amount.
* The `SendKeeper` and `ViewKeeper` interfaces in `x/bank` have been modified to account for changes.
* (staking) [\#5600](https://github.com/cosmos/cosmos-sdk/pull/5600) Migrate the `x/staking` module to use Protocol Buffer for state
serialization instead of Amino. The exact codec used is `codec.HybridCodec` which utilizes Protobuf for binary encoding and Amino
for JSON encoding.
* `BondStatus` is now of type `int32` instead of `byte`.
* Types of `int16` in the `Params` type are now of type `int32`.
* Every reference of `crypto.Pubkey` in context of a `Validator` is now of type string. `GetPubKeyFromBech32` must be used to get the `crypto.Pubkey`.
* The `Keeper` constructor now takes a `codec.Marshaler` instead of a concrete Amino codec. This exact type
provided is specified by `ModuleCdc`.

### Improvements

Expand Down
8 changes: 4 additions & 4 deletions codec/amino.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ func RegisterEvidences(cdc *Codec) {
// MarshalJSONIndent provides a utility for indented JSON encoding of an object
// via an Amino codec. It returns an error if it cannot serialize or indent as
// JSON.
func MarshalJSONIndent(cdc *Codec, obj interface{}) ([]byte, error) {
bz, err := cdc.MarshalJSON(obj)
func MarshalJSONIndent(m JSONMarshaler, obj interface{}) ([]byte, error) {
bz, err := m.MarshalJSON(obj)
if err != nil {
return nil, err
}
Expand All @@ -60,8 +60,8 @@ func MarshalJSONIndent(cdc *Codec, obj interface{}) ([]byte, error) {
}

// MustMarshalJSONIndent executes MarshalJSONIndent except it panics upon failure.
func MustMarshalJSONIndent(cdc *Codec, obj interface{}) []byte {
bz, err := MarshalJSONIndent(cdc, obj)
func MustMarshalJSONIndent(m JSONMarshaler, obj interface{}) []byte {
bz, err := MarshalJSONIndent(m, obj)
if err != nil {
panic(fmt.Sprintf("failed to marshal JSON: %s", err))
}
Expand Down
4 changes: 4 additions & 0 deletions codec/codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ type (
UnmarshalBinaryLengthPrefixed(bz []byte, ptr ProtoMarshaler) error
MustUnmarshalBinaryLengthPrefixed(bz []byte, ptr ProtoMarshaler)

JSONMarshaler
}

JSONMarshaler interface {
MarshalJSON(o interface{}) ([]byte, error) // nolint: stdmethods
MustMarshalJSON(o interface{}) []byte

Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ require (
github.com/cosmos/ledger-cosmos-go v0.11.1
github.com/gogo/protobuf v1.3.1
github.com/golang/mock v1.3.1-0.20190508161146-9fa652df1129
github.com/golang/protobuf v1.3.2
github.com/gorilla/mux v1.7.3
github.com/hashicorp/golang-lru v0.5.4
github.com/mattn/go-isatty v0.0.12
Expand Down
16 changes: 4 additions & 12 deletions simapp/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import (
"github.com/cosmos/cosmos-sdk/version"
"github.com/cosmos/cosmos-sdk/x/auth"
"github.com/cosmos/cosmos-sdk/x/auth/ante"
"github.com/cosmos/cosmos-sdk/x/auth/vesting"
"github.com/cosmos/cosmos-sdk/x/bank"
"github.com/cosmos/cosmos-sdk/x/crisis"
distr "github.com/cosmos/cosmos-sdk/x/distribution"
Expand Down Expand Up @@ -79,16 +78,6 @@ var (
}
)

// MakeCodec - custom tx codec
func MakeCodec() *codec.Codec {
var cdc = codec.New()
ModuleBasics.RegisterCodec(cdc)
vesting.RegisterCodec(cdc)
sdk.RegisterCodec(cdc)
codec.RegisterCrypto(cdc)
return cdc
}

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

Expand Down Expand Up @@ -135,6 +124,9 @@ func NewSimApp(
invCheckPeriod uint, baseAppOptions ...func(*bam.BaseApp),
) *SimApp {

appCodec := NewAppCodec()

// TODO: Remove cdc in favor of appCodec once all modules are migrated.
cdc := MakeCodec()

bApp := bam.NewBaseApp(appName, logger, db, auth.DefaultTxDecoder(cdc), baseAppOptions...)
Expand Down Expand Up @@ -180,7 +172,7 @@ func NewSimApp(
app.cdc, keys[supply.StoreKey], app.AccountKeeper, app.BankKeeper, maccPerms,
)
stakingKeeper := staking.NewKeeper(
app.cdc, keys[staking.StoreKey], app.BankKeeper, app.SupplyKeeper, app.subspaces[staking.ModuleName],
appCodec.Staking, keys[staking.StoreKey], app.BankKeeper, app.SupplyKeeper, app.subspaces[staking.ModuleName],
)
app.MintKeeper = mint.NewKeeper(
app.cdc, keys[mint.StoreKey], app.subspaces[mint.ModuleName], &stakingKeeper,
Expand Down
40 changes: 40 additions & 0 deletions simapp/codec.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package simapp

import (
"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/auth/vesting"
"github.com/cosmos/cosmos-sdk/x/staking"
)

// AppCodec defines the application-level codec. This codec contains all the
// required module-specific codecs that are to be provided upon initialization.
type AppCodec struct {
amino *codec.Codec

Staking *staking.Codec
}

func NewAppCodec() *AppCodec {
amino := MakeCodec()

return &AppCodec{
amino: amino,
Staking: staking.NewCodec(amino),
}
}

// MakeCodec creates and returns a reference to an Amino codec that has all the
// necessary types and interfaces registered. This codec is provided to all the
// modules the application depends on.
//
// NOTE: This codec will be deprecated in favor of AppCodec once all modules are
// migrated.
func MakeCodec() *codec.Codec {
var cdc = codec.New()
ModuleBasics.RegisterCodec(cdc)
vesting.RegisterCodec(cdc)
sdk.RegisterCodec(cdc)
codec.RegisterCrypto(cdc)
return cdc
}
Loading

0 comments on commit 53bf227

Please sign in to comment.