WIP server sql backend adapter handle duplicates

This commit is contained in:
nolash 2021-06-02 13:09:21 +02:00
parent eca9dd95d6
commit b0eb0c474d
Signed by: lash
GPG Key ID: 21D2E7BB88C2A746
4 changed files with 24 additions and 5 deletions

View File

@ -16,5 +16,6 @@ class EthAdapter:
def add(self, chain_spec, bytecode): def add(self, chain_spec, bytecode):
tx = unpack(bytecode, chain_spec) tx = unpack(bytecode, chain_spec)
session = self.backend.create_session() session = self.backend.create_session()
self.backend.create(chain_spec, tx['nonce'], tx['from'], tx['hash'], add_0x(bytecode.hex()), session=session) r = self.backend.create(chain_spec, tx['nonce'], tx['from'], tx['hash'], add_0x(bytecode.hex()), session=session)
session.close() session.close()
return r

View File

@ -27,4 +27,3 @@ class BackendIntegrityError(ChainQueueException):
""" """
pass pass

View File

@ -124,6 +124,7 @@ if __name__ == '__main__':
data_in = srvs.recv(1024) data_in = srvs.recv(1024)
data_in_str = data_in.decode('utf-8') data_in_str = data_in.decode('utf-8')
data = bytes.fromhex(strip_0x(data_in_str)) data = bytes.fromhex(strip_0x(data_in_str))
adapter.add(chain_spec, data) r = adapter.add(chain_spec, data)
srvs.send(r.to_bytes(4, byteorder='big'))
ctrl.shutdown(None, None) ctrl.shutdown(None, None)

View File

@ -1,13 +1,31 @@
# standard imports
import logging
# external imports
from sqlalchemy.exc import (
IntegrityError,
)
# local imports # local imports
from chainqueue.sql.tx import create from chainqueue.sql.tx import create as queue_create
from chainqueue.db.models.base import SessionBase from chainqueue.db.models.base import SessionBase
logg = logging.getLogger(__name__)
class SQLBackend: class SQLBackend:
def __init__(self, conn_spec, *args, **kwargs): def __init__(self, conn_spec, *args, **kwargs):
SessionBase.connect(conn_spec, pool_size=kwargs.get('poolsize', 0), debug=kwargs.get('debug', False)) SessionBase.connect(conn_spec, pool_size=kwargs.get('poolsize', 0), debug=kwargs.get('debug', False))
self.create = create
def create(self, chain_spec, nonce, holder_address, tx_hash, signed_tx, obsolete_predecessors=True, session=None):
try:
queue_create(chain_spec, nonce, holder_address, tx_hash, signed_tx, obsolete_predecessors=True, session=None)
except IntegrityError as e:
logg.warning('skipped possible duplicate insert {}'.format(e))
return 1
return 0
def create_session(self): def create_session(self):