Node atomicity rehabilitated

This commit is contained in:
nolash 2021-03-29 11:35:41 +02:00
parent bd9070ae44
commit 1047a1a487
Signed by untrusted user who does not match committer: lash
GPG Key ID: 21D2E7BB88C2A746
2 changed files with 17 additions and 3 deletions

View File

@ -55,6 +55,20 @@ class Nonce(SessionBase):
conn.execute("UPDATE nonce set nonce = {} WHERE address_hex = '{}'".format(nonce, address))
@staticmethod
def __inc(conn, address):
#conn.execute("UPDATE nonce set nonce = nonce + 1 WHERE address_hex = '{}'".format(address))
q = conn.query(Nonce)
q = q.filter(Nonce.address_hex==address)
q = q.with_for_update()
o = q.first()
nonce = o.nonce
o.nonce += 1
conn.add(o)
conn.flush()
return nonce
@staticmethod
def __init(conn, address, nonce):
conn.execute("INSERT INTO nonce (nonce, address_hex) VALUES ({}, '{}')".format(nonce, address))
@ -107,7 +121,7 @@ class Nonce(SessionBase):
#Nonce.__init(conn, address, nonce)
Nonce.__init(session, address, nonce)
#Nonce.__set(conn, address, nonce+1)
Nonce.__set(session, address, nonce + 1)
nonce = Nonce.__inc(session, address)
#if Nonce.transactional:
#conn.execute('COMMIT')
# logg.debug('unlocking nonce table for address {}'.format(address))

View File

@ -48,7 +48,7 @@ def test_nonce_reserve(
uu = uuid.uuid4()
nonce = NonceReservation.next(eth_empty_accounts[0], str(uu), session=init_database)
init_database.commit()
assert nonce == 42
assert nonce == (str(uu), 42)
q = init_database.query(Nonce)
q = q.filter(Nonce.address_hex==eth_empty_accounts[0])
@ -57,7 +57,7 @@ def test_nonce_reserve(
nonce = NonceReservation.release(eth_empty_accounts[0], str(uu))
init_database.commit()
assert nonce == 42
assert nonce == (str(uu), 42)
q = init_database.query(NonceReservation)
q = q.filter(NonceReservation.key==str(uu))