From 2a43e703b51d9f128cc75de59a9ea57fd164feb4 Mon Sep 17 00:00:00 2001 From: Rachit Sonthalia Date: Tue, 26 Mar 2024 03:05:34 +0530 Subject: [PATCH] fix executor when batch is nil --- interop/executor.go | 10 ++- interop/executor_test.go | 106 ++++++++++++++++++----- mocks/db.generated.go | 2 +- mocks/eth_tx_manager.generated.go | 2 +- mocks/etherman.generated.go | 2 +- mocks/etherman_client.generated.go | 2 +- mocks/zk_evm_client.generated.go | 2 +- mocks/zk_evm_client_creator.generated.go | 2 +- 8 files changed, 101 insertions(+), 27 deletions(-) diff --git a/interop/executor.go b/interop/executor.go index 16246865..ed5e0d2b 100644 --- a/interop/executor.go +++ b/interop/executor.go @@ -163,7 +163,15 @@ func (e *Executor) Execute(ctx context.Context, signedTx tx.SignedTx) error { } log.Debugf("get batch by number: %v", batch) - if batch.StateRoot != signedTx.Tx.ZKP.NewStateRoot || batch.LocalExitRoot != signedTx.Tx.ZKP.NewLocalExitRoot { + 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(), + ) + } 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", signedTx.Tx.ZKP.NewLocalExitRoot.Hex(), diff --git a/interop/executor_test.go b/interop/executor_test.go index 6d746f27..8a43ee5f 100644 --- a/interop/executor_test.go +++ b/interop/executor_test.go @@ -3,6 +3,7 @@ package interop import ( "context" "errors" + "fmt" "math/big" "testing" @@ -176,31 +177,96 @@ func TestExecutor_Execute(t *testing.T) { LastVerifiedBatch: 0, NewVerifiedBatch: 1, ZKP: tx.ZKP{ - NewStateRoot: common.BytesToHash([]byte("sampleNewStateRoot")), - Proof: []byte("sampleProof"), + 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) - - mockZkEVMClientCreator.On("NewClient", mock.Anything).Return(mockZkEVMClient).Once() - mockZkEVMClient.On("BatchByNumber", mock.Anything, big.NewInt(int64(signedTx.Tx.NewVerifiedBatch))). - Return(&rpctypes.Batch{ - StateRoot: signedTx.Tx.ZKP.NewStateRoot, - LocalExitRoot: signedTx.Tx.ZKP.NewLocalExitRoot, - // Add other necessary fields here - }, nil).Once() - - // Set the ZkEVMClientCreator to return the mock ZkEVMClient - executor.ZkEVMClientCreator = mockZkEVMClientCreator + t.Run("Batch is not 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(&rpctypes.Batch{ + StateRoot: signedTx.Tx.ZKP.NewStateRoot, + LocalExitRoot: signedTx.Tx.ZKP.NewLocalExitRoot, + // Add other necessary fields here + }, 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) + }) + + t.Run("Returns expected error when Batch is nil and roots do not 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() + + // Set the ZkEVMClientCreator to return the mock ZkEVMClient + executor.ZkEVMClientCreator = mockZkEVMClientCreator + + err := executor.Execute(context.Background(), signedTx) + 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(), + ) + assert.Contains(t, err.Error(), expectedError) + mockZkEVMClientCreator.AssertExpectations(t) + mockZkEVMClient.AssertExpectations(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() + + // Set the ZkEVMClientCreator to return the mock ZkEVMClient + executor.ZkEVMClientCreator = mockZkEVMClientCreator + + newSignedTx := tx.SignedTx{ + Tx: tx.Tx{ + LastVerifiedBatch: 0, + NewVerifiedBatch: 1, + ZKP: tx.ZKP{ + NewStateRoot: common.Hash{}, + NewLocalExitRoot: common.Hash{}, + Proof: []byte("sampleProof"), + }, + }, + } - err := executor.Execute(context.Background(), signedTx) - require.NoError(t, err) - mockZkEVMClientCreator.AssertExpectations(t) - mockZkEVMClient.AssertExpectations(t) + err := executor.Execute(context.Background(), newSignedTx) + require.NoError(t, err) + mockZkEVMClientCreator.AssertExpectations(t) + mockZkEVMClient.AssertExpectations(t) + }) } func TestExecutor_Settle(t *testing.T) { diff --git a/mocks/db.generated.go b/mocks/db.generated.go index 9c23a1cf..1946d157 100644 --- a/mocks/db.generated.go +++ b/mocks/db.generated.go @@ -1,4 +1,4 @@ -// Code generated by mockery v2.39.0. DO NOT EDIT. +// Code generated by mockery v2.40.1. DO NOT EDIT. package mocks diff --git a/mocks/eth_tx_manager.generated.go b/mocks/eth_tx_manager.generated.go index 74fdc226..3c0016f3 100644 --- a/mocks/eth_tx_manager.generated.go +++ b/mocks/eth_tx_manager.generated.go @@ -1,4 +1,4 @@ -// Code generated by mockery v2.39.0. DO NOT EDIT. +// Code generated by mockery v2.40.1. DO NOT EDIT. package mocks diff --git a/mocks/etherman.generated.go b/mocks/etherman.generated.go index cf7ed8da..a79052d8 100644 --- a/mocks/etherman.generated.go +++ b/mocks/etherman.generated.go @@ -1,4 +1,4 @@ -// Code generated by mockery v2.39.0. DO NOT EDIT. +// Code generated by mockery v2.40.1. DO NOT EDIT. package mocks diff --git a/mocks/etherman_client.generated.go b/mocks/etherman_client.generated.go index 3b9211aa..fda4481a 100644 --- a/mocks/etherman_client.generated.go +++ b/mocks/etherman_client.generated.go @@ -1,4 +1,4 @@ -// Code generated by mockery v2.39.0. DO NOT EDIT. +// Code generated by mockery v2.40.1. DO NOT EDIT. package mocks diff --git a/mocks/zk_evm_client.generated.go b/mocks/zk_evm_client.generated.go index 4412a765..d0f363c5 100644 --- a/mocks/zk_evm_client.generated.go +++ b/mocks/zk_evm_client.generated.go @@ -1,4 +1,4 @@ -// Code generated by mockery v2.39.0. DO NOT EDIT. +// Code generated by mockery v2.40.1. DO NOT EDIT. package mocks diff --git a/mocks/zk_evm_client_creator.generated.go b/mocks/zk_evm_client_creator.generated.go index d3e934ce..4a5d690a 100644 --- a/mocks/zk_evm_client_creator.generated.go +++ b/mocks/zk_evm_client_creator.generated.go @@ -1,4 +1,4 @@ -// Code generated by mockery v2.39.0. DO NOT EDIT. +// Code generated by mockery v2.40.1. DO NOT EDIT. package mocks