2021-02-01 18:12:51 +01:00
|
|
|
# standard imports
|
|
|
|
import datetime
|
|
|
|
import logging
|
|
|
|
|
|
|
|
# third-party imports
|
|
|
|
import celery
|
2021-03-29 15:27:53 +02:00
|
|
|
from chainlib.eth.constant import ZERO_ADDRESS
|
|
|
|
from chainlib.chain import ChainSpec
|
2021-02-01 18:12:51 +01:00
|
|
|
|
|
|
|
# local imports
|
|
|
|
from cic_eth.db.enum import LockEnum
|
2021-03-01 21:15:17 +01:00
|
|
|
from cic_eth.db.models.base import SessionBase
|
2021-02-01 18:12:51 +01:00
|
|
|
from cic_eth.db.models.lock import Lock
|
2021-03-07 19:01:44 +01:00
|
|
|
from cic_eth.task import (
|
|
|
|
CriticalSQLAlchemyTask,
|
|
|
|
)
|
2021-02-01 18:12:51 +01:00
|
|
|
from cic_eth.error import LockedError
|
|
|
|
|
|
|
|
celery_app = celery.current_app
|
|
|
|
logg = logging.getLogger()
|
|
|
|
|
2021-03-07 19:01:44 +01:00
|
|
|
@celery_app.task(base=CriticalSQLAlchemyTask)
|
2021-03-29 15:27:53 +02:00
|
|
|
def lock(chained_input, chain_spec_dict, address=ZERO_ADDRESS, flags=LockEnum.ALL, tx_hash=None):
|
2021-02-01 18:12:51 +01:00
|
|
|
"""Task wrapper to set arbitrary locks
|
|
|
|
|
|
|
|
:param chain_str: Chain spec string representation
|
|
|
|
:type chain_str: str
|
|
|
|
:param flags: Flags to set
|
|
|
|
:type flags: number
|
|
|
|
:param address: Ethereum address
|
|
|
|
:type address: str, 0x-hex
|
|
|
|
:returns: New lock state for address
|
|
|
|
:rtype: number
|
|
|
|
"""
|
2021-04-23 23:02:51 +02:00
|
|
|
chain_str = '::'
|
|
|
|
if chain_spec_dict != None:
|
|
|
|
chain_str = str(ChainSpec.from_dict(chain_spec_dict))
|
2021-02-01 18:12:51 +01:00
|
|
|
r = Lock.set(chain_str, flags, address=address, tx_hash=tx_hash)
|
|
|
|
logg.debug('Locked {} for {}, flag now {}'.format(flags, address, r))
|
|
|
|
return chained_input
|
|
|
|
|
|
|
|
|
2021-03-07 19:01:44 +01:00
|
|
|
@celery_app.task(base=CriticalSQLAlchemyTask)
|
2021-03-29 15:27:53 +02:00
|
|
|
def unlock(chained_input, chain_spec_dict, address=ZERO_ADDRESS, flags=LockEnum.ALL):
|
2021-02-01 18:12:51 +01:00
|
|
|
"""Task wrapper to reset arbitrary locks
|
|
|
|
|
|
|
|
:param chain_str: Chain spec string representation
|
|
|
|
:type chain_str: str
|
|
|
|
:param flags: Flags to set
|
|
|
|
:type flags: number
|
|
|
|
:param address: Ethereum address
|
|
|
|
:type address: str, 0x-hex
|
|
|
|
:returns: New lock state for address
|
|
|
|
:rtype: number
|
|
|
|
"""
|
2021-04-23 23:02:51 +02:00
|
|
|
chain_str = '::'
|
|
|
|
if chain_spec_dict != None:
|
|
|
|
chain_str = str(ChainSpec.from_dict(chain_spec_dict))
|
2021-02-01 18:12:51 +01:00
|
|
|
r = Lock.reset(chain_str, flags, address=address)
|
|
|
|
logg.debug('Unlocked {} for {}, flag now {}'.format(flags, address, r))
|
|
|
|
return chained_input
|
|
|
|
|
|
|
|
|
2021-03-07 19:01:44 +01:00
|
|
|
@celery_app.task(base=CriticalSQLAlchemyTask)
|
2021-03-29 15:27:53 +02:00
|
|
|
def lock_send(chained_input, chain_spec_dict, address=ZERO_ADDRESS, tx_hash=None):
|
2021-02-01 18:12:51 +01:00
|
|
|
"""Task wrapper to set send lock
|
|
|
|
|
|
|
|
:param chain_str: Chain spec string representation
|
|
|
|
:type chain_str: str
|
|
|
|
:param address: Ethereum address
|
|
|
|
:type address: str, 0x-hex
|
|
|
|
:returns: New lock state for address
|
|
|
|
:rtype: number
|
|
|
|
"""
|
2021-03-29 15:27:53 +02:00
|
|
|
chain_str = str(ChainSpec.from_dict(chain_spec_dict))
|
2021-02-01 18:12:51 +01:00
|
|
|
r = Lock.set(chain_str, LockEnum.SEND, address=address, tx_hash=tx_hash)
|
|
|
|
logg.debug('Send locked for {}, flag now {}'.format(address, r))
|
|
|
|
return chained_input
|
|
|
|
|
|
|
|
|
2021-03-07 19:01:44 +01:00
|
|
|
@celery_app.task(base=CriticalSQLAlchemyTask)
|
2021-03-29 15:27:53 +02:00
|
|
|
def unlock_send(chained_input, chain_spec_dict, address=ZERO_ADDRESS):
|
2021-02-01 18:12:51 +01:00
|
|
|
"""Task wrapper to reset send lock
|
|
|
|
|
|
|
|
:param chain_str: Chain spec string representation
|
|
|
|
:type chain_str: str
|
|
|
|
:param address: Ethereum address
|
|
|
|
:type address: str, 0x-hex
|
|
|
|
:returns: New lock state for address
|
|
|
|
:rtype: number
|
|
|
|
"""
|
2021-03-29 15:27:53 +02:00
|
|
|
chain_str = str(ChainSpec.from_dict(chain_spec_dict))
|
2021-02-01 18:12:51 +01:00
|
|
|
r = Lock.reset(chain_str, LockEnum.SEND, address=address)
|
|
|
|
logg.debug('Send unlocked for {}, flag now {}'.format(address, r))
|
|
|
|
return chained_input
|
|
|
|
|
|
|
|
|
2021-03-07 19:01:44 +01:00
|
|
|
@celery_app.task(base=CriticalSQLAlchemyTask)
|
2021-03-29 15:27:53 +02:00
|
|
|
def lock_queue(chained_input, chain_spec_dict, address=ZERO_ADDRESS, tx_hash=None):
|
2021-02-01 18:12:51 +01:00
|
|
|
"""Task wrapper to set queue direct lock
|
|
|
|
|
|
|
|
:param chain_str: Chain spec string representation
|
|
|
|
:type chain_str: str
|
|
|
|
:param address: Ethereum address
|
|
|
|
:type address: str, 0x-hex
|
|
|
|
:returns: New lock state for address
|
|
|
|
:rtype: number
|
|
|
|
"""
|
2021-03-29 15:27:53 +02:00
|
|
|
chain_str = str(ChainSpec.from_dict(chain_spec_dict))
|
2021-02-01 18:12:51 +01:00
|
|
|
r = Lock.set(chain_str, LockEnum.QUEUE, address=address, tx_hash=tx_hash)
|
|
|
|
logg.debug('Queue direct locked for {}, flag now {}'.format(address, r))
|
|
|
|
return chained_input
|
|
|
|
|
|
|
|
|
2021-03-07 19:01:44 +01:00
|
|
|
@celery_app.task(base=CriticalSQLAlchemyTask)
|
2021-03-29 15:27:53 +02:00
|
|
|
def unlock_queue(chained_input, chain_spec_dict, address=ZERO_ADDRESS):
|
2021-02-01 18:12:51 +01:00
|
|
|
"""Task wrapper to reset queue direct lock
|
|
|
|
|
|
|
|
:param chain_str: Chain spec string representation
|
|
|
|
:type chain_str: str
|
|
|
|
:param address: Ethereum address
|
|
|
|
:type address: str, 0x-hex
|
|
|
|
:returns: New lock state for address
|
|
|
|
:rtype: number
|
|
|
|
"""
|
2021-03-29 15:27:53 +02:00
|
|
|
chain_str = str(ChainSpec.from_dict(chain_spec_dict))
|
2021-02-01 18:12:51 +01:00
|
|
|
r = Lock.reset(chain_str, LockEnum.QUEUE, address=address)
|
|
|
|
logg.debug('Queue direct unlocked for {}, flag now {}'.format(address, r))
|
|
|
|
return chained_input
|
|
|
|
|
|
|
|
|
2021-03-07 19:01:44 +01:00
|
|
|
@celery_app.task(base=CriticalSQLAlchemyTask)
|
2021-03-29 15:27:53 +02:00
|
|
|
def check_lock(chained_input, chain_spec_dict, lock_flags, address=None):
|
2021-04-23 23:02:51 +02:00
|
|
|
chain_str = '::'
|
|
|
|
if chain_spec_dict != None:
|
|
|
|
chain_str = str(ChainSpec.from_dict(chain_spec_dict))
|
2021-03-01 21:15:17 +01:00
|
|
|
session = SessionBase.create_session()
|
2021-03-29 15:27:53 +02:00
|
|
|
r = Lock.check(chain_str, lock_flags, address=ZERO_ADDRESS, session=session)
|
2021-02-01 18:12:51 +01:00
|
|
|
if address != None:
|
2021-03-01 21:15:17 +01:00
|
|
|
r |= Lock.check(chain_str, lock_flags, address=address, session=session)
|
2021-02-01 18:12:51 +01:00
|
|
|
if r > 0:
|
|
|
|
logg.debug('lock check {} has match {} for {}'.format(lock_flags, r, address))
|
2021-03-29 15:27:53 +02:00
|
|
|
session.close()
|
2021-02-01 18:12:51 +01:00
|
|
|
raise LockedError(r)
|
2021-03-29 15:27:53 +02:00
|
|
|
session.flush()
|
|
|
|
session.close()
|
2021-02-01 18:12:51 +01:00
|
|
|
return chained_input
|