refactor(core,x/**): simplify core service api and embed environment in keepers (#20071)
This commit is contained in:
@@ -10,7 +10,7 @@ import (
|
||||
)
|
||||
|
||||
func (k Keeper) InitGenesis(ctx context.Context, data *types.GenesisState) error {
|
||||
currentTime := k.environment.HeaderService.GetHeaderInfo(ctx).Time
|
||||
currentTime := k.HeaderService.HeaderInfo(ctx).Time
|
||||
for _, cf := range data.ContinuousFund {
|
||||
// ignore expired ContinuousFunds
|
||||
if cf.Expiry != nil && cf.Expiry.Before(currentTime) {
|
||||
|
||||
@@ -11,7 +11,7 @@ import (
|
||||
)
|
||||
|
||||
func (suite *KeeperTestSuite) TestUnclaimedBudget() {
|
||||
startTime := suite.environment.HeaderService.GetHeaderInfo(suite.ctx).Time.Add(-70 * time.Second)
|
||||
startTime := suite.environment.HeaderService.HeaderInfo(suite.ctx).Time.Add(-70 * time.Second)
|
||||
period := time.Duration(60) * time.Second
|
||||
zeroCoin := sdk.NewCoin("foo", math.ZeroInt())
|
||||
nextClaimFrom := startTime.Add(period)
|
||||
|
||||
@@ -10,7 +10,6 @@ import (
|
||||
"cosmossdk.io/collections"
|
||||
"cosmossdk.io/core/appmodule"
|
||||
errorsmod "cosmossdk.io/errors"
|
||||
"cosmossdk.io/log"
|
||||
"cosmossdk.io/math"
|
||||
"cosmossdk.io/x/protocolpool/types"
|
||||
|
||||
@@ -20,7 +19,8 @@ import (
|
||||
)
|
||||
|
||||
type Keeper struct {
|
||||
environment appmodule.Environment
|
||||
appmodule.Environment
|
||||
|
||||
authKeeper types.AccountKeeper
|
||||
bankKeeper types.BankKeeper
|
||||
stakingKeeper types.StakingKeeper
|
||||
@@ -55,7 +55,7 @@ func NewKeeper(cdc codec.BinaryCodec, env appmodule.Environment, ak types.Accoun
|
||||
sb := collections.NewSchemaBuilder(env.KVStoreService)
|
||||
|
||||
keeper := Keeper{
|
||||
environment: env,
|
||||
Environment: env,
|
||||
authKeeper: ak,
|
||||
bankKeeper: bk,
|
||||
stakingKeeper: sk,
|
||||
@@ -82,11 +82,6 @@ func (k Keeper) GetAuthority() string {
|
||||
return k.authority
|
||||
}
|
||||
|
||||
// Logger returns a module-specific logger.
|
||||
func (k Keeper) Logger(ctx context.Context) log.Logger {
|
||||
return k.environment.Logger.With(log.ModuleKey, "x/"+types.ModuleName)
|
||||
}
|
||||
|
||||
// FundCommunityPool allows an account to directly fund the community fund pool.
|
||||
func (k Keeper) FundCommunityPool(ctx context.Context, amount sdk.Coins, sender sdk.AccAddress) error {
|
||||
return k.bankKeeper.SendCoinsFromAccountToModule(ctx, sender, types.ModuleName, amount)
|
||||
@@ -126,7 +121,7 @@ func (k Keeper) withdrawContinuousFund(ctx context.Context, recipientAddr string
|
||||
}
|
||||
return sdk.Coin{}, fmt.Errorf("get continuous fund failed for recipient: %s", recipientAddr)
|
||||
}
|
||||
if cf.Expiry != nil && cf.Expiry.Before(k.environment.HeaderService.GetHeaderInfo(ctx).Time) {
|
||||
if cf.Expiry != nil && cf.Expiry.Before(k.HeaderService.HeaderInfo(ctx).Time) {
|
||||
return sdk.Coin{}, fmt.Errorf("cannot withdraw continuous funds: continuous fund expired for recipient: %s", recipientAddr)
|
||||
}
|
||||
|
||||
@@ -193,7 +188,7 @@ func (k Keeper) SetToDistribute(ctx context.Context, amount sdk.Coins, addr stri
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
hasPermission, err := k.hasPermission(ctx, authAddr)
|
||||
hasPermission, err := k.hasPermission(authAddr)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -249,7 +244,7 @@ func (k Keeper) sendFundsToStreamModule(ctx context.Context, denom string, perce
|
||||
return nil
|
||||
}
|
||||
|
||||
func (k Keeper) hasPermission(ctx context.Context, addr []byte) (bool, error) {
|
||||
func (k Keeper) hasPermission(addr []byte) (bool, error) {
|
||||
authority := k.GetAuthority()
|
||||
authAcc, err := k.authKeeper.AddressCodec().StringToBytes(authority)
|
||||
if err != nil {
|
||||
@@ -368,7 +363,7 @@ func (k Keeper) getClaimableFunds(ctx context.Context, recipientAddr string) (am
|
||||
}
|
||||
}
|
||||
|
||||
currentTime := k.environment.HeaderService.GetHeaderInfo(ctx).Time
|
||||
currentTime := k.HeaderService.HeaderInfo(ctx).Time
|
||||
startTime := budget.StartTime
|
||||
|
||||
// Check if the start time is reached
|
||||
@@ -416,7 +411,7 @@ func (k Keeper) calculateClaimableFunds(ctx context.Context, recipient sdk.AccAd
|
||||
nextClaimFrom := budget.NextClaimFrom.Add(*budget.Period)
|
||||
budget.NextClaimFrom = &nextClaimFrom
|
||||
|
||||
k.Logger(ctx).Debug(fmt.Sprintf("Processing budget for recipient: %s. Amount: %s", budget.RecipientAddress, coinsToDistribute.String()))
|
||||
k.Logger.Debug(fmt.Sprintf("Processing budget for recipient: %s. Amount: %s", budget.RecipientAddress, coinsToDistribute.String()))
|
||||
|
||||
// Save the updated budget in the state
|
||||
if err := k.BudgetProposal.Set(ctx, recipient, budget); err != nil {
|
||||
@@ -435,7 +430,7 @@ func (k Keeper) validateAndUpdateBudgetProposal(ctx context.Context, bp types.Ms
|
||||
return nil, fmt.Errorf("invalid budget proposal: %w", err)
|
||||
}
|
||||
|
||||
currentTime := k.environment.HeaderService.GetHeaderInfo(ctx).Time
|
||||
currentTime := k.HeaderService.HeaderInfo(ctx).Time
|
||||
if bp.StartTime.IsZero() || bp.StartTime == nil {
|
||||
bp.StartTime = ¤tTime
|
||||
}
|
||||
@@ -478,7 +473,7 @@ func (k Keeper) validateContinuousFund(ctx context.Context, msg types.MsgCreateC
|
||||
}
|
||||
|
||||
// Validate expiry
|
||||
currentTime := k.environment.HeaderService.GetHeaderInfo(ctx).Time
|
||||
currentTime := k.HeaderService.HeaderInfo(ctx).Time
|
||||
if msg.Expiry != nil && msg.Expiry.Compare(currentTime) == -1 {
|
||||
return fmt.Errorf("expiry time cannot be less than the current block time")
|
||||
}
|
||||
|
||||
@@ -95,7 +95,7 @@ func (k MsgServer) CommunityPoolSpend(ctx context.Context, msg *types.MsgCommuni
|
||||
return nil, err
|
||||
}
|
||||
|
||||
k.Logger(ctx).Info("transferred from the community pool to recipient", "amount", msg.Amount.String(), "recipient", msg.Recipient)
|
||||
k.Logger.Info("transferred from the community pool to recipient", "amount", msg.Amount.String(), "recipient", msg.Recipient)
|
||||
|
||||
return &types.MsgCommunityPoolSpendResponse{}, nil
|
||||
}
|
||||
@@ -164,7 +164,7 @@ func (k MsgServer) WithdrawContinuousFund(ctx context.Context, msg *types.MsgWit
|
||||
return nil, err
|
||||
}
|
||||
if amount.IsNil() {
|
||||
k.Logger(ctx).Info(fmt.Sprintf("no distribution amount found for recipient %s", msg.RecipientAddress))
|
||||
k.Logger.Info(fmt.Sprintf("no distribution amount found for recipient %s", msg.RecipientAddress))
|
||||
}
|
||||
|
||||
return &types.MsgWithdrawContinuousFundResponse{Amount: amount}, nil
|
||||
@@ -180,8 +180,8 @@ func (k MsgServer) CancelContinuousFund(ctx context.Context, msg *types.MsgCance
|
||||
return nil, err
|
||||
}
|
||||
|
||||
canceledHeight := k.environment.HeaderService.GetHeaderInfo(ctx).Height
|
||||
canceledTime := k.environment.HeaderService.GetHeaderInfo(ctx).Time
|
||||
canceledHeight := k.HeaderService.HeaderInfo(ctx).Height
|
||||
canceledTime := k.HeaderService.HeaderInfo(ctx).Time
|
||||
|
||||
found, err := k.ContinuousFund.Has(ctx, recipient)
|
||||
if !found {
|
||||
|
||||
@@ -21,8 +21,8 @@ var (
|
||||
|
||||
func (suite *KeeperTestSuite) TestMsgSubmitBudgetProposal() {
|
||||
invalidCoin := sdk.NewInt64Coin("foo", 0)
|
||||
startTime := suite.environment.HeaderService.GetHeaderInfo(suite.ctx).Time.Add(10 * time.Second)
|
||||
invalidStartTime := suite.environment.HeaderService.GetHeaderInfo(suite.ctx).Time.Add(-15 * time.Second)
|
||||
startTime := suite.environment.HeaderService.HeaderInfo(suite.ctx).Time.Add(10 * time.Second)
|
||||
invalidStartTime := suite.environment.HeaderService.HeaderInfo(suite.ctx).Time.Add(-15 * time.Second)
|
||||
period := time.Duration(60) * time.Second
|
||||
zeroPeriod := time.Duration(0) * time.Second
|
||||
recipientStrAddr, err := codectestutil.CodecOptions{}.GetAddressCodec().BytesToString(recipientAddr)
|
||||
@@ -145,7 +145,7 @@ func (suite *KeeperTestSuite) TestMsgSubmitBudgetProposal() {
|
||||
}
|
||||
|
||||
func (suite *KeeperTestSuite) TestMsgClaimBudget() {
|
||||
startTime := suite.environment.HeaderService.GetHeaderInfo(suite.ctx).Time.Add(-70 * time.Second)
|
||||
startTime := suite.environment.HeaderService.HeaderInfo(suite.ctx).Time.Add(-70 * time.Second)
|
||||
period := time.Duration(60) * time.Second
|
||||
recipientStrAddr, err := codectestutil.CodecOptions{}.GetAddressCodec().BytesToString(recipientAddr)
|
||||
suite.Require().NoError(err)
|
||||
@@ -169,7 +169,7 @@ func (suite *KeeperTestSuite) TestMsgClaimBudget() {
|
||||
},
|
||||
"claiming before start time": {
|
||||
preRun: func() {
|
||||
startTime := suite.environment.HeaderService.GetHeaderInfo(suite.ctx).Time.Add(3600 * time.Second)
|
||||
startTime := suite.environment.HeaderService.HeaderInfo(suite.ctx).Time.Add(3600 * time.Second)
|
||||
// Prepare the budget proposal with a future start time
|
||||
budget := types.Budget{
|
||||
RecipientAddress: recipientStrAddr,
|
||||
@@ -187,7 +187,7 @@ func (suite *KeeperTestSuite) TestMsgClaimBudget() {
|
||||
},
|
||||
"budget period has not passed": {
|
||||
preRun: func() {
|
||||
startTime := suite.environment.HeaderService.GetHeaderInfo(suite.ctx).Time.Add(-50 * time.Second)
|
||||
startTime := suite.environment.HeaderService.HeaderInfo(suite.ctx).Time.Add(-50 * time.Second)
|
||||
// Prepare the budget proposal with start time and a short period
|
||||
budget := types.Budget{
|
||||
RecipientAddress: recipientStrAddr,
|
||||
@@ -248,7 +248,7 @@ func (suite *KeeperTestSuite) TestMsgClaimBudget() {
|
||||
"valid double claim attempt": {
|
||||
preRun: func() {
|
||||
oneMonthInSeconds := int64(30 * 24 * 60 * 60) // Approximate number of seconds in 1 month
|
||||
startTimeBeforeMonth := suite.environment.HeaderService.GetHeaderInfo(suite.ctx).Time.Add(time.Duration(-oneMonthInSeconds) * time.Second)
|
||||
startTimeBeforeMonth := suite.environment.HeaderService.HeaderInfo(suite.ctx).Time.Add(time.Duration(-oneMonthInSeconds) * time.Second)
|
||||
oneMonthPeriod := time.Duration(oneMonthInSeconds) * time.Second
|
||||
// Prepare the budget proposal with valid start time and period of 1 month (in seconds)
|
||||
budget := types.Budget{
|
||||
@@ -270,7 +270,7 @@ func (suite *KeeperTestSuite) TestMsgClaimBudget() {
|
||||
suite.Require().NoError(err)
|
||||
|
||||
// Create a new context with an updated block time to simulate a delay
|
||||
newBlockTime := suite.environment.HeaderService.GetHeaderInfo(suite.ctx).Time.Add(time.Duration(oneMonthInSeconds) * time.Second)
|
||||
newBlockTime := suite.environment.HeaderService.HeaderInfo(suite.ctx).Time.Add(time.Duration(oneMonthInSeconds) * time.Second)
|
||||
suite.ctx = suite.ctx.WithHeaderInfo(header.Info{
|
||||
Time: newBlockTime,
|
||||
})
|
||||
@@ -301,7 +301,7 @@ func (suite *KeeperTestSuite) TestMsgClaimBudget() {
|
||||
suite.Require().NoError(err)
|
||||
|
||||
// Create a new context with an updated block time to simulate a delay
|
||||
newBlockTime := suite.environment.HeaderService.GetHeaderInfo(suite.ctx).Time.Add(60 * time.Second)
|
||||
newBlockTime := suite.environment.HeaderService.HeaderInfo(suite.ctx).Time.Add(60 * time.Second)
|
||||
suite.ctx = suite.ctx.WithHeaderInfo(header.Info{
|
||||
Time: newBlockTime,
|
||||
})
|
||||
@@ -378,7 +378,7 @@ func (suite *KeeperTestSuite) TestWithdrawContinuousFund() {
|
||||
percentage, err := math.LegacyNewDecFromStr("0.2")
|
||||
suite.Require().NoError(err)
|
||||
oneMonthInSeconds := int64(30 * 24 * 60 * 60) // Approximate number of seconds in 1 month
|
||||
expiry := suite.environment.HeaderService.GetHeaderInfo(suite.ctx).Time.Add(time.Duration(oneMonthInSeconds) * time.Second)
|
||||
expiry := suite.environment.HeaderService.HeaderInfo(suite.ctx).Time.Add(time.Duration(oneMonthInSeconds) * time.Second)
|
||||
cf := types.ContinuousFund{
|
||||
Recipient: recipientStrAddr,
|
||||
Percentage: percentage,
|
||||
@@ -424,7 +424,7 @@ func (suite *KeeperTestSuite) TestWithdrawContinuousFund() {
|
||||
preRun: func() {
|
||||
percentage, err := math.LegacyNewDecFromStr("0.2")
|
||||
suite.Require().NoError(err)
|
||||
expiry := suite.environment.HeaderService.GetHeaderInfo(suite.ctx).Time.Add(time.Duration(-1) * time.Second)
|
||||
expiry := suite.environment.HeaderService.HeaderInfo(suite.ctx).Time.Add(time.Duration(-1) * time.Second)
|
||||
cf := types.ContinuousFund{
|
||||
Recipient: recipientStrAddr,
|
||||
Percentage: percentage,
|
||||
@@ -443,7 +443,7 @@ func (suite *KeeperTestSuite) TestWithdrawContinuousFund() {
|
||||
percentage, err := math.LegacyNewDecFromStr("0.2")
|
||||
suite.Require().NoError(err)
|
||||
oneMonthInSeconds := int64(30 * 24 * 60 * 60) // Approximate number of seconds in 1 month
|
||||
expiry := suite.environment.HeaderService.GetHeaderInfo(suite.ctx).Time.Add(time.Duration(oneMonthInSeconds) * time.Second)
|
||||
expiry := suite.environment.HeaderService.HeaderInfo(suite.ctx).Time.Add(time.Duration(oneMonthInSeconds) * time.Second)
|
||||
cf := types.ContinuousFund{
|
||||
Recipient: recipientStrAddr,
|
||||
Percentage: percentage,
|
||||
@@ -496,7 +496,7 @@ func (suite *KeeperTestSuite) TestWithdrawContinuousFund() {
|
||||
percentage, err := math.LegacyNewDecFromStr("0.2")
|
||||
suite.Require().NoError(err)
|
||||
oneMonthInSeconds := int64(30 * 24 * 60 * 60) // Approximate number of seconds in 1 month
|
||||
expiry := suite.environment.HeaderService.GetHeaderInfo(suite.ctx).Time.Add(time.Duration(oneMonthInSeconds) * time.Second)
|
||||
expiry := suite.environment.HeaderService.HeaderInfo(suite.ctx).Time.Add(time.Duration(oneMonthInSeconds) * time.Second)
|
||||
cf := types.ContinuousFund{
|
||||
Recipient: recipientStrAddr,
|
||||
Percentage: percentage,
|
||||
@@ -526,7 +526,7 @@ func (suite *KeeperTestSuite) TestWithdrawContinuousFund() {
|
||||
percentage, err := math.LegacyNewDecFromStr("0.3")
|
||||
suite.Require().NoError(err)
|
||||
oneMonthInSeconds := int64(30 * 24 * 60 * 60) // Approximate number of seconds in 1 month
|
||||
expiry := suite.environment.HeaderService.GetHeaderInfo(suite.ctx).Time.Add(time.Duration(oneMonthInSeconds) * time.Second)
|
||||
expiry := suite.environment.HeaderService.HeaderInfo(suite.ctx).Time.Add(time.Duration(oneMonthInSeconds) * time.Second)
|
||||
cf := types.ContinuousFund{
|
||||
Recipient: recipientStrAddr,
|
||||
Percentage: percentage,
|
||||
@@ -630,9 +630,9 @@ func (suite *KeeperTestSuite) TestCreateContinuousFund() {
|
||||
suite.Require().NoError(err)
|
||||
negativePercentage, err := math.LegacyNewDecFromStr("-0.2")
|
||||
suite.Require().NoError(err)
|
||||
invalidExpirty := suite.environment.HeaderService.GetHeaderInfo(suite.ctx).Time.Add(-15 * time.Second)
|
||||
invalidExpirty := suite.environment.HeaderService.HeaderInfo(suite.ctx).Time.Add(-15 * time.Second)
|
||||
oneMonthInSeconds := int64(30 * 24 * 60 * 60) // Approximate number of seconds in 1 month
|
||||
expiry := suite.environment.HeaderService.GetHeaderInfo(suite.ctx).Time.Add(time.Duration(oneMonthInSeconds) * time.Second)
|
||||
expiry := suite.environment.HeaderService.HeaderInfo(suite.ctx).Time.Add(time.Duration(oneMonthInSeconds) * time.Second)
|
||||
recipientStrAddr, err := codectestutil.CodecOptions{}.GetAddressCodec().BytesToString(recipientAddr)
|
||||
suite.Require().NoError(err)
|
||||
testCases := map[string]struct {
|
||||
@@ -790,7 +790,7 @@ func (suite *KeeperTestSuite) TestCancelContinuousFund() {
|
||||
percentage, err := math.LegacyNewDecFromStr("0.2")
|
||||
suite.Require().NoError(err)
|
||||
oneMonthInSeconds := int64(30 * 24 * 60 * 60) // Approximate number of seconds in 1 month
|
||||
expiry := suite.environment.HeaderService.GetHeaderInfo(suite.ctx).Time.Add(time.Duration(oneMonthInSeconds) * time.Second)
|
||||
expiry := suite.environment.HeaderService.HeaderInfo(suite.ctx).Time.Add(time.Duration(oneMonthInSeconds) * time.Second)
|
||||
cf := types.ContinuousFund{
|
||||
Recipient: "",
|
||||
Percentage: percentage,
|
||||
@@ -813,7 +813,7 @@ func (suite *KeeperTestSuite) TestCancelContinuousFund() {
|
||||
percentage, err := math.LegacyNewDecFromStr("0.2")
|
||||
suite.Require().NoError(err)
|
||||
oneMonthInSeconds := int64(30 * 24 * 60 * 60) // Approximate number of seconds in 1 month
|
||||
expiry := suite.environment.HeaderService.GetHeaderInfo(suite.ctx).Time.Add(time.Duration(oneMonthInSeconds) * time.Second)
|
||||
expiry := suite.environment.HeaderService.HeaderInfo(suite.ctx).Time.Add(time.Duration(oneMonthInSeconds) * time.Second)
|
||||
cf := types.ContinuousFund{
|
||||
Recipient: recipientStrAddr,
|
||||
Percentage: percentage,
|
||||
@@ -873,7 +873,7 @@ func (suite *KeeperTestSuite) TestCancelContinuousFund() {
|
||||
percentage, err := math.LegacyNewDecFromStr("0.2")
|
||||
suite.Require().NoError(err)
|
||||
oneMonthInSeconds := int64(30 * 24 * 60 * 60) // Approximate number of seconds in 1 month
|
||||
expiry := suite.environment.HeaderService.GetHeaderInfo(suite.ctx).Time.Add(time.Duration(oneMonthInSeconds) * time.Second)
|
||||
expiry := suite.environment.HeaderService.HeaderInfo(suite.ctx).Time.Add(time.Duration(oneMonthInSeconds) * time.Second)
|
||||
cf := types.ContinuousFund{
|
||||
Recipient: recipientStrAddr,
|
||||
Percentage: percentage,
|
||||
|
||||
Reference in New Issue
Block a user