Skip to content

Commit

Permalink
#179 Change Bandwidth Price to Average for 24h Sliding Window
Browse files Browse the repository at this point in the history
  • Loading branch information
arturalbov authored and hleb-albau committed Jan 24, 2019
1 parent a405b4d commit b12c051
Show file tree
Hide file tree
Showing 13 changed files with 197 additions and 259 deletions.
36 changes: 12 additions & 24 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,11 @@ import (
cbdbank "github.com/cybercongress/cyberd/x/bank"
"github.com/cybercongress/cyberd/x/link"
"github.com/cybercongress/cyberd/x/link/keeper"
lnk "github.com/cybercongress/cyberd/x/link/types"
"github.com/cybercongress/cyberd/x/rank"
abci "github.com/tendermint/tendermint/abci/types"
cmn "github.com/tendermint/tendermint/libs/common"
dbm "github.com/tendermint/tendermint/libs/db"
"github.com/tendermint/tendermint/libs/log"
"math"
"os"
"sort"
"time"
Expand Down Expand Up @@ -57,11 +55,8 @@ type CyberdApp struct {
txDecoder sdk.TxDecoder

// bandwidth
bandwidthMeter bw.BandwidthMeter
//todo 3 fields below should be in bw meter
curBlockSpentBandwidth uint64 //resets every block
lastTotalSpentBandwidth uint64 //resets every bandwidth price adjustment interval
currentCreditPrice float64
bandwidthMeter bw.BandwidthMeter
blockBandwidthKeeper bw.BlockSpentBandwidthKeeper

// keys to access the multistore
dbKeys CyberdAppDbKeys
Expand All @@ -85,8 +80,6 @@ type CyberdApp struct {
rankState *rank.RankState

latestBlockHeight int64

// TODO: move to RankState???
}

// NewBasecoinApp returns a reference to a new CyberdApp given a
Expand Down Expand Up @@ -120,6 +113,7 @@ func NewCyberdApp(
app.feeCollectionKeeper = NoopFeeCollectionKeeper{}
app.paramsKeeper = params.NewKeeper(app.cdc, dbKeys.params, dbKeys.tParams)
app.accBandwidthKeeper = bandwidth.NewAccBandwidthKeeper(dbKeys.accBandwidth)
app.blockBandwidthKeeper = bandwidth.NewBlockSpentBandwidthKeeper(dbKeys.blockBandwidth)

app.accountKeeper = auth.NewAccountKeeper(
app.cdc, dbKeys.acc,
Expand Down Expand Up @@ -169,7 +163,8 @@ func NewCyberdApp(
)

app.bandwidthMeter = bandwidth.NewBaseMeter(
app.accountKeeper, app.bankKeeper, app.accBandwidthKeeper, bandwidth.MsgBandwidthCosts,
app.mainKeeper, app.accountKeeper, app.bankKeeper, app.accBandwidthKeeper, bandwidth.MsgBandwidthCosts,
app.blockBandwidthKeeper,
)

// register message routes
Expand Down Expand Up @@ -202,6 +197,7 @@ func NewCyberdApp(

ctx := app.BaseApp.NewContext(true, abci.Header{})
app.latestBlockHeight = int64(ms.GetLatestBlockNumber(ctx))
ctx = ctx.WithBlockHeight(app.latestBlockHeight)

// build context for current rank calculation round
rankRoundBlockNumber := (app.latestBlockHeight / rank.CalculationPeriod) * rank.CalculationPeriod
Expand All @@ -214,10 +210,8 @@ func NewCyberdApp(
app.stakeIndex.Load(rankCtx, ctx)
app.BaseApp.Logger.Info("App loaded", "time", time.Since(start))

// BANDWIDTH PARAMS
app.lastTotalSpentBandwidth = ms.GetSpentBandwidth(ctx)
app.currentCreditPrice = math.Float64frombits(ms.GetBandwidthPrice(ctx, bandwidth.BaseCreditPrice))
app.curBlockSpentBandwidth = 0
// BANDWIDTH LOAD
app.bandwidthMeter.Load(ctx)

// RANK PARAMS
app.rankState.Load(ctx, app.latestBlockHeight, app.Logger)
Expand Down Expand Up @@ -315,7 +309,7 @@ func (app *CyberdApp) CheckTx(txBytes []byte) (res abci.ResponseCheckTx) {

if err == nil {

txCost := app.bandwidthMeter.GetTxCost(ctx, app.currentCreditPrice, tx)
txCost := app.bandwidthMeter.GetTxCost(ctx, tx)
accBw := app.bandwidthMeter.GetCurrentAccBandwidth(ctx, acc)

if !accBw.HasEnoughRemained(txCost) {
Expand Down Expand Up @@ -348,7 +342,7 @@ func (app *CyberdApp) DeliverTx(txBytes []byte) (res abci.ResponseDeliverTx) {

if err == nil {

txCost := app.bandwidthMeter.GetTxCost(ctx, app.currentCreditPrice, tx)
txCost := app.bandwidthMeter.GetTxCost(ctx, tx)
accBw := app.bandwidthMeter.GetCurrentAccBandwidth(ctx, acc)

if !accBw.HasEnoughRemained(txCost) {
Expand All @@ -357,7 +351,7 @@ func (app *CyberdApp) DeliverTx(txBytes []byte) (res abci.ResponseDeliverTx) {

resp := app.BaseApp.DeliverTx(txBytes)
app.bandwidthMeter.ConsumeAccBandwidth(ctx, accBw, txCost)
app.curBlockSpentBandwidth = app.curBlockSpentBandwidth + uint64(txCost)
app.bandwidthMeter.AddToBlockBandwidth(uint64(txCost))

return abci.ResponseDeliverTx{
Code: uint32(resp.Code),
Expand Down Expand Up @@ -453,13 +447,7 @@ func (app *CyberdApp) EndBlocker(ctx sdk.Context, _ abci.RequestEndBlock) abci.R
validatorUpdates, tags := staking.EndBlocker(ctx, app.stakingKeeper)
app.stakeIndex.EndBlocker(ctx)

newPrice, totalSpentBandwidth := bandwidth.EndBlocker(
ctx, app.lastTotalSpentBandwidth+app.curBlockSpentBandwidth, app.currentCreditPrice,
app.mainKeeper, app.bandwidthMeter,
)
app.lastTotalSpentBandwidth = totalSpentBandwidth
app.currentCreditPrice = newPrice
app.curBlockSpentBandwidth = 0
bandwidth.EndBlocker(ctx, app.bandwidthMeter)

// RANK CALCULATION
app.rankState.EndBlocker(ctx, app.Logger)
Expand Down
4 changes: 0 additions & 4 deletions app/fee.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,13 @@ package app

import (
sdk "github.com/cosmos/cosmos-sdk/types"
"log"
)

var noCoins = sdk.Coins{}

//__________________________________________________________________________________
// fee collection keeper used only for testing
type NoopFeeCollectionKeeper struct{}

func (fck NoopFeeCollectionKeeper) AddCollectedFees(_ sdk.Context, c sdk.Coins) sdk.Coins {
log.Println(c.String())
return noCoins
}

Expand Down
61 changes: 31 additions & 30 deletions app/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,51 +12,52 @@ import (
)

type CyberdAppDbKeys struct {
main *sdk.KVStoreKey
acc *sdk.KVStoreKey
accIndex *sdk.KVStoreKey
cidNum *sdk.KVStoreKey
cidNumReverse *sdk.KVStoreKey
links *sdk.KVStoreKey
rank *sdk.KVStoreKey
stake *sdk.KVStoreKey
tStake *sdk.TransientStoreKey
distr *sdk.KVStoreKey
tDistr *sdk.TransientStoreKey
slashing *sdk.KVStoreKey
main *sdk.KVStoreKey
acc *sdk.KVStoreKey
accIndex *sdk.KVStoreKey
cidNum *sdk.KVStoreKey
cidNumReverse *sdk.KVStoreKey
links *sdk.KVStoreKey
rank *sdk.KVStoreKey
stake *sdk.KVStoreKey
tStake *sdk.TransientStoreKey
distr *sdk.KVStoreKey
tDistr *sdk.TransientStoreKey
slashing *sdk.KVStoreKey
mint *sdk.KVStoreKey
params *sdk.KVStoreKey
tParams *sdk.TransientStoreKey
accBandwidth *sdk.KVStoreKey
params *sdk.KVStoreKey
tParams *sdk.TransientStoreKey
accBandwidth *sdk.KVStoreKey
blockBandwidth *sdk.KVStoreKey
}

func NewCyberdAppDbKeys() CyberdAppDbKeys {

return CyberdAppDbKeys{

main: sdk.NewKVStoreKey(bam.MainStoreKey),
acc: sdk.NewKVStoreKey(auth.StoreKey),
stake: sdk.NewKVStoreKey(staking.StoreKey),
tStake: sdk.NewTransientStoreKey(staking.TStoreKey),
mint: sdk.NewKVStoreKey(mint.StoreKey),
distr: sdk.NewKVStoreKey(distr.StoreKey),
main: sdk.NewKVStoreKey(bam.MainStoreKey),
acc: sdk.NewKVStoreKey(auth.StoreKey),
stake: sdk.NewKVStoreKey(staking.StoreKey),
tStake: sdk.NewTransientStoreKey(staking.TStoreKey),
mint: sdk.NewKVStoreKey(mint.StoreKey),
distr: sdk.NewKVStoreKey(distr.StoreKey),
tDistr: sdk.NewTransientStoreKey(distr.TStoreKey),
slashing: sdk.NewKVStoreKey(slashing.StoreKey),
params: sdk.NewKVStoreKey(params.StoreKey),
tParams: sdk.NewTransientStoreKey(params.TStoreKey),
slashing: sdk.NewKVStoreKey(slashing.StoreKey),
params: sdk.NewKVStoreKey(params.StoreKey),
tParams: sdk.NewTransientStoreKey(params.TStoreKey),

cidNum: sdk.NewKVStoreKey("cid_index"),
cidNumReverse: sdk.NewKVStoreKey("cid_index_reverse"),
cidNum: sdk.NewKVStoreKey("cid_index"),
cidNumReverse: sdk.NewKVStoreKey("cid_index_reverse"),
links: sdk.NewKVStoreKey("links"),
rank: sdk.NewKVStoreKey("rank"),
accBandwidth: sdk.NewKVStoreKey("acc_bandwidth"),
rank: sdk.NewKVStoreKey("rank"),
accBandwidth: sdk.NewKVStoreKey("acc_bandwidth"),
blockBandwidth: sdk.NewKVStoreKey("block_spent_bandwidth"),
}
}

func (k CyberdAppDbKeys) GetStoreKeys() []*sdk.KVStoreKey {
return []*sdk.KVStoreKey{
k.main, k.acc, k.cidNum, k.cidNumReverse, k.links, k.rank, k.stake,
k.slashing, k.params, k.distr, k.mint, k.accBandwidth,
k.slashing, k.params, k.distr, k.mint, k.accBandwidth, k.blockBandwidth,
}
}

Expand Down
2 changes: 1 addition & 1 deletion app/rpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ func (app *CyberdApp) IsLinkExist(from cbdlink.Cid, to cbdlink.Cid, address sdk.
}

func (app *CyberdApp) CurrentBandwidthPrice() float64 {
return app.currentCreditPrice
return app.bandwidthMeter.GetCurrentCreditPrice()
}

func (app *CyberdApp) CidsCount() uint64 {
Expand Down
19 changes: 0 additions & 19 deletions wiki/cid.go

This file was deleted.

83 changes: 0 additions & 83 deletions wiki/index.go

This file was deleted.

53 changes: 0 additions & 53 deletions wiki/wiki.go

This file was deleted.

Loading

0 comments on commit b12c051

Please sign in to comment.