def setUp(self): self._score_root_path = self._SCORE_ROOT_PATH sys.path.append(self._score_root_path) IconScoreContext.engine = ContextEngine( icx=None, deploy=Mock(spec=DeployEngine), fee=None, iiss=None, prep=None, issue=None ) IconScoreContext.storage = ContextStorage( icx=None, deploy=Mock(spec=DeployStorage), fee=None, iiss=None, prep=None, issue=None, rc=None, meta=None ) self._context = IconScoreContext(IconScoreContextType.DIRECT) ContextContainer._push_context(self._context)
def setUp(self): db_path = os.path.join(PROJECT_ROOT_PATH, self._TEST_DB_PATH) score_path = os.path.join(PROJECT_ROOT_PATH, self._ROOT_SCORE_PATH) self.score_path = score_path self._tx_index = 0 self.__ensure_dir(db_path) self._icx_db = ContextDatabaseFactory.create_by_name(ICON_DEX_DB_NAME) self._icx_db.address = ICX_ENGINE_ADDRESS self._icx_storage = IcxStorage(self._icx_db) self._score_deploy_engine = isde.Engine() self._deploy_storage = DeployStorage(self._icx_db) self._icon_score_mapper = IconScoreMapper() self.addr1 = create_address(AddressPrefix.EOA) self.addr2 = create_address(AddressPrefix.EOA) self.score_address = create_address(AddressPrefix.CONTRACT) self._score_deploy_engine.open() IconScoreContext.storage = ContextStorage( deploy=self._deploy_storage, fee=None, icx=None, iiss=None, prep=None, issue=None, rc=None, meta=None, ) self.make_context()
def setUp(self): address = Address.from_data(AddressPrefix.CONTRACT, os.urandom(20)) db = Mock(spec=IconScoreDatabase) db.attach_mock(address, 'address') context = IconScoreContext() traces = Mock(spec=list) step_counter = Mock(spec=IconScoreStepCounter) IconScoreContext.engine = ContextEngine(icx=Mock(IcxEngine), deploy=Mock(DeployEngine), fee=None, iiss=None, prep=None, issue=None) IconScoreContext.storage = ContextStorage(icx=Mock(IcxStorage), deploy=Mock(DeployStorage), fee=None, iiss=None, prep=None, issue=None, rc=None, meta=None) IconScoreContext.icx_engine = Mock(spec=IcxEngine) context.type = IconScoreContextType.INVOKE context.func_type = IconScoreFuncType.WRITABLE context.tx_batch = TransactionBatch() context.event_logs = [] context.traces = traces context.step_counter = step_counter context.get_owner = Mock() ContextContainer._push_context(context) self._mock_score = EventlogScore(db)
def _patch_service_engine(icon_service_engine, revision): # Mocks get_balance so, it returns always 100 icx # TODO : patch when use get_balance or transfer IconScoreContext.engine = ContextEngine( deploy=DeployEngine(), fee=FeeEngine(), icx=IcxEngine(), iiss=IISSEngine(), prep=PRepEngine(), issue=IssueEngine() ) db = icon_service_engine._icx_context_db IconScoreContext.storage = ContextStorage( deploy=DeployStorage(db), fee=FeeStorage(db), icx=IcxStorage(db), iiss=IISSStorage(db), prep=PRepStorage(db), issue=IssueStorage(db), meta=MetaDBStorage(db), rc=RewardCalcStorage() ) # Patch revision def set_revision_to_context(context): context.revision = revision icon_service_engine._set_revision_to_context = \ Mock(side_effect=set_revision_to_context) return icon_service_engine
def _patch_service_engine(icon_service_engine, revision): # Mocks get_balance so, it returns always 100 icx # TODO : patch when use get_balance or transfer IconScoreContext.engine = ContextEngine(deploy=DeployEngine(), fee=FeeEngine(), icx=IcxEngine(), iiss=IISSEngine(), prep=PRepEngine(), issue=IssueEngine(), inv=INVEngine()) db = icon_service_engine._icx_context_db IconScoreContext.storage = ContextStorage(deploy=DeployStorage(db), fee=FeeStorage(db), icx=IcxStorage(db), iiss=IISSStorage(db), prep=PRepStorage(db), issue=IssueStorage(db), meta=MetaDBStorage(db), rc=RewardCalcStorage(), inv=INVStorage(db)) IconScoreContext.engine.inv._inv_container = generate_inv_container( False, revision) return icon_service_engine
def storage(context): db_name = 'icx.db' db = ContextDatabase.from_path(db_name) storage = Storage(db) context.storage = ContextStorage(icx=storage) storage.open(context) yield storage storage.close(context) shutil.rmtree(db_name)
def get_context(self): context = IconScoreContext(IconScoreContextType.INVOKE) context.step_counter = Mock(spec=IconScoreStepCounter) context.step_counter.step_price = 10 ** 10 context.tx = Mock(spec=Transaction) context.tx.to = self._score_address block = Mock(spec=Block) block.attach_mock(Mock(return_value=0), 'height') context.block = block context.storage = ContextStorage(deploy=self.deploy_storage,fee=self.fee_storage, icx=self.icx_storage, iiss=None, prep=None, issue=None, rc=None, meta=None) context.engine = ContextEngine(deploy=None, fee=None, icx=self._icx_engine, iiss=None, prep=None, issue=None) return context
def setUp(self): empty_address = MalformedAddress.from_string('') short_address_without_hx = MalformedAddress.from_string('12341234') short_address = MalformedAddress.from_string('hx1234512345') long_address_without_hx = MalformedAddress.from_string( 'cf85fac2d0b507a2db9ce9526e6d01476f16a2d269f51636f9c4b2d512017faf') long_address = MalformedAddress.from_string( 'hxdf85fac2d0b507a2db9ce9526e6d01476f16a2d269f51636f9c4b2d512017faf' ) self.malformed_addresses = [ empty_address, short_address_without_hx, short_address, long_address_without_hx, long_address ] self.db_name = 'engine.db' db = ContextDatabase.from_path(self.db_name) self.engine = IcxEngine() self._from = Address.from_string('hx' + 'a' * 40) self.to = Address.from_string('hx' + 'b' * 40) self.genesis_address = Address.from_string('hx' + '0' * 40) self.fee_treasury_address = Address.from_string('hx' + '1' * 40) self.total_supply = 10**20 # 100 icx self.context = IconScoreContext(IconScoreContextType.DIRECT) block = Mock(spec=Block) block.attach_mock(Mock(return_value=0), 'height') self.context.block = block self.storage = IcxStorage(db) self.engine.open() accounts: list = [{ 'address': self.genesis_address, 'balance': self.total_supply }, { 'address': self.fee_treasury_address, 'balance': 0 }] self.context.storage = ContextStorage(deploy=None, fee=None, icx=self.storage, iiss=None, prep=None, issue=None, rc=None, meta=None) self.storage.put_genesis_accounts(self.context, accounts)
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, prep=None, issue=None, meta=None, rc=None, inv=None) ContextContainer._push_context(ctx) yield ctx ContextContainer._pop_context()
def context_with_icx_storage(context_db, genesis_address, fee_treasury_address): accounts: list = [{ 'address': genesis_address, 'balance': TOTAL_SUPPLY }, { 'address': fee_treasury_address, 'balance': 0 }] storage = IcxStorage(context_db) context = IconScoreContext(IconScoreContextType.DIRECT) block = Mock(spec=Block) block.attach_mock(Mock(return_value=0), 'height') context.block = block context.storage = ContextStorage(icx=storage) storage.put_genesis_accounts(context, accounts) yield context storage.close(context)
def context(score_db): context = IconScoreContext() context.icon_score_deploy_engine = Mock() traces = Mock(spec=list) context.tx = Mock(spec=Transaction) context.block = Mock(spec=Block) context.cumulative_step_used = Mock(spec=int) context.cumulative_step_used.attach_mock(Mock(), '__add__') context.step_counter = Mock(spec=IconScoreStepCounter) context.event_logs = [] context.traces = traces context.tx_batch = TransactionBatch() IconScoreContext.engine = ContextEngine(icx=Mock(spec=IcxEngine), deploy=Mock(spec=DeployEngine)) IconScoreContext.storage = ContextStorage(deploy=Mock(spec=DeployStorage)) context.icon_score_mapper = Mock() return context
def setUp(self): self.context = IconScoreContext(IconScoreContextType.INVOKE) self.context.engine = ContextEngine(deploy=None, fee=Mock(FeeEngine), icx=None, iiss=None, prep=None, issue=None) self.context.storage = ContextStorage(deploy=Mock(DeployStorage), fee=Mock(FeeStorage), icx=None, iiss=None, prep=None, issue=None, rc=None, meta=None) self.block_height = randrange(0, 100000) self.context.block = Block(self.block_height, os.urandom(32), 0, os.urandom(32), 0)
def setUp(self): db = Mock(spec=IconScoreDatabase) db.address = create_address(AddressPrefix.CONTRACT) context = IconScoreContext() context.icon_score_deploy_engine = Mock() traces = Mock(spec=list) context.tx = Mock(spec=Transaction) context.block = Mock(spec=Block) context.cumulative_step_used = Mock(spec=int) context.cumulative_step_used.attach_mock(Mock(), '__add__') context.step_counter = Mock(spec=IconScoreStepCounter) context.event_logs = [] context.traces = traces context.tx_batch = TransactionBatch() ContextContainer._push_context(context) InternalCall._other_score_call = Mock() IconScoreContext.engine = ContextEngine( icx=Mock(spec=IcxEngine), deploy=Mock(spec=DeployEngine), fee=None, iiss=None, prep=None, issue=None ) IconScoreContext.storage = ContextStorage( icx=None, deploy=Mock(spec=DeployStorage), fee=None, iiss=None, prep=None, issue=None, rc=None, meta=None ) context.icon_score_mapper = Mock() context.icon_score_mapper.get_icon_score = Mock(return_value=TestScore(db)) self._score = TestScore(db)
def setUp(self): self.validator = IconPreValidator() IconScoreContext.engine = ContextEngine( icx=Mock(spec=IcxEngine), deploy=Mock(spec=DeployEngine), fee=None, iiss=None, prep=None, issue=None, ) IconScoreContext.storage = ContextStorage( icx=Mock(spec=IcxStorage), deploy=Mock(spec=DeployStorage), fee=None, iiss=None, prep=None, issue=None, rc=None, meta=None) self.context = IconScoreContext()
def setUp(self): db_path: str = os.path.join(TEST_ROOT_PATH, self._TEST_DB_PATH) # score_root_path: str = os.path.join(TEST_ROOT_PATH, self._SCORE_ROOT_PATH) self._tx_index = 0 self.__ensure_dir(db_path) self._icx_db = ContextDatabaseFactory.create_by_name('icx_db') self._icx_db.address = ICX_ENGINE_ADDRESS self._icx_storage = IcxStorage(self._icx_db) self._icon_deploy_storage = DeployStorage(self._icx_db) self._icon_score_mapper = IconScoreMapper() IconScoreContextUtil.validate_score_blacklist = Mock() IconScoreContextUtil.get_owner = Mock() IconScoreContextUtil.get_icon_score = Mock() IconScoreContextUtil.is_service_flag_on = Mock() self.from_address = create_address(AddressPrefix.EOA) self.sample_token_address = create_address(AddressPrefix.CONTRACT) self.make_context() self._engine = DeployEngine() self._engine.open(self._icon_deploy_storage) IconScoreContext.storage = ContextStorage( deploy=Mock(DeployStorage), fee=None, icx=None, iiss=None, prep=None, issue=None, rc=None, meta=None ) self._one_icx = 1 * 10 ** 18 self._one_icx_to_token = 1
def setUp(self): self.db_name = 'engine.db' db = ContextDatabase.from_path(self.db_name) self.engine = IcxEngine() self.storage = IcxStorage(db) self.from_ = Address.from_string('hx' + 'a' * 40) self.to = Address.from_string('hx' + 'b' * 40) self.genesis_address = Address.from_string('hx' + '0' * 40) self.fee_treasury_address = Address.from_string('hx' + '1' * 40) self.total_supply = 10**20 # 100 icx self.fee_treasury_address_icx_amount = 0 self.context = IconScoreContext(IconScoreContextType.DIRECT) block = Mock(spec=Block) block.attach_mock(Mock(return_value=0), 'height') self.context.block = block self.engine.open() accounts: list = [{ 'address': self.genesis_address, 'balance': self.total_supply }, { 'address': self.fee_treasury_address, 'balance': 0 }] self.context.storage = ContextStorage(deploy=None, fee=None, icx=self.storage, iiss=None, prep=None, issue=None, rc=None, meta=None) self.storage.put_genesis_accounts(self.context, accounts)
def setUp(self): context = IconScoreContext(IconScoreContextType.DIRECT) block = Mock(spec=Block) block.attach_mock(Mock(return_value=0), 'height') context.block = block self._sender = Address.from_data(AddressPrefix.EOA, os.urandom(20)) self._score_address = Address.from_data(AddressPrefix.CONTRACT, os.urandom(20)) context_db = create_context_db() self.deploy_storage = DeployStorage(context_db) deploy_info = IconScoreDeployInfo(self._score_address, DeployState.ACTIVE, self._sender, os.urandom(32), os.urandom(32)) self.icx_storage = IcxStorage(context_db) self._icx_engine = IcxEngine() self.fee_storage = FeeStorage(context_db) patch_fee_storage(self.fee_storage) self.deploy_storage.put_deploy_info(context, deploy_info) context.storage = ContextStorage(deploy=self.deploy_storage, fee=self.fee_storage, icx=self.icx_storage, iiss=None, prep=None, issue=None, rc=None, meta=None) context.engine = ContextEngine(deploy=None, fee=None, icx=self._icx_engine, iiss=None, prep=None, issue=None) self._icx_engine.open(self.icx_storage) self.icx_storage._put_genesis_data_account(context, CoinPartType.GENERAL, self._sender, 100000000 * 10 ** 18) self.icx_storage._put_genesis_data_account(context, CoinPartType.TREASURY, Address.from_data(AddressPrefix.EOA, os.urandom(20)), 0) self._engine = FeeEngine()
from iconservice.icon_constant import (PREP_MAIN_PREPS, PREP_MAIN_AND_SUB_PREPS, TERM_PERIOD, IconScoreContextType, PenaltyReason, PRepStatus, TermFlag) from iconservice.iconscore.icon_score_context import IconScoreContext from iconservice.icx import IcxStorage from iconservice.prep import PRepStorage from iconservice.prep.data import PRep, Term from iconservice.utils import ContextStorage context = IconScoreContext(IconScoreContextType.DIRECT) context.storage = ContextStorage(deploy=None, fee=None, icx=Mock(spec=IcxStorage), iiss=None, issue=None, rc=None, prep=Mock(spec=PRepStorage), meta=None, inv=None) context.storage.prep.put_term = Mock() def _check_prep_snapshots_in_term(term: 'Term'): assert len(term) == len(term.main_preps) + len(term.sub_preps) for snapshot in term.preps: assert snapshot.address in term class TestTerm(unittest.TestCase):
def context(): IconScoreContext.storage = ContextStorage(deploy=Mock(spec=INVStorage)) context = Mock(spec=IconScoreContext) return context