2021-02-01 18:12:51 +01:00
|
|
|
|
# standard imports
|
|
|
|
|
import logging
|
2021-03-06 18:55:51 +01:00
|
|
|
|
import datetime
|
2021-02-01 18:12:51 +01:00
|
|
|
|
|
|
|
|
|
# third-party imports
|
2021-03-06 18:55:51 +01:00
|
|
|
|
from sqlalchemy import Column, String, Integer, DateTime
|
2021-02-01 18:12:51 +01:00
|
|
|
|
|
|
|
|
|
# local imports
|
|
|
|
|
from .base import SessionBase
|
2021-03-06 18:55:51 +01:00
|
|
|
|
from cic_eth.error import (
|
|
|
|
|
InitializationError,
|
|
|
|
|
IntegrityError,
|
|
|
|
|
)
|
2021-02-01 18:12:51 +01:00
|
|
|
|
|
|
|
|
|
logg = logging.getLogger()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class Nonce(SessionBase):
|
|
|
|
|
"""Provides thread-safe nonce increments.
|
|
|
|
|
"""
|
|
|
|
|
__tablename__ = 'nonce'
|
|
|
|
|
|
|
|
|
|
nonce = Column(Integer)
|
|
|
|
|
address_hex = Column(String(42))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
|
def get(address, session=None):
|
2021-03-01 21:15:17 +01:00
|
|
|
|
session = SessionBase.bind_session(session)
|
2021-02-01 18:12:51 +01:00
|
|
|
|
|
2021-03-01 21:15:17 +01:00
|
|
|
|
q = session.query(Nonce)
|
2021-02-01 18:12:51 +01:00
|
|
|
|
q = q.filter(Nonce.address_hex==address)
|
|
|
|
|
nonce = q.first()
|
|
|
|
|
|
|
|
|
|
nonce_value = None
|
|
|
|
|
if nonce != None:
|
|
|
|
|
nonce_value = nonce.nonce;
|
|
|
|
|
|
2021-03-01 21:15:17 +01:00
|
|
|
|
SessionBase.release_session(session)
|
2021-02-01 18:12:51 +01:00
|
|
|
|
|
|
|
|
|
return nonce_value
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@staticmethod
|
2021-03-06 18:55:51 +01:00
|
|
|
|
def __get(conn, address):
|
|
|
|
|
r = conn.execute("SELECT nonce FROM nonce WHERE address_hex = '{}'".format(address))
|
2021-02-01 18:12:51 +01:00
|
|
|
|
nonce = r.fetchone()
|
|
|
|
|
if nonce == None:
|
|
|
|
|
return None
|
|
|
|
|
return nonce[0]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@staticmethod
|
2021-03-06 18:55:51 +01:00
|
|
|
|
def __set(conn, address, nonce):
|
|
|
|
|
conn.execute("UPDATE nonce set nonce = {} WHERE address_hex = '{}'".format(nonce, address))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
|
def __init(conn, address, nonce):
|
|
|
|
|
conn.execute("INSERT INTO nonce (nonce, address_hex) VALUES ({}, '{}')".format(nonce, address))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
|
def init(address, nonce=0, session=None):
|
|
|
|
|
session = SessionBase.bind_session(session)
|
|
|
|
|
|
|
|
|
|
q = session.query(Nonce)
|
|
|
|
|
q = q.filter(Nonce.address_hex==address)
|
|
|
|
|
o = q.first()
|
|
|
|
|
if o != None:
|
|
|
|
|
session.flush()
|
|
|
|
|
raise InitializationError('nonce on {} already exists ({})'.format(address, o.nonce))
|
2021-03-01 21:15:17 +01:00
|
|
|
|
session.flush()
|
2021-03-06 18:55:51 +01:00
|
|
|
|
Nonce.__init(session, address, nonce)
|
|
|
|
|
|
|
|
|
|
SessionBase.release_session(session)
|
2021-02-01 18:12:51 +01:00
|
|
|
|
|
|
|
|
|
|
2021-03-06 18:55:51 +01:00
|
|
|
|
# TODO: Incrementing nonce MUST be done by separate tasks.
|
2021-02-01 18:12:51 +01:00
|
|
|
|
@staticmethod
|
2021-03-09 07:43:31 +01:00
|
|
|
|
def next(address, initial_if_not_exists=0, session=None):
|
2021-02-01 18:12:51 +01:00
|
|
|
|
"""Generate next nonce for the given address.
|
|
|
|
|
|
|
|
|
|
If there is no previous nonce record for the address, the nonce may be initialized to a specified value, or 0 if no value has been given.
|
|
|
|
|
|
|
|
|
|
:param address: Associate Ethereum address
|
|
|
|
|
:type address: str, 0x-hex
|
|
|
|
|
:param initial_if_not_exists: Initial nonce value to set if no record exists
|
|
|
|
|
:type initial_if_not_exists: number
|
|
|
|
|
:returns: Nonce
|
|
|
|
|
:rtype: number
|
|
|
|
|
"""
|
2021-03-09 07:43:31 +01:00
|
|
|
|
session = SessionBase.bind_session(session)
|
2021-03-01 21:15:17 +01:00
|
|
|
|
|
2021-03-11 11:40:02 +01:00
|
|
|
|
#session.begin_nested()
|
2021-03-09 07:43:31 +01:00
|
|
|
|
#conn = Nonce.engine.connect()
|
|
|
|
|
#if Nonce.transactional:
|
|
|
|
|
# conn.execute('BEGIN')
|
|
|
|
|
# conn.execute('LOCK TABLE nonce IN SHARE ROW EXCLUSIVE MODE')
|
|
|
|
|
# logg.debug('locking nonce table for address {}'.format(address))
|
|
|
|
|
#nonce = Nonce.__get(conn, address)
|
|
|
|
|
nonce = Nonce.__get(session, address)
|
2021-02-01 18:12:51 +01:00
|
|
|
|
logg.debug('get nonce {} for address {}'.format(nonce, address))
|
|
|
|
|
if nonce == None:
|
|
|
|
|
nonce = initial_if_not_exists
|
|
|
|
|
logg.debug('setting default nonce to {} for address {}'.format(nonce, address))
|
2021-03-09 07:43:31 +01:00
|
|
|
|
#Nonce.__init(conn, address, nonce)
|
|
|
|
|
Nonce.__init(session, address, nonce)
|
|
|
|
|
#Nonce.__set(conn, address, nonce+1)
|
|
|
|
|
Nonce.__set(session, address, nonce + 1)
|
|
|
|
|
#if Nonce.transactional:
|
|
|
|
|
#conn.execute('COMMIT')
|
|
|
|
|
# logg.debug('unlocking nonce table for address {}'.format(address))
|
|
|
|
|
#conn.close()
|
2021-03-11 11:40:02 +01:00
|
|
|
|
#session.commit()
|
2021-03-09 07:43:31 +01:00
|
|
|
|
|
|
|
|
|
SessionBase.release_session(session)
|
2021-03-06 18:55:51 +01:00
|
|
|
|
return nonce
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class NonceReservation(SessionBase):
|
|
|
|
|
|
|
|
|
|
__tablename__ = 'nonce_task_reservation'
|
|
|
|
|
|
2021-03-18 11:57:26 +01:00
|
|
|
|
address_hex = Column(String(42))
|
2021-03-06 18:55:51 +01:00
|
|
|
|
nonce = Column(Integer)
|
|
|
|
|
key = Column(String)
|
|
|
|
|
date_created = Column(DateTime, default=datetime.datetime.utcnow)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@staticmethod
|
2021-03-18 11:57:26 +01:00
|
|
|
|
def peek(address, key, session=None):
|
2021-03-06 18:55:51 +01:00
|
|
|
|
session = SessionBase.bind_session(session)
|
|
|
|
|
|
|
|
|
|
q = session.query(NonceReservation)
|
|
|
|
|
q = q.filter(NonceReservation.key==key)
|
2021-03-18 11:57:26 +01:00
|
|
|
|
q = q.filter(NonceReservation.address_hex==address)
|
2021-03-06 18:55:51 +01:00
|
|
|
|
o = q.first()
|
|
|
|
|
|
2021-03-28 18:32:37 +02:00
|
|
|
|
r = None
|
2021-03-06 18:55:51 +01:00
|
|
|
|
if o != None:
|
2021-03-28 18:32:37 +02:00
|
|
|
|
r = (o.key, o.nonce)
|
2021-03-06 18:55:51 +01:00
|
|
|
|
|
|
|
|
|
session.flush()
|
2021-03-01 21:15:17 +01:00
|
|
|
|
|
|
|
|
|
SessionBase.release_session(session)
|
2021-03-06 18:55:51 +01:00
|
|
|
|
|
2021-03-28 18:32:37 +02:00
|
|
|
|
return r
|
2021-02-01 18:12:51 +01:00
|
|
|
|
|
|
|
|
|
|
2021-03-06 18:55:51 +01:00
|
|
|
|
@staticmethod
|
2021-03-18 11:57:26 +01:00
|
|
|
|
def release(address, key, session=None):
|
2021-03-06 18:55:51 +01:00
|
|
|
|
|
|
|
|
|
session = SessionBase.bind_session(session)
|
|
|
|
|
|
2021-03-28 18:32:37 +02:00
|
|
|
|
o = NonceReservation.peek(address, key, session=session)
|
|
|
|
|
|
|
|
|
|
if o == None:
|
|
|
|
|
SessionBase.release_session(session)
|
|
|
|
|
raise IntegrityError('"release" called on key {} address {} which does not exists'.format(key, address))
|
2021-03-06 18:55:51 +01:00
|
|
|
|
|
|
|
|
|
q = session.query(NonceReservation)
|
|
|
|
|
q = q.filter(NonceReservation.key==key)
|
2021-03-28 18:32:37 +02:00
|
|
|
|
q = q.filter(NonceReservation.address_hex==address)
|
2021-03-06 18:55:51 +01:00
|
|
|
|
o = q.first()
|
2021-03-28 18:32:37 +02:00
|
|
|
|
r = (o.key, o.nonce)
|
2021-03-06 18:55:51 +01:00
|
|
|
|
|
|
|
|
|
session.delete(o)
|
|
|
|
|
session.flush()
|
|
|
|
|
|
|
|
|
|
SessionBase.release_session(session)
|
|
|
|
|
|
2021-03-28 18:32:37 +02:00
|
|
|
|
return r
|
2021-03-06 18:55:51 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
|
def next(address, key, session=None):
|
|
|
|
|
session = SessionBase.bind_session(session)
|
|
|
|
|
|
2021-03-28 18:32:37 +02:00
|
|
|
|
o = NonceReservation.peek(address, key, session)
|
|
|
|
|
if o != None:
|
|
|
|
|
raise IntegrityError('"next" called on nonce for key {} address {} during active key {}'.format(key, address, o[0]))
|
2021-03-06 18:55:51 +01:00
|
|
|
|
|
2021-03-09 07:43:31 +01:00
|
|
|
|
nonce = Nonce.next(address, session=session)
|
2021-03-06 18:55:51 +01:00
|
|
|
|
|
|
|
|
|
o = NonceReservation()
|
|
|
|
|
o.nonce = nonce
|
|
|
|
|
o.key = key
|
2021-03-18 11:57:26 +01:00
|
|
|
|
o.address_hex = address
|
2021-03-06 18:55:51 +01:00
|
|
|
|
session.add(o)
|
2021-03-28 18:32:37 +02:00
|
|
|
|
r = (key, nonce)
|
2021-03-06 18:55:51 +01:00
|
|
|
|
|
|
|
|
|
SessionBase.release_session(session)
|
|
|
|
|
|
2021-03-28 18:32:37 +02:00
|
|
|
|
return r
|