Skip to content

Commit

Permalink
Merge pull request #174 from stratosnet/qb-1320_move_distribution_to_…
Browse files Browse the repository at this point in the history
…endblock

Feat/Qb-1323,Qb-1320: Pot module account issue & move distribution to endblock
  • Loading branch information
alexstratos authored Aug 24, 2022
2 parents 4e95fde + 06487a8 commit 3018456
Show file tree
Hide file tree
Showing 16 changed files with 187 additions and 1,056 deletions.
2 changes: 0 additions & 2 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,8 +173,6 @@ var (
registertypes.TotalSlashedPoolName: {authtypes.Minter, authtypes.Burner},

pottypes.FoundationAccount: {authtypes.Minter, authtypes.Burner},
pottypes.MiningRewardPool: nil,
pottypes.TrafficRewardPool: nil,
pottypes.TotalRewardPool: nil,
//pottypes.TotalMinedTokens: {authtypes.Minter, authtypes.Burner},

Expand Down
2 changes: 1 addition & 1 deletion x/evm/keeper/abci.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func (k *Keeper) BeginBlock(ctx sdk.Context, req abci.RequestBeginBlock) {

k.SetBaseFeeParam(ctx, baseFee)

if !ctx.IsCheckTx() {
if !ctx.IsCheckTx() && len(ctx.HeaderHash()) > 0 {
overriddenTxHashes = make(map[string]struct{}, 0)
block, err := core.BlockByHash(&rpctypes.Context{}, ctx.HeaderHash().Bytes())
rawTxs := block.Block.Txs
Expand Down
27 changes: 25 additions & 2 deletions x/pot/abci.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,29 @@ func BeginBlocker(ctx sdk.Context, req abci.RequestBeginBlock, k keeper.Keeper)
}

// EndBlocker called every block, process inflation, update validator set.
func EndBlocker(ctx sdk.Context, k keeper.Keeper) {
// TODO: fill out if your application requires endblock, if not you can delete this function
func EndBlocker(ctx sdk.Context, req abci.RequestEndBlock, k keeper.Keeper) []abci.ValidatorUpdate {

// Do not distribute rewards until the next block
if !k.GetIsReadyToDistributeReward(ctx) && k.GetUnhandledEpoch(ctx).GT(sdk.ZeroInt()) {
k.SetIsReadyToDistributeReward(ctx, true)
return []abci.ValidatorUpdate{}
}

walletVolumes, found := k.GetUnhandledReport(ctx)
if !found {
return []abci.ValidatorUpdate{}
}
epoch := k.GetUnhandledEpoch(ctx)
logger := k.Logger(ctx)

//distribute POT reward
_, err := k.DistributePotReward(ctx, walletVolumes, epoch)
if err != nil {
logger.Error("An error occurred while distributing the reward. ", err)
}

k.SetUnhandledReport(ctx, nil)
k.SetUnhandledEpoch(ctx, sdk.ZeroInt())

return []abci.ValidatorUpdate{}
}
15 changes: 12 additions & 3 deletions x/pot/app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,7 @@ func TestPotVolumeReportMsgs(t *testing.T) {
println("S=" + S.String() + "\nPt=" + Pt.String() + "\nY=" + Y.String() + "\nLt=" + Lt.String() + "\nR=" + R.String() + "\n")

println("---------------------------")
potKeeper.InitVariable(ctx)
distributeGoal := types.InitDistributeGoal()
distributeGoal, err := potKeeper.CalcTrafficRewardInTotal(ctx, distributeGoal, totalConsumedUoz)
require.NoError(t, err)
Expand All @@ -304,10 +305,9 @@ func TestPotVolumeReportMsgs(t *testing.T) {

println("---------------------------")
println("distribute detail:")
distributeGoalBalance := distributeGoal
rewardDetailMap := make(map[string]types.Reward)
rewardDetailMap, distributeGoalBalance = potKeeper.CalcRewardForResourceNode(ctx, totalConsumedUoz, volumeReportMsg.WalletVolumes, distributeGoalBalance, rewardDetailMap)
rewardDetailMap, distributeGoalBalance = potKeeper.CalcRewardForMetaNode(ctx, distributeGoalBalance, rewardDetailMap)
rewardDetailMap = potKeeper.CalcRewardForResourceNode(ctx, totalConsumedUoz, volumeReportMsg.WalletVolumes, distributeGoal, rewardDetailMap)
rewardDetailMap = potKeeper.CalcRewardForMetaNode(ctx, distributeGoal, rewardDetailMap)

println("resource_wallet1: address = " + resOwner1.String())
println(" miningReward = " + rewardDetailMap[resOwner1.String()].RewardFromMiningPool.String())
Expand Down Expand Up @@ -356,6 +356,11 @@ func TestPotVolumeReportMsgs(t *testing.T) {
require.NoError(t, err)

/********************* commit & check result *********************/
// reward distribution start at height = height + 1 where volume report tx executed
header = tmproto.Header{Height: stApp.LastBlockHeight() + 1, ChainID: chainID}
stApp.BeginBlock(abci.RequestBeginBlock{Header: header})
stApp.EndBlock(abci.RequestEndBlock{Height: header.Height})
stApp.Commit()
header = tmproto.Header{Height: stApp.LastBlockHeight() + 1, ChainID: chainID}
stApp.BeginBlock(abci.RequestBeginBlock{Header: header})
ctx = stApp.BaseApp.NewContext(true, header)
Expand Down Expand Up @@ -466,6 +471,10 @@ func checkResult(t *testing.T, ctx sdk.Context,
}
println("matureTotalOfResNode1Change = " + matureTotalOfResNode1Change.String())
require.Equal(t, matureTotalOfResNode1Change.String(), upcomingMaturedIndividual.String())

totalRewardPoolAddr := accountKeeper.GetModuleAddress(types.TotalRewardPool)
totalRewardPoolBalance := bankKeeper.GetAllBalances(ctx, totalRewardPoolAddr)
println("totalRewardPoolBalance = " + totalRewardPoolBalance.String())
}

func checkValidator(t *testing.T, app *app.NewApp, addr sdk.ValAddress, expFound bool) stakingtypes.Validator {
Expand Down
Loading

0 comments on commit 3018456

Please sign in to comment.