Skip to content

Commit

Permalink
test: chaingersender ut
Browse files Browse the repository at this point in the history
  • Loading branch information
Stefan-Ethernal committed Dec 20, 2024
1 parent f43da3b commit 8df5890
Show file tree
Hide file tree
Showing 4 changed files with 411 additions and 23 deletions.
49 changes: 27 additions & 22 deletions aggoracle/chaingersender/evm.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,29 +118,34 @@ func (c *EVMChainGERSender) InjectGER(ctx context.Context, ger common.Hash) erro
}

for {
<-ticker.C

c.logger.Debugf("waiting for tx %s to be mined", id.Hex())
res, err := c.ethTxMan.Result(ctx, id)
if err != nil {
c.logger.Errorf("failed to check the transaction %s status: %s", id.Hex(), err)
continue
}

switch res.Status {
case ethtxtypes.MonitoredTxStatusCreated,
ethtxtypes.MonitoredTxStatusSent:
continue
case ethtxtypes.MonitoredTxStatusFailed:
return fmt.Errorf("inject GER tx %s failed", id.Hex())
case ethtxtypes.MonitoredTxStatusMined,
ethtxtypes.MonitoredTxStatusSafe,
ethtxtypes.MonitoredTxStatusFinalized:
c.logger.Debugf("inject GER tx %s was successfully mined at block %d", id.Hex(), res.MinedAtBlockNumber)

select {
case <-ctx.Done():
c.logger.Infof("context cancelled")
return nil
default:
c.logger.Error("unexpected tx status:", res.Status)

case <-ticker.C:
c.logger.Debugf("waiting for tx %s to be mined", id.Hex())
res, err := c.ethTxMan.Result(ctx, id)
if err != nil {
c.logger.Errorf("failed to check the transaction %s status: %s", id.Hex(), err)
return err
}

switch res.Status {
case ethtxtypes.MonitoredTxStatusCreated,
ethtxtypes.MonitoredTxStatusSent:
continue
case ethtxtypes.MonitoredTxStatusFailed:
return fmt.Errorf("inject GER tx %s failed", id.Hex())
case ethtxtypes.MonitoredTxStatusMined,
ethtxtypes.MonitoredTxStatusSafe,
ethtxtypes.MonitoredTxStatusFinalized:
c.logger.Debugf("inject GER tx %s was successfully mined at block %d", id.Hex(), res.MinedAtBlockNumber)

return nil
default:
c.logger.Error("unexpected tx status:", res.Status)
}
}
}
}
112 changes: 112 additions & 0 deletions aggoracle/chaingersender/evm_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
package chaingersender

import (
"context"
"errors"
"math/big"
"strings"
"testing"
"time"

"github.com/0xPolygon/cdk/aggoracle/mocks"
"github.com/0xPolygon/cdk/log"
"github.com/0xPolygon/zkevm-ethtx-manager/types"
"github.com/ethereum/go-ethereum/accounts/abi"
"github.com/ethereum/go-ethereum/common"
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/require"
)

func TestEVMChainGERSender_InjectGER(t *testing.T) {
insertGERFuncABI := `[{
"inputs": [
{
"internalType": "bytes32",
"name": "_newRoot",
"type": "bytes32"
}
],
"name": "insertGlobalExitRoot",
"outputs": [],
"stateMutability": "nonpayable",
"type": "function"
}]`
l2GERManagerAddr := common.HexToAddress("0x123")
l2GERManagerAbi, err := abi.JSON(strings.NewReader(insertGERFuncABI))
require.NoError(t, err)

gasOffset := uint64(1000)

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

tests := []struct {
name string
addReturnTxID common.Hash
addReturnErr error
resultReturn types.MonitoredTxResult
resultReturnErr error
expectedErr string
}{
{
name: "successful injection",
addReturnTxID: txID,
addReturnErr: nil,
resultReturn: types.MonitoredTxResult{Status: types.MonitoredTxStatusMined, MinedAtBlockNumber: big.NewInt(123)},
resultReturnErr: nil,
expectedErr: "",
},
{
name: "injection fails due to transaction failure",
addReturnTxID: txID,
addReturnErr: nil,
resultReturn: types.MonitoredTxResult{Status: types.MonitoredTxStatusFailed},
resultReturnErr: nil,
expectedErr: "inject GER tx",
},
{
name: "injection fails due to Add method error",
addReturnTxID: common.Hash{},
addReturnErr: errors.New("add error"),
resultReturn: types.MonitoredTxResult{},
resultReturnErr: nil,
expectedErr: "add error",
},
{
name: "injection fails due to Result method error",
addReturnTxID: txID,
addReturnErr: nil,
resultReturn: types.MonitoredTxResult{},
resultReturnErr: errors.New("result error"),
expectedErr: "result error",
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
ctx, cancelFn := context.WithTimeout(context.Background(), time.Millisecond*500)
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)

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

err := sender.InjectGER(ctx, ger)
if tt.expectedErr == "" {
require.NoError(t, err)
} else {
require.Error(t, err)
require.Contains(t, err.Error(), tt.expectedErr)
}
})
}
}
Loading

0 comments on commit 8df5890

Please sign in to comment.