Skip to content

Commit

Permalink
Sync Committee: Tracer RPC Client
Browse files Browse the repository at this point in the history
* Declared `api.TracerRpcClient` interface with a narrowed method set;
* Created `rpcClientDecorator` to wrap all rpc errors into `ErrRpcCallFailed` (which is handled in prover's task handler);
  • Loading branch information
zadykian committed Jan 29, 2025
1 parent 52366e6 commit ae911b8
Show file tree
Hide file tree
Showing 9 changed files with 70 additions and 24 deletions.
1 change: 1 addition & 0 deletions nil/services/synccommittee/internal/types/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ const (
)

var RetryableErrors = map[TaskErrType]bool{
TaskErrTimeout: true,
TaskErrIO: true,
TaskErrTerminated: true,
TaskErrUnknown: true,
Expand Down
7 changes: 3 additions & 4 deletions nil/services/synccommittee/prover/commands/partial_proof.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,24 @@ import (
"path/filepath"
"slices"

"github.com/NilFoundation/nil/nil/client"
"github.com/NilFoundation/nil/nil/common/logging"
"github.com/NilFoundation/nil/nil/services/rpc/transport"
"github.com/NilFoundation/nil/nil/services/synccommittee/internal/rpc"
"github.com/NilFoundation/nil/nil/services/synccommittee/internal/types"
"github.com/NilFoundation/nil/nil/services/synccommittee/prover/tracer"
"github.com/NilFoundation/nil/nil/services/synccommittee/prover/tracer/api"
"github.com/rs/zerolog"
)

type partialProofCmd struct {
client client.Client
client api.TracerRpcClient
logger zerolog.Logger
nilRpcEndpoint string
cmdCommon
}

func NewPartialProofCmd(config CommandConfig, logger zerolog.Logger) Command {
return &partialProofCmd{
client: rpc.NewRetryClient(config.NilRpcEndpoint, logging.NewLogger("client")),
client: api.NewRpcClientDecorator(rpc.NewRetryClient(config.NilRpcEndpoint, logger)),
nilRpcEndpoint: config.NilRpcEndpoint,
cmdCommon: makeCmdCommon(config),
logger: logger,
Expand Down
4 changes: 0 additions & 4 deletions nil/services/synccommittee/prover/task_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,7 @@ import (
"strings"
"syscall"

"github.com/NilFoundation/nil/nil/client"
"github.com/NilFoundation/nil/nil/common"
"github.com/NilFoundation/nil/nil/common/logging"
"github.com/NilFoundation/nil/nil/services/synccommittee/internal/api"
"github.com/NilFoundation/nil/nil/services/synccommittee/internal/log"
"github.com/NilFoundation/nil/nil/services/synccommittee/internal/storage"
Expand All @@ -27,7 +25,6 @@ type taskHandler struct {
timer common.Timer
logger zerolog.Logger
config taskHandlerConfig
client client.Client
}

func newTaskHandler(
Expand All @@ -41,7 +38,6 @@ func newTaskHandler(
timer: timer,
logger: logger,
config: config,
client: NewRPCClient(config.NilRpcEndpoint, logging.NewLogger("client")),
}
}

Expand Down
51 changes: 51 additions & 0 deletions nil/services/synccommittee/prover/tracer/api/tracer_rpc_client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package api

import (
"context"
"fmt"

"github.com/NilFoundation/nil/nil/internal/types"
"github.com/NilFoundation/nil/nil/services/rpc/jsonrpc"
scTypes "github.com/NilFoundation/nil/nil/services/synccommittee/internal/types"
)

type TracerRpcClient interface {
GetBlock(ctx context.Context, shardId types.ShardId, blockId any, fullTx bool) (*jsonrpc.RPCBlock, error)

GetDebugBlock(ctx context.Context, shardId types.ShardId, blockId any, fullTx bool) (*jsonrpc.DebugRPCBlock, error)

GetDebugContract(ctx context.Context, contractAddr types.Address, blockId any) (*jsonrpc.DebugRPCContract, error)
}

type rpcClientDecorator struct {
inner TracerRpcClient
}

func NewRpcClientDecorator(inner TracerRpcClient) TracerRpcClient {
return &rpcClientDecorator{
inner: inner,
}
}

func (c *rpcClientDecorator) GetBlock(ctx context.Context, shardId types.ShardId, blockId any, fullTx bool) (*jsonrpc.RPCBlock, error) {
block, err := c.inner.GetBlock(ctx, shardId, blockId, fullTx)
return block, c.wrapError(err)
}

func (c *rpcClientDecorator) GetDebugBlock(ctx context.Context, shardId types.ShardId, blockId any, fullTx bool) (*jsonrpc.DebugRPCBlock, error) {
block, err := c.inner.GetDebugBlock(ctx, shardId, blockId, fullTx)
return block, c.wrapError(err)
}

func (c *rpcClientDecorator) GetDebugContract(ctx context.Context, contractAddr types.Address, blockId any) (*jsonrpc.DebugRPCContract, error) {
contract, err := c.inner.GetDebugContract(ctx, contractAddr, blockId)
return contract, c.wrapError(err)
}

func (*rpcClientDecorator) wrapError(cause error) error {
if cause == nil {
return nil
}

return fmt.Errorf("%w: %w", scTypes.ErrRpcCallFailed, cause)
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ package mpttracer
import (
"context"

"github.com/NilFoundation/nil/nil/client"
"github.com/NilFoundation/nil/nil/common"
"github.com/NilFoundation/nil/nil/internal/db"
"github.com/NilFoundation/nil/nil/internal/execution"
"github.com/NilFoundation/nil/nil/internal/mpt"
"github.com/NilFoundation/nil/nil/internal/types"
"github.com/NilFoundation/nil/nil/services/rpc/jsonrpc"
"github.com/NilFoundation/nil/nil/services/rpc/transport"
"github.com/NilFoundation/nil/nil/services/synccommittee/prover/tracer/api"
)

type DeserializedDebugRPCContract struct {
Expand Down Expand Up @@ -45,15 +45,15 @@ func deserializeDebugRPCContract(debugRPCContract *jsonrpc.DebugRPCContract) (*D

// DebugApiContractReader implements ContractReader for debug API
type DebugApiContractReader struct {
client client.Client
client api.TracerRpcClient
shardBlockNumber types.BlockNumber
rwTx db.RwTx
shardId types.ShardId
}

// NewDebugApiContractReader creates a new DebugApiContractReader
func NewDebugApiContractReader(
client client.Client,
client api.TracerRpcClient,
shardBlockNumber types.BlockNumber,
rwTx db.RwTx,
shardId types.ShardId,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,12 @@ import (
"context"
"errors"

"github.com/NilFoundation/nil/nil/client"
"github.com/NilFoundation/nil/nil/common"
"github.com/NilFoundation/nil/nil/internal/db"
"github.com/NilFoundation/nil/nil/internal/execution"
"github.com/NilFoundation/nil/nil/internal/mpt"
"github.com/NilFoundation/nil/nil/internal/types"
"github.com/rs/zerolog"
"github.com/NilFoundation/nil/nil/services/synccommittee/prover/tracer/api"
)

// MPTTracer handles interaction with Merkle Patricia Tries
Expand All @@ -26,11 +25,10 @@ var _ = (*MPTTracer)(nil)

// New creates a new MPTTracer using a debug API client
func New(
client client.Client,
client api.TracerRpcClient,
shardBlockNumber types.BlockNumber,
rwTx db.RwTx,
shardId types.ShardId,
logger zerolog.Logger,
) *MPTTracer {
debugApiReader := NewDebugApiContractReader(client, shardBlockNumber, rwTx, shardId)
return NewWithReader(debugApiReader, rwTx, shardId)
Expand Down
8 changes: 4 additions & 4 deletions nil/services/synccommittee/prover/tracer/state_db.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"fmt"
"runtime/debug"

"github.com/NilFoundation/nil/nil/client"
"github.com/NilFoundation/nil/nil/common"
"github.com/NilFoundation/nil/nil/internal/config"
"github.com/NilFoundation/nil/nil/internal/db"
Expand All @@ -15,12 +14,13 @@ import (
"github.com/NilFoundation/nil/nil/internal/tracing"
"github.com/NilFoundation/nil/nil/internal/types"
"github.com/NilFoundation/nil/nil/internal/vm"
"github.com/NilFoundation/nil/nil/services/synccommittee/prover/tracer/api"
"github.com/NilFoundation/nil/nil/services/synccommittee/prover/tracer/internal/mpttracer"
"github.com/rs/zerolog"
)

type TracerStateDB struct {
client client.Client
client api.TracerRpcClient
shardId types.ShardId
shardBlockNumber types.BlockNumber
InTransactions []*types.Transaction
Expand Down Expand Up @@ -156,7 +156,7 @@ func (mtc *transactionTraceContext) saveTransactionTraces(dst ExecutionTraces) e
func NewTracerStateDB(
ctx context.Context,
aggTraces ExecutionTraces,
client client.Client,
client api.TracerRpcClient,
shardId types.ShardId,
shardBlockNumber types.BlockNumber,
blkContext *vm.BlockContext,
Expand All @@ -170,7 +170,7 @@ func NewTracerStateDB(

return &TracerStateDB{
client: client,
mptTracer: mpttracer.New(client, shardBlockNumber, rwTx, shardId, logger),
mptTracer: mpttracer.New(client, shardBlockNumber, rwTx, shardId),
shardId: shardId,
shardBlockNumber: shardBlockNumber,
blkContext: blkContext,
Expand Down
8 changes: 4 additions & 4 deletions nil/services/synccommittee/prover/tracer/tracer.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ import (
"errors"
"math/big"

"github.com/NilFoundation/nil/nil/client"
"github.com/NilFoundation/nil/nil/common"
"github.com/NilFoundation/nil/nil/common/logging"
"github.com/NilFoundation/nil/nil/internal/db"
"github.com/NilFoundation/nil/nil/internal/types"
"github.com/NilFoundation/nil/nil/internal/vm"
"github.com/NilFoundation/nil/nil/services/rpc/transport"
"github.com/NilFoundation/nil/nil/services/synccommittee/prover/tracer/api"
"github.com/rs/zerolog"
)

Expand All @@ -20,7 +20,7 @@ type RemoteTracer interface {
}

type RemoteTracerImpl struct {
client client.Client
client api.TracerRpcClient
logger zerolog.Logger
}

Expand All @@ -33,7 +33,7 @@ type TraceConfig struct {
MarshalMode MarshalMode
}

func NewRemoteTracer(client client.Client, logger zerolog.Logger) (*RemoteTracerImpl, error) {
func NewRemoteTracer(client api.TracerRpcClient, logger zerolog.Logger) (*RemoteTracerImpl, error) {
return &RemoteTracerImpl{
client: client,
logger: logger,
Expand Down Expand Up @@ -137,7 +137,7 @@ func (rt *RemoteTracerImpl) GetBlockTraces(
return nil
}

func GenerateTrace(ctx context.Context, rpcClient client.Client, cfg *TraceConfig) error {
func GenerateTrace(ctx context.Context, rpcClient api.TracerRpcClient, cfg *TraceConfig) error {
remoteTracer, err := NewRemoteTracer(rpcClient, logging.NewLogger("tracer"))
if err != nil {
return err
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,14 @@ import (
"github.com/NilFoundation/nil/nil/internal/vm"
"github.com/NilFoundation/nil/nil/services/rpc/jsonrpc"
"github.com/NilFoundation/nil/nil/services/rpc/transport"
"github.com/NilFoundation/nil/nil/services/synccommittee/prover/tracer/api"
"github.com/stretchr/testify/suite"
)

type TracerMockClientTestSuite struct {
suite.Suite

cl client.Client
cl api.TracerRpcClient
shardId types.ShardId
accounts map[types.Address]types.Code
smartAccount types.Address
Expand Down

0 comments on commit ae911b8

Please sign in to comment.