-
Notifications
You must be signed in to change notification settings - Fork 3.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
42b04ee
commit 7cef4d1
Showing
2 changed files
with
150 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package simapp | ||
|
||
import ( | ||
"errors" | ||
|
||
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" | ||
"github.com/cosmos/cosmos-sdk/x/supply" | ||
) | ||
|
||
var _ authexported.GenesisAccount = (*SimGenesisAccount)(nil) | ||
|
||
// SimGenesisAccount defines a type that implements the GenesisAccount interface | ||
// to be used for simulation accounts in the genesis state. | ||
type SimGenesisAccount struct { | ||
*authtypes.BaseAccount | ||
|
||
// vesting account fields | ||
OriginalVesting sdk.Coins `json:"original_vesting" yaml:"original_vesting"` // total vesting coins upon initialization | ||
DelegatedFree sdk.Coins `json:"delegated_free" yaml:"delegated_free"` // delegated vested coins at time of delegation | ||
DelegatedVesting sdk.Coins `json:"delegated_vesting" yaml:"delegated_vesting"` // delegated vesting coins at time of delegation | ||
StartTime int64 `json:"start_time" yaml:"start_time"` // vesting start time (UNIX Epoch time) | ||
EndTime int64 `json:"end_time" yaml:"end_time"` // vesting end time (UNIX Epoch time) | ||
|
||
// module account fields | ||
ModuleName string `json:"module_name" yaml:"module_name"` // name of the module account | ||
ModulePermissions []string `json:"module_permissions" yaml:"module_permissions"` // permissions of module account | ||
} | ||
|
||
// Validate checks for errors on the vesting and module account parameters | ||
func (sga SimGenesisAccount) Validate() error { | ||
if !sga.OriginalVesting.IsZero() { | ||
if sga.OriginalVesting.IsAnyGT(sga.Coins) { | ||
return errors.New("vesting amount cannot be greater than total amount") | ||
} | ||
if sga.StartTime >= sga.EndTime { | ||
return errors.New("vesting start-time cannot be before end-time") | ||
} | ||
} | ||
|
||
if sga.ModuleName != "" { | ||
ma := supply.ModuleAccount{ | ||
BaseAccount: sga.BaseAccount, Name: sga.ModuleName, Permissions: sga.ModulePermissions, | ||
} | ||
if err := ma.Validate(); err != nil { | ||
return err | ||
} | ||
} | ||
|
||
return sga.BaseAccount.Validate() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
package simapp_test | ||
|
||
import ( | ||
"testing" | ||
"time" | ||
|
||
"github.com/cosmos/cosmos-sdk/simapp" | ||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types" | ||
|
||
"github.com/stretchr/testify/require" | ||
"github.com/tendermint/tendermint/crypto" | ||
"github.com/tendermint/tendermint/crypto/secp256k1" | ||
) | ||
|
||
func TestSimGenesisAccountValidate(t *testing.T) { | ||
pubkey := secp256k1.GenPrivKey().PubKey() | ||
addr := sdk.AccAddress(pubkey.Address()) | ||
|
||
vestingStart := time.Now().UTC() | ||
|
||
coins := sdk.NewCoins(sdk.NewInt64Coin("test", 1000)) | ||
baseAcc := authtypes.NewBaseAccount(addr, nil, pubkey, 0, 0) | ||
require.NoError(t, baseAcc.SetCoins(coins)) | ||
|
||
testCases := []struct { | ||
name string | ||
sga simapp.SimGenesisAccount | ||
wantErr bool | ||
}{ | ||
{ | ||
"valid basic account", | ||
simapp.SimGenesisAccount{ | ||
BaseAccount: baseAcc, | ||
}, | ||
false, | ||
}, | ||
{ | ||
"invalid basic account with mismatching address/pubkey", | ||
simapp.SimGenesisAccount{ | ||
BaseAccount: authtypes.NewBaseAccount(addr, nil, secp256k1.GenPrivKey().PubKey(), 0, 0), | ||
}, | ||
true, | ||
}, | ||
{ | ||
"valid basic account with module name", | ||
simapp.SimGenesisAccount{ | ||
BaseAccount: authtypes.NewBaseAccount(sdk.AccAddress(crypto.AddressHash([]byte("testmod"))), nil, nil, 0, 0), | ||
ModuleName: "testmod", | ||
}, | ||
false, | ||
}, | ||
{ | ||
"valid basic account with invalid module name/pubkey pair", | ||
simapp.SimGenesisAccount{ | ||
BaseAccount: baseAcc, | ||
ModuleName: "testmod", | ||
}, | ||
true, | ||
}, | ||
{ | ||
"valid basic account with valid vesting attributes", | ||
simapp.SimGenesisAccount{ | ||
BaseAccount: baseAcc, | ||
OriginalVesting: coins, | ||
StartTime: vestingStart.Unix(), | ||
EndTime: vestingStart.Add(1 * time.Hour).Unix(), | ||
}, | ||
false, | ||
}, | ||
{ | ||
"valid basic account with invalid vesting end time", | ||
simapp.SimGenesisAccount{ | ||
BaseAccount: baseAcc, | ||
OriginalVesting: coins, | ||
StartTime: vestingStart.Add(2 * time.Hour).Unix(), | ||
EndTime: vestingStart.Add(1 * time.Hour).Unix(), | ||
}, | ||
true, | ||
}, | ||
{ | ||
"valid basic account with invalid original vesting coins", | ||
simapp.SimGenesisAccount{ | ||
BaseAccount: baseAcc, | ||
OriginalVesting: coins.Add(coins), | ||
StartTime: vestingStart.Unix(), | ||
EndTime: vestingStart.Add(1 * time.Hour).Unix(), | ||
}, | ||
true, | ||
}, | ||
} | ||
|
||
for _, tc := range testCases { | ||
t.Run(tc.name, func(t *testing.T) { | ||
require.Equal(t, tc.wantErr, tc.sga.Validate() != nil) | ||
}) | ||
} | ||
} |