This repository has been archived by the owner on Dec 15, 2021. It is now read-only.
/
test_integrate_existent_scores_audit.py
219 lines (191 loc) · 7.99 KB
/
test_integrate_existent_scores_audit.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
# -*- coding: utf-8 -*-
# Copyright 2018 ICON Foundation
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""IconScoreEngine testcase
"""
import os
import time
from typing import TYPE_CHECKING, List, Union, Optional
from iconcommons import IconConfig
from iconservice import SYSTEM_SCORE_ADDRESS, Address
from iconservice.base.address import generate_score_address, GOVERNANCE_SCORE_ADDRESS
from iconservice.icon_config import default_icon_config
from iconservice.icon_constant import ConfigKey, BUILTIN_SCORE_ADDRESS_MAPPER
from iconservice.icon_service_engine import IconServiceEngine
from iconservice.iconscore.icon_score_result import TransactionResult
from tests import root_clear
from tests.integrate_test.test_integrate_base import TestIntegrateBase
if TYPE_CHECKING:
pass
FILE_PATH = os.path.dirname(__file__)
PROJECT_ROOT_PATH = os.path.join(FILE_PATH, "..", "..")
BUILTIN_SCORE_SRC_ROOT_PATH = os.path.join(
PROJECT_ROOT_PATH, "iconservice", "builtin_scores"
)
GOVERNANCE_SCORE_PATH = os.path.join(BUILTIN_SCORE_SRC_ROOT_PATH, "governance")
class TestIntegrateExistentScoresAudit(TestIntegrateBase):
# override setUp method for making directory before begin tests.
def setUp(self):
root_clear(
self._score_root_path, self._state_db_root_path, self._iiss_db_root_path
)
self._block_height = -1
self._prev_block_hash = None
self.config = IconConfig("", default_icon_config)
self.config.load()
self.config.update_conf(
{ConfigKey.BUILTIN_SCORE_OWNER: str(self._admin.address)}
)
self.config.update_conf(
{
ConfigKey.SCORE_ROOT_PATH: self._score_root_path,
ConfigKey.STATE_DB_ROOT_PATH: self._state_db_root_path,
}
)
self.config.update_conf(self._make_init_config())
def _setUp_audit(self):
self.config.update_conf(
{
ConfigKey.SERVICE: {
ConfigKey.SERVICE_AUDIT: True,
ConfigKey.SERVICE_FEE: False,
ConfigKey.SERVICE_SCORE_PACKAGE_VALIDATOR: False,
}
}
)
self.icon_service_engine = IconServiceEngine()
self.icon_service_engine.open(self.config)
self._genesis_invoke()
self.token_initial_params = {"init_supply": hex(1000), "decimal": "0x12"}
def _make_directories_in_builtin_score_path(self):
for score_name, address in BUILTIN_SCORE_ADDRESS_MAPPER.items():
os.makedirs(
os.path.join(
self._score_root_path, f"01{address[2:]}", f"0x{'0' * 64}"
),
exist_ok=True,
)
def _make_directory_using_address_and_hash(
self, score_address: "Address", tx_hash: bytes
):
tx_str = f"0x{bytes.hex(tx_hash)}"
os.makedirs(
os.path.join(self._score_root_path, f"01{str(score_address)[2:]}", tx_str)
)
def _create_deploy_score_tx(
self,
score_root: str,
score_name: str,
from_: Union["EOAAccount", "Address", None],
to_: Union["EOAAccount", "Address"],
deploy_params: dict,
) -> dict:
addr_from: Optional["Address"] = self._convert_address_from_address_type(from_)
addr_to: "Address" = self._convert_address_from_address_type(to_)
timestamp = int(time.time() * 10 ** 6)
tx: dict = self.create_deploy_score_tx(
score_root=score_root,
score_name=score_name,
from_=addr_from,
to_=addr_to,
deploy_params=deploy_params,
timestamp_us=timestamp,
)
score_address: "Address" = generate_score_address(addr_from, timestamp, nonce=0)
tx_hash: bytes = tx["params"]["txHash"]
self._make_directory_using_address_and_hash(score_address, tx_hash)
return tx
def _deploy_score(
self,
score_root: str,
score_name: str,
from_: Union["EOAAccount", "Address", None],
to_: Union["EOAAccount", "Address"],
deploy_params: dict,
expected_status: bool = True,
) -> List["TransactionResult"]:
tx: dict = self._create_deploy_score_tx(
score_root=score_root,
score_name=score_name,
from_=from_,
to_=to_,
deploy_params=deploy_params,
)
tx_results: List["TransactionResult"] = self.process_confirm_block_tx(
[tx], expected_status
)
return tx_results
def test_existent_builtin_score_audit(self):
self._make_directories_in_builtin_score_path()
self._setUp_audit()
# original SCORE api
original_governance_api: dict = self.get_score_api(GOVERNANCE_SCORE_ADDRESS)
# update governance SCORE(revision2)
tx_results: List["TransactionResult"] = self.update_governance("0_0_4")
self.accept_score(tx_results[0].tx_hash)
# updated SCORE api
updated_governance_api: dict = self.get_score_api(GOVERNANCE_SCORE_ADDRESS)
self.assertNotEqual(original_governance_api, updated_governance_api)
def test_exists_score_revision3_abnormal_scores_audit(self):
self._setUp_audit()
# set revision to 3
tx_results: List["TransactionResult"] = self.update_governance("0_0_4")
self.accept_score(tx_results[0].tx_hash)
self.set_revision(3)
sample_score_params = {"value": hex(1000)}
# deploy SCORE(not python)
tx_results: List["TransactionResult"] = self._deploy_score(
score_root="sample_deploy_scores",
score_name="install/test_score_no_python",
from_=self._accounts[0],
to_=SYSTEM_SCORE_ADDRESS,
deploy_params=sample_score_params,
)
self.accept_score(tx_hash=tx_results[0].tx_hash, expected_status=False)
# deploy SCORE(has no external function)
tx_results: List["TransactionResult"] = self._deploy_score(
score_root="sample_deploy_scores",
score_name="install/test_score_no_external_func",
from_=self._accounts[0],
to_=SYSTEM_SCORE_ADDRESS,
deploy_params=sample_score_params,
)
self.accept_score(tx_hash=tx_results[0].tx_hash, expected_status=False)
# deploy SCORE(no scorebase)
tx_results: List["TransactionResult"] = self._deploy_score(
score_root="sample_deploy_scores",
score_name="install/test_score_no_scorebase",
from_=self._accounts[0],
to_=SYSTEM_SCORE_ADDRESS,
deploy_params=sample_score_params,
)
self.accept_score(tx_hash=tx_results[0].tx_hash, expected_status=False)
# deploy SCORE(on install error)
tx_results: List["TransactionResult"] = self._deploy_score(
score_root="sample_deploy_scores",
score_name="install/test_on_install_error",
from_=self._accounts[0],
to_=SYSTEM_SCORE_ADDRESS,
deploy_params=sample_score_params,
)
self.accept_score(tx_hash=tx_results[0].tx_hash, expected_status=False)
# deploy SCORE(different encoding)
tx_results: List["TransactionResult"] = self._deploy_score(
score_root="sample_deploy_scores",
score_name="install/test_score_with_korean_comment",
from_=self._accounts[0],
to_=SYSTEM_SCORE_ADDRESS,
deploy_params=sample_score_params,
)
self.accept_score(tx_hash=tx_results[0].tx_hash, expected_status=False)