Skip to content

Commit

Permalink
Merge PR cosmos#6842: Remove custom acc JSON marshl
Browse files Browse the repository at this point in the history
  • Loading branch information
alexanderbez authored Jul 26, 2020
1 parent 89ceb62 commit 7b931e2
Show file tree
Hide file tree
Showing 4 changed files with 0 additions and 358 deletions.
226 changes: 0 additions & 226 deletions x/auth/vesting/types/vesting_account.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package types

import (
"encoding/json"
"errors"
"time"

Expand All @@ -10,7 +9,6 @@ import (
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
vestexported "github.com/cosmos/cosmos-sdk/x/auth/vesting/exported"

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

Expand Down Expand Up @@ -236,59 +234,6 @@ 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 := vestingAccountPretty{
Address: bva.Address,
Coins: bva.Coins,
AccountNumber: bva.AccountNumber,
Sequence: bva.Sequence,
OriginalVesting: bva.OriginalVesting,
DelegatedFree: bva.DelegatedFree,
DelegatedVesting: bva.DelegatedVesting,
EndTime: bva.EndTime,
}

if bva.PubKey != nil {
pks, err := sdk.Bech32ifyPubKey(sdk.Bech32PubKeyTypeAccPub, bva.PubKey)
if err != nil {
return nil, err
}

alias.PubKey = pks
}

return json.Marshal(alias)
}

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

var (
pk crypto.PubKey
err error
)

if alias.PubKey != "" {
pk, err = sdk.GetPubKeyFromBech32(sdk.Bech32PubKeyTypeAccPub, alias.PubKey)
if err != nil {
return err
}
}

bva.BaseAccount = authtypes.NewBaseAccount(alias.Address, alias.Coins, pk, 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 @@ -422,63 +367,6 @@ 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 := vestingAccountPretty{
Address: cva.Address,
Coins: cva.Coins,
AccountNumber: cva.AccountNumber,
Sequence: cva.Sequence,
OriginalVesting: cva.OriginalVesting,
DelegatedFree: cva.DelegatedFree,
DelegatedVesting: cva.DelegatedVesting,
EndTime: cva.EndTime,
StartTime: cva.StartTime,
}

if cva.PubKey != nil {
pks, err := sdk.Bech32ifyPubKey(sdk.Bech32PubKeyTypeAccPub, cva.PubKey)
if err != nil {
return nil, err
}

alias.PubKey = pks
}

return json.Marshal(alias)
}

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

var (
pk crypto.PubKey
err error
)

if alias.PubKey != "" {
pk, err = sdk.GetPubKeyFromBech32(sdk.Bech32PubKeyTypeAccPub, alias.PubKey)
if err != nil {
return err
}
}

cva.BaseVestingAccount = &BaseVestingAccount{
BaseAccount: authtypes.NewBaseAccount(alias.Address, alias.Coins, pk, 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 @@ -638,65 +526,6 @@ 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 := vestingAccountPretty{
Address: pva.Address,
Coins: pva.Coins,
AccountNumber: pva.AccountNumber,
Sequence: pva.Sequence,
OriginalVesting: pva.OriginalVesting,
DelegatedFree: pva.DelegatedFree,
DelegatedVesting: pva.DelegatedVesting,
EndTime: pva.EndTime,
StartTime: pva.StartTime,
VestingPeriods: pva.VestingPeriods,
}

if pva.PubKey != nil {
pks, err := sdk.Bech32ifyPubKey(sdk.Bech32PubKeyTypeAccPub, pva.PubKey)
if err != nil {
return nil, err
}

alias.PubKey = pks
}

return json.Marshal(alias)
}

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

var (
pk crypto.PubKey
err error
)

if alias.PubKey != "" {
pk, err = sdk.GetPubKeyFromBech32(sdk.Bech32PubKeyTypeAccPub, alias.PubKey)
if err != nil {
return err
}
}

pva.BaseVestingAccount = &BaseVestingAccount{
BaseAccount: authtypes.NewBaseAccount(alias.Address, alias.Coins, pk, 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 @@ -766,58 +595,3 @@ 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 := vestingAccountPretty{
Address: dva.Address,
Coins: dva.Coins,
AccountNumber: dva.AccountNumber,
Sequence: dva.Sequence,
OriginalVesting: dva.OriginalVesting,
DelegatedFree: dva.DelegatedFree,
DelegatedVesting: dva.DelegatedVesting,
EndTime: dva.EndTime,
}

if dva.PubKey != nil {
pks, err := sdk.Bech32ifyPubKey(sdk.Bech32PubKeyTypeAccPub, dva.PubKey)
if err != nil {
return nil, err
}

alias.PubKey = pks
}

return json.Marshal(alias)
}

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

var (
pk crypto.PubKey
err error
)

if alias.PubKey != "" {
pk, err = sdk.GetPubKeyFromBech32(sdk.Bech32PubKeyTypeAccPub, alias.PubKey)
if err != nil {
return err
}
}

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

return nil
}
84 changes: 0 additions & 84 deletions x/auth/vesting/types/vesting_account_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package types

import (
"encoding/json"
"errors"
"testing"
"time"
Expand Down Expand Up @@ -733,86 +732,3 @@ func TestGenesisAccountValidate(t *testing.T) {
})
}
}

