Skip to content

Commit

Permalink
test: IsGERInjected unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
Stefan-Ethernal committed Dec 20, 2024
1 parent 8df5890 commit 3a9a8df
Show file tree
Hide file tree
Showing 4 changed files with 164 additions and 6 deletions.
6 changes: 5 additions & 1 deletion aggoracle/chaingersender/evm.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ type EthTxManager interface {
) (common.Hash, error)
}

type L2GERManager interface {
GlobalExitRootMap(opts *bind.CallOpts, ger [common.HashLength]byte) (*big.Int, error)
}

type EVMConfig struct {
GlobalExitRootL2Addr common.Address `mapstructure:"GlobalExitRootL2"`
URLRPCL2 string `mapstructure:"URLRPCL2"`
Expand All @@ -55,7 +59,7 @@ type EVMConfig struct {
type EVMChainGERSender struct {
logger *log.Logger

l2GERManager *globalexitrootmanagerl2sovereignchain.Globalexitrootmanagerl2sovereignchain
l2GERManager L2GERManager
l2GERManagerAddr common.Address
l2GERManagerAbi *abi.ABI

Expand Down
66 changes: 61 additions & 5 deletions aggoracle/chaingersender/evm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,6 @@ func TestEVMChainGERSender_InjectGER(t *testing.T) {
l2GERManagerAbi, err := abi.JSON(strings.NewReader(insertGERFuncABI))
require.NoError(t, err)

gasOffset := uint64(1000)

ger := common.HexToHash("0x456")
txID := common.HexToHash("0x789")

Expand Down Expand Up @@ -88,15 +86,18 @@ func TestEVMChainGERSender_InjectGER(t *testing.T) {
defer cancelFn()

ethTxMan := new(mocks.EthTxManagerMock)
ethTxMan.On("Add", ctx, &l2GERManagerAddr, common.Big0, mock.Anything, gasOffset, mock.Anything).Return(tt.addReturnTxID, tt.addReturnErr)
ethTxMan.On("Result", ctx, tt.addReturnTxID).Return(tt.resultReturn, tt.resultReturnErr)
ethTxMan.
On("Add", ctx, &l2GERManagerAddr, common.Big0, mock.Anything, mock.Anything, mock.Anything).
Return(tt.addReturnTxID, tt.addReturnErr)
ethTxMan.
On("Result", ctx, tt.addReturnTxID).
Return(tt.resultReturn, tt.resultReturnErr)

sender := &EVMChainGERSender{
logger: log.GetDefaultLogger(),
l2GERManagerAddr: l2GERManagerAddr,
l2GERManagerAbi: &l2GERManagerAbi,
ethTxMan: ethTxMan,
gasOffset: gasOffset,
waitPeriodMonitorTx: time.Millisecond * 10,
}

Expand All @@ -110,3 +111,58 @@ func TestEVMChainGERSender_InjectGER(t *testing.T) {
})
}
}

func TestEVMChainGERSender_IsGERInjected(t *testing.T) {
tests := []struct {
name string
mockReturn *big.Int
mockError error
expectedResult bool
expectedErrMsg string
}{
{
name: "GER is injected",
mockReturn: big.NewInt(1),
mockError: nil,
expectedResult: true,
expectedErrMsg: "",
},
{
name: "GER is not injected",
mockReturn: big.NewInt(0),
mockError: nil,
expectedResult: false,
expectedErrMsg: "",
},
{
name: "Error checking GER injection",
mockReturn: nil,
mockError: errors.New("some error"),
expectedResult: false,
expectedErrMsg: "failed to check if global exit root is injected",
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
mockL2GERManager := new(mocks.L2GERManagerMock)
mockL2GERManager.On("GlobalExitRootMap", mock.Anything, mock.Anything).
Return(tt.mockReturn, tt.mockError)

evmChainGERSender := &EVMChainGERSender{
l2GERManager: mockL2GERManager,
}

ger := common.HexToHash("0x12345")
result, err := evmChainGERSender.IsGERInjected(ger)
if tt.expectedErrMsg != "" {
require.ErrorContains(t, err, tt.expectedErrMsg)
} else {
require.NoError(t, err)
}
require.Equal(t, tt.expectedResult, result)

mockL2GERManager.AssertExpectations(t)
})
}
}
97 changes: 97 additions & 0 deletions aggoracle/mocks/mock_l2germanager.go

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

1 change: 1 addition & 0 deletions test/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ generate-mocks-helpers: ## Generates mocks for helpers, using mockery tool
generate-mocks-aggoracle: ## Generates mocks for aggoracle, using mockery tool
rm -Rf ../aggoracle/mocks
export "GOROOT=$$(go env GOROOT)" && $$(go env GOPATH)/bin/mockery --name=EthTxManager --dir ../aggoracle/chaingersender --output ../aggoracle/mocks --outpkg mocks --structname=EthTxManagerMock --filename=mock_ethtxmanager.go ${COMMON_MOCKERY_PARAMS}
export "GOROOT=$$(go env GOROOT)" && $$(go env GOPATH)/bin/mockery --name=L2GERManager --dir ../aggoracle/chaingersender --output ../aggoracle/mocks --outpkg mocks --structname=L2GERManagerMock --filename=mock_l2germanager.go ${COMMON_MOCKERY_PARAMS}

.PHONY: generate-mocks-sync
generate-mocks-sync: ## Generates mocks for sync, using mockery tool
Expand Down

0 comments on commit 3a9a8df

Please sign in to comment.