Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(inflation): Implement Akash custom inflation function according to the whitepaper. #1352

Merged
merged 10 commits into from
Nov 24, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 29 additions & 3 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"io"
"net/http"
"os"
"path/filepath"
"time"

bam "github.com/cosmos/cosmos-sdk/baseapp"
"github.com/cosmos/cosmos-sdk/client"
Expand Down Expand Up @@ -55,9 +57,12 @@ import (
ibc "github.com/cosmos/ibc-go/modules/core"
porttypes "github.com/cosmos/ibc-go/modules/core/05-port/types"
"github.com/gorilla/mux"
"github.com/ovrclk/akash/x/inflation"
inflationtypes "github.com/ovrclk/akash/x/inflation/types/v1beta2"
"github.com/rakyll/statik/fs"
"github.com/spf13/cast"
tmjson "github.com/tendermint/tendermint/libs/json"
tmtypes "github.com/tendermint/tendermint/types"

"github.com/cosmos/cosmos-sdk/x/params"
paramskeeper "github.com/cosmos/cosmos-sdk/x/params/keeper"
Expand Down Expand Up @@ -155,6 +160,7 @@ type AkashApp struct {
provider pkeeper.IKeeper
audit audit.Keeper
cert cert.Keeper
inflation inflation.Keeper
}

mm *module.Manager
Expand All @@ -173,6 +179,8 @@ func NewApp(
logger log.Logger, db dbm.DB, tio io.Writer, loadLatest bool, invCheckPeriod uint, skipUpgradeHeights map[int64]bool,
homePath string, appOpts servertypes.AppOptions, options ...func(*bam.BaseApp),
) *AkashApp {
// find out the genesis time, to be used later in inflation calculation
genesisTime := getGenesisTime(appOpts, homePath)

// TODO: Remove cdc in favor of appCodec once all modules are migrated.
encodingConfig := MakeEncodingConfig()
Expand Down Expand Up @@ -349,7 +357,7 @@ func NewApp(
capability.NewAppModule(appCodec, *app.keeper.cap),
crisis.NewAppModule(&app.keeper.crisis, skipGenesisInvariants),
gov.NewAppModule(appCodec, app.keeper.gov, app.keeper.acct, app.keeper.bank),
mint.NewAppModule(appCodec, app.keeper.mint, app.keeper.acct),
mint.NewAppModule(appCodec, app.keeper.mint, app.keeper.acct, inflationtypes.GetInflationCalculator(genesisTime, app.GetSubspace(inflation.ModuleName))),
slashing.NewAppModule(appCodec, app.keeper.slashing, app.keeper.acct, app.keeper.bank, app.keeper.staking),
distr.NewAppModule(appCodec, app.keeper.distr, app.keeper.acct, app.keeper.bank, app.keeper.staking),
staking.NewAppModule(appCodec, app.keeper.staking, app.keeper.acct, app.keeper.bank),
Expand Down Expand Up @@ -416,7 +424,7 @@ func NewApp(
bank.NewAppModule(appCodec, app.keeper.bank, app.keeper.acct),
capability.NewAppModule(appCodec, *app.keeper.cap),
gov.NewAppModule(appCodec, app.keeper.gov, app.keeper.acct, app.keeper.bank),
mint.NewAppModule(appCodec, app.keeper.mint, app.keeper.acct),
mint.NewAppModule(appCodec, app.keeper.mint, app.keeper.acct, inflationtypes.GetInflationCalculator(genesisTime, app.GetSubspace(inflation.ModuleName))),
staking.NewAppModule(appCodec, app.keeper.staking, app.keeper.acct, app.keeper.bank),
distr.NewAppModule(appCodec, app.keeper.distr, app.keeper.acct, app.keeper.bank, app.keeper.staking),
slashing.NewAppModule(appCodec, app.keeper.slashing, app.keeper.acct, app.keeper.bank, app.keeper.staking),
Expand Down Expand Up @@ -510,14 +518,32 @@ func (app *AkashApp) registerUpgradeHandlers() {

if upgradeInfo.Name == "akash_v0.15.0_cosmos_v0.44.x" && !app.keeper.upgrade.IsSkipHeight(upgradeInfo.Height) {
storeUpgrades := storetypes.StoreUpgrades{
Added: []string{"authz"},
Added: []string{"authz", "inflation"},
}

// configure store loader that checks if version == upgradeHeight and applies store upgrades
app.SetStoreLoader(upgradetypes.UpgradeStoreLoader(upgradeInfo.Height, &storeUpgrades))
}
}

func getGenesisTime(appOpts servertypes.AppOptions, homePath string) time.Time {
if v := appOpts.Get("GenesisTime"); v != nil {
// in tests, GenesisTime is supplied using appOpts
genTime, ok := v.(time.Time)
if !ok {
panic("expected GenesisTime to be a Time value")
}
return genTime
}

genDoc, err := tmtypes.GenesisDocFromFile(filepath.Join(homePath, "config/genesis.json"))
if err != nil {
panic(err)
}

return genDoc.GenesisTime
}

// MakeCodecs constructs the *std.Codec and *codec.LegacyAmino instances used by
// simapp. It is useful for tests and clients who do not want to construct the
// full simapp
Expand Down
20 changes: 20 additions & 0 deletions app/app_configure.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/ovrclk/akash/x/deployment"
"github.com/ovrclk/akash/x/escrow"
ekeeper "github.com/ovrclk/akash/x/escrow/keeper"
"github.com/ovrclk/akash/x/inflation"
"github.com/ovrclk/akash/x/market"
mhooks "github.com/ovrclk/akash/x/market/hooks"
"github.com/ovrclk/akash/x/provider"
Expand All @@ -22,6 +23,7 @@ func akashModuleBasics() []module.AppModuleBasic {
provider.AppModuleBasic{},
audit.AppModuleBasic{},
cert.AppModuleBasic{},
inflation.AppModuleBasic{},
}
}

Expand All @@ -33,12 +35,14 @@ func akashKVStoreKeys() []string {
provider.StoreKey,
audit.StoreKey,
cert.StoreKey,
inflation.StoreKey,
}
}

func akashSubspaces(k paramskeeper.Keeper) paramskeeper.Keeper {
k.Subspace(deployment.ModuleName)
k.Subspace(market.ModuleName)
k.Subspace(inflation.ModuleName)
return k
}

Expand Down Expand Up @@ -83,6 +87,12 @@ func (app *AkashApp) setAkashKeepers() {
app.appCodec,
app.keys[cert.StoreKey],
)

app.keeper.inflation = inflation.NewKeeper(
app.appCodec,
app.keys[inflation.StoreKey],
app.GetSubspace(inflation.ModuleName),
)
}

func (app *AkashApp) akashAppModules() []module.AppModule {
Expand Down Expand Up @@ -128,6 +138,11 @@ func (app *AkashApp) akashAppModules() []module.AppModule {
app.appCodec,
app.keeper.cert,
),

inflation.NewAppModule(
app.appCodec,
app.keeper.inflation,
),
}
}

