-
Notifications
You must be signed in to change notification settings - Fork 645
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add Database for tracking key accesses #1918
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
96527de
Speed up make validate-docs: pip install is slow
carver 79d0cd2
Include block hash prefix in str(block)
carver 852b330
Add a database that keeps track of key reads
carver f4cdc79
Remove unnecessary type: ignore
carver 74dbaa8
Add release notes #1918
carver File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
from contextlib import contextmanager | ||
import logging | ||
from typing import ( | ||
Iterator, | ||
FrozenSet, | ||
Set, | ||
) | ||
|
||
from eth.abc import ( | ||
AtomicWriteBatchAPI, | ||
AtomicDatabaseAPI, | ||
DatabaseAPI, | ||
) | ||
from eth.db.backends.base import ( | ||
BaseDB, | ||
) | ||
from eth.db.atomic import ( | ||
BaseAtomicDB, | ||
) | ||
|
||
|
||
class KeyAccessLoggerDB(BaseDB): | ||
""" | ||
Wraps around a database, and tracks all the keys that were read since initialization. | ||
""" | ||
|
||
logger = logging.getLogger("eth.db.KeyAccessLoggerDB") | ||
|
||
def __init__(self, wrapped_db: DatabaseAPI, log_missing_keys: bool=True) -> None: | ||
""" | ||
:param log_missing_keys: True if a key is added to :attr:`keys_read` even if the | ||
key/value does not exist in the database. | ||
""" | ||
self.wrapped_db = wrapped_db | ||
self._keys_read: Set[bytes] = set() | ||
self._log_missing_keys = log_missing_keys | ||
|
||
@property | ||
def keys_read(self) -> FrozenSet[bytes]: | ||
# Make a defensive copy so callers can't modify the list externally | ||
return frozenset(self._keys_read) | ||
|
||
def __getitem__(self, key: bytes) -> bytes: | ||
try: | ||
result = self.wrapped_db.__getitem__(key) | ||
except KeyError: | ||
if self._log_missing_keys: | ||
self._keys_read.add(key) | ||
raise | ||
else: | ||
self._keys_read.add(key) | ||
return result | ||
|
||
def __setitem__(self, key: bytes, value: bytes) -> None: | ||
self.wrapped_db[key] = value | ||
|
||
def __delitem__(self, key: bytes) -> None: | ||
del self.wrapped_db[key] | ||
|
||
def _exists(self, key: bytes) -> bool: | ||
does_exist = key in self.wrapped_db | ||
if does_exist or self._log_missing_keys: | ||
self._keys_read.add(key) | ||
return does_exist | ||
|
||
|
||
class KeyAccessLoggerAtomicDB(BaseAtomicDB): | ||
""" | ||
Wraps around an atomic database, and tracks all the keys that were read since initialization. | ||
""" | ||
logger = logging.getLogger("eth.db.KeyAccessLoggerAtomicDB") | ||
|
||
def __init__(self, wrapped_db: AtomicDatabaseAPI, log_missing_keys: bool=True) -> None: | ||
""" | ||
:param log_missing_keys: True if a key is added to :attr:`keys_read` even if the | ||
key/value does not exist in the database. | ||
""" | ||
self.wrapped_db = wrapped_db | ||
self._keys_read: Set[bytes] = set() | ||
self._log_missing_keys = log_missing_keys | ||
|
||
@property | ||
def keys_read(self) -> FrozenSet[bytes]: | ||
# Make a defensive copy so callers can't modify the list externally | ||
return frozenset(self._keys_read) | ||
|
||
def __getitem__(self, key: bytes) -> bytes: | ||
try: | ||
result = self.wrapped_db.__getitem__(key) | ||
except KeyError: | ||
if self._log_missing_keys: | ||
self._keys_read.add(key) | ||
raise | ||
else: | ||
self._keys_read.add(key) | ||
return result | ||
|
||
def __setitem__(self, key: bytes, value: bytes) -> None: | ||
self.wrapped_db[key] = value | ||
|
||
def __delitem__(self, key: bytes) -> None: | ||
del self.wrapped_db[key] | ||
|
||
def _exists(self, key: bytes) -> bool: | ||
does_exist = key in self.wrapped_db | ||
if does_exist or self._log_missing_keys: | ||
self._keys_read.add(key) | ||
return does_exist | ||
|
||
@contextmanager | ||
def atomic_batch(self) -> Iterator[AtomicWriteBatchAPI]: | ||
with self.wrapped_db.atomic_batch() as readable_batch: | ||
yield readable_batch |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,19 @@ | ||
from eth.abc import ( | ||
BlockAPI, | ||
VirtualMachineAPI, | ||
) | ||
from eth.consensus import ( | ||
pow, | ||
) | ||
|
||
from eth.rlp.blocks import ( | ||
BaseBlock, | ||
) | ||
|
||
|
||
class POWMiningMixin: | ||
class POWMiningMixin(VirtualMachineAPI): | ||
""" | ||
A VM that does POW mining as well. Should be used only in tests, when we | ||
need to programatically populate a ChainDB. | ||
""" | ||
def finalize_block(self, block: BaseBlock) -> BaseBlock: | ||
block = super().finalize_block(block) # type: ignore | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Always nice to get rid of |
||
def finalize_block(self, block: BlockAPI) -> BlockAPI: | ||
block = super().finalize_block(block) | ||
nonce, mix_hash = pow.mine_pow_nonce( | ||
block.number, block.header.mining_hash, block.header.difficulty) | ||
return block.copy(header=block.header.copy(nonce=nonce, mix_hash=mix_hash)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
Added :class:`~eth.db.accesslog.KeyAccessLoggerDB` and its atomic twin; faster ``make | ||
validate-docs`` (but you have to remember to ``pip install -e .[doc]`` yourself); ``str(block)`` now | ||
includes the first 3 bytes of the block hash. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
from hypothesis import ( | ||
given, | ||
strategies as st, | ||
) | ||
import pytest | ||
|
||
from eth.db.accesslog import ( | ||
KeyAccessLoggerDB, | ||
KeyAccessLoggerAtomicDB, | ||
) | ||
from eth.db.backends.memory import MemoryDB | ||
|
||
|
||
@given(st.lists(st.binary())) | ||
@pytest.mark.parametrize('DB', ( | ||
lambda: KeyAccessLoggerAtomicDB(MemoryDB()), | ||
lambda: KeyAccessLoggerAtomicDB(MemoryDB(), log_missing_keys=False), | ||
lambda: KeyAccessLoggerAtomicDB(MemoryDB(), log_missing_keys=True), | ||
lambda: KeyAccessLoggerDB(MemoryDB()), | ||
lambda: KeyAccessLoggerDB(MemoryDB(), log_missing_keys=False), | ||
lambda: KeyAccessLoggerDB(MemoryDB(), log_missing_keys=True), | ||
)) | ||
def test_log_accesses(DB, keys): | ||
db = DB() | ||
assert len(db.keys_read) == 0 | ||
for key in keys: | ||
db[key] = b'placeholder' # value doesn't matter | ||
assert db[key] == b'placeholder' | ||
|
||
for key in keys: | ||
assert key in db.keys_read | ||
|
||
|
||
@pytest.mark.parametrize('DB', ( | ||
lambda: KeyAccessLoggerAtomicDB(MemoryDB()), | ||
lambda: KeyAccessLoggerAtomicDB(MemoryDB(), log_missing_keys=True), | ||
lambda: KeyAccessLoggerDB(MemoryDB()), | ||
lambda: KeyAccessLoggerDB(MemoryDB(), log_missing_keys=True), | ||
)) | ||
def test_logs_missing_keys(DB): | ||
db_logs_missing = DB() | ||
assert len(db_logs_missing.keys_read) == 0 | ||
assert b'exist-test' not in db_logs_missing | ||
|
||
assert b'exist-test' in db_logs_missing.keys_read | ||
|
||
with pytest.raises(KeyError, match='get-test'): | ||
db_logs_missing[b'get-test'] | ||
|
||
assert b'get-test' in db_logs_missing.keys_read | ||
assert len(db_logs_missing.keys_read) == 2 | ||
|
||
|
||
@pytest.mark.parametrize('DB', ( | ||
lambda: KeyAccessLoggerAtomicDB(MemoryDB(), log_missing_keys=False), | ||
lambda: KeyAccessLoggerDB(MemoryDB(), log_missing_keys=False), | ||
)) | ||
def test_dont_log_missing_keys(DB): | ||
db_doesnt_log_missing = DB() | ||
assert len(db_doesnt_log_missing.keys_read) == 0 | ||
assert b'exist-test' not in db_doesnt_log_missing | ||
|
||
assert b'exist-test' not in db_doesnt_log_missing.keys_read | ||
|
||
with pytest.raises(KeyError, match='get-test'): | ||
db_doesnt_log_missing[b'get-test'] | ||
|
||
assert b'get-test' not in db_doesnt_log_missing.keys_read | ||
assert len(db_doesnt_log_missing.keys_read) == 0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I guess the idea here was that one would be able to run
make build-docs
without the assumption that docs dependencies were already installed.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, if it was really fast, I probably wouldn't object, but the pip install actually takes a while. So if you run
make build-docs
over and over, it gets old really fast. Also, a bunch of our othermake
targets assume that you have the appropriate installed tools (wheel, setuptools, towncrier, etc). We could probably document them all better, though. 👍