From 841904bd538e15023546b1f1759857bfd68168e9 Mon Sep 17 00:00:00 2001 From: Eugene Kabanov Date: Tue, 24 Sep 2024 07:23:38 +0300 Subject: [PATCH] Do not cache resolved hostnames in REST client. (#6570) --- beacon_chain/beacon_node.nim | 13 ++++++++----- beacon_chain/deposits.nim | 12 ++++++++++-- beacon_chain/trusted_node_sync.nim | 15 +++++++++++---- beacon_chain/validators/beacon_validators.nim | 15 ++++++++++----- beacon_chain/validators/validator_pool.nim | 3 ++- 5 files changed, 41 insertions(+), 17 deletions(-) diff --git a/beacon_chain/beacon_node.nim b/beacon_chain/beacon_node.nim index d85e7113b..2ccca461a 100644 --- a/beacon_chain/beacon_node.nim +++ b/beacon_chain/beacon_node.nim @@ -151,8 +151,11 @@ proc getPayloadBuilderClient*( if payloadBuilderAddress.isNone: return err "Payload builder disabled" - let res = RestClientRef.new(payloadBuilderAddress.get) - if res.isOk and res.get.isNil: - err "Got nil payload builder REST client reference" - else: - res + + let + flags = {RestClientFlag.CommaSeparatedArray, + RestClientFlag.ResolveAlways} + socketFlags = {SocketFlags.TcpNoDelay} + + RestClientRef.new(payloadBuilderAddress.get, flags = flags, + socketFlags = socketFlags) diff --git a/beacon_chain/deposits.nim b/beacon_chain/deposits.nim index ed365bd1d..b9fd7b42e 100644 --- a/beacon_chain/deposits.nim +++ b/beacon_chain/deposits.nim @@ -149,8 +149,16 @@ func getIdent*(storage: ValidatorStorage): ValidatorIdent = proc restValidatorExit(config: BeaconNodeConf) {.async.} = let - client = RestClientRef.new(config.restUrlForExit).valueOr: - raise (ref RestError)(msg: $error) + client = + block: + let + flags = {RestClientFlag.CommaSeparatedArray, + RestClientFlag.ResolveAlways} + socketFlags = {SocketFlags.TcpNoDelay} + + RestClientRef.new(config.restUrlForExit, flags = flags, + socketFlags = socketFlags).valueOr: + raise (ref RestError)(msg: $error) stateIdHead = StateIdent(kind: StateQueryKind.Named, value: StateIdentType.Head) diff --git a/beacon_chain/trusted_node_sync.nim b/beacon_chain/trusted_node_sync.nim index f0a739e4f..bca31f3be 100644 --- a/beacon_chain/trusted_node_sync.nim +++ b/beacon_chain/trusted_node_sync.nim @@ -61,6 +61,13 @@ func shortLog*(v: TrustedNodeSyncTarget): auto = chronicles.formatIt(TrustedNodeSyncTarget): shortLog(it) +proc createNewRestClient(url: string): Result[RestClientRef, cstring] = + let + flags = {RestClientFlag.CommaSeparatedArray, + RestClientFlag.ResolveAlways} + socketFlags = {SocketFlags.TcpNoDelay} + RestClientRef.new(url, flags = flags, socketFlags = socketFlags) + proc doTrustedNodeSync*( db: BeaconChainDB, cfg: RuntimeConfig, @@ -80,8 +87,8 @@ proc doTrustedNodeSync*( databaseDir, backfill, reindex var - client = RestClientRef.new(restUrl).valueOr: - error "Cannot connect to server", error = error + client = createNewRestClient(restUrl).valueOr: + error "Cannot connect to server", reason = error quit 1 # If possible, we'll store the genesis state in the database - this is not @@ -440,8 +447,8 @@ proc doTrustedNodeSync*( lastError = exc warn "Retrying download of block", slot, err = exc.msg - client = RestClientRef.new(restUrl).valueOr: - error "Cannot connect to server", url = restUrl, error = error + client = createNewRestClient(restUrl).valueOr: + error "Cannot connect to server", url = restUrl, reason = error quit 1 raise lastError diff --git a/beacon_chain/validators/beacon_validators.nim b/beacon_chain/validators/beacon_validators.nim index d232a3512..30761f1b2 100644 --- a/beacon_chain/validators/beacon_validators.nim +++ b/beacon_chain/validators/beacon_validators.nim @@ -1718,11 +1718,16 @@ proc registerValidatorsPerBuilder( BUILDER_VALIDATOR_REGISTRATION_DELAY_TOLERANCE = 6.seconds let payloadBuilderClient = - RestClientRef.new(payloadBuilderAddress).valueOr: - debug "Unable to initialize payload builder client while registering validators", - payloadBuilderAddress, epoch, - err = error - return + block: + let + flags = {RestClientFlag.CommaSeparatedArray, + RestClientFlag.ResolveAlways} + socketFlags = {SocketFlags.TcpNoDelay} + RestClientRef.new(payloadBuilderAddress, flags = flags, + socketFlags = socketFlags).valueOr: + debug "Unable to initialize payload builder client while registering validators", + payloadBuilderAddress, epoch, reason = error + return if payloadBuilderClient.isNil: debug "registerValidatorsPerBuilder: got nil payload builder REST client reference", diff --git a/beacon_chain/validators/validator_pool.nim b/beacon_chain/validators/validator_pool.nim index c90238feb..97ce697ba 100644 --- a/beacon_chain/validators/validator_pool.nim +++ b/beacon_chain/validators/validator_pool.nim @@ -183,7 +183,8 @@ proc addRemoteValidator(pool: var ValidatorPool, {HttpClientFlag.NoVerifyHost, HttpClientFlag.NoVerifyServerName} else: {} - prestoFlags = {RestClientFlag.CommaSeparatedArray} + prestoFlags = {RestClientFlag.CommaSeparatedArray, + RestClientFlag.ResolveAlways} socketFlags = {SocketFlags.TcpNoDelay} clients = block: