Skip to content

Commit

Permalink
fix: refund delegation in endBlock
Browse files Browse the repository at this point in the history
  • Loading branch information
j75689 committed Nov 20, 2023
1 parent cd2eebc commit 3628661
Showing 1 changed file with 46 additions and 3 deletions.
49 changes: 46 additions & 3 deletions x/stake/endblock.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,20 @@ func EndBlocker(ctx sdk.Context, k keeper.Keeper) (validatorUpdates []abci.Valid
csEvents sdk.Events
refundEvents sdk.Events
)
if sdk.IsUpgrade(sdk.BCFusionSecondHardFork) {
refundEvents = handleRefundCrossStake(ctx, k)
}

if !sdk.IsUpgrade(sdk.BEP159) {
_, validatorUpdates, completedUbds, _, events = handleValidatorAndDelegations(ctx, k)
} else {
k.DistributeInBlock(ctx, types.ChainIDForBeaconChain)
validatorUpdates = k.PopPendingABCIValidatorUpdate(ctx)
}

if sdk.IsUpgrade(sdk.BCFusionSecondHardFork) {
refundEvents = handleRefundCrossStake(ctx, k)
validatorUpdates, completedUbds, events = endRefundBlock(ctx, k)
refundEvents.AppendEvents(events)
}

if sdk.IsUpgrade(sdk.BEP128) {
sideChainIds, storePrefixes := k.ScKeeper.GetAllSideChainPrefixes(ctx)
if len(sideChainIds) == len(storePrefixes) {
Expand Down Expand Up @@ -304,3 +308,42 @@ func handleRefundCrossStake(ctx sdk.Context, k keeper.Keeper) sdk.Events {
}
return refundEvents
}

func endRefundBlock(ctx sdk.Context, k keeper.Keeper) (validatorUpdates []abci.ValidatorUpdate, completedUbds []types.UnbondingDelegation, events sdk.Events) {
var newVals []types.Validator
var completedREDs []types.DVVTriplet
newVals, validatorUpdates, completedUbds, completedREDs, events = handleValidatorAndDelegations(ctx, k)
ctx.Logger().Debug("endRefundBlock", "newValsLen", len(newVals), "newVals", newVals)
publishCompletedUBD(k, completedUbds, ChainIDForBeaconChain, ctx.BlockHeight())
publishCompletedRED(k, completedREDs, ChainIDForBeaconChain)
if k.PbsbServer != nil {
sideValidatorsEvent := types.ElectedValidatorsEvent{
Validators: newVals,
ChainId: ChainIDForBeaconChain,
}
k.PbsbServer.Publish(sideValidatorsEvent)
}
if sdk.IsUpgrade(sdk.BEP159) {
storeValidatorsWithHeight(ctx, newVals, k)
}

if sdk.IsUpgrade(sdk.LaunchBscUpgrade) && k.ScKeeper != nil {
// distribute sidechain rewards
sideChainIds, storePrefixes := k.ScKeeper.GetAllSideChainPrefixes(ctx)
for i := range storePrefixes {
sideChainCtx := ctx.WithSideChainKeyPrefix(storePrefixes[i])
newVals, _, completedUbds, completedREDs, scEvents := handleValidatorAndDelegations(sideChainCtx, k)
for j := range scEvents {
scEvents[j] = scEvents[j].AppendAttributes(sdk.NewAttribute(types.AttributeKeySideChainId, sideChainIds[i]))
}
events = events.AppendEvents(scEvents)
// TODO: need to add UBDs for side chains to the return value

storeValidatorsWithHeight(sideChainCtx, newVals, k)

publishCompletedUBD(k, completedUbds, sideChainIds[i], ctx.BlockHeight())
publishCompletedRED(k, completedREDs, sideChainIds[i])
}
}
return
}

0 comments on commit 3628661

Please sign in to comment.