Beispiel #1
0
    def test_int_to_bytes(self):
        n = 0x80
        for i in range(0, 32):
            # 0x80, 0x8000, 0x800000, 0x80000000, ...
            data = int_to_bytes(-n)
            self.assertEqual(0x80, data[0])

            # 0x0080, 0x008000, 0x00800000, 0x0080000000, ...
            data = int_to_bytes(n)
            first, second = data[:2]
            self.assertListEqual([0x00, 0x80], [first, second])

            n <<= 8
Beispiel #2
0
    def test_int_index_event(self):
        context = ContextContainer._get_context()

        amount = 123456789

        # Tests simple event emit
        self._mock_score.IntIndexEvent(amount)
        self.assertEqual(len(context.event_logs), 1)
        event_log = context.event_logs[0]
        self.assertEqual(2, len(event_log.indexed))
        self.assertEqual(0, len(event_log.data))

        logs_bloom = IconServiceEngine._generate_logs_bloom(context.event_logs)

        # Asserts whether the SCORE address is included in the bloom
        self.assert_score_address_in_bloom(logs_bloom)

        event_bloom_data = \
            int(0).to_bytes(1, DATA_BYTE_ORDER) + \
            'IntIndexEvent(int)'.encode('utf-8')
        self.assertIn(event_bloom_data, logs_bloom)

        indexed_bloom_data = \
            int(1).to_bytes(1, DATA_BYTE_ORDER) + int_to_bytes(amount)
        self.assertIn(indexed_bloom_data, logs_bloom)
Beispiel #3
0
    def test_array_db(self, context, key_value_db, values, value_type):
        context_db = ContextDatabase(key_value_db, is_shared=False)
        address = Address(AddressPrefix.CONTRACT, os.urandom(20))
        score_db = IconScoreDatabase(address, context_db)

        self._init_context(context, score_db.address)
        self._set_revision(context, Revision.USE_RLP.value - 1)

        name = "array_db"
        array_db = ArrayDB(name, score_db, value_type)

        for i in range(2):
            array_db.put(values[i])
        assert len(array_db) == 2

        self._set_revision(context, Revision.USE_RLP.value)

        array_db.put(values[2])
        array_db.put(values[3])
        assert len(array_db) == 4

        for i, value in enumerate(array_db):
            assert value == values[i]

        final_key: bytes = _get_final_key(address, ContainerTag.ARRAY, name.encode(), use_rlp=True)
        assert key_value_db.get(final_key) == int_to_bytes(len(array_db))

        for i, use_rlp in enumerate((False, False, True, True)):
            key = _get_final_key(
                address.to_bytes(),
                ContainerTag.ARRAY.value,
                name.encode(),
                int_to_bytes(i),
                use_rlp=use_rlp,
            )
            assert key_value_db.get(key) == ContainerUtil.encode_value(values[i])

        for v in reversed(values):
            assert v == array_db.pop()
        assert len(array_db) == 0

        # 2 values for array_db size still remain
        # even though all items in array_db have been popped.
        assert len(key_value_db) == 2
    def test_get_final_key_4(self, size):
        keys = [Key(f"prefix{i}".encode()) for i in range(size)]
        prefixes = PrefixStorage(keys)
        assert prefixes._tag is None
        assert len(prefixes) == size
        for key, expected in zip(prefixes, keys):
            assert key is expected

        name = Key(b"array", KeyType.ARRAY)
        prefixes.append(name)
        assert prefixes._tag is ContainerTag.ARRAY
        assert len(prefixes) == size + 1

        last_key = int_to_bytes(0)

        final_key: bytes = prefixes.get_final_key(last_key, use_rlp=False)
        assert final_key == _get_final_key(*keys[:size], ContainerTag.ARRAY, name, last_key, use_rlp=False)

        final_key: bytes = prefixes.get_final_key(last_key, True)
        assert final_key == _get_final_key(ContainerTag.ARRAY, *prefixes, last_key, use_rlp=True)
Beispiel #5
0
    def run(self, args):
        db_path: str = args.db
        score_address: "Address" = Address.from_string(args.score)
        address: "Address" = Address.from_string(args.user)
        balance: int = args.balance
        # Name of DictDB in Standard Token
        name: str = "balances"

        manager = ScoreDatabaseManager()
        manager.open(db_path, score_address)

        if balance < 0:
            value: bytes = manager.read_from_dict_db(name, address)
            balance: int = int.from_bytes(value, "big")
            print(f"token balance: {balance}")
        else:
            value: bytes = int_to_bytes(balance)
            manager.write_to_dict_db(name, address, value)

        manager.close()
Beispiel #6
0
def run_command_token(args):
    db_path: str = args.db
    score_address: 'Address' = Address.from_string(args.score)
    address: 'Address' = Address.from_string(args.user)
    balance: int = args.balance
    # Name of DictDB in Standard Token
    name: str = 'balances'

    manager = ScoreDatabaseManager()
    manager.open(db_path, score_address)

    if balance < 0:
        value: bytes = manager.read_from_dict_db(name, address)
        balance: int = int.from_bytes(value, 'big')
        print(f'token balance: {balance}')
    else:
        value: bytes = int_to_bytes(balance)
        manager.write_to_dict_db(name, address, value)

    manager.close()
