Exemple #1
0
    def test_tx_params_from_bytes_to_bytes(self):
        tx_hash1 = create_tx_hash()
        score_address = create_address(1)
        deploy_state = DeployType.INSTALL
        data_params = {
            "contentType": "application/zip",
            "content":
            "0x1867291283973610982301923812873419826abcdef91827319263187263a7326e",
            "params": {
                "name": "ABCToken",
                "symbol": "abc",
                "decimals": "0x12"
            }
        }

        tx_params1 = IconScoreDeployTXParams(tx_hash1, deploy_state,
                                             score_address, data_params)

        data = IconScoreDeployTXParams.to_bytes(tx_params1)
        self.assertTrue(isinstance(data, bytes))

        tx_params2 = IconScoreDeployTXParams.from_bytes(data)
        self.assertEqual(tx_params2.tx_hash, tx_hash1)
        self.assertEqual(tx_params2.deploy_type, deploy_state)
        self.assertEqual(tx_params2._score_address, score_address)
        self.assertEqual(tx_params2.deploy_data, data_params)
    def test_put_deploy_tx_params(self):
        context = Mock(spec=IconScoreContext)
        tx_hash = create_tx_hash()
        tx_params = IconScoreDeployTXParams(tx_hash, DeployType.INSTALL, create_address(1), {})
        self.storage._create_db_key = Mock(return_value=tx_hash)
        self.storage._db.put = Mock()

        self.storage._put_deploy_tx_params(context, tx_params)
        self.storage._db.put.assert_called_once_with(context, tx_params.tx_hash, tx_params.to_bytes())
    def test_get_deploy_tx_params(self):
        context = Mock(spec=IconScoreContext)

        tx_hash = create_tx_hash()
        self.storage._create_db_key = Mock(return_value=tx_hash)
        self.storage._db.get = Mock(return_value=None)
        self.assertEqual(None, self.storage.get_deploy_tx_params(context, tx_hash))

        tx_hash = create_tx_hash()
        tx_params = IconScoreDeployTXParams(tx_hash, DeployType.INSTALL, create_address(1), {})
        self.storage._create_db_key = Mock(return_value=tx_hash)
        self.storage._db.get = Mock(return_value=tx_params.to_bytes())

        self.assertEqual(tx_params.to_bytes(), self.storage.get_deploy_tx_params(context, tx_hash).to_bytes())
