Skip to content

challenger unit test #62

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 22, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
85 changes: 85 additions & 0 deletions challenger/child/common_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
package child

import (
"context"
"errors"

ophosttypes "github.com/initia-labs/OPinit/x/ophost/types"
challengertypes "github.com/initia-labs/opinit-bots/challenger/types"
"github.com/initia-labs/opinit-bots/types"
)

type mockHost struct {
db types.DB
height int64
pendingEvents []challengertypes.ChallengeEvent
syncedOutputs map[uint64]map[uint64]*ophosttypes.Output
}

func NewMockHost(db types.DB, height int64) *mockHost {
return &mockHost{
db: db,
height: height,
pendingEvents: make([]challengertypes.ChallengeEvent, 0),
syncedOutputs: make(map[uint64]map[uint64]*ophosttypes.Output),
}
}

func (m *mockHost) DB() types.DB {
return m.db
}

func (m *mockHost) SetPendingEvents(events []challengertypes.ChallengeEvent) {
m.pendingEvents = append(m.pendingEvents, events...)
}

func (m *mockHost) Height() int64 {
return m.height
}

func (m *mockHost) SetSyncedOutput(bridgeId uint64, outputIndex uint64, syncedOutput *ophosttypes.Output) {
if _, ok := m.syncedOutputs[bridgeId]; !ok {
m.syncedOutputs[bridgeId] = make(map[uint64]*ophosttypes.Output)
}
m.syncedOutputs[bridgeId][outputIndex] = syncedOutput
}

func (m *mockHost) QuerySyncedOutput(ctx context.Context, bridgeId uint64, outputIndex uint64) (*ophosttypes.QueryOutputProposalResponse, error) {
if _, ok := m.syncedOutputs[bridgeId]; !ok {
return nil, errors.New("collections: not found")
}

if _, ok := m.syncedOutputs[bridgeId][outputIndex]; !ok {
return nil, errors.New("collections: not found")
}

return &ophosttypes.QueryOutputProposalResponse{
BridgeId: bridgeId,
OutputIndex: outputIndex,
OutputProposal: *m.syncedOutputs[bridgeId][outputIndex],
}, nil
}

var _ hostNode = (*mockHost)(nil)

type mockChallenger struct {
db types.DB
pendingChallenges []challengertypes.Challenge
}

func NewMockChallenger(db types.DB) *mockChallenger {
return &mockChallenger{
db: db,
pendingChallenges: make([]challengertypes.Challenge, 0),
}
}

func (m *mockChallenger) DB() types.DB {
return m.db
}

func (m *mockChallenger) SendPendingChallenges(challenges []challengertypes.Challenge) {
m.pendingChallenges = append(m.pendingChallenges, challenges...)
}

var _ challenger = (*mockChallenger)(nil)
17 changes: 5 additions & 12 deletions challenger/child/deposit.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
package child

