From 36e7d5317383defb59c73feebe14754c3fc5120a Mon Sep 17 00:00:00 2001 From: nolash Date: Sun, 21 Feb 2021 16:29:34 +0100 Subject: [PATCH] Plug remaning postgres leaks --- apps/cic-eth/cic_eth/db/models/base.py | 1 + apps/cic-eth/cic_eth/db/models/role.py | 70 +++++++++++-------- apps/cic-eth/cic_eth/eth/gas.py | 5 +- .../cic_eth/runnable/daemons/tasker.py | 2 + apps/cic-eth/config/cic.ini | 2 +- ...b33114c4aa8a9e5a25c152ac39d9feab22b7e13e00 | 2 - 6 files changed, 50 insertions(+), 32 deletions(-) delete mode 100644 apps/contract-migration/scripts/false/_cacache/index-v5/34/8e/30c54cce758990bcd1b33114c4aa8a9e5a25c152ac39d9feab22b7e13e00 diff --git a/apps/cic-eth/cic_eth/db/models/base.py b/apps/cic-eth/cic_eth/db/models/base.py index 353c67cc..db570dfc 100644 --- a/apps/cic-eth/cic_eth/db/models/base.py +++ b/apps/cic-eth/cic_eth/db/models/base.py @@ -114,6 +114,7 @@ class SessionBase(Model): @staticmethod def release_session(session=None): + session.flush() session_key = str(id(session)) if SessionBase.localsessions.get(session_key) != None: logg.debug('destroying session {}'.format(session_key)) diff --git a/apps/cic-eth/cic_eth/db/models/role.py b/apps/cic-eth/cic_eth/db/models/role.py index e2d4824c..d632b0a9 100644 --- a/apps/cic-eth/cic_eth/db/models/role.py +++ b/apps/cic-eth/cic_eth/db/models/role.py @@ -24,9 +24,10 @@ class AccountRole(SessionBase): tag = Column(Text) address_hex = Column(String(42)) - + + # TODO: @staticmethod - def get_address(tag): + def get_address(tag, session): """Get Ethereum address matching the given tag :param tag: Tag @@ -34,14 +35,24 @@ class AccountRole(SessionBase): :returns: Ethereum address, or zero-address if tag does not exist :rtype: str, 0x-hex """ - role = AccountRole.get_role(tag) - if role == None: - return zero_address - return role.address_hex + if session == None: + raise ValueError('nested bind session calls will not succeed as the first call to release_session in the stack will leave the db object detached further down the stack. We will need additional reference count.') + + session = SessionBase.bind_session(session) + + role = AccountRole.get_role(tag, session) + + r = zero_address + if role != None: + r = role.address_hex + + SessionBase.release_session(session) + + return r @staticmethod - def get_role(tag): + def get_role(tag, session=None): """Get AccountRole model object matching the given tag :param tag: Tag @@ -49,20 +60,26 @@ class AccountRole(SessionBase): :returns: Role object, if found :rtype: cic_eth.db.models.role.AccountRole """ - session = AccountRole.create_session() - role = AccountRole.__get_role(session, tag) - session.close() - #return role.address_hex + session = SessionBase.bind_session(session) + + role = AccountRole.__get_role(tag, session) + + SessionBase.release_session(session) + return role @staticmethod - def __get_role(session, tag): - return session.query(AccountRole).filter(AccountRole.tag==tag).first() + def __get_role(tag, session): + q = session.query(AccountRole) + q = q.filter(AccountRole.tag==tag) + r = q.first() + session.flush() + return r @staticmethod - def set(tag, address_hex): + def set(tag, address_hex, session=None): """Persist a tag to Ethereum address association. This will silently overwrite the existing value. @@ -74,16 +91,16 @@ class AccountRole(SessionBase): :returns: Role object :rtype: cic_eth.db.models.role.AccountRole """ - #session = AccountRole.create_session() - #role = AccountRole.__get(session, tag) - role = AccountRole.get_role(tag) #session, tag) + session = SessionBase.bind_session(session) + + role = AccountRole.get_role(tag, session) if role == None: role = AccountRole(tag) role.address_hex = address_hex - #session.add(role) - #session.commit() - #session.close() - return role #address_hex + + SessionBase.release_session(session) + + return role @staticmethod @@ -95,20 +112,17 @@ class AccountRole(SessionBase): :returns: Role tag, or None if no match :rtype: str or None """ - localsession = session - if localsession == None: - localsession = SessionBase.create_session() + session = SessionBase.bind_session(session) - q = localsession.query(AccountRole) + q = session.query(AccountRole) q = q.filter(AccountRole.address_hex==address) role = q.first() tag = None if role != None: tag = role.tag - if session == None: - localsession.close() - + SessionBase.release_session(session) + return tag diff --git a/apps/cic-eth/cic_eth/eth/gas.py b/apps/cic-eth/cic_eth/eth/gas.py index 5bbd13d3..8cf99055 100644 --- a/apps/cic-eth/cic_eth/eth/gas.py +++ b/apps/cic-eth/cic_eth/eth/gas.py @@ -52,7 +52,10 @@ class GasOracle(): :returns: Etheerum account address :rtype: str, 0x-hex """ - return AccountRole.get_address('GAS_GIFTER') + session = SessionBase.create_session() + a = AccountRole.get_address('GAS_GIFTER', session) + session.close() + return a def gas_price(self, category='safe'): diff --git a/apps/cic-eth/cic_eth/runnable/daemons/tasker.py b/apps/cic-eth/cic_eth/runnable/daemons/tasker.py index 384e6584..9a4bb2c8 100644 --- a/apps/cic-eth/cic_eth/runnable/daemons/tasker.py +++ b/apps/cic-eth/cic_eth/runnable/daemons/tasker.py @@ -50,6 +50,7 @@ argparser = argparse.ArgumentParser() argparser.add_argument('-p', '--provider', dest='p', type=str, help='web3 provider') argparser.add_argument('-c', type=str, default=config_dir, help='config file') argparser.add_argument('-q', type=str, default='cic-eth', help='queue name for worker tasks') +argparser.add_argument('-r', type=str, help='CIC registry address') argparser.add_argument('--abi-dir', dest='abi_dir', type=str, help='Directory containing bytecode and abi') argparser.add_argument('--trace-queue-status', default=None, dest='trace_queue_status', action='store_true', help='set to perist all queue entry status changes to storage') argparser.add_argument('-i', '--chain-spec', dest='i', type=str, help='chain spec') @@ -69,6 +70,7 @@ config.process() args_override = { 'ETH_ABI_DIR': getattr(args, 'abi_dir'), 'CIC_CHAIN_SPEC': getattr(args, 'i'), + 'CIC_REGISTRY_ADDRESS': getattr(args, 'r'), 'ETH_PROVIDER': getattr(args, 'p'), 'TASKS_TRACE_QUEUE_STATUS': getattr(args, 'trace_queue_status'), } diff --git a/apps/cic-eth/config/cic.ini b/apps/cic-eth/config/cic.ini index bb167d68..73cc99c6 100644 --- a/apps/cic-eth/config/cic.ini +++ b/apps/cic-eth/config/cic.ini @@ -2,4 +2,4 @@ registry_address = chain_spec = tx_retry_delay = -trust_address = +trust_address = diff --git a/apps/contract-migration/scripts/false/_cacache/index-v5/34/8e/30c54cce758990bcd1b33114c4aa8a9e5a25c152ac39d9feab22b7e13e00 b/apps/contract-migration/scripts/false/_cacache/index-v5/34/8e/30c54cce758990bcd1b33114c4aa8a9e5a25c152ac39d9feab22b7e13e00 deleted file mode 100644 index 77bc0d2b..00000000 --- a/apps/contract-migration/scripts/false/_cacache/index-v5/34/8e/30c54cce758990bcd1b33114c4aa8a9e5a25c152ac39d9feab22b7e13e00 +++ /dev/null @@ -1,2 +0,0 @@ - -6b69c14ec8e6b622aedfcaaa86ffa959e64f832e {"key":"make-fetch-happen:request-cache:https://registry.npmjs.org/-/npm/v1/security/audits/quick","integrity":null,"time":1613156363320} \ No newline at end of file