-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathrelaycache.py
58 lines (49 loc) · 1.77 KB
/
relaycache.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
"""
Local cache to hold objects for responding to `GETDATA` network payloads.
"""
from __future__ import annotations
from typing import Optional
from neo3.network.payloads import inventory, block
from neo3.core import types, msgrouter
from neo3 import network_logger as logger, singleton
from contextlib import suppress
class RelayCache(singleton._Singleton):
"""
A cache holding transactions broadcast to the network to be included in a block.
Will be accessed in response to a GETDATA network payload.
"""
def init(self):
self.cache: dict[types.UInt256, inventory.IInventory] = dict()
msgrouter.on_block_persisted += self.update_cache_for_block_persist
def add(self, inventory: inventory.IInventory) -> None:
"""
Add an inventory to the cache.
"""
self.cache.update({inventory.hash(): inventory})
def get_and_remove(
self, inventory_hash: types.UInt256
) -> Optional[inventory.IInventory]:
"""
Pop an inventory from the cache if found.
"""
try:
return self.cache.pop(inventory_hash)
except KeyError:
return None
def try_get(self, inventory_hash: types.UInt256) -> Optional[inventory.IInventory]:
"""
Get an inventory from the cache.
"""
return self.cache.get(inventory_hash, None)
def update_cache_for_block_persist(self, block: block.Block) -> None:
for tx in block.transactions:
with suppress(KeyError):
self.cache.pop(tx.hash())
logger.debug(
f"Found {tx.hash()} in last persisted block. Removing from relay cache"
)
def reset(self) -> None:
"""
Empty the cache.
"""
self.cache = dict()