-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* replace NBSP with space * fix core logic - store only sdk.Coins when storing a reward in kvstore - fix store methods - delete a staking when all coins has been unstaked and no rewards left - emit events in keeper method rather than in msg handler * revert back parameter order of store methods * revert back old Reward struct * rename and simplify Denoms method * drop legacy amino dependency and replace simd with farmingd * implement Stakings query endpoint and minor fixes - rename key getter methods - unify variable/parameter namings * fix Unstake logic * make tx commands work * change time format * move plan-related logics into plan.go * fix Rewards endpoint logic and minor bugs * use sdk.BigEndianToUint64 instead of encoding/binary * fix query endpoint bugs * improve stake/unstake cli commands * use test suite for testing * add tests and update sentinel errors * make logics executed in EndBlock not BeginBlock * implement epoch-related functions * implement ProcessQueuedCoins and add test helper * add and refactor tests * implement reward distribution logic also make Create*Plan to return the plan * apply PR review suggestions - remove code for debugging purpose - accumulate reward coins, not overwrite - only update staking object when there is a change
- Loading branch information
1 parent
5ff55e5
commit e06ecb9
Showing
18 changed files
with
690 additions
and
113 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package keeper | ||
|
||
import ( | ||
"time" | ||
|
||
gogotypes "github.com/gogo/protobuf/types" | ||
|
||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
|
||
"github.com/tendermint/farming/x/farming/types" | ||
) | ||
|
||
func (k Keeper) GetLastEpochTime(ctx sdk.Context) (time.Time, bool) { | ||
store := ctx.KVStore(k.storeKey) | ||
bz := store.Get(types.GlobalLastEpochTimeKey) | ||
if bz == nil { | ||
return time.Time{}, false | ||
} | ||
var ts gogotypes.Timestamp | ||
k.cdc.MustUnmarshal(bz, &ts) | ||
t, err := gogotypes.TimestampFromProto(&ts) | ||
if err != nil { | ||
panic(err) | ||
} | ||
return t, true | ||
} | ||
|
||
func (k Keeper) SetLastEpochTime(ctx sdk.Context, t time.Time) { | ||
store := ctx.KVStore(k.storeKey) | ||
ts, err := gogotypes.TimestampProto(t) | ||
if err != nil { | ||
panic(err) | ||
} | ||
bz := k.cdc.MustMarshal(ts) | ||
store.Set(types.GlobalLastEpochTimeKey, bz) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package keeper_test | ||
|
||
func (suite *KeeperTestSuite) TestLastEpochTime() { | ||
_, found := suite.keeper.GetLastEpochTime(suite.ctx) | ||
suite.Require().False(found) | ||
|
||
t := mustParseRFC3339("2021-07-23T05:01:02Z") | ||
suite.keeper.SetLastEpochTime(suite.ctx, t) | ||
|
||
t2, found := suite.keeper.GetLastEpochTime(suite.ctx) | ||
suite.Require().True(found) | ||
suite.Require().Equal(t, t2) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,80 @@ | ||
package keeper_test | ||
|
||
import ( | ||
"testing" | ||
"time" | ||
|
||
"github.com/stretchr/testify/suite" | ||
tmproto "github.com/tendermint/tendermint/proto/tendermint/types" | ||
|
||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
|
||
"github.com/tendermint/farming/app" | ||
simapp "github.com/tendermint/farming/app" | ||
"github.com/tendermint/farming/x/farming/keeper" | ||
"github.com/tendermint/farming/x/farming/types" | ||
) | ||
|
||
// createTestApp returns a farming app with custom FarmingKeeper. | ||
func createTestApp(isCheckTx bool) (*app.FarmingApp, sdk.Context) { | ||
app := app.Setup(isCheckTx) | ||
ctx := app.BaseApp.NewContext(isCheckTx, tmproto.Header{}) | ||
app.FarmingKeeper.SetParams(ctx, types.DefaultParams()) | ||
const ( | ||
denom1 = "denom1" | ||
denom2 = "denom2" | ||
denom3 = "denom3" | ||
) | ||
|
||
var ( | ||
initialBalances = sdk.NewCoins( | ||
sdk.NewInt64Coin(sdk.DefaultBondDenom, 1_000_000_000), | ||
sdk.NewInt64Coin(denom1, 1_000_000_000), | ||
sdk.NewInt64Coin(denom2, 1_000_000_000), | ||
sdk.NewInt64Coin(denom3, 1_000_000_000)) | ||
) | ||
|
||
type KeeperTestSuite struct { | ||
suite.Suite | ||
|
||
app *simapp.FarmingApp | ||
ctx sdk.Context | ||
keeper keeper.Keeper | ||
addrs []sdk.AccAddress | ||
} | ||
|
||
func TestKeeperTestSuite(t *testing.T) { | ||
suite.Run(t, new(KeeperTestSuite)) | ||
} | ||
|
||
func (suite *KeeperTestSuite) SetupTest() { | ||
app := simapp.Setup(false) | ||
ctx := app.BaseApp.NewContext(false, tmproto.Header{}) | ||
|
||
suite.app = app | ||
suite.ctx = ctx | ||
suite.keeper = suite.app.FarmingKeeper | ||
suite.addrs = simapp.AddTestAddrs(suite.app, suite.ctx, 4, sdk.ZeroInt()) | ||
for _, addr := range suite.addrs { | ||
err := simapp.FundAccount(suite.app.BankKeeper, suite.ctx, addr, initialBalances) | ||
suite.Require().NoError(err) | ||
} | ||
} | ||
|
||
// Stake is a convenient method to test Keeper.Stake. | ||
func (suite *KeeperTestSuite) Stake(farmer sdk.AccAddress, amt sdk.Coins) types.Staking { | ||
staking, err := suite.keeper.Stake(suite.ctx, farmer, amt) | ||
suite.Require().NoError(err) | ||
|
||
return staking | ||
} | ||
|
||
func intEq(exp, got sdk.Int) (bool, string, string, string) { | ||
return exp.Equal(got), "expected:\t%v\ngot:\t\t%v", exp.String(), got.String() | ||
} | ||
|
||
func coinsEq(exp, got sdk.Coins) (bool, string, string, string) { | ||
return exp.IsEqual(got), "expected:\t%v\ngot:\t\t%v", exp.String(), got.String() | ||
} | ||
|
||
return app, ctx | ||
func mustParseRFC3339(s string) time.Time { | ||
t, err := time.Parse(time.RFC3339, s) | ||
if err != nil { | ||
panic(err) | ||
} | ||
return t | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.