Skip to content

Commit

Permalink
[bugfix][protocol] make funding payment event ordering deterministic
Browse files Browse the repository at this point in the history
  • Loading branch information
lucas-dydx committed Jan 3, 2024
1 parent 3e2c518 commit 840da26
Showing 1 changed file with 11 additions and 1 deletion.
12 changes: 11 additions & 1 deletion protocol/x/subaccounts/keeper/subaccount.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"math/big"
"math/rand"
"sort"
"time"

errorsmod "cosmossdk.io/errors"
Expand Down Expand Up @@ -280,7 +281,16 @@ func (k Keeper) UpdateSubaccounts(
// Emit an event indicating a funding payment was paid / received for each settled funding
// payment. Note that `fundingPaid` is positive if the subaccount paid funding,
// and negative if the subaccount received funding.
for perpetualId, fundingPaid := range fundingPayments {
// Note the perpetual IDs are sorted first to ensure event emission determinism.
perpsWithFunding := make([]uint32, 0, len(fundingPayments))
for perpetualId := range fundingPayments {
perpsWithFunding = append(perpsWithFunding, perpetualId)
}
sort.Slice(perpsWithFunding, func(i, j int) bool {
return perpsWithFunding[i] < perpsWithFunding[j]
})
for _, perpetualId := range perpsWithFunding {
fundingPaid := fundingPayments[perpetualId]
ctx.EventManager().EmitEvent(
types.NewCreateSettledFundingEvent(
*u.SettledSubaccount.Id,
Expand Down

0 comments on commit 840da26

Please sign in to comment.