2022-04-26 16:17:15 +00:00
package utils
import (
"testing"
"time"
2024-08-03 13:44:37 +00:00
"github.com/0glabs/0g-chain/chaincfg"
2023-04-05 23:21:59 +00:00
sdkmath "cosmossdk.io/math"
2022-04-26 16:17:15 +00:00
"github.com/cosmos/cosmos-sdk/crypto/keys/secp256k1"
sdk "github.com/cosmos/cosmos-sdk/types"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
vestingtypes "github.com/cosmos/cosmos-sdk/x/auth/vesting/types"
"github.com/stretchr/testify/assert"
)
func createVestingAccount ( balance sdk . Coins , vestingStart time . Time , vestingPeriods vestingtypes . Periods ) * vestingtypes . PeriodicVestingAccount {
key := secp256k1 . GenPrivKey ( )
pub := key . PubKey ( )
addr := sdk . AccAddress ( pub . Address ( ) )
acc := authtypes . NewBaseAccount ( addr , pub , 1 , 1 )
originalVesting := sdk . NewCoins ( )
for _ , vp := range vestingPeriods {
originalVesting = originalVesting . Add ( vp . Amount ... )
}
return vestingtypes . NewPeriodicVestingAccount ( acc , originalVesting , vestingStart . Unix ( ) , vestingPeriods )
}
func TestResetPeriodVestingAccount_NoVestingPeriods ( t * testing . T ) {
vestingStartTime := time . Now ( ) . Add ( - 1 * time . Hour )
vacc := createVestingAccount ( sdk . Coins { } , vestingStartTime , vestingtypes . Periods { } )
newVestingStartTime := vestingStartTime . Add ( time . Hour )
ResetPeriodicVestingAccount ( vacc , newVestingStartTime )
assert . Equal ( t , sdk . Coins { } , vacc . OriginalVesting , "expected original vesting to be zero" )
assert . Equal ( t , newVestingStartTime . Unix ( ) , vacc . StartTime , "expected vesting start time to be updated" )
assert . Equal ( t , newVestingStartTime . Unix ( ) , vacc . EndTime , "expected vesting end time to be updated" )
assert . Equal ( t , [ ] vestingtypes . Period { } , vacc . VestingPeriods , "expected vesting periods to be empty" )
}
func TestResetPeriodVestingAccount_SingleVestingPeriod_Vested ( t * testing . T ) {
2024-08-03 13:44:37 +00:00
balance := sdk . NewCoins ( sdk . NewCoin ( chaincfg . DisplayDenom , sdkmath . NewInt ( 1e6 ) ) )
2022-04-26 16:17:15 +00:00
vestingStartTime := time . Now ( ) . Add ( - 30 * 24 * time . Hour ) // 30 days in past
periods := vestingtypes . Periods {
vestingtypes . Period {
Length : 15 * 24 * 60 * 60 , // 15 days (-15 days in past)
Amount : balance ,
} ,
}
vacc := createVestingAccount ( balance , vestingStartTime , periods )
newVestingStartTime := vestingStartTime . Add ( 30 * 24 * time . Hour )
ResetPeriodicVestingAccount ( vacc , newVestingStartTime )
assert . Equal ( t , sdk . Coins { } , vacc . OriginalVesting , "expected original vesting to be zero" )
assert . Equal ( t , newVestingStartTime . Unix ( ) , vacc . StartTime , "expected vesting start time to be updated" )
assert . Equal ( t , newVestingStartTime . Unix ( ) , vacc . EndTime , "expected vesting end time to be updated" )
assert . Equal ( t , [ ] vestingtypes . Period { } , vacc . VestingPeriods , "expected vesting periods to be empty" )
}
func TestResetPeriodVestingAccount_SingleVestingPeriod_Vesting ( t * testing . T ) {
2024-08-03 13:44:37 +00:00
balance := sdk . NewCoins ( sdk . NewCoin ( chaincfg . DisplayDenom , sdkmath . NewInt ( 1e6 ) ) )
2022-04-26 16:17:15 +00:00
vestingStartTime := time . Now ( ) . Add ( - 30 * 24 * time . Hour ) // 30 days in past
periods := vestingtypes . Periods {
vestingtypes . Period {
Length : 45 * 24 * 60 * 60 , // 45 days
Amount : balance ,
} ,
}
vacc := createVestingAccount ( balance , vestingStartTime , periods )
newVestingStartTime := vestingStartTime . Add ( 30 * 24 * time . Hour )
ResetPeriodicVestingAccount ( vacc , newVestingStartTime )
// new period length 15 days
expectedEndtime := newVestingStartTime . Add ( 15 * 24 * time . Hour ) . Unix ( )
// new period length changed, amount unchanged
expectedPeriods := [ ] vestingtypes . Period {
2022-05-09 18:37:36 +00:00
{
2022-04-26 16:17:15 +00:00
Length : 15 * 24 * 60 * 60 , // 15 days
Amount : balance ,
} ,
}
assert . Equal ( t , balance , vacc . OriginalVesting , "expected original vesting to be unchanged" )
assert . Equal ( t , newVestingStartTime . Unix ( ) , vacc . StartTime , "expected vesting start time to be updated" )
assert . Equal ( t , expectedEndtime , vacc . EndTime , "expected vesting end time end at last period" )
assert . Equal ( t , expectedPeriods , vacc . VestingPeriods , "expected vesting periods to be updated" )
}
func TestResetPeriodVestingAccount_SingleVestingPeriod_ExactStartTime ( t * testing . T ) {
2024-08-03 13:44:37 +00:00
balance := sdk . NewCoins ( sdk . NewCoin ( chaincfg . DisplayDenom , sdkmath . NewInt ( 1e6 ) ) )
2022-04-26 16:17:15 +00:00
vestingStartTime := time . Now ( ) . Add ( - 30 * 24 * time . Hour ) // 30 days in past
periods := vestingtypes . Periods {
vestingtypes . Period {
Length : 30 * 24 * 60 * 60 , // 30 days - exact on the start time
Amount : balance ,
} ,
}
vacc := createVestingAccount ( balance , vestingStartTime , periods )
newVestingStartTime := vestingStartTime . Add ( 30 * 24 * time . Hour )
ResetPeriodicVestingAccount ( vacc , newVestingStartTime )
// new period length is 0
expectedEndtime := newVestingStartTime . Unix ( )
// new period length changed, amount unchanged
expectedPeriods := [ ] vestingtypes . Period { }
assert . Equal ( t , sdk . Coins { } , vacc . OriginalVesting , "expected original vesting to be unchanged" )
assert . Equal ( t , newVestingStartTime . Unix ( ) , vacc . StartTime , "expected vesting start time to be updated" )
assert . Equal ( t , expectedEndtime , vacc . EndTime , "expected vesting end time end at last period" )
assert . Equal ( t , expectedPeriods , vacc . VestingPeriods , "expected vesting periods to be updated" )
}
func TestResetPeriodVestingAccount_MultiplePeriods ( t * testing . T ) {
2024-08-03 13:44:37 +00:00
balance := sdk . NewCoins ( sdk . NewCoin ( chaincfg . DisplayDenom , sdkmath . NewInt ( 4e6 ) ) )
2022-04-26 16:17:15 +00:00
vestingStartTime := time . Now ( ) . Add ( - 30 * 24 * time . Hour ) // 30 days in past
periods := vestingtypes . Periods {
vestingtypes . Period {
Length : 15 * 24 * 60 * 60 , // -15 days - vested
2024-08-03 13:44:37 +00:00
Amount : sdk . NewCoins ( sdk . NewCoin ( chaincfg . DisplayDenom , sdkmath . NewInt ( 1e6 ) ) ) ,
2022-04-26 16:17:15 +00:00
} ,
vestingtypes . Period {
Length : 15 * 24 * 60 * 60 , // 0 days - exact on the start time
2024-08-03 13:44:37 +00:00
Amount : sdk . NewCoins ( sdk . NewCoin ( chaincfg . DisplayDenom , sdkmath . NewInt ( 1e6 ) ) ) ,
2022-04-26 16:17:15 +00:00
} ,
vestingtypes . Period {
Length : 15 * 24 * 60 * 60 , // +15 days - vesting
2024-08-03 13:44:37 +00:00
Amount : sdk . NewCoins ( sdk . NewCoin ( chaincfg . DisplayDenom , sdkmath . NewInt ( 1e6 ) ) ) ,
2022-04-26 16:17:15 +00:00
} ,
vestingtypes . Period {
Length : 15 * 24 * 60 * 60 , // +30 days - vesting
2024-08-03 13:44:37 +00:00
Amount : sdk . NewCoins ( sdk . NewCoin ( chaincfg . DisplayDenom , sdkmath . NewInt ( 1e6 ) ) ) ,
2022-04-26 16:17:15 +00:00
} ,
}
vacc := createVestingAccount ( balance , vestingStartTime , periods )
newVestingStartTime := vestingStartTime . Add ( 30 * 24 * time . Hour )
ResetPeriodicVestingAccount ( vacc , newVestingStartTime )
// new period length 15 days
expectedEndtime := newVestingStartTime . Add ( 30 * 24 * time . Hour ) . Unix ( )
// new period length changed, amount unchanged
expectedPeriods := [ ] vestingtypes . Period {
2022-05-09 18:37:36 +00:00
{
2022-04-26 16:17:15 +00:00
Length : 15 * 24 * 60 * 60 , // 15 days
2024-08-03 13:44:37 +00:00
Amount : sdk . NewCoins ( sdk . NewCoin ( chaincfg . DisplayDenom , sdkmath . NewInt ( 1e6 ) ) ) ,
2022-04-26 16:17:15 +00:00
} ,
2022-05-09 18:37:36 +00:00
{
2022-04-26 16:17:15 +00:00
Length : 15 * 24 * 60 * 60 , // 15 days
2024-08-03 13:44:37 +00:00
Amount : sdk . NewCoins ( sdk . NewCoin ( chaincfg . DisplayDenom , sdkmath . NewInt ( 1e6 ) ) ) ,
2022-04-26 16:17:15 +00:00
} ,
}
2024-08-03 13:44:37 +00:00
assert . Equal ( t , sdk . NewCoins ( sdk . NewCoin ( chaincfg . DisplayDenom , sdkmath . NewInt ( 2e6 ) ) ) , vacc . OriginalVesting , "expected original vesting to be updated" )
2022-04-26 16:17:15 +00:00
assert . Equal ( t , newVestingStartTime . Unix ( ) , vacc . StartTime , "expected vesting start time to be updated" )
assert . Equal ( t , expectedEndtime , vacc . EndTime , "expected vesting end time end at last period" )
assert . Equal ( t , expectedPeriods , vacc . VestingPeriods , "expected vesting periods to be updated" )
}
func TestResetPeriodVestingAccount_DelegatedVesting_GreaterThanVesting ( t * testing . T ) {
2024-08-03 13:44:37 +00:00
balance := sdk . NewCoins ( sdk . NewCoin ( chaincfg . DisplayDenom , sdkmath . NewInt ( 3e6 ) ) )
2022-04-26 16:17:15 +00:00
vestingStartTime := time . Now ( ) . Add ( - 30 * 24 * time . Hour ) // 30 days in past
periods := vestingtypes . Periods {
vestingtypes . Period {
Length : 15 * 24 * 60 * 60 , // -15 days - vested
2024-08-03 13:44:37 +00:00
Amount : sdk . NewCoins ( sdk . NewCoin ( chaincfg . DisplayDenom , sdkmath . NewInt ( 1e6 ) ) ) ,
2022-04-26 16:17:15 +00:00
} ,
vestingtypes . Period {
Length : 15 * 24 * 60 * 60 , // 0 days - exact on the start time
2024-08-03 13:44:37 +00:00
Amount : sdk . NewCoins ( sdk . NewCoin ( chaincfg . DisplayDenom , sdkmath . NewInt ( 1e6 ) ) ) ,
2022-04-26 16:17:15 +00:00
} ,
vestingtypes . Period {
Length : 15 * 24 * 60 * 60 , // +15 days - vesting
2024-08-03 13:44:37 +00:00
Amount : sdk . NewCoins ( sdk . NewCoin ( chaincfg . DisplayDenom , sdkmath . NewInt ( 1e6 ) ) ) ,
2022-04-26 16:17:15 +00:00
} ,
}
vacc := createVestingAccount ( balance , vestingStartTime , periods )
vacc . TrackDelegation ( vestingStartTime , balance , balance )
newVestingStartTime := vestingStartTime . Add ( 30 * 24 * time . Hour )
ResetPeriodicVestingAccount ( vacc , newVestingStartTime )
2024-08-03 13:44:37 +00:00
assert . Equal ( t , sdk . NewCoins ( sdk . NewCoin ( chaincfg . DisplayDenom , sdkmath . NewInt ( 2e6 ) ) ) , vacc . DelegatedFree , "expected delegated free to be updated" )
assert . Equal ( t , sdk . NewCoins ( sdk . NewCoin ( chaincfg . DisplayDenom , sdkmath . NewInt ( 1e6 ) ) ) , vacc . DelegatedVesting , "expected delegated vesting to be updated" )
2022-04-26 16:17:15 +00:00
}
func TestResetPeriodVestingAccount_DelegatedVesting_LessThanVested ( t * testing . T ) {
2024-08-03 13:44:37 +00:00
balance := sdk . NewCoins ( sdk . NewCoin ( chaincfg . DisplayDenom , sdkmath . NewInt ( 3e6 ) ) )
2022-04-26 16:17:15 +00:00
vestingStartTime := time . Now ( ) . Add ( - 30 * 24 * time . Hour ) // 30 days in past
periods := vestingtypes . Periods {
vestingtypes . Period {
Length : 15 * 24 * 60 * 60 , // -15 days - vested
2024-08-03 13:44:37 +00:00
Amount : sdk . NewCoins ( sdk . NewCoin ( chaincfg . DisplayDenom , sdkmath . NewInt ( 1e6 ) ) ) ,
2022-04-26 16:17:15 +00:00
} ,
vestingtypes . Period {
Length : 15 * 24 * 60 * 60 , // 0 days - exact on the start time
2024-08-03 13:44:37 +00:00
Amount : sdk . NewCoins ( sdk . NewCoin ( chaincfg . DisplayDenom , sdkmath . NewInt ( 1e6 ) ) ) ,
2022-04-26 16:17:15 +00:00
} ,
vestingtypes . Period {
Length : 15 * 24 * 60 * 60 , // +15 days - vesting
2024-08-03 13:44:37 +00:00
Amount : sdk . NewCoins ( sdk . NewCoin ( chaincfg . DisplayDenom , sdkmath . NewInt ( 1e6 ) ) ) ,
2022-04-26 16:17:15 +00:00
} ,
}
vacc := createVestingAccount ( balance , vestingStartTime , periods )
2024-08-03 13:44:37 +00:00
vacc . TrackDelegation ( vestingStartTime , balance , sdk . NewCoins ( sdk . NewCoin ( chaincfg . DisplayDenom , sdkmath . NewInt ( 1e6 ) ) ) )
2022-04-26 16:17:15 +00:00
newVestingStartTime := vestingStartTime . Add ( 30 * 24 * time . Hour )
ResetPeriodicVestingAccount ( vacc , newVestingStartTime )
assert . Equal ( t , sdk . Coins ( nil ) , vacc . DelegatedFree , "expected delegrated free to be unmodified" )
2024-08-03 13:44:37 +00:00
assert . Equal ( t , sdk . NewCoins ( sdk . NewCoin ( chaincfg . DisplayDenom , sdkmath . NewInt ( 1e6 ) ) ) , vacc . DelegatedVesting , "expected delegated vesting to be unmodified" )
2022-04-26 16:17:15 +00:00
}