Exemple #4
0
    def test_put_deploy_info_and_tx_params(self):
        self.storage._put_deploy_tx_params = Mock()
        self.storage.get_deploy_tx_params = Mock(return_value=bytes())
        context = Mock(spec=IconScoreContext)
        score_address = create_address(1)
        deploy_type = DeployType.INSTALL
        owner = create_address()
        tx_hash = create_tx_hash()
        deploy_data = {}
        with self.assertRaises(ServerErrorException) as e:
            self.storage.put_deploy_info_and_tx_params(context, score_address,
                                                       deploy_type, owner,
                                                       tx_hash, deploy_data)
        self.assertEqual(e.exception.code, ExceptionCode.SERVER_ERROR)
        self.assertEqual(e.exception.message,
                         f'deploy_params already exists: {tx_hash}')
        self.storage._put_deploy_tx_params.assert_not_called()

        with patch(
                'iconservice.deploy.icon_score_deploy_storage.IconScoreDeployTXParams'
        ) as MockTxParams:
            with patch(
                    'iconservice.deploy.icon_score_deploy_storage.IconScoreDeployInfo'
            ) as MockDeployInfos:
                self.storage._put_deploy_tx_params.reset_mock()
                self.storage.get_deploy_tx_params.reset_mock()
                self.storage.get_deploy_tx_params.return_value = None
                self.storage.get_deploy_info = Mock(return_value=None)
                self.storage._put_deploy_info = Mock()
                context = Mock(spec=IconScoreContext)
                score_address = create_address(1)
                deploy_type = DeployType.INSTALL
                owner = create_address()
                tx_hash = create_tx_hash()
                deploy_data = {}
                tx_params = IconScoreDeployTXParams(tx_hash, deploy_type,
                                                    score_address, deploy_data)
                MockTxParams.return_value = tx_params
                deploy_info = IconScoreDeployInfo(score_address,
                                                  DeployState.INACTIVE, owner,
                                                  None, tx_hash)
                MockDeployInfos.return_value = deploy_info

                self.storage.put_deploy_info_and_tx_params(
                    context, score_address, deploy_type, owner, tx_hash,
                    deploy_data)
                self.storage.get_deploy_tx_params.assert_called_once_with(
                    context, tx_hash)
                self.storage._put_deploy_tx_params.assert_called_once_with(
                    context, tx_params)
                self.storage.get_deploy_info.assert_called_once_with(
                    context, score_address)
                self.storage._put_deploy_info.assert_called_once_with(
                    context, deploy_info)

        with patch(
                'iconservice.deploy.icon_score_deploy_storage.IconScoreDeployTXParams'
        ) as MockTxParams:
            self.storage._put_deploy_tx_params.reset_mock()
            self.storage.get_deploy_tx_params.reset_mock()
            self.storage.get_deploy_tx_params.return_value = None
            self.storage._put_deploy_info = Mock()
            context = Mock(spec=IconScoreContext)
            score_address = create_address(1)
            deploy_type = DeployType.INSTALL
            owner = create_address()
            tx_hash = create_tx_hash()
            deploy_data = {}
            deploy_info = IconScoreDeployInfo(score_address,
                                              DeployState.INACTIVE, owner,
                                              None, tx_hash)
            tx_params = IconScoreDeployTXParams(tx_hash, deploy_type,
                                                score_address, deploy_data)
            self.storage.get_deploy_info = Mock(return_value=deploy_info)
            MockTxParams.return_value = tx_params

            other_owner = create_address()

            with self.assertRaises(ServerErrorException) as e:
                self.storage.put_deploy_info_and_tx_params(
                    context, score_address, deploy_type, other_owner, tx_hash,
                    deploy_data)
            self.assertEqual(e.exception.code, ExceptionCode.SERVER_ERROR)
            self.assertEqual(
                e.exception.message,
                f'invalid owner: {deploy_info.owner} != {other_owner}')

            self.storage.get_deploy_tx_params.assert_called_once_with(
                context, tx_hash)
            self.storage._put_deploy_tx_params.assert_called_once_with(
                context, tx_params)
            self.storage.get_deploy_info.assert_called_once_with(
                context, score_address)

        with patch(
                'iconservice.deploy.icon_score_deploy_storage.IconScoreDeployTXParams'
        ) as MockTxParams:
            self.storage._put_deploy_tx_params.reset_mock()
            self.storage.get_deploy_tx_params.reset_mock()
            self.storage.get_deploy_tx_params.return_value = None
            self.storage._put_deploy_info = Mock()
            self.storage._db.delete = Mock()
            context = Mock(spec=IconScoreContext)
            context.get_revision = Mock(return_value=0)
            score_address = create_address(1)
            deploy_type = DeployType.INSTALL
            owner = create_address()
            tx_hash = create_tx_hash()
            deploy_data = {}
            already_tx_hash = create_tx_hash()
            deploy_info = IconScoreDeployInfo(score_address,
                                              DeployState.INACTIVE, owner,
                                              None, already_tx_hash)
            tx_params = IconScoreDeployTXParams(tx_hash, deploy_type,
                                                score_address, deploy_data)
            self.storage.get_deploy_info = Mock(
                return_value=deepcopy(deploy_info))
            self.storage._create_db_key = Mock(
                return_value=deploy_info.next_tx_hash)
            MockTxParams.return_value = tx_params

            self.storage.put_deploy_info_and_tx_params(context, score_address,
                                                       deploy_type, owner,
                                                       tx_hash, deploy_data)
            self.storage.get_deploy_tx_params.assert_called_once_with(
                context, tx_hash)
            self.storage._put_deploy_tx_params.assert_called_once_with(
                context, tx_params)
            self.storage.get_deploy_info.assert_called_once_with(
                context, score_address)
            self.storage._db.delete.assert_called_once_with(
                context, deploy_info.next_tx_hash)

            self.storage._put_deploy_info.assert_called_once()
            ret_context, ret_deployinfo = self.storage._put_deploy_info.call_args[
                0]
            self.assertEqual(ret_context, context)
            self.assertEqual(ret_deployinfo.next_tx_hash, tx_hash)