Beispiel #7
0
    def test_bool_index_event(self):
        context = ContextContainer._get_context()

        yes_no = True

        # Tests simple event emit
        self._mock_score.BoolIndexEvent(yes_no)
        self.assertEqual(len(context.event_logs), 1)
        event_log = context.event_logs[0]
        self.assertEqual(2, len(event_log.indexed))
        self.assertEqual(0, len(event_log.data))

        logs_bloom = IconServiceEngine._generate_logs_bloom(context.event_logs)

        event_bloom_data = \
            int(0).to_bytes(1, DATA_BYTE_ORDER) + \
            'BoolIndexEvent(bool)'.encode('utf-8')
        self.assertIn(event_bloom_data, logs_bloom)

        indexed_bloom_data = \
            int(1).to_bytes(1, DATA_BYTE_ORDER) + int_to_bytes(yes_no)
        self.assertIn(indexed_bloom_data, logs_bloom)
Beispiel #8
0
    def test_iterator(self):
        version: int = 7
        msg_id: int = 1234
        block_height: int = 100
        state_hash: bytes = hashlib.sha3_256(b'').digest()
        block_hash: bytes = hashlib.sha3_256(b'block_hash').digest()
        tx_index: int = 1
        tx_hash: bytes = hashlib.sha3_256(b"tx_hash").digest()
        address = Address.from_data(AddressPrefix.EOA, b'')
        iscore: int = 5000
        success: bool = True

        status: int = 1

        messages = [
            (MessageType.VERSION, msg_id, (version, block_height)),
            (MessageType.CALCULATE, msg_id, (status, block_height)),
            (MessageType.QUERY, msg_id, (address.to_bytes_including_prefix(),
                                         int_to_bytes(iscore), block_height)),
            (MessageType.CLAIM, msg_id,
             (address.to_bytes_including_prefix(), block_height, block_hash,
              tx_index, tx_hash, int_to_bytes(iscore))),
            (MessageType.COMMIT_BLOCK, msg_id, (success, block_height,
                                                block_hash)),
            (MessageType.COMMIT_CLAIM, msg_id),
            (MessageType.READY, msg_id, (version, block_height, block_hash)),
            (MessageType.CALCULATE_DONE, msg_id,
             (success, block_height, int_to_bytes(iscore), state_hash)),
            (MessageType.INIT, msg_id, (success, block_height))
        ]

        for message in messages:
            data: bytes = msgpack.packb(message)
            self.unpacker.feed(data)

        it = iter(self.unpacker)
        version_response = next(it)
        self.assertIsInstance(version_response, VersionResponse)
        self.assertEqual(version, version_response.version)

        calculate_response = next(it)
        self.assertIsInstance(calculate_response, CalculateResponse)

        query_response = next(it)
        self.assertIsInstance(query_response, QueryResponse)
        self.assertEqual(iscore, query_response.iscore)
        self.assertEqual(block_height, query_response.block_height)

        claim_response = next(it)
        self.assertIsInstance(claim_response, ClaimResponse)
        self.assertEqual(iscore, claim_response.iscore)
        self.assertEqual(block_height, claim_response.block_height)
        self.assertEqual(tx_index, claim_response.tx_index)
        self.assertEqual(tx_hash, claim_response.tx_hash)

        commit_block_response = next(it)
        self.assertIsInstance(commit_block_response, CommitBlockResponse)
        self.assertEqual(block_height, commit_block_response.block_height)
        self.assertEqual(block_hash, commit_block_response.block_hash)

        commit_claim_response = next(it)
        self.assertIsInstance(commit_claim_response, CommitClaimResponse)

        ready_notification = next(it)
        self.assertIsInstance(ready_notification, ReadyNotification)
        self.assertEqual(version, ready_notification.version)
        self.assertEqual(block_height, ready_notification.block_height)
        self.assertEqual(block_hash, ready_notification.block_hash)

        calculate_done_notification = next(it)
        self.assertIsInstance(calculate_done_notification,
                              CalculateDoneNotification)
        self.assertTrue(calculate_done_notification.success)
        self.assertEqual(block_height,
                         calculate_done_notification.block_height)
        self.assertEqual(state_hash, calculate_done_notification.state_hash)

        init_response = next(it)
        self.assertIsInstance(init_response, InitResponse)
        self.assertEqual(success, init_response.success)
        self.assertEqual(block_height, init_response.block_height)

        with self.assertRaises(StopIteration):
            next(it)

        for message in messages:
            data: bytes = msgpack.packb(message)
            self.unpacker.feed(data)

        expected = [
            version_response, calculate_response, query_response,
            claim_response, commit_block_response, commit_claim_response,
            ready_notification, calculate_done_notification
        ]
        for expected_response, response in zip(expected, self.unpacker):
            self.assertEqual(expected_response.MSG_TYPE, response.MSG_TYPE)
            self.assertEqual(msg_id, response.msg_id)