From 5f91b1b68587054920a7e9566e94e673e3bae75e Mon Sep 17 00:00:00 2001 From: bhartnett <51288821+bhartnett@users.noreply.github.com> Date: Mon, 20 Jan 2025 20:40:24 +0800 Subject: [PATCH] Remove helper templates. --- fluffy/network/state/state_network.nim | 8 ++++++-- fluffy/network/wire/portal_protocol.nim | 16 +++++----------- fluffy/rpc/rpc_portal_state_api.nim | 11 ++++++++--- 3 files changed, 19 insertions(+), 16 deletions(-) diff --git a/fluffy/network/state/state_network.nim b/fluffy/network/state/state_network.nim index 75d0ad8f6..17d899e0f 100644 --- a/fluffy/network/state/state_network.nim +++ b/fluffy/network/state/state_network.nim @@ -109,7 +109,9 @@ proc getContent( continue validateRetrieval(key, contentValue).isOkOr: - n.portalProtocol.contentLookupFailedValidation(lookupRes.receivedFrom.id) + n.portalProtocol.banPeer( + lookupRes.receivedFrom.id, PeerBanDurationContentLookupFailedValidation + ) error "Validation of retrieved state content failed" continue @@ -245,7 +247,9 @@ proc processContentLoop(n: StateNetwork) {.async: (raises: []).} = srcNodeId, contentKeyBytes else: if srcNodeId.isSome(): - n.portalProtocol.offerFailedValidation(srcNodeId.get()) + n.portalProtocol.banPeer( + srcNodeId.get(), PeerBanDurationOfferFailedValidation + ) state_network_offers_failed.inc(labelValues = [$n.portalProtocol.protocolId]) error "Received offered content failed validation", srcNodeId, contentKeyBytes, error = offerRes.error() diff --git a/fluffy/network/wire/portal_protocol.nim b/fluffy/network/wire/portal_protocol.nim index 8982ca887..5fc511986 100644 --- a/fluffy/network/wire/portal_protocol.nim +++ b/fluffy/network/wire/portal_protocol.nim @@ -127,8 +127,8 @@ const initialLookups = 1 ## Amount of lookups done when populating the routing table # Ban durations for the banned peers table - PeerBanDurationContentLookupFailedValidation = 30.minutes - PeerBanDurationOfferFailedValidation = 60.minutes + PeerBanDurationContentLookupFailedValidation* = 60.minutes + PeerBanDurationOfferFailedValidation* = 60.minutes type ToContentIdHandler* = @@ -170,12 +170,12 @@ type of Database: contentKeys: ContentKeysList - BanTimeout = chronos.Moment + PeerBanTimeout = chronos.Moment PortalProtocol* = ref object of TalkProtocol protocolId*: PortalProtocolId routingTable*: RoutingTable - bannedPeers: Table[NodeId, BanTimeout] + bannedPeers: Table[NodeId, PeerBanTimeout] baseProtocol*: protocol.Protocol toContentId*: ToContentIdHandler contentCache: ContentCache @@ -291,7 +291,7 @@ func getProtocolId*( of PortalSubnetwork.transactionGossip: [portalPrefix, 0x4F] -proc banPeer(p: PortalProtocol, nodeId: NodeId, period: chronos.Duration) = +proc banPeer*(p: PortalProtocol, nodeId: NodeId, period: chronos.Duration) = let banTimeout = now(chronos.Moment) + period if p.bannedPeers.contains(nodeId): @@ -315,12 +315,6 @@ proc isBanned(p: PortalProtocol, nodeId: NodeId): bool = p.bannedPeers.del(nodeId) false -template contentLookupFailedValidation*(p: PortalProtocol, nodeId: NodeId) = - p.banPeer(nodeId, PeerBanDurationContentLookupFailedValidation) - -template offerFailedValidation*(p: PortalProtocol, nodeId: NodeId) = - p.banPeer(nodeId, PeerBanDurationOfferFailedValidation) - func `$`(id: PortalProtocolId): string = id.toHex() diff --git a/fluffy/rpc/rpc_portal_state_api.nim b/fluffy/rpc/rpc_portal_state_api.nim index dd0fb8080..564e7e357 100644 --- a/fluffy/rpc/rpc_portal_state_api.nim +++ b/fluffy/rpc/rpc_portal_state_api.nim @@ -46,7 +46,7 @@ proc installPortalStateApiHandlers*(rpcServer: RpcServer, p: PortalProtocol) = of Content: let valueBytes = foundContentResult.content validateRetrieval(key, valueBytes).isOkOr: - p.contentLookupFailedValidation(node.id) + p.banPeer(node.id, PeerBanDurationContentLookupFailedValidation) raise invalidValueErr() let res = ContentInfo( @@ -98,7 +98,10 @@ proc installPortalStateApiHandlers*(rpcServer: RpcServer, p: PortalProtocol) = valueBytes = contentLookupResult.content validateRetrieval(key, valueBytes).isOkOr: - p.contentLookupFailedValidation(contentLookupResult.receivedFrom.id) + p.banPeer( + contentLookupResult.receivedFrom.id, + PeerBanDurationContentLookupFailedValidation, + ) raise invalidValueErr() p.storeContent(keyBytes, contentId, valueBytes, cacheContent = true) @@ -135,7 +138,9 @@ proc installPortalStateApiHandlers*(rpcServer: RpcServer, p: PortalProtocol) = validateRetrieval(key, valueBytes).isOkOr: if res.trace.receivedFrom.isSome(): - p.contentLookupFailedValidation(res.trace.receivedFrom.get()) + p.banPeer( + res.trace.receivedFrom.get(), PeerBanDurationContentLookupFailedValidation + ) raise invalidValueErr() p.storeContent(keyBytes, contentId, valueBytes, cacheContent = true)