ValidatorVestingAccount validation (#552)

This commit is contained in:
Federico Kunze 2020-06-05 08:59:16 -04:00 committed by GitHub
parent 5723001390
commit b2a4369d4e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 38 additions and 19 deletions

View File

@ -67,5 +67,4 @@ type (
VestingProgress = types.VestingProgress VestingProgress = types.VestingProgress
CurrentPeriodProgress = types.CurrentPeriodProgress CurrentPeriodProgress = types.CurrentPeriodProgress
ValidatorVestingAccount = types.ValidatorVestingAccount ValidatorVestingAccount = types.ValidatorVestingAccount
TotalCirculatingSupply = types.TotalCirculatingSupply
) )

View File

@ -3,6 +3,7 @@ package types
import ( import (
"encoding/json" "encoding/json"
"errors" "errors"
"fmt"
"time" "time"
yaml "gopkg.in/yaml.v2" yaml "gopkg.in/yaml.v2"
@ -71,11 +72,6 @@ type ValidatorVestingAccount struct {
DebtAfterFailedVesting sdk.Coins `json:"debt_after_failed_vesting" yaml:"debt_after_failed_vesting"` DebtAfterFailedVesting sdk.Coins `json:"debt_after_failed_vesting" yaml:"debt_after_failed_vesting"`
} }
// TotalCirculatingSupply represents the total circulating supply of Kava
type TotalCirculatingSupply struct {
TotalSupply uint64 `json:"total_supply" yaml:"total_supply"` // total circulating supply
}
// NewValidatorVestingAccountRaw creates a new ValidatorVestingAccount object from BaseVestingAccount // NewValidatorVestingAccountRaw creates a new ValidatorVestingAccount object from BaseVestingAccount
func NewValidatorVestingAccountRaw(bva *vestingtypes.BaseVestingAccount, func NewValidatorVestingAccountRaw(bva *vestingtypes.BaseVestingAccount,
startTime int64, periods vestingtypes.Periods, validatorAddress sdk.ConsAddress, returnAddress sdk.AccAddress, signingThreshold int64) *ValidatorVestingAccount { startTime int64, periods vestingtypes.Periods, validatorAddress sdk.ConsAddress, returnAddress sdk.AccAddress, signingThreshold int64) *ValidatorVestingAccount {
@ -95,7 +91,10 @@ func NewValidatorVestingAccountRaw(bva *vestingtypes.BaseVestingAccount,
ValidatorAddress: validatorAddress, ValidatorAddress: validatorAddress,
ReturnAddress: returnAddress, ReturnAddress: returnAddress,
SigningThreshold: signingThreshold, SigningThreshold: signingThreshold,
CurrentPeriodProgress: CurrentPeriodProgress{0, 0}, CurrentPeriodProgress: CurrentPeriodProgress{
MissedBlocks: 0,
TotalBlocks: 0,
},
VestingPeriodProgress: vestingPeriodProgress, VestingPeriodProgress: vestingPeriodProgress,
DebtAfterFailedVesting: sdk.NewCoins(), DebtAfterFailedVesting: sdk.NewCoins(),
} }
@ -193,12 +192,18 @@ func (vva *ValidatorVestingAccount) TrackDelegation(blockTime time.Time, amount
// Validate checks for errors on the account fields // Validate checks for errors on the account fields
func (vva ValidatorVestingAccount) Validate() error { func (vva ValidatorVestingAccount) Validate() error {
if vva.ValidatorAddress.Empty() {
return errors.New("validator address cannot be empty")
}
if vva.SigningThreshold > 100 || vva.SigningThreshold < 0 { if vva.SigningThreshold > 100 || vva.SigningThreshold < 0 {
return errors.New("signing threshold must be between 0 and 100") return errors.New("signing threshold must be between 0 and 100")
} }
if vva.ReturnAddress.Equals(vva.Address) { if vva.ReturnAddress.Equals(vva.Address) {
return errors.New("return address cannot be the same as the account address") return errors.New("return address cannot be the same as the account address")
} }
if !vva.DebtAfterFailedVesting.IsValid() {
return fmt.Errorf("invalid debt after failed vesting coins: %s", vva.DebtAfterFailedVesting)
}
return vva.PeriodicVestingAccount.Validate() return vva.PeriodicVestingAccount.Validate()
} }

View File

@ -1,7 +1,6 @@
package types package types
import ( import (
"errors"
"testing" "testing"
"time" "time"
@ -394,36 +393,52 @@ func TestGenesisAccountValidate(t *testing.T) {
origCoins := sdk.Coins{sdk.NewInt64Coin(feeDenom, 1000), sdk.NewInt64Coin(stakeDenom, 100)} origCoins := sdk.Coins{sdk.NewInt64Coin(feeDenom, 1000), sdk.NewInt64Coin(stakeDenom, 100)}
bacc := auth.NewBaseAccountWithAddress(testAddr) bacc := auth.NewBaseAccountWithAddress(testAddr)
bacc.SetCoins(origCoins) bacc.SetCoins(origCoins)
invAcc := NewValidatorVestingAccount(&bacc, now.Unix(), periods, testConsAddr, nil, 100)
invAcc.DebtAfterFailedVesting = sdk.Coins{sdk.Coin{Denom: "KAVA", Amount: sdk.OneInt()}}
tests := []struct { tests := []struct {
name string name string
acc authexported.GenesisAccount acc authexported.GenesisAccount
expErr error expPass bool
}{ }{
{ {
"valid validator vesting account", "valid validator vesting account",
NewValidatorVestingAccount(&bacc, now.Unix(), periods, testConsAddr, nil, 100), NewValidatorVestingAccount(&bacc, now.Unix(), periods, testConsAddr, nil, 100),
nil, true,
},
{
"empty validator address",
NewValidatorVestingAccount(&bacc, now.Unix(), periods, nil, nil, 100),
false,
}, },
{ {
"invalid signing threshold", "invalid signing threshold",
NewValidatorVestingAccount(&bacc, now.Unix(), periods, testConsAddr, nil, -1), NewValidatorVestingAccount(&bacc, now.Unix(), periods, testConsAddr, nil, -1),
errors.New("signing threshold must be between 0 and 100"), false,
}, },
{ {
"invalid signing threshold", "invalid signing threshold",
NewValidatorVestingAccount(&bacc, now.Unix(), periods, testConsAddr, nil, 120), NewValidatorVestingAccount(&bacc, now.Unix(), periods, testConsAddr, nil, 120),
errors.New("signing threshold must be between 0 and 100"), false,
}, },
{ {
"invalid return address", "invalid return address",
NewValidatorVestingAccount(&bacc, now.Unix(), periods, testConsAddr, testAddr, 90), NewValidatorVestingAccount(&bacc, now.Unix(), periods, testConsAddr, testAddr, 90),
errors.New("return address cannot be the same as the account address"), false,
},
{
"invalid debt after failed vesting ",
invAcc,
false,
}, },
} }
for _, tt := range tests { for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) { err := tt.acc.Validate()
err := tt.acc.Validate() if tt.expPass {
require.Equal(t, tt.expErr, err) require.NoError(t, err, tt.name)
}) } else {
require.Error(t, err, tt.name)
}
} }
} }