def __init__(self, config=my_config.CONFIG_PATH, mytype='json', path=''): if not any(mytype == _ for _ in self._support_types.keys()): raise IOError('{0} type not in support_types {1}'.format( mytype, self._support_types)) if not path: config_handler = ConfigHandler(config) path = config_handler.get_chain_config('DB', 'db_path') self._type_db = self._support_types[mytype](path) self._onchain_handler = ProvedDBOnChainHandler(config)
def testDeleteNoEntry(self): test_db = ProvedDB(_TEST_CONFIG, 'json') data = self.TEST_DATA['testDeleteNoEntry'] key = list(data)[0] test_db.delete(key) data = test_db.retrieve(key) self.assertEqual(data, '', 'data is deleted!') onchain_handler = ProvedDBOnChainHandler(_TEST_CONFIG) exist, retrieve_hash = onchain_handler.retrieve(key) self.assertEqual(exist, False, 'key is not on chain') self.assertEqual(retrieve_hash, ZERO_VALUE, 'data on chain is inconsistent {0} != {1}'.format(retrieve_hash, ZERO_VALUE))
def testCreateEntry(self): test_db = ProvedDB(_TEST_CONFIG, 'json') data = self.TEST_DATA['testCreateEntry'] test_db.create(data) # Don't use select check here, so assume create is success here # Call solidity select for find hash is already on smart contract onchain_handler = ProvedDBOnChainHandler(_TEST_CONFIG) key = list(data)[0] val = onchain_handler.hash_entry([key, data[key]]) exist, data = onchain_handler.retrieve(key) self.assertEqual(exist, True, 'key is not on chain') self.assertEqual(data, val, 'data on chain is inconsistent {0} != {1}'.format(data, val))
def testDeleteEntry(self): test_db = ProvedDB(_TEST_CONFIG, 'json') data = self.TEST_DATA['testDeleteEntry'] test_db.create(data) key = list(data)[0] test_db.delete(key) data = test_db.retrieve(key) self.assertEqual(data, '', 'data is deleted!') onchain_handler = ProvedDBOnChainHandler(_TEST_CONFIG) val = onchain_handler.hash_entry([key, data]) exist, data = onchain_handler.retrieve(key) self.assertEqual(exist, False, 'key is on chain') self.assertEqual(data, ZERO_VALUE, 'data on chain is inconsistent {0} != {1}'.format(data, val))
def testRetrieveEntry(self): test_db = ProvedDB(_TEST_CONFIG, 'json') data = self.TEST_DATA['testRetrieveEntry'] test_db.create(data) retrieve_data = test_db.retrieve(list(data)[0]) check_data = data[list(data)[0]] self.assertEqual(retrieve_data, check_data, 'retrive should be the same data, {0} != {1}'.format(retrieve_data, check_data)) onchain_handler = ProvedDBOnChainHandler(_TEST_CONFIG) key = list(data)[0] onchain_hash = onchain_handler.hash_entry([key, data[key]]) exist, retrieve_hash = onchain_handler.retrieve(key) self.assertEqual(exist, True, 'key is not on chain') self.assertEqual(retrieve_hash, onchain_hash, 'data on chain is inconsistent {0} != {1}'.format(retrieve_hash, onchain_hash))
def testRetrieveNoEntry(self): onchain_handler = ProvedDBOnChainHandler(_TEST_CONFIG) exist, data = onchain_handler.retrieve('You should not exist!!!') self.assertEqual(exist, False, 'key not on chain') self.assertEqual(data, ZERO_VALUE, "data doesn't exist also!")
class ProvedDB(): _support_types = {'json': ProvedJsonDB} def __init__(self, config=my_config.CONFIG_PATH, mytype='json', path=''): if not any(mytype == _ for _ in self._support_types.keys()): raise IOError('{0} type not in support_types {1}'.format( mytype, self._support_types)) if not path: config_handler = ConfigHandler(config) path = config_handler.get_chain_config('DB', 'db_path') self._type_db = self._support_types[mytype](path) self._onchain_handler = ProvedDBOnChainHandler(config) def create(self, entry): self._type_db.create(entry) key = list(entry)[0] val = entry[key] self._onchain_handler.create(key, val) # use retrive to check it retrieve_data = self.retrieve(key) if retrieve_data != entry[list(entry)[0]]: raise IOError('create fail...') def retrieve(self, key): db_data = self._type_db.retrieve(key) onchain_exist, onchain_hash = self._onchain_handler.retrieve(key) if not onchain_exist: if db_data or onchain_hash != ZERO_VALUE: raise IOError( 'key "{0}" is not exist, shouldn\'t have any data, {1} v.s. {2}' .format(key, onchain_hash, db_data)) else: db_hash = self._onchain_handler.hash_entry([key, db_data]) if onchain_hash != db_hash: raise IOError( 'hash value doens\'t consist, {0} v.s. {1}'.format( onchain_hash, db_hash)) return db_data def update(self, entry): self._type_db.update(entry) key = list(entry)[0] val = entry[key] self._onchain_handler.update(key, val) retrieve_data = self.retrieve(key) if retrieve_data != val: raise IOError('update fail...') def delete(self, key): self._type_db.delete(key) self._onchain_handler.delete(key) def check_entry(self, key, val): if not self._type_db.check_entry(key, val): return False if not self._onchain_handler.check_entry(key, val): return False return True def check_all_entries(self): data_keys = [_ for _ in self._type_db.get_keys()] online_keys = [ self._onchain_handler.get_key(idx) for idx in range(self._onchain_handler.get_keys_length()) ] if set(data_keys) != set(online_keys): return False for key in data_keys: data_val = self._type_db.retrieve(key) if not self._onchain_handler.check_entry(key, data_val): return False return True def get_finalise_entries_length(self, hash_sum): return self._onchain_handler.get_finalise_entries_length(hash_sum) def get_finalise_entry(self, hash_sum, idx): return self._onchain_handler.get_finalise_entry(hash_sum, idx) def finalise(self, hash_sum): self._onchain_handler.finalise(hash_sum) def get_finalised_group_entries_length(self, hash_sum): return self._onchain_handler.get_finalised_group_entries_length( hash_sum) def get_finalised_group_entry(self, hash_sum, idx): return self._onchain_handler.get_finalised_group_entry(hash_sum, idx)