Skip to content

Commit

Permalink
use current time
Browse files Browse the repository at this point in the history
  • Loading branch information
BrandonWeng committed Apr 25, 2023
1 parent 15a8024 commit 922ceae
Show file tree
Hide file tree
Showing 8 changed files with 209 additions and 62 deletions.
3 changes: 2 additions & 1 deletion scripts/old_initialize_local.sh
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ python3 loadtest/scripts/populate_genesis_accounts.py 50 loc
~/go/bin/seid collect-gentxs
cat ~/.sei/config/genesis.json | jq '.app_state["gov"]["deposit_params"]["max_deposit_period"]="300s"' > ~/.sei/config/tmp_genesis.json && mv ~/.sei/config/tmp_genesis.json ~/.sei/config/genesis.json
cat ~/.sei/config/genesis.json | jq '.app_state["gov"]["voting_params"]["voting_period"]="5s"' > ~/.sei/config/tmp_genesis.json && mv ~/.sei/config/tmp_genesis.json ~/.sei/config/genesis.json
cat ~/.sei/config/genesis.json | jq --arg release_date "$(date +"%Y-%m-%d")" '.app_state["mint"]["params"]["token_release_schedule"]=[{"date": $release_date, "token_release_amount": "999999999999"}]' > ~/.sei/config/tmp_genesis.json && mv ~/.sei/config/tmp_genesis.json ~/.sei/config/genesis.json
cat ~/.sei/config/genesis.json | jq --arg start_date "$(date +"%Y-%m-%d")" --arg end_date "$(date -v+3d +"%Y-%m-%d")" '.app_state["mint"]["params"]["token_release_schedule"]=[{"start_date": $start_date, "end_date": $end_date, "token_release_amount": "999999999999"}]' > ~/.sei/config/tmp_genesis.json && mv ~/.sei/config/tmp_genesis.json ~/.sei/config/genesis.json
cat ~/.sei/config/genesis.json | jq --arg start_date "$(date -v+3d +"%Y-%m-%d")" --arg end_date "$(date -v+5d +"%Y-%m-%d")" '.app_state["mint"]["params"]["token_release_schedule"] += [{"start_date": $start_date, "end_date": $end_date, "token_release_amount": "999999999999"}]' > ~/.sei/config/tmp_genesis.json && mv ~/.sei/config/tmp_genesis.json ~/.sei/config/genesis.json
cat ~/.sei/config/genesis.json | jq '.app_state["gov"]["voting_params"]["expedited_voting_period"]="4s"' > ~/.sei/config/tmp_genesis.json && mv ~/.sei/config/tmp_genesis.json ~/.sei/config/genesis.json
cat ~/.sei/config/genesis.json | jq '.app_state["oracle"]["params"]["vote_period"]="1"' > ~/.sei/config/tmp_genesis.json && mv ~/.sei/config/tmp_genesis.json ~/.sei/config/genesis.json
cat ~/.sei/config/genesis.json | jq '.app_state["distribution"]["params"]["community_tax"]="0.000000000000000000"' > ~/.sei/config/tmp_genesis.json && mv ~/.sei/config/tmp_genesis.json ~/.sei/config/genesis.json
Expand Down
3 changes: 0 additions & 3 deletions x/mint/keeper/hooks.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package keeper

