Skip to content

Commit 430fde7

Browse files
committed
Ensure atomic_batch works over DBProxy
Fixes #1494
1 parent a251ffb commit 430fde7

File tree

2 files changed

+29
-2
lines changed

2 files changed

+29
-2
lines changed

tests/trinity/core/database/test_database_over_ipc_manager.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,3 +94,19 @@ def test_db_over_ipc_manager(manager):
9494

9595
with pytest.raises(KeyError):
9696
db[b'not-present']
97+
98+
99+
def test_atomic_batch_over_ipc_manager(manager):
100+
db = manager.get_db()
101+
102+
assert b'key-b' not in db
103+
assert b'key-c' not in db
104+
105+
with db.atomic_batch() as batch:
106+
batch.set(b'key-b', b'value-b')
107+
batch.set(b'key-c', b'value-c')
108+
109+
assert b'key-b' in db
110+
assert db[b'key-b'] == b'value-b'
111+
assert b'key-c' in db
112+
assert db[b'key-c'] == b'value-c'

trinity/db/base.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
1+
from contextlib import contextmanager
2+
from typing import (
3+
Generator,
4+
)
15
# Typeshed definitions for multiprocessing.managers is incomplete, so ignore them for now:
26
# https://github.com/python/typeshed/blob/85a788dbcaa5e9e9a62e55f15d44530cd28ba830/stdlib/3/multiprocessing/managers.pyi#L3
37
from multiprocessing.managers import ( # type: ignore
48
BaseProxy,
59
)
610

711
from eth.db.backends.base import BaseDB
12+
from eth.db.atomic import AtomicDBWriteBatch
813

914
from trinity.utils.mp import async_method
1015

@@ -15,12 +20,13 @@ class DBProxy(BaseProxy):
1520
'__delitem__',
1621
'__getitem__',
1722
'__setitem__',
23+
'atomic_batch',
24+
'coro_set',
25+
'coro_exists',
1826
'delete',
1927
'exists',
2028
'get',
2129
'set',
22-
'coro_set',
23-
'coro_exists',
2430
)
2531
coro_set = async_method('set')
2632
coro_exists = async_method('exists')
@@ -49,6 +55,11 @@ def exists(self, key: bytes) -> bool:
4955
def __contains__(self, key: bytes) -> bool:
5056
return self._callmethod('__contains__', (key,))
5157

58+
@contextmanager
59+
def atomic_batch(self) -> Generator['AtomicDBWriteBatch', None, None]:
60+
with AtomicDBWriteBatch._commit_unless_raises(self) as readable_batch:
61+
yield readable_batch
62+
5263

5364
class AsyncBaseDB(BaseDB):
5465

0 commit comments

Comments
 (0)