Skip to content

Commit

Permalink
Merge branch 'main' into kogisin/59-add-metrics
Browse files Browse the repository at this point in the history
* main:
  docs: add -i options for sed considering os (tendermint#68)
  fix: Add govHandler on testcode for budget proposal and Fix Expired rule (tendermint#66)
  fix: panic instead of ignoring errors
  • Loading branch information
jaybxyz committed Nov 3, 2021
2 parents 7446de3 + 7aaf7b8 commit 62151eb
Show file tree
Hide file tree
Showing 6 changed files with 297 additions and 29 deletions.
31 changes: 22 additions & 9 deletions docs/Tutorials/localnet/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,28 @@ $BINARY add-genesis-account $($BINARY keys show user2 --keyring-backend test -a)
$BINARY gentx val1 100000000stake --chain-id $CHAIN_ID --keyring-backend test
$BINARY collect-gentxs

# Modify app.toml
sed -i '' 's/enable = false/enable = true/g' $HOME_BUDGETAPP/config/app.toml
sed -i '' 's/swagger = false/swagger = true/g' $HOME_BUDGETAPP/config/app.toml

# Modify parameters for the governance proposal
sed -i '' 's%"amount": "10000000"%"amount": "1"%g' $HOME_BUDGETAPP/config/genesis.json
sed -i '' 's%"quorum": "0.334000000000000000",%"quorum": "0.000000000000000001",%g' $HOME_BUDGETAPP/config/genesis.json
sed -i '' 's%"threshold": "0.500000000000000000",%"threshold": "0.000000000000000001",%g' $HOME_BUDGETAPP/config/genesis.json
sed -i '' 's%"voting_period": "172800s"%"voting_period": "30s"%g' $HOME_BUDGETAPP/config/genesis.json
# Check platform
platform='unknown'
unamestr=`uname`
if [ "$unamestr" = 'Linux' ]; then
platform='linux'
fi

if [ $platform = 'linux' ]; then
sed -i 's/enable = false/enable = true/g' $HOME_BUDGETAPP/config/app.toml
sed -i 's/swagger = false/swagger = true/g' $HOME_BUDGETAPP/config/app.toml
sed -i 's%"amount": "10000000"%"amount": "1"%g' $HOME_BUDGETAPP/config/genesis.json
sed -i 's%"quorum": "0.334000000000000000",%"quorum": "0.000000000000000001",%g' $HOME_BUDGETAPP/config/genesis.json
sed -i 's%"threshold": "0.500000000000000000",%"threshold": "0.000000000000000001",%g' $HOME_BUDGETAPP/config/genesis.json
sed -i 's%"voting_period": "172800s"%"voting_period": "30s"%g' $HOME_BUDGETAPP/config/genesis.json
else
sed -i '' 's/enable = false/enable = true/g' $HOME_BUDGETAPP/config/app.toml
sed -i '' 's/swagger = false/swagger = true/g' $HOME_BUDGETAPP/config/app.toml
sed -i '' 's%"amount": "10000000"%"amount": "1"%g' $HOME_BUDGETAPP/config/genesis.json
sed -i '' 's%"quorum": "0.334000000000000000",%"quorum": "0.000000000000000001",%g' $HOME_BUDGETAPP/config/genesis.json
sed -i '' 's%"threshold": "0.500000000000000000",%"threshold": "0.000000000000000001",%g' $HOME_BUDGETAPP/config/genesis.json
sed -i '' 's%"voting_period": "172800s"%"voting_period": "30s"%g' $HOME_BUDGETAPP/config/genesis.json
fi

# Start
$BINARY start
Expand Down
28 changes: 22 additions & 6 deletions scripts/localnet.sh
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,29 @@ echo "Creating and collecting gentx..."
$BINARY gentx validator 1000000000stake --home $CHAIN_DIR/$CHAIN_ID --chain-id $CHAIN_ID --keyring-backend test
$BINARY collect-gentxs --home $CHAIN_DIR/$CHAIN_ID

# Check platform
platform='unknown'
unamestr=`uname`
if [ "$unamestr" = 'Linux' ]; then
platform='linux'
fi

echo "Change settings in config.toml file..."
sed -i '' 's#"tcp://127.0.0.1:26657"#"tcp://0.0.0.0:'"$RPC_PORT"'"#g' $CHAIN_DIR/$CHAIN_ID/config/config.toml
sed -i '' 's/timeout_commit = "5s"/timeout_commit = "1s"/g' $CHAIN_DIR/$CHAIN_ID/config/config.toml
sed -i '' 's/timeout_propose = "3s"/timeout_propose = "1s"/g' $CHAIN_DIR/$CHAIN_ID/config/config.toml
sed -i '' 's/index_all_keys = false/index_all_keys = true/g' $CHAIN_DIR/$CHAIN_ID/config/config.toml
sed -i '' 's/enable = false/enable = true/g' $CHAIN_DIR/$CHAIN_ID/config/app.toml
sed -i '' 's/swagger = false/swagger = true/g' $CHAIN_DIR/$CHAIN_ID/config/app.toml
if [ $platform = 'linux' ]; then
sed -i 's#"tcp://127.0.0.1:26657"#"tcp://0.0.0.0:'"$RPC_PORT"'"#g' $CHAIN_DIR/$CHAIN_ID/config/config.toml
sed -i 's/timeout_commit = "5s"/timeout_commit = "1s"/g' $CHAIN_DIR/$CHAIN_ID/config/config.toml
sed -i 's/timeout_propose = "3s"/timeout_propose = "1s"/g' $CHAIN_DIR/$CHAIN_ID/config/config.toml
sed -i 's/index_all_keys = false/index_all_keys = true/g' $CHAIN_DIR/$CHAIN_ID/config/config.toml
sed -i 's/enable = false/enable = true/g' $CHAIN_DIR/$CHAIN_ID/config/app.toml
sed -i 's/swagger = false/swagger = true/g' $CHAIN_DIR/$CHAIN_ID/config/app.toml
else
sed -i '' 's#"tcp://127.0.0.1:26657"#"tcp://0.0.0.0:'"$RPC_PORT"'"#g' $CHAIN_DIR/$CHAIN_ID/config/config.toml
sed -i '' 's/timeout_commit = "5s"/timeout_commit = "1s"/g' $CHAIN_DIR/$CHAIN_ID/config/config.toml
sed -i '' 's/timeout_propose = "3s"/timeout_propose = "1s"/g' $CHAIN_DIR/$CHAIN_ID/config/config.toml
sed -i '' 's/index_all_keys = false/index_all_keys = true/g' $CHAIN_DIR/$CHAIN_ID/config/config.toml
sed -i '' 's/enable = false/enable = true/g' $CHAIN_DIR/$CHAIN_ID/config/app.toml
sed -i '' 's/swagger = false/swagger = true/g' $CHAIN_DIR/$CHAIN_ID/config/app.toml
fi

echo "Starting $CHAIN_ID in $CHAIN_DIR..."
echo "Log file is located at $CHAIN_DIR/$CHAIN_ID.log"
Expand Down
8 changes: 4 additions & 4 deletions x/budget/keeper/budget.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func (k Keeper) CollectBudgets(ctx sdk.Context) error {
for budgetSource, budgetsBySource := range budgetsBySourceMap {
budgetSourceAcc, err := sdk.AccAddressFromBech32(budgetSource)
if err != nil {
continue
return err
}
budgetSourceBalances := sdk.NewDecCoinsFromCoins(k.bankKeeper.GetAllBalances(ctx, budgetSourceAcc)...)
if budgetSourceBalances.IsZero() {
Expand All @@ -37,7 +37,7 @@ func (k Keeper) CollectBudgets(ctx sdk.Context) error {
for i, budget := range budgetsBySource.Budgets {
collectionAcc, err := sdk.AccAddressFromBech32(budget.CollectionAddress)
if err != nil {
continue
return err
}

collectionCoins, _ := budgetSourceBalances.MulDecTruncate(budget.Rate).TruncateDecimal()
Expand All @@ -51,7 +51,7 @@ func (k Keeper) CollectBudgets(ctx sdk.Context) error {
}

if err := k.bankKeeper.InputOutputCoins(ctx, inputs, outputs); err != nil {
continue
return err
}
for i, budget := range budgetsBySource.Budgets {
k.AddTotalCollectedCoins(ctx, budget.Name, budgetsBySource.CollectionCoins[i])
Expand Down Expand Up @@ -95,7 +95,7 @@ func (k Keeper) CollectibleBudgets(ctx sdk.Context) (budgets []types.Budget) {
if params.EpochBlocks > 0 && ctx.BlockHeight()%int64(params.EpochBlocks) == 0 {
for _, budget := range params.Budgets {
err := budget.Validate()
if err == nil && !budget.Expired(ctx.BlockTime()) {
if err == nil && budget.Collectible(ctx.BlockTime()) {
budgets = append(budgets, budget)
}
}
Expand Down
222 changes: 222 additions & 0 deletions x/budget/keeper/budget_test.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
package keeper_test

import (
"time"

sdk "github.com/cosmos/cosmos-sdk/types"
paramscutils "github.com/cosmos/cosmos-sdk/x/params/client/utils"
"github.com/cosmos/cosmos-sdk/x/params/types/proposal"
paramproposal "github.com/cosmos/cosmos-sdk/x/params/types/proposal"

"github.com/tendermint/budget/app"
"github.com/tendermint/budget/x/budget/types"
)

Expand Down Expand Up @@ -173,6 +179,222 @@ func (suite *KeeperTestSuite) TestBudgetExpiration() {
// TODO: not implemented
}

func (suite *KeeperTestSuite) TestBudgetChangeSituation() {
encCfg := app.MakeTestEncodingConfig()
params := suite.keeper.GetParams(suite.ctx)
suite.keeper.SetParams(suite.ctx, params)
height := 1
suite.ctx = suite.ctx.WithBlockTime(mustParseRFC3339("2021-08-01T00:00:00Z"))
suite.ctx = suite.ctx.WithBlockHeight(int64(height))

for _, tc := range []struct {
name string
proposal *proposal.ParameterChangeProposal
budgetCount int
collectibleBudgetCount int
govTime time.Time
nextBlockTime time.Time
expErr error
}{

{
"add budget 1",
testProposal(proposal.ParamChange{
Subspace: types.ModuleName,
Key: string(types.KeyBudgets),
Value: `[
{
"name": "gravity-dex-farming-20213Q-20313Q",
"rate": "0.500000000000000000",
"budget_source_address": "cosmos17xpfvakm2amg962yls6f84z3kell8c5lserqta",
"collection_address": "cosmos1228ryjucdpdv3t87rxle0ew76a56ulvnfst0hq0sscd3nafgjpqqkcxcky",
"start_time": "2021-09-01T00:00:00Z",
"end_time": "2031-09-30T00:00:00Z"
}
]`,
}),
1,
0,
mustParseRFC3339("2021-08-01T00:00:00Z"),
mustParseRFC3339("2021-08-01T00:00:00Z"),
nil,
},
{
"add budget 2",
testProposal(proposal.ParamChange{
Subspace: types.ModuleName,
Key: string(types.KeyBudgets),
Value: `[
{
"name": "gravity-dex-farming-20213Q-20313Q",
"rate": "0.500000000000000000",
"budget_source_address": "cosmos17xpfvakm2amg962yls6f84z3kell8c5lserqta",
"collection_address": "cosmos1228ryjucdpdv3t87rxle0ew76a56ulvnfst0hq0sscd3nafgjpqqkcxcky",
"start_time": "2021-09-01T00:00:00Z",
"end_time": "2031-09-30T00:00:00Z"
},
{
"name": "gravity-dex-farming-2",
"rate": "0.500000000000000000",
"budget_source_address": "cosmos17xpfvakm2amg962yls6f84z3kell8c5lserqta",
"collection_address": "cosmos1m63436cdxnu9ymyj02e7k3xljkn8klyf5ahqa75degq748xxkmksvtlp8n",
"start_time": "2021-09-01T00:00:00Z",
"end_time": "2021-09-30T00:00:00Z"
}
]`,
}),
2,
2,
mustParseRFC3339("2021-09-03T00:00:00Z"),
mustParseRFC3339("2021-09-03T00:00:00Z"),
nil,
},
{
"add budget 3 with invalid total rate case 1",
testProposal(proposal.ParamChange{
Subspace: types.ModuleName,
Key: string(types.KeyBudgets),
Value: `[
{
"name": "gravity-dex-farming-20213Q-20313Q",
"rate": "0.500000000000000000",
"budget_source_address": "cosmos17xpfvakm2amg962yls6f84z3kell8c5lserqta",
"collection_address": "cosmos1228ryjucdpdv3t87rxle0ew76a56ulvnfst0hq0sscd3nafgjpqqkcxcky",
"start_time": "2021-09-01T00:00:00Z",
"end_time": "2031-09-30T00:00:00Z"
},
{
"name": "gravity-dex-farming-2",
"rate": "0.500000000000000000",
"budget_source_address": "cosmos17xpfvakm2amg962yls6f84z3kell8c5lserqta",
"collection_address": "cosmos1m63436cdxnu9ymyj02e7k3xljkn8klyf5ahqa75degq748xxkmksvtlp8n",
"start_time": "2021-09-01T00:00:00Z",
"end_time": "2021-09-30T00:00:00Z"
},
{
"name": "gravity-dex-farming-3",
"rate": "0.500000000000000000",
"budget_source_address": "cosmos17xpfvakm2amg962yls6f84z3kell8c5lserqta",
"collection_address": "cosmos17avp6xs5c8ycqzy20yv99ccxwunu32e507kpm8ql5nfg47pzj9qqxhujxr",
"start_time": "2021-09-30T00:00:00Z",
"end_time": "2021-10-10T00:00:00Z"
}
]`,
}),
0,
0,
mustParseRFC3339("2021-09-29T00:00:00Z"),
mustParseRFC3339("2021-09-30T00:00:00Z"),
types.ErrInvalidTotalBudgetRate,
},
{
"add budget 3 with invalid total rate case 2",
testProposal(proposal.ParamChange{
Subspace: types.ModuleName,
Key: string(types.KeyBudgets),
Value: `[
{
"name": "gravity-dex-farming-20213Q-20313Q",
"rate": "0.500000000000000000",
"budget_source_address": "cosmos17xpfvakm2amg962yls6f84z3kell8c5lserqta",
"collection_address": "cosmos1228ryjucdpdv3t87rxle0ew76a56ulvnfst0hq0sscd3nafgjpqqkcxcky",
"start_time": "2021-09-01T00:00:00Z",
"end_time": "2031-09-30T00:00:00Z"
},
{
"name": "gravity-dex-farming-2",
"rate": "0.500000000000000000",
"budget_source_address": "cosmos17xpfvakm2amg962yls6f84z3kell8c5lserqta",
"collection_address": "cosmos1m63436cdxnu9ymyj02e7k3xljkn8klyf5ahqa75degq748xxkmksvtlp8n",
"start_time": "2021-09-01T00:00:00Z",
"end_time": "2021-09-30T00:00:00Z"
},
{
"name": "gravity-dex-farming-3",
"rate": "0.500000000000000000",
"budget_source_address": "cosmos17xpfvakm2amg962yls6f84z3kell8c5lserqta",
"collection_address": "cosmos17avp6xs5c8ycqzy20yv99ccxwunu32e507kpm8ql5nfg47pzj9qqxhujxr",
"start_time": "2021-09-30T00:00:00Z",
"end_time": "2021-10-10T00:00:00Z"
}
]`,
}),
0,
0,
mustParseRFC3339("2021-10-01T00:00:00Z"),
mustParseRFC3339("2021-10-01T00:00:00Z"),
types.ErrInvalidTotalBudgetRate,
},
{
"add budget 3",
testProposal(proposal.ParamChange{
Subspace: types.ModuleName,
Key: string(types.KeyBudgets),
Value: `[
{
"name": "gravity-dex-farming-20213Q-20313Q",
"rate": "0.500000000000000000",
"budget_source_address": "cosmos17xpfvakm2amg962yls6f84z3kell8c5lserqta",
"collection_address": "cosmos1228ryjucdpdv3t87rxle0ew76a56ulvnfst0hq0sscd3nafgjpqqkcxcky",
"start_time": "2021-09-01T00:00:00Z",
"end_time": "2031-09-30T00:00:00Z"
},
{
"name": "gravity-dex-farming-3",
"rate": "0.500000000000000000",
"budget_source_address": "cosmos17xpfvakm2amg962yls6f84z3kell8c5lserqta",
"collection_address": "cosmos17avp6xs5c8ycqzy20yv99ccxwunu32e507kpm8ql5nfg47pzj9qqxhujxr",
"start_time": "2021-09-30T00:00:00Z",
"end_time": "2021-10-10T00:00:00Z"
}
]`,
}),
2,
2,
mustParseRFC3339("2021-10-01T00:00:00Z"),
mustParseRFC3339("2021-10-01T00:00:00Z"),
nil,
},
} {
suite.Run(tc.name, func() {
proposalJson := paramscutils.ParamChangeProposalJSON{}
bz, err := tc.proposal.Marshal()
suite.Require().NoError(err)
err = encCfg.Amino.Unmarshal(bz, &proposalJson)
suite.Require().NoError(err)
proposal := paramproposal.NewParameterChangeProposal(
proposalJson.Title, proposalJson.Description, proposalJson.Changes.ToParamChanges(),
)
suite.Require().NoError(err)

// endblock gov paramchange ->(new block)-> beginblock budget -> mempool -> endblock gov paramchange ->(new block)-> ...
suite.ctx = suite.ctx.WithBlockTime(tc.govTime)
err = suite.govHandler(suite.ctx, proposal)
if tc.expErr != nil {
suite.Require().Error(err)
} else {
suite.Require().NoError(err)
params := suite.keeper.GetParams(suite.ctx)
suite.Require().Len(params.Budgets, tc.budgetCount)
for _, budget := range params.Budgets {
err := budget.Validate()
suite.Require().NoError(err)
}
// (new block)
height += 1
suite.ctx = suite.ctx.WithBlockHeight(int64(height))
suite.ctx = suite.ctx.WithBlockTime(tc.nextBlockTime)
budgets := suite.keeper.CollectibleBudgets(suite.ctx)
suite.Require().Len(budgets, tc.collectibleBudgetCount)

// BeginBlocker
err := suite.keeper.CollectBudgets(suite.ctx)
suite.Require().NoError(err)
}
})
}
}

func (suite *KeeperTestSuite) TestGetSetTotalCollectedCoins() {
collectedCoins := suite.keeper.GetTotalCollectedCoins(suite.ctx, "budget1")
suite.Require().Nil(collectedCoins)
Expand Down
Loading

0 comments on commit 62151eb

Please sign in to comment.