@@ -25,12 +25,18 @@ import ../../logutils
2525
2626export payments, nitro
2727
28+ const
29+ MinRefreshInterval = 5 .seconds
30+ MaxRefreshBackoff = 36 # 3 minutes
31+
2832type BlockExcPeerCtx * = ref object of RootObj
2933 id* : PeerId
3034 blocks* : Table [BlockAddress , Presence ] # remote peer have list including price
3135 wantedBlocks* : HashSet [BlockAddress ] # blocks that the peer wants
3236 exchanged* : int # times peer has exchanged with us
37+ refreshInProgress* : bool # indicates if a refresh is in progress
3338 lastRefresh* : Moment # last time we refreshed our knowledge of the blocks this peer has
39+ refreshBackoff* : int = 1 # backoff factor for refresh requests
3440 account* : ? Account # ethereum account of this peer
3541 paymentChannel* : ? ChannelId # payment channel id
3642 blocksSent* : HashSet [BlockAddress ] # blocks sent to peer
@@ -39,7 +45,7 @@ type BlockExcPeerCtx* = ref object of RootObj
3945 activityTimeout* : Duration
4046
4147proc isKnowledgeStale * (self: BlockExcPeerCtx ): bool =
42- self.lastRefresh + 5 .minutes < Moment .now ()
48+ self.lastRefresh + self.refreshBackoff * MinRefreshInterval < Moment .now ()
4349
4450proc isBlockSent * (self: BlockExcPeerCtx , address: BlockAddress ): bool =
4551 address in self.blocksSent
@@ -50,8 +56,18 @@ proc markBlockAsSent*(self: BlockExcPeerCtx, address: BlockAddress) =
5056proc markBlockAsNotSent * (self: BlockExcPeerCtx , address: BlockAddress ) =
5157 self.blocksSent.excl (address)
5258
53- proc refreshed * (self: BlockExcPeerCtx ) =
59+ proc refreshRequested * (self: BlockExcPeerCtx ) =
60+ trace " Refresh requested for peer" , peer = self.id, backoff = self.refreshBackoff
61+ self.refreshInProgress = true
62+ self.lastRefresh = Moment .now ()
63+
64+ proc refreshReplied * (self: BlockExcPeerCtx ) =
65+ self.refreshInProgress = false
5466 self.lastRefresh = Moment .now ()
67+ self.refreshBackoff = min (self.refreshBackoff * 2 , MaxRefreshBackoff )
68+
69+ proc havesUpdated (self: BlockExcPeerCtx ) =
70+ self.refreshBackoff = 1
5571
5672proc peerHave * (self: BlockExcPeerCtx ): HashSet [BlockAddress ] =
5773 # XXX: this is ugly an inefficient, but since those will typically
@@ -63,6 +79,9 @@ proc contains*(self: BlockExcPeerCtx, address: BlockAddress): bool =
6379 address in self.blocks
6480
6581func setPresence * (self: BlockExcPeerCtx , presence: Presence ) =
82+ if presence.address notin self.blocks:
83+ self.havesUpdated ()
84+
6685 self.blocks[presence.address] = presence
6786
6887func cleanPresence * (self: BlockExcPeerCtx , addresses: seq [BlockAddress ]) =
0 commit comments