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

Commit

Permalink
fix executor when batch is nil
Browse files Browse the repository at this point in the history
  • Loading branch information
rachit77 committed Mar 25, 2024
1 parent 8720afd commit 2a43e70
Show file tree
Hide file tree
Showing 8 changed files with 101 additions and 27 deletions.
10 changes: 9 additions & 1 deletion interop/executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
106 changes: 86 additions & 20 deletions interop/executor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package interop
import (
"context"
"errors"
"fmt"
"math/big"
"testing"

Expand Down Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion mocks/db.generated.go

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

2 changes: 1 addition & 1 deletion mocks/eth_tx_manager.generated.go

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

2 changes: 1 addition & 1 deletion mocks/etherman.generated.go

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

2 changes: 1 addition & 1 deletion mocks/etherman_client.generated.go

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

2 changes: 1 addition & 1 deletion mocks/zk_evm_client.generated.go

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

2 changes: 1 addition & 1 deletion mocks/zk_evm_client_creator.generated.go

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

0 comments on commit 2a43e70

Please sign in to comment.