From 9e6b2eba9510625d319b5362a325f45bbb0728ef Mon Sep 17 00:00:00 2001 From: yys Date: Mon, 27 Jul 2020 17:03:25 +0900 Subject: [PATCH 1/3] remove custom json marshaler for vesting accounts --- x/auth/vesting/types/vesting_account.go | 59 -------------------- x/auth/vesting/types/vesting_account_test.go | 54 ------------------ 2 files changed, 113 deletions(-) diff --git a/x/auth/vesting/types/vesting_account.go b/x/auth/vesting/types/vesting_account.go index d5069310d..4c8ccbbd1 100644 --- a/x/auth/vesting/types/vesting_account.go +++ b/x/auth/vesting/types/vesting_account.go @@ -1,8 +1,6 @@ package types import ( - "encoding/json" - "github.com/tendermint/tendermint/crypto" "time" sdk "github.com/cosmos/cosmos-sdk/types" @@ -184,63 +182,6 @@ func (lgva LazyGradedVestingAccount) MarshalYAML() (interface{}, error) { return string(bz), err } -// MarshalJSON returns the JSON representation of a LazyGradedVestingAccount. -func (lgva LazyGradedVestingAccount) MarshalJSON() ([]byte, error) { - alias := vestingAccountPretty{ - Address: lgva.Address, - Coins: lgva.Coins, - AccountNumber: lgva.AccountNumber, - Sequence: lgva.Sequence, - OriginalVesting: lgva.OriginalVesting, - DelegatedFree: lgva.DelegatedFree, - DelegatedVesting: lgva.DelegatedVesting, - EndTime: lgva.EndTime, - } - - 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) -} - -// UnmarshalJSON unmarshals raw JSON bytes into a ContinuousVestingAccount. -func (lgva *LazyGradedVestingAccount) 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 - } - } - - lgva.BaseVestingAccount = &vesttypes.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, - } - - lgva.VestingSchedules = alias.VestingSchedules - - return nil -} - // spendableCoins returns all the spendable coins for a vesting account given a // set of vesting coins. // diff --git a/x/auth/vesting/types/vesting_account_test.go b/x/auth/vesting/types/vesting_account_test.go index 40becf30d..18c1b45a5 100644 --- a/x/auth/vesting/types/vesting_account_test.go +++ b/x/auth/vesting/types/vesting_account_test.go @@ -1,7 +1,6 @@ package types import ( - "encoding/json" "errors" "testing" "time" @@ -378,56 +377,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 := authvesttypes.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 authvesttypes.BaseVestingAccount - require.NoError(t, json.Unmarshal(bz, &a)) - require.Equal(t, acc.String(), a.String()) -} - -func TestLazyGradedVestingAccountJSON(t *testing.T) { - now := tmtime.Now() - endTime := now.Add(24 * time.Hour) - - 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 := authvesttypes.NewBaseVestingAccount(baseAcc, coins, now.Unix()) - acc := NewLazyGradedVestingAccountRaw(baseVesting, VestingSchedules{ - NewVestingSchedule(feeDenom, []LazySchedule{ - NewLazySchedule(now.Unix(), endTime.Unix(), sdk.NewDec(1)), - }), - NewVestingSchedule(stakeDenom, []LazySchedule{ - NewLazySchedule(now.Unix(), endTime.Unix(), sdk.NewDec(1)), - }), - }) - 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 LazyGradedVestingAccount - require.NoError(t, json.Unmarshal(bz, &a)) - require.Equal(t, acc.String(), a.String()) -} From d30980693d8dac75a4611a81d7cee9b08279433e Mon Sep 17 00:00:00 2001 From: yys Date: Tue, 4 Aug 2020 10:38:17 +0900 Subject: [PATCH 2/3] fix custom json to use amino codec --- x/auth/vesting/types/vesting_account.go | 62 ++++++++++++++++++++++++- 1 file changed, 60 insertions(+), 2 deletions(-) diff --git a/x/auth/vesting/types/vesting_account.go b/x/auth/vesting/types/vesting_account.go index 4c8ccbbd1..49b652b05 100644 --- a/x/auth/vesting/types/vesting_account.go +++ b/x/auth/vesting/types/vesting_account.go @@ -3,18 +3,21 @@ package types import ( "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"` @@ -29,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 @@ -154,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, @@ -163,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 { @@ -182,6 +202,44 @@ func (lgva LazyGradedVestingAccount) MarshalYAML() (interface{}, error) { return string(bz), err } +// MarshalJSON returns the JSON representation of a LazyGradedVestingAccount. +func (lgva LazyGradedVestingAccount) MarshalJSON() ([]byte, error) { + 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, + } + + return codec.Cdc.MarshalJSON(alias) +} + +// UnmarshalJSON unmarshals raw JSON bytes into a ContinuousVestingAccount. +func (lgva *LazyGradedVestingAccount) UnmarshalJSON(bz []byte) error { + var alias vestingAccountJSON + if err := codec.Cdc.UnmarshalJSON(bz, &alias); err != nil { + return err + } + + lgva.BaseVestingAccount = &vesttypes.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, + } + + lgva.VestingSchedules = alias.VestingSchedules + + return nil +} + // spendableCoins returns all the spendable coins for a vesting account given a // set of vesting coins. // From 890b3bd710ca362e4e4d02a925aacbea4583d43b Mon Sep 17 00:00:00 2001 From: yys Date: Tue, 4 Aug 2020 11:23:54 +0900 Subject: [PATCH 3/3] decrease number of lazy vesting schedules, to lower gas consumption at simulation --- x/auth/simulation/genesis.go | 5 +- x/auth/vesting/types/vesting_account.go | 2 +- x/auth/vesting/types/vesting_account_test.go | 55 ++++++++++++++++++++ 3 files changed, 59 insertions(+), 3 deletions(-) diff --git a/x/auth/simulation/genesis.go b/x/auth/simulation/genesis.go index 8be8c85cf..4fb025b3f 100644 --- a/x/auth/simulation/genesis.go +++ b/x/auth/simulation/genesis.go @@ -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" @@ -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++ } diff --git a/x/auth/vesting/types/vesting_account.go b/x/auth/vesting/types/vesting_account.go index 49b652b05..367d99863 100644 --- a/x/auth/vesting/types/vesting_account.go +++ b/x/auth/vesting/types/vesting_account.go @@ -220,7 +220,7 @@ func (lgva LazyGradedVestingAccount) MarshalJSON() ([]byte, error) { 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 vestingAccountJSON if err := codec.Cdc.UnmarshalJSON(bz, &alias); err != nil { diff --git a/x/auth/vesting/types/vesting_account_test.go b/x/auth/vesting/types/vesting_account_test.go index 18c1b45a5..b1bcea7c4 100644 --- a/x/auth/vesting/types/vesting_account_test.go +++ b/x/auth/vesting/types/vesting_account_test.go @@ -1,6 +1,7 @@ package types import ( + "encoding/json" "errors" "testing" "time" @@ -9,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" @@ -377,3 +379,56 @@ 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 := authvesttypes.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 authvesttypes.BaseVestingAccount + require.NoError(t, json.Unmarshal(bz, &a)) + require.Equal(t, acc.String(), a.String()) +} + +func TestLazyGradedVestingAccountJSON(t *testing.T) { + now := tmtime.Now() + endTime := now.Add(24 * time.Hour) + + 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 := authvesttypes.NewBaseVestingAccount(baseAcc, coins, now.Unix()) + acc := NewLazyGradedVestingAccountRaw(baseVesting, VestingSchedules{ + NewVestingSchedule(feeDenom, []LazySchedule{ + NewLazySchedule(now.Unix(), endTime.Unix(), sdk.NewDec(1)), + }), + NewVestingSchedule(stakeDenom, []LazySchedule{ + NewLazySchedule(now.Unix(), endTime.Unix(), sdk.NewDec(1)), + }), + }) + require.NoError(t, err) + + 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, codec.Cdc.UnmarshalJSON(bz, &a)) + require.Equal(t, acc.String(), a.String()) +}