diff --git a/app/app.go b/app/app.go index 41517e6e..1a519c85 100644 --- a/app/app.go +++ b/app/app.go @@ -445,6 +445,7 @@ func (app *CyberdApp) DeliverTx(req abci.RequestDeliverTx) (res abci.ResponseDel } app.bandwidthMeter.AddToBlockBandwidth(app.bandwidthMeter.GetTxCost(ctx, tx)) + app.bandwidthMeter.AddToOverallKarma(linkingCost) return abci.ResponseDeliverTx{ Code: resp.GetCode(), diff --git a/x/bandwidth/internal/types/meter.go b/x/bandwidth/internal/types/meter.go index 284c0d33..46faa6d4 100644 --- a/x/bandwidth/internal/types/meter.go +++ b/x/bandwidth/internal/types/meter.go @@ -19,6 +19,8 @@ type BandwidthMeter interface { Load(ctx sdk.Context) // add value to consumed bandwidth for current block AddToBlockBandwidth(value int64) + // add value to overall linked bandwidth + AddToOverallKarma(value int64) // adjust price based on 24h loading AdjustPrice(ctx sdk.Context) // get current bandwidth price diff --git a/x/bandwidth/meter.go b/x/bandwidth/meter.go index d4c05eb4..3b806e26 100644 --- a/x/bandwidth/meter.go +++ b/x/bandwidth/meter.go @@ -67,6 +67,10 @@ func (m *BaseBandwidthMeter) AddToBlockBandwidth(value int64) { m.curBlockSpentBandwidth += uint64(value) } +func (m *BaseBandwidthMeter) AddToOverallKarma(value int64) { + m.bandwidthSpentLinking += uint64(value) +} + // Here we move bandwidth window: // Remove first block of window and add new block to window end func (m *BaseBandwidthMeter) CommitBlockBandwidth(ctx sdk.Context) { @@ -87,7 +91,6 @@ func (m *BaseBandwidthMeter) CommitBlockBandwidth(ctx sdk.Context) { } m.blockBandwidthKeeper.SetBlockSpentBandwidth(ctx, uint64(ctx.BlockHeight()), m.curBlockSpentBandwidth) m.bandwidthSpent[uint64(newWindowEnd)] = m.curBlockSpentBandwidth - m.bandwidthSpentLinking += m.curBlockSpentBandwidth m.curBlockSpentBandwidth = 0 } diff --git a/x/bank/exported/exported.go b/x/bank/exported/exported.go index 0fd870e2..c23f9a54 100644 --- a/x/bank/exported/exported.go +++ b/x/bank/exported/exported.go @@ -28,7 +28,7 @@ type IndexedKeeper interface { Load(sdk.Context, sdk.Context) - FixUserStake() bool + FixUserStake(ctx sdk.Context) bool UpdateStake(cbd.AccNumber, int64) GetTotalStakes() map[cbd.AccNumber]uint64 EndBlocker(sdk.Context) diff --git a/x/bank/internal/keeper/index.go b/x/bank/internal/keeper/index.go index dfa3fd63..6eeb3549 100644 --- a/x/bank/internal/keeper/index.go +++ b/x/bank/internal/keeper/index.go @@ -3,6 +3,8 @@ package keeper import ( sdk "github.com/cosmos/cosmos-sdk/types" "github.com/cosmos/cosmos-sdk/x/auth" + "github.com/tendermint/tendermint/crypto" + cbd "github.com/cybercongress/cyberd/types" ) @@ -51,7 +53,17 @@ func (s *IndexedKeeper) getCollectFunc(ctx sdk.Context, userStake map[cbd.AccNum } // return true if some stake changed -func (s *IndexedKeeper) FixUserStake() bool { +func (s *IndexedKeeper) FixUserStake(ctx sdk.Context) bool { + + // Standalone changes of modules balance should not trigger a rank recalculation + modulesNames := [6]string{"bonded_tokens_pool", "not_bonded_tokens_pool", "gov", "distribution", "mint", "fee_collector"} + for _, name := range modulesNames { + supplyModuleAddress := sdk.AccAddress(crypto.AddressHash([]byte(name))) + supplyModuleAccount := s.accountKeeper.GetAccount(ctx, supplyModuleAddress) + supplyModuleAccountNumber := cbd.AccNumber(supplyModuleAccount.GetAccountNumber()) + s.userTotalStake[supplyModuleAccountNumber] = s.userNewTotalStake[supplyModuleAccountNumber] + } + stakeChanged := false for k, v := range s.userNewTotalStake { if s.userTotalStake[k] != v { diff --git a/x/rank/internal/keeper/state.go b/x/rank/internal/keeper/state.go index a88b66da..c7386f81 100644 --- a/x/rank/internal/keeper/state.go +++ b/x/rank/internal/keeper/state.go @@ -130,7 +130,7 @@ func (s *StateKeeper) EndBlocker(ctx sdk.Context, log log.Logger) { s.applyNextRank() s.cidCount = int64(currentCidsCount) - stakeChanged := s.stakeKeeper.FixUserStake() + stakeChanged := s.stakeKeeper.FixUserStake(ctx) // start new calculation if s.hasNewLinksForPeriod || stakeChanged { diff --git a/x/rank/internal/types/expected_keepers.go b/x/rank/internal/types/expected_keepers.go index c3ceb0d0..ea60dd4a 100644 --- a/x/rank/internal/types/expected_keepers.go +++ b/x/rank/internal/types/expected_keepers.go @@ -8,7 +8,7 @@ import ( ) type StakeKeeper interface { - FixUserStake() bool + FixUserStake(ctx sdk.Context) bool GetTotalStakes() map[cbd.AccNumber]uint64 } diff --git a/x/rank/internal/types/rank.go b/x/rank/internal/types/rank.go index d7ae984a..9478ad39 100644 --- a/x/rank/internal/types/rank.go +++ b/x/rank/internal/types/rank.go @@ -32,6 +32,7 @@ func NewRank(values []float64, logger log.Logger, fullTree bool) Rank { newSortedCIDs := make(sortableCidNumbers, 0, len(values)) for cid, rank := range values { + if math.IsNaN(rank) { continue } // TODO remove this after rank's NaN fix newRankedCid := RankedCidNumber{link.CidNumber(cid), rank} newSortedCIDs = append(newSortedCIDs, newRankedCid) }