Skip to content

Commit

Permalink
Add rotate handler tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mpetrun5 committed Nov 8, 2023
1 parent a818d4d commit 84e1b35
Show file tree
Hide file tree
Showing 6 changed files with 190 additions and 6 deletions.
3 changes: 2 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ test:
./scripts/tests.sh

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

Expand Down
2 changes: 1 addition & 1 deletion chains/evm/listener/events/handlers/deposit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ type DepositHandlerTestSuite struct {
mockStepProver *mock.MockProver
}

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

Expand Down
7 changes: 4 additions & 3 deletions chains/evm/listener/events/handlers/rotate.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,17 @@ type RotateHandler struct {
prover Prover

syncCommitteeFetcher SyncCommitteeFetcher
currentSyncCommittee *api.Response[*apiv1.SyncCommittee]
currentSyncCommittee *apiv1.SyncCommittee
}

func NewRotateHandler(domainID uint8, domains []uint8, msgChan chan []*message.Message, syncCommitteeFetcher SyncCommitteeFetcher, prover Prover) *RotateHandler {
func NewRotateHandler(msgChan chan []*message.Message, syncCommitteeFetcher SyncCommitteeFetcher, prover Prover, domainID uint8, domains []uint8) *RotateHandler {
return &RotateHandler{
syncCommitteeFetcher: syncCommitteeFetcher,
prover: prover,
domainID: domainID,
domains: domains,
msgChan: msgChan,
currentSyncCommittee: &apiv1.SyncCommittee{},
}
}

Expand All @@ -48,7 +49,7 @@ func (h *RotateHandler) HandleEvents(startBlock *big.Int, endBlock *big.Int) err
if err != nil {
return err
}
if syncCommittee.Data.String() == h.currentSyncCommittee.Data.String() {
if syncCommittee.Data.String() == h.currentSyncCommittee.String() {
return nil
}

Expand Down
126 changes: 126 additions & 0 deletions chains/evm/listener/events/handlers/rotate_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
// The Licensed Work is (c) 2023 Sygma
// SPDX-License-Identifier: LGPL-3.0-only

package handlers_test

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

"github.com/attestantio/go-eth2-client/api"
apiv1 "github.com/attestantio/go-eth2-client/api/v1"
"github.com/attestantio/go-eth2-client/spec/phase0"
"github.com/stretchr/testify/suite"
"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"
)

type RotateHandlerTestSuite struct {
suite.Suite

handler *handlers.RotateHandler

msgChan chan []*message.Message
mockProver *mock.MockProver
mockSyncCommitteeFetcher *mock.MockSyncCommitteeFetcher
}

func TestRunRotateTestSuite(t *testing.T) {
suite.Run(t, new(RotateHandlerTestSuite))
}

func (s *RotateHandlerTestSuite) SetupTest() {
ctrl := gomock.NewController(s.T())
s.mockProver = mock.NewMockProver(ctrl)
s.mockSyncCommitteeFetcher = mock.NewMockSyncCommitteeFetcher(ctrl)
s.msgChan = make(chan []*message.Message, 2)
s.handler = handlers.NewRotateHandler(
s.msgChan,
s.mockSyncCommitteeFetcher,
s.mockProver,
1,
[]uint8{2, 3},
)
}

func (s *RotateHandlerTestSuite) Test_HandleEvents_FetchingCommitteeFails() {
startBlock := big.NewInt(0)
endBlock := big.NewInt(4)
s.mockSyncCommitteeFetcher.EXPECT().SyncCommittee(context.Background(), gomock.Any()).Return(nil, fmt.Errorf("error"))

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

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

func (s *RotateHandlerTestSuite) Test_HandleEvents_SyncCommitteeNotChanged() {
startBlock := big.NewInt(0)
endBlock := big.NewInt(4)
s.mockSyncCommitteeFetcher.EXPECT().SyncCommittee(context.Background(), gomock.Any()).Return(&api.Response[*apiv1.SyncCommittee]{
Data: &apiv1.SyncCommittee{},
}, nil)

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

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

func (s *RotateHandlerTestSuite) Test_HandleEvents_NewSyncCommittee_ProofFails() {
startBlock := big.NewInt(0)
endBlock := big.NewInt(4)
s.mockSyncCommitteeFetcher.EXPECT().SyncCommittee(context.Background(), gomock.Any()).Return(&api.Response[*apiv1.SyncCommittee]{
Data: &apiv1.SyncCommittee{
Validators: []phase0.ValidatorIndex{128},
},
}, nil)
s.mockProver.EXPECT().StepProof(endBlock).Return([32]byte{}, fmt.Errorf("error"))

err := s.handler.HandleEvents(startBlock, endBlock)
s.NotNil(err)
_, err = readFromChannel(s.msgChan)
s.NotNil(err)

s.mockSyncCommitteeFetcher.EXPECT().SyncCommittee(context.Background(), gomock.Any()).Return(&api.Response[*apiv1.SyncCommittee]{
Data: &apiv1.SyncCommittee{
Validators: []phase0.ValidatorIndex{128},
},
}, nil)
s.mockProver.EXPECT().StepProof(endBlock).Return([32]byte{}, nil)
s.mockProver.EXPECT().RotateProof(endBlock).Return([32]byte{}, fmt.Errorf("error"))

err = s.handler.HandleEvents(startBlock, endBlock)
s.NotNil(err)
_, err = readFromChannel(s.msgChan)
s.NotNil(err)
}

func (s *RotateHandlerTestSuite) Test_HandleEvents_NewSyncCommittee() {
startBlock := big.NewInt(0)
endBlock := big.NewInt(4)
s.mockSyncCommitteeFetcher.EXPECT().SyncCommittee(context.Background(), gomock.Any()).Return(&api.Response[*apiv1.SyncCommittee]{
Data: &apiv1.SyncCommittee{
Validators: []phase0.ValidatorIndex{128},
},
}, nil)
s.mockProver.EXPECT().StepProof(endBlock).Return([32]byte{1}, nil)
s.mockProver.EXPECT().RotateProof(endBlock).Return([32]byte{1}, nil)

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

msgs, err := readFromChannel(s.msgChan)
s.Nil(err)
s.Equal(msgs[0].Destination, uint8(2))

msgs, err = readFromChannel(s.msgChan)
s.Nil(err)
s.Equal(msgs[0].Destination, uint8(3))
}
2 changes: 1 addition & 1 deletion mock/handlers.go → mock/deposit.go

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

56 changes: 56 additions & 0 deletions mock/rotate.go

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

0 comments on commit 84e1b35

Please sign in to comment.