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

test: Fixing tests for the new econ #312

Closed
wants to merge 17 commits into from
Closed
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
20 changes: 11 additions & 9 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -507,24 +507,26 @@ func NewArchwayApp(
// Setting gas recorder here to avoid cyclic loop
trackingWasmVm.SetGasRecorder(app.TrackingKeeper)

// Note we set up mint keeper before the x/rewards keeper as we pass it in
app.MintKeeper = mintkeeper.NewKeeper(
appCodec,
keys[minttypes.StoreKey],
app.getSubspace(minttypes.ModuleName),
app.BankKeeper,
app.StakingKeeper,
)

app.RewardsKeeper = rewardsKeeper.NewKeeper(
appCodec,
keys[rewardsTypes.StoreKey],
app.WASMKeeper,
app.TrackingKeeper,
app.AccountKeeper,
app.BankKeeper,
app.MintKeeper,
app.getSubspace(rewardsTypes.ModuleName),
)

// Note we set up mint keeper after the x/rewards keeper
app.MintKeeper = mintkeeper.NewKeeper(
appCodec,
keys[minttypes.StoreKey],
app.getSubspace(minttypes.ModuleName),
app.BankKeeper,
)

// The gov proposal types can be individually enabled
if len(enabledProposals) != 0 {
govRouter.AddRoute(wasm.RouterKey, wasm.NewWasmProposalHandler(app.WASMKeeper, enabledProposals))
Expand Down Expand Up @@ -606,7 +608,7 @@ func NewArchwayApp(
wasm.ModuleName,
// wasm gas tracking
trackingTypes.ModuleName,
rewardsTypes.ModuleName,
rewardsTypes.ModuleName, // should always be after x/mint
)

app.mm.SetOrderEndBlockers(
Expand Down
17 changes: 10 additions & 7 deletions e2e/gastracking_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,22 @@ func (s *E2ETestSuite) TestGasTrackingAndRewardsDistribution() {
inflationRewardsRatio := sdk.NewDecWithPrec(5, 1)
blockGasLimit := int64(10_000_000)
mintParams := mintTypes.DefaultParams()
mintParams.MinInflation = sdk.OneDec()
mintParams.MinInflation = sdk.NewDecWithPrec(8, 1)
mintParams.MaxInflation = sdk.NewDecWithPrec(8, 1)
mintParams.InflationRecipients = []*mintTypes.InflationRecipient{{
Recipient: rewardsTypes.ModuleName,
Ratio: inflationRewardsRatio,
}, {
Recipient: authTypes.FeeCollectorName,
Ratio: sdk.OneDec().Sub(inflationRewardsRatio),
}}

// Setup (create new chain here with custom params)
chain := e2eTesting.NewTestChain(s.T(), 1,
e2eTesting.WithTxFeeRebatesRewardsRatio(txFeeRebateRewardsRatio),
e2eTesting.WithBlockGasLimit(blockGasLimit),
// Artificially increase the minted inflation coin to get some rewards for the contract (otherwise contractOp gas / blockGasLimit ratio will be 0)
e2eTesting.WithMintParams(mintParams),
e2eTesting.WithInflationDistributionRecipient(authTypes.FeeCollectorName, sdk.OneDec().Sub(inflationRewardsRatio)),
e2eTesting.WithInflationDistributionRecipient(rewardsTypes.ModuleName, inflationRewardsRatio),
// Set default Tx fee for non-manual transaction like Upload / Instantiate
e2eTesting.WithDefaultFeeAmount("10000"),
)
Expand Down Expand Up @@ -117,10 +123,7 @@ func (s *E2ETestSuite) TestGasTrackingAndRewardsDistribution() {
{
ctx := chain.GetContext()

mintedCoin, _ := chain.GetApp().MintKeeper.GetBlockProvisions(ctx)
inflationRewards, _ := pkg.SplitCoins(sdk.NewCoins(mintedCoin), inflationRewardsRatio)
s.Require().Len(inflationRewards, 1)
blockInflationRewardsExpected = inflationRewards[0]
blockInflationRewardsExpected, _ = chain.GetApp().RewardsKeeper.GetInflationForRewards(ctx)
}

// Send contract Execute Tx with fees, fetch ABCI events and Tx gas used
Expand Down
19 changes: 13 additions & 6 deletions e2e/rewards_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
mintTypes "github.com/archway-network/archway/x/mint/types"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/query"
authTypes "github.com/cosmos/cosmos-sdk/x/auth/types"

voterCustomTypes "github.com/archway-network/voter/src/pkg/archway/custom"
voterTypes "github.com/archway-network/voter/src/types"
Expand All @@ -28,6 +29,13 @@ func (s *E2ETestSuite) TestRewardsWithdrawProfitAndFees() {
mintParams := mintTypes.DefaultParams()
mintParams.MinInflation = sdk.MustNewDecFromStr("0.1") // 10%
mintParams.MaxInflation = sdk.MustNewDecFromStr("0.1") // 10%
mintParams.InflationRecipients = []*mintTypes.InflationRecipient{{
Recipient: rewardsTypes.ModuleName,
Ratio: sdk.NewDecWithPrec(2, 1), // 20%
}, {
Recipient: authTypes.FeeCollectorName,
Ratio: sdk.NewDecWithPrec(8, 1), // 80%
}}

// Create a custom chain with "close to mainnet" params
chain := e2eTesting.NewTestChain(s.T(), 1,
Expand All @@ -40,11 +48,10 @@ func (s *E2ETestSuite) TestRewardsWithdrawProfitAndFees() {
e2eTesting.WithDefaultFeeAmount("10000000"),
// Set block gas limit (Archway mainnet param)
e2eTesting.WithBlockGasLimit(100_000_000),
// x/rewards distribution params
e2eTesting.WithTxFeeRebatesRewardsRatio(sdk.NewDecWithPrec(5, 1)),
e2eTesting.WithInflationDistributionRecipient(rewardsTypes.ModuleName, sdk.NewDecWithPrec(2, 1)),
// Set constant inflation rate
e2eTesting.WithMintParams(mintParams),
// x/rewards distribution params
e2eTesting.WithTxFeeRebatesRewardsRatio(sdk.NewDecWithPrec(5, 1)),
)
trackingKeeper, rewardsKeeper := chain.GetApp().TrackingKeeper, chain.GetApp().RewardsKeeper
chain.NextBlock(0)
Expand Down Expand Up @@ -169,7 +176,7 @@ func (s *E2ETestSuite) TestRewardsWithdrawProfitAndFees() {
}

// Mint rewards coins
s.Require().NoError(chain.GetApp().MintKeeper.MintCoins(ctx, mintTypes.ModuleName, coinsToMint))
s.Require().NoError(chain.GetApp().MintKeeper.MintCoins(ctx, coinsToMint))
s.Require().NoError(chain.GetApp().BankKeeper.SendCoinsFromModuleToModule(ctx, mintTypes.ModuleName, rewardsTypes.ContractRewardCollector, coinsToMint))

// Invariants check (just in case)
Expand Down Expand Up @@ -272,7 +279,7 @@ func (s *E2ETestSuite) TestRewardsParamMaxWithdrawRecordsLimit() {
}

mintCoins := sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, sdk.NewIntFromUint64(rewardsTypes.MaxWithdrawRecordsParamLimit)))
s.Require().NoError(mintKeeper.MintCoins(ctx, mintTypes.ModuleName, mintCoins))
s.Require().NoError(mintKeeper.MintCoins(ctx, mintCoins))
s.Require().NoError(bankKeeper.SendCoinsFromModuleToModule(ctx, mintTypes.ModuleName, rewardsTypes.ContractRewardCollector, mintCoins))
}

Expand Down Expand Up @@ -346,7 +353,7 @@ func (s *E2ETestSuite) TestRewardsRecordsQueryLimit() {
}

mintCoins := sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, sdk.NewIntFromUint64(rewardsTypes.MaxRecordsQueryLimit)))
s.Require().NoError(mintKeeper.MintCoins(ctx, mintTypes.ModuleName, mintCoins))
s.Require().NoError(mintKeeper.MintCoins(ctx, mintCoins))
s.Require().NoError(bankKeeper.SendCoinsFromModuleToModule(ctx, mintTypes.ModuleName, rewardsTypes.ContractRewardCollector, mintCoins))

recordsExpected = records
Expand Down
20 changes: 20 additions & 0 deletions e2e/testing/chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ import (
dbm "github.com/tendermint/tm-db"

"github.com/archway-network/archway/app"
mintTypes "github.com/archway-network/archway/x/mint/types"
rewardsTypes "github.com/archway-network/archway/x/rewards/types"
)

// TestChain keeps a test chain state and provides helper functions to simulate various operations.
Expand Down Expand Up @@ -196,6 +198,24 @@ func NewTestChain(t *testing.T, chainIdx int, opts ...interface{}) *TestChain {
bankGenesis := bankTypes.NewGenesisState(bankTypes.DefaultGenesisState().Params, balances, totalSupply, []bankTypes.Metadata{})
genState[bankTypes.ModuleName] = archApp.AppCodec().MustMarshalJSON(bankGenesis)

mintParams := mintTypes.NewParams(
sdk.MustNewDecFromStr("0.1"), sdk.OneDec(), // inflation
sdk.ZeroDec(), sdk.OneDec(), // bonded
sdk.MustNewDecFromStr("0.1"), // inflation change
time.Minute,
[]*mintTypes.InflationRecipient{
{
Recipient: authTypes.FeeCollectorName,
Ratio: sdk.MustNewDecFromStr("0.9"),
},
{
Recipient: rewardsTypes.ModuleName,
Ratio: sdk.MustNewDecFromStr("0.1"),
},
})
mintGenesis := mintTypes.NewGenesisState(mintParams, mintTypes.LastBlockInfo{})
genState[mintTypes.ModuleName] = archApp.AppCodec().MustMarshalJSON(mintGenesis)

signInfo := make([]slashingTypes.SigningInfo, len(validatorSet.Validators))
for i, v := range validatorSet.Validators {
signInfo[i] = slashingTypes.SigningInfo{
Expand Down
20 changes: 13 additions & 7 deletions e2e/txfees_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
wasmdTypes "github.com/CosmWasm/wasmd/x/wasm/types"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkErrors "github.com/cosmos/cosmos-sdk/types/errors"
authTypes "github.com/cosmos/cosmos-sdk/x/auth/types"
abci "github.com/tendermint/tendermint/abci/types"

voterTypes "github.com/archway-network/voter/src/types"
Expand Down Expand Up @@ -38,6 +39,13 @@ func (s *E2ETestSuite) TestTxFees() {
mintParams := mintTypes.DefaultParams()
mintParams.MinInflation = sdk.MustNewDecFromStr("0.1") // 10% (Archway mainnet param)
mintParams.MaxInflation = sdk.MustNewDecFromStr("0.1") // 10% (Archway mainnet param)
mintParams.InflationRecipients = []*mintTypes.InflationRecipient{{
Recipient: rewardsTypes.ModuleName,
Ratio: sdk.NewDecWithPrec(2, 1), // 20% (Archway mainnet param)
}, {
Recipient: authTypes.FeeCollectorName,
Ratio: sdk.NewDecWithPrec(8, 1), // 80%
}}

// Create a custom chain with fixed inflation (10%) and 10M block gas limit
chain := e2eTesting.NewTestChain(s.T(), 1,
Expand All @@ -50,18 +58,15 @@ func (s *E2ETestSuite) TestTxFees() {
e2eTesting.WithDefaultFeeAmount("10000000"),
// Set block gas limit (Archway mainnet param)
e2eTesting.WithBlockGasLimit(100_000_000),
// x/rewards distribution params
e2eTesting.WithTxFeeRebatesRewardsRatio(sdk.NewDecWithPrec(5, 1)), // 50 % (Archway mainnet param)
e2eTesting.WithInflationDistributionRecipient(rewardsTypes.ModuleName, sdk.NewDecWithPrec(2, 1)), // 20 % (Archway mainnet param)
// Set constant inflation rate
e2eTesting.WithMintParams(mintParams),
// x/rewards distribution params
e2eTesting.WithTxFeeRebatesRewardsRatio(sdk.NewDecWithPrec(5, 1)), // 50 % (Archway mainnet param)
)

// Check total supply
{
ctx := chain.GetContext()

totalSupplyMaxAmtExpected, ok := sdk.NewIntFromString("1000000050000000") // small gap for minted coins for 2 blocks
totalSupplyMaxAmtExpected, ok := sdk.NewIntFromString("1000000500000000") // small gap for minted coins for 2 blocks
s.Require().True(ok)

totalSupplyReceived := chain.GetApp().BankKeeper.GetSupply(ctx, sdk.DefaultBondDenom)
Expand Down Expand Up @@ -99,7 +104,8 @@ func (s *E2ETestSuite) TestTxFees() {
{
ctx := chain.GetContext()

mintedCoin, _ := chain.GetApp().MintKeeper.GetBlockProvisions(ctx)
mintedAmount, _ := chain.GetApp().MintKeeper.GetBlockProvisions(ctx)
mintedCoin := sdk.NewInt64Coin(chain.GetApp().StakingKeeper.BondDenom(ctx), mintedAmount.BigInt().Int64())
s.T().Logf("x/mint minted amount per block: %s", coinsToStr(mintedCoin))
}

Expand Down
2 changes: 1 addition & 1 deletion wasmbinding/rewards/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ func TestRewardsWASMBindings(t *testing.T) {
keeper.GetState().RewardsRecord(ctx).CreateRewardsRecord(contractAddr, record1RewardsExpected, ctx.BlockHeight(), ctx.BlockTime())
keeper.GetState().RewardsRecord(ctx).CreateRewardsRecord(contractAddr, record2RewardsExpected, ctx.BlockHeight(), ctx.BlockTime())
keeper.GetState().RewardsRecord(ctx).CreateRewardsRecord(contractAddr, record3RewardsExpected, ctx.BlockHeight(), ctx.BlockTime())
require.NoError(t, chain.GetApp().MintKeeper.MintCoins(ctx, mintTypes.ModuleName, recordsRewards))
require.NoError(t, chain.GetApp().MintKeeper.MintCoins(ctx, recordsRewards))
require.NoError(t, chain.GetApp().BankKeeper.SendCoinsFromModuleToModule(ctx, mintTypes.ModuleName, rewardsTypes.ContractRewardCollector, recordsRewards))

// Query available rewards
Expand Down
57 changes: 57 additions & 0 deletions x/mint/abci.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package mint

import (
"time"

"github.com/archway-network/archway/x/mint/keeper"
"github.com/archway-network/archway/x/mint/types"
"github.com/cosmos/cosmos-sdk/telemetry"
sdk "github.com/cosmos/cosmos-sdk/types"
)

// BeginBlocker mints new tokens and distributes to the inflation recipients.
func BeginBlocker(ctx sdk.Context, k keeper.Keeper) {
defer telemetry.ModuleMeasureSince(types.ModuleName, time.Now(), telemetry.MetricKeyBeginBlocker)
tokenToMint, blockInflation := k.GetBlockProvisions(ctx)

// if no tokens to be minted
if tokenToMint.IsZero() {
return
}

// mint the tokens to the recipients
mintAndDistribute(k, ctx, tokenToMint)

// update the current block inflation
blockTime := ctx.BlockTime()
lbi := types.LastBlockInfo{
Inflation: blockInflation,
Time: &blockTime,
}
err := k.SetLastBlockInfo(ctx, lbi)
if err != nil {
panic(err)
}
}

func mintAndDistribute(k keeper.Keeper, ctx sdk.Context, tokenToMint sdk.Dec) {
mintParams := k.GetParams(ctx)
denom := k.BondDenom(ctx)
mintCoin := sdk.NewInt64Coin(denom, tokenToMint.BigInt().Int64()) // as sdk.Coin

err := k.MintCoins(ctx, sdk.NewCoins(mintCoin)) // mint the tokens into x/mint
if err != nil {
panic(err)
}

for _, distribution := range mintParams.GetInflationRecipients() {
amount := distribution.Ratio.MulInt(mintCoin.Amount) // distribution.Ratio * mintedCoins
coin := sdk.NewCoin(denom, amount.TruncateInt()) // as sdk.Coin

err := k.SendCoinsToModule(ctx, distribution.Recipient, sdk.NewCoins(coin)) // distribute the tokens from x/mint
if err != nil {
panic(err)
}
k.SetInflationForRecipient(ctx, distribution.Recipient, coin) // store how much was was minted for given module
}
}
Loading