Example #1
0
    def setUp(self):
        self.ctx = zmq.asyncio.Context()
        self.w = Wallet()

        masternodes = [
            Wallet().verifying_key().hex(),
            Wallet().verifying_key().hex(),
            Wallet().verifying_key().hex(),
        ]
        delegates = [
            Wallet().verifying_key().hex(),
            Wallet().verifying_key().hex(),
            Wallet().verifying_key().hex(),
        ]

        self.c = ContractingClient()
        self.c.flush()

        sync.submit_from_genesis_json_file(cilantro_ee.contracts.__path__[0] +
                                           '/genesis.json',
                                           client=self.c)
        sync.submit_node_election_contracts(initial_masternodes=masternodes,
                                            boot_mns=1,
                                            initial_delegates=delegates,
                                            boot_dels=1,
                                            client=self.c)

        self.s = SocketAuthenticator(ctx=self.ctx)
def seed_vk_book(num_mn=10, mn_quorum=10, num_del=10, del_quorum=10):
    mn_wallets = [Wallet().verifying_key().hex() for _ in range(num_mn)]
    dn_wallets = [Wallet().verifying_key().hex() for _ in range(num_del)]

    # Sync contracts
    sync.submit_from_genesis_json_file(cilantro_ee.contracts.__path__[0] + '/genesis.json')
    sync.submit_node_election_contracts(
        initial_masternodes=mn_wallets,
        boot_mns=mn_quorum,
        initial_delegates=dn_wallets,
        boot_dels=del_quorum
    )
    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)
Example #4
0
    def setUp(self):

        self.client = ContractingClient()
        self.driver = self.client.raw_driver

        # Sync contracts
        sync.submit_from_genesis_json_file(cilantro_ee.contracts.__path__[0] +
                                           '/genesis.json',
                                           client=self.client)
        sync.submit_node_election_contracts(
            initial_masternodes=['stu', 'raghu', 'steve'],
            boot_mns=2,
            initial_delegates=['tejas', 'alex'],
            boot_dels=3,
            client=self.client)

        self.r = RewardManager(driver=self.driver)
Example #5
0
    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))
Example #6
0
    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'))
Example #8
0
    def __init__(self,
                 socket_base,
                 ctx: zmq.asyncio.Context,
                 wallet,
                 constitution: dict,
                 overwrite=False,
                 bootnodes=[],
                 network_parameters=NetworkParameters(),
                 driver=BlockchainDriver(),
                 mn_seed=None,
                 debug=True,
                 store=False):

        self.driver = driver
        self.store = store

        self.blocks = None
        if self.store:
            self.blocks = MasterStorage()

        self.waiting_for_confirmation = False

        self.log = get_logger('NODE')
        self.log.propagate = debug
        self.log.info(constitution)
        self.socket_base = socket_base
        self.wallet = wallet
        self.ctx = ctx
        self.ctx.max_sockets = 50_000

        self.client = ContractingClient(
            driver=self.driver,
            submission_filename=cilantro_ee.contracts.__path__[0] +
            '/submission.s.py')

        # Sync contracts

        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)

        self.driver.commit()
        self.driver.clear_pending_state()

        self.contacts = VKBook(
            client=self.client,
            boot_mn=constitution['masternode_min_quorum'],
            boot_del=constitution['delegate_min_quorum'],
        )

        self.current_masters = deepcopy(self.contacts.masternodes)
        self.current_delegates = deepcopy(self.contacts.delegates)

        self.parameters = Parameters(socket_base,
                                     ctx,
                                     wallet,
                                     contacts=self.contacts)

        self.socket_authenticator = SocketAuthenticator(ctx=self.ctx)

        self.elect_masternodes = self.client.get_contract('elect_masternodes')
        self.elect_delegates = self.client.get_contract('elect_delegates')

        self.masternode_contract = self.client.get_contract('masternodes')
        self.delegate_contract = self.client.get_contract('delegates')

        self.update_sockets()

        # Cilantro version / upgrade

        self.version_state = self.client.get_contract('upgrade')
        self.active_upgrade = self.version_state.quick_read('upg_lock')

        self.tol_mn = self.version_state.quick_read('tol_mn')
        self.tot_dl = self.version_state.quick_read('tot_dl')

        if self.tol_mn is None:
            self.tol_mn = 0

        if self.tot_dl is None:
            self.tot_dl = 0

        self.all_votes = self.tol_mn + self.tot_dl
        self.mn_votes = self.version_state.quick_read('mn_vote')
        self.dl_votes = self.version_state.quick_read('dl_vote')
        # self.pending_cnt = self.all_votes - self.vote_cnt
        # stuff

        self.network_parameters = network_parameters

        self.bootnodes = bootnodes
        self.constitution = constitution
        self.overwrite = overwrite

        # Should have a function to get the current NBN
        self.block_fetcher = BlockFetcher(
            wallet=self.wallet,
            ctx=self.ctx,
            parameters=self.parameters,
        )

        self.network = Network(
            wallet=self.wallet,
            ctx=self.ctx,
            socket_base=socket_base,
            bootnodes=self.bootnodes,
            params=self.network_parameters,
            initial_del_quorum=deepcopy(self.contacts.delegate_quorum_min),
            initial_mn_quorum=deepcopy(self.contacts.masternode_quorum_min),
            mn_to_find=deepcopy(self.contacts.masternodes),
            del_to_find=deepcopy(self.contacts.delegates),
            mn_seed=mn_seed)

        self.nbn_inbox = NBNInbox(socket_id=self.network_parameters.resolve(
            self.socket_base,
            service_type=ServiceType.BLOCK_NOTIFICATIONS,
            bind=True),
                                  ctx=self.ctx,
                                  driver=self.driver,
                                  contacts=self.contacts,
                                  wallet=wallet)

        self.reward_manager = RewardManager(driver=self.driver, debug=True)

        self.running = False
    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)