import (
"fmt"

sdk "github.com/cosmos/cosmos-sdk/types"
epochTypes "github.com/sei-protocol/sei-chain/x/epoch/types"
)
Expand All @@ -14,7 +12,6 @@ func (k Keeper) AfterEpochEnd(ctx sdk.Context, epoch epochTypes.Epoch) {
latestMinter := k.GetOrUpdateLatestMinter(ctx, epoch)
coinsToMint := latestMinter.GetReleaseAmountToday(epoch.CurrentEpochStartTime)

fmt.Printf("Coins to Mint: %s\n", coinsToMint.String())
if coinsToMint.IsZero() {
k.Logger(ctx).Debug("No coins to mint", "minter", latestMinter)
return
Expand Down
175 changes: 157 additions & 18 deletions x/mint/keeper/hooks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,22 +32,32 @@ func getEpoch(genesisTime time.Time, currTime time.Time) types.Epoch {
}

func TestEndOfEpochMintedCoinDistribution(t *testing.T) {
seiApp := keepertest.TestApp()
ctx := seiApp.BaseApp.NewContext(false, tmproto.Header{Time: time.Now()})
ctx = ctx.WithContext(context.WithValue(ctx.Context(), dexutils.DexMemStateContextKey, dexcache.NewMemState(seiApp.GetKey(types.StoreKey))))

header := tmproto.Header{
Height: seiApp.LastBlockHeight() + 1,
Time: time.Now(),
}
seiApp.BeginBlock(ctx, abci.RequestBeginBlock{Header: header})
genesisTime := header.Time
t.Parallel()

t.Run("Initial should be zero", func(t *testing.T) {
seiApp := keepertest.TestApp()
ctx := seiApp.BaseApp.NewContext(false, tmproto.Header{Time: time.Now()})
ctx = ctx.WithContext(context.WithValue(ctx.Context(), dexutils.DexMemStateContextKey, dexcache.NewMemState(seiApp.GetKey(types.StoreKey))))

header := tmproto.Header{
Height: seiApp.LastBlockHeight() + 1,
Time: time.Now().UTC(),
}
seiApp.BeginBlock(ctx, abci.RequestBeginBlock{Header: header})
require.Equal(t, int64(0), seiApp.MintKeeper.GetMinter(ctx).GetLastMintAmountCoin().Amount.Int64())
})

t.Run("even full release", func(t *testing.T) {
seiApp := keepertest.TestApp()
ctx := seiApp.BaseApp.NewContext(false, tmproto.Header{Time: time.Now()})
ctx = ctx.WithContext(context.WithValue(ctx.Context(), dexutils.DexMemStateContextKey, dexcache.NewMemState(seiApp.GetKey(types.StoreKey))))

header := tmproto.Header{
Height: seiApp.LastBlockHeight() + 1,
Time: time.Now().UTC(),
}
seiApp.BeginBlock(ctx, abci.RequestBeginBlock{Header: header})
genesisTime := header.Time
tokenReleaseSchedle := []minttypes.ScheduledTokenRelease{
{
StartDate: genesisTime.AddDate(0, 0, 0).Format(minttypes.TokenReleaseDateFormat),
Expand All @@ -61,7 +71,7 @@ func TestEndOfEpochMintedCoinDistribution(t *testing.T) {
)
seiApp.MintKeeper.SetParams(ctx, mintParams)

for i := 0; i < 26; i++ {
for i := 0; i < 25; i++ {
currTime := genesisTime.AddDate(0, 0, i)
currEpoch := getEpoch(genesisTime, currTime)
seiApp.EpochKeeper.BeforeEpochStart(ctx, currEpoch)
Expand All @@ -72,7 +82,8 @@ func TestEndOfEpochMintedCoinDistribution(t *testing.T) {
expectedAmount := int64(100000)
newMinter := seiApp.MintKeeper.GetMinter(ctx)

if newMinter.GetRemainingMintAmount() == 0 && i == 25 {
if i == 25 {
require.Zero(t, newMinter.GetRemainingMintAmount(), "Remaining amount should be zero")
break
}

Expand All @@ -83,6 +94,17 @@ func TestEndOfEpochMintedCoinDistribution(t *testing.T) {
})

t.Run("uneven full release", func(t *testing.T) {
seiApp := keepertest.TestApp()
ctx := seiApp.BaseApp.NewContext(false, tmproto.Header{Time: time.Now()})
ctx = ctx.WithContext(context.WithValue(ctx.Context(), dexutils.DexMemStateContextKey, dexcache.NewMemState(seiApp.GetKey(types.StoreKey))))

header := tmproto.Header{
Height: seiApp.LastBlockHeight() + 1,
Time: time.Now().UTC(),
}
seiApp.BeginBlock(ctx, abci.RequestBeginBlock{Header: header})
genesisTime := header.Time

tokenReleaseSchedle := []minttypes.ScheduledTokenRelease{
{
StartDate: genesisTime.AddDate(0, 0, 0).Format(minttypes.TokenReleaseDateFormat),
Expand All @@ -96,21 +118,138 @@ func TestEndOfEpochMintedCoinDistribution(t *testing.T) {
)
seiApp.MintKeeper.SetParams(ctx, mintParams)

for i := 0; i < 15; i++ {
for i := 0; i < 25; i++ {
currTime := genesisTime.AddDate(0, 0, i)
currEpoch := getEpoch(genesisTime, currTime)
seiApp.EpochKeeper.BeforeEpochStart(ctx, currEpoch)
seiApp.EpochKeeper.AfterEpochEnd(ctx, currEpoch)
mintParams = seiApp.MintKeeper.GetParams(ctx)

// 250k / 25 days = 100000 per day
expectedAmount := int64(100000)
expectedAmount := int64(104166)
newMinter := seiApp.MintKeeper.GetMinter(ctx)
println(currTime.Format(minttypes.TokenReleaseDateFormat))

// Uneven distribution still results in 250k total distributed
if i == 24 {
require.Zero(t, newMinter.GetRemainingMintAmount(), "Remaining amount should be zero")
break
}

require.Equal(t, currTime.Format(minttypes.TokenReleaseDateFormat), newMinter.GetLastMintDate(), "Last mint date should be correct")
require.Equal(t, expectedAmount, newMinter.GetLastMintAmountCoin().Amount.Int64(), "Minted amount should be correct")
require.Equal(t, int64(2500000 - expectedAmount * int64(i+1)), int64(newMinter.GetRemainingMintAmount()), "Remaining amount should be correct")
require.InDelta(t, expectedAmount, newMinter.GetLastMintAmountCoin().Amount.Int64(), 1, "Minted amount should be correct")
require.InDelta(t, int64(2500000 - expectedAmount * int64(i+1)), int64(newMinter.GetRemainingMintAmount()), 24, "Remaining amount should be correct")
}
})

t.Run("multiple full releases", func(t *testing.T) {
seiApp := keepertest.TestApp()
ctx := seiApp.BaseApp.NewContext(false, tmproto.Header{Time: time.Now()})
ctx = ctx.WithContext(context.WithValue(ctx.Context(), dexutils.DexMemStateContextKey, dexcache.NewMemState(seiApp.GetKey(types.StoreKey))))

header := tmproto.Header{
Height: seiApp.LastBlockHeight() + 1,
Time: time.Now().UTC(),
}
seiApp.BeginBlock(ctx, abci.RequestBeginBlock{Header: header})
genesisTime := header.Time

tokenReleaseSchedle := []minttypes.ScheduledTokenRelease{
{
StartDate: genesisTime.AddDate(0, 0, 0).Format(minttypes.TokenReleaseDateFormat),
EndDate: genesisTime.AddDate(0, 0, 24).Format(minttypes.TokenReleaseDateFormat),
TokenReleaseAmount: 2500000,
},
{
StartDate: genesisTime.AddDate(0, 0, 24).Format(minttypes.TokenReleaseDateFormat),
EndDate: genesisTime.AddDate(0, 0, 30).Format(minttypes.TokenReleaseDateFormat),
TokenReleaseAmount: 2500000,
},
{
StartDate: genesisTime.AddDate(0, 0, 30).Format(minttypes.TokenReleaseDateFormat),
EndDate: genesisTime.AddDate(0, 0, 40).Format(minttypes.TokenReleaseDateFormat),
TokenReleaseAmount: 2500000,
},
{
StartDate: genesisTime.AddDate(0, 0, 45).Format(minttypes.TokenReleaseDateFormat),
EndDate: genesisTime.AddDate(0, 0, 50).Format(minttypes.TokenReleaseDateFormat),
TokenReleaseAmount: 2500000,
},
}
mintParams := minttypes.NewParams(
"usei",
tokenReleaseSchedle,
)
seiApp.MintKeeper.SetParams(ctx, mintParams)

for i := 0; i < 50; i++ {
currTime := genesisTime.AddDate(0, 0, i)
currEpoch := getEpoch(genesisTime, currTime)
seiApp.EpochKeeper.BeforeEpochStart(ctx, currEpoch)
seiApp.EpochKeeper.AfterEpochEnd(ctx, currEpoch)
mintParams = seiApp.MintKeeper.GetParams(ctx)

newMinter := seiApp.MintKeeper.GetMinter(ctx)

// Should be zero by the end of each release and when there's no release scheduled
if i == 23 || i == 29 || i == 39 || i == 49 || (i >= 40 && i < 45) {
require.Zero(t, newMinter.GetRemainingMintAmount(), "Remaining amount should be zero at %s", currTime.Format(minttypes.TokenReleaseDateFormat))
continue
}

require.Equal(t, currTime.Format(minttypes.TokenReleaseDateFormat), newMinter.GetLastMintDate(), "Last mint date should be correct")
}
})

t.Run("outage during release", func(t *testing.T) {
seiApp := keepertest.TestApp()
ctx := seiApp.BaseApp.NewContext(false, tmproto.Header{Time: time.Now()})
ctx = ctx.WithContext(context.WithValue(ctx.Context(), dexutils.DexMemStateContextKey, dexcache.NewMemState(seiApp.GetKey(types.StoreKey))))

header := tmproto.Header{
Height: seiApp.LastBlockHeight() + 1,
Time: time.Now().UTC(),
}
seiApp.BeginBlock(ctx, abci.RequestBeginBlock{Header: header})
genesisTime := header.Time

tokenReleaseSchedle := []minttypes.ScheduledTokenRelease{
{
StartDate: genesisTime.AddDate(0, 0, 0).Format(minttypes.TokenReleaseDateFormat),
EndDate: genesisTime.AddDate(0, 0, 24).Format(minttypes.TokenReleaseDateFormat),
TokenReleaseAmount: 2500000,
},
}
mintParams := minttypes.NewParams(
"usei",
tokenReleaseSchedle,
)
seiApp.MintKeeper.SetParams(ctx, mintParams)

for i := 0; i < 13; i++ {
currTime := genesisTime.AddDate(0, 0, i)
currEpoch := getEpoch(genesisTime, currTime)
seiApp.EpochKeeper.BeforeEpochStart(ctx, currEpoch)
seiApp.EpochKeeper.AfterEpochEnd(ctx, currEpoch)
mintParams = seiApp.MintKeeper.GetParams(ctx)

newMinter := seiApp.MintKeeper.GetMinter(ctx)
expectedAmount := int64(104166)

require.Equal(t, currTime.Format(minttypes.TokenReleaseDateFormat), newMinter.GetLastMintDate(), "Last mint date should be correct")
require.InDelta(t, expectedAmount, newMinter.GetLastMintAmountCoin().Amount.Int64(), 1, "Minted amount should be correct")
require.InDelta(t, int64(2500000 - expectedAmount * int64(i+1)), int64(newMinter.GetRemainingMintAmount()), 24, "Remaining amount should be correct")
}

// 3 day outage
postOutageTime := genesisTime.AddDate(0, 0, 15)
currEpoch := getEpoch(genesisTime, postOutageTime)
seiApp.EpochKeeper.BeforeEpochStart(ctx, currEpoch)
seiApp.EpochKeeper.AfterEpochEnd(ctx, currEpoch)
mintParams = seiApp.MintKeeper.GetParams(ctx)

newMinter := seiApp.MintKeeper.GetMinter(ctx)
require.Equal(t, postOutageTime.Format(minttypes.TokenReleaseDateFormat), newMinter.GetLastMintDate(), "Last mint date should be correct")
require.InDelta(t, 127315, newMinter.GetLastMintAmountCoin().Amount.Int64(), 1, "Minted amount should be correct")
require.InDelta(t, int64(1018522), int64(newMinter.GetRemainingMintAmount()), 24, "Remaining amount should be correct")
})
}

Expand Down
14 changes: 9 additions & 5 deletions x/mint/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,6 @@ func (k Keeper) GetMinter(ctx sdk.Context) (minter types.Minter) {

// set the minter
func (k Keeper) SetMinter(ctx sdk.Context, minter types.Minter) {
fmt.Printf("New Minter: %s\n", minter.String())
store := ctx.KVStore(k.storeKey)
b := k.cdc.MustMarshal(&minter)
store.Set(types.MinterKey, b)
Expand Down Expand Up @@ -135,9 +134,10 @@ func (k Keeper) GetOrUpdateLatestMinter(
params := k.GetParams(ctx)
currentReleaseMinter := k.GetMinter(ctx)
nextScheduledRelease := GetNextScheduledTokenRelease(epoch, params.TokenReleaseSchedule, currentReleaseMinter)

// There's still an ongoing release
if currentReleaseMinter.OngoingRelease() || nextScheduledRelease == nil {
k.Logger(ctx).Debug("Ongoing token release or no nextScheduledRelease", "minter", currentReleaseMinter, "nextScheduledRelease", nextScheduledRelease)
k.Logger(ctx).Debug("Ongoing token release or no nextScheduledRelease", "minter", currentReleaseMinter)
return currentReleaseMinter
}

Expand All @@ -160,9 +160,13 @@ func GetNextScheduledTokenRelease(
// This should not happen as the scheduled release date is validated when the param is updated
panic(fmt.Errorf("invalid scheduled release date: %s", err))
}
// If blocktime is after the currentScheduled date and it's after the current release
if epoch.GetCurrentEpochStartTime().After(scheduledStartDate) && scheduledStartDate.After(currentMinter.GetEndDateTime()) {
return &scheduledRelease
scheduledStartDateTime := scheduledStartDate.UTC()

// If epoch is after the currentScheduled date and it's after the current release
if epoch.GetCurrentEpochStartTime().After(scheduledStartDateTime) {
if scheduledStartDateTime.After(currentMinter.GetEndDateTime()) || scheduledStartDateTime.Equal(currentMinter.GetEndDateTime()) {
return &scheduledRelease
}
}
}
return nil
Expand Down
16 changes: 13 additions & 3 deletions x/mint/keeper/keeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
func TestGetNextScheduledTokenRelease(t *testing.T) {
t.Parallel()

currentTime := time.Now()
currentTime := time.Now().UTC()
epoch := epochTypes.Epoch{
CurrentEpochStartTime: currentTime,
CurrentEpochHeight: 100,
Expand All @@ -41,21 +41,31 @@ func TestGetNextScheduledTokenRelease(t *testing.T) {
}

t.Run("Get the next scheduled token release", func(t *testing.T) {
epoch.CurrentEpochStartTime = currentTime.AddDate(0, 0, 1)
// No next scheduled token release intially
epoch.CurrentEpochStartTime = currentTime.AddDate(0, 0, 0)
nextScheduledRelease := mintKeeper.GetNextScheduledTokenRelease(epoch, tokenReleaseSchedule, currentMinter)
require.Nil(t, nextScheduledRelease)

epoch.CurrentEpochStartTime = currentTime.AddDate(0, 0, 1)
nextScheduledRelease = mintKeeper.GetNextScheduledTokenRelease(epoch, tokenReleaseSchedule, currentMinter)
require.NotNil(t, nextScheduledRelease)
require.Equal(t, uint64(100), nextScheduledRelease.TokenReleaseAmount)
})

t.Run("No next scheduled token release, assume we are on the second period", func(t *testing.T) {
// No next scheduled token release intially
epoch.CurrentEpochStartTime = currentTime.AddDate(0, 0, 0)
nextScheduledRelease := mintKeeper.GetNextScheduledTokenRelease(epoch, tokenReleaseSchedule, currentMinter)
require.Nil(t, nextScheduledRelease)

secondMinter := mintTypes.NewMinter(
currentTime.AddDate(0, 0, 30).Format(minttypes.TokenReleaseDateFormat),
currentTime.AddDate(0, 2, 0).Format(minttypes.TokenReleaseDateFormat),
"usei",
200,
)
epoch.CurrentEpochStartTime = currentTime.AddDate(0, 5, 0)
nextScheduledRelease := mintKeeper.GetNextScheduledTokenRelease(epoch, tokenReleaseSchedule, secondMinter)
nextScheduledRelease = mintKeeper.GetNextScheduledTokenRelease(epoch, tokenReleaseSchedule, secondMinter)
require.Nil(t, nextScheduledRelease)
})

Expand Down
Loading

0 comments on commit 922ceae

Please sign in to comment.