def testMultipleFinaliseGroup(self): test_db = ProvedDB(_TEST_CONFIG, 'json') for i in range(0, TEST_PAIR_PERIOD * TEST_PAIR_LENGTH, TEST_PAIR_PERIOD): for j in range(TEST_PAIR_PERIOD): val = str(i + j) test_db.create({val: val}) for j in range(TEST_PAIR_PERIOD): check_group_hash = calculate_entry_hash([str(i + j), str(i + j)]) existed, entries_length = test_db.get_finalised_group_entries_length(check_group_hash) self.assertEqual(False, existed, 'hash does exist') self.assertEqual(0, entries_length, 'hash entry index should be zero') check_hash_sum = calculate_submit_hash([[str(i + j), str(i + j)] for j in range(TEST_PAIR_PERIOD)]) test_db.finalise(check_hash_sum) for i in range(0, TEST_PAIR_PERIOD * TEST_PAIR_LENGTH, TEST_PAIR_PERIOD): for j in range(TEST_PAIR_PERIOD): check_group_hash = calculate_entry_hash([str(i + j), str(i + j)]) existed, entries_length = test_db.get_finalised_group_entries_length(check_group_hash) self.assertEqual(True, existed, 'hash does exist') self.assertEqual(TEST_PAIR_PERIOD, entries_length, 'hash entry index should not be zero') for k in range(TEST_PAIR_PERIOD): entry_hash = test_db.get_finalised_group_entry(check_group_hash, k) self.assertEqual(calculate_entry_hash([str(i + k), str(i + k)]), entry_hash, 'hash should be the same')
def testMultipleSubmitChecking(self): test_db = ProvedDB(_TEST_CONFIG, 'json') for i in range(0, TEST_PAIR_PERIOD * TEST_PAIR_LENGTH, TEST_PAIR_PERIOD): for j in range(TEST_PAIR_PERIOD): val = str(i + j) test_db.create({val: val}) for i in range(0, TEST_PAIR_PERIOD * TEST_PAIR_LENGTH, TEST_PAIR_PERIOD): check_hash_sum = calculate_submit_hash([[str(i), str(i)], [str(i + 1), str(i + 1)]]) existed, finalised, entries_length = test_db.get_finalise_entries_length(check_hash_sum) self.assertEqual(True, existed, 'hash does exist') self.assertEqual(False, finalised, 'hash doesn finalise') self.assertEqual(2, entries_length, 'hash entry index should not be zero') test_db.finalise(check_hash_sum) existed, finalised, entries_length = test_db.get_finalise_entries_length(check_hash_sum) self.assertEqual(True, existed, 'hash does exist') self.assertEqual(True, finalised, 'hash doesn finalise') self.assertEqual(2, entries_length, 'hash entry index should not be zero') for j in range(entries_length): entry_hash = test_db.get_finalise_entry(check_hash_sum, j) self.assertEqual(calculate_entry_hash([str(i + j), str(i + j)]), entry_hash, 'hash should be the same')
def testSingleFinaliseGroup(self): test_db = ProvedDB(_TEST_CONFIG, 'json') test_key = 'show me the money' test_data = [{ 'testaaa': 'hash1' }, { 'testbbb': 'hash2' }] test_db.create({test_key: test_data[0]}) test_db.update({test_key: test_data[1]}) for check_val in test_data: check_group_hash = calculate_entry_hash([test_key, check_val]) existed, entries_length = test_db.get_finalised_group_entries_length(check_group_hash) self.assertEqual(False, existed, 'hash does exist') self.assertEqual(0, entries_length, 'hash entry index should be zero') check_hash_sum = calculate_submit_hash([[test_key, _] for _ in test_data]) test_db.finalise(check_hash_sum) for check_val in test_data: check_group_hash = calculate_entry_hash([test_key, check_val]) existed, entries_length = test_db.get_finalised_group_entries_length(check_group_hash) self.assertEqual(True, existed, 'hash does exist') self.assertEqual(len(test_data), entries_length, 'hash entry index should not be zero') for i in range(entries_length): entry_hash = test_db.get_finalised_group_entry(check_group_hash, i) self.assertEqual(calculate_entry_hash([test_key, test_data[i]]), entry_hash, 'hash should be the same')
def testSingleEvent(self): private_node = SubmitAndRecordChainNode(config_path=_TEST_CONFIG, submit_hash_callback_objs=[], record_over_callback_objs=[], wait_time=1) private_node.start() test_db = ProvedDB(_TEST_CONFIG, 'json') test_data = [{'testaaa': 'hash1'}, {'testbbb': 'hash2'}] test_key = 'May the force be with you' test_db.create({test_key: test_data[0]}) test_db.update({test_key: test_data[1]}) gevent.sleep(1) check_hash_sum = calculate_submit_hash([[test_key, _] for _ in test_data]) existed, finalised, entries_length = test_db.get_finalise_entries_length( check_hash_sum) self.assertEqual(True, existed, 'hash does exist') self.assertEqual(True, finalised, 'hash doesn finalise') self.assertEqual(2, entries_length, 'hash entry index should not be zero') for i in range(entries_length): entry_hash = test_db.get_finalise_entry(check_hash_sum, i) self.assertEqual(calculate_entry_hash([test_key, test_data[i]]), entry_hash, 'hash should be the same') private_node.kill()
def testCheckEntryWithEntry(self): test_key = 'testCheckEntryWithEntry' test_db = ProvedDB(_TEST_CONFIG, 'json') test_db.create(self.TEST_DATA[test_key][0]) now_key = list(self.TEST_DATA[test_key][0])[0] now_val = self.TEST_DATA[test_key][0][now_key] ret = test_db.check_entry(now_key, now_val) self.assertEqual(ret, True, 'checking should pass')
def testCheckAllEntriesEntry(self): deploy.deploy(_TEST_CONFIG) test_key = 'testCheckAllEntriesEntry' test_db = ProvedDB(_TEST_CONFIG, 'json') for test_case in self.TEST_DATA[test_key]: test_db.create(test_case) self.assertEqual(test_db.check_all_entries(), True, 'There should pass the checking') test_db.delete(list(self.TEST_DATA[test_key][1])[0]) self.assertEqual(test_db.check_all_entries(), True, 'There should pass the checking')
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 testSingleEvent(self): private_node = BaseChainNode(config_path=_TEST_CONFIG, submit_hash_callback_objs=[self], record_over_callback_objs=[], wait_time=1) private_node.start() test_db = ProvedDB(_TEST_CONFIG, 'json') test_data = [{'testaaa': 'hash1'}, {'testbbb': 'hash2'}] test_key = 'May the force be with you' test_db.create({test_key: test_data[0]}) test_db.update({test_key: test_data[1]}) private_node.join() check_hash_sum = calculate_submit_hash([[test_key, _] for _ in test_data]) self.assertEqual(self._submit_single_hash_data, check_hash_sum, 'should be the same')
def testMultipleEvent(self): self._submit_multiple_hash_data = [] test_db = ProvedDB(_TEST_CONFIG, 'json') private_node = BaseChainNode(config_path=_TEST_CONFIG, submit_hash_callback_objs=[self], record_over_callback_objs=[], wait_time=1) private_node.start() for i in range(0, TEST_PAIR_PERIOD * TEST_PAIR_LENGTH, TEST_PAIR_PERIOD): for j in range(TEST_PAIR_PERIOD): val = str(i + j) test_db.create({val: val}) private_node.join(10) for i in range(0, TEST_PAIR_PERIOD * TEST_PAIR_LENGTH, TEST_PAIR_PERIOD): check_hash_sum = calculate_submit_hash([[str(i), str(i)], [str(i + 1), str(i + 1)]]) self.assertEqual( self._submit_multiple_hash_data[int(i / TEST_PAIR_PERIOD)], check_hash_sum, 'should be the same')
def testBehaior(self): private_node = SubmitAndRecordChainNode(config_path=_TEST_CONFIG, submit_hash_callback_objs=[], record_over_callback_objs=[], wait_time=1) private_node.start() test_db = ProvedDB(_TEST_CONFIG, 'json') # setting key val test_db.create({'my_key1': 'key1_data_01'}) test_db.create({'my_key2': 'key1_data_01'}) test_db.create({'my_key3': 'key1_data_01'}) test_db.create({'my_key4': 'key1_data_01'}) test_db.create({'my_key5': 'key1_data_01'}) # check all entry test_db.check_all_entries() val = test_db.retrieve('my_key2') self.assertEqual(True, test_db.check_entry('my_key2', val), 'should pass') gevent.sleep(1) # check finalised data check_hash = calculate_entry_hash(['my_key3', 'key1_data_01']) existed, entries_length = test_db.get_finalised_group_entries_length( check_hash) self.assertEqual(True, existed, 'should be pass') all_check_hash = [ test_db.get_finalised_group_entry(check_hash, i) for i in range(entries_length) ] self.assertEqual(True, check_hash in all_check_hash, 'should pass') check_hash_sum = calculate_submit_hash_from_group(all_check_hash) existed, finalised, entries_length = test_db.get_finalise_entries_length( check_hash_sum) self.assertEqual(True, existed, 'hash does exist') self.assertEqual(True, finalised, 'hash doesn finalise') self.assertEqual(2, entries_length, 'hash entry index should not be zero') double_check_hashes = [ test_db.get_finalise_entry(check_hash_sum, i) for i in range(entries_length) ] self.assertEqual(double_check_hashes, all_check_hash, 'should pass') check_hash = calculate_entry_hash(['my_key5', 'key1_data_01']) existed, entries_length = test_db.get_finalised_group_entries_length( check_hash) self.assertEqual(False, existed, 'should be pass') # Test again test_db.update({'my_key5': 'key1_data_02'}) gevent.sleep(1) check_hash = calculate_entry_hash(['my_key5', 'key1_data_01']) existed, entries_length = test_db.get_finalised_group_entries_length( check_hash) self.assertEqual(True, existed, 'should be pass') all_check_hash = [ test_db.get_finalised_group_entry(check_hash, i) for i in range(entries_length) ] self.assertEqual(True, check_hash in all_check_hash, 'should pass') check_hash_sum = calculate_submit_hash_from_group(all_check_hash) existed, finalised, entries_length = test_db.get_finalise_entries_length( check_hash_sum) self.assertEqual(True, existed, 'hash does exist') self.assertEqual(True, finalised, 'hash doesn finalise') self.assertEqual(2, entries_length, 'hash entry index should not be zero') double_check_hashes = [ test_db.get_finalise_entry(check_hash_sum, i) for i in range(entries_length) ] self.assertEqual(double_check_hashes, all_check_hash, 'should pass') # # setting key val again test_db.delete('my_key2') test_db.create({'my_key6': 'key1_data_01'}) # Test again test_db.update({'my_key6': 'key1_data_02'}) gevent.sleep(1) check_hash = calculate_entry_hash(['my_key6', 'key1_data_01']) existed, entries_length = test_db.get_finalised_group_entries_length( check_hash) self.assertEqual(True, existed, 'should be pass') all_check_hash = [ test_db.get_finalised_group_entry(check_hash, i) for i in range(entries_length) ] self.assertEqual(True, check_hash in all_check_hash, 'should pass') check_hash_sum = calculate_submit_hash_from_group(all_check_hash) existed, finalised, entries_length = test_db.get_finalise_entries_length( check_hash_sum) self.assertEqual(True, existed, 'hash does exist') self.assertEqual(True, finalised, 'hash doesn finalise') self.assertEqual(2, entries_length, 'hash entry index should not be zero') double_check_hashes = [ test_db.get_finalise_entry(check_hash_sum, i) for i in range(entries_length) ] self.assertEqual(double_check_hashes, all_check_hash, 'should pass') private_node.kill()
def testCreateDuplicateID(self): test_db = ProvedDB(_TEST_CONFIG, 'json') data = self.TEST_DATA['testCreateDuplicateID'] test_db.create(data) self.assertRaisesRegex(IOError, 'unique key already exist', test_db.create, data)