Skip to content

Commit

Permalink
Merge commit '1e6f94641f22d89210c5de7e92f6b268514751cd' into kogisin/…
Browse files Browse the repository at this point in the history
…96-fix-epoch-params

# Conflicts:
#	proto/tendermint/farming/v1beta1/genesis.proto
#	x/farming/client/cli/query.go
#	x/farming/keeper/epoch_test.go
#	x/farming/keeper/genesis.go
#	x/farming/keeper/keeper_test.go
#	x/farming/types/farming.pb.go
#	x/farming/types/genesis.go
#	x/farming/types/genesis.pb.go
  • Loading branch information
jaybxyz committed Sep 16, 2021
2 parents e9b77aa + 1e6f946 commit 3760c11
Show file tree
Hide file tree
Showing 25 changed files with 1,481 additions and 304 deletions.
6 changes: 6 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,10 @@ endif
ldflags += $(LDFLAGS)
ldflags := $(strip $(ldflags))

testing_ldflags = -X github.com/tendermint/farming/x/farming/keeper.enableAdvanceEpoch=true

BUILD_FLAGS := -tags "$(build_tags)" -ldflags '$(ldflags)'
TESTING_BUILD_FLAGS := -tags "$(build_tags)" -ldflags '$(ldflags) $(testing_ldflags)'

all: tools install lint

Expand All @@ -87,6 +90,9 @@ build-linux: go.sum
install: go.sum
go install $(BUILD_FLAGS) ./cmd/farmingd

install-testing: go.sum
go install $(TESTING_BUILD_FLAGS) ./cmd/farmingd

build-reproducible: go.sum
$(DOCKER) rm latest-build || true
$(DOCKER) run --volume=$(CURDIR):/sources:ro \
Expand Down
7 changes: 7 additions & 0 deletions proto/tendermint/farming/v1beta1/farming.proto
Original file line number Diff line number Diff line change
Expand Up @@ -164,4 +164,11 @@ message HistoricalRewards {
(gogoproto.castrepeated) = "github.com/cosmos/cosmos-sdk/types.DecCoins",
(gogoproto.nullable) = false
];
}

