Skip to content

Commit

Permalink
fix comments
Browse files Browse the repository at this point in the history
  • Loading branch information
j75689 committed Nov 23, 2023
1 parent 3628661 commit c471bad
Show file tree
Hide file tree
Showing 7 changed files with 73 additions and 52 deletions.
2 changes: 1 addition & 1 deletion types/stake.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ type ValidatorSet interface {
ValidatorByConsAddr(Context, ConsAddress) Validator // get a particular validator by consensus address
ValidatorByVoteAddr(Context, []byte) Validator // get a particular validator by vote address
TotalPower(Context) Dec // total power of the validator set
GetSideChainTotalVotingPower(ctx Context) Dec // total voting power of the side chain validator set
GetTotalVotingPower(ctx Context) Dec // total voting power of the validator set

// slash the validator and delegators of the validator, specifying offence height, offence power, and slash fraction
Slash(Context, ConsAddress, int64, int64, Dec)
Expand Down
4 changes: 3 additions & 1 deletion x/gov/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,11 @@ func NewHandler(keeper Keeper) sdk.Handler {
case MsgVote:
return handleMsgVote(ctx, keeper, msg)
case MsgSideChainDeposit:
// TODO: refund the token to the depositor after second hard fork
return handleMsgSideChainDeposit(ctx, keeper, msg)
case MsgSideChainSubmitProposal:
if sdk.IsUpgrade(sdk.BCFusionSecondHardFork) {
return sdk.ErrMsgNotSupported("").Result()
}
return handleMsgSideChainSubmitProposal(ctx, keeper, msg)
case MsgSideChainVote:
return handleMsgSideChainVote(ctx, keeper, msg)
Expand Down
16 changes: 4 additions & 12 deletions x/gov/handler_sidechain.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,12 @@ import (
)

func handleMsgSideChainSubmitProposal(ctx sdk.Context, keeper Keeper, msg MsgSideChainSubmitProposal) sdk.Result {
if sdk.IsUpgrade(sdk.BCFusionSecondHardFork) {
return sdk.ErrMsgNotSupported("").Result()
ctx, err := keeper.ScKeeper.PrepareCtxForSideChain(ctx, msg.SideChainId)
if err != nil {
return ErrInvalidSideChainId(keeper.codespace, msg.SideChainId).Result()
}
if sdk.IsUpgrade(sdk.BCFusionFirstHardFork) {
ctx, err := keeper.ScKeeper.PrepareCtxForSideChain(ctx, msg.SideChainId)
if err != nil {
return ErrInvalidSideChainId(keeper.codespace, msg.SideChainId).Result()
}
vp := keeper.vs.GetSideChainTotalVotingPower(ctx)
vp := keeper.vs.GetTotalVotingPower(ctx)
if vp.LTE(sdk.NewDecFromInt(sdk.BCFusionStopGovThreshold)) {
return sdk.ErrMsgNotSupported("").Result()
}
Expand All @@ -24,11 +21,6 @@ func handleMsgSideChainSubmitProposal(ctx sdk.Context, keeper Keeper, msg MsgSid
return ErrInvalidProposalType(keeper.codespace, msg.ProposalType).Result()
}

ctx, err := keeper.ScKeeper.PrepareCtxForSideChain(ctx, msg.SideChainId)
if err != nil {
return ErrInvalidSideChainId(keeper.codespace, msg.SideChainId).Result()
}

result := handleMsgSubmitProposal(ctx, keeper,
NewMsgSubmitProposal(msg.Title, msg.Description, msg.ProposalType, msg.Proposer, msg.InitialDeposit,
msg.VotingPeriod))
Expand Down
47 changes: 31 additions & 16 deletions x/ibc/endblock.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,30 +34,45 @@ func closeSideChainChannels(ctx sdk.Context, k Keeper) sdk.Events {
sideChainId := k.sideKeeper.BscSideChainId(ctx)
// disable side chain channels
id := k.sideKeeper.Config().DestChainNameToID(sideChainId)

govChannelId := sdk.ChannelID(0x09)
for _, channelId := range k.sideKeeper.Config().ChannelIDs() {
if channelId == govChannelId {
// skip gov channel
continue
}
permissions := k.sideKeeper.GetChannelSendPermissions(ctx, id)
if permissions[channelId] == sdk.ChannelForbidden {
// skip forbidden channel
continue
}
_, err := k.sideKeeper.SaveChannelSettingChangeToIbc(ctx, id, 0, sdk.ChannelForbidden)
if err != nil {
ctx.Logger().Error("closeSideChainChannels", "err", err.Error())
events.AppendEvent(sdk.NewEvent("failed to closeSideChainChannels ",
sdk.NewAttribute("sideChainId", sideChainId),
sdk.NewAttribute("channelId", fmt.Sprint(channelId)),
sdk.NewAttribute("error", err.Error()),
))
return events
}
events.AppendEvent(sdk.NewEvent("closeSideChainChannels",
sdk.NewAttribute("sideChainId", sideChainId),
sdk.NewAttribute("channelId", fmt.Sprint(channelId)),

events = events.AppendEvents(saveChannelSetting(ctx, k, id, channelId, sdk.ChannelForbidden))
}

// disable side chain gov channel
events = events.AppendEvents(saveChannelSetting(ctx, k, id, govChannelId, sdk.ChannelForbidden))
return events
}

func saveChannelSetting(ctx sdk.Context, k Keeper,
destChainID sdk.ChainID, channelID sdk.ChannelID, permission sdk.ChannelPermission) sdk.Events {
var events sdk.Events
_, err := k.sideKeeper.SaveChannelSettingChangeToIbc(ctx, destChainID, channelID, sdk.ChannelForbidden)
if err != nil {
ctx.Logger().Error("closeSideChainChannels", "err", err.Error())
events.AppendEvent(sdk.NewEvent("failed to closeSideChainChannels ",
sdk.NewAttribute("sideChainId", fmt.Sprint(destChainID)),
sdk.NewAttribute("channelId", fmt.Sprint(channelID)),
sdk.NewAttribute("error", err.Error()),
))
// close bc side chain channel
k.sideKeeper.SetChannelSendPermission(ctx, id, channelId, sdk.ChannelForbidden)
return events
}
events.AppendEvent(sdk.NewEvent("closeSideChainChannels",
sdk.NewAttribute("sideChainId", fmt.Sprint(destChainID)),
sdk.NewAttribute("channelId", fmt.Sprint(channelID)),
))
// close bc side chain channel
k.sideKeeper.SetChannelSendPermission(ctx, destChainID, channelID, sdk.ChannelForbidden)

return events
}
42 changes: 24 additions & 18 deletions x/stake/endblock.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,8 @@ func EndBlocker(ctx sdk.Context, k keeper.Keeper) (validatorUpdates []abci.Valid
validatorUpdates = k.PopPendingABCIValidatorUpdate(ctx)
}

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

sideChainIds, storePrefixes := k.ScKeeper.GetAllSideChainPrefixes(ctx)
if sdk.IsUpgrade(sdk.BEP128) {
sideChainIds, storePrefixes := k.ScKeeper.GetAllSideChainPrefixes(ctx)
if len(sideChainIds) == len(storePrefixes) {
for i := range storePrefixes {
sideChainCtx := ctx.WithSideChainKeyPrefix(storePrefixes[i])
Expand All @@ -42,6 +36,20 @@ func EndBlocker(ctx sdk.Context, k keeper.Keeper) (validatorUpdates []abci.Valid
panic("sideChainIds does not equal to sideChainStores")
}
}

if sdk.IsUpgrade(sdk.BCFusionSecondHardFork) {
for i := range storePrefixes {
sideChainCtx := ctx.WithSideChainKeyPrefix(storePrefixes[i])
refundEvents.AppendEvents(handleRefundStake(sideChainCtx, k))
validatorUpdates, completedUbds, events = endRefundBlock(ctx, k)
refundEvents.AppendEvents(events)
}

refundEvents.AppendEvents(handleRefundStake(ctx, k))
validatorUpdates, completedUbds, events = endRefundBlock(ctx, k)
refundEvents.AppendEvents(events)
}

if sdk.IsUpgrade(sdk.BEP153) {
events = events.AppendEvents(csEvents)
}
Expand Down Expand Up @@ -268,10 +276,10 @@ func handleMatureUnbondingDelegations(k keeper.Keeper, ctx sdk.Context) ([]types
}

const (
maxProcessedRefundCount = 50
maxProcessedRefundCount = 10
)

func handleRefundCrossStake(ctx sdk.Context, k keeper.Keeper) sdk.Events {
func handleRefundStake(ctx sdk.Context, k keeper.Keeper) sdk.Events {
iterator := k.IteratorAllDelegations(ctx)
defer iterator.Close()
var refundEvents sdk.Events
Expand All @@ -284,22 +292,20 @@ func handleRefundCrossStake(ctx sdk.Context, k keeper.Keeper) sdk.Events {
for ; iterator.Valid(); iterator.Next() {
delegation := types.MustUnmarshalDelegation(k.CDC(), iterator.Key(), iterator.Value())
if delegation.CrossStake {
result := handleMsgSideChainUndelegate(ctx, types.MsgSideChainUndelegate{
result := handleMsgSideChainUndelegate(ctx.WithCrossStake(true), types.MsgSideChainUndelegate{
DelegatorAddr: delegation.DelegatorAddr,
ValidatorAddr: delegation.ValidatorAddr,
Amount: sdk.NewCoin(k.BondDenom(ctx), delegation.GetShares().RawInt()),
SideChainId: k.ScKeeper.BscSideChainId(ctx),
}, k)
refundEvents = refundEvents.AppendEvents(result.Events)
} else {
if _, exist := bcValidatorsMap[delegation.DelegatorAddr.String()]; !exist {
result := handleMsgUndelegate(ctx, types.MsgUndelegate{
DelegatorAddr: delegation.DelegatorAddr,
ValidatorAddr: delegation.ValidatorAddr,
Amount: sdk.NewCoin(k.BondDenom(ctx), delegation.GetShares().RawInt()),
}, k)
refundEvents = refundEvents.AppendEvents(result.Events)
}
result := handleMsgUndelegate(ctx, types.MsgUndelegate{
DelegatorAddr: delegation.DelegatorAddr,
ValidatorAddr: delegation.ValidatorAddr,
Amount: sdk.NewCoin(k.BondDenom(ctx), delegation.GetShares().RawInt()),
}, k)
refundEvents = refundEvents.AppendEvents(result.Events)
}
count++
if count >= maxProcessedRefundCount {
Expand Down
12 changes: 9 additions & 3 deletions x/stake/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ func NewHandler(k keeper.Keeper, govKeeper gov.Keeper) sdk.Handler {
return handleMsgRemoveValidatorAfterProposal(ctx, msg, k, govKeeper)
// Beacon Chain New Staking in BEP-159
case types.MsgCreateValidatorOpen:
if sdk.IsUpgrade(sdk.BCFusionFirstHardFork) {
return sdk.ErrMsgNotSupported("").Result()
}
if !sdk.IsUpgrade(sdk.BEP159Phase2) {
return sdk.ErrMsgNotSupported("BEP-159 Phase 2 not activated yet").Result()
}
Expand Down Expand Up @@ -83,8 +86,14 @@ func NewStakeHandler(k Keeper) sdk.Handler {
case types.MsgEditValidator:
return handleMsgEditValidator(ctx, msg, k)
case types.MsgDelegate:
if sdk.IsUpgrade(sdk.BCFusionSecondHardFork) {
return sdk.ErrMsgNotSupported("").Result()
}
return handleMsgDelegate(ctx, msg, k)
case types.MsgRedelegate:
if sdk.IsUpgrade(sdk.BCFusionSecondHardFork) {
return sdk.ErrMsgNotSupported("").Result()
}
return handleMsgRedelegate(ctx, msg, k)
case types.MsgBeginUnbonding:
return handleMsgBeginUnbonding(ctx, msg, k)
Expand Down Expand Up @@ -477,9 +486,6 @@ func handleMsgBeginUnbonding(ctx sdk.Context, msg types.MsgBeginUnbonding, k kee
}

func handleMsgRedelegate(ctx sdk.Context, msg types.MsgRedelegate, k keeper.Keeper) sdk.Result {
if sdk.IsUpgrade(sdk.BCFusionSecondHardFork) {
return sdk.ErrMsgNotSupported("").Result()
}
if msg.Amount.Denom != k.BondDenom(ctx) {
return ErrBadDenom(k.Codespace()).Result()
}
Expand Down
2 changes: 1 addition & 1 deletion x/stake/keeper/validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ func (k Keeper) GetSideChainTotalVotingPower(ctx sdk.Context) sdk.Dec {
address := iterator.Value()
validator, found := k.GetValidator(ctx, address)
if !found {
ctx.Logger().Error("GetSideChainTotalVotingPower can't load validator", "operator_addr", string(address))
ctx.Logger().Error("can't load validator", "operator_addr", string(address))
continue
}
votingPower = votingPower.Add(validator.GetPower())
Expand Down

0 comments on commit c471bad

Please sign in to comment.