Skip to content

Commit

Permalink
Merge pull request #35 from initia-labs/bug/can-delete-finalized-output
Browse files Browse the repository at this point in the history
delete output proposal bug
  • Loading branch information
beer-1 authored Mar 25, 2024
2 parents 9061c8f + 6062b27 commit 8e6f980
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 2 deletions.
14 changes: 14 additions & 0 deletions x/ophost/keeper/output.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 isFinalized, err := k.isFinalized(ctx, bridgeId, output); err != nil {
return err
} else if isFinalized {
return types.ErrAlreadyFinalized
}

return k.OutputProposals.Remove(ctx, collections.Join(bridgeId, outputIndex))
}

Expand All @@ -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
Expand Down
30 changes: 30 additions & 0 deletions x/ophost/keeper/output_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
5 changes: 3 additions & 2 deletions x/ophost/types/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
)

0 comments on commit 8e6f980

Please sign in to comment.