-
Notifications
You must be signed in to change notification settings - Fork 47
/
cache.py
60 lines (47 loc) · 1.79 KB
/
cache.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
from __future__ import absolute_import
from twisted.internet.defer import Deferred
from ...requestcache import NumberCache
class ProposalCache(NumberCache):
"""
Cache for keeping track of a made proposal.
This cache concludes upon proposal (1) accept, (2) reject or (3) timeout.
"""
def __init__(self, overlay, peer, nonce):
super(ProposalCache, self).__init__(overlay.request_cache, u"proposal-cache",
self.number_from_pk_nonce(peer.mid, nonce))
self.overlay = overlay
self.peer = peer
@classmethod
def number_from_pk_nonce(cls, public_key, nonce):
"""
Create an identifier from a public key and a nonce.
:param public_key: the counterparty public key
:type public_key: str or bytes
:param nonce: the nonce for this proposal
:type nonce: str or bytes
:return: the identifier for the given parameters
:rtype: int
"""
number = nonce
for c in public_key:
number <<= 8
number += c if isinstance(c, int) else ord(c)
return number
def on_timeout(self):
"""
When timing out, we remove this proposal from the open proposals.
:returns: None
"""
try:
self.overlay.open_proposals.remove(self.peer)
except KeyError:
self.overlay.logger.debug("Proposal timed out, but peer already removed.")
class StatsRequestCache(NumberCache):
"""
Cache for waiting for a stats response.
"""
def __init__(self, overlay):
super(StatsRequestCache, self).__init__(overlay.request_cache, u"stats-request", overlay.claim_global_time())
self.deferred = Deferred()
def on_timeout(self):
self.deferred.errback(None)