connector

This commit is contained in:
4tochka 2019-05-12 19:27:38 +04:00
parent fcc83e69da
commit 471b1cd8a4

View File

@ -19,8 +19,7 @@ class UTXO():
self.clear_tail = False self.clear_tail = False
self.last_saved_block = 0 self.last_saved_block = 0
self.last_cached_block = 0 self.last_cached_block = 0
self.save_future = asyncio.Future() self.save_process = False
self.save_future.set_result(True)
self.load_utxo_future = asyncio.Future() self.load_utxo_future = asyncio.Future()
self.load_utxo_future.set_result(True) self.load_utxo_future.set_result(True)
self._requests = 0 self._requests = 0
@ -60,71 +59,83 @@ class UTXO():
# save to db tail from cache # save to db tail from cache
self.log.critical("save utxo>>>>") self.log.critical("save utxo>>>>")
return return
if not self.save_future.done(): if self.save_process or not self.cached:
await self.save_future.done()
return return
self.save_future = asyncio.Future() self.save_process = True
while True: try:
c = len(self.cached) - self._cache_soft_limit - self.block_txo_max lb = 0
if c <= 0: break block_changed = False
self.log.critical("str>>>>") utxo = set()
try: while self.cached:
lb = 0 i = self.cached.pop()
for key in iter(self.cached): if lb != i[1][0] >> 42:
i = self.cached[key] block_changed = True
if c>0 and (i[0] >> 42) <= block_height: lb = i[1][0] >> 42
c -= 1 if self.cached <= self.size_limit:
lb = i[0] >> 42 if block_changed:
continue break
break utxo.add((i[0],b"".join((int_to_c_int(i[1][0]),
int_to_c_int(i[1][1]),
i[1][2]))))
if block_changed:
self.cached.append({i[0]: i[1]})
if lb: #
d = set() # block_height
for key in range(self.last_saved_block + 1, lb + 1): # for key in iter(self.cached):
try: # i = self.cached[key]
[d.add(i) for i in self.deleted[key]] # if c>0 and (i[0] >> 42) <= block_height:
except: # c -= 1
pass # lb = i[0] >> 42
# continue
# break
#
# if lb:
# d = set()
# for key in range(self.last_saved_block + 1, lb + 1):
# try:
# [d.add(i) for i in self.deleted[key]]
# except:
# pass
#
# a = set()
# for key in iter(self.cached):
# i = self.cached[key]
# if (i[0] >> 42) > lb: break
# a.add((key,b"".join((int_to_c_int(i[0]),
# int_to_c_int(i[1]),
# i[2]))))
a = set() # insert to db
for key in iter(self.cached): d = set()
i = self.cached[key] async with self._db_pool.acquire() as conn:
if (i[0] >> 42) > lb: break async with conn.transaction():
a.add((key,b"".join((int_to_c_int(i[0]), if d:
int_to_c_int(i[1]), await conn.execute("DELETE FROM connector_utxo WHERE "
i[2])))) "outpoint = ANY($1);", d)
if a:
await conn.copy_records_to_table('connector_utxo',
columns=["outpoint", "data"], records=utxo)
await conn.execute("UPDATE connector_utxo_state SET value = $1 "
"WHERE name = 'last_block';", lb)
self.saved_utxo += len(utxo)
self.deleted_utxo += len(d)
# insert to db # # remove from cache
async with self._db_pool.acquire() as conn: # for key in a:
async with conn.transaction(): # try:
if d: # self.cached.pop(key[0])
await conn.execute("DELETE FROM connector_utxo WHERE " # except:
"outpoint = ANY($1);", d) # pass
if a: #
await conn.copy_records_to_table('connector_utxo', # for key in range(self.last_saved_block + 1, lb + 1):
columns=["outpoint", "data"], records=a) # try:
await conn.execute("UPDATE connector_utxo_state SET value = $1 " # self.deleted.pop(key)
"WHERE name = 'last_block';", lb) # except:
await conn.execute("UPDATE connector_utxo_state SET value = $1 " # pass
"WHERE name = 'last_cached_block';", block_height) self.last_saved_block = lb
self.saved_utxo += len(a) finally:
self.deleted_utxo += len(d) self.save_future = False
# remove from cache
for key in a:
try:
self.cached.pop(key[0])
except:
pass
for key in range(self.last_saved_block + 1, lb + 1):
try:
self.deleted.pop(key)
except:
pass
self.last_saved_block = lb
finally:
self.save_future = False
def get(self, key): def get(self, key):
self._requests += 1 self._requests += 1