Beispiel #1
0
    def test_digest(self):
        block_batch = self.block_batch

        tx_batch = TransactionBatch(create_hash_256())
        key0 = create_hash_256()
        tx_batch[key0] = TransactionBatchValue(b'value0', True)
        key1 = create_hash_256()
        tx_batch[key1] = TransactionBatchValue(b'value1', True)
        key2 = create_hash_256()
        tx_batch[key2] = TransactionBatchValue(b'value2', True)

        block_batch.update(tx_batch)
        data = [key0, b'value0', key1, b'value1', key2, b'value2']
        expected = sha3_256(b'|'.join(data))
        ret = block_batch.digest()
        self.assertEqual(expected, ret)

        tx_batch[key2] = TransactionBatchValue(None, True)
        block_batch.update(tx_batch)
        hash1 = block_batch.digest()

        tx_batch[key2] = TransactionBatchValue(b'', True)
        block_batch.update(tx_batch)
        hash2 = block_batch.digest()
        self.assertNotEqual(hash1, hash2)
Beispiel #2
0
    def test_put(self):
        """WritableDatabase supports put()
        """
        context = self.context
        self.context_db._put(context, b'key0', b'value0', True)
        value = self.context_db.get(context, b'key0')
        self.assertEqual(b'value0', value)

        batch = self.context.tx_batch
        self.assertEqual(TransactionBatchValue(b'value0', True), batch[b'key0'])

        self.context_db._put(context, b'key0', b'value1', True)
        self.context_db._put(context, b'key1', b'value1', True)

        self.assertEqual(len(batch), 2)
        self.assertEqual(batch[b'key0'], TransactionBatchValue(b'value1', True))
        self.assertEqual(batch[b'key1'], TransactionBatchValue(b'value1', True))

        self.context_db._put(context, b'key2', b'value2', False)
        self.context_db._put(context, b'key3', b'value3', False)

        value2 = self.context_db.get(context, b'key2')
        value3 = self.context_db.get(context, b'key3')
        self.assertEqual(b'value2', value2)
        self.assertEqual(b'value3', value3)

        self.assertEqual(len(batch), 4)
        self.assertEqual(batch[b'key2'], TransactionBatchValue(b'value2', False))
        self.assertEqual(batch[b'key3'], TransactionBatchValue(b'value3', False))

        # overwrite
        self.assertRaises(DatabaseException, self.context_db._put, context, b'key3', b'value3', True)
        self.assertRaises(DatabaseException, self.context_db._delete, context, b'key3', True)
    def test_contains(self):
        tx_batch = TransactionBatch()
        init_call_count = tx_batch.call_count
        self.assertEqual(0, len(tx_batch))
        self.assertEqual(1, init_call_count)

        tx_batch[b'key0'] = TransactionBatchValue(b'value0', True)
        self.assertEqual(1, len(tx_batch))

        tx_batch.enter_call()
        tx_batch[b'key1'] = TransactionBatchValue(b'value1', True)
        self.assertEqual(2, len(tx_batch))
        self.assertEqual(init_call_count + 1, tx_batch.call_count)

        tx_batch.enter_call()
        tx_batch[b'key0'] = TransactionBatchValue(None, True)
        tx_batch[b'key1'] = TransactionBatchValue(b'key1', True)
        tx_batch[b'key2'] = TransactionBatchValue(b'value2', True)
        self.assertEqual(5, len(tx_batch))
        self.assertEqual(init_call_count + 2, tx_batch.call_count)

        keys = [b'key0', b'key1', b'key2']
        for key in keys:
            self.assertTrue(key in tx_batch)

        keys = [b'key3', b'key4']
        for key in keys:
            self.assertFalse(key in tx_batch)

        tx_batch = TransactionBatch()
        self.assertFalse(b'key' in tx_batch)
