Skip to content

Commit

Permalink
Remove helper templates.
Browse files Browse the repository at this point in the history
  • Loading branch information
bhartnett committed Jan 20, 2025
1 parent 4b8cf1c commit 5f91b1b
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 16 deletions.
8 changes: 6 additions & 2 deletions fluffy/network/state/state_network.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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()
Expand Down
16 changes: 5 additions & 11 deletions fluffy/network/wire/portal_protocol.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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* =
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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):
Expand All @@ -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()

Expand Down
11 changes: 8 additions & 3 deletions fluffy/rpc/rpc_portal_state_api.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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)

Expand Down

0 comments on commit 5f91b1b

Please sign in to comment.