Skip to content

Commit

Permalink
Update stake pool total offers only when it has changed
Browse files Browse the repository at this point in the history
  • Loading branch information
peterlimg committed Sep 16, 2022
1 parent c5ecaed commit c4e791d
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 16 deletions.
6 changes: 3 additions & 3 deletions code/go/0chain.net/smartcontract/storagesc/allocation.go
Expand Up @@ -714,7 +714,7 @@ func (sc *StorageSmartContract) closeAllocation(t *transaction.Transaction,
return "", fmt.Errorf("can't get stake pool of %s: %v", ba.BlobberID,
err)
}
if err := sp.removeOffer(ba.Offer()); err != nil {
if err := sp.reduceOffer(ba.Offer()); err != nil {
return "", common.NewError("fini_alloc_failed",
"error removing offer: "+err.Error())
}
Expand Down Expand Up @@ -1071,7 +1071,7 @@ func (sc *StorageSmartContract) reduceAllocation(
err)
}
if newOffer < oldOffer {
if err := sp.removeOffer(oldOffer - newOffer); err != nil {
if err := sp.reduceOffer(oldOffer - newOffer); err != nil {
return fmt.Errorf("removing offer: %v", err)
}
} else {
Expand Down Expand Up @@ -1447,7 +1447,7 @@ func (sc *StorageSmartContract) cancelAllocationRequest(
return "", common.NewError("fini_alloc_failed",
"can't get stake pool of "+d.BlobberID+": "+err.Error())
}
if err := sp.removeOffer(d.Offer()); err != nil {
if err := sp.reduceOffer(d.Offer()); err != nil {
return "", common.NewError("fini_alloc_failed",
"error removing offer: "+err.Error())
}
Expand Down
5 changes: 4 additions & 1 deletion code/go/0chain.net/smartcontract/storagesc/challenge.go
Expand Up @@ -355,7 +355,10 @@ func (sc *StorageSmartContract) blobberPenalty(t *transaction.Transaction,
return fmt.Errorf("can't move tokens to write pool: %v", err)
}

sp.TotalOffers -= move // subtract the offer stake
if err := sp.reduceOffer(move); err != nil {
return err
}

penalty, err := currency.AddCoin(blobAlloc.Penalty, move) // penalty statistic
if err != nil {
return err
Expand Down
21 changes: 9 additions & 12 deletions code/go/0chain.net/smartcontract/storagesc/stakepool.go
Expand Up @@ -56,7 +56,8 @@ type stakePool struct {
stakepool.StakePool
// TotalOffers represents tokens required by currently
// open offers of the blobber. It's allocation_id -> {lock, expire}
TotalOffers currency.Coin `json:"total_offers"`
TotalOffers currency.Coin `json:"total_offers"`
isOfferChanged bool `json:"-" msg:"-"`
// Total amount to be un staked
TotalUnStake currency.Coin `json:"total_un_stake"`
}
Expand Down Expand Up @@ -98,8 +99,11 @@ func (sp *stakePool) save(sscKey, blobberID string,

logging.Logger.Debug("after stake pool save", zap.String("root", util.ToHex([]byte(r))))

tag, data := event.NewUpdateBlobberTotalOffersEvent(blobberID, sp.TotalOffers)
balances.EmitEvent(event.TypeStats, tag, blobberID, data)
if sp.isOfferChanged {
tag, data := event.NewUpdateBlobberTotalOffersEvent(blobberID, sp.TotalOffers)
balances.EmitEvent(event.TypeStats, tag, blobberID, data)
sp.isOfferChanged = false
}

return
}
Expand Down Expand Up @@ -213,6 +217,7 @@ func (sp *stakePool) addOffer(amount currency.Coin) error {
return err
}
sp.TotalOffers = newTotalOffers
sp.isOfferChanged = true
return nil
}

Expand All @@ -223,15 +228,7 @@ func (sp *stakePool) reduceOffer(amount currency.Coin) error {
return err
}
sp.TotalOffers = newTotalOffers
return nil
}

// remove offer of an allocation related to blobber owns this stake pool
func (sp *stakePool) removeOffer(amount currency.Coin) error {
if amount > sp.TotalOffers {
return fmt.Errorf("amount to be removed %v > total offer present %v", amount, sp.TotalOffers)
}
sp.TotalOffers -= amount
sp.isOfferChanged = true
return nil
}

Expand Down

0 comments on commit c4e791d

Please sign in to comment.