Skip to content

Commit

Permalink
chore: address generation of the empty coins in x/foundation (#952)
Browse files Browse the repository at this point in the history
* Address generation of the empty coins in x/foundation

* Update CHANGELOG.md

* Update unit test

* Update the test

* Increase the coverage
  • Loading branch information
0Tech authored Apr 12, 2023
1 parent 8792d89 commit 88c4242
Show file tree
Hide file tree
Showing 5 changed files with 110 additions and 21 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ Ref: https://keepachangelog.com/en/1.0.0/
* (x/foundation) [\#912](https://github.com/line/lbm-sdk/pull/912) Introduce censorship into x/foundation
* (x/foundation) [\#933](https://github.com/line/lbm-sdk/pull/933) Clean up x/foundation apis
* (x/collection) [\#938](https://github.com/line/lbm-sdk/pull/938) Add progress log into x/collection import-genesis
* (x/foundation) [\#952](https://github.com/line/lbm-sdk/pull/952) Address generation of the empty coins in x/foundation

### Bug Fixes
* (swagger) [\#898](https://github.com/line/lbm-sdk/pull/898) fix a bug not added `lbm.tx.v1beta1.Service/GetBlockWithTxs` in swagger
Expand Down
2 changes: 1 addition & 1 deletion client/grpc/tmservice/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ func (s IntegrationTestSuite) TestQueryBlockResultsByHeight() {
s.Require().Equal(0, len(txResult))

beginBlock := blockResultsRes.GetResBeginBlock()
s.Require().Equal(11, len(beginBlock.Events)) // coinbase event (6) + transfer mintModule to feeCollectorName(5)
s.Require().Equal(7, len(beginBlock.Events)) // coinbase event (6) + transfer mintModule to feeCollectorName(5) - foundation abci (4)

endBlock := blockResultsRes.GetResEndBlock()
s.Require().Equal(0, len(endBlock.Events))
Expand Down
48 changes: 28 additions & 20 deletions x/foundation/keeper/internal/abci_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,28 +7,36 @@ import (
)

func (s *KeeperTestSuite) TestBeginBlocker() {
ctx, _ := s.ctx.CacheContext()

taxRatio := sdk.MustNewDecFromStr("0.123456789")
s.impl.SetParams(ctx, foundation.Params{
FoundationTax: taxRatio,
})

before := s.impl.GetTreasury(ctx)
s.Require().Equal(1, len(before))
s.Require().Equal(sdk.NewDecFromInt(s.balance), before[0].Amount)

// collect
internal.BeginBlocker(ctx, s.impl)
for name, tc := range map[string]struct {
taxRatio sdk.Dec
valid bool
}{
"valid ratio": {
taxRatio: sdk.OneDec(),
valid: true,
},
"ratio > 1": {
taxRatio: sdk.MustNewDecFromStr("1.00000001"),
},
} {
s.Run(name, func() {
ctx, _ := s.ctx.CacheContext()

tax := sdk.NewDecFromInt(s.balance).MulTruncate(taxRatio).TruncateInt()
// ensure the behavior does not change
s.Require().Equal(sdk.NewInt(121932631), tax)
s.impl.SetParams(ctx, foundation.Params{
FoundationTax: tc.taxRatio,
})

expectedAfter := s.balance.Add(tax)
after := s.impl.GetTreasury(ctx)
s.Require().Equal(1, len(after))
s.Require().Equal(sdk.NewDecFromInt(expectedAfter), after[0].Amount)
// collect
testing := func() {
internal.BeginBlocker(ctx, s.impl)
}
if tc.valid {
s.Require().NotPanics(testing)
} else {
s.Require().Panics(testing)
}
})
}
}

func (s *KeeperTestSuite) TestEndBlocker() {
Expand Down
6 changes: 6 additions & 0 deletions x/foundation/keeper/internal/treasury.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,17 @@ import (
func (k Keeper) CollectFoundationTax(ctx sdk.Context) error {
feeCollector := k.authKeeper.GetModuleAccount(ctx, k.feeCollectorName).GetAddress()
feesCollectedInt := k.bankKeeper.GetAllBalances(ctx, feeCollector)
if feesCollectedInt.Empty() {
return nil
}
feesCollected := sdk.NewDecCoinsFromCoins(feesCollectedInt...)

// calculate the tax
taxRatio := k.GetFoundationTax(ctx)
tax, _ := feesCollected.MulDecTruncate(taxRatio).TruncateDecimal()
if tax.Empty() {
return nil
}

// collect the tax
if err := k.FundTreasury(ctx, feeCollector, tax); err != nil {
Expand Down
74 changes: 74 additions & 0 deletions x/foundation/keeper/internal/treasury_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,80 @@ import (
"github.com/line/lbm-sdk/x/foundation"
)

func (s *KeeperTestSuite) TestCollectFoundationTax() {
ctx, _ := s.ctx.CacheContext()

// empty fee collector first
// send the fee to the stranger
// and get it back later if the test case requires
collector := authtypes.NewModuleAddress(authtypes.FeeCollectorName)
fees := s.bankKeeper.GetAllBalances(ctx, collector)
s.bankKeeper.SendCoinsFromModuleToAccount(ctx, authtypes.FeeCollectorName, s.stranger, fees)

for name, tc := range map[string]struct {
fee sdk.Int
taxRatio sdk.Dec
tax sdk.Int
valid bool
}{
"common": {
fee: fees[0].Amount,
taxRatio: sdk.MustNewDecFromStr("0.123456789"),
tax: sdk.NewInt(121932631),
valid: true,
},
"zero fee": {
fee: sdk.ZeroInt(),
taxRatio: sdk.MustNewDecFromStr("0.123456789"),
tax: sdk.ZeroInt(),
valid: true,
},
"zero ratio": {
fee: fees[0].Amount,
taxRatio: sdk.ZeroDec(),
tax: sdk.ZeroInt(),
valid: true,
},
"send fails": {
fee: fees[0].Amount,
taxRatio: sdk.MustNewDecFromStr("1.00000001"),
tax: sdk.NewInt(987654330),
},
} {
s.Run(name, func() {
ctx, _ := ctx.CacheContext()

// set fee
s.bankKeeper.SendCoinsFromAccountToModule(ctx, s.stranger, authtypes.FeeCollectorName, sdk.NewCoins(sdk.NewCoin(fees[0].Denom, tc.fee)))

// set tax ratio
s.impl.SetParams(ctx, foundation.Params{
FoundationTax: tc.taxRatio,
})

before := s.impl.GetTreasury(ctx)
s.Require().Equal(1, len(before))
s.Require().Equal(sdk.NewDecFromInt(s.balance), before[0].Amount)

tax := sdk.NewDecFromInt(tc.fee).MulTruncate(tc.taxRatio).TruncateInt()
// ensure the behavior does not change
s.Require().Equal(tc.tax, tax)

err := s.impl.CollectFoundationTax(ctx)
if !tc.valid {
s.Require().Error(err)
return
}
s.Require().NoError(err)

expectedAfter := s.balance.Add(tax)
after := s.impl.GetTreasury(ctx)
s.Require().Equal(1, len(after))
s.Require().Equal(sdk.NewDecFromInt(expectedAfter), after[0].Amount)
})
}
}

func (s *KeeperTestSuite) TestFundTreasury() {
testCases := map[string]struct {
amount sdk.Int
Expand Down

0 comments on commit 88c4242

Please sign in to comment.