Correct flags on resume

This commit is contained in:
nolash 2021-02-22 20:54:27 +01:00
parent be405b8376
commit b69005682c
Signed by: lash
GPG Key ID: 21D2E7BB88C2A746
3 changed files with 29 additions and 19 deletions

View File

@ -95,6 +95,7 @@ class SyncerBackend:
"""
self.connect()
pair = self.db_object.set(block_height, tx_height)
self.db_object_filter.clear()
(filter_state, count, digest)= self.db_object_filter.cursor()
self.disconnect()
return (pair, filter_state,)
@ -204,8 +205,6 @@ class SyncerBackend:
syncers.append(s)
last_live_id = BlockchainSync.get_last(session=session)
logg.debug('last_live_id {}'.format(last_live_id))
logg.debug('higesst {} {}'.format(highest_unsynced_block, highest_unsynced_tx))
if last_live_id != None:
q = session.query(BlockchainSync)
@ -279,6 +278,13 @@ class SyncerBackend:
self.disconnect()
def complete_filter(self, n):
self.connect()
self.db_object_filter.set(n)
self.disconnect()
class MemBackend:
def __init__(self, chain_spec, object_id):

View File

@ -32,7 +32,7 @@ class BlockchainSyncFilter(SessionBase):
if flags == None:
flags = bytearray(0)
else: # TODO: handle bytes too
bytecount = int((count - 1) / 8 + 1) + 1
bytecount = int((count - 1) / 8 + 1)
flags = flags.to_bytes(bytecount, 'big')
self.flags_start = flags
self.flags = flags
@ -49,7 +49,6 @@ class BlockchainSyncFilter(SessionBase):
old_byte_count = int((self.count - 1) / 8 + 1)
new_byte_count = int((self.count) / 8 + 1)
logg.debug('old new {} {}'.format(old_byte_count, new_byte_count))
if old_byte_count != new_byte_count:
self.flags = bytearray(1) + self.flags
self.count += 1
@ -72,7 +71,7 @@ class BlockchainSyncFilter(SessionBase):
def clear(self):
self.flags = 0
self.flags = bytearray(len(self.flags))
def set(self, n):
@ -80,10 +79,10 @@ class BlockchainSyncFilter(SessionBase):
raise IndexError('bit flag out of range')
b = 1 << (n % 8)
i = int((n - 1) / 8 + 1)
if self.flags[i] & b > 0:
SessionBase.release_session(session)
i = int(n / 8)
byte_idx = len(self.flags)-1-i
if (self.flags[byte_idx] & b) > 0:
raise AttributeError('Filter bit already set')
flags = bytearray(self.flags)
flags[i] |= b
flags[byte_idx] |= b
self.flags = flags

View File

@ -76,7 +76,6 @@ class TestDatabase(TestBase):
s.set(42, 13)
s = SyncerBackend.first(self.chain_spec)
logg.debug('start {}'.format(s))
self.assertEqual(s.get(), ((42,13), 0))
@ -127,17 +126,8 @@ class TestDatabase(TestBase):
SyncerBackend.live(self.chain_spec, 666)
s[0].set(123, 2)
logg.debug('>>>>>')
s = SyncerBackend.resume(self.chain_spec, 1024)
SyncerBackend.live(self.chain_spec, 1024)
s[0].connect()
logg.debug('syncer 1 {}'.format(s[0].db_object))
s[0].disconnect()
s[1].connect()
logg.debug('syncer 2 {}'.format(s[1].db_object))
s[1].disconnect()
self.assertEqual(len(s), 2)
self.assertEqual(s[0].target(), (666, 0))
@ -146,6 +136,21 @@ class TestDatabase(TestBase):
self.assertEqual(s[1].get(), ((666, 0), 0))
def test_backend_resume_filter(self):
s = SyncerBackend.live(self.chain_spec, 42)
s.register_filter('foo')
s.register_filter('bar')
s.register_filter('baz')
s.set(43, 13)
s.complete_filter(0)
s.complete_filter(2)
s = SyncerBackend.resume(self.chain_spec, 666)
(pair, flags) = s[0].get()
self.assertEqual(flags, 5)
if __name__ == '__main__':
unittest.main()