Beispiel #1
0
    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')
Beispiel #2
0
    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')
Beispiel #3
0
    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')
class SubmitAndRecordChainNode(BaseChainNode):
    def __init__(self,
                 config_path=my_config.CONFIG_PATH,
                 submit_hash_callback_objs=[],
                 record_over_callback_objs=[],
                 wait_time=3):
        self._record_hash_mgr = RecordHash(config_path)
        self._proved_db_mgr = ProvedDB(config_path, 'json')
        submit_hash_callback_objs = [self] + submit_hash_callback_objs
        record_over_callback_objs = [self] + record_over_callback_objs
        super(SubmitAndRecordChainNode,
              self).__init__(config_path, submit_hash_callback_objs,
                             record_over_callback_objs, wait_time)

    def submitHashEventCallback(self, node, event):
        event_finalise_hash = event['args']['finalise_hash']
        self._record_hash_mgr.record(event_finalise_hash)

    def recordOverEventCallback(self, node, event):
        event_finalise_hash = event['args']['finalise_hash']
        self._proved_db_mgr.finalise(event_finalise_hash)