connector

This commit is contained in:
4tochka 2019-05-03 22:40:50 +04:00
parent 55480a46d4
commit 9233637e96

View File

@ -744,6 +744,7 @@ class UTXO():
self._hit = 0 self._hit = 0
self.saved_utxo = 0 self.saved_utxo = 0
self.deleted_utxo = 0 self.deleted_utxo = 0
self.deleted_utxo_saved = 0
self.loaded_utxo = 0 self.loaded_utxo = 0
self.destroyed_utxo = 0 self.destroyed_utxo = 0
self.destroyed_utxo_block = 0 self.destroyed_utxo_block = 0
@ -758,90 +759,87 @@ class UTXO():
def destroy_utxo(self, block_height): def destroy_utxo(self, block_height):
block_height -= self.maturity block_height -= self.maturity
self.destroyed_utxo_block = block_height
k = set() for key in range(self.destroyed_utxo_block + 1, block_height + 1):
for key in self.destroyed: if key not in self.destroyed: continue
if key < block_height: n = set()
k.add(key) for outpoint in self.destroyed[key]:
n = set() try:
for outpoint in self.destroyed[key]: del self.cached[outpoint]
self.destroyed_utxo += 1
except:
try: try:
del self.cached[outpoint] del self.loaded[outpoint]
self.destroyed_utxo += 1 n.add(outpoint)
except: except:
try: pass
del self.loaded[outpoint] self.deleted[key] = n
n.add(outpoint) self.destroyed.pop(key)
except:
pass self.destroyed_utxo_block = block_height
self.deleted[key] = n if len(self.cached) - self._cache_size > 0 and not self.save_process:
[self.destroyed.pop(i) for i in k]
if len(self.cached) - self._cache_size > 0:
self.loop.create_task(self.save_utxo(block_height)) self.loop.create_task(self.save_utxo(block_height))
async def save_utxo(self, block_height): async def save_utxo(self, block_height):
# save to db tail from cache # save to db tail from cache
self.save_process = True
c = len(self.cached) - self._cache_size c = len(self.cached) - self._cache_size
if not self.save_process: try:
try: lb = 0
self.save_process = True for key in iter(self.cached):
lb = 0 i = self.cached[key]
ln, rs = set(), set() if c>0 and (i[0] >> 42) <= block_height:
r = set() c -= 1
db = set() 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): for key in iter(self.cached):
i = self.cached[key] i = self.cached[key]
if (c>0 or lb == i[0] >> 42) and (i[0] >> 42) < block_height: if (i[0] >> 42) > lb: break
c -= 1 a.add((key,b"".join((int_to_c_int(i[0]),
continue int_to_c_int(i[1]),
break i[2]))))
if lb: # insert to db
d = set() async with self._db_pool.acquire() as conn:
for key in range(self.last_saved_block + 1, lb + 1): async with conn.transaction():
try: if d:
[d.add(i) for i in self.deleted[key]] await conn.execute("DELETE FROM connector_utxo WHERE "
except: "outpoint = ANY($1);", d)
pass if a:
await conn.copy_records_to_table('connector_utxo',
columns=["outpoint", "data"], records=a)
await conn.execute("UPDATE connector_utxo_state SET value = $1 "
"WHERE name = 'last_block';", lb)
await conn.execute("UPDATE connector_utxo_state SET value = $1 "
"WHERE name = 'last_cached_block';", block_height)
self.saved_utxo += len(a)
self.deleted_utxo += len(d)
a = set() # remove from cache
for key in range(self.last_saved_block + 1, lb + 1): for key in a:
i = self.cached[key] try:
a.add((key,b"".join((int_to_c_int(i[0]), self.cached.pop(key[0])
int_to_c_int(i[1]), except:
i[2])))) pass
# insert to db for key in range(self.last_saved_block + 1, lb + 1):
async with self._db_pool.acquire() as conn: try:
async with conn.transaction(): self.deleted.pop(key)
if d: except:
await conn.execute("DELETE FROM connector_utxo WHERE " pass
"outpoint = ANY($1);", d) self.last_saved_block = lb
if a: finally:
await conn.copy_records_to_table('connector_utxo', self.save_process = False
columns=["outpoint", "data"], records=a)
await conn.execute("UPDATE connector_utxo_state SET value = $1 "
"WHERE name = 'last_block';", lb)
await conn.execute("UPDATE connector_utxo_state SET value = $1 "
"WHERE name = 'last_cached_block';", block_height)
self.saved_utxo += len(a)
self.deleted_utxo += len(d)
# 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_process = False
def get(self, key, block_height): def get(self, key, block_height):
self._requests += 1 self._requests += 1