Skip to content
This repository was archived by the owner on May 23, 2023. It is now read-only.

Commit 7c084bc

Browse files
committed
Merge branch 'develop' of github.com:ethereum/pyethereum into develop
2 parents f2f94da + 931311a commit 7c084bc

File tree

4 files changed

+4
-176
lines changed

4 files changed

+4
-176
lines changed

ethereum/chain.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import os
2-
import threading
32
import time
43
from ethereum import utils
54
from ethereum.utils import to_string, is_string
@@ -104,7 +103,6 @@ def __init__(self, db, genesis=None, new_head_cb=None, coinbase='\x00' * 20):
104103
self.index = Index(db)
105104
self.head_candidate = None
106105
self._coinbase = coinbase
107-
self.lock = threading.Lock()
108106
if genesis:
109107
self._initialize_blockchain(genesis)
110108
log.debug('chain @', head_hash=self.head)

ethereum/db.py

Lines changed: 3 additions & 164 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
1-
import os
2-
import threading
31
from ethereum.compress import compress, decompress
4-
from hashlib import md5
52
from ethereum import utils
63
from ethereum.slogging import get_logger
74
from rlp.utils import str_to_bytes
@@ -11,172 +8,16 @@
118
databases = {}
129

1310

14-
try:
15-
from CodernityDB.database import Database, DatabasePathException, \
16-
RecordNotFound
17-
from CodernityDB.hash_index import HashIndex
18-
except ImportError:
19-
_CodernityDB = None
20-
else:
21-
class MD5Index(HashIndex):
22-
23-
def __init__(self, *args, **kwargs):
24-
kwargs['key_format'] = '16s'
25-
super(MD5Index, self).__init__(*args, **kwargs)
26-
27-
def make_key_value(self, data):
28-
return md5(data['key']).digest(), None
29-
30-
def make_key(self, key):
31-
return md5(key).digest()
32-
33-
class _CodernityDB(object):
34-
35-
def __init__(self, dbfile):
36-
self.dbfile = os.path.abspath(dbfile)
37-
if dbfile in databases:
38-
self.db = databases[dbfile]
39-
assert isinstance(self.db, self.db.__class__)
40-
else:
41-
self.db = Database(dbfile)
42-
try:
43-
self.db.open()
44-
except DatabasePathException:
45-
self.db.create()
46-
self.db.add_index(MD5Index(dbfile, 'key'))
47-
databases[dbfile] = self.db
48-
self.uncommitted = dict()
49-
self.uncommitted_lock = threading.Lock()
50-
51-
def get(self, key):
52-
if key in self.uncommitted:
53-
if self.uncommitted[key] is None:
54-
raise KeyError("key not in db")
55-
return self.uncommitted[key]
56-
try:
57-
value = self.db.get('key', key, with_doc=True)['doc']['value']
58-
except RecordNotFound:
59-
raise KeyError("key not in db")
60-
return decompress(str_to_bytes(value))
61-
62-
def put(self, key, value):
63-
with self.uncommitted_lock:
64-
self.uncommitted[key] = value
65-
66-
def commit(self):
67-
log.debug('commit', db=self)
68-
with self.uncommitted_lock:
69-
for k, v in self.uncommitted.items():
70-
if v is None:
71-
doc = self.db.get('key', k, with_doc=True)['doc']
72-
self.db.delete(doc)
73-
else:
74-
self.db.insert({'key': k, 'value': compress(v)})
75-
self.uncommitted.clear()
76-
77-
def delete(self, key):
78-
with self.uncommitted_lock:
79-
self.uncommitted[key] = None
80-
81-
def __contains__(self, key):
82-
try:
83-
self.get(key)
84-
except KeyError:
85-
return False
86-
return True
87-
88-
def __eq__(self, other):
89-
return isinstance(other, self.__class__) and self.db == other.db
90-
91-
def __hash__(self):
92-
return utils.big_endian_to_int(str_to_bytes(self.__repr__()))
93-
94-
def __repr__(self):
95-
return '<DB at %d uncommitted=%d>' % (id(self.db), len(self.uncommitted))
96-
97-
def delete_db(self):
98-
del databases[self.dbfile]
99-
100-
try:
101-
import leveldb
102-
except ImportError:
103-
_LevelDB = None
104-
else:
105-
class _LevelDB(object):
106-
107-
def __init__(self, dbfile):
108-
self.dbfile = os.path.abspath(dbfile)
109-
if dbfile in databases:
110-
self.db = databases[dbfile]
111-
assert isinstance(self.db, leveldb.LevelDB)
112-
else:
113-
self.db = leveldb.LevelDB(dbfile)
114-
databases[dbfile] = self.db
115-
self.uncommitted = dict()
116-
self.lock = threading.Lock()
117-
118-
def get(self, key):
119-
if key in self.uncommitted:
120-
if self.uncommitted[key] is None:
121-
raise KeyError("key not in db")
122-
return self.uncommitted[key]
123-
o = decompress(str_to_bytes(self.db.Get(key)))
124-
self.uncommitted[key] = o
125-
return o
126-
127-
def put(self, key, value):
128-
with self.lock:
129-
self.uncommitted[key] = value
130-
131-
def commit(self):
132-
log.debug('commit', db=self)
133-
with self.lock:
134-
batch = leveldb.WriteBatch()
135-
for k, v in self.uncommitted.items():
136-
if v is None:
137-
batch.Delete(k)
138-
else:
139-
batch.Put(k, compress(v))
140-
self.db.Write(batch, sync=False)
141-
self.uncommitted.clear()
142-
143-
def delete(self, key):
144-
with self.lock:
145-
self.uncommitted[key] = None
146-
147-
def _has_key(self, key):
148-
try:
149-
self.get(key)
150-
return True
151-
except KeyError:
152-
return False
153-
154-
def __contains__(self, key):
155-
return self._has_key(key)
156-
157-
def __eq__(self, other):
158-
return isinstance(other, self.__class__) and self.db == other.db
159-
160-
def __hash__(self):
161-
return utils.big_endian_to_int(str_to_bytes(self.__repr__()))
162-
163-
def __repr__(self):
164-
return '<DB at %d uncommitted=%d>' % (id(self.db), len(self.uncommitted))
165-
166-
def delete_db(self):
167-
del databases[self.dbfile]
168-
169-
17011
class _EphemDB(object):
17112

