Skip to content

Commit

Permalink
Add deposit handler tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mpetrun5 committed Oct 23, 2023
1 parent 7279b6f commit 599fd7c
Show file tree
Hide file tree
Showing 6 changed files with 193 additions and 2 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ test:
./scripts/tests.sh

genmocks:
echo ''
mockgen -source=./chains/evm/listener/events/handlers/deposit.go -destination=./mock/handlers.go -package mock


PLATFORMS := linux/amd64 darwin/amd64 darwin/arm64 linux/arm
Expand Down
6 changes: 5 additions & 1 deletion chains/evm/listener/events/handlers/deposit.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,9 @@ func (h *DepositEventHandler) HandleEvents(startBlock *big.Int, endBlock *big.In
return nil
}

h.msgChan <- make([]*message.Message, 0)
h.msgChan <- []*message.Message{
{},
}
return nil
}

Expand All @@ -76,6 +78,8 @@ func (h *DepositEventHandler) fetchDeposits(startBlock *big.Int, endBlock *big.I
}

func (h *DepositEventHandler) unpackDeposit(data []byte) (*events.Deposit, error) {
fmt.Println(data)

var d events.Deposit
err := h.routerABI.UnpackIntoInterface(&d, "Deposit", data)
if err != nil {
Expand Down
128 changes: 128 additions & 0 deletions chains/evm/listener/events/handlers/deposit_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
// The Licensed Work is (c) 2023 Sygma
// SPDX-License-Identifier: LGPL-3.0-only

package handlers_test

import (
"context"
"encoding/hex"
"fmt"
"math/big"
"testing"

"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
"github.com/stretchr/testify/suite"
"github.com/sygmaprotocol/spectre-node/chains/evm/listener/events"
"github.com/sygmaprotocol/spectre-node/chains/evm/listener/events/handlers"
"github.com/sygmaprotocol/spectre-node/mock"
"github.com/sygmaprotocol/sygma-core/relayer/message"
"go.uber.org/mock/gomock"
)

func readFromChannel(msgChan chan []*message.Message) ([]*message.Message, error) {
select {
case msgs := <-msgChan:
return msgs, nil
default:
return make([]*message.Message, 0), fmt.Errorf("no message sent")
}
}

func SliceTo32Bytes(in []byte) [32]byte {
var res [32]byte
copy(res[:], in)
return res
}

type DepositHandlerTestSuite struct {
suite.Suite

depositHandler *handlers.DepositEventHandler

msgChan chan []*message.Message
mockEventFetcher *mock.MockEventFetcher
}

func TestRunConfigTestSuite(t *testing.T) {
suite.Run(t, new(DepositHandlerTestSuite))
}

func (s *DepositHandlerTestSuite) SetupTest() {
ctrl := gomock.NewController(s.T())
s.mockEventFetcher = mock.NewMockEventFetcher(ctrl)
s.msgChan = make(chan []*message.Message, 1)
s.depositHandler = handlers.NewDepositEventHandler(
s.msgChan,
s.mockEventFetcher,
common.HexToAddress("0xb0b13f0109ef097C3Aa70Fb543EA4942114A845d"))
}

func (s *DepositHandlerTestSuite) Test_HandleEvents_FetchingDepositsFails() {
startBlock := big.NewInt(0)
endBlock := big.NewInt(4)
s.mockEventFetcher.EXPECT().FetchEventLogs(
context.Background(),
gomock.Any(),
string(events.DepositSig),
startBlock,
endBlock,
).Return(nil, fmt.Errorf("Error"))

err := s.depositHandler.HandleEvents(startBlock, endBlock)
s.NotNil(err)

_, err = readFromChannel(s.msgChan)
s.NotNil(err)
}

func (s *DepositHandlerTestSuite) Test_HandleEvents_NoEvents_MessageNotSent() {
startBlock := big.NewInt(0)
endBlock := big.NewInt(4)
s.mockEventFetcher.EXPECT().FetchEventLogs(
context.Background(),
gomock.Any(),
string(events.DepositSig),
startBlock,
endBlock,
).Return(make([]types.Log, 0), nil)

err := s.depositHandler.HandleEvents(startBlock, endBlock)
s.Nil(err)

_, err = readFromChannel(s.msgChan)
s.NotNil(err)
}

func (s *DepositHandlerTestSuite) Test_HandleEvents_ValidDeposit() {
validDepositData, _ := hex.DecodeString("000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000a00000000000000000000000000000000000000000000000000000000000000000")
invalidDepositData := []byte("invalid")

startBlock := big.NewInt(0)
endBlock := big.NewInt(4)
s.mockEventFetcher.EXPECT().FetchEventLogs(
context.Background(),
gomock.Any(),
string(events.DepositSig),
startBlock,
endBlock,
).Return([]types.Log{
{
Data: invalidDepositData,
},
{
Data: validDepositData,
Topics: []common.Hash{
{},
common.HexToHash("0xd68eb9b5E135b96c1Af165e1D8c4e2eB0E1CE4CD"),
},
},
}, nil)

err := s.depositHandler.HandleEvents(startBlock, endBlock)
s.Nil(err)

msgs, err := readFromChannel(s.msgChan)
s.Nil(err)
s.Equal(len(msgs), 1)
}
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ require (
github.com/rs/zerolog v1.31.0
github.com/stretchr/testify v1.8.4
github.com/sygmaprotocol/sygma-core v0.0.0-20231019093207-256532b316d8
go.uber.org/mock v0.3.0
)

require (
Expand Down
1 change: 1 addition & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ github.com/tklauser/go-sysconf v0.3.12 h1:0QaGUFOdQaIVdPgfITYzaTegZvdCjmYO52cSFA
github.com/tklauser/numcpus v0.6.1 h1:ng9scYS7az0Bk4OZLvrNXNSAO2Pxr1XXRAPyjhIx+Fk=
github.com/vedhavyas/go-subkey v1.0.4 h1:QwjBZx4w7qXC2lmqol2jJfhaNXPI9BsgLZiMiCwqGDU=
go.uber.org/mock v0.3.0 h1:3mUxI1No2/60yUYax92Pt8eNOEecx2D3lcXZh2NEZJo=
go.uber.org/mock v0.3.0/go.mod h1:a6FSlNadKUHUa9IP5Vyt1zh4fC7uAwxMutEAscFbkZc=
golang.org/x/crypto v0.14.0 h1:wBqGXzWJW6m1XrIKlAH0Hs1JJ7+9KBwnIO8v66Q9cHc=
golang.org/x/crypto v0.14.0/go.mod h1:MVFd36DqK4CsrnJYDkBA3VC4m2GkXAM0PvzMCn4JQf4=
golang.org/x/exp v0.0.0-20230905200255-921286631fa9 h1:GoHiUyI/Tp2nVkLI2mCxVkOjsbSXD66ic0XW0js0R9g=
Expand Down
57 changes: 57 additions & 0 deletions mock/handlers.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 599fd7c

Please sign in to comment.