import (
"time"

childprovider "github.com/initia-labs/opinit-bots/provider/child"
"github.com/initia-labs/opinit-bots/types"
"go.uber.org/zap"

sdk "github.com/cosmos/cosmos-sdk/types"

challengertypes "github.com/initia-labs/opinit-bots/challenger/types"
nodetypes "github.com/initia-labs/opinit-bots/node/types"
"github.com/pkg/errors"
Expand All @@ -19,14 +15,7 @@ func (ch *Child) finalizeDepositHandler(ctx types.Context, args nodetypes.EventH
if err != nil {
return errors.Wrap(err, "failed to parse finalize deposit event")
}
ch.handleFinalizeDeposit(ctx, args.BlockTime, l1BlockHeight, l1Sequence, from, to, amount, baseDenom)
ch.lastFinalizedDepositL1BlockHeight = l1BlockHeight
ch.lastFinalizedDepositL1Sequence = l1Sequence
return nil
}

func (ch *Child) handleFinalizeDeposit(ctx types.Context, l2BlockTime time.Time, l1BlockHeight int64, l1Sequence uint64, from string, to string, amount sdk.Coin, baseDenom string) {
deposit := challengertypes.NewDeposit(l1Sequence, l1BlockHeight, from, to, baseDenom, amount.String(), l2BlockTime)
deposit := challengertypes.NewDeposit(l1Sequence, l1BlockHeight, from, to, baseDenom, amount.String(), args.BlockTime)
ch.eventQueue = append(ch.eventQueue, deposit)

ctx.Logger().Info("finalize token deposit",
Expand All @@ -37,4 +26,8 @@ func (ch *Child) handleFinalizeDeposit(ctx types.Context, l2BlockTime time.Time,
zap.String("amount", amount.String()),
zap.String("base_denom", baseDenom),
)

ch.lastFinalizedDepositL1BlockHeight = l1BlockHeight
ch.lastFinalizedDepositL1Sequence = l1Sequence
return nil
}
83 changes: 83 additions & 0 deletions challenger/child/deposit_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package child

import (
"context"
"strconv"
"testing"
"time"

ophosttypes "github.com/initia-labs/OPinit/x/ophost/types"
challengertypes "github.com/initia-labs/opinit-bots/challenger/types"
"github.com/initia-labs/opinit-bots/db"
"github.com/initia-labs/opinit-bots/node"
nodetypes "github.com/initia-labs/opinit-bots/node/types"
childprovider "github.com/initia-labs/opinit-bots/provider/child"
"github.com/initia-labs/opinit-bots/types"
"github.com/stretchr/testify/require"
"go.uber.org/zap"

sdk "github.com/cosmos/cosmos-sdk/types"
)

func TestFinalizeDepositHandler(t *testing.T) {
db, err := db.NewMemDB()
require.NoError(t, err)

childNode := node.NewTestNode(nodetypes.NodeConfig{}, db.WithPrefix([]byte("test_child")), nil, nil, nil, nil)
bridgeInfo := ophosttypes.QueryBridgeResponse{
BridgeId: 1,
}

ch := Child{
BaseChild: childprovider.NewTestBaseChild(0, childNode, nil, bridgeInfo, nil, nodetypes.NodeConfig{}),
eventQueue: make([]challengertypes.ChallengeEvent, 0),
}

cases := []struct {
name string
eventHandlerArgs nodetypes.EventHandlerArgs
expected []challengertypes.ChallengeEvent
err bool
}{
{
name: "empty event queue",
eventHandlerArgs: nodetypes.EventHandlerArgs{
BlockTime: time.Unix(0, 100).UTC(),
EventAttributes: childprovider.FinalizeDepositEvents(1, "sender", "recipient", "denom", "baseDenom", sdk.NewInt64Coin("denom", 10000), 2),
},
expected: []challengertypes.ChallengeEvent{
&challengertypes.Deposit{
EventType: "Deposit",
Sequence: 1,
L1BlockHeight: 2,
From: "sender",
To: "recipient",
L1Denom: "baseDenom",
Amount: "10000denom",
Time: time.Unix(0, 100).UTC(),
},
},
err: false,
},
}

for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
ctx := types.NewContext(context.Background(), zap.NewNop(), "")

err := ch.finalizeDepositHandler(ctx, tc.eventHandlerArgs)
require.NoError(t, err)
expectedL1Height, err := strconv.ParseInt(tc.eventHandlerArgs.EventAttributes[6].Value, 10, 64)
require.NoError(t, err)
expectedL1Sequence, err := strconv.ParseUint(tc.eventHandlerArgs.EventAttributes[0].Value, 10, 64)
require.NoError(t, err)

require.Equal(t, expectedL1Height, ch.lastFinalizedDepositL1BlockHeight)
require.Equal(t, expectedL1Sequence, ch.lastFinalizedDepositL1Sequence)

require.Equal(t, tc.expected, ch.eventQueue)
ch.eventQueue = make([]challengertypes.ChallengeEvent, 0)
})
}
require.NoError(t, err)
}
2 changes: 1 addition & 1 deletion challenger/child/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ func (ch *Child) endBlockHandler(ctx types.Context, args nodetypes.EndBlockArgs)

ch.eventHandler.DeletePendingEvents(processedEvents)
ch.eventHandler.SetPendingEvents(timeoutEvents)
ch.challenger.SendPendingChallenges(challenges)
ch.challenger.SendPendingChallenges(pendingChallenges)
return nil
}

Expand Down
Loading
Loading