Skip to content

Commit

Permalink
implement withdrawals listing by height
Browse files Browse the repository at this point in the history
  • Loading branch information
chmllr committed Feb 19, 2025
1 parent 5de8bd7 commit f34bcd8
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 7 deletions.
32 changes: 32 additions & 0 deletions octane/evmengine/keeper/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package keeper
import (
"bytes"
"context"
"sort"

"github.com/omni-network/omni/lib/cast"
"github.com/omni-network/omni/lib/errors"
Expand Down Expand Up @@ -113,3 +114,34 @@ func (k *Keeper) listWithdrawalsByAddress(ctx context.Context, withdrawalAddr co

return withdrawals, nil
}

// ListWithdrawalsBelowHeight returns all withdrawals created below the specified height, sorted by the created height, limited by the provided count.
func (k *Keeper) ListWithdrawalsBelowHeight(ctx context.Context, height uint64, limit int) ([]*Withdrawal, error) {
iter, err := k.withdrawalTable.List(ctx, WithdrawalPrimaryKey{})
if err != nil {
return nil, errors.Wrap(err, "list withdrawals")
}
defer iter.Close()

var withdrawals []*Withdrawal
for iter.Next() {
val, err := iter.Value()
if err != nil {
return nil, errors.Wrap(err, "get withdrawal")
}

if val.GetCreatedHeight() < height {
withdrawals = append(withdrawals, val)
}
}

sort.Slice(withdrawals, func(i, j int) bool {
return withdrawals[i].GetCreatedHeight() < withdrawals[j].GetCreatedHeight()
})

if len(withdrawals) > limit {
withdrawals = withdrawals[:limit]
}

return withdrawals, nil
}
58 changes: 51 additions & 7 deletions octane/evmengine/keeper/db_internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,16 +43,20 @@ func TestKeeper_withdrawalsPersistence(t *testing.T) {

addr1 := tutil.RandomAddress()
addr2 := tutil.RandomAddress()
addr3 := tutil.RandomAddress()

inputs := []struct {
type testCase struct {
addr common.Address
height uint64
amount uint64
expID uint64
}{
}

inputs := []testCase{
{addr1, 1, 777, 1},
{addr2, 2, 8888, 2},
{addr1, 100, 9999999, 3},
{addr3, 120, 10000000, 4},
}

for _, in := range inputs {
Expand All @@ -63,13 +67,17 @@ func TestKeeper_withdrawalsPersistence(t *testing.T) {

withdrawals, err := getAllWithdrawals(ctx, keeper)
require.NoError(t, err)
require.Len(t, withdrawals, 3)
require.Len(t, withdrawals, len(inputs))

matchesTestCase := func(w *Withdrawal, in testCase) {
require.Equal(t, in.expID, w.GetId())
require.Equal(t, in.addr.Bytes(), w.GetAddress())
require.Equal(t, in.amount, w.GetAmountGwei())
require.Equal(t, in.height, w.GetCreatedHeight())
}

for i, in := range inputs {
require.Equal(t, in.expID, withdrawals[i].GetId())
require.Equal(t, in.addr.Bytes(), withdrawals[i].GetAddress())
require.Equal(t, in.amount, withdrawals[i].GetAmountGwei())
require.Equal(t, in.height, withdrawals[i].GetCreatedHeight())
matchesTestCase(withdrawals[i], in)
}

withdrawalsByAddr, err := keeper.listWithdrawalsByAddress(ctx, addr1)
Expand All @@ -85,6 +93,42 @@ func TestKeeper_withdrawalsPersistence(t *testing.T) {
withdrawalsByAddr, err = keeper.listWithdrawalsByAddress(ctx, tutil.RandomAddress())
require.NoError(t, err)
require.Empty(t, withdrawalsByAddr)

limit := 4

// make sure we have no withdrawals below height 1
withdrawalsByHeight, err := keeper.ListWithdrawalsBelowHeight(ctx, 1, limit)
require.NoError(t, err)
require.Empty(t, withdrawalsByHeight)

// make sure we have exactly one withdrawal below height 2
withdrawalsByHeight, err = keeper.ListWithdrawalsBelowHeight(ctx, 2, limit)
require.NoError(t, err)
require.Len(t, withdrawalsByHeight, 1)
matchesTestCase(withdrawalsByHeight[0], inputs[0])

// under height 50 we only have 2 withdrawals
withdrawalsByHeight, err = keeper.ListWithdrawalsBelowHeight(ctx, 50, limit)
require.NoError(t, err)
require.Len(t, withdrawalsByHeight, 2)
matchesTestCase(withdrawalsByHeight[0], inputs[0])
matchesTestCase(withdrawalsByHeight[1], inputs[1])

// under height 1000 we get all of them
withdrawalsByHeight, err = keeper.ListWithdrawalsBelowHeight(ctx, 1000, limit)
require.NoError(t, err)
require.Len(t, withdrawalsByHeight, 4)
matchesTestCase(withdrawalsByHeight[0], inputs[0])
matchesTestCase(withdrawalsByHeight[1], inputs[1])
matchesTestCase(withdrawalsByHeight[2], inputs[2])
matchesTestCase(withdrawalsByHeight[3], inputs[3])

// under height 1000 we get the first 2 if we limit the output by 2
withdrawalsByHeight, err = keeper.ListWithdrawalsBelowHeight(ctx, 1000, limit/2)
require.NoError(t, err)
require.Len(t, withdrawalsByHeight, limit/2)
matchesTestCase(withdrawalsByHeight[0], inputs[0])
matchesTestCase(withdrawalsByHeight[1], inputs[1])
}

// getAllWithdrawals returns all withdrawals in the keeper DB.
Expand Down

0 comments on commit f34bcd8

Please sign in to comment.