From ae911b8003ec7b654fb7aa76f0eae55f4bb69350 Mon Sep 17 00:00:00 2001 From: zadykian Date: Thu, 23 Jan 2025 10:27:56 +0000 Subject: [PATCH] Sync Committee: Tracer RPC Client * 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); --- .../synccommittee/internal/types/errors.go | 1 + .../prover/commands/partial_proof.go | 7 ++- .../synccommittee/prover/task_handler.go | 4 -- .../prover/tracer/api/tracer_rpc_client.go | 51 +++++++++++++++++++ .../internal/mpttracer/contract_reader.go | 6 +-- .../tracer/internal/mpttracer/mpt_tracer.go | 6 +-- .../synccommittee/prover/tracer/state_db.go | 8 +-- .../synccommittee/prover/tracer/tracer.go | 8 +-- .../prover/tracer/tracer_mock_client_test.go | 3 +- 9 files changed, 70 insertions(+), 24 deletions(-) create mode 100644 nil/services/synccommittee/prover/tracer/api/tracer_rpc_client.go diff --git a/nil/services/synccommittee/internal/types/errors.go b/nil/services/synccommittee/internal/types/errors.go index ce3effca3..ebd59b458 100644 --- a/nil/services/synccommittee/internal/types/errors.go +++ b/nil/services/synccommittee/internal/types/errors.go @@ -51,6 +51,7 @@ const ( ) var RetryableErrors = map[TaskErrType]bool{ + TaskErrTimeout: true, TaskErrIO: true, TaskErrTerminated: true, TaskErrUnknown: true, diff --git a/nil/services/synccommittee/prover/commands/partial_proof.go b/nil/services/synccommittee/prover/commands/partial_proof.go index 59851a16b..9683683e5 100644 --- a/nil/services/synccommittee/prover/commands/partial_proof.go +++ b/nil/services/synccommittee/prover/commands/partial_proof.go @@ -7,17 +7,16 @@ 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 @@ -25,7 +24,7 @@ type partialProofCmd struct { 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, diff --git a/nil/services/synccommittee/prover/task_handler.go b/nil/services/synccommittee/prover/task_handler.go index dc87e3ec2..23c24c01a 100644 --- a/nil/services/synccommittee/prover/task_handler.go +++ b/nil/services/synccommittee/prover/task_handler.go @@ -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" @@ -27,7 +25,6 @@ type taskHandler struct { timer common.Timer logger zerolog.Logger config taskHandlerConfig - client client.Client } func newTaskHandler( @@ -41,7 +38,6 @@ func newTaskHandler( timer: timer, logger: logger, config: config, - client: NewRPCClient(config.NilRpcEndpoint, logging.NewLogger("client")), } } diff --git a/nil/services/synccommittee/prover/tracer/api/tracer_rpc_client.go b/nil/services/synccommittee/prover/tracer/api/tracer_rpc_client.go new file mode 100644 index 000000000..81ee6c0d1 --- /dev/null +++ b/nil/services/synccommittee/prover/tracer/api/tracer_rpc_client.go @@ -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) +} diff --git a/nil/services/synccommittee/prover/tracer/internal/mpttracer/contract_reader.go b/nil/services/synccommittee/prover/tracer/internal/mpttracer/contract_reader.go index dd1fa0baf..09b6e54e4 100644 --- a/nil/services/synccommittee/prover/tracer/internal/mpttracer/contract_reader.go +++ b/nil/services/synccommittee/prover/tracer/internal/mpttracer/contract_reader.go @@ -3,7 +3,6 @@ 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" @@ -11,6 +10,7 @@ import ( "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 { @@ -45,7 +45,7 @@ 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 @@ -53,7 +53,7 @@ type DebugApiContractReader struct { // NewDebugApiContractReader creates a new DebugApiContractReader func NewDebugApiContractReader( - client client.Client, + client api.TracerRpcClient, shardBlockNumber types.BlockNumber, rwTx db.RwTx, shardId types.ShardId, diff --git a/nil/services/synccommittee/prover/tracer/internal/mpttracer/mpt_tracer.go b/nil/services/synccommittee/prover/tracer/internal/mpttracer/mpt_tracer.go index c883c19c3..332c360de 100644 --- a/nil/services/synccommittee/prover/tracer/internal/mpttracer/mpt_tracer.go +++ b/nil/services/synccommittee/prover/tracer/internal/mpttracer/mpt_tracer.go @@ -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 @@ -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) diff --git a/nil/services/synccommittee/prover/tracer/state_db.go b/nil/services/synccommittee/prover/tracer/state_db.go index c5326966b..88a01e051 100644 --- a/nil/services/synccommittee/prover/tracer/state_db.go +++ b/nil/services/synccommittee/prover/tracer/state_db.go @@ -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" @@ -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 @@ -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, @@ -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, diff --git a/nil/services/synccommittee/prover/tracer/tracer.go b/nil/services/synccommittee/prover/tracer/tracer.go index 1cf46c689..02bc85499 100644 --- a/nil/services/synccommittee/prover/tracer/tracer.go +++ b/nil/services/synccommittee/prover/tracer/tracer.go @@ -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" ) @@ -20,7 +20,7 @@ type RemoteTracer interface { } type RemoteTracerImpl struct { - client client.Client + client api.TracerRpcClient logger zerolog.Logger } @@ -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, @@ -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 diff --git a/nil/services/synccommittee/prover/tracer/tracer_mock_client_test.go b/nil/services/synccommittee/prover/tracer/tracer_mock_client_test.go index 6d0d251fa..7f6312ab9 100644 --- a/nil/services/synccommittee/prover/tracer/tracer_mock_client_test.go +++ b/nil/services/synccommittee/prover/tracer/tracer_mock_client_test.go @@ -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