def setUp(self): self.block_hash: bytes = create_hash_256() self.prev_block_hash: bytes = create_hash_256() block = Block( block_height=10, block_hash=self.block_hash, timestamp=0, prev_hash=self.prev_block_hash) self.block_batch = BlockBatch(block)
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'] = (b'value0', True) self.assertEqual(1, len(tx_batch)) block_batch = BlockBatch() block_batch.update(tx_batch) self.assertEqual((b'value0', True), block_batch[b'key0'])
def context(): context = IconScoreContext(IconScoreContextType.DIRECT) context.tx_batch = TransactionBatch() context.block_batch = BlockBatch() ContextContainer._push_context(context) yield context ContextContainer._pop_context()
def _create_context(context_type: IconScoreContextType) -> IconScoreContext: context = context_factory.create(context_type) if context.type == IconScoreContextType.INVOKE: context.block_batch = BlockBatch() context.tx_batch = TransactionBatch() return context
def _create_context(context_type: IconScoreContextType) -> IconScoreContext: context = IconScoreContext(context_type) if context.type == IconScoreContextType.INVOKE: context.block_batch = BlockBatch() context.tx_batch = TransactionBatch() mock_block: 'Mock' = Mock(spec=Block) mock_block.attach_mock(Mock(return_value=0), 'height') context.block = mock_block return context
def setUp(self): self.db_name = 'fee.db' self.address = create_address(AddressPrefix.EOA) db = ContextDatabase.from_path(self.db_name) self.assertIsNotNone(db) self.storage = FeeStorage(db) context = IconScoreContext(IconScoreContextType.DIRECT) context.tx_batch = TransactionBatch() context.block_batch = BlockBatch() self.context = context
def setUp(self): self.db_name = 'icx.db' self.address = create_address(AddressPrefix.EOA) db = ContextDatabase.from_path(self.db_name) self.assertIsNotNone(db) self.storage = IcxStorage(db) self.factory = IconScoreContextFactory(max_size=1) context = self.factory.create(IconScoreContextType.DIRECT) context.tx_batch = TransactionBatch() context.block_batch = BlockBatch() self.context = context
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 setUp(self): self.db_name = 'icx.db' db = ContextDatabase.from_path(self.db_name) self.assertIsNotNone(db) self.storage = IcxStorage(db) context = IconScoreContext(IconScoreContextType.DIRECT) context.tx_batch = TransactionBatch() mock_block: 'Mock' = Mock(spec=Block) mock_block.attach_mock(Mock(return_value=0), 'height') context.block = mock_block context.block_batch = BlockBatch() self.context = context
def setUp(self): state_db_root_path = 'state_db' self.state_db_root_path = state_db_root_path rmtree(state_db_root_path) os.mkdir(state_db_root_path) address = Address.from_data(AddressPrefix.CONTRACT, b'score') context = IconScoreContext(IconScoreContextType.INVOKE) context.block_batch = BlockBatch() context.tx_batch = TransactionBatch() db_path = os.path.join(state_db_root_path, 'db') context_db = ContextDatabase.from_path(db_path, True) meta_context_db = MetaContextDatabase(context_db.key_value_db) self.context_db = context_db self.meta_context_db = meta_context_db self.address = address self.context = context
def setUp(self): state_db_root_path = 'state_db' self.state_db_root_path = state_db_root_path rmtree(state_db_root_path) os.mkdir(state_db_root_path) address = Address.from_data(AddressPrefix.CONTRACT, b'score') context_factory = IconScoreContextFactory(max_size=2) context = context_factory.create(IconScoreContextType.INVOKE) context.block_batch = BlockBatch() context.tx_batch = TransactionBatch() db_path = os.path.join(state_db_root_path, 'db') context_db = ContextDatabase.from_path(db_path, True) self.context_factory = context_factory self.context_db = context_db self.address = address self.context = context
class TestBatch(unittest.TestCase): def setUp(self): self.block_hash: bytes = create_hash_256() self.prev_block_hash: bytes = create_hash_256() block = Block( block_height=10, block_hash=self.block_hash, timestamp=0, prev_hash=self.prev_block_hash) self.block_batch = BlockBatch(block) def test_property(self): self.assertEqual(10, self.block_batch.block.height) self.assertEqual(0, self.block_batch.block.timestamp) self.assertEqual(self.block_hash, self.block_batch.block.hash) self.assertEqual( self.prev_block_hash, self.block_batch.block.prev_hash) def test_len(self): key = create_hash_256() self.block_batch[key] = b'value0' self.assertEqual(1, len(self.block_batch)) self.block_batch[key] = b'value1' self.assertEqual(1, len(self.block_batch)) key1 = create_hash_256() self.block_batch[key1] = b'value1' 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 test_get_item(self): byteorder = 'big' key = create_hash_256() self.block_batch[key] = b'value' self.assertEqual(b'value', self.block_batch[key]) value = 100 self.block_batch[key] = value.to_bytes(8, byteorder) self.assertEqual( value, int.from_bytes(self.block_batch[key], byteorder)) def test_put_tx_batch(self): tx_hash = create_hash_256() tx_batch = TransactionBatch(tx_hash) key = create_hash_256() tx_batch[key] = b'value' key0 = create_hash_256() tx_batch[key0] = b'value0' key1 = create_hash_256() tx_batch[key1] = b'value1' key2 = create_hash_256() tx_batch[key2] = b'value2' self.assertEqual(4, len(tx_batch)) self.block_batch[key] = b'haha' self.assertEqual(1, len(self.block_batch)) self.block_batch.update(tx_batch) self.assertEqual(4, len(self.block_batch)) self.assertEqual(b'value0', self.block_batch[key0]) self.assertEqual(b'value1', self.block_batch[key1]) self.assertEqual(b'value2', self.block_batch[key2]) self.assertEqual(b'value', self.block_batch[key]) def test_digest(self): block_batch = self.block_batch key0 = create_hash_256() block_batch[key0] = b'value0' key1 = create_hash_256() block_batch[key1] = b'value1' key2 = create_hash_256() block_batch[key2] = b'value2' data = [key0, b'value0', key1, b'value1', key2, b'value2'] expected = sha3_256(b'|'.join(data)) ret = block_batch.digest() self.assertEqual(expected, ret) block_batch[key2] = None hash1 = block_batch.digest() block_batch[key2] = b'' hash2 = block_batch.digest() self.assertNotEqual(hash1, hash2)
class TestBatch(unittest.TestCase): def setUp(self): self.block_hash: bytes = create_hash_256() self.prev_block_hash: bytes = create_hash_256() block = Block(block_height=10, block_hash=self.block_hash, timestamp=0, prev_hash=self.prev_block_hash, cumulative_fee=0) self.block_batch = BlockBatch(block) def test_property(self): self.assertEqual(10, self.block_batch.block.height) self.assertEqual(0, self.block_batch.block.timestamp) self.assertEqual(self.block_hash, self.block_batch.block.hash) self.assertEqual(self.prev_block_hash, self.block_batch.block.prev_hash) def test_data_format(self): # Cannot set data to block batch directly key = create_hash_256() with self.assertRaises(AccessDeniedException): self.block_batch[key] = b'value0' def test_block_batch_direct_put(self): key = create_hash_256() with self.assertRaises(AccessDeniedException): self.block_batch[key] = (b'value', True) 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 test_get_item(self): byteorder = 'big' key = create_hash_256() tx_batch = TransactionBatch(create_hash_256()) tx_batch[key] = (b'value', True) self.block_batch.update(tx_batch) self.assertEqual((b'value', True), self.block_batch[key]) value = 100 tx_batch[key] = (value.to_bytes(8, byteorder), True) self.block_batch.update(tx_batch) self.assertEqual(value, int.from_bytes(self.block_batch[key][0], byteorder)) def test_put_tx_batch(self): tx_hash = create_hash_256() tx_batch = TransactionBatch(tx_hash) key = create_hash_256() tx_batch[key] = (b'value', True) key0 = create_hash_256() tx_batch[key0] = (b'value0', True) key1 = create_hash_256() tx_batch[key1] = (b'value1', True) key2 = create_hash_256() tx_batch[key2] = (b'value2', True) self.assertEqual(4, len(tx_batch)) tx_batch_2 = TransactionBatch(tx_hash) tx_batch_2[key] = (b'haha', True) self.block_batch.update(tx_batch_2) self.assertEqual(1, len(self.block_batch)) self.block_batch.update(tx_batch) self.assertEqual(4, len(self.block_batch)) self.assertEqual((b'value0', True), self.block_batch[key0]) self.assertEqual((b'value1', True), self.block_batch[key1]) self.assertEqual((b'value2', True), self.block_batch[key2]) self.assertEqual((b'value', True), self.block_batch[key]) 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) 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)
class TestBatch(unittest.TestCase): def setUp(self): self.block_hash: bytes = create_hash_256() self.prev_block_hash: bytes = create_hash_256() block = Block(block_height=10, block_hash=self.block_hash, timestamp=0, prev_hash=self.prev_block_hash, cumulative_fee=0) self.block_batch = BlockBatch(block) def test_property(self): self.assertEqual(10, self.block_batch.block.height) self.assertEqual(0, self.block_batch.block.timestamp) self.assertEqual(self.block_hash, self.block_batch.block.hash) self.assertEqual(self.prev_block_hash, self.block_batch.block.prev_hash) def test_data_format(self): # Cannot set data to block batch directly key = create_hash_256() with self.assertRaises(AccessDeniedException): self.block_batch[key] = b'value0' def test_block_batch_direct_put(self): key = create_hash_256() with self.assertRaises(AccessDeniedException): self.block_batch[key] = (b'value', True) 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 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)) 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]) 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) 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_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 context(): context = IconScoreContext(IconScoreContextType.DIRECT) context.tx_batch = TransactionBatch() context.block_batch = BlockBatch() yield context