Skip to content

Commit

Permalink
jsonrpc server allow 3rd party services (0xPolygonHermez#2130)
Browse files Browse the repository at this point in the history
jsonrpc server allow 3rd party services
  • Loading branch information
arnaubennassar authored May 30, 2023
1 parent d2f07f8 commit 88ef97f
Show file tree
Hide file tree
Showing 10 changed files with 223 additions and 129 deletions.
45 changes: 44 additions & 1 deletion cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,50 @@ func runJSONRPCServer(c config.Config, chainID uint64, pool *pool.Pool, st *stat
storage := jsonrpc.NewStorage()
c.RPC.MaxCumulativeGasUsed = c.Sequencer.MaxCumulativeGasUsed

if err := jsonrpc.NewServer(c.RPC, chainID, pool, st, storage, apis).Start(); err != nil {
services := []jsonrpc.Service{}
if _, ok := apis[jsonrpc.APIEth]; ok {
services = append(services, jsonrpc.Service{
Name: jsonrpc.APIEth,
Service: jsonrpc.NewEthEndpoints(c.RPC, chainID, pool, st, storage),
})
}

if _, ok := apis[jsonrpc.APINet]; ok {
services = append(services, jsonrpc.Service{
Name: jsonrpc.APINet,
Service: jsonrpc.NewNetEndpoints(chainID),
})
}

if _, ok := apis[jsonrpc.APIZKEVM]; ok {
services = append(services, jsonrpc.Service{
Name: jsonrpc.APIZKEVM,
Service: jsonrpc.NewZKEVMEndpoints(st),
})
}

if _, ok := apis[jsonrpc.APITxPool]; ok {
services = append(services, jsonrpc.Service{
Name: jsonrpc.APITxPool,
Service: &jsonrpc.TxPoolEndpoints{},
})
}

if _, ok := apis[jsonrpc.APIDebug]; ok {
services = append(services, jsonrpc.Service{
Name: jsonrpc.APIDebug,
Service: jsonrpc.NewDebugEndpoints(st),
})
}

if _, ok := apis[jsonrpc.APIWeb3]; ok {
services = append(services, jsonrpc.Service{
Name: jsonrpc.APIWeb3,
Service: &jsonrpc.Web3Endpoints{},
})
}

if err := jsonrpc.NewServer(c.RPC, chainID, pool, st, storage, services).Start(); err != nil {
log.Fatal(err)
}
}
Expand Down
22 changes: 15 additions & 7 deletions jsonrpc/dbtxmanager.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,27 +7,35 @@ import (
"github.com/jackc/pgx/v4"
)

type dbTxManager struct{}
// DBTxManager allows to do scopped DB txs
type DBTxManager struct{}

type dbTxScopedFn func(ctx context.Context, dbTx pgx.Tx) (interface{}, types.Error)
// DBTxScopedFn function to do scopped DB txs
type DBTxScopedFn func(ctx context.Context, dbTx pgx.Tx) (interface{}, types.Error)

func (f *dbTxManager) NewDbTxScope(st types.StateInterface, scopedFn dbTxScopedFn) (interface{}, types.Error) {
// DBTxer interface to begin DB txs
type DBTxer interface {
BeginStateTransaction(ctx context.Context) (pgx.Tx, error)
}

// NewDbTxScope function to initiate DB scopped txs
func (f *DBTxManager) NewDbTxScope(db DBTxer, scopedFn DBTxScopedFn) (interface{}, types.Error) {
ctx := context.Background()
dbTx, err := st.BeginStateTransaction(ctx)
dbTx, err := db.BeginStateTransaction(ctx)
if err != nil {
return rpcErrorResponse(types.DefaultErrorCode, "failed to connect to the state", err)
return RPCErrorResponse(types.DefaultErrorCode, "failed to connect to the state", err)
}

v, rpcErr := scopedFn(ctx, dbTx)
if rpcErr != nil {
if txErr := dbTx.Rollback(context.Background()); txErr != nil {
return rpcErrorResponse(types.DefaultErrorCode, "failed to rollback db transaction", txErr)
return RPCErrorResponse(types.DefaultErrorCode, "failed to rollback db transaction", txErr)
}
return v, rpcErr
}

if txErr := dbTx.Commit(context.Background()); txErr != nil {
return rpcErrorResponse(types.DefaultErrorCode, "failed to commit db transaction", txErr)
return RPCErrorResponse(types.DefaultErrorCode, "failed to commit db transaction", txErr)
}
return v, rpcErr
}
4 changes: 2 additions & 2 deletions jsonrpc/dbtxmanager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
func TestNewDbTxScope(t *testing.T) {
type testCase struct {
Name string
Fn dbTxScopedFn
Fn DBTxScopedFn
ExpectedResult interface{}
ExpectedError types.Error
SetupMocks func(s *mocks.StateMock, d *mocks.DBTxMock)
Expand Down Expand Up @@ -82,7 +82,7 @@ func TestNewDbTxScope(t *testing.T) {
},
}

dbTxManager := dbTxManager{}
dbTxManager := DBTxManager{}
s := mocks.NewStateMock(t)
d := mocks.NewDBTxMock(t)

Expand Down
19 changes: 13 additions & 6 deletions jsonrpc/endpoints_debug.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,14 @@ var defaultTraceConfig = &traceConfig{
// DebugEndpoints is the debug jsonrpc endpoint
type DebugEndpoints struct {
state types.StateInterface
txMan dbTxManager
txMan DBTxManager
}

// NewDebugEndpoints returns DebugEndpoints
func NewDebugEndpoints(state types.StateInterface) *DebugEndpoints {
return &DebugEndpoints{
state: state,
}
}

type traceConfig struct {
Expand Down Expand Up @@ -86,7 +93,7 @@ func (d *DebugEndpoints) TraceBlockByNumber(number types.BlockNumber, cfg *trace
if errors.Is(err, state.ErrNotFound) {
return nil, types.NewRPCError(types.DefaultErrorCode, fmt.Sprintf("block #%d not found", blockNumber))
} else if err == state.ErrNotFound {
return rpcErrorResponse(types.DefaultErrorCode, "failed to get block by number", err)
return RPCErrorResponse(types.DefaultErrorCode, "failed to get block by number", err)
}

traces, rpcErr := d.buildTraceBlock(ctx, block.Transactions(), cfg, dbTx)
Expand All @@ -106,7 +113,7 @@ func (d *DebugEndpoints) TraceBlockByHash(hash types.ArgHash, cfg *traceConfig)
if errors.Is(err, state.ErrNotFound) {
return nil, types.NewRPCError(types.DefaultErrorCode, fmt.Sprintf("block %s not found", hash.Hash().String()))
} else if err == state.ErrNotFound {
return rpcErrorResponse(types.DefaultErrorCode, "failed to get block by hash", err)
return RPCErrorResponse(types.DefaultErrorCode, "failed to get block by hash", err)
}

traces, rpcErr := d.buildTraceBlock(ctx, block.Transactions(), cfg, dbTx)
Expand All @@ -124,7 +131,7 @@ func (d *DebugEndpoints) buildTraceBlock(ctx context.Context, txs []*ethTypes.Tr
traceTransaction, err := d.buildTraceTransaction(ctx, tx.Hash(), cfg, dbTx)
if err != nil {
errMsg := fmt.Sprintf("failed to get trace for transaction %v", tx.Hash().String())
return rpcErrorResponse(types.DefaultErrorCode, errMsg, err)
return RPCErrorResponse(types.DefaultErrorCode, errMsg, err)
}
traceBlockTransaction := traceBlockTransactionResponse{
Result: traceTransaction,
Expand All @@ -143,7 +150,7 @@ func (d *DebugEndpoints) buildTraceTransaction(ctx context.Context, hash common.

// check tracer
if traceCfg.Tracer != nil && *traceCfg.Tracer != "" && !isBuiltInTracer(*traceCfg.Tracer) && !isJSCustomTracer(*traceCfg.Tracer) {
return rpcErrorResponse(types.DefaultErrorCode, "invalid tracer", nil)
return RPCErrorResponse(types.DefaultErrorCode, "invalid tracer", nil)
}

stateTraceConfig := state.TraceConfig{
Expand All @@ -156,7 +163,7 @@ func (d *DebugEndpoints) buildTraceTransaction(ctx context.Context, hash common.
}
result, err := d.state.DebugTransaction(ctx, hash, stateTraceConfig, dbTx)
if errors.Is(err, state.ErrNotFound) {
return rpcErrorResponse(types.DefaultErrorCode, "transaction not found", nil)
return RPCErrorResponse(types.DefaultErrorCode, "transaction not found", nil)
} else if err != nil {
const errorMessage = "failed to get trace"
log.Errorf("%v: %v", errorMessage, err)
Expand Down
Loading

0 comments on commit 88ef97f

Please sign in to comment.