Beispiel #4
0
    def test_delete(self):
        context = self.context
        db = self.context_db
        tx_batch = context.tx_batch
        block_batch = context.block_batch

        db._put(context, b'key0', b'value0', True)
        db._put(context, b'key1', b'value1', True)
        self.assertEqual(b'value0', db.get(context, b'key0'))
        self.assertEqual(TransactionBatchValue(b'value0', True),
                         tx_batch[b'key0'])

        block_batch.update(tx_batch)
        state_wal = StateWAL(block_batch)
        db.write_batch(context, state_wal)
        tx_batch.clear()
        block_batch.clear()

        self.assertEqual(0, len(tx_batch))
        self.assertEqual(b'value0', db.get(context, b'key0'))

        db._delete(context, b'key0', True)
        db._delete(context, b'key1', False)
        self.assertEqual(None, db.get(context, b'key0'))
        self.assertEqual(None, db.get(context, b'key1'))
        self.assertEqual(TransactionBatchValue(None, True), tx_batch[b'key0'])
        self.assertEqual(TransactionBatchValue(None, False), tx_batch[b'key1'])
        block_batch.update(tx_batch)
        db.write_batch(context, state_wal)
        tx_batch.clear()
        block_batch.clear()

        self.assertEqual(0, len(tx_batch))
        self.assertIsNone(db.get(context, b'key0'))
        self.assertIsNone(db.get(context, b'key1'))
Beispiel #5
0
    def test_put_tx_batch(self):
        tx_hash = create_hash_256()
        tx_index = 1
        tx_batch = TransactionBatch(tx_hash)
        key = create_hash_256()
        tx_batch[key] = TransactionBatchValue(b'value', True, tx_index)
        key0 = create_hash_256()
        tx_batch[key0] = TransactionBatchValue(b'value0', True, tx_index)
        key1 = create_hash_256()
        tx_batch[key1] = TransactionBatchValue(b'value1', True, tx_index)
        key2 = create_hash_256()
        tx_batch[key2] = TransactionBatchValue(b'value2', True, tx_index)
        self.assertEqual(4, len(tx_batch))
        self.block_batch.update(tx_batch)
        self.assertEqual(4, len(self.block_batch))

        tx_index_2 = 2
        tx_batch_2 = TransactionBatch(tx_hash)
        tx_batch_2[key] = TransactionBatchValue(b'updated_value', True,
                                                tx_index_2)
        self.block_batch.update(tx_batch_2)
        self.assertEqual(4, len(self.block_batch))

        self.assertEqual(BlockBatchValue(b'value0', True, [tx_index]),
                         self.block_batch[key0])
        self.assertEqual(BlockBatchValue(b'value1', True, [tx_index]),
                         self.block_batch[key1])
        self.assertEqual(BlockBatchValue(b'value2', True, [tx_index]),
                         self.block_batch[key2])
        # As overwrite twice
        self.assertEqual(
            BlockBatchValue(b'updated_value', True, [tx_index, tx_index_2]),
            self.block_batch[key])
Beispiel #6
0
    def test_block_batch_update_tx_index(self):
        block_batch = self.block_batch

        overwrite_key = create_hash_256()
        last_value = None

        for i in range(3):
            last_value = b'value' + i.to_bytes(1, 'big')
            tx_batch = TransactionBatch(create_hash_256())
            tx_batch[overwrite_key] = TransactionBatchValue(
                last_value, True, i)
            block_batch.update(tx_batch)
        tx_batch = TransactionBatch(create_hash_256())
        tx_batch[overwrite_key] = TransactionBatchValue(
            b'reverted_value1', True, 3)
        tx_batch.revert_call()
        block_batch.update(tx_batch)

        tx_batch = TransactionBatch(create_hash_256())
        tx_batch[overwrite_key] = TransactionBatchValue(
            b'reverted_value2', True, 4)
        tx_batch.clear()
        block_batch.update(tx_batch)

        actual_overwrite_value: 'BlockBatchValue' = block_batch.get(
            overwrite_key)
        assert actual_overwrite_value.value == last_value
        assert actual_overwrite_value.tx_indexes == [0, 1, 2]
    def test_set_item(self):
        tx_batch = TransactionBatch()
        init_call_count = tx_batch.call_count
        self.assertEqual(0, len(tx_batch))
        self.assertEqual(1, init_call_count)

        tx_batch[b'key0'] = TransactionBatchValue(b'value0', True)
        self.assertEqual(1, len(tx_batch))

        tx_batch.enter_call()
        tx_batch[b'key1'] = TransactionBatchValue(b'value1', True)
        self.assertEqual(2, len(tx_batch))
        self.assertEqual(init_call_count + 1, tx_batch.call_count)

        tx_batch.enter_call()
        tx_batch[b'key0'] = TransactionBatchValue(None, True)
        tx_batch[b'key1'] = TransactionBatchValue(b'key1', True)
        tx_batch[b'key2'] = TransactionBatchValue(b'value2', True)
        self.assertEqual(5, len(tx_batch))
        self.assertEqual(init_call_count + 2, tx_batch.call_count)

        tx_batch.leave_call()
        self.assertEqual(4, len(tx_batch))
        self.assertEqual(TransactionBatchValue(b'key1', True),
                         tx_batch[b'key1'])
        self.assertEqual(init_call_count + 1, tx_batch.call_count)

        tx_batch.leave_call()
        self.assertEqual(3, len(tx_batch))
        self.assertEqual(TransactionBatchValue(None, True), tx_batch[b'key0'])
        self.assertEqual(TransactionBatchValue(b'key1', True),
                         tx_batch[b'key1'])
        self.assertEqual(TransactionBatchValue(b'value2', True),
                         tx_batch[b'key2'])
        self.assertEqual(init_call_count, tx_batch.call_count)
