From b5f8449bff0645f176daebf91fd51fb6464025ff Mon Sep 17 00:00:00 2001 From: hoon Date: Fri, 22 Mar 2024 14:29:22 +0900 Subject: [PATCH 1/2] delete output proposal only if it is not finalized --- x/ophost/keeper/output.go | 14 ++++++++++++++ x/ophost/types/error.go | 5 +++-- 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/x/ophost/keeper/output.go b/x/ophost/keeper/output.go index 8e2d88da..75d99f6e 100644 --- a/x/ophost/keeper/output.go +++ b/x/ophost/keeper/output.go @@ -22,6 +22,16 @@ func (k Keeper) GetOutputProposal(ctx context.Context, bridgeId, outputIndex uin } func (k Keeper) DeleteOutputProposal(ctx context.Context, bridgeId, outputIndex uint64) error { + output, err := k.GetOutputProposal(ctx, bridgeId, outputIndex) + if err != nil { + return err + } + if isFinal, err := k.isFinalized(ctx, bridgeId, output); err != nil { + return err + } else if isFinal { + return types.ErrAlreadyFinalized + } + return k.OutputProposals.Remove(ctx, collections.Join(bridgeId, outputIndex)) } @@ -35,6 +45,10 @@ func (k Keeper) IsFinalized(ctx context.Context, bridgeId, outputIndex uint64) ( return false, err } + return k.isFinalized(ctx, bridgeId, output) +} + +func (k Keeper) isFinalized(ctx context.Context, bridgeId uint64, output types.Output) (bool, error) { bridgeConfig, err := k.GetBridgeConfig(ctx, bridgeId) if err != nil { return false, err diff --git a/x/ophost/types/error.go b/x/ophost/types/error.go index fcc93b3f..0ea7261e 100644 --- a/x/ophost/types/error.go +++ b/x/ophost/types/error.go @@ -13,6 +13,7 @@ var ( ErrInvalidSequence = errorsmod.Register(ModuleName, 6, "invalid sequence") ErrInvalidL2BlockNumber = errorsmod.Register(ModuleName, 7, "invalid l2 block number") ErrNotFinalized = errorsmod.Register(ModuleName, 8, "output has not finalized") - ErrFailedToVerifyWithdrawal = errorsmod.Register(ModuleName, 9, "failed to verify withdrawal tx") - ErrWithdrawalAlreadyFinalized = errorsmod.Register(ModuleName, 10, "withdrawal already finalized") + ErrAlreadyFinalized = errorsmod.Register(ModuleName, 9, "output has already finalized") + ErrFailedToVerifyWithdrawal = errorsmod.Register(ModuleName, 10, "failed to verify withdrawal tx") + ErrWithdrawalAlreadyFinalized = errorsmod.Register(ModuleName, 11, "withdrawal already finalized") ) From 6062b278aff348c9962b505b8c5555c9c4bdfb1e Mon Sep 17 00:00:00 2001 From: beer-1 <147697694+beer-1@users.noreply.github.com> Date: Mon, 25 Mar 2024 12:06:04 +0900 Subject: [PATCH 2/2] add tests --- x/ophost/keeper/output.go | 4 ++-- x/ophost/keeper/output_test.go | 30 ++++++++++++++++++++++++++++++ 2 files changed, 32 insertions(+), 2 deletions(-) diff --git a/x/ophost/keeper/output.go b/x/ophost/keeper/output.go index 75d99f6e..50db2a6c 100644 --- a/x/ophost/keeper/output.go +++ b/x/ophost/keeper/output.go @@ -26,9 +26,9 @@ func (k Keeper) DeleteOutputProposal(ctx context.Context, bridgeId, outputIndex if err != nil { return err } - if isFinal, err := k.isFinalized(ctx, bridgeId, output); err != nil { + if isFinalized, err := k.isFinalized(ctx, bridgeId, output); err != nil { return err - } else if isFinal { + } else if isFinalized { return types.ErrAlreadyFinalized } diff --git a/x/ophost/keeper/output_test.go b/x/ophost/keeper/output_test.go index 762c4d45..11d3cdd1 100644 --- a/x/ophost/keeper/output_test.go +++ b/x/ophost/keeper/output_test.go @@ -130,3 +130,33 @@ func Test_NextOutputIndex(t *testing.T) { require.NoError(t, err) require.Equal(t, uint64(101), index) } + +func Test_DeleteOutputProposal(t *testing.T) { + ctx, input := createDefaultTestInput(t) + + output := types.Output{ + OutputRoot: []byte{1, 2, 3}, + L1BlockTime: ctx.BlockTime(), + L2BlockNumber: 100, + } + err := input.OPHostKeeper.SetOutputProposal(ctx, 1, 1, output) + require.NoError(t, err) + + err = input.OPHostKeeper.SetBridgeConfig(ctx, 1, types.BridgeConfig{ + Challenger: "", + Proposer: "", + SubmissionInterval: time.Second * 10, + FinalizationPeriod: time.Second * 10, + SubmissionStartTime: ctx.BlockTime(), + Metadata: nil, + }) + require.NoError(t, err) + + // delete should fail due to already finalized error + err = input.OPHostKeeper.DeleteOutputProposal(ctx.WithBlockTime(ctx.BlockTime().Add(time.Second*11)), 1, 1) + require.ErrorIs(t, err, types.ErrAlreadyFinalized) + + // delete should success + err = input.OPHostKeeper.DeleteOutputProposal(ctx.WithBlockTime(ctx.BlockTime().Add(time.Second*9)), 1, 1) + require.NoError(t, err) +}