def test_nbn_puts_messages_on_q(self): n = NBNInbox( contacts=VKBook(), driver=BlockchainDriver(), socket_id=_socket('tcp://127.0.0.1:8888'), ctx=self.ctx, linger=500, poll_timeout=500, verify=False ) async def send(): socket = self.ctx.socket(zmq.DEALER) socket.connect('tcp://127.0.0.1:8888') await socket.send(b'') async def stop(): await asyncio.sleep(0.5) n.stop() tasks = asyncio.gather( n.serve(), send(), stop() ) self.loop.run_until_complete(tasks) self.assertEqual(len(n.q), 1)
def test_block_notification_wrong_type_throws_exception(self): n = NBNInbox(contacts=VKBook(), driver=BlockchainDriver(), socket_id=_socket('tcp://127.0.0.1:8888'), ctx=self.ctx, linger=50, poll_timeout=50) msg = Message.get_message_packed_2(MessageType.BURN_INPUT_HASHES) with self.assertRaises(NotBlockNotificationMessageType): n.validate_nbn(msg)
def test_block_notification_invalid_block_num_throws_exception(self): n = NBNInbox(contacts=VKBook(), driver=BlockchainDriver(), socket_id=_socket('tcp://127.0.0.1:8888'), ctx=self.ctx, linger=50, poll_timeout=50) n.driver.set_latest_block_num(1) msg = Message.get_message_packed_2(MessageType.BLOCK_NOTIFICATION, blockNum=100) with self.assertRaises(BlockNumberMismatch): n.validate_nbn(msg)
def test_set_mn_id_test_hook_false_master_in_active_masters(self): PhoneBook = VKBook(self.client) vk = PhoneBook.masternodes[0] self.db.test_hook = False success = self.db.set_mn_id(vk) self.assertEqual(self.db.mn_id, 0) self.assertTrue(success)
def test_get_sockets_with_service(self): constitution = self.get_vkbook_args() sync.submit_from_genesis_json_file(cilantro_ee.contracts.__path__[0] + '/genesis.json', client=self.client) sync.submit_node_election_contracts( initial_masternodes=constitution['masternodes'], boot_mns=constitution['masternode_min_quorum'], initial_delegates=constitution['delegates'], boot_dels=constitution['delegate_min_quorum'], client=self.client) PhoneBook = VKBook(client=self.client) w1 = Wallet() p1 = Network(wallet=w1, ctx=self.ctx, socket_base='tcp://127.0.0.1') # 'tcp://127.0.0.1:10003' raw = {'stu': 'tcp://127.0.0.1', 'raghu': 'tcp://127.0.0.2'} p1.peer_service.table = raw expected = { 'stu': _socket('tcp://127.0.0.1:19003'), 'raghu': _socket('tcp://127.0.0.2:19003') } # CHANGE CLIENT TO SOCKET masternodes = Parameters(socket_base='tcp://127.0.0.1', wallet=w1, contacts=MockContacts(['stu'], delegates=['raghu']), ctx=self.ctx) self.assertDictEqual(masternodes.sockets, {}) async def late_refresh(): await asyncio.sleep(0.3) await masternodes.refresh() async def stop(): await asyncio.sleep(0.5) p1.stop() tasks = asyncio.gather(p1.start(discover=False), late_refresh(), stop()) self.loop.run_until_complete(tasks) self.assertEqual( masternodes.get_all_sockets(service=ServiceType.EVENT), expected)
def setUp(self): #m, d = sync.get_masternodes_and_delegates_from_constitution() self.client = ContractingClient() self.client.flush() sync.submit_from_genesis_json_file(cilantro_ee.contracts.__path__[0] + '/genesis.json', client=self.client) sync.submit_node_election_contracts( initial_masternodes=Wallet().verifying_key().hex(), boot_mns=1, initial_delegates=Wallet().verifying_key().hex(), boot_dels=1, client=self.client) w = Wallet() sk, vk = w.signing_key(), w.verifying_key() self.db = DistributedMasterStorage(key=sk, vkbook=VKBook(self.client, 1, 1))
def test_refresh(self): vkbook_args = self.get_vkbook_args() sync.submit_vkbook(vkbook_args, overwrite=True) PhoneBook = VKBook() w1 = Wallet() p1 = Network(wallet=w1, ctx=self.ctx, socket_base='tcp://127.0.0.1') #'tcp://127.0.0.1:10003' raw = {'stu': 'tcp://127.0.0.1', 'raghu': 'tcp://127.0.0.2'} p1.peer_service.table.peers = raw expected = { 'stu': _socket('tcp://127.0.0.1:{}'.format(EVENT_PORT)), 'raghu': _socket('tcp://127.0.0.2:{}'.format(EVENT_PORT)) } # CHANGE CLIENT TO SOCKET masternodes = SocketBook( socket_base='tcp://127.0.0.1', service_type=ServiceType.EVENT, ctx=self.ctx, phonebook_function=PhoneBook.contract.get_masternodes) self.assertDictEqual(masternodes.sockets, {}) async def late_refresh(): await asyncio.sleep(0.3) await masternodes.refresh() async def stop(): await asyncio.sleep(0.5) p1.stop() tasks = asyncio.gather(p1.start(discover=False), late_refresh(), stop()) self.loop.run_until_complete(tasks) self.assertDictEqual(masternodes.sockets, expected)
def test_build_write_list_curr_node_0_jump_idx_1_returns_all(self): masternodes = list(range(100)) delegates = list(range(10)) self.client.flush() sync.submit_from_genesis_json_file(cilantro_ee.contracts.__path__[0] + '/genesis.json', client=self.client) sync.submit_node_election_contracts(initial_masternodes=masternodes, boot_mns=1, initial_delegates=delegates, boot_dels=1, client=self.client) big_vkbook = VKBook(self.client) self.db.vkbook = big_vkbook write_list = self.db.build_wr_list(0, 1) self.assertEqual(masternodes, write_list)
def test_remove_node_doesnt_exist_does_nothing(self): constitution = self.get_vkbook_args() sync.submit_from_genesis_json_file(cilantro_ee.contracts.__path__[0] + '/genesis.json', client=self.client) sync.submit_node_election_contracts( initial_masternodes=constitution['masternodes'], boot_mns=constitution['masternode_min_quorum'], initial_delegates=constitution['delegates'], boot_dels=constitution['delegate_min_quorum'], client=self.client) ctx = zmq.Context() m = Parameters(socket_base='tcp://127.0.0.1', ctx=ctx, contacts=VKBook(client=self.client), wallet=Wallet()) m.sockets = {'a': ctx.socket(zmq.PUB), 'b': ctx.socket(zmq.PUB)} m.remove_node('c') self.assertIsNotNone(m.sockets.get('a')) self.assertIsNotNone(m.sockets.get('b'))
def test_nbn_wait_for_next_nbn_returns_first_on_q(self): n = NBNInbox(contacts=VKBook(), driver=BlockchainDriver(), socket_id=_socket('tcp://127.0.0.1:8888'), ctx=self.ctx, linger=50, poll_timeout=50, verify=False) async def send(): socket = self.ctx.socket(zmq.DEALER) socket.connect('tcp://127.0.0.1:8888') await socket.send(b'\x00') async def stop(): await asyncio.sleep(0.5) n.stop() tasks = asyncio.gather( n.serve(), send(), stop(), n.wait_for_next_nbn() ) _, _, _, a = self.loop.run_until_complete(tasks) self.assertEqual(a, b'\x00')
def test_refresh_remove_old_nodes(self): vkbook_args = self.get_vkbook_args() sync.submit_vkbook(vkbook_args, overwrite=True) PhoneBook = VKBook() w1 = Wallet() p1 = Network(wallet=w1, ctx=self.ctx, socket_base='tcp://127.0.0.1') peeps = { 'stu': 'tcp://127.0.0.1', 'raghu': 'tcp://127.0.0.8', 'tejas': 'tcp://127.0.2.1', 'steve': 'tcp://127.0.54.6' } p1.peer_service.table.peers = peeps ctx2 = zmq.asyncio.Context() masternodes = SocketBook( socket_base='tcp://127.0.0.1', service_type=ServiceType.EVENT, ctx=ctx2, phonebook_function=PhoneBook.contract.get_masternodes) self.assertDictEqual(masternodes.sockets, {}) async def late_refresh(): await asyncio.sleep(0.3) await masternodes.refresh() async def stop(): await asyncio.sleep(0.5) p1.stop() tasks = asyncio.gather(p1.start(discover=False), late_refresh(), stop()) self.loop.run_until_complete(tasks) expected = { 'stu': _socket('tcp://127.0.0.1:{}'.format(EVENT_PORT)), 'raghu': _socket('tcp://127.0.0.8:{}'.format(EVENT_PORT)) } self.assertDictEqual(masternodes.sockets, expected) self.ctx.destroy() self.loop.close() self.ctx = zmq.asyncio.Context() self.loop = asyncio.new_event_loop() asyncio.set_event_loop(self.loop) w1 = Wallet() p1 = Network(wallet=w1, ctx=self.ctx, socket_base='tcp://127.0.0.1') peeps = { 'stu': 'tcp://127.0.2.1', 'raghu': 'tcp://127.0.0.8', 'tejas': 'tcp://127.0.2.1', 'steve': 'tcp://127.0.54.6' } p1.peer_service.table.peers = peeps vkbook_args = self.get_vkbook_args(mns=['stu', 'tejas']) sync.submit_vkbook(vkbook_args, overwrite=True) async def late_refresh(): await asyncio.sleep(0.3) await masternodes.refresh() async def stop(): await asyncio.sleep(1) p1.stop() tasks = asyncio.gather(p1.start(discover=False), late_refresh(), stop()) self.loop.run_until_complete(tasks) expected = { 'stu': _socket('tcp://127.0.2.1:{}'.format(EVENT_PORT)), 'tejas': _socket('tcp://127.0.2.1:{}'.format(EVENT_PORT)), } self.assertDictEqual(masternodes.sockets, expected)
def test_build_write_list_returns_all_mns_when_jump_idx_0(self): PhoneBook = VKBook(self.client) mns = PhoneBook.masternodes self.assertEqual(mns, self.db.build_wr_list(None, 0))
def test_refresh_remove_old_nodes(self): constitution = self.get_vkbook_args() sync.submit_from_genesis_json_file(cilantro_ee.contracts.__path__[0] + '/genesis.json', client=self.client) sync.submit_node_election_contracts( initial_masternodes=constitution['masternodes'], boot_mns=constitution['masternode_min_quorum'], initial_delegates=constitution['delegates'], boot_dels=constitution['delegate_min_quorum'], client=self.client) PhoneBook = VKBook(client=self.client) w1 = Wallet() p1 = Network(wallet=w1, ctx=self.ctx, socket_base='tcp://127.0.0.1') peeps = { 'stu': 'tcp://127.0.0.1', 'raghu': 'tcp://127.0.0.8', 'tejas': 'tcp://127.0.2.1', 'steve': 'tcp://127.0.54.6' } p1.peer_service.table = peeps ctx2 = zmq.asyncio.Context() masternodes = Parameters(socket_base='tcp://127.0.0.1', wallet=w1, contacts=MockContacts(['stu', 'raghu'], ['tejas', 'steve']), ctx=ctx2) self.assertDictEqual(masternodes.sockets, {}) async def late_refresh(): await asyncio.sleep(0.3) await masternodes.refresh() async def stop(): await asyncio.sleep(0.5) p1.stop() tasks = asyncio.gather(p1.start(discover=False), late_refresh(), stop()) self.loop.run_until_complete(tasks) expected = {'stu': 'tcp://127.0.0.1', 'raghu': 'tcp://127.0.0.8'} self.assertDictEqual(masternodes.sockets, expected) self.ctx.destroy() self.loop.close() self.ctx = zmq.asyncio.Context() self.loop = asyncio.new_event_loop() asyncio.set_event_loop(self.loop) w1 = Wallet() p1 = Network(wallet=w1, ctx=self.ctx, socket_base='tcp://127.0.0.1') peeps = { 'stu': 'tcp://127.0.2.1', 'raghu': 'tcp://127.0.0.8', 'tejas': 'tcp://127.0.2.1', 'steve': 'tcp://127.0.54.6' } p1.peer_service.table.peers = peeps vkbook_args = self.get_vkbook_args(mns=['stu', 'tejas']) sync.submit_vkbook(vkbook_args, overwrite=True) async def late_refresh(): await asyncio.sleep(0.3) await masternodes.refresh() async def stop(): await asyncio.sleep(1) p1.stop() tasks = asyncio.gather(p1.start(discover=False), late_refresh(), stop()) self.loop.run_until_complete(tasks) expected = { 'stu': 'tcp://127.0.2.1', 'tejas': 'tcp://127.0.2.1', } self.assertDictEqual(masternodes.sockets, expected)
def test_init(self): n = NBNInbox(contacts=VKBook(), driver=BlockchainDriver(), socket_id=_socket('tcp://127.0.0.1:8888'), ctx=self.ctx)