Beispiel #8
0
    def test_write_batch(self):
        data = {
            b'key0': TransactionBatchValue(b'value0', True),
            b'key1': TransactionBatchValue(b'value1', True)
        }
        db = self.db

        db.write_batch(StateWAL(data))

        self.assertEqual(b'value1', db.get(b'key1'))
        self.assertEqual(b'value0', db.get(b'key0'))
    def test_iter(self):
        tx_batch = TransactionBatch()
        tx_batch[b'key0'] = TransactionBatchValue(b'value0', True)

        tx_batch.enter_call()
        tx_batch[b'key1'] = TransactionBatchValue(b'value1', True)

        keys = []
        for key in tx_batch:
            keys.append(key)

        self.assertEqual(b'key0', keys[0])
        self.assertEqual(b'key1', keys[1])
Beispiel #10
0
    def test_put_and_delete_of_meta_context_db(self):
        context = self.context
        context_db = self.context_db
        meta_context_db = self.meta_context_db

        context_db.put(context, b'c_key', b'value0')
        meta_context_db.put(context, b'm_key', b'value0')
        self.assertEqual(TransactionBatchValue(b'value0', True), context.tx_batch[b'c_key'])
        self.assertEqual(TransactionBatchValue(b'value0', False), context.tx_batch[b'm_key'])

        context_db.delete(context, b'c_key')
        meta_context_db.delete(context, b'm_key')
        self.assertEqual(TransactionBatchValue(None, True), context.tx_batch[b'c_key'])
        self.assertEqual(TransactionBatchValue(None, False), context.tx_batch[b'm_key'])
Beispiel #11
0
    def test_get_item(self):
        byteorder = 'big'

        key = create_hash_256()
        tx_batch = TransactionBatch(create_hash_256())
        tx_batch[key] = TransactionBatchValue(b'value', True)
        self.block_batch.update(tx_batch)
        self.assertEqual(BlockBatchValue(b'value', True, [-1]),
                         self.block_batch[key])

        value = 100
        tx_batch[key] = TransactionBatchValue(value.to_bytes(8, byteorder),
                                              True)
        self.block_batch.update(tx_batch)
        self.assertEqual(
            value, int.from_bytes(self.block_batch[key].value, byteorder))
Beispiel #12
0
    def test_delete_on_readonly_exception(self):
        context = self.context
        db = self.context_db
        tx_batch = context.tx_batch

        db._put(context, b'key0', b'value0', True)
        self.assertEqual(b'value0', db.get(context, b'key0'))
        self.assertEqual(TransactionBatchValue(b'value0', True), tx_batch[b'key0'])

        context.func_type = IconScoreFuncType.READONLY
        with self.assertRaises(DatabaseException):
            db._delete(context, b'key0', True)

        context.func_type = IconScoreFuncType.WRITABLE
        db._delete(context, b'key0', True)
        self.assertIsNone(db.get(context, b'key0'))
        self.assertEqual(TransactionBatchValue(None, True), tx_batch[b'key0'])