Expand All @@ -144,6 +159,7 @@ func (app *AkashApp) akashInitGenesisOrder() []string {
deployment.ModuleName,
provider.ModuleName,
market.ModuleName,
inflation.ModuleName,
}
}

Expand Down Expand Up @@ -172,5 +188,9 @@ func (app *AkashApp) akashSimModules() []module.AppModuleSimulation {
cert.NewAppModuleSimulation(
app.keeper.cert,
),

inflation.NewAppModuleSimulation(
app.keeper.inflation,
),
}
}
5 changes: 2 additions & 3 deletions app/app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,13 @@ import (
"github.com/tendermint/tendermint/libs/log"
dbm "github.com/tendermint/tm-db"

"github.com/cosmos/cosmos-sdk/simapp"
abci "github.com/tendermint/tendermint/abci/types"
)

func TestAppExport(t *testing.T) {
db := dbm.NewMemDB()
app1 := NewApp(log.NewTMLogger(log.NewSyncWriter(os.Stdout)),
db, nil, true, 0, map[int64]bool{}, DefaultHome, simapp.EmptyAppOptions{})
db, nil, true, 0, map[int64]bool{}, DefaultHome, OptsWithGenesisTime(0))

for acc := range MacPerms() {
require.Equal(t, !allowedReceivingModAcc[acc], app1.keeper.bank.BlockedAddr(app1.keeper.acct.GetModuleAddress(acc)),
Expand All @@ -37,7 +36,7 @@ func TestAppExport(t *testing.T) {
app1.Commit()

// Making a new app object with the db, so that initchain hasn't been called
app2 := NewApp(log.NewTMLogger(log.NewSyncWriter(os.Stdout)), db, nil, true, 0, map[int64]bool{}, DefaultHome, simapp.EmptyAppOptions{})
app2 := NewApp(log.NewTMLogger(log.NewSyncWriter(os.Stdout)), db, nil, true, 0, map[int64]bool{}, DefaultHome, OptsWithGenesisTime(0))
_, err = app2.ExportAppStateAndValidators(false, []string{})
require.NoError(t, err, "ExportAppStateAndValidators should not have an error")
}
16 changes: 15 additions & 1 deletion app/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package app
import (
"encoding/json"
"log"
"math/rand"

abci "github.com/tendermint/tendermint/abci/types"
logger "github.com/tendermint/tendermint/libs/log"
Expand All @@ -12,9 +13,11 @@ import (
servertypes "github.com/cosmos/cosmos-sdk/server/types"
"github.com/cosmos/cosmos-sdk/simapp"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/simulation"
slashingtypes "github.com/cosmos/cosmos-sdk/x/slashing/types"
"github.com/cosmos/cosmos-sdk/x/staking"
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
"github.com/spf13/viper"
)

// ExportAppStateAndValidators exports the state of the application for a genesis
Expand Down Expand Up @@ -204,7 +207,7 @@ func Setup(isCheckTx bool) *AkashApp {
db := dbm.NewMemDB()

// TODO: make this configurable via config or flag.
app := NewApp(logger.NewNopLogger(), db, nil, true, 5, map[int64]bool{}, DefaultHome, simapp.EmptyAppOptions{})
app := NewApp(logger.NewNopLogger(), db, nil, true, 5, map[int64]bool{}, DefaultHome, OptsWithGenesisTime(0))
if !isCheckTx {
// init chain must be called to stop deliverState from being nil
genesisState := NewDefaultGenesisState()
Expand All @@ -224,3 +227,14 @@ func Setup(isCheckTx bool) *AkashApp {

return app
}

func OptsWithGenesisTime(seed int64) servertypes.AppOptions {
r := rand.New(rand.NewSource(seed)) // nolint: gosec
genTime := simulation.RandTimestamp(r)

appOpts := viper.New()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is the rationale behind creating our own viper instance here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NewApp needs an instance of servertypes.AppOptions interface.
Viper implements this interface, so using it for testing.

appOpts.Set("GenesisTime", genTime)
simapp.FlagGenesisTimeValue = genTime.Unix()

return appOpts
}
12 changes: 6 additions & 6 deletions app/sim_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ func TestFullAppSimulation(t *testing.T) {
require.NoError(t, os.RemoveAll(dir))
}()

app1 := NewApp(logger, db, nil, true, simapp.FlagPeriodValue, map[int64]bool{}, DefaultHome, simapp.EmptyAppOptions{}, fauxMerkleModeOpt)
app1 := NewApp(logger, db, nil, true, simapp.FlagPeriodValue, map[int64]bool{}, DefaultHome, OptsWithGenesisTime(config.Seed), fauxMerkleModeOpt)
require.Equal(t, "akash", app1.Name())

fmt.Printf("config-------- %v", config)
Expand Down Expand Up @@ -112,7 +112,7 @@ func TestAppImportExport(t *testing.T) {
require.NoError(t, os.RemoveAll(dir))
}()

app := NewApp(logger, db, nil, true, simapp.FlagPeriodValue, map[int64]bool{}, DefaultHome, simapp.EmptyAppOptions{}, fauxMerkleModeOpt)
app := NewApp(logger, db, nil, true, simapp.FlagPeriodValue, map[int64]bool{}, DefaultHome, OptsWithGenesisTime(config.Seed), fauxMerkleModeOpt)
require.Equal(t, AppName, app.Name())

// Run randomized simulation
Expand Down Expand Up @@ -142,7 +142,7 @@ func TestAppImportExport(t *testing.T) {
require.NoError(t, os.RemoveAll(newDir))
}()

newApp := NewApp(log.NewNopLogger(), newDB, nil, true, simapp.FlagPeriodValue, map[int64]bool{}, DefaultHome, simapp.EmptyAppOptions{}, fauxMerkleModeOpt)
newApp := NewApp(log.NewNopLogger(), newDB, nil, true, simapp.FlagPeriodValue, map[int64]bool{}, DefaultHome, OptsWithGenesisTime(config.Seed), fauxMerkleModeOpt)
require.Equal(t, AppName, newApp.Name())

var genesisState simapp.GenesisState
Expand Down Expand Up @@ -202,7 +202,7 @@ func TestAppSimulationAfterImport(t *testing.T) {
require.NoError(t, os.RemoveAll(dir))
}()

app := NewApp(logger, db, nil, true, simapp.FlagPeriodValue, map[int64]bool{}, DefaultHome, simapp.EmptyAppOptions{}, fauxMerkleModeOpt)
app := NewApp(logger, db, nil, true, simapp.FlagPeriodValue, map[int64]bool{}, DefaultHome, OptsWithGenesisTime(config.Seed), fauxMerkleModeOpt)
require.Equal(t, AppName, app.Name())

// Run randomized simulation
Expand Down Expand Up @@ -237,7 +237,7 @@ func TestAppSimulationAfterImport(t *testing.T) {
require.NoError(t, os.RemoveAll(newDir))
}()

newApp := NewApp(log.NewNopLogger(), newDB, nil, true, simapp.FlagPeriodValue, map[int64]bool{}, DefaultHome, simapp.EmptyAppOptions{}, fauxMerkleModeOpt)
newApp := NewApp(log.NewNopLogger(), newDB, nil, true, simapp.FlagPeriodValue, map[int64]bool{}, DefaultHome, OptsWithGenesisTime(config.Seed), fauxMerkleModeOpt)
require.Equal(t, AppName, newApp.Name())

newApp.InitChain(abci.RequestInitChain{
Expand Down Expand Up @@ -277,7 +277,7 @@ func TestAppStateDeterminism(t *testing.T) {

db := dbm.NewMemDB()

app := NewApp(logger, db, nil, true, simapp.FlagPeriodValue, map[int64]bool{}, DefaultHome, simapp.EmptyAppOptions{}, interBlockCacheOpt())
app := NewApp(logger, db, nil, true, simapp.FlagPeriodValue, map[int64]bool{}, DefaultHome, OptsWithGenesisTime(config.Seed), interBlockCacheOpt())

fmt.Printf(
"running non-determinism simulation; seed %d: %d/%d, attempt: %d/%d\n",
Expand Down
8 changes: 5 additions & 3 deletions client/docs/statik/statik.go

Large diffs are not rendered by default.

Loading