Skip to content
This repository has been archived by the owner on Sep 23, 2024. It is now read-only.

Commit

Permalink
fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
rachit77 committed Mar 25, 2024
1 parent 2a43e70 commit 9370cc0
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 33 deletions.
4 changes: 2 additions & 2 deletions interop/executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,14 +166,14 @@ func (e *Executor) Execute(ctx context.Context, signedTx tx.SignedTx) error {
if batch == nil && (signedTx.Tx.ZKP.NewStateRoot != common.Hash{} || signedTx.Tx.ZKP.NewLocalExitRoot != common.Hash{}) {
return fmt.Errorf(
"Mismatch detected, expected local exit root: %s actual: %s. expected state root: %s actual: %s",
common.Hash{}.Hex(),
signedTx.Tx.ZKP.NewLocalExitRoot.Hex(),
common.Hash{}.Hex(),
signedTx.Tx.ZKP.NewStateRoot.Hex(),
common.Hash{}.Hex(),
)
} else if batch != nil && (batch.StateRoot != signedTx.Tx.ZKP.NewStateRoot || batch.LocalExitRoot != signedTx.Tx.ZKP.NewLocalExitRoot) {
return fmt.Errorf(
"Mismatch detected, expected local exit root: %s actual: %s. expected state root: %s actual: %s",
"Mismatch detected, expected local exit root: %s actual: %s. expected state root: %s actual: %s",
signedTx.Tx.ZKP.NewLocalExitRoot.Hex(),
batch.LocalExitRoot.Hex(),
signedTx.Tx.ZKP.NewStateRoot.Hex(),
Expand Down
92 changes: 61 additions & 31 deletions interop/executor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,29 +164,31 @@ func TestExecutor_VerifySignature(t *testing.T) {
}

func TestExecutor_Execute(t *testing.T) {
cfg := &config.Config{}
interopAdminAddr := common.HexToAddress("0x1234567890abcdef")
etherman := mocks.NewEthermanMock(t)
ethTxManager := mocks.NewEthTxManagerMock(t)

executor := New(log.WithFields("test", "test"), cfg, interopAdminAddr, etherman, ethTxManager)

// Create a sample signed transaction for testing
signedTx := tx.SignedTx{
Tx: tx.Tx{
LastVerifiedBatch: 0,
NewVerifiedBatch: 1,
ZKP: tx.ZKP{
NewStateRoot: common.BytesToHash([]byte("sampleNewStateRoot")),
NewLocalExitRoot: common.BytesToHash([]byte("sampleNewLocalExitRoot")),
Proof: []byte("sampleProof"),
},
},
}
t.Parallel()

t.Run("Batch is not nil and roots match", func(t *testing.T) {
t.Parallel()

cfg := &config.Config{}
interopAdminAddr := common.HexToAddress("0x1234567890abcdef")
etherman := mocks.NewEthermanMock(t)
ethTxManager := mocks.NewEthTxManagerMock(t)

executor := New(log.WithFields("test", "test"), cfg, interopAdminAddr, etherman, ethTxManager)

// Create a sample signed transaction for testing
signedTx := tx.SignedTx{
Tx: tx.Tx{
LastVerifiedBatch: 0,
NewVerifiedBatch: 1,
ZKP: tx.ZKP{
NewStateRoot: common.BytesToHash([]byte("sampleNewStateRoot")),
NewLocalExitRoot: common.BytesToHash([]byte("sampleNewLocalExitRoot")),
Proof: []byte("sampleProof"),
},
},
}

// Mock the ZkEVMClientCreator.NewClient method
mockZkEVMClientCreator := mocks.NewZkEVMClientClientCreatorMock(t)
mockZkEVMClient := mocks.NewZkEVMClientMock(t)
Expand All @@ -211,6 +213,26 @@ func TestExecutor_Execute(t *testing.T) {
t.Run("Returns expected error when Batch is nil and roots do not match", func(t *testing.T) {
t.Parallel()

cfg := &config.Config{}
interopAdminAddr := common.HexToAddress("0x1234567890abcdef")
etherman := mocks.NewEthermanMock(t)
ethTxManager := mocks.NewEthTxManagerMock(t)

executor := New(log.WithFields("test", "test"), cfg, interopAdminAddr, etherman, ethTxManager)

// Create a sample signed transaction for testing
signedTx := tx.SignedTx{
Tx: tx.Tx{
LastVerifiedBatch: 0,
NewVerifiedBatch: 1,
ZKP: tx.ZKP{
NewStateRoot: common.BytesToHash([]byte("sampleNewStateRoot")),
NewLocalExitRoot: common.BytesToHash([]byte("sampleNewLocalExitRoot")),
Proof: []byte("sampleProof"),
},
},
}

// Mock the ZkEVMClientCreator.NewClient method
mockZkEVMClientCreator := mocks.NewZkEVMClientClientCreatorMock(t)
mockZkEVMClient := mocks.NewZkEVMClientMock(t)
Expand All @@ -226,10 +248,10 @@ func TestExecutor_Execute(t *testing.T) {
require.Error(t, err)
expectedError := fmt.Sprintf(
"Mismatch detected, expected local exit root: %s actual: %s. expected state root: %s actual: %s",
common.Hash{}.Hex(),
signedTx.Tx.ZKP.NewLocalExitRoot.Hex(),
common.Hash{}.Hex(),
signedTx.Tx.ZKP.NewStateRoot.Hex(),
common.Hash{}.Hex(),
)
assert.Contains(t, err.Error(), expectedError)
mockZkEVMClientCreator.AssertExpectations(t)
Expand All @@ -239,18 +261,15 @@ func TestExecutor_Execute(t *testing.T) {
t.Run("Batch is nil and roots match", func(t *testing.T) {
t.Parallel()

// Mock the ZkEVMClientCreator.NewClient method
mockZkEVMClientCreator := mocks.NewZkEVMClientClientCreatorMock(t)
mockZkEVMClient := mocks.NewZkEVMClientMock(t)

mockZkEVMClientCreator.On("NewClient", mock.Anything).Return(mockZkEVMClient).Once()
mockZkEVMClient.On("BatchByNumber", mock.Anything, big.NewInt(int64(signedTx.Tx.NewVerifiedBatch))).
Return(nil, nil).Once()
cfg := &config.Config{}
interopAdminAddr := common.HexToAddress("0x1234567890abcdef")
etherman := mocks.NewEthermanMock(t)
ethTxManager := mocks.NewEthTxManagerMock(t)

// Set the ZkEVMClientCreator to return the mock ZkEVMClient
executor.ZkEVMClientCreator = mockZkEVMClientCreator
executor := New(log.WithFields("test", "test"), cfg, interopAdminAddr, etherman, ethTxManager)

newSignedTx := tx.SignedTx{
// Create a sample signed transaction for testing
signedTx := tx.SignedTx{
Tx: tx.Tx{
LastVerifiedBatch: 0,
NewVerifiedBatch: 1,
Expand All @@ -262,7 +281,18 @@ func TestExecutor_Execute(t *testing.T) {
},
}

err := executor.Execute(context.Background(), newSignedTx)
// Mock the ZkEVMClientCreator.NewClient method
mockZkEVMClientCreator := mocks.NewZkEVMClientClientCreatorMock(t)
mockZkEVMClient := mocks.NewZkEVMClientMock(t)

mockZkEVMClientCreator.On("NewClient", mock.Anything).Return(mockZkEVMClient).Once()
mockZkEVMClient.On("BatchByNumber", mock.Anything, big.NewInt(int64(signedTx.Tx.NewVerifiedBatch))).
Return(nil, nil).Once()

// Set the ZkEVMClientCreator to return the mock ZkEVMClient
executor.ZkEVMClientCreator = mockZkEVMClientCreator

err := executor.Execute(context.Background(), signedTx)
require.NoError(t, err)
mockZkEVMClientCreator.AssertExpectations(t)
mockZkEVMClient.AssertExpectations(t)
Expand Down

0 comments on commit 9370cc0

Please sign in to comment.