Skip to content

Commit

Permalink
fix: abci begin block execution order
Browse files Browse the repository at this point in the history
  • Loading branch information
ironman0x7b2 committed Jul 15, 2024
1 parent fafe609 commit a368da1
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 37 deletions.
23 changes: 13 additions & 10 deletions x/lease/keeper/abci.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import (

func (k *Keeper) handleInactiveLeases(ctx sdk.Context) {
k.IterateLeasesForInactiveAt(ctx, ctx.BlockTime(), func(_ int, item v1.Lease) bool {
k.DeleteLeaseForInactiveAt(ctx, item.InactiveAt, item.ID)

msg := &v1.MsgEndLeaseRequest{
From: item.ProvAddress,
ID: item.ID,
Expand All @@ -30,25 +32,24 @@ func (k *Keeper) handleLeasePayouts(ctx sdk.Context) {
share := k.StakingShare(ctx)

k.IterateLeasesForPayoutAt(ctx, ctx.BlockTime(), func(_ int, item v1.Lease) bool {
k.DeleteLeaseForPayoutAt(ctx, item.PayoutAt, item.ID)

reward := baseutils.GetProportionOfCoin(item.Price, share)
payment := item.Price.Sub(reward)

provAddr, err := base.ProvAddressFromBech32(item.ProvAddress)
if err != nil {
panic(err)
}

if err := k.SendCoinFromDepositToModule(ctx, provAddr.Bytes(), k.feeCollectorName, reward); err != nil {
nodeAddr, err := base.NodeAddressFromBech32(item.NodeAddress)
if err != nil {
panic(err)
}

nodeAddr, err := base.NodeAddressFromBech32(item.NodeAddress)
if err != nil {
k.DeleteLeaseForPayoutAt(ctx, item.PayoutAt, item.ID)

reward := baseutils.GetProportionOfCoin(item.Price, share)
if err := k.SendCoinFromDepositToModule(ctx, provAddr.Bytes(), k.feeCollectorName, reward); err != nil {
panic(err)
}

payment := item.Price.Sub(reward)
if err := k.SendCoinFromDepositToAccount(ctx, provAddr.Bytes(), nodeAddr.Bytes(), payment); err != nil {
panic(err)
}
Expand Down Expand Up @@ -89,6 +90,8 @@ func (k *Keeper) handleLeasePayouts(ctx sdk.Context) {

func (k *Keeper) handleLeaseRenewals(ctx sdk.Context) {
k.IterateLeasesForRenewalAt(ctx, ctx.BlockTime(), func(_ int, item v1.Lease) bool {
k.DeleteLeaseForRenewalAt(ctx, item.RenewalAt(), item.ID)

msg := &v1.MsgRenewLeaseRequest{
From: item.ProvAddress,
ID: item.ID,
Expand All @@ -106,7 +109,7 @@ func (k *Keeper) handleLeaseRenewals(ctx sdk.Context) {
}

func (k *Keeper) BeginBlock(ctx sdk.Context) {
k.handleInactiveLeases(ctx)
k.handleLeasePayouts(ctx)
k.handleLeaseRenewals(ctx)
k.handleLeasePayouts(ctx)
k.handleInactiveLeases(ctx)
}
8 changes: 8 additions & 0 deletions x/node/keeper/abci.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,21 @@ package keeper
import (
sdk "github.com/cosmos/cosmos-sdk/types"

base "github.com/sentinel-official/hub/v12/types"
v1base "github.com/sentinel-official/hub/v12/types/v1"
"github.com/sentinel-official/hub/v12/x/node/types/v2"
"github.com/sentinel-official/hub/v12/x/node/types/v3"
)

func (k *Keeper) handleInactiveNodes(ctx sdk.Context) {
k.IterateNodesForInactiveAt(ctx, ctx.BlockTime(), func(_ int, item v2.Node) bool {
nodeAddr, err := base.NodeAddressFromBech32(item.Address)
if err != nil {
panic(err)
}

k.DeleteNodeForInactiveAt(ctx, item.InactiveAt, nodeAddr)

msg := &v3.MsgUpdateNodeStatusRequest{
From: item.Address,
Status: v1base.StatusInactive,
Expand Down
29 changes: 16 additions & 13 deletions x/node/keeper/hooks.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,42 +25,45 @@ func (k *Keeper) SessionInactivePreHook(ctx sdk.Context, id uint64) error {
return nil
}

payment := session.PaymentAmount()
share := k.StakingShare(ctx)

accAddr, err := sdk.AccAddressFromBech32(session.AccAddress)
if err != nil {
return err
}

reward := baseutils.GetProportionOfCoin(payment, share)
if err := k.SendCoinFromDepositToModule(ctx, accAddr, k.feeCollectorName, reward); err != nil {
return err
}

nodeAddr, err := base.NodeAddressFromBech32(session.NodeAddress)
if err != nil {
return err
}

payment = payment.Sub(reward)
if err := k.SendCoinFromDepositToAccount(ctx, accAddr, nodeAddr.Bytes(), payment); err != nil {
amount := session.PaymentAmount()
share := k.StakingShare(ctx)

reward := baseutils.GetProportionOfCoin(amount, share)
if err := k.SendCoinFromDepositToModule(ctx, accAddr, k.feeCollectorName, reward); err != nil {
return err
}

refund := session.RefundAmount()
if err := k.SubtractDeposit(ctx, accAddr, refund); err != nil {
payment := amount.Sub(reward)
if err := k.SendCoinFromDepositToAccount(ctx, accAddr, nodeAddr.Bytes(), payment); err != nil {
return err
}

ctx.EventManager().EmitTypedEvents(
ctx.EventManager().EmitTypedEvent(
&v3.EventPay{
ID: session.ID,
AccAddress: session.AccAddress,
NodeAddress: session.NodeAddress,
Payment: payment.String(),
StakingReward: reward.String(),
},
)

refund := session.RefundAmount()
if err := k.SubtractDeposit(ctx, accAddr, refund); err != nil {
return err
}

ctx.EventManager().EmitTypedEvent(
&v3.EventRefund{
ID: session.ID,
AccAddress: session.AccAddress,
Expand Down
13 changes: 8 additions & 5 deletions x/session/keeper/abci.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ func (k *Keeper) handleInactivePendingSessions(ctx sdk.Context) {
return false
}

k.DeleteSessionForInactiveAt(ctx, item.GetInactiveAt(), item.GetID())

msg := &v3.MsgCancelSessionRequest{
From: item.GetAccAddress(),
ID: item.GetID(),
Expand All @@ -34,10 +36,6 @@ func (k *Keeper) handleInactiveSessions(ctx sdk.Context) {
return false
}

if err := k.SessionInactivePreHook(ctx, item.GetID()); err != nil {
panic(err)
}

accAddr, err := sdk.AccAddressFromBech32(item.GetAccAddress())
if err != nil {
panic(err)
Expand All @@ -48,12 +46,17 @@ func (k *Keeper) handleInactiveSessions(ctx sdk.Context) {
panic(err)
}

k.DeleteSessionForInactiveAt(ctx, item.GetInactiveAt(), item.GetID())

if err := k.SessionInactivePreHook(ctx, item.GetID()); err != nil {
panic(err)
}

k.DeleteSession(ctx, item.GetID())
k.DeleteSessionForAccount(ctx, accAddr, item.GetID())
k.DeleteSessionForAllocation(ctx, 0, accAddr, item.GetID())
k.DeleteSessionForNode(ctx, nodeAddr, item.GetID())
k.DeleteSessionForSubscription(ctx, 0, item.GetID())
k.DeleteSessionForInactiveAt(ctx, item.GetInactiveAt(), item.GetID())

ctx.EventManager().EmitTypedEvent(
&v3.EventUpdateStatus{
Expand Down
15 changes: 10 additions & 5 deletions x/subscription/keeper/abci.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ func (k *Keeper) handleInactivePendingSubscriptions(ctx sdk.Context) {
return false
}

k.DeleteSubscriptionForInactiveAt(ctx, item.InactiveAt, item.ID)

msg := &v3.MsgCancelSubscriptionRequest{
From: item.AccAddress,
ID: item.ID,
Expand All @@ -34,6 +36,11 @@ func (k *Keeper) handleInactiveSubscriptions(ctx sdk.Context) {
return false
}

k.DeleteSubscriptionForInactiveAt(ctx, item.InactiveAt, item.ID)

k.DeleteSubscription(ctx, item.ID)
k.DeleteSubscriptionForPlan(ctx, item.PlanID, item.ID)

k.IterateAllocationsForSubscription(ctx, item.ID, func(_ int, item v2.Allocation) bool {
accAddr, err := sdk.AccAddressFromBech32(item.Address)
if err != nil {
Expand All @@ -46,10 +53,6 @@ func (k *Keeper) handleInactiveSubscriptions(ctx sdk.Context) {
return false
})

k.DeleteSubscription(ctx, item.ID)
k.DeleteSubscriptionForPlan(ctx, item.PlanID, item.ID)
k.DeleteSubscriptionForInactiveAt(ctx, item.InactiveAt, item.ID)

ctx.EventManager().EmitTypedEvent(
&v3.EventUpdate{
ID: item.ID,
Expand All @@ -66,6 +69,8 @@ func (k *Keeper) handleInactiveSubscriptions(ctx sdk.Context) {

func (k *Keeper) handleSubscriptionRenewals(ctx sdk.Context) {
k.IterateSubscriptionsForRenewalAt(ctx, ctx.BlockTime(), func(_ int, item v3.Subscription) bool {
k.DeleteSubscriptionForRenewalAt(ctx, item.RenewalAt(), item.ID)

msg := &v3.MsgRenewSubscriptionRequest{
From: item.AccAddress,
ID: item.ID,
Expand All @@ -82,7 +87,7 @@ func (k *Keeper) handleSubscriptionRenewals(ctx sdk.Context) {
}

func (k *Keeper) BeginBlock(ctx sdk.Context) {
k.handleSubscriptionRenewals(ctx)
k.handleInactivePendingSubscriptions(ctx)
k.handleInactiveSubscriptions(ctx)
k.handleSubscriptionRenewals(ctx)
}
9 changes: 5 additions & 4 deletions x/vpn/keeper/abci.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@ import (
sdk "github.com/cosmos/cosmos-sdk/types"
)

func cacheContext(c sdk.Context) (cc sdk.Context, writeCache func()) {
cms := c.MultiStore().CacheMultiStore()
cc = c.WithMultiStore(cms)
return cc, cms.Write
func cacheContext(ctx sdk.Context) (c sdk.Context, write func()) {
cms := ctx.MultiStore().CacheMultiStore()
c = ctx.WithMultiStore(cms)

return c, cms.Write
}

func (k *Keeper) BeginBlock(ctx sdk.Context) {
Expand Down

0 comments on commit a368da1

Please sign in to comment.