Skip to content

Commit

Permalink
prevent updating zeta accounting twice during genesis
Browse files Browse the repository at this point in the history
  • Loading branch information
kingpinXD committed Dec 4, 2024
1 parent 40fade6 commit b2c1b32
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 8 deletions.
5 changes: 4 additions & 1 deletion x/crosschain/genesis.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package crosschain

import (
sdkmath "cosmossdk.io/math"
sdk "github.com/cosmos/cosmos-sdk/types"

"github.com/zeta-chain/node/x/crosschain/keeper"
Expand All @@ -10,7 +11,9 @@ import (
// InitGenesis initializes the crosschain module's state from a provided genesis
// state.
func InitGenesis(ctx sdk.Context, k keeper.Keeper, genState types.GenesisState) {
k.SetZetaAccounting(ctx, genState.ZetaAccounting)
// Always set the zeta accounting to zero at genesis.
// ZetaAccounting value is build by iterating through all the cctxs and adding the amount to the zeta accounting.
k.SetZetaAccounting(ctx, types.ZetaAccounting{AbortedZetaAmount: sdkmath.ZeroUint()})
// Set all the outbound tracker
for _, elem := range genState.OutboundTrackerList {
k.SetOutboundTracker(ctx, elem)
Expand Down
5 changes: 4 additions & 1 deletion x/crosschain/keeper/cctx.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,14 @@ func (k Keeper) updateInboundHashToCCTX(
k.SetInboundHashToCctx(ctx, in)
}

// updateZetaAccounting updates the zeta accounting with the amount of zeta that was locked in an aborted cctx
func (k Keeper) updateZetaAccounting(
ctx sdk.Context,
cctx types.CrossChainTx,
) {
if cctx.CctxStatus.Status == types.CctxStatus_Aborted && cctx.InboundParams.CoinType == coin.CoinType_Zeta {
if cctx.CctxStatus.Status == types.CctxStatus_Aborted &&
cctx.InboundParams.CoinType == coin.CoinType_Zeta &&
cctx.CctxStatus.IsAbortRefunded == false {
k.AddZetaAbortedAmount(ctx, GetAbortedAmount(cctx))
}
}
Expand Down
40 changes: 34 additions & 6 deletions x/crosschain/keeper/cctx_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -602,8 +602,10 @@ func TestKeeper_UpdateZetaAccounting(t *testing.T) {
k, ctx, _, _ := keepertest.CrosschainKeeper(t)
amount := sdkmath.NewUint(100)
cctx := types.CrossChainTx{
InboundParams: &types.InboundParams{CoinType: coin.CoinType_Zeta},
CctxStatus: &types.Status{Status: types.CctxStatus_Aborted},
InboundParams: &types.InboundParams{CoinType: coin.CoinType_Zeta},
CctxStatus: &types.Status{
IsAbortRefunded: false,
Status: types.CctxStatus_Aborted},
OutboundParams: []*types.OutboundParams{{Amount: amount}},
}
k.SetZetaAccounting(ctx, types.ZetaAccounting{AbortedZetaAmount: math.ZeroUint()})
Expand All @@ -622,8 +624,10 @@ func TestKeeper_UpdateZetaAccounting(t *testing.T) {
k, ctx, _, _ := keepertest.CrosschainKeeper(t)
amount := sdkmath.NewUint(100)
cctx := types.CrossChainTx{
InboundParams: &types.InboundParams{CoinType: coin.CoinType_Zeta},
CctxStatus: &types.Status{Status: types.CctxStatus_PendingOutbound},
InboundParams: &types.InboundParams{CoinType: coin.CoinType_Zeta},
CctxStatus: &types.Status{
IsAbortRefunded: false,
Status: types.CctxStatus_PendingOutbound},
OutboundParams: []*types.OutboundParams{{Amount: amount}},
}
k.SetZetaAccounting(ctx, types.ZetaAccounting{AbortedZetaAmount: math.ZeroUint()})
Expand All @@ -642,8 +646,10 @@ func TestKeeper_UpdateZetaAccounting(t *testing.T) {
k, ctx, _, _ := keepertest.CrosschainKeeper(t)
amount := sdkmath.NewUint(100)
cctx := types.CrossChainTx{
InboundParams: &types.InboundParams{CoinType: coin.CoinType_Zeta},
CctxStatus: &types.Status{Status: types.CctxStatus_Aborted},
InboundParams: &types.InboundParams{CoinType: coin.CoinType_Zeta},
CctxStatus: &types.Status{
IsAbortRefunded: false,
Status: types.CctxStatus_Aborted},
OutboundParams: []*types.OutboundParams{{Amount: amount}},
}

Expand All @@ -655,4 +661,26 @@ func TestKeeper_UpdateZetaAccounting(t *testing.T) {
require.True(t, found)
require.Equal(t, amount, zetaAccounting.AbortedZetaAmount)
})

t.Run("should not update zeta accounting if the cctx is already refunded", func(t *testing.T) {
// Arrange
k, ctx, _, _ := keepertest.CrosschainKeeper(t)
amount := sdkmath.NewUint(100)
cctx := types.CrossChainTx{
InboundParams: &types.InboundParams{CoinType: coin.CoinType_Zeta},
CctxStatus: &types.Status{
IsAbortRefunded: true,
Status: types.CctxStatus_Aborted},
OutboundParams: []*types.OutboundParams{{Amount: amount}},
}
k.SetZetaAccounting(ctx, types.ZetaAccounting{AbortedZetaAmount: math.ZeroUint()})

// Act
k.UpdateZetaAccounting(ctx, cctx)

// Assert
zetaAccounting, found := k.GetZetaAccounting(ctx)
require.True(t, found)
require.Equal(t, math.ZeroUint(), zetaAccounting.AbortedZetaAmount)
})
}

0 comments on commit b2c1b32

Please sign in to comment.