// OutstandingRewards represents outstanding(un-withdrawn) rewards
// for a staking coin denom.
message OutstandingRewards {
repeated cosmos.base.v1beta1.DecCoin rewards = 1
[(gogoproto.castrepeated) = "github.com/cosmos/cosmos-sdk/types.DecCoins", (gogoproto.nullable) = false];
}
30 changes: 20 additions & 10 deletions proto/tendermint/farming/v1beta1/genesis.proto
Original file line number Diff line number Diff line change
Expand Up @@ -32,34 +32,34 @@ message GenesisState {
repeated HistoricalRewardsRecord historical_rewards_records = 5
[(gogoproto.nullable) = false, (gogoproto.moretags) = "yaml:\"historical_rewards_records\""];

repeated CurrentEpochRecord current_epoch_records = 6
repeated OutstandingRewardsRecord outstanding_rewards_records = 6
[(gogoproto.nullable) = false, (gogoproto.moretags) = "yaml:\"outstanding_rewards_records\""];

repeated CurrentEpochRecord current_epoch_records = 7
[(gogoproto.nullable) = false, (gogoproto.moretags) = "yaml:\"current_epoch_records\""];

// staking_reserve_coins specifies balance of the staking reserve pool staked in the plans
// this param is needed for import/export validation
repeated cosmos.base.v1beta1.Coin staking_reserve_coins = 7 [
repeated cosmos.base.v1beta1.Coin staking_reserve_coins = 8 [
(gogoproto.moretags) = "yaml:\"staking_reserve_coins\"",
(gogoproto.castrepeated) = "github.com/cosmos/cosmos-sdk/types.Coins",
(gogoproto.nullable) = false
];

// reward_pool_coins specifies balance of the reward pool to be distributed in the plans
// this param is needed for import/export validation
repeated cosmos.base.v1beta1.Coin reward_pool_coins = 8 [
repeated cosmos.base.v1beta1.Coin reward_pool_coins = 9 [
(gogoproto.moretags) = "yaml:\"reward_pool_coins\"",
(gogoproto.castrepeated) = "github.com/cosmos/cosmos-sdk/types.Coins",
(gogoproto.nullable) = false
];

// global_last_epoch_time specifies the last executed epoch time of the plans
google.protobuf.Timestamp global_last_epoch_time = 9 [
(gogoproto.stdtime) = true,
(gogoproto.nullable) = false,
(gogoproto.moretags) = "yaml:\"global_last_epoch_time\""
];
// last_epoch_time specifies the last executed epoch time of the plans
google.protobuf.Timestamp last_epoch_time = 10
[(gogoproto.stdtime) = true, (gogoproto.moretags) = "yaml:\"last_epoch_time\""];

// current_epoch_days specifies the epoch used when allocating farming rewards in end blocker
uint32 current_epoch_days = 10;
uint32 current_epoch_days = 11;
}

// PlanRecord is used for import/export via genesis json.
Expand Down Expand Up @@ -121,6 +121,16 @@ message HistoricalRewardsRecord {
[(gogoproto.nullable) = false, (gogoproto.moretags) = "yaml:\"historical_rewards\""];
}

message OutstandingRewardsRecord {
option (gogoproto.equal) = false;
option (gogoproto.goproto_getters) = false;

string staking_coin_denom = 1 [(gogoproto.moretags) = "yaml:\"staking_coin_denom\""];

OutstandingRewards outstanding_rewards = 2
[(gogoproto.nullable) = false, (gogoproto.moretags) = "yaml:\"outstanding_rewards\""];
}

message CurrentEpochRecord {
option (gogoproto.equal) = false;
option (gogoproto.goproto_getters) = false;
Expand Down
17 changes: 16 additions & 1 deletion proto/tendermint/farming/v1beta1/tx.proto
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,12 @@ service Msg {
// Unstake defines a method for unstaking coins from the farming plan
rpc Unstake(MsgUnstake) returns (MsgUnstakeResponse);

// harvest defines a method for claiming farming rewards
// Harvest defines a method for claiming farming rewards
rpc Harvest(MsgHarvest) returns (MsgHarvestResponse);

// AdvanceEpoch defines a method for advancing epoch by one, just for testing purpose
// and shouldn't be used in real world
rpc AdvanceEpoch(MsgAdvanceEpoch) returns (MsgAdvanceEpochResponse);
}

// MsgCreateFixedAmountPlan defines a SDK message for creating a new fixed
Expand Down Expand Up @@ -156,3 +160,14 @@ message MsgHarvest {

// MsgHarvestResponse defines the Msg/MsgHarvestResponse response type.
message MsgHarvestResponse {}

// MsgAdvanceEpoch defines a message to advance epoch by one.
message MsgAdvanceEpoch {
option (gogoproto.goproto_getters) = false;

// requester defines the bech32-encoded address of the requester
string requester = 1;
}

// MsgAdvanceEpochResponse defines the Msg/AdvanceEpoch response type.
message MsgAdvanceEpochResponse {}
2 changes: 1 addition & 1 deletion x/farming/abci.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func EndBlocker(ctx sdk.Context, k keeper.Keeper) {
} else {
y, m, d := lastEpochTime.AddDate(0, 0, int(currentEpochDays)).Date()
y2, m2, d2 := ctx.BlockTime().Date()
if y == y2 && m == m2 && d == d2 {
if !time.Date(y2, m2, d2, 0, 0, 0, 0, time.UTC).Before(time.Date(y, m, d, 0, 0, 0, 0, time.UTC)) {
if err := k.AdvanceEpoch(ctx); err != nil {
panic(err)
}
Expand Down
4 changes: 2 additions & 2 deletions x/farming/client/cli/cli_test.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
// +build norace

package cli_test

import (
Expand All @@ -22,6 +20,8 @@ import (
farmingtypes "github.com/tendermint/farming/x/farming/types"
)

// // +build norace

type IntegrationTestSuite struct {
suite.Suite

Expand Down
5 changes: 5 additions & 0 deletions x/farming/client/cli/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,9 @@ func GetCmdQueryStakings() *cobra.Command {
Short: "Query stakings by a farmer",
Long: strings.TrimSpace(
fmt.Sprintf(`Query all stakings by a farmer.
Optionally restrict coins for a staking coin denom.
Example:
$ %s query %s stakings %s1gghjut3ccd8ay0zduzj64hwre2fxs9ldmqhffj
$ %s query %s stakings %s1gghjut3ccd8ay0zduzj64hwre2fxs9ldmqhffj --staking-coin-denom poolD35A0CC16EE598F90B044CE296A405BA9C381E38837599D96F2F70C2F02A23A4
Expand Down Expand Up @@ -247,6 +249,7 @@ func GetCmdQueryTotalStakings() *cobra.Command {
Short: "Query total staking amount for a staking coin denom",
Long: strings.TrimSpace(
fmt.Sprintf(`Query total staking amount for a staking coin denom.
Example:
$ %s query %s total-stakings poolD35A0CC16EE598F90B044CE296A405BA9C381E38837599D96F2F70C2F02A23A4
`,
Expand Down Expand Up @@ -290,7 +293,9 @@ func GetCmdQueryRewards() *cobra.Command {
Short: "Query rewards for a farmer",
Long: strings.TrimSpace(
fmt.Sprintf(`Query all rewards for a farmer.
Optionally restrict rewards for a staking coin denom.
Example:
$ %s query %s rewards %s1gghjut3ccd8ay0zduzj64hwre2fxs9ldmqhffj
$ %s query %s rewards %s1gghjut3ccd8ay0zduzj64hwre2fxs9ldmqhffj --staking-coin-denom poolD35A0CC16EE598F90B044CE296A405BA9C381E38837599D96F2F70C2F02A23A4
Expand Down
28 changes: 28 additions & 0 deletions x/farming/client/cli/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"github.com/cosmos/cosmos-sdk/x/gov/client/cli"
gov "github.com/cosmos/cosmos-sdk/x/gov/types"

"github.com/tendermint/farming/x/farming/keeper"
"github.com/tendermint/farming/x/farming/types"
)

Expand All @@ -38,6 +39,9 @@ func GetTxCmd() *cobra.Command {
NewUnstakeCmd(),
NewHarvestCmd(),
)
if keeper.EnableAdvanceEpoch {
farmingTxCmd.AddCommand(NewAdvanceEpochCmd())
}

return farmingTxCmd
}
Expand Down Expand Up @@ -302,6 +306,30 @@ $ %s tx %s harvest --staking-coin-denoms="poolD35A0CC16EE598F90B044CE296A405BA9C
return cmd
}

func NewAdvanceEpochCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "advance-epoch",
Args: cobra.NoArgs,
Short: "advance epoch by one to simulate reward distribution",
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx, err := client.GetClientTxContext(cmd)
if err != nil {
return err
}

requesterAcc := clientCtx.GetFromAddress()

msg := types.NewMsgAdvanceEpoch(requesterAcc)

return tx.GenerateOrBroadcastTxCLI(clientCtx, cmd.Flags(), msg)
},
}

flags.AddTxFlagsToCmd(cmd)

return cmd
}

// GetCmdSubmitPublicPlanProposal implements a command handler for submitting a public farming plan transaction to create, update, and delete plan.
func GetCmdSubmitPublicPlanProposal() *cobra.Command {
cmd := &cobra.Command{
Expand Down
3 changes: 2 additions & 1 deletion x/farming/client/testutil/cli_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package testutil
import (
"fmt"

dbm "github.com/tendermint/tm-db"

"github.com/cosmos/cosmos-sdk/baseapp"
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/flags"
Expand All @@ -17,7 +19,6 @@ import (

farmingapp "github.com/tendermint/farming/app"
farmingcli "github.com/tendermint/farming/x/farming/client/cli"
dbm "github.com/tendermint/tm-db"
)

// NewConfig returns config that defines the necessary testing requirements
Expand Down
13 changes: 13 additions & 0 deletions x/farming/keeper/epoch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,19 @@ func (suite *KeeperTestSuite) TestEpochDays() {
})
}
}
func (suite *KeeperTestSuite) TestDelayedBlockTime() {
// Entire network can be down for several days,
// and the epoch should be advanced after the downtime.
suite.keeper.SetLastEpochTime(suite.ctx, types.ParseTime("2021-09-23T00:00:05Z"))

t := types.ParseTime("2021-10-03T00:00:04Z")
suite.ctx = suite.ctx.WithBlockTime(t)
farming.EndBlocker(suite.ctx, suite.keeper)

lastEpochTime, found := suite.keeper.GetLastEpochTime(suite.ctx)
suite.Require().True(found)
suite.Require().Equal(t, lastEpochTime)
}

func (suite *KeeperTestSuite) TestCurrentEpochDays() {
currentEpochDays := suite.keeper.GetCurrentEpochDays(suite.ctx)
Expand Down
Loading

0 comments on commit 3760c11

Please sign in to comment.