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

Commit

Permalink
Refactor and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
vcastellm committed Jan 4, 2024
1 parent 574e018 commit 72afdc4
Show file tree
Hide file tree
Showing 6 changed files with 222 additions and 146 deletions.
23 changes: 12 additions & 11 deletions interop/executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,36 +8,37 @@ import (

"github.com/0xPolygon/beethoven/config"
"github.com/0xPolygon/beethoven/tx"
"github.com/0xPolygonHermez/zkevm-node/jsonrpc/client"
"github.com/0xPolygon/beethoven/types"

"github.com/0xPolygonHermez/zkevm-node/jsonrpc/types"
"github.com/0xPolygonHermez/zkevm-node/jsonrpc/client"
rpctypes "github.com/0xPolygonHermez/zkevm-node/jsonrpc/types"
"github.com/0xPolygonHermez/zkevm-node/log"
"github.com/ethereum/go-ethereum"
"github.com/ethereum/go-ethereum/common"
"github.com/jackc/pgx/v4"
)

var _ ZkEVMClientClientCreator = (*zkEVMClientCreator)(nil)
var _ types.ZkEVMClientClientCreator = (*zkEVMClientCreator)(nil)

type zkEVMClientCreator struct{}

func (zc *zkEVMClientCreator) NewClient(rpc string) ZkEVMClientInterface {
func (zc *zkEVMClientCreator) NewClient(rpc string) types.ZkEVMClientInterface {
return client.NewClient(rpc)
}

type Executor struct {
logger *log.Logger
interopAdminAddr common.Address
config *config.Config
ethTxMan EthTxManager
etherman EthermanInterface
ZkEVMClientCreator ZkEVMClientClientCreator
ethTxMan types.EthTxManager
etherman types.EthermanInterface
ZkEVMClientCreator types.ZkEVMClientClientCreator
}

func New(logger *log.Logger, cfg *config.Config,
interopAdminAddr common.Address,
etherman EthermanInterface,
ethTxManager EthTxManager,
etherman types.EthermanInterface,
ethTxManager types.EthTxManager,
) *Executor {
return &Executor{
logger: logger,
Expand Down Expand Up @@ -169,11 +170,11 @@ func (e *Executor) Settle(signedTx tx.SignedTx, dbTx pgx.Tx) (common.Hash, error
return signedTx.Tx.Hash(), nil
}

func (e *Executor) GetTxStatus(ctx context.Context, hash common.Hash, dbTx pgx.Tx) (result string, err types.Error) {
func (e *Executor) GetTxStatus(ctx context.Context, hash common.Hash, dbTx pgx.Tx) (result string, err rpctypes.Error) {
res, innerErr := e.ethTxMan.Result(ctx, ethTxManOwner, hash.Hex(), dbTx)
if innerErr != nil {
result = "0x0"
err = types.NewRPCError(types.DefaultErrorCode, fmt.Sprintf("failed to get tx, error: %s", innerErr))
err = rpctypes.NewRPCError(rpctypes.DefaultErrorCode, fmt.Sprintf("failed to get tx, error: %s", innerErr))

return
}
Expand Down
58 changes: 58 additions & 0 deletions interop/executor_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package interop

import (
"context"
"testing"

"github.com/0xPolygon/beethoven/config"
"github.com/0xPolygon/beethoven/test"
"github.com/0xPolygon/beethoven/tx"

"github.com/0xPolygonHermez/zkevm-node/log"
"github.com/ethereum/go-ethereum/common"
"github.com/stretchr/testify/assert"
)

func TestNewExecutor(t *testing.T) {
cfg := &config.Config{
// Set your desired config values here
}
interopAdminAddr := common.HexToAddress("0x1234567890abcdef")
etherman := &test.EthermanMock{}
ethTxManager := &test.EthTxManagerMock{}

executor := New(nil, cfg, interopAdminAddr, etherman, ethTxManager)

assert.NotNil(t, executor)
assert.Equal(t, interopAdminAddr, executor.interopAdminAddr)
assert.Equal(t, cfg, executor.config)
assert.Equal(t, ethTxManager, executor.ethTxMan)
assert.Equal(t, etherman, executor.etherman)
assert.NotNil(t, executor.ZkEVMClientCreator)
}

func TestExecutor_CheckTx(t *testing.T) {
cfg := &config.Config{
// Set your desired config values here
}
interopAdminAddr := common.HexToAddress("0x1234567890abcdef")
etherman := &test.EthermanMock{}
ethTxManager := &test.EthTxManagerMock{}

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{
Proof: []byte("sampleProof"),
},
L1Contract: common.HexToAddress("0x1234567890abcdef"),
},
}

err := executor.CheckTx(context.Background(), signedTx)
assert.NoError(t, err)
}
30 changes: 16 additions & 14 deletions rpc/rpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ import (
"fmt"

"github.com/0xPolygon/beethoven/interop"
"github.com/0xPolygonHermez/zkevm-node/jsonrpc/types"
"github.com/0xPolygon/beethoven/types"

rpctypes "github.com/0xPolygonHermez/zkevm-node/jsonrpc/types"
"github.com/0xPolygonHermez/zkevm-node/log"
"github.com/ethereum/go-ethereum/common"

Expand All @@ -22,14 +24,14 @@ const (
type InteropEndpoints struct {
ctx context.Context
executor *interop.Executor
db interop.DBInterface
db types.DBInterface
}

// NewInteropEndpoints returns InteropEndpoints
func NewInteropEndpoints(
ctx context.Context,
executor *interop.Executor,
db interop.DBInterface,
db types.DBInterface,
) *InteropEndpoints {
return &InteropEndpoints{
ctx: ctx,
Expand All @@ -38,62 +40,62 @@ func NewInteropEndpoints(
}
}

func (i *InteropEndpoints) SendTx(signedTx tx.SignedTx) (interface{}, types.Error) {
func (i *InteropEndpoints) SendTx(signedTx tx.SignedTx) (interface{}, rpctypes.Error) {
// Check if the RPC is actually registered, if not it won't be possible to assert soundness (in the future once we are stateless won't be needed)
if err := i.executor.CheckTx(i.ctx, signedTx); err != nil {
return "0x0", types.NewRPCError(types.DefaultErrorCode, fmt.Sprintf("there is no RPC registered for %s", signedTx.Tx.L1Contract))
return "0x0", rpctypes.NewRPCError(rpctypes.DefaultErrorCode, fmt.Sprintf("there is no RPC registered for %s", signedTx.Tx.L1Contract))
}

// Verify ZKP using eth_call
if err := i.executor.Verify(signedTx); err != nil {
return "0x0", types.NewRPCError(types.DefaultErrorCode, fmt.Sprintf("failed to verify tx: %s", err))
return "0x0", rpctypes.NewRPCError(rpctypes.DefaultErrorCode, fmt.Sprintf("failed to verify tx: %s", err))
}

if err := i.executor.Execute(signedTx); err != nil {
return "0x0", types.NewRPCError(types.DefaultErrorCode, fmt.Sprintf("failed to execute tx: %s", err))
return "0x0", rpctypes.NewRPCError(rpctypes.DefaultErrorCode, fmt.Sprintf("failed to execute tx: %s", err))
}

// Send L1 tx
dbTx, err := i.db.BeginStateTransaction(i.ctx)
if err != nil {
return "0x0", types.NewRPCError(types.DefaultErrorCode, fmt.Sprintf("failed to begin dbTx, error: %s", err))
return "0x0", rpctypes.NewRPCError(rpctypes.DefaultErrorCode, fmt.Sprintf("failed to begin dbTx, error: %s", err))
}

_, err = i.executor.Settle(signedTx, dbTx)
if err != nil {
if errRollback := dbTx.Rollback(i.ctx); errRollback != nil {
log.Error("rollback err: ", errRollback)
}
return "0x0", types.NewRPCError(types.DefaultErrorCode, fmt.Sprintf("failed to add tx to ethTxMan, error: %s", err))
return "0x0", rpctypes.NewRPCError(rpctypes.DefaultErrorCode, fmt.Sprintf("failed to add tx to ethTxMan, error: %s", err))
}
if err := dbTx.Commit(i.ctx); err != nil {
return "0x0", types.NewRPCError(types.DefaultErrorCode, fmt.Sprintf("failed to commit dbTx, error: %s", err))
return "0x0", rpctypes.NewRPCError(rpctypes.DefaultErrorCode, fmt.Sprintf("failed to commit dbTx, error: %s", err))
}
log.Debugf("successfuly added tx %s to ethTxMan", signedTx.Tx.Hash().Hex())

return signedTx.Tx.Hash(), nil
}

func (i *InteropEndpoints) GetTxStatus(hash common.Hash) (result interface{}, err types.Error) {
func (i *InteropEndpoints) GetTxStatus(hash common.Hash) (result interface{}, err rpctypes.Error) {
dbTx, innerErr := i.db.BeginStateTransaction(i.ctx)
if innerErr != nil {
result = "0x0"
err = types.NewRPCError(types.DefaultErrorCode, fmt.Sprintf("failed to begin dbTx, error: %s", innerErr))
err = rpctypes.NewRPCError(rpctypes.DefaultErrorCode, fmt.Sprintf("failed to begin dbTx, error: %s", innerErr))

return
}

defer func() {
if innerErr := dbTx.Rollback(i.ctx); innerErr != nil {
result = "0x0"
err = types.NewRPCError(types.DefaultErrorCode, fmt.Sprintf("failed to rollback dbTx, error: %s", innerErr))
err = rpctypes.NewRPCError(rpctypes.DefaultErrorCode, fmt.Sprintf("failed to rollback dbTx, error: %s", innerErr))
}
}()

result, innerErr = i.executor.GetTxStatus(i.ctx, hash, dbTx)
if innerErr != nil {
result = "0x0"
err = types.NewRPCError(types.DefaultErrorCode, fmt.Sprintf("failed to get tx, error: %s", innerErr))
err = rpctypes.NewRPCError(rpctypes.DefaultErrorCode, fmt.Sprintf("failed to get tx, error: %s", innerErr))

return
}
Expand Down
Loading

0 comments on commit 72afdc4

Please sign in to comment.