Skip to content

Commit

Permalink
[Bugfix] Convert Custom JSON Marshaling Codec to Amino Codec (#377)
Browse files Browse the repository at this point in the history
* remove custom json marshaler for vesting accounts

* fix custom json to use amino codec

* decrease number of lazy vesting schedules, to lower gas consumption at simulation
  • Loading branch information
yys authored Aug 4, 2020
1 parent 1b24b92 commit b99a7cf
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 35 deletions.
5 changes: 3 additions & 2 deletions x/auth/simulation/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ package simulation

import (
"fmt"
core "github.com/terra-project/core/types"
"math/rand"

core "github.com/terra-project/core/types"

"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/module"
Expand Down Expand Up @@ -111,7 +112,7 @@ func RandomGenesisAccounts(simState *module.SimulationState) (genesisAccs export
var lazySchedules vestingtypes.LazySchedules

// Make sure scheduleNum is even number
scheduleNum := 1 + int64(simState.Rand.Intn(100))
scheduleNum := 1 + int64(simState.Rand.Intn(10))
if scheduleNum%2 != 0 {
scheduleNum++
}
Expand Down
61 changes: 30 additions & 31 deletions x/auth/vesting/types/vesting_account.go
Original file line number Diff line number Diff line change
@@ -1,22 +1,23 @@
package types

import (
"encoding/json"
"github.com/tendermint/tendermint/crypto"
"time"

"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"
vesttypes "github.com/cosmos/cosmos-sdk/x/auth/vesting/types"
"github.com/tendermint/tendermint/crypto"

customauthtypes "github.com/terra-project/core/x/auth/internal/types"

"gopkg.in/yaml.v2"
)

type vestingAccountPretty struct {
// for pretty purpose
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 @@ -31,6 +32,22 @@ type vestingAccountPretty struct {
VestingSchedules VestingSchedules `json:"vesting_schedules,omitempty" yaml:"vesting_schedules,omitempty"`
}

// To prevent stack overflow
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
VestingSchedules VestingSchedules `json:"vesting_schedules,omitempty" yaml:"vesting_schedules,omitempty"`
}

//-----------------------------------------------------------------------------
// Lazy Graded Vesting Account

Expand Down Expand Up @@ -156,7 +173,7 @@ func (lgva LazyGradedVestingAccount) String() string {

// MarshalYAML returns the YAML representation of a LazyGradedVestingAccount.
func (lgva LazyGradedVestingAccount) MarshalYAML() (interface{}, error) {
alias := vestingAccountPretty{
alias := vestingAccountYAML{
Address: lgva.Address,
Coins: lgva.Coins,
AccountNumber: lgva.AccountNumber,
Expand All @@ -165,6 +182,7 @@ func (lgva LazyGradedVestingAccount) MarshalYAML() (interface{}, error) {
DelegatedFree: lgva.DelegatedFree,
DelegatedVesting: lgva.DelegatedVesting,
EndTime: lgva.EndTime,
VestingSchedules: lgva.VestingSchedules,
}

if lgva.PubKey != nil {
Expand All @@ -186,50 +204,31 @@ func (lgva LazyGradedVestingAccount) MarshalYAML() (interface{}, error) {

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

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

alias.PubKey = pks
}

return json.Marshal(alias)
return codec.Cdc.MarshalJSON(alias)
}

// UnmarshalJSON unmarshals raw JSON bytes into a ContinuousVestingAccount.
// UnmarshalJSON unmarshals raw JSON bytes into a LazyGradedVestingAccount.
func (lgva *LazyGradedVestingAccount) UnmarshalJSON(bz []byte) error {
var alias vestingAccountPretty
if err := json.Unmarshal(bz, &alias); err != nil {
var alias vestingAccountJSON
if err := codec.Cdc.UnmarshalJSON(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
}
}

lgva.BaseVestingAccount = &vesttypes.BaseVestingAccount{
BaseAccount: authtypes.NewBaseAccount(alias.Address, alias.Coins, pk, alias.AccountNumber, alias.Sequence),
BaseAccount: authtypes.NewBaseAccount(alias.Address, alias.Coins, alias.PubKey, alias.AccountNumber, alias.Sequence),
OriginalVesting: alias.OriginalVesting,
DelegatedFree: alias.DelegatedFree,
DelegatedVesting: alias.DelegatedVesting,
Expand Down
5 changes: 3 additions & 2 deletions x/auth/vesting/types/vesting_account_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/tendermint/tendermint/crypto/secp256k1"
tmtime "github.com/tendermint/tendermint/types/time"

"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"
Expand Down Expand Up @@ -420,14 +421,14 @@ func TestLazyGradedVestingAccountJSON(t *testing.T) {
})
require.NoError(t, err)

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

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

var a LazyGradedVestingAccount
require.NoError(t, json.Unmarshal(bz, &a))
require.NoError(t, codec.Cdc.UnmarshalJSON(bz, &a))
require.Equal(t, acc.String(), a.String())
}

0 comments on commit b99a7cf

Please sign in to comment.