|
| 1 | +from abc import ABC, abstractmethod |
| 2 | +from collections import namedtuple |
| 3 | +import datetime |
| 4 | +import functools |
| 5 | +from pathlib import Path |
| 6 | +import sqlite3 |
| 7 | +from typing import Any, Callable, TypeVar, cast, Dict, Type, Optional |
| 8 | + |
| 9 | +from trinity._utils.logging import HasExtendedDebugLogger |
| 10 | + |
| 11 | +from p2p.kademlia import Node |
| 12 | +from p2p.exceptions import ( |
| 13 | + BadDatabaseError, |
| 14 | + BaseP2PError, |
| 15 | + HandshakeFailure, |
| 16 | + TooManyPeersFailure, |
| 17 | + WrongNetworkFailure, |
| 18 | + WrongGenesisFailure, |
| 19 | +) |
| 20 | + |
| 21 | + |
| 22 | +BadNode = namedtuple('BadNode', ['enode', 'until', 'reason', 'error_count']) |
| 23 | + |
| 24 | + |
| 25 | +ONE_DAY = 60 * 60 * 24 |
| 26 | +FAILURE_TIMEOUTS: Dict[Type[Exception], int] = { |
| 27 | + HandshakeFailure: 10, # 10 seconds |
| 28 | + WrongNetworkFailure: ONE_DAY, |
| 29 | + WrongGenesisFailure: ONE_DAY, |
| 30 | + TooManyPeersFailure: 60, # one minute |
| 31 | +} |
| 32 | + |
| 33 | + |
| 34 | +def timeout_for_failure(failure: BaseP2PError) -> int: |
| 35 | + for cls in type(failure).__mro__: |
| 36 | + if cls in FAILURE_TIMEOUTS: |
| 37 | + return FAILURE_TIMEOUTS[cls] |
| 38 | + failure_name = type(failure).__name__ |
| 39 | + raise Exception(f'Unknown failure type: {failure_name}') |
| 40 | + |
| 41 | + |
| 42 | +def time_to_str(time: datetime.datetime) -> str: |
| 43 | + return time.isoformat(timespec='seconds') |
| 44 | + |
| 45 | + |
| 46 | +def str_to_time(as_str: str) -> datetime.datetime: |
| 47 | + # use datetime.datetime.fromisoformat once support for 3.6 is dropped |
| 48 | + return datetime.datetime.strptime(as_str, "%Y-%m-%dT%H:%M:%S") |
| 49 | + |
| 50 | + |
| 51 | +def utc_to_local(utc: datetime.datetime) -> datetime.datetime: |
| 52 | + local_tz = datetime.datetime.now().astimezone() |
| 53 | + return utc + local_tz.utcoffset() |
| 54 | + |
| 55 | + |
| 56 | +class BasePeerInfo(ABC, HasExtendedDebugLogger): |
| 57 | + @abstractmethod |
| 58 | + def record_failure(self, remote: Node, failure: BaseP2PError) -> None: |
| 59 | + pass |
| 60 | + |
| 61 | + @abstractmethod |
| 62 | + def should_connect_to(self, remote: Node) -> bool: |
| 63 | + pass |
| 64 | + |
| 65 | + |
| 66 | +class NoopPeerInfo(BasePeerInfo): |
| 67 | + def record_failure(self, remote: Node, failure: BaseP2PError) -> None: |
| 68 | + pass |
| 69 | + |
| 70 | + def should_connect_to(self, remote: Node) -> bool: |
| 71 | + return True |
| 72 | + |
| 73 | + |
| 74 | +class ClosedException(Exception): |
| 75 | + 'This should never happen, this represents a logic error somewhere in the code' |
| 76 | + pass |
| 77 | + |
| 78 | + |
| 79 | +T = TypeVar('T', bound=Callable[..., Any]) |
| 80 | + |
| 81 | + |
| 82 | +def must_be_open(func: T) -> T: |
| 83 | + @functools.wraps(func) |
| 84 | + def wrapper(self: 'SQLitePeerInfo', *args: Any, **kwargs: Any) -> Any: |
| 85 | + if self.closed: |
| 86 | + msg = "SQLitePeerInfo cannot be used after it's been closed" |
| 87 | + raise ClosedException(msg) |
| 88 | + return func(self, *args, **kwargs) |
| 89 | + return cast(T, wrapper) |
| 90 | + |
| 91 | + |
| 92 | +class SQLitePeerInfo(BasePeerInfo): |
| 93 | + def __init__(self, path: Path) -> None: |
| 94 | + self.path = path |
| 95 | + self.closed = False |
| 96 | + |
| 97 | + # python 3.6 does not support sqlite3.connect(Path) |
| 98 | + self.db = sqlite3.connect(str(self.path)) |
| 99 | + self.db.row_factory = sqlite3.Row |
| 100 | + self.setup_schema() |
| 101 | + |
| 102 | + def __str__(self) -> str: |
| 103 | + return f'<SQLitePeerInfo({self.path})>' |
| 104 | + |
| 105 | + @must_be_open |
| 106 | + def record_failure(self, remote: Node, failure: BaseP2PError) -> None: |
| 107 | + failure_name = type(failure).__name__ |
| 108 | + timeout = timeout_for_failure(failure) |
| 109 | + |
| 110 | + self._record_bad_node( |
| 111 | + remote, |
| 112 | + timeout=timeout, # one minute |
| 113 | + reason=failure_name |
| 114 | + ) |
| 115 | + |
| 116 | + @must_be_open |
| 117 | + def _record_bad_node(self, remote: Node, timeout: int, reason: str) -> None: |
| 118 | + enode = remote.uri() |
| 119 | + bad_node = self._fetch_bad_node(remote) |
| 120 | + now = datetime.datetime.utcnow() |
| 121 | + if bad_node: |
| 122 | + new_error_count = bad_node.error_count + 1 |
| 123 | + usable_time = now + datetime.timedelta(seconds=timeout * new_error_count) |
| 124 | + local_time = utc_to_local(usable_time) |
| 125 | + self.logger.debug( |
| 126 | + '%s will not be retried until %s because %s', remote, local_time, reason |
| 127 | + ) |
| 128 | + self._update_bad_node(enode, usable_time, reason, new_error_count) |
| 129 | + return |
| 130 | + |
| 131 | + usable_time = now + datetime.timedelta(seconds=timeout) |
| 132 | + local_time = utc_to_local(usable_time) |
| 133 | + self.logger.debug( |
| 134 | + '%s will not be retried until %s because %s', remote, local_time, reason |
| 135 | + ) |
| 136 | + self._insert_bad_node(enode, usable_time, reason, error_count=1) |
| 137 | + |
| 138 | + @must_be_open |
| 139 | + def should_connect_to(self, remote: Node) -> bool: |
| 140 | + bad_node = self._fetch_bad_node(remote) |
| 141 | + |
| 142 | + if not bad_node: |
| 143 | + return True |
| 144 | + |
| 145 | + until = str_to_time(bad_node.until) |
| 146 | + if datetime.datetime.utcnow() < until: |
| 147 | + local_time = utc_to_local(until) |
| 148 | + self.logger.debug( |
| 149 | + 'skipping %s, it failed because "%s" and is not usable until %s', |
| 150 | + remote, bad_node.reason, local_time |
| 151 | + ) |
| 152 | + return False |
| 153 | + |
| 154 | + return True |
| 155 | + |
| 156 | + def _fetch_bad_node(self, remote: Node) -> Optional[BadNode]: |
| 157 | + enode = remote.uri() |
| 158 | + cursor = self.db.execute('SELECT * from bad_nodes WHERE enode = ?', (enode,)) |
| 159 | + row = cursor.fetchone() |
| 160 | + if not row: |
| 161 | + return None |
| 162 | + result = BadNode(row['enode'], row['until'], row['reason'], row['error_count']) |
| 163 | + return result |
| 164 | + |
| 165 | + def _insert_bad_node(self, |
| 166 | + enode: str, |
| 167 | + until: datetime.datetime, |
| 168 | + reason: str, |
| 169 | + error_count: int) -> None: |
| 170 | + with self.db: |
| 171 | + self.db.execute( |
| 172 | + ''' |
| 173 | + INSERT INTO bad_nodes (enode, until, reason, error_count) |
| 174 | + VALUES (?, ?, ?, ?) |
| 175 | + ''', |
| 176 | + (enode, time_to_str(until), reason, error_count), |
| 177 | + ) |
| 178 | + |
| 179 | + def _update_bad_node(self, |
| 180 | + enode: str, |
| 181 | + until: datetime.datetime, |
| 182 | + reason: str, |
| 183 | + error_count: int) -> None: |
| 184 | + with self.db: |
| 185 | + self.db.execute( |
| 186 | + ''' |
| 187 | + UPDATE bad_nodes |
| 188 | + SET until = ?, reason = ?, error_count = ? |
| 189 | + WHERE enode = ? |
| 190 | + ''', |
| 191 | + (time_to_str(until), reason, error_count, enode), |
| 192 | + ) |
| 193 | + |
| 194 | + def close(self) -> None: |
| 195 | + self.db.close() |
| 196 | + self.db = None |
| 197 | + self.closed = True |
| 198 | + |
| 199 | + @must_be_open |
| 200 | + def setup_schema(self) -> None: |
| 201 | + try: |
| 202 | + if self._schema_already_created(): |
| 203 | + return |
| 204 | + except Exception: |
| 205 | + self.close() |
| 206 | + raise |
| 207 | + |
| 208 | + with self.db: |
| 209 | + self.db.execute('create table bad_nodes (enode, until, reason, error_count)') |
| 210 | + self.db.execute('create table schema_version (version)') |
| 211 | + self.db.execute('insert into schema_version VALUES (1)') |
| 212 | + |
| 213 | + def _schema_already_created(self) -> bool: |
| 214 | + "Inspects the database to see if the expected tables already exist" |
| 215 | + |
| 216 | + count = self.db.execute(""" |
| 217 | + SELECT count() FROM sqlite_master |
| 218 | + WHERE type='table' AND name='schema_version' |
| 219 | + """).fetchone()['count()'] |
| 220 | + if count == 0: |
| 221 | + return False |
| 222 | + |
| 223 | + # a schema_version table already exists, get the version |
| 224 | + cur = self.db.execute("SELECT version FROM schema_version") |
| 225 | + rows = cur.fetchall() |
| 226 | + if len(rows) != 1: |
| 227 | + self.logger.error( |
| 228 | + "malformed nodedb. try deleting %s. (got rows: %s)", |
| 229 | + self.path, rows, |
| 230 | + ) |
| 231 | + raise BadDatabaseError( |
| 232 | + "malformed nodedb: Expected one row in schema_version and got %s", |
| 233 | + len(rows), |
| 234 | + ) |
| 235 | + version = rows[0]['version'] |
| 236 | + if version != 1: |
| 237 | + # in the future this block might kick off a schema migration |
| 238 | + self.logger.error("malformed. try deleting %s", self.path) |
| 239 | + raise BadDatabaseError( |
| 240 | + "cannot read nodedb: version %s is unsupported", version |
| 241 | + ) |
| 242 | + |
| 243 | + # schema_version exists and is 1, this database has already been initialized! |
| 244 | + return True |
| 245 | + |
| 246 | + |
| 247 | +class MemoryPeerInfo(SQLitePeerInfo): |
| 248 | + def __init__(self) -> None: |
| 249 | + super().__init__(Path(":memory:")) |
| 250 | + |
| 251 | + def __str__(self) -> str: |
| 252 | + return '<MemoryPeerInfo()>' |
0 commit comments