Skip to content

Commit

Permalink
#99 Rename bandwidth handler -> bandwidth meter.
Browse files Browse the repository at this point in the history
  • Loading branch information
hleb-albau committed Dec 17, 2018
1 parent edee772 commit 3305e7b
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 16 deletions.
6 changes: 3 additions & 3 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ type CyberdApp struct {
txDecoder sdk.TxDecoder

// bandwidth
bandwidthHandler bw.Handler
bandwidthHandler bw.BandwidthMeter
curBlockSpentBandwidth uint64 //resets every block
lastTotalSpentBandwidth uint64 //resets every bandwidth price adjustment interval
currentCreditPrice float64
Expand Down Expand Up @@ -187,7 +187,7 @@ func NewCyberdApp(
// so that it can be modified like below:
app.stakeKeeper = *stakeKeeper.SetHooks(NewHooks(app.slashingKeeper.Hooks()))

app.bandwidthHandler = bandwidth.NewHandler(
app.bandwidthHandler = bandwidth.NewBaseMeter(
app.accountKeeper, app.bankKeeper, app.accBandwidthKeeper, bandwidth.MsgBandwidthCosts,
)

Expand Down Expand Up @@ -359,7 +359,7 @@ func (app *CyberdApp) CheckTx(txBytes []byte) (res abci.ResponseCheckTx) {

func (app *CyberdApp) DeliverTx(txBytes []byte) (res abci.ResponseDeliverTx) {

ctx := app.NewContext(true, abci.Header{Height: app.latestBlockHeight})
ctx := app.NewContext(false, abci.Header{Height: app.latestBlockHeight})
tx, acc, err := app.decodeTxAndAcc(ctx, txBytes)

if err == nil {
Expand Down
2 changes: 1 addition & 1 deletion x/bandwidth/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
)

// Genesis accounts should contains fully restored bandwidth on block 0
func InitGenesis(ctx sdk.Context, bwHandler types.Handler, bwKeeper types.Keeper, accs []genesis.GenesisAccount) {
func InitGenesis(ctx sdk.Context, bwHandler types.BandwidthMeter, bwKeeper types.Keeper, accs []genesis.GenesisAccount) {

for _, acc := range accs {
accMaxBw := bwHandler.GetAccMaxBandwidth(ctx, acc.Address)
Expand Down
28 changes: 17 additions & 11 deletions x/bandwidth/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ import (
"github.com/cybercongress/cyberd/x/bandwidth/types"
)

var _ types.Handler = BaseHandler{}
var _ types.BandwidthMeter = BaseBandwidthMeter{}

type BaseHandler struct {
type BaseBandwidthMeter struct {
// data providers
accKeeper auth.AccountKeeper
stakeProvider types.AccStakeProvider
Expand All @@ -18,41 +18,47 @@ type BaseHandler struct {
msgCost types.MsgBandwidthCost
}

func NewHandler(
func NewBaseMeter(
ak auth.AccountKeeper, sp types.AccStakeProvider, bwKeeper types.Keeper, msgCost types.MsgBandwidthCost,
) BaseHandler {
) BaseBandwidthMeter {

return BaseHandler{
return BaseBandwidthMeter{
accKeeper: ak,
stakeProvider: sp,
bwKeeper: bwKeeper,
msgCost: msgCost,
}
}

func (h BaseHandler) GetTxCost(ctx sdk.Context, price float64, tx sdk.Tx) int64 {
func (h BaseBandwidthMeter) GetTxCost(ctx sdk.Context, price float64, tx sdk.Tx) int64 {
bandwidthForTx := TxCost
for _, msg := range tx.GetMsgs() {
bandwidthForTx = bandwidthForTx + h.msgCost(msg)
}
return bandwidthForTx
}

func (h BaseHandler) GetAccMaxBandwidth(ctx sdk.Context, addr sdk.AccAddress) int64 {
func (h BaseBandwidthMeter) GetAccMaxBandwidth(ctx sdk.Context, addr sdk.AccAddress) int64 {
accStakePercentage := h.stakeProvider.GetAccStakePercentage(ctx, addr)
return int64(accStakePercentage * float64(MaxNetworkBandwidth) / 2)
}

func (h BaseHandler) GetCurrentAccBandwidth(ctx sdk.Context, address sdk.AccAddress) types.AcсBandwidth {
func (h BaseBandwidthMeter) GetCurrentAccBandwidth(ctx sdk.Context, address sdk.AccAddress) types.AcсBandwidth {
accBw := h.bwKeeper.GetAccBandwidth(ctx, address)
accMaxBw := h.GetAccMaxBandwidth(ctx, address)
accBw.UpdateMax(accMaxBw, ctx.BlockHeight(), RecoveryPeriod)
return accBw
}

// Double save for case:
// When acc send coins, we should consume bw before cutting max bw.
func (h BaseHandler) ConsumeAccBandwidth(ctx sdk.Context, bw types.AcсBandwidth, amt int64) {
//
// Performs bw consumption for given acc
// To get right number, should be called after tx delivery with bw state obtained prior delivery
//
// Pseudo code:
// bw := getCurrentBw(addr)
// bwCost := deliverTx(tx)
// consumeBw(bw, bwCost)
func (h BaseBandwidthMeter) ConsumeAccBandwidth(ctx sdk.Context, bw types.AcсBandwidth, amt int64) {
bw.Consume(amt)
h.bwKeeper.SetAccBandwidth(ctx, bw)
bw = h.GetCurrentAccBandwidth(ctx, bw.Address)
Expand Down
1 change: 1 addition & 0 deletions x/bandwidth/types/acc_bandwidth.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ func (bs AcсBandwidth) HasEnoughRemained(bandwidthToConsume int64) bool {
func (bs *AcсBandwidth) Consume(bandwidthToConsume int64) {
bs.RemainedValue = bs.RemainedValue - bandwidthToConsume
if bs.RemainedValue < 0 {
// should not happen
panic("Negative bandwidth!!")
}
}
Expand Down
2 changes: 1 addition & 1 deletion x/bandwidth/types/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ type AccStakeProvider interface {
// 3. Run Tx, if tx succeed, than:
// 4. Consume bw and save with old max bw value
// 5. Load bw with new max value and save it
type Handler interface {
type BandwidthMeter interface {
// Returns recovered to current block height acc bandwidth
GetCurrentAccBandwidth(ctx sdk.Context, address sdk.AccAddress) AcсBandwidth
// Returns acc max bandwidth
Expand Down

0 comments on commit 3305e7b

Please sign in to comment.