This repository has been archived by the owner on Jun 13, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
195 additions
and
9 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,3 +3,4 @@ contract_data/ | |
**/__pycache__/ | ||
*.pyc | ||
.pytest_cache/ | ||
.db/ |
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,5 +1,19 @@ | ||
import os | ||
|
||
PROJECT_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) | ||
|
||
plasma_config = dict( | ||
ROOT_CHAIN_CONTRACT_ADDRESS='0xda52B0A0a040BFeAb711065cB69321ebAE9bB96f', | ||
AUTHORITY=b';\x08\x84\xf4\xe5\x0e\x9b\xc2\xce\x9b"J\xb7/\xea\x89\xa8\x1c\xdf|', | ||
AUTHORITY_KEY=b'\xa1\x89i\x81|,\xef\xad\xf5+\x93\xeb \xf9\x17\xdc\xe7`\xce\x13\xb2\xac\x90%\xe06\x1a\xd1\xe7\xa1\xd4H', | ||
) | ||
|
||
""" | ||
db_config = { | ||
'type': 'leveldb' | 'memory' (required) | ||
'path': '' (optional, if nor specific set, would have default path) | ||
} | ||
""" | ||
db_config = { | ||
'type': 'memory' | ||
} |
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,42 @@ | ||
import plyvel | ||
import rlp | ||
from ethereum import utils | ||
|
||
from plasma_cash.child_chain.block import Block | ||
|
||
from .db_interface import DbInterface | ||
from .exceptions import BlockAlreadyExistsException | ||
|
||
|
||
class LevelDb(DbInterface): | ||
CURRENT_BLOCK_NUM_KEY = b'current_block_num' | ||
|
||
def __init__(self, path): | ||
self.db = plyvel.DB(path, create_if_missing=True) | ||
|
||
def get_block(self, block_number): | ||
key = str.encode(str(block_number)) | ||
encoded_block = self.db.get(key) | ||
if encoded_block: | ||
return rlp.decode(utils.decode_hex(encoded_block.decode()), Block) | ||
else: | ||
return None | ||
|
||
def save_block(self, block, block_number): | ||
key = str.encode(str(block_number)) | ||
if self.db.get(key): | ||
raise BlockAlreadyExistsException('should not save block with same blknum again.') | ||
self.db.put(key, str.encode(rlp.encode(block).hex())) | ||
|
||
def get_current_block_num(self): | ||
current_block_num = self.db.get(self.CURRENT_BLOCK_NUM_KEY) | ||
if not current_block_num: | ||
self.db.put(self.CURRENT_BLOCK_NUM_KEY, '1'.encode()) | ||
return 1 | ||
return int(current_block_num.decode()) | ||
|
||
def increment_current_block_num(self): | ||
current_block_num = self.get_current_block_num() | ||
incr_block_num = current_block_num + 1 | ||
self.db.put(self.CURRENT_BLOCK_NUM_KEY, str(incr_block_num).encode()) | ||
return incr_block_num |
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 |
---|---|---|
|
@@ -4,6 +4,7 @@ Flask==1.0.2 | |
requests==2.18.4 | ||
rlp==0.6.0 | ||
ethereum==2.3.1 | ||
plyvel==1.0.4 | ||
|
||
# test | ||
pytest==3.5.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
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,66 @@ | ||
import pytest | ||
from mockito import ANY, when | ||
|
||
from plasma_cash.child_chain.block import Block | ||
from plasma_cash.utils.db.exceptions import BlockAlreadyExistsException | ||
from plasma_cash.utils.db.leveldb import LevelDb | ||
from unit_tests.unstub_mixin import UnstubMixin | ||
|
||
|
||
def is_byte(data): | ||
try: | ||
data = data.decode() | ||
except Exception: | ||
return False | ||
return True | ||
|
||
|
||
class FakeLevelDb(object): | ||
def __init__(self): | ||
self.data = {} | ||
|
||
def put(self, k, v): | ||
if not is_byte(k) or not is_byte(v): | ||
raise Exception('leveldb could only take byte type as input') | ||
self.data[k] = v | ||
|
||
def get(self, k): | ||
if not is_byte(k): | ||
raise Exception('leveldb could only take byte type as input') | ||
return self.data.get(k) | ||
|
||
|
||
class TestLevelDb(UnstubMixin): | ||
|
||
@pytest.fixture(scope='function') | ||
def db(self): | ||
db = FakeLevelDb() | ||
(when('plasma_cash.utils.db.leveldb.plyvel') | ||
.DB(ANY, create_if_missing=True).thenReturn(db)) | ||
return LevelDb('test_db') | ||
|
||
def test_block_normal_case(self, db): | ||
DUMMY_BLOCK = Block() | ||
DUMMY_BLK_NUM = 1 | ||
db.save_block(DUMMY_BLOCK, DUMMY_BLK_NUM) | ||
assert db.get_block(DUMMY_BLK_NUM) == DUMMY_BLOCK | ||
|
||
def test_get_block_none(self, db): | ||
NON_EXIST_BLK_NUM = -1 | ||
assert db.get_block(NON_EXIST_BLK_NUM) is None | ||
|
||
def test_save_block_already_exists(self, db): | ||
DUMMY_BLOCK = Block() | ||
DUMMY_BLK_NUM = 1 | ||
db.save_block(DUMMY_BLOCK, DUMMY_BLK_NUM) | ||
with pytest.raises(BlockAlreadyExistsException): | ||
db.save_block('second block should fail', DUMMY_BLK_NUM) | ||
|
||
def test_get_current_block_num_first_time_return_1(self, db): | ||
assert db.get_current_block_num() == 1 | ||
|
||
def test_increment_current_block_num(self, db): | ||
block_num = db.get_current_block_num() | ||
db.increment_current_block_num() | ||
block_num_incr = db.get_current_block_num() | ||
assert block_num_incr == block_num + 1 |
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