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

Commit

Permalink
Use the correct package for rpc.Error (#49)
Browse files Browse the repository at this point in the history
  • Loading branch information
Stefan-Ethernal authored Jan 23, 2024
1 parent 445fe58 commit d8f2b45
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 23 deletions.
6 changes: 3 additions & 3 deletions interop/executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import (
"github.com/0xPolygon/beethoven/tx"
"github.com/0xPolygon/beethoven/types"

jRPC "github.com/0xPolygon/cdk-data-availability/rpc"
"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"
Expand Down Expand Up @@ -168,11 +168,11 @@ func (e *Executor) Settle(ctx context.Context, signedTx tx.SignedTx, dbTx pgx.Tx
return signedTx.Tx.Hash(), nil
}

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

return
}
Expand Down
11 changes: 6 additions & 5 deletions interop/executor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,7 @@ import (
"math/big"
"testing"

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

jRPC "github.com/0xPolygon/cdk-data-availability/rpc"
"github.com/0xPolygonHermez/zkevm-node/ethtxmanager"
rpctypes "github.com/0xPolygonHermez/zkevm-node/jsonrpc/types"
"github.com/0xPolygonHermez/zkevm-node/log"
Expand All @@ -18,6 +15,10 @@ import (
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/require"

"github.com/0xPolygon/beethoven/config"
"github.com/0xPolygon/beethoven/mocks"
"github.com/0xPolygon/beethoven/tx"
)

func TestNewExecutor(t *testing.T) {
Expand Down Expand Up @@ -269,7 +270,7 @@ func TestExecutor_GetTxStatus(t *testing.T) {

hash := common.HexToHash("0x1234567890abcdef")
expectedResult := "0x1"
expectedError := rpctypes.NewRPCError(rpctypes.DefaultErrorCode, "failed to get tx, error: sampleError")
expectedError := jRPC.NewRPCError(rpctypes.DefaultErrorCode, "failed to get tx, error: sampleError")

ethTxManager.On("Result", mock.Anything, ethTxManOwner, hash.Hex(), dbTx).
Return(ethtxmanager.MonitoredTxResult{
Expand Down
29 changes: 14 additions & 15 deletions rpc/rpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,13 @@ import (
"context"
"fmt"

"github.com/0xPolygon/beethoven/interop"
"github.com/0xPolygon/beethoven/types"

rpctypes "github.com/0xPolygonHermez/zkevm-node/jsonrpc/types"
jRPC "github.com/0xPolygon/cdk-data-availability/rpc"
"github.com/0xPolygonHermez/zkevm-node/log"
"github.com/ethereum/go-ethereum/common"

"github.com/0xPolygon/beethoven/interop"
"github.com/0xPolygon/beethoven/tx"
"github.com/0xPolygon/beethoven/types"
)

// INTEROP is the namespace of the interop service
Expand Down Expand Up @@ -40,62 +39,62 @@ func NewInteropEndpoints(
}
}

func (i *InteropEndpoints) SendTx(signedTx tx.SignedTx) (interface{}, rpctypes.Error) {
func (i *InteropEndpoints) SendTx(signedTx tx.SignedTx) (interface{}, jRPC.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(signedTx); err != nil {
return "0x0", rpctypes.NewRPCError(rpctypes.DefaultErrorCode, fmt.Sprintf("there is no RPC registered for %d", signedTx.Tx.RollupID))
return "0x0", jRPC.NewRPCError(jRPC.DefaultErrorCode, fmt.Sprintf("there is no RPC registered for %d", signedTx.Tx.RollupID))
}

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

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

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

_, err = i.executor.Settle(i.ctx, signedTx, dbTx)
if err != nil {
if errRollback := dbTx.Rollback(i.ctx); errRollback != nil {
log.Error("rollback err: ", errRollback)
}
return "0x0", rpctypes.NewRPCError(rpctypes.DefaultErrorCode, fmt.Sprintf("failed to add tx to ethTxMan, error: %s", err))
return "0x0", jRPC.NewRPCError(jRPC.DefaultErrorCode, fmt.Sprintf("failed to add tx to ethTxMan, error: %s", err))
}
if err := dbTx.Commit(i.ctx); err != nil {
return "0x0", rpctypes.NewRPCError(rpctypes.DefaultErrorCode, fmt.Sprintf("failed to commit dbTx, error: %s", err))
return "0x0", jRPC.NewRPCError(jRPC.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 rpctypes.Error) {
func (i *InteropEndpoints) GetTxStatus(hash common.Hash) (result interface{}, err jRPC.Error) {
dbTx, innerErr := i.db.BeginStateTransaction(i.ctx)
if innerErr != nil {
result = "0x0"
err = rpctypes.NewRPCError(rpctypes.DefaultErrorCode, fmt.Sprintf("failed to begin dbTx, error: %s", innerErr))
err = jRPC.NewRPCError(jRPC.DefaultErrorCode, fmt.Sprintf("failed to begin dbTx, error: %s", innerErr))

return
}

defer func() {
if innerErr := dbTx.Rollback(i.ctx); innerErr != nil {
result = "0x0"
err = rpctypes.NewRPCError(rpctypes.DefaultErrorCode, fmt.Sprintf("failed to rollback dbTx, error: %s", innerErr))
err = jRPC.NewRPCError(jRPC.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 = rpctypes.NewRPCError(rpctypes.DefaultErrorCode, fmt.Sprintf("failed to get tx, error: %s", innerErr))
err = jRPC.NewRPCError(jRPC.DefaultErrorCode, fmt.Sprintf("failed to get tx, error: %s", innerErr))

return
}
Expand Down

0 comments on commit d8f2b45

Please sign in to comment.