-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
447 additions
and
55 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
// The Licensed Work is (c) 2023 Sygma | ||
// SPDX-License-Identifier: LGPL-3.0-only | ||
|
||
package handlers_test | ||
|
||
import ( | ||
"fmt" | ||
"math/big" | ||
"testing" | ||
|
||
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/handlers" | ||
evmMessage "github.com/sygmaprotocol/spectre-node/chains/evm/message" | ||
"github.com/sygmaprotocol/spectre-node/chains/evm/prover" | ||
"github.com/sygmaprotocol/spectre-node/mock" | ||
"github.com/sygmaprotocol/sygma-core/relayer/message" | ||
consensus "github.com/umbracle/go-eth-consensus" | ||
"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") | ||
} | ||
} | ||
|
||
type RotateHandlerTestSuite struct { | ||
suite.Suite | ||
|
||
handler *handlers.RotateHandler | ||
|
||
msgChan chan []*message.Message | ||
mockProver *mock.MockProver | ||
mockPeriodStorer *mock.MockPeriodStorer | ||
} | ||
|
||
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.mockPeriodStorer = mock.NewMockPeriodStorer(ctrl) | ||
s.msgChan = make(chan []*message.Message, 2) | ||
s.handler = handlers.NewRotateHandler( | ||
s.msgChan, | ||
s.mockPeriodStorer, | ||
s.mockProver, | ||
1, | ||
[]uint8{2, 3}, | ||
256, | ||
) | ||
} | ||
|
||
func (s *RotateHandlerTestSuite) Test_HandleEvents_PeriodFetchingFails() { | ||
s.mockPeriodStorer.EXPECT().Period(uint8(1)).Return(nil, fmt.Errorf("error")) | ||
|
||
err := s.handler.HandleEvents(&apiv1.Finality{}) | ||
s.NotNil(err) | ||
|
||
_, err = readFromChannel(s.msgChan) | ||
s.NotNil(err) | ||
} | ||
|
||
func (s *RotateHandlerTestSuite) Test_HandleEvents_CurrentPeriodOlderThanLatest() { | ||
s.mockPeriodStorer.EXPECT().Period(uint8(1)).Return(big.NewInt(2), nil) | ||
|
||
err := s.handler.HandleEvents(&apiv1.Finality{ | ||
Finalized: &phase0.Checkpoint{ | ||
Epoch: phase0.Epoch(300), | ||
}, | ||
}) | ||
s.Nil(err) | ||
|
||
_, err = readFromChannel(s.msgChan) | ||
s.NotNil(err) | ||
} | ||
|
||
func (s *RotateHandlerTestSuite) Test_HandleEvents_ValidPeriod() { | ||
s.mockPeriodStorer.EXPECT().Period(uint8(1)).Return(big.NewInt(2), nil) | ||
s.mockPeriodStorer.EXPECT().StorePeriod(uint8(1), big.NewInt(3)).Return(nil) | ||
s.mockProver.EXPECT().RotateArgs(uint64(3)).Return(&prover.RotateArgs{ | ||
Update: &consensus.LightClientUpdateCapella{}, | ||
Domain: phase0.Domain{}, | ||
Spec: "mainnet", | ||
Pubkeys: [512][48]byte{}, | ||
}, nil) | ||
s.mockProver.EXPECT().RotateProof(gomock.Any()).Return(&prover.EvmProof[evmMessage.RotateInput]{ | ||
Proof: []byte{}, | ||
Input: evmMessage.RotateInput{}, | ||
}, nil) | ||
s.mockProver.EXPECT().StepProof(gomock.Any()).Return(&prover.EvmProof[evmMessage.SyncStepInput]{ | ||
Proof: []byte{}, | ||
Input: evmMessage.SyncStepInput{}, | ||
}, nil) | ||
|
||
err := s.handler.HandleEvents(&apiv1.Finality{ | ||
Finalized: &phase0.Checkpoint{ | ||
Epoch: phase0.Epoch(1500), | ||
}, | ||
}) | ||
s.Nil(err) | ||
|
||
msg1, err := readFromChannel(s.msgChan) | ||
s.Nil(err) | ||
s.Equal(len(msg1), 1) | ||
msg2, err := readFromChannel(s.msgChan) | ||
s.Nil(err) | ||
s.Equal(len(msg2), 1) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.