func TestBaseVestingAccountJSON(t *testing.T) {
pubkey := secp256k1.GenPrivKey().PubKey()
addr := sdk.AccAddress(pubkey.Address())
coins := sdk.NewCoins(sdk.NewInt64Coin("test", 5))
baseAcc := authtypes.NewBaseAccount(addr, coins, pubkey, 10, 50)

acc, err := NewBaseVestingAccount(baseAcc, coins, time.Now().Unix())
require.NoError(t, err)

bz, err := json.Marshal(acc)
require.NoError(t, err)

bz1, err := acc.MarshalJSON()
require.NoError(t, err)
require.Equal(t, string(bz1), string(bz))

var a BaseVestingAccount
require.NoError(t, json.Unmarshal(bz, &a))
require.Equal(t, acc.String(), a.String())
}

func TestContinuousVestingAccountJSON(t *testing.T) {
pubkey := secp256k1.GenPrivKey().PubKey()
addr := sdk.AccAddress(pubkey.Address())
coins := sdk.NewCoins(sdk.NewInt64Coin("test", 5))
baseAcc := authtypes.NewBaseAccount(addr, coins, pubkey, 10, 50)

baseVesting, err := NewBaseVestingAccount(baseAcc, coins, time.Now().Unix())
acc := NewContinuousVestingAccountRaw(baseVesting, baseVesting.EndTime)
require.NoError(t, err)

bz, err := json.Marshal(acc)
require.NoError(t, err)

bz1, err := acc.MarshalJSON()
require.NoError(t, err)
require.Equal(t, string(bz1), string(bz))

var a ContinuousVestingAccount
require.NoError(t, json.Unmarshal(bz, &a))
require.Equal(t, acc.String(), a.String())
}

func TestPeriodicVestingAccountJSON(t *testing.T) {
pubkey := secp256k1.GenPrivKey().PubKey()
addr := sdk.AccAddress(pubkey.Address())
coins := sdk.NewCoins(sdk.NewInt64Coin("test", 5))
baseAcc := authtypes.NewBaseAccount(addr, coins, pubkey, 10, 50)

acc := NewPeriodicVestingAccount(baseAcc, time.Now().Unix(), Periods{Period{3600, coins}})

bz, err := json.Marshal(acc)
require.NoError(t, err)

bz1, err := acc.MarshalJSON()
require.NoError(t, err)
require.Equal(t, string(bz1), string(bz))

var a PeriodicVestingAccount
require.NoError(t, json.Unmarshal(bz, &a))
require.Equal(t, acc.String(), a.String())
}

func TestDelayedVestingAccountJSON(t *testing.T) {
pubkey := secp256k1.GenPrivKey().PubKey()
addr := sdk.AccAddress(pubkey.Address())
coins := sdk.NewCoins(sdk.NewInt64Coin("test", 5))
baseAcc := authtypes.NewBaseAccount(addr, coins, pubkey, 10, 50)

acc := NewDelayedVestingAccount(baseAcc, time.Now().Unix())

bz, err := json.Marshal(acc)
require.NoError(t, err)

bz1, err := acc.MarshalJSON()
require.NoError(t, err)
require.Equal(t, string(bz1), string(bz))

var a DelayedVestingAccount
require.NoError(t, json.Unmarshal(bz, &a))
require.Equal(t, acc.String(), a.String())
}
28 changes: 0 additions & 28 deletions x/supply/internal/types/account.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package types

import (
"encoding/json"
"errors"
"fmt"
"strings"
Expand Down Expand Up @@ -147,30 +146,3 @@ func (ma ModuleAccount) MarshalYAML() (interface{}, error) {

return string(bs), nil
}

// MarshalJSON returns the JSON representation of a ModuleAccount.
func (ma ModuleAccount) MarshalJSON() ([]byte, error) {
return json.Marshal(moduleAccountPretty{
Address: ma.Address,
Coins: ma.Coins,
PubKey: "",
AccountNumber: ma.AccountNumber,
Sequence: ma.Sequence,
Name: ma.Name,
Permissions: ma.Permissions,
})
}

// UnmarshalJSON unmarshals raw JSON bytes into a ModuleAccount.
func (ma *ModuleAccount) UnmarshalJSON(bz []byte) error {
var alias moduleAccountPretty
if err := json.Unmarshal(bz, &alias); err != nil {
return err
}

ma.BaseAccount = authtypes.NewBaseAccount(alias.Address, alias.Coins, nil, alias.AccountNumber, alias.Sequence)
ma.Name = alias.Name
ma.Permissions = alias.Permissions

return nil
}
Loading

0 comments on commit 7b931e2

Please sign in to comment.