Skip to content

Commit

Permalink
Merge PR #6861: Revert "Backport 0.39.1: Remove Custom JSON Marshalin…
Browse files Browse the repository at this point in the history
…g for Accounts"
  • Loading branch information
alexanderbez authored Jul 27, 2020
1 parent 9d00f71 commit b0ab544
Show file tree
Hide file tree
Showing 5 changed files with 319 additions and 13 deletions.
10 changes: 9 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,12 @@ Ref: https://keepachangelog.com/en/1.0.0/

# Changelog

## [v0.39.1]

### Client Breaking Changes

* (x/auth) [\#6861](https://github.com/cosmos/cosmos-sdk/pull/6861) Remove public key Bech32 encoding for all account types for JSON serialization, instead relying on direct Amino encoding. In addition, JSON serialization utilizes Amino instead of the Go stdlib, so integers are treated as strings.

## [v0.39.0]

### Improvements
Expand Down Expand Up @@ -2949,7 +2955,9 @@ BUG FIXES:
<!-- Release links -->
[Unreleased]: https://github.com/cosmos/cosmos-sdk/compare/v0.38.2...HEAD
[Unreleased]: https://github.com/cosmos/cosmos-sdk/compare/v0.39.1...HEAD
[v0.39.1]: https://github.com/cosmos/cosmos-sdk/releases/tag/v0.39.1
[v0.39.0]: https://github.com/cosmos/cosmos-sdk/releases/tag/v0.39.0
[v0.38.2]: https://github.com/cosmos/cosmos-sdk/releases/tag/v0.38.2
[v0.38.1]: https://github.com/cosmos/cosmos-sdk/releases/tag/v0.38.1
[v0.38.0]: https://github.com/cosmos/cosmos-sdk/releases/tag/v0.38.0
Expand Down
189 changes: 177 additions & 12 deletions x/auth/vesting/types/vesting_account.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@ import (
"errors"
"time"

"github.com/tendermint/tendermint/crypto"
"gopkg.in/yaml.v2"

"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
authexported "github.com/cosmos/cosmos-sdk/x/auth/exported"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
vestexported "github.com/cosmos/cosmos-sdk/x/auth/vesting/exported"

"gopkg.in/yaml.v2"
)

// Compile-time type assertions
Expand Down Expand Up @@ -183,7 +185,7 @@ func (bva BaseVestingAccount) Validate() error {
return bva.BaseAccount.Validate()
}

type vestingAccountPretty struct {
type vestingAccountYAML struct {
Address sdk.AccAddress `json:"address" yaml:"address"`
Coins sdk.Coins `json:"coins" yaml:"coins"`
PubKey string `json:"public_key" yaml:"public_key"`
Expand All @@ -199,14 +201,30 @@ type vestingAccountPretty struct {
VestingPeriods Periods `json:"vesting_periods,omitempty" yaml:"vesting_periods,omitempty"`
}

type vestingAccountJSON struct {
Address sdk.AccAddress `json:"address" yaml:"address"`
Coins sdk.Coins `json:"coins" yaml:"coins"`
PubKey crypto.PubKey `json:"public_key" yaml:"public_key"`
AccountNumber uint64 `json:"account_number" yaml:"account_number"`
Sequence uint64 `json:"sequence" yaml:"sequence"`
OriginalVesting sdk.Coins `json:"original_vesting" yaml:"original_vesting"`
DelegatedFree sdk.Coins `json:"delegated_free" yaml:"delegated_free"`
DelegatedVesting sdk.Coins `json:"delegated_vesting" yaml:"delegated_vesting"`
EndTime int64 `json:"end_time" yaml:"end_time"`

// custom fields based on concrete vesting type which can be omitted
StartTime int64 `json:"start_time,omitempty" yaml:"start_time,omitempty"`
VestingPeriods Periods `json:"vesting_periods,omitempty" yaml:"vesting_periods,omitempty"`
}

func (bva BaseVestingAccount) String() string {
out, _ := bva.MarshalYAML()
return out.(string)
}

// MarshalYAML returns the YAML representation of a BaseVestingAccount.
func (bva BaseVestingAccount) MarshalYAML() (interface{}, error) {
alias := vestingAccountPretty{
alias := vestingAccountYAML{
Address: bva.Address,
Coins: bva.Coins,
AccountNumber: bva.AccountNumber,
Expand All @@ -217,8 +235,9 @@ func (bva BaseVestingAccount) MarshalYAML() (interface{}, error) {
EndTime: bva.EndTime,
}

if bva.PubKey != nil {
pks, err := sdk.Bech32ifyPubKey(sdk.Bech32PubKeyTypeAccPub, bva.PubKey)
pk := bva.GetPubKey()
if pk != nil {
pks, err := sdk.Bech32ifyPubKey(sdk.Bech32PubKeyTypeAccPub, pk)
if err != nil {
return nil, err
}
Expand All @@ -234,6 +253,39 @@ func (bva BaseVestingAccount) MarshalYAML() (interface{}, error) {
return string(bz), err
}

// MarshalJSON returns the JSON representation of a BaseVestingAccount.
func (bva BaseVestingAccount) MarshalJSON() ([]byte, error) {
alias := vestingAccountJSON{
Address: bva.Address,
Coins: bva.Coins,
PubKey: bva.GetPubKey(),
AccountNumber: bva.AccountNumber,
Sequence: bva.Sequence,
OriginalVesting: bva.OriginalVesting,
DelegatedFree: bva.DelegatedFree,
DelegatedVesting: bva.DelegatedVesting,
EndTime: bva.EndTime,
}

return codec.Cdc.MarshalJSON(alias)
}

// UnmarshalJSON unmarshals raw JSON bytes into a BaseVestingAccount.
func (bva *BaseVestingAccount) UnmarshalJSON(bz []byte) error {
var alias vestingAccountJSON
if err := codec.Cdc.UnmarshalJSON(bz, &alias); err != nil {
return err
}

bva.BaseAccount = authtypes.NewBaseAccount(alias.Address, alias.Coins, alias.PubKey, alias.AccountNumber, alias.Sequence)
bva.OriginalVesting = alias.OriginalVesting
bva.DelegatedFree = alias.DelegatedFree
bva.DelegatedVesting = alias.DelegatedVesting
bva.EndTime = alias.EndTime

return nil
}

//-----------------------------------------------------------------------------
// Continuous Vesting Account

Expand Down Expand Up @@ -338,7 +390,7 @@ func (cva ContinuousVestingAccount) String() string {

// MarshalYAML returns the YAML representation of a ContinuousVestingAccount.
func (cva ContinuousVestingAccount) MarshalYAML() (interface{}, error) {
alias := vestingAccountPretty{
alias := vestingAccountYAML{
Address: cva.Address,
Coins: cva.Coins,
AccountNumber: cva.AccountNumber,
Expand All @@ -350,8 +402,9 @@ func (cva ContinuousVestingAccount) MarshalYAML() (interface{}, error) {
StartTime: cva.StartTime,
}

if cva.PubKey != nil {
pks, err := sdk.Bech32ifyPubKey(sdk.Bech32PubKeyTypeAccPub, cva.PubKey)
pk := cva.GetPubKey()
if pk != nil {
pks, err := sdk.Bech32ifyPubKey(sdk.Bech32PubKeyTypeAccPub, pk)
if err != nil {
return nil, err
}
Expand All @@ -367,6 +420,43 @@ func (cva ContinuousVestingAccount) MarshalYAML() (interface{}, error) {
return string(bz), err
}

// MarshalJSON returns the JSON representation of a ContinuousVestingAccount.
func (cva ContinuousVestingAccount) MarshalJSON() ([]byte, error) {
alias := vestingAccountJSON{
Address: cva.Address,
Coins: cva.Coins,
PubKey: cva.GetPubKey(),
AccountNumber: cva.AccountNumber,
Sequence: cva.Sequence,
OriginalVesting: cva.OriginalVesting,
DelegatedFree: cva.DelegatedFree,
DelegatedVesting: cva.DelegatedVesting,
EndTime: cva.EndTime,
StartTime: cva.StartTime,
}

return codec.Cdc.MarshalJSON(alias)
}

// UnmarshalJSON unmarshals raw JSON bytes into a ContinuousVestingAccount.
func (cva *ContinuousVestingAccount) UnmarshalJSON(bz []byte) error {
var alias vestingAccountJSON
if err := codec.Cdc.UnmarshalJSON(bz, &alias); err != nil {
return err
}

cva.BaseVestingAccount = &BaseVestingAccount{
BaseAccount: authtypes.NewBaseAccount(alias.Address, alias.Coins, alias.PubKey, alias.AccountNumber, alias.Sequence),
OriginalVesting: alias.OriginalVesting,
DelegatedFree: alias.DelegatedFree,
DelegatedVesting: alias.DelegatedVesting,
EndTime: alias.EndTime,
}
cva.StartTime = alias.StartTime

return nil
}

//-----------------------------------------------------------------------------
// Periodic Vesting Account

Expand Down Expand Up @@ -496,7 +586,7 @@ func (pva PeriodicVestingAccount) String() string {

// MarshalYAML returns the YAML representation of a PeriodicVestingAccount.
func (pva PeriodicVestingAccount) MarshalYAML() (interface{}, error) {
alias := vestingAccountPretty{
alias := vestingAccountYAML{
Address: pva.Address,
Coins: pva.Coins,
AccountNumber: pva.AccountNumber,
Expand All @@ -509,8 +599,9 @@ func (pva PeriodicVestingAccount) MarshalYAML() (interface{}, error) {
VestingPeriods: pva.VestingPeriods,
}

if pva.PubKey != nil {
pks, err := sdk.Bech32ifyPubKey(sdk.Bech32PubKeyTypeAccPub, pva.PubKey)
pk := pva.GetPubKey()
if pk != nil {
pks, err := sdk.Bech32ifyPubKey(sdk.Bech32PubKeyTypeAccPub, pk)
if err != nil {
return nil, err
}
Expand All @@ -526,6 +617,45 @@ func (pva PeriodicVestingAccount) MarshalYAML() (interface{}, error) {
return string(bz), err
}

// MarshalJSON returns the JSON representation of a PeriodicVestingAccount.
func (pva PeriodicVestingAccount) MarshalJSON() ([]byte, error) {
alias := vestingAccountJSON{
Address: pva.Address,
Coins: pva.Coins,
PubKey: pva.GetPubKey(),
AccountNumber: pva.AccountNumber,
Sequence: pva.Sequence,
OriginalVesting: pva.OriginalVesting,
DelegatedFree: pva.DelegatedFree,
DelegatedVesting: pva.DelegatedVesting,
EndTime: pva.EndTime,
StartTime: pva.StartTime,
VestingPeriods: pva.VestingPeriods,
}

return codec.Cdc.MarshalJSON(alias)
}

// UnmarshalJSON unmarshals raw JSON bytes into a PeriodicVestingAccount.
func (pva *PeriodicVestingAccount) UnmarshalJSON(bz []byte) error {
var alias vestingAccountJSON
if err := codec.Cdc.UnmarshalJSON(bz, &alias); err != nil {
return err
}

pva.BaseVestingAccount = &BaseVestingAccount{
BaseAccount: authtypes.NewBaseAccount(alias.Address, alias.Coins, alias.PubKey, alias.AccountNumber, alias.Sequence),
OriginalVesting: alias.OriginalVesting,
DelegatedFree: alias.DelegatedFree,
DelegatedVesting: alias.DelegatedVesting,
EndTime: alias.EndTime,
}
pva.StartTime = alias.StartTime
pva.VestingPeriods = alias.VestingPeriods

return nil
}

//-----------------------------------------------------------------------------
// Delayed Vesting Account

Expand Down Expand Up @@ -595,3 +725,38 @@ func (dva DelayedVestingAccount) GetStartTime() int64 {
func (dva DelayedVestingAccount) Validate() error {
return dva.BaseVestingAccount.Validate()
}

// MarshalJSON returns the JSON representation of a DelayedVestingAccount.
func (dva DelayedVestingAccount) MarshalJSON() ([]byte, error) {
alias := vestingAccountJSON{
Address: dva.Address,
Coins: dva.Coins,
PubKey: dva.GetPubKey(),
AccountNumber: dva.AccountNumber,
Sequence: dva.Sequence,
OriginalVesting: dva.OriginalVesting,
DelegatedFree: dva.DelegatedFree,
DelegatedVesting: dva.DelegatedVesting,
EndTime: dva.EndTime,
}

return codec.Cdc.MarshalJSON(alias)
}

// UnmarshalJSON unmarshals raw JSON bytes into a DelayedVestingAccount.
func (dva *DelayedVestingAccount) UnmarshalJSON(bz []byte) error {
var alias vestingAccountJSON
if err := codec.Cdc.UnmarshalJSON(bz, &alias); err != nil {
return err
}

dva.BaseVestingAccount = &BaseVestingAccount{
BaseAccount: authtypes.NewBaseAccount(alias.Address, alias.Coins, alias.PubKey, alias.AccountNumber, alias.Sequence),
OriginalVesting: alias.OriginalVesting,
DelegatedFree: alias.DelegatedFree,
DelegatedVesting: alias.DelegatedVesting,
EndTime: alias.EndTime,
}

return nil
}
Loading

0 comments on commit b0ab544

Please sign in to comment.