2022-03-16 19:44:13 +01:00
|
|
|
# standard imporst
|
2021-02-17 12:44:35 +01:00
|
|
|
import unittest
|
|
|
|
|
|
|
|
# external imports
|
2022-03-16 19:44:13 +01:00
|
|
|
from shep import State
|
2021-02-17 12:44:35 +01:00
|
|
|
|
|
|
|
# local imports
|
2022-03-16 19:44:13 +01:00
|
|
|
from chainsyncer.state import SyncState
|
|
|
|
from chainsyncer.session import SyncSession
|
2021-02-17 12:44:35 +01:00
|
|
|
|
|
|
|
|
2022-03-16 19:44:13 +01:00
|
|
|
class MockStore(State):
|
2021-02-17 12:44:35 +01:00
|
|
|
|
2022-03-17 11:09:12 +01:00
|
|
|
def __init__(self, bits=0):
|
2022-03-16 19:44:13 +01:00
|
|
|
super(MockStore, self).__init__(bits, check_alias=False)
|
2021-02-17 12:44:35 +01:00
|
|
|
|
|
|
|
|
2022-03-16 19:44:13 +01:00
|
|
|
class MockFilter:
|
|
|
|
|
2022-03-17 11:09:12 +01:00
|
|
|
def __init__(self, name, brk=False, z=None):
|
2022-03-16 19:44:13 +01:00
|
|
|
self.name = name
|
2022-03-17 11:09:12 +01:00
|
|
|
if z == None:
|
|
|
|
h = hashlib.sha256()
|
|
|
|
h.update(self.name.encode('utf-8'))
|
|
|
|
z = h.digest()
|
|
|
|
self.z = z
|
|
|
|
self.brk = brk
|
2022-03-16 19:44:13 +01:00
|
|
|
|
|
|
|
|
|
|
|
def sum(self):
|
|
|
|
return self.z
|
|
|
|
|
|
|
|
|
|
|
|
def common_name(self):
|
|
|
|
return self.name
|
|
|
|
|
|
|
|
|
2022-03-17 11:09:12 +01:00
|
|
|
def filter(self, conn, block, tx):
|
|
|
|
return self.brk
|
2022-03-16 19:44:13 +01:00
|
|
|
|
|
|
|
|
2022-03-17 11:09:12 +01:00
|
|
|
class TestSync(unittest.TestCase):
|
2022-03-16 19:44:13 +01:00
|
|
|
|
|
|
|
def test_basic(self):
|
2022-03-17 11:09:12 +01:00
|
|
|
store = MockStore(6)
|
|
|
|
state = SyncState(store)
|
|
|
|
session = SyncSession(None, state)
|
2022-03-16 19:44:13 +01:00
|
|
|
self.assertTrue(session.is_default)
|
|
|
|
|
2022-03-17 11:09:12 +01:00
|
|
|
session = SyncSession(None, state, session_id='foo')
|
2022-03-16 19:44:13 +01:00
|
|
|
self.assertFalse(session.is_default)
|
|
|
|
|
|
|
|
|
|
|
|
def test_sum(self):
|
2022-03-17 11:09:12 +01:00
|
|
|
store = MockStore(4)
|
|
|
|
state = SyncState(store)
|
|
|
|
|
2022-03-16 19:44:13 +01:00
|
|
|
b = b'\x2a' * 32
|
2022-03-17 11:09:12 +01:00
|
|
|
fltr = MockFilter('foo', z=b)
|
|
|
|
state.register(fltr)
|
2022-03-16 19:44:13 +01:00
|
|
|
|
|
|
|
b = b'\x0d' * 31
|
2022-03-17 11:09:12 +01:00
|
|
|
fltr = MockFilter('bar', z=b)
|
2022-03-16 19:44:13 +01:00
|
|
|
with self.assertRaises(ValueError):
|
2022-03-17 11:09:12 +01:00
|
|
|
state.register(fltr)
|
2022-03-16 19:44:13 +01:00
|
|
|
|
|
|
|
b = b'\x0d' * 32
|
2022-03-17 11:09:12 +01:00
|
|
|
fltr = MockFilter('bar', z=b)
|
|
|
|
state.register(fltr)
|
2022-03-16 19:44:13 +01:00
|
|
|
|
2022-03-17 11:09:12 +01:00
|
|
|
v = state.sum()
|
2022-03-16 19:44:13 +01:00
|
|
|
self.assertEqual(v.hex(), 'a24abf9fec112b4e0210ae874b4a371f8657b1ee0d923ad6d974aef90bad8550')
|
|
|
|
|
|
|
|
|
|
|
|
def test_session_start(self):
|
2022-03-17 11:09:12 +01:00
|
|
|
store = MockStore(6)
|
|
|
|
state = SyncState(store)
|
|
|
|
session = SyncSession(None, state)
|
2022-03-16 19:44:13 +01:00
|
|
|
session.start()
|
2022-03-17 11:09:12 +01:00
|
|
|
|
|
|
|
|
|
|
|
def test_state_dynamic(self):
|
|
|
|
store = MockStore()
|
|
|
|
state = SyncState(store)
|
|
|
|
|
|
|
|
b = b'\x0d' * 32
|
|
|
|
fltr = MockFilter(name='foo', z=b)
|
|
|
|
state.register(fltr)
|
|
|
|
|
|
|
|
|
2021-02-17 12:44:35 +01:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
unittest.main()
|