Skip to content

Commit

Permalink
side chain reward distribution fix upgrade (#242)
Browse files Browse the repository at this point in the history
* side chain reward distribution fix upgrade
  • Loading branch information
fletcher142 authored Aug 21, 2020
1 parent 2c44892 commit 5b5d91f
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 7 deletions.
2 changes: 1 addition & 1 deletion types/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ func (c Context) WithBlockHeader(header abci.Header) Context {

func (c Context) WithBlockTime(newTime time.Time) Context {
newHeader := c.BlockHeader()
newHeader.Time = newTime
return c.WithBlockHeader(newHeader)
}

Expand Down Expand Up @@ -205,7 +206,6 @@ func (c Context) WithEventManager(em *EventManager) Context {
return c
}


func (c Context) WithSideChainKeyPrefix(prefix []byte) Context {
c.sideChainKeyPrefix = prefix
return c
Expand Down
13 changes: 7 additions & 6 deletions types/upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@ import "fmt"
var UpgradeMgr = NewUpgradeManager(UpgradeConfig{})

const (
FixSignBytesOverflow = "FixSignBytesOverflow" // fix json unmarshal overflow when build SignBytes
BEP9 = "BEP9" // https://github.com/binance-chain/BEPs/pull/9
BEP12 = "BEP12" // https://github.com/binance-chain/BEPs/pull/17
BEP3 = "BEP3" // https://github.com/binance-chain/BEPs/pull/30
BEP8 = "BEP8" // Mini-BEP2 token
LaunchBscUpgrade = "LaunchBscUpgrade"
FixSignBytesOverflow = "FixSignBytesOverflow" // fix json unmarshal overflow when build SignBytes
BEP9 = "BEP9" // https://github.com/binance-chain/BEPs/pull/9
BEP12 = "BEP12" // https://github.com/binance-chain/BEPs/pull/17
BEP3 = "BEP3" // https://github.com/binance-chain/BEPs/pull/30
BEP8 = "BEP8" // Mini-BEP2 token
LaunchBscUpgrade = "LaunchBscUpgrade"
FixSideChainRewardDistribution = "FixSideChainRewardDistribution"
)

var MainNetConfig = UpgradeConfig{
Expand Down
15 changes: 15 additions & 0 deletions x/stake/keeper/delegation.go
Original file line number Diff line number Diff line change
Expand Up @@ -476,6 +476,21 @@ func (k Keeper) SyncDelegationByValDel(ctx sdk.Context, valAddr sdk.ValAddress,
k.SetDelegationByVal(ctx, delegation)
}

func (k Keeper) SyncAllSelfDelegationsToValDel(ctx sdk.Context, sideChainId string) {
if scCtx, err := k.ScKeeper.PrepareCtxForSideChain(ctx, sideChainId); err != nil {
panic(err)
} else {
ctx = scCtx
}

validators := k.GetAllValidators(ctx)
if len(validators) > 0 {
for i := range validators {
k.SyncDelegationByValDel(ctx, validators[i].OperatorAddr, validators[i].FeeAddr)
}
}
}

// Perform a delegation, set/update everything necessary within the store.
func (k Keeper) Delegate(ctx sdk.Context, delAddr sdk.AccAddress, bondAmt sdk.Coin,
validator types.Validator, subtractAccount bool) (newShares sdk.Dec, err sdk.Error) {
Expand Down
8 changes: 8 additions & 0 deletions x/stake/keeper/reward.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,14 @@ func allocate(sharers []types.Sharer, totalRewards sdk.Dec, totalShares sdk.Dec)
var minToDistribute int64
var shouldCarry []types.Reward
var shouldNotCarry []types.Reward

if sdk.IsUpgrade(sdk.FixSideChainRewardDistribution) {
totalShares = sdk.ZeroDec()
for _, sharer := range sharers {
totalShares = totalShares.Add(sharer.Shares)
}
}

for _, sharer := range sharers {

afterRoundDown, firstDecimalValue := mulQuoDecWithExtraDecimal(sharer.Shares, totalRewards, totalShares, 1)
Expand Down
3 changes: 3 additions & 0 deletions x/stake/keeper/slash_sidechain.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ func (k Keeper) SlashSideChain(ctx sdk.Context, sideChainId string, sideConsAddr
return nil, sdk.ZeroDec(), errors.New(fmt.Sprintf("error unbonding delegator: %v", err))
}
remainingSlashAmount = remainingSlashAmount.Sub(unbondAmount)
if sdk.IsUpgrade(sdk.FixSideChainRewardDistribution) { // sync delegation changes(because of unbond operation) to the store with DelegationKeyByVal key based
k.SyncDelegationByValDel(sideCtx, validator.OperatorAddr, selfDelegation.DelegatorAddr)
}
}
}

Expand Down

0 comments on commit 5b5d91f

Please sign in to comment.