Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def context():
ctx = IconScoreContext(IconScoreContextType.DIRECT)
ctx.tx = Transaction(tx_hash=create_tx_hash(), origin=EOA1)
ctx.block = Block(
block_height=0, block_hash=create_block_hash(), timestamp=0, prev_hash=None
)
ctx.msg = Message(sender=EOA1, value=0)
ctx.icon_score_mapper = IconScoreMapper()
ctx.new_icon_score_mapper = {}
ctx.step_counter = IconScoreStepCounter(1, {}, 1000, False)
ctx.event_logs = []
ctx.traces = []
ctx.current_address = EOA1
IconScoreContext.storage = ContextStorage(
deploy=Mock(spec=Storage),
fee=None,
icx=None,
iiss=None,
def test_scenario5(self):
# 1 block
# PRepA a ---- a
# PRepB b ---- b
# penalty PRepB (low productivity)
# after 1 block
# PRepA a ---- b
self.set_revision(Revision.DIVIDE_NODE_ADDRESS.value)
self.distribute_icx(accounts=self._accounts[:PREP_MAIN_PREPS],
init_balance=1 * ICX_IN_LOOP)
PREV_PENALTY_GRACE_PERIOD = IconScoreContext.engine.prep._penalty_imposer._penalty_grace_period
PREV_LOW_PRODUCTIVITY_PENALTY_THRESHOLD = \
IconScoreContext.engine.prep._penalty_imposer._low_productivity_penalty_threshold
PENALTY_GRACE_PERIOD = 0
# enable low productivity
LOW_PRODUCTIVITY_PENALTY_THRESHOLD = 100
IconScoreContext.engine.prep._penalty_imposer._penalty_grace_period = PENALTY_GRACE_PERIOD
IconScoreContext.engine.prep._penalty_imposer._low_productivity_penalty_threshold = \
LOW_PRODUCTIVITY_PENALTY_THRESHOLD
votes = [[self._accounts[1].address, False]] + \
[[account.address, True] for account in self._accounts[2:PREP_MAIN_PREPS]]
tx_results = self.make_blocks(to=self._block_height + 2,
prev_block_generator=self._accounts[0].address,
prev_block_votes=votes)
def _make_issue_info(self) -> tuple:
context = IconScoreContext(IconScoreContextType.DIRECT)
context._preps = context.engine.prep.preps.copy(mutable=True)
context._term = context.engine.prep.term.copy()
block_height: int = self._block_height
block_hash = create_block_hash()
timestamp_us = create_timestamp()
block = Block(block_height, block_hash, timestamp_us, self._prev_block_hash, 0)
context.block = block
issue_data = IconScoreContext.engine.issue.create_icx_issue_info(context)
total_issue_amount = 0
for group_dict in issue_data.values():
if "value" in group_dict:
total_issue_amount += group_dict["value"]
return issue_data, total_issue_amount
def make_and_req_block(self,
tx_list: list,
block_height: int = None,
prev_block_generator: Optional['Address'] = None,
prev_block_validators: Optional[List['Address']] = None,
prev_block_votes: Optional[List[Tuple['Address', int]]] = None,
block_hash: bytes = None) \
-> Tuple['Block', List[bytes]]:
if block_height is None:
block_height: int = self._block_height + 1
if block_hash is None:
block_hash = create_block_hash()
timestamp_us = create_timestamp()
block = Block(block_height, block_hash, timestamp_us, self._prev_block_hash, 0)
context = IconScoreContext(IconScoreContextType.DIRECT)
context._inv_container = context.engine.inv.inv_container
context._term = context.engine.prep.term
is_block_editable = False
if context.is_decentralized():
is_block_editable = True
tx_results, state_root_hash, added_transactions, next_preps = \
self.icon_service_engine.invoke(block=block,
tx_requests=tx_list,
prev_block_generator=prev_block_generator,
prev_block_validators=prev_block_validators,
prev_block_votes=prev_block_votes,
is_block_editable=is_block_editable)
self.add_tx_result(tx_results)
return block, self.get_hash_list_from_tx_list(tx_list)
def make_and_req_block_for_2_depth_invocation(
self,
tx_list: list,
prev_block: 'Block',
prev_block_generator: Optional['Address'] = None,
prev_block_validators: Optional[List['Address']] = None,
prev_block_votes: Optional[List[Tuple['Address', int]]] = None,
) -> Tuple['Block', List[bytes]]:
block_height: int = prev_block.height + 1
block_hash = create_block_hash()
timestamp_us = create_timestamp()
block = Block(block_height, block_hash, timestamp_us, prev_block.hash, 0)
context = IconScoreContext(IconScoreContextType.DIRECT)
is_block_editable = False
if context.is_decentralized():
is_block_editable = True
tx_results, state_root_hash, added_transactions, next_preps = \
self.icon_service_engine.invoke(block=block,
tx_requests=tx_list,
prev_block_generator=prev_block_generator,
prev_block_validators=prev_block_validators,
prev_block_votes=prev_block_votes,
is_block_editable=is_block_editable)
self.add_tx_result(tx_results)
return block, self.get_hash_list_from_tx_list(tx_list)
def _create_base_transaction(self):
context = IconScoreContext(IconScoreContextType.DIRECT)
context.preps: 'PRepContainer' = context.engine.prep.preps.copy(PRepFlag.NONE)
block_height: int = self._block_height
block_hash = create_block_hash()
timestamp_us = create_timestamp()
block = Block(block_height, block_hash, timestamp_us, self._prev_block_hash, 0)
context.block = block
issue_data, _ = context.engine.issue.create_icx_issue_info(context)
transaction, regulator = BaseTransactionCreator.create_base_transaction(context)
return transaction
icon_deploy_storage=self._deploy_storage)
self.score_engine = IconScoreEngine()
self.score_engine.open(
self._icx_storage, self._icon_score_mapper)
self._addr1 = create_address(AddressPrefix.EOA)
self._addr2 = create_address(AddressPrefix.EOA)
self._addr3 = create_address(AddressPrefix.EOA)
self._addr_token_score = create_address(AddressPrefix.CONTRACT)
self._addr_crowd_sale_score = create_address(AddressPrefix.CONTRACT)
self._factory = IconScoreContextFactory(max_size=1)
IconScoreContext.icon_score_manager = self._icon_score_manager
IconScoreContext.icon_score_mapper = self._icon_score_mapper
self.make_context()
self._total_supply = 1000 * 10 ** 18
self._one_icx = 1 * 10 ** 18
self._one_icx_to_token = 1
def test_set_unstakes_info(self):
context: 'IconScoreContext' = Mock(spec=IconScoreContext)
unstake_slot_max = 10
context.configure_mock(unstake_slot_max=unstake_slot_max)
context.configure_mock(revision=Revision.MULTIPLE_UNSTAKE.value)
stake = 500
unstake = 0
unstake_block_height = 0
stake_part: 'StakePart' = StakePart()
stake_part.normalize(0, Revision.MULTIPLE_UNSTAKE.value)
stake_part.add_stake(stake)
self.assertEqual(stake, stake_part.stake)
self.assertEqual(unstake, stake_part.unstake)
self.assertEqual(unstake_block_height, stake_part.unstake_block_height)
irep,
penalty_grace_period,
low_productivity_penalty_threshold,
block_validation_penalty_threshold)
IconScoreContext.engine.issue.open(context)
IconScoreContext.storage.deploy.open(context)
IconScoreContext.storage.fee.open(context)
IconScoreContext.storage.icx.open(context)
IconScoreContext.storage.iiss.open(context,
iiss_meta_data,
calc_period)
IconScoreContext.storage.prep.open(context,
prep_reg_fee)
IconScoreContext.storage.issue.open(context)
IconScoreContext.storage.meta.open(context)
IconScoreContext.storage.rc.open(context, rc_data_path)