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,41 +59,55 @@ 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:
c = len(self.cached) - self._cache_soft_limit - self.block_txo_max
if c <= 0: break
self.log.critical("str>>>>")
try: try:
lb = 0 lb = 0
for key in iter(self.cached): block_changed = False
i = self.cached[key] utxo = set()
if c>0 and (i[0] >> 42) <= block_height: while self.cached:
c -= 1 i = self.cached.pop()
lb = i[0] >> 42 if lb != i[1][0] >> 42:
continue block_changed = True
lb = i[1][0] >> 42
if self.cached <= self.size_limit:
if block_changed:
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
a = set() # break
for key in iter(self.cached): #
i = self.cached[key] # if lb:
if (i[0] >> 42) > lb: break # d = set()
a.add((key,b"".join((int_to_c_int(i[0]), # for key in range(self.last_saved_block + 1, lb + 1):
int_to_c_int(i[1]), # try:
i[2])))) # [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]))))
# insert to db # insert to db
d = set()
async with self._db_pool.acquire() as conn: async with self._db_pool.acquire() as conn:
async with conn.transaction(): async with conn.transaction():
if d: if d:
@ -102,26 +115,24 @@ class UTXO():
"outpoint = ANY($1);", d) "outpoint = ANY($1);", d)
if a: if a:
await conn.copy_records_to_table('connector_utxo', await conn.copy_records_to_table('connector_utxo',
columns=["outpoint", "data"], records=a) columns=["outpoint", "data"], records=utxo)
await conn.execute("UPDATE connector_utxo_state SET value = $1 " await conn.execute("UPDATE connector_utxo_state SET value = $1 "
"WHERE name = 'last_block';", lb) "WHERE name = 'last_block';", lb)
await conn.execute("UPDATE connector_utxo_state SET value = $1 " self.saved_utxo += len(utxo)
"WHERE name = 'last_cached_block';", block_height)
self.saved_utxo += len(a)
self.deleted_utxo += len(d) self.deleted_utxo += len(d)
# remove from cache # # remove from cache
for key in a: # for key in a:
try: # try:
self.cached.pop(key[0]) # self.cached.pop(key[0])
except: # except:
pass # pass
#
for key in range(self.last_saved_block + 1, lb + 1): # for key in range(self.last_saved_block + 1, lb + 1):
try: # try:
self.deleted.pop(key) # self.deleted.pop(key)
except: # except:
pass # pass
self.last_saved_block = lb self.last_saved_block = lb
finally: finally:
self.save_future = False self.save_future = False