Beispiel #13
0
    def test_digest_with_excluded_data(self):
        block_batch = self.block_batch

        tx_batch = TransactionBatch(create_hash_256())
        include_key1 = create_hash_256()
        tx_batch[include_key1] = TransactionBatchValue(b'value0', True)
        include_key2 = create_hash_256()
        tx_batch[include_key2] = TransactionBatchValue(b'value1', True)
        include_key3 = create_hash_256()
        tx_batch[include_key3] = TransactionBatchValue(b'', True)
        include_key4 = create_hash_256()
        tx_batch[include_key4] = TransactionBatchValue(None, True)

        exclude_key1 = create_hash_256()
        tx_batch[exclude_key1] = TransactionBatchValue(b'value2', False)
        exclude_key2 = create_hash_256()
        tx_batch[exclude_key2] = TransactionBatchValue(b'value3', False)

        block_batch.update(tx_batch)
        data = [
            include_key1, b'value0', include_key2, b'value1', include_key3,
            b'', include_key4
        ]
        expected = sha3_256(b'|'.join(data))
        ret = block_batch.digest()
        self.assertEqual(expected, ret)
    def test_iterable(self):
        tx_batch = TransactionBatch()
        init_call_count = tx_batch.call_count
        self.assertEqual(0, len(tx_batch))
        self.assertEqual(1, init_call_count)

        tx_batch[b'key0'] = TransactionBatchValue(b'value0', True)
        self.assertEqual(1, len(tx_batch))

        block_batch = BlockBatch()
        block_batch.update(tx_batch)
        self.assertEqual(BlockBatchValue(b'value0', True, [-1]),
                         block_batch[b'key0'])
Beispiel #15
0
    def test_len(self):
        key = create_hash_256()

        tx_batch = TransactionBatch(create_hash_256())
        tx_batch[key] = TransactionBatchValue(b'value0', True)
        self.block_batch.update(tx_batch)
        self.assertEqual(1, len(self.block_batch))

        tx_batch[key] = TransactionBatchValue(b'value1', True)
        self.block_batch.update(tx_batch)
        self.assertEqual(1, len(self.block_batch))

        key1 = create_hash_256()
        tx_batch[key1] = TransactionBatchValue(b'value0', True)
        self.block_batch.update(tx_batch)
        self.assertEqual(2, len(self.block_batch))

        del self.block_batch[key]
        self.assertEqual(1, len(self.block_batch))

        del self.block_batch[key1]
        self.assertEqual(0, len(self.block_batch))
def block_batch():
    block = Block(block_height=1,
                  block_hash=create_block_hash(),
                  timestamp=create_timestamp(),
                  prev_hash=create_block_hash(),
                  cumulative_fee=5)
    block_batch: 'BlockBatch' = BlockBatch(block)
    block_batch.block = block
    tx_batch: 'TransactionBatch' = TransactionBatch()

    for i, data in enumerate(DATA_LIST):
        tx_batch[create_hash_256()] = TransactionBatchValue(data, True, i)
    block_batch.update(tx_batch)

    return block_batch
    def test_enter_call(self):
        tx_batch = TransactionBatch()
        call_count: int = tx_batch.call_count
        self.assertEqual(1, call_count)

        tx_batch.enter_call()
        self.assertEqual(call_count + 1, tx_batch.call_count)

        tx_batch[b'key'] = TransactionBatchValue(b'value', True)
        self.assertEqual(TransactionBatchValue(b'value', True),
                         tx_batch[b'key'])

        tx_batch.leave_call()
        self.assertEqual(call_count, tx_batch.call_count)
        self.assertEqual(TransactionBatchValue(b'value', True),
                         tx_batch[b'key'])

        tx_batch.enter_call()
        self.assertEqual(call_count + 1, tx_batch.call_count)

        tx_batch[b'id'] = TransactionBatchValue(b'hello', True)
        self.assertEqual(TransactionBatchValue(b'hello', True),
                         tx_batch[b'id'])
        self.assertEqual(TransactionBatchValue(b'value', True),
                         tx_batch[b'key'])

        tx_batch.revert_call()
        self.assertEqual(None, tx_batch[b'id'])
        self.assertEqual(TransactionBatchValue(b'value', True),
                         tx_batch[b'key'])
        self.assertEqual(call_count + 1, tx_batch.call_count)

        tx_batch.leave_call()
        self.assertEqual(None, tx_batch[b'id'])
        self.assertEqual(TransactionBatchValue(b'value', True),
                         tx_batch[b'key'])
        self.assertEqual(call_count, tx_batch.call_count)
    def test_delitem(self):
        tx_batch = TransactionBatch()
        tx_batch[b'key0'] = TransactionBatchValue(b'value0', True)

        with self.assertRaises(DatabaseException):
            del tx_batch[b'key0']