2022-10-08 17:20:50 +00:00
|
|
|
# Nimbus
|
|
|
|
# Copyright (c) 2021 Status Research & Development GmbH
|
|
|
|
# Licensed under either of
|
|
|
|
# * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE) or
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0)
|
|
|
|
# * MIT license ([LICENSE-MIT](LICENSE-MIT) or
|
|
|
|
# http://opensource.org/licenses/MIT)
|
|
|
|
# at your option. This file may not be copied, modified, or distributed
|
|
|
|
# except according to those terms.
|
|
|
|
|
|
|
|
import
|
|
|
|
chronos,
|
2022-11-01 15:07:44 +00:00
|
|
|
../../../sync_desc,
|
|
|
|
../../constants
|
2022-10-08 17:20:50 +00:00
|
|
|
|
|
|
|
type
|
|
|
|
ComErrorStatsRef* = ref object
|
|
|
|
## particular error counters so connections will not be cut immediately
|
|
|
|
## after a particular error.
|
|
|
|
nTimeouts*: uint
|
2022-11-01 15:07:44 +00:00
|
|
|
nNoData*: uint
|
2022-10-08 17:20:50 +00:00
|
|
|
nNetwork*: uint
|
|
|
|
|
|
|
|
ComError* = enum
|
|
|
|
ComNothingSerious
|
|
|
|
ComAccountsMaxTooLarge
|
|
|
|
ComAccountsMinTooSmall
|
|
|
|
ComEmptyAccountsArguments
|
2022-10-14 16:40:32 +00:00
|
|
|
ComEmptyPartialRange
|
2022-10-08 17:20:50 +00:00
|
|
|
ComEmptyRequestArguments
|
|
|
|
ComNetworkProblem
|
|
|
|
ComNoAccountsForStateRoot
|
|
|
|
ComNoByteCodesAvailable
|
2022-11-01 15:07:44 +00:00
|
|
|
#ComNoHeaderAvailable -- unused, see get_block_header.nim
|
2022-10-08 17:20:50 +00:00
|
|
|
ComNoStorageForAccounts
|
|
|
|
ComNoTrieNodesAvailable
|
|
|
|
ComResponseTimeout
|
|
|
|
ComTooManyByteCodes
|
|
|
|
ComTooManyHeaders
|
|
|
|
ComTooManyStorageSlots
|
|
|
|
ComTooManyTrieNodes
|
|
|
|
|
|
|
|
|
2022-11-01 15:07:44 +00:00
|
|
|
proc resetComError*(stats: ComErrorStatsRef) =
|
|
|
|
## Reset error counts after successful network operation
|
|
|
|
stats[].reset
|
2022-10-08 17:20:50 +00:00
|
|
|
|
|
|
|
proc stopAfterSeriousComError*(
|
|
|
|
ctrl: BuddyCtrlRef;
|
|
|
|
error: ComError;
|
|
|
|
stats: ComErrorStatsRef;
|
|
|
|
): Future[bool]
|
|
|
|
{.async.} =
|
2022-11-01 15:07:44 +00:00
|
|
|
## Error handling after data protocol failed. Returns `true` if the current
|
|
|
|
## worker should be terminated as *zombie*.
|
2022-10-08 17:20:50 +00:00
|
|
|
case error:
|
|
|
|
of ComResponseTimeout:
|
|
|
|
stats.nTimeouts.inc
|
|
|
|
if comErrorsTimeoutMax < stats.nTimeouts:
|
|
|
|
# Mark this peer dead, i.e. avoid fetching from this peer for a while
|
|
|
|
ctrl.zombie = true
|
2022-11-01 15:07:44 +00:00
|
|
|
return true
|
2022-10-08 17:20:50 +00:00
|
|
|
|
2022-11-01 15:07:44 +00:00
|
|
|
when 0 < comErrorsTimeoutSleepMSecs:
|
|
|
|
# Otherwise try again some time later.
|
|
|
|
await sleepAsync(comErrorsTimeoutSleepMSecs.milliseconds)
|
|
|
|
|
|
|
|
of ComNetworkProblem:
|
2022-10-08 17:20:50 +00:00
|
|
|
stats.nNetwork.inc
|
2022-11-01 15:07:44 +00:00
|
|
|
if comErrorsNetworkMax < stats.nNetwork:
|
|
|
|
ctrl.zombie = true
|
|
|
|
return true
|
2022-10-08 17:20:50 +00:00
|
|
|
|
2022-11-01 15:07:44 +00:00
|
|
|
when 0 < comErrorsNetworkSleepMSecs:
|
|
|
|
# Otherwise try again some time later.
|
|
|
|
await sleepAsync(comErrorsNetworkSleepMSecs.milliseconds)
|
2022-10-08 17:20:50 +00:00
|
|
|
|
|
|
|
of ComNoAccountsForStateRoot,
|
|
|
|
ComNoByteCodesAvailable,
|
2022-11-01 15:07:44 +00:00
|
|
|
ComNoStorageForAccounts,
|
|
|
|
#ComNoHeaderAvailable,
|
|
|
|
ComNoTrieNodesAvailable:
|
|
|
|
stats.nNoData.inc
|
|
|
|
if comErrorsNoDataMax < stats.nNoData:
|
|
|
|
ctrl.zombie = true
|
|
|
|
return true
|
|
|
|
|
|
|
|
when 0 < comErrorsNoDataSleepMSecs:
|
|
|
|
# Otherwise try again some time later.
|
|
|
|
await sleepAsync(comErrorsNoDataSleepMSecs.milliseconds)
|
|
|
|
|
2022-11-08 18:56:04 +00:00
|
|
|
of ComAccountsMinTooSmall,
|
2022-11-01 15:07:44 +00:00
|
|
|
ComAccountsMaxTooLarge,
|
2022-10-08 17:20:50 +00:00
|
|
|
ComTooManyByteCodes,
|
|
|
|
ComTooManyHeaders,
|
|
|
|
ComTooManyStorageSlots,
|
|
|
|
ComTooManyTrieNodes:
|
|
|
|
# Mark this peer dead, i.e. avoid fetching from this peer for a while
|
|
|
|
ctrl.zombie = true
|
|
|
|
return true
|
|
|
|
|
2022-11-01 15:07:44 +00:00
|
|
|
of ComEmptyAccountsArguments,
|
|
|
|
ComEmptyRequestArguments,
|
|
|
|
ComEmptyPartialRange,
|
|
|
|
ComNothingSerious:
|
|
|
|
discard
|
|
|
|
|
2022-10-08 17:20:50 +00:00
|
|
|
# End
|