17213
def __init__(self):
17314
self.db = {}
17415

17516
def get(self, key):
176-
return self.db[key]
17+
return decompress(self.db[key])
17718

17819
def put(self, key, value):
179-
self.db[key] = value
20+
self.db[key] = compress(value)
18021

18122
def delete(self, key):
18223
del self.db[key]
@@ -197,6 +38,4 @@ def __hash__(self):
19738
return utils.big_endian_to_int(str_to_bytes(self.__repr__()))
19839

19940

200-
DB = _LevelDB or _CodernityDB
201-
assert DB is not None
202-
EphemDB = _EphemDB
41+
DB = EphemDB = _EphemDB

ethereum/tests/utils.py

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@
1111
import json
1212
import os
1313
import tempfile
14-
import ethereum.utils as utils
15-
import ethereum.testutils as testutils
1614
from ethereum.db import DB as DB
1715
__TESTDATADIR = "../tests"
1816

@@ -24,10 +22,4 @@ def load_test_data(fname):
2422

2523

2624
def new_db():
27-
return DB(utils.db_path(tempfile.mktemp()))
28-
29-
# def set_db(name=''):
30-
# if name:
31-
# utils.data_dir.set(os.path.join(tempdir, name))
32-
# else:
33-
# utils.data_dir.set(tempfile.mktemp())
25+
return DB()

requirements.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
bitcoin
2-
leveldb
32
pysha3
43
pytest
54
pytest-timeout

0 commit comments

Comments
 (0)