mirror of
https://github.com/logos-messaging/logos-messaging-nim.git
synced 2026-02-08 16:03:11 +00:00
replace compile reputation flag with config flag
This commit is contained in:
parent
b50fd481bf
commit
d8ba7b7eef
@ -25,10 +25,13 @@ proc newTestWakuLightpushNode*(
|
|||||||
|
|
||||||
return proto
|
return proto
|
||||||
|
|
||||||
proc newTestWakuLightpushClient*(switch: Switch): WakuLightPushClient =
|
proc newTestWakuLightpushClient*(
|
||||||
|
switch: Switch,
|
||||||
|
reputationEnabled: bool = false
|
||||||
|
): WakuLightPushClient =
|
||||||
let peerManager = PeerManager.new(switch)
|
let peerManager = PeerManager.new(switch)
|
||||||
let reputationManager =
|
let reputationManager =
|
||||||
if defined(reputation):
|
if reputationEnabled:
|
||||||
some(ReputationManager.new())
|
some(ReputationManager.new())
|
||||||
else:
|
else:
|
||||||
none(ReputationManager)
|
none(ReputationManager)
|
||||||
|
|||||||
@ -75,7 +75,8 @@ suite "Waku Lightpush Client":
|
|||||||
server = await newTestWakuLightpushNode(serverSwitch, handler)
|
server = await newTestWakuLightpushNode(serverSwitch, handler)
|
||||||
serverFailsLightpush =
|
serverFailsLightpush =
|
||||||
await newTestWakuLightpushNode(serverSwitchFailsLightpush, handlerFailsLightpush)
|
await newTestWakuLightpushNode(serverSwitchFailsLightpush, handlerFailsLightpush)
|
||||||
client = newTestWakuLightpushClient(clientSwitch)
|
## FIXME: how to pass reputationEnabled from config to here? should we?
|
||||||
|
client = newTestWakuLightpushClient(clientSwitch, reputationEnabled = true)
|
||||||
|
|
||||||
await allFutures(
|
await allFutures(
|
||||||
serverSwitch.start(), serverSwitchFailsLightpush.start(), clientSwitch.start()
|
serverSwitch.start(), serverSwitchFailsLightpush.start(), clientSwitch.start()
|
||||||
@ -330,8 +331,8 @@ suite "Waku Lightpush Client":
|
|||||||
# Then the response is positive
|
# Then the response is positive
|
||||||
assertResultOk publishResponse
|
assertResultOk publishResponse
|
||||||
|
|
||||||
when defined(reputation):
|
if client.reputationManager.isSome:
|
||||||
check client.reputationManager.getReputation(serverRemotePeerInfo.peerId) ==
|
check client.reputationManager.get().getReputation(serverRemotePeerInfo.peerId) ==
|
||||||
some(true)
|
some(true)
|
||||||
|
|
||||||
# TODO: Improve: Add more negative responses variations
|
# TODO: Improve: Add more negative responses variations
|
||||||
@ -351,8 +352,8 @@ suite "Waku Lightpush Client":
|
|||||||
# Then the response is negative
|
# Then the response is negative
|
||||||
check not publishResponse.isOk()
|
check not publishResponse.isOk()
|
||||||
|
|
||||||
when defined(reputation):
|
if client.reputationManager.isSome:
|
||||||
check client.reputationManager.getReputation(
|
check client.reputationManager.get().getReputation(
|
||||||
serverRemotePeerInfoFailsLightpush.peerId
|
serverRemotePeerInfoFailsLightpush.peerId
|
||||||
) == some(false)
|
) == some(false)
|
||||||
|
|
||||||
@ -386,6 +387,9 @@ suite "Waku Lightpush Client":
|
|||||||
|
|
||||||
check not publishResponse1.isOk()
|
check not publishResponse1.isOk()
|
||||||
|
|
||||||
|
if client.reputationManager.isSome:
|
||||||
|
client.reputationManager.get().setReputation(serverRemotePeerInfoFailsLightpush.peerId, some(false))
|
||||||
|
|
||||||
# add a peer that supports the Lightpush protocol to the client's PeerManager
|
# add a peer that supports the Lightpush protocol to the client's PeerManager
|
||||||
client.peerManager.addPeer(serverRemotePeerInfo) # supports Lightpush
|
client.peerManager.addPeer(serverRemotePeerInfo) # supports Lightpush
|
||||||
|
|
||||||
@ -394,12 +398,15 @@ suite "Waku Lightpush Client":
|
|||||||
|
|
||||||
check publishResponse2.isOk()
|
check publishResponse2.isOk()
|
||||||
|
|
||||||
when defined(reputation):
|
if client.reputationManager.isSome:
|
||||||
|
client.reputationManager.get().setReputation(serverRemotePeerInfo.peerId, some(true))
|
||||||
|
|
||||||
|
if client.reputationManager.isSome:
|
||||||
# the reputation of a failed peer is negative
|
# the reputation of a failed peer is negative
|
||||||
check client.reputationManager.getReputation(
|
check client.reputationManager.get().getReputation(
|
||||||
serverRemotePeerInfoFailsLightpush.peerId
|
serverRemotePeerInfoFailsLightpush.peerId
|
||||||
) == some(false)
|
) == some(false)
|
||||||
|
|
||||||
# the reputation of a successful peer is positive
|
# the reputation of a successful peer is positive
|
||||||
check client.reputationManager.getReputation(serverRemotePeerInfo.peerId) ==
|
check client.reputationManager.get().getReputation(serverRemotePeerInfo.peerId) ==
|
||||||
some(true)
|
some(true)
|
||||||
|
|||||||
@ -496,6 +496,14 @@ hence would have reachability issues.""",
|
|||||||
name: "lightpushnode"
|
name: "lightpushnode"
|
||||||
.}: string
|
.}: string
|
||||||
|
|
||||||
|
## Reputation config
|
||||||
|
## FIXME: should be set to false by default
|
||||||
|
reputationEnabled* {.
|
||||||
|
desc: "Enable client-side reputation for light protocols: true|false",
|
||||||
|
defaultValue: true,
|
||||||
|
name: "reputation"
|
||||||
|
.}: bool
|
||||||
|
|
||||||
## Reliability config
|
## Reliability config
|
||||||
reliabilityEnabled* {.
|
reliabilityEnabled* {.
|
||||||
desc:
|
desc:
|
||||||
|
|||||||
@ -370,6 +370,8 @@ proc setupProtocols(
|
|||||||
else:
|
else:
|
||||||
return err("failed to set node waku lightpush peer: " & lightPushNode.error)
|
return err("failed to set node waku lightpush peer: " & lightPushNode.error)
|
||||||
|
|
||||||
|
## TODO: initialize reputation manager here??
|
||||||
|
|
||||||
# Filter setup. NOTE Must be mounted after relay
|
# Filter setup. NOTE Must be mounted after relay
|
||||||
if conf.filter:
|
if conf.filter:
|
||||||
try:
|
try:
|
||||||
|
|||||||
@ -1009,10 +1009,10 @@ proc mountLightPush*(
|
|||||||
|
|
||||||
node.switch.mount(node.wakuLightPush, protocolMatcher(WakuLightPushCodec))
|
node.switch.mount(node.wakuLightPush, protocolMatcher(WakuLightPushCodec))
|
||||||
|
|
||||||
proc mountLightPushClient*(node: WakuNode) =
|
proc mountLightPushClient*(node: WakuNode, reputationEnabled: bool = false) =
|
||||||
info "mounting light push client"
|
info "mounting light push client"
|
||||||
|
|
||||||
node.wakuLightpushClient = WakuLightPushClient.new(node.peerManager, node.rng)
|
node.wakuLightpushClient = WakuLightPushClient.new(node.peerManager, node.rng, reputationEnabled)
|
||||||
|
|
||||||
proc lightpushPublish*(
|
proc lightpushPublish*(
|
||||||
node: WakuNode,
|
node: WakuNode,
|
||||||
|
|||||||
@ -20,21 +20,18 @@ logScope:
|
|||||||
type WakuLightPushClient* = ref object
|
type WakuLightPushClient* = ref object
|
||||||
peerManager*: PeerManager
|
peerManager*: PeerManager
|
||||||
rng*: ref rand.HmacDrbgContext
|
rng*: ref rand.HmacDrbgContext
|
||||||
reputationManager*: ReputationManager
|
reputationManager*: Option[ReputationManager]
|
||||||
publishObservers: seq[PublishObserver]
|
publishObservers: seq[PublishObserver]
|
||||||
|
|
||||||
proc new*(
|
proc new*(
|
||||||
T: type WakuLightPushClient,
|
T: type WakuLightPushClient,
|
||||||
peerManager: PeerManager,
|
peerManager: PeerManager,
|
||||||
rng: ref rand.HmacDrbgContext,
|
rng: ref rand.HmacDrbgContext,
|
||||||
reputationManager: Option[ReputationManager] = none(ReputationManager),
|
reputationManager: Option[ReputationManager],
|
||||||
): T =
|
): T =
|
||||||
if reputationManager.isSome:
|
WakuLightPushClient(
|
||||||
WakuLightPushClient(
|
peerManager: peerManager, rng: rng, reputationManager: reputationManager
|
||||||
peerManager: peerManager, rng: rng, reputationManager: reputationManager.get()
|
)
|
||||||
)
|
|
||||||
else:
|
|
||||||
WakuLightPushClient(peerManager: peerManager, rng: rng)
|
|
||||||
|
|
||||||
proc addPublishObserver*(wl: WakuLightPushClient, obs: PublishObserver) =
|
proc addPublishObserver*(wl: WakuLightPushClient, obs: PublishObserver) =
|
||||||
wl.publishObservers.add(obs)
|
wl.publishObservers.add(obs)
|
||||||
@ -75,8 +72,8 @@ proc sendPushRequest(
|
|||||||
else:
|
else:
|
||||||
return err("unknown failure")
|
return err("unknown failure")
|
||||||
|
|
||||||
when defined(reputation):
|
if wl.reputationManager.isSome:
|
||||||
wl.reputationManager.updateReputationFromResponse(peer.peerId, response)
|
wl.reputationManager.get().updateReputationFromResponse(peer.peerId, response)
|
||||||
|
|
||||||
return ok()
|
return ok()
|
||||||
|
|
||||||
@ -92,8 +89,8 @@ proc publish*(
|
|||||||
let pushRequest = PushRequest(pubSubTopic: pubSubTopic, message: message)
|
let pushRequest = PushRequest(pubSubTopic: pubSubTopic, message: message)
|
||||||
let pushResult = await wl.sendPushRequest(pushRequest, peer)
|
let pushResult = await wl.sendPushRequest(pushRequest, peer)
|
||||||
if pushResult.isErr:
|
if pushResult.isErr:
|
||||||
when defined(reputation):
|
if wl.reputationManager.isSome:
|
||||||
wl.reputationManager.setReputation(peer.peerId, some(false))
|
wl.reputationManager.get().setReputation(peer.peerId, some(false))
|
||||||
return err(pushResult.error)
|
return err(pushResult.error)
|
||||||
|
|
||||||
for obs in wl.publishObservers:
|
for obs in wl.publishObservers:
|
||||||
@ -110,29 +107,27 @@ proc publish*(
|
|||||||
proc selectPeerForLightPush*(
|
proc selectPeerForLightPush*(
|
||||||
wl: WakuLightPushClient
|
wl: WakuLightPushClient
|
||||||
): Future[Result[RemotePeerInfo, string]] {.async, gcsafe.} =
|
): Future[Result[RemotePeerInfo, string]] {.async, gcsafe.} =
|
||||||
## If reputation flag is defined, try to ensure the selected peer is not bad-rep.
|
|
||||||
## Repeat peer selection until either maxAttempts is exceeded,
|
|
||||||
## or a good-rep or neutral-rep peer is found.
|
|
||||||
## Note: this procedure CAN return a bad-rep peer if maxAttempts is exceeded.
|
|
||||||
let maxAttempts = if defined(reputation): 10 else: 1
|
let maxAttempts = if defined(reputation): 10 else: 1
|
||||||
var attempts = 0
|
var attempts = 0
|
||||||
var peerResult: Result[RemotePeerInfo, string]
|
var peerResult: Result[RemotePeerInfo, string]
|
||||||
while attempts < maxAttempts:
|
while attempts < maxAttempts:
|
||||||
let candidate = wl.peerManager.selectPeer(WakuLightPushCodec, none(PubsubTopic)).valueOr:
|
let candidate = wl.peerManager.selectPeer(WakuLightPushCodec, none(PubsubTopic)).valueOr:
|
||||||
return err("could not retrieve a peer supporting WakuLightPushCodec")
|
return err("could not retrieve a peer supporting WakuLightPushCodec")
|
||||||
if not (wl.reputationManager.getReputation(candidate.peerId) == some(false)):
|
if wl.reputationManager.isSome():
|
||||||
return ok(candidate)
|
let reputation = wl.reputationManager.get().getReputation(candidate.peerId)
|
||||||
attempts += 1
|
info "Peer selected", peerId = candidate.peerId, reputation = $reputation, attempts = $attempts
|
||||||
|
if (reputation == some(false)):
|
||||||
|
attempts += 1
|
||||||
|
continue
|
||||||
|
return ok(candidate)
|
||||||
warn "Maximum reputation-based retries exceeded; continuing with a bad-reputation peer."
|
warn "Maximum reputation-based retries exceeded; continuing with a bad-reputation peer."
|
||||||
# Return last candidate even if it has bad reputation
|
|
||||||
return peerResult
|
return peerResult
|
||||||
|
|
||||||
proc publishToAny*(
|
proc publishToAny*(
|
||||||
wl: WakuLightPushClient, pubSubTopic: PubsubTopic, message: WakuMessage
|
wl: WakuLightPushClient, pubSubTopic: PubsubTopic, message: WakuMessage
|
||||||
): Future[WakuLightPushResult[string]] {.async, gcsafe.} =
|
): Future[WakuLightPushResult[string]] {.async, gcsafe.} =
|
||||||
## Publish a message via a peer that we get from the peer manager
|
|
||||||
|
|
||||||
info "publishToAny", msg_hash = computeMessageHash(pubSubTopic, message).to0xHex
|
info "publishToAny", msg_hash = computeMessageHash(pubSubTopic, message).to0xHex
|
||||||
|
|
||||||
let peer = ?await wl.selectPeerForLightPush()
|
let peer = ?await wl.selectPeerForLightPush()
|
||||||
return await wl.publish(pubSubTopic, message, peer)
|
let publishResult = await wl.publish(pubSubTopic, message, peer)
|
||||||
|
info "Publish result", result = $publishResult
|
||||||
|
return publishResult
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user