From 956cdc076e3ab5bf86c87cf1de4e6ebe10076b21 Mon Sep 17 00:00:00 2001 From: cheatfate Date: Sat, 3 Jun 2023 13:31:44 +0300 Subject: [PATCH] After bump fixes. --- beacon_chain/validator_client/api.nim | 162 ++++++----------------- beacon_chain/validator_client/common.nim | 3 +- 2 files changed, 46 insertions(+), 119 deletions(-) diff --git a/beacon_chain/validator_client/api.nim b/beacon_chain/validator_client/api.nim index d4b8594bb..c7c0a25ed 100644 --- a/beacon_chain/validator_client/api.nim +++ b/beacon_chain/validator_client/api.nim @@ -678,6 +678,12 @@ template handle500(): untyped {.dirty.} = node.updateStatus(RestBeaconNodeStatus.InternalError, failure) failures.add(failure) +template handle501(): untyped {.dirty.} = + let failure = ApiNodeFailure.init(ApiFailure.NotImplemented, RequestName, + strategy, node, response.status, response.getErrorMessage()) + node.updateStatus(RestBeaconNodeStatus.Incompatible, failure) + failures.add(failure) + template handle503(): untyped {.dirty.} = let failure = ApiNodeFailure.init(ApiFailure.NotSynced, RequestName, strategy, node, response.status, response.getErrorMessage()) @@ -2439,11 +2445,8 @@ proc submitBeaconCommitteeSelections*( data: seq[RestBeaconCommitteeSelection], strategy: ApiStrategyKind ): Future[SubmitBeaconCommitteeSelectionsResponse] {.async.} = - logScope: - request = "submitBeaconCommitteeSelections" - strategy = $strategy - - const ErrorMessage = "Unable to submit beacon committee selections" + const + RequestName = "submitBeaconCommitteeSelections" var failures: seq[ApiNodeFailure] @@ -2457,9 +2460,7 @@ proc submitBeaconCommitteeSelections*( {BeaconNodeRole.Duties}, submitBeaconCommitteeSelectionsPlain(it, data)): if apiResponse.isErr(): - debug ErrorMessage, endpoint = node, error = apiResponse.error - node.updateStatus(RestBeaconNodeStatus.Offline) - failures.add(ApiNodeFailure.init(node, ApiFailure.Communication)) + handleCommunicationError() ApiResponse[SubmitBeaconCommitteeSelectionsResponse].err( apiResponse.error) else: @@ -2469,43 +2470,28 @@ proc submitBeaconCommitteeSelections*( let res = decodeBytes(SubmitBeaconCommitteeSelectionsResponse, response.data, response.contentType) if res.isErr(): - node.updateStatus(RestBeaconNodeStatus.Unexpected) + handleUnexpectedData() ApiResponse[SubmitBeaconCommitteeSelectionsResponse].err($res.error) else: ApiResponse[SubmitBeaconCommitteeSelectionsResponse].ok(res.get()) of 400: - debug ResponseInvalidError, response_code = response.status, - endpoint = node, reason = response.getErrorMessage() - node.updateStatus(RestBeaconNodeStatus.Incompatible) - failures.add(ApiNodeFailure.init(node, ApiFailure.Invalid)) + handle400() ApiResponse[SubmitBeaconCommitteeSelectionsResponse].err( ResponseInvalidError) of 500: - debug ResponseInternalError, response_code = response.status, - endpoint = node, reason = response.getErrorMessage() - node.updateStatus(RestBeaconNodeStatus.InternalError) - failures.add(ApiNodeFailure.init(node, ApiFailure.Internal)) + handle500() ApiResponse[SubmitBeaconCommitteeSelectionsResponse].err( ResponseInternalError) of 501: - warn ResponseNotImplementedError, response_code = response.status, - endpoint = node, reason = response.getErrorMessage() - node.updateStatus(RestBeaconNodeStatus.Incompatible) - failures.add(ApiNodeFailure.init(node, ApiFailure.Invalid)) + handle501() ApiResponse[SubmitBeaconCommitteeSelectionsResponse].err( ResponseNotImplementedError) of 503: - debug ResponseNoSyncError, response_code = response.status, - endpoint = node, reason = response.getErrorMessage() - node.updateStatus(RestBeaconNodeStatus.NotSynced) - failures.add(ApiNodeFailure.init(node, ApiFailure.NotSynced)) + handle503() ApiResponse[SubmitBeaconCommitteeSelectionsResponse].err( ResponseNoSyncError) else: - debug ResponseUnexpectedError, response_code = response.status, - endpoint = node, reason = response.getErrorMessage() - node.updateStatus(RestBeaconNodeStatus.Unexpected) - failures.add(ApiNodeFailure.init(node, ApiFailure.Unexpected)) + handleUnexpectedCode() ApiResponse[SubmitBeaconCommitteeSelectionsResponse].err( ResponseUnexpectedError) @@ -2520,9 +2506,7 @@ proc submitBeaconCommitteeSelections*( {BeaconNodeRole.Duties}, submitBeaconCommitteeSelectionsPlain(it, data)): if apiResponse.isErr(): - debug ErrorMessage, endpoint = node, error = apiResponse.error - node.updateStatus(RestBeaconNodeStatus.Offline) - failures.add(ApiNodeFailure.init(node, ApiFailure.Communication)) + handleCommunicationError() false else: let response = apiResponse.get() @@ -2530,56 +2514,35 @@ proc submitBeaconCommitteeSelections*( of 200: let res = decodeBytes(SubmitBeaconCommitteeSelectionsResponse, response.data, response.contentType) - if res.isOk(): - return res.get() - debug ResponseDecodeError, response_code = response.status, - endpoint = node, reason = res.error - node.updateStatus(RestBeaconNodeStatus.Unexpected) - failures.add(ApiNodeFailure.init(node, ApiFailure.Invalid)) + if res.isOk(): return res.get() + handleUnexpectedData() false of 400: - debug ResponseInvalidError, response_code = response.status, - endpoint = node, reason = response.getErrorMessage() - node.updateStatus(RestBeaconNodeStatus.Incompatible) - failures.add(ApiNodeFailure.init(node, ApiFailure.Invalid)) + handle400() false of 500: - debug ResponseInternalError, response_code = response.status, - endpoint = node, reason = response.getErrorMessage() - node.updateStatus(RestBeaconNodeStatus.InternalError) - failures.add(ApiNodeFailure.init(node, ApiFailure.Internal)) + handle500() false of 501: - warn ResponseNotImplementedError, response_code = response.status, - endpoint = node, reason = response.getErrorMessage() - node.updateStatus(RestBeaconNodeStatus.Incompatible) - failures.add(ApiNodeFailure.init(node, ApiFailure.Invalid)) + handle501() false of 503: - debug ResponseNoSyncError, response_code = response.status, - endpoint = node, reason = response.getErrorMessage() - node.updateStatus(RestBeaconNodeStatus.NotSynced) - failures.add(ApiNodeFailure.init(node, ApiFailure.NotSynced)) + handle503() false else: - debug ResponseUnexpectedError, response_code = response.status, - endpoint = node, reason = response.getErrorMessage() - node.updateStatus(RestBeaconNodeStatus.Unexpected) - failures.add(ApiNodeFailure.init(node, ApiFailure.Unexpected)) + handleUnexpectedCode() false - raise (ref ValidatorApiError)(msg: ErrorMessage, data: failures) + raise (ref ValidatorApiError)( + msg: "Failed to submit beacon committee selections", data: failures) proc submitSyncCommitteeSelections*( vc: ValidatorClientRef, data: seq[RestSyncCommitteeSelection], strategy: ApiStrategyKind ): Future[SubmitSyncCommitteeSelectionsResponse] {.async.} = - logScope: - request = "submitSyncCommitteeSelections" - strategy = $strategy - - const ErrorMessage = "Unable to submit sync committee selections" + const + RequestName = "submitBeaconCommitteeSelections" var failures: seq[ApiNodeFailure] @@ -2593,9 +2556,7 @@ proc submitSyncCommitteeSelections*( {BeaconNodeRole.Duties}, submitSyncCommitteeSelectionsPlain(it, data)): if apiResponse.isErr(): - debug ErrorMessage, endpoint = node, error = apiResponse.error - node.updateStatus(RestBeaconNodeStatus.Offline) - failures.add(ApiNodeFailure.init(node, ApiFailure.Communication)) + handleCommunicationError() ApiResponse[SubmitSyncCommitteeSelectionsResponse].err( apiResponse.error) else: @@ -2605,43 +2566,28 @@ proc submitSyncCommitteeSelections*( let res = decodeBytes(SubmitSyncCommitteeSelectionsResponse, response.data, response.contentType) if res.isErr(): - node.updateStatus(RestBeaconNodeStatus.Unexpected) + handleUnexpectedData() ApiResponse[SubmitSyncCommitteeSelectionsResponse].err($res.error) else: ApiResponse[SubmitSyncCommitteeSelectionsResponse].ok(res.get()) of 400: - debug ResponseInvalidError, response_code = response.status, - endpoint = node, reason = response.getErrorMessage() - node.updateStatus(RestBeaconNodeStatus.Incompatible) - failures.add(ApiNodeFailure.init(node, ApiFailure.Invalid)) + handle400() ApiResponse[SubmitSyncCommitteeSelectionsResponse].err( ResponseInvalidError) of 500: - debug ResponseInternalError, response_code = response.status, - endpoint = node, reason = response.getErrorMessage() - node.updateStatus(RestBeaconNodeStatus.InternalError) - failures.add(ApiNodeFailure.init(node, ApiFailure.Internal)) + handle500() ApiResponse[SubmitSyncCommitteeSelectionsResponse].err( ResponseInternalError) of 501: - warn ResponseNotImplementedError, response_code = response.status, - endpoint = node, reason = response.getErrorMessage() - node.updateStatus(RestBeaconNodeStatus.Incompatible) - failures.add(ApiNodeFailure.init(node, ApiFailure.Invalid)) + handle501() ApiResponse[SubmitSyncCommitteeSelectionsResponse].err( ResponseNotImplementedError) of 503: - debug ResponseNoSyncError, response_code = response.status, - endpoint = node, reason = response.getErrorMessage() - node.updateStatus(RestBeaconNodeStatus.NotSynced) - failures.add(ApiNodeFailure.init(node, ApiFailure.NotSynced)) + handle503() ApiResponse[SubmitSyncCommitteeSelectionsResponse].err( ResponseNoSyncError) else: - debug ResponseUnexpectedError, response_code = response.status, - endpoint = node, reason = response.getErrorMessage() - node.updateStatus(RestBeaconNodeStatus.Unexpected) - failures.add(ApiNodeFailure.init(node, ApiFailure.Unexpected)) + handleUnexpectedCode() ApiResponse[SubmitSyncCommitteeSelectionsResponse].err( ResponseUnexpectedError) @@ -2656,9 +2602,7 @@ proc submitSyncCommitteeSelections*( {BeaconNodeRole.Duties}, submitSyncCommitteeSelectionsPlain(it, data)): if apiResponse.isErr(): - debug ErrorMessage, endpoint = node, error = apiResponse.error - node.updateStatus(RestBeaconNodeStatus.Offline) - failures.add(ApiNodeFailure.init(node, ApiFailure.Communication)) + handleCommunicationError() false else: let response = apiResponse.get() @@ -2666,42 +2610,24 @@ proc submitSyncCommitteeSelections*( of 200: let res = decodeBytes(SubmitSyncCommitteeSelectionsResponse, response.data, response.contentType) - if res.isOk(): - return res.get() - debug ResponseDecodeError, response_code = response.status, - endpoint = node, reason = res.error - node.updateStatus(RestBeaconNodeStatus.Unexpected) - failures.add(ApiNodeFailure.init(node, ApiFailure.Invalid)) + if res.isOk(): return res.get() + handleUnexpectedData() false of 400: - debug ResponseInvalidError, response_code = response.status, - endpoint = node, reason = response.getErrorMessage() - node.updateStatus(RestBeaconNodeStatus.Incompatible) - failures.add(ApiNodeFailure.init(node, ApiFailure.Invalid)) + handle400() false of 500: - debug ResponseInternalError, response_code = response.status, - endpoint = node, reason = response.getErrorMessage() - node.updateStatus(RestBeaconNodeStatus.InternalError) - failures.add(ApiNodeFailure.init(node, ApiFailure.Internal)) + handle500() false of 501: - warn ResponseNotImplementedError, response_code = response.status, - endpoint = node, reason = response.getErrorMessage() - node.updateStatus(RestBeaconNodeStatus.Incompatible) - failures.add(ApiNodeFailure.init(node, ApiFailure.Invalid)) + handle501() false of 503: - debug ResponseNoSyncError, response_code = response.status, - endpoint = node, reason = response.getErrorMessage() - node.updateStatus(RestBeaconNodeStatus.NotSynced) - failures.add(ApiNodeFailure.init(node, ApiFailure.NotSynced)) + handle503() false else: - debug ResponseUnexpectedError, response_code = response.status, - endpoint = node, reason = response.getErrorMessage() - node.updateStatus(RestBeaconNodeStatus.Unexpected) - failures.add(ApiNodeFailure.init(node, ApiFailure.Unexpected)) + handleUnexpectedCode() false - raise (ref ValidatorApiError)(msg: ErrorMessage, data: failures) + raise (ref ValidatorApiError)( + msg: "Failed to submit sync committee selections", data: failures) diff --git a/beacon_chain/validator_client/common.nim b/beacon_chain/validator_client/common.nim index f5dace362..7cceddcd3 100644 --- a/beacon_chain/validator_client/common.nim +++ b/beacon_chain/validator_client/common.nim @@ -199,7 +199,7 @@ type ApiFailure* {.pure.} = enum Communication, Invalid, NotFound, OptSynced, NotSynced, Internal, - UnexpectedCode, UnexpectedResponse, NoError + NotImplemented, UnexpectedCode, UnexpectedResponse, NoError ApiNodeFailure* = object node*: BeaconNodeServerRef @@ -281,6 +281,7 @@ proc `$`*(failure: ApiFailure): string = of ApiFailure.NotSynced: "not-synced" of ApiFailure.OptSynced: "opt-synced" of ApiFailure.Internal: "internal-issue" + of ApiFailure.NotImplemented: "not-implemented" of ApiFailure.UnexpectedCode: "unexpected-code" of ApiFailure.UnexpectedResponse: "unexpected-data" of ApiFailure.NoError: "status-update"