Skip to content

Commit

Permalink
Update stake index every block
Browse files Browse the repository at this point in the history
  • Loading branch information
hleb-albau authored and arturalbov committed Dec 21, 2018
1 parent cae04e4 commit d5a8caf
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 32 deletions.
36 changes: 21 additions & 15 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,8 @@ type CyberdApp struct {
txDecoder sdk.TxDecoder

// bandwidth
bandwidthMeter bw.BandwidthMeter
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
Expand All @@ -101,7 +102,7 @@ type CyberdApp struct {
// cyberd storages
linkIndexedKeeper keeper.LinkIndexedKeeper
cidNumKeeper keeper.CidNumberKeeper
stakeIndex cbdbank.IndexedKeeper
stakeIndex *cbdbank.IndexedKeeper
rankState *rank.RankState

latestRankHash []byte
Expand Down Expand Up @@ -160,9 +161,12 @@ func NewCyberdApp(
app.feeCollectionKeeper = auth.NewFeeCollectionKeeper(app.cdc, dbKeys.fees)

var stakeKeeper stake.Keeper
coinTransferHooks := []cbdbank.CoinsTransferHook{bandwidth.CollectAddressesWithStakeChange()}
app.bankKeeper = cbdbank.NewBankKeeper(app.accountKeeper, &stakeKeeper, coinTransferHooks)

app.bankKeeper = cbdbank.NewBankKeeper(app.accountKeeper, &stakeKeeper)
app.bankKeeper.AddHook(bandwidth.CollectAddressesWithStakeChange())

app.accBandwidthKeeper = bandwidth.NewAccBandwidthKeeper(dbKeys.accBandwidth)

stakeKeeper = stake.NewKeeper(
app.cdc, dbKeys.stake,
dbKeys.tStake, app.bankKeeper,
Expand All @@ -185,7 +189,7 @@ func NewCyberdApp(
// cyberd keepers
app.linkIndexedKeeper = keeper.NewLinkIndexedKeeper(keeper.NewBaseLinkKeeper(ms, dbKeys.links))
app.cidNumKeeper = keeper.NewBaseCidNumberKeeper(ms, dbKeys.cidNum, dbKeys.cidNumReverse)
app.stakeIndex = cbdbank.NewIndexedKeeper(app.bankKeeper, app.accountKeeper)
app.stakeIndex = cbdbank.NewIndexedKeeper(&app.bankKeeper, app.accountKeeper)
app.rankState = rank.NewRankState(&app.linkIndexedKeeper)

// register the staking hooks
Expand Down Expand Up @@ -462,11 +466,22 @@ func (app *CyberdApp) BeginBlocker(ctx sdk.Context, req abci.RequestBeginBlock)
// App state is consensus driven state.
func (app *CyberdApp) EndBlocker(ctx sdk.Context, _ abci.RequestEndBlock) abci.ResponseEndBlock {

validatorUpdates, tags := stake.EndBlocker(ctx, app.stakeKeeper)
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

linksCount := app.mainKeeper.GetLinksCount(ctx)
cidsCount := app.mainKeeper.GetCidsCount(ctx)

start := time.Now()
calcCtx := rank.NewCalcContext(ctx, app.linkIndexedKeeper, app.cidNumKeeper, app.stakeIndex)
calcCtx := rank.NewCalcContext(ctx, app.linkIndexedKeeper, app.cidNumKeeper, *app.stakeIndex)
newRank, steps := rank.CalculateRank(calcCtx, app.computeUnit, app.BaseApp.Logger)
app.BaseApp.Logger.Info(
"Rank calculated", "steps", steps, "time", time.Since(start), "links", linksCount, "cids", cidsCount,
Expand All @@ -485,15 +500,6 @@ func (app *CyberdApp) EndBlocker(ctx sdk.Context, _ abci.RequestEndBlock) abci.R
app.rankState.UpdateRank(newRank, int64(app.cidNumKeeper.GetCidsCount(ctx)))
app.mainKeeper.StoreAppHash(ctx, hash[:])

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

validatorUpdates, tags := stake.EndBlocker(ctx, app.stakeKeeper)
return abci.ResponseEndBlock{
ValidatorUpdates: validatorUpdates,
Tags: tags,
Expand Down
46 changes: 37 additions & 9 deletions x/bank/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,39 +6,67 @@ import (
cbd "github.com/cybercongress/cyberd/types"
)

// Used to hodl total user stake in memory for further rank calculation.
// Updated once at block and the beginning of end block.
type IndexedKeeper struct {
Keeper

accKeeper auth.AccountKeeper

userStake map[cbd.AccNumber]uint64
userTotalStake map[cbd.AccNumber]uint64

// used to track accs with changed stake
accsToUpdate []sdk.AccAddress
}

func NewIndexedKeeper(keeper Keeper, accKeeper auth.AccountKeeper) IndexedKeeper {
return IndexedKeeper{Keeper: keeper, accKeeper: accKeeper}
func NewIndexedKeeper(keeper *Keeper, accKeeper auth.AccountKeeper) *IndexedKeeper {
index := IndexedKeeper{Keeper: *keeper, accKeeper: accKeeper, accsToUpdate: make([]sdk.AccAddress, 0)}
hook := func(ctx sdk.Context, from sdk.AccAddress, to sdk.AccAddress) {
if ctx.IsCheckTx() {
return
}
if from != nil {
index.accsToUpdate = append(index.accsToUpdate, from)
}
if to != nil {
index.accsToUpdate = append(index.accsToUpdate, to)
}
}
keeper.AddHook(hook)
return &index
}

func (s *IndexedKeeper) Empty() {
s.userStake = make(map[cbd.AccNumber]uint64)
s.userTotalStake = make(map[cbd.AccNumber]uint64)
}

func (s *IndexedKeeper) Load(ctx sdk.Context) {

s.userStake = make(map[cbd.AccNumber]uint64)
s.userTotalStake = make(map[cbd.AccNumber]uint64)

collect := func(acc auth.Account) bool {
balance := s.Keeper.GetAccountTotalStake(ctx, acc.GetAddress())
s.userStake[cbd.AccNumber(acc.GetAccountNumber())] = uint64(balance)
s.userTotalStake[cbd.AccNumber(acc.GetAccountNumber())] = uint64(balance)
return false
}

s.accKeeper.IterateAccounts(ctx, collect)
}

func (s *IndexedKeeper) UpdateStake(acc cbd.AccNumber, stake int64) {
s.userStake[acc] += uint64(stake)
s.userTotalStake[acc] += uint64(stake)
}

func (s *IndexedKeeper) GetStakes() map[cbd.AccNumber]uint64 {
return s.userStake
func (s *IndexedKeeper) GetTotalStakes() map[cbd.AccNumber]uint64 {
return s.userTotalStake
}

// Performs stakes updates for acc touched in current block
func (s *IndexedKeeper) EndBlocker(ctx sdk.Context) {
for _, addr := range s.accsToUpdate {
stake := s.Keeper.GetAccountTotalStake(ctx, addr)
accNum := cbd.AccNumber(s.accKeeper.GetAccount(ctx, addr).GetAccountNumber())
s.userTotalStake[accNum] = uint64(stake)
}
s.accsToUpdate = make([]sdk.AccAddress, 0)
}
18 changes: 11 additions & 7 deletions x/bank/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,36 +17,40 @@ type Keeper struct {
coinsTransferHooks []CoinsTransferHook
}

func NewBankKeeper(ak auth.AccountKeeper, sk *stake.Keeper, coinsTransferHooks []CoinsTransferHook) Keeper {
func NewBankKeeper(ak auth.AccountKeeper, sk *stake.Keeper) Keeper {
return Keeper{
Keeper: bank.NewBaseKeeper(ak),
ak: ak,
sk: sk,
coinsTransferHooks: coinsTransferHooks,
coinsTransferHooks: make([]CoinsTransferHook, 0),
}
}

func (k *Keeper) AddHook(hook CoinsTransferHook) {
k.coinsTransferHooks = append(k.coinsTransferHooks, hook)
}

/* Override methods */
// sdk acc keeper is not interface yet
func (k Keeper) AddCoins(ctx sdk.Context, addr sdk.AccAddress, amt sdk.Coins) (sdk.Coins, sdk.Tags, sdk.Error) {
coins, tags, err := k.Keeper.AddCoins(ctx, addr, amt)
if err != nil {
if err == nil {
k.onCoinsTransfer(ctx, nil, addr)
}
return coins, tags, err
}

func (k Keeper) SubtractCoins(ctx sdk.Context, addr sdk.AccAddress, amt sdk.Coins) (sdk.Coins, sdk.Tags, sdk.Error) {
coins, tags, err := k.Keeper.SubtractCoins(ctx, addr, amt)
if err != nil {
if err == nil {
k.onCoinsTransfer(ctx, nil, addr)
}
return coins, tags, err
}

func (k Keeper) SetCoins(ctx sdk.Context, addr sdk.AccAddress, amt sdk.Coins) sdk.Error {
err := k.Keeper.SetCoins(ctx, addr, amt)
if err != nil {
if err == nil {
k.onCoinsTransfer(ctx, nil, addr)
}
return err
Expand All @@ -57,7 +61,7 @@ func (k Keeper) SendCoins(
) (sdk.Tags, sdk.Error) {

tags, err := k.Keeper.SendCoins(ctx, fromAddr, toAddr, amt)
if err != nil {
if err == nil {
k.onCoinsTransfer(ctx, fromAddr, toAddr)
}
return tags, err
Expand All @@ -67,7 +71,7 @@ func (k Keeper) InputOutputCoins(
ctx sdk.Context, inputs []bank.Input, outputs []bank.Output,
) (sdk.Tags, sdk.Error) {
tags, err := k.Keeper.InputOutputCoins(ctx, inputs, outputs)
if err != nil {
if err == nil {
for _, i := range inputs {
k.onCoinsTransfer(ctx, i.Address, nil)
}
Expand Down
2 changes: 1 addition & 1 deletion x/rank/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func NewCalcContext(
inLinks: linkIndex.GetInLinks(),
outLinks: linkIndex.GetOutLinks(),

stakes: indexedKeeper.GetStakes(),
stakes: indexedKeeper.GetTotalStakes(),
}
}

Expand Down

0 comments on commit d5a8caf

Please sign in to comment.