avoid memory safety errors with nim 1.4.x
This commit is contained in:
parent
a6eea0c275
commit
ce42674d80
|
@ -47,7 +47,8 @@ proc init*(_: type[TopicParams]): TopicParams =
|
||||||
)
|
)
|
||||||
|
|
||||||
proc withPeerStats*(
|
proc withPeerStats*(
|
||||||
g: GossipSub, peerId: PeerId,
|
g: GossipSub,
|
||||||
|
peerId: PeerId,
|
||||||
action: proc (stats: var PeerStats) {.gcsafe, raises: [Defect].}) =
|
action: proc (stats: var PeerStats) {.gcsafe, raises: [Defect].}) =
|
||||||
## Add or update peer statistics for a particular peer id - the statistics
|
## Add or update peer statistics for a particular peer id - the statistics
|
||||||
## are retained across multiple connections until they expire
|
## are retained across multiple connections until they expire
|
||||||
|
@ -293,9 +294,10 @@ proc punishInvalidMessage*(g: GossipSub, peer: PubSubPeer, topics: seq[string])
|
||||||
if t notin g.topics:
|
if t notin g.topics:
|
||||||
continue
|
continue
|
||||||
|
|
||||||
|
let tt = t
|
||||||
# update stats
|
# update stats
|
||||||
g.withPeerStats(peer.peerId) do (stats: var PeerStats):
|
g.withPeerStats(peer.peerId) do (stats: var PeerStats):
|
||||||
stats.topicInfos.mgetOrPut(t, TopicInfo()).invalidMessageDeliveries += 1
|
stats.topicInfos.mgetOrPut(tt, TopicInfo()).invalidMessageDeliveries += 1
|
||||||
|
|
||||||
proc addCapped*[T](stat: var T, diff, cap: T) =
|
proc addCapped*[T](stat: var T, diff, cap: T) =
|
||||||
stat += min(diff, cap - stat)
|
stat += min(diff, cap - stat)
|
||||||
|
@ -306,11 +308,13 @@ proc rewardDelivered*(
|
||||||
let t = tt
|
let t = tt
|
||||||
if t notin g.topics:
|
if t notin g.topics:
|
||||||
continue
|
continue
|
||||||
|
|
||||||
|
let tt = t
|
||||||
let topicParams = g.topicParams.mgetOrPut(t, TopicParams.init())
|
let topicParams = g.topicParams.mgetOrPut(t, TopicParams.init())
|
||||||
# if in mesh add more delivery score
|
# if in mesh add more delivery score
|
||||||
|
|
||||||
g.withPeerStats(peer.peerId) do (stats: var PeerStats):
|
g.withPeerStats(peer.peerId) do (stats: var PeerStats):
|
||||||
stats.topicInfos.withValue(t, tstats):
|
stats.topicInfos.withValue(tt, tstats):
|
||||||
if tstats[].inMesh:
|
if tstats[].inMesh:
|
||||||
if first:
|
if first:
|
||||||
tstats[].firstMessageDeliveries.addCapped(
|
tstats[].firstMessageDeliveries.addCapped(
|
||||||
|
@ -319,4 +323,4 @@ proc rewardDelivered*(
|
||||||
tstats[].meshMessageDeliveries.addCapped(
|
tstats[].meshMessageDeliveries.addCapped(
|
||||||
1, topicParams.meshMessageDeliveriesCap)
|
1, topicParams.meshMessageDeliveriesCap)
|
||||||
do: # make sure we don't loose this information
|
do: # make sure we don't loose this information
|
||||||
stats.topicInfos[t] = TopicInfo(meshMessageDeliveries: 1)
|
stats.topicInfos[tt] = TopicInfo(meshMessageDeliveries: 1)
|
||||||
|
|
Loading…
Reference in New Issue