Ejemplo n.º 1
0
def add(dispatcher,
        gossip,
        context_manager,
        executor,
        completer,
        block_store,
        batch_tracker,
        merkle_db,
        get_current_root,
        receipt_store,
        event_broadcaster,
        permission_verifier,
        thread_pool,
        sig_pool,
        block_publisher,
        metrics_registry=None):

    # -- Transaction Processor -- #
    dispatcher.add_handler(
        validator_pb2.Message.TP_RECEIPT_ADD_DATA_REQUEST,
        tp_state_handlers.TpReceiptAddDataHandler(context_manager),
        thread_pool)

    dispatcher.add_handler(
        validator_pb2.Message.TP_EVENT_ADD_REQUEST,
        tp_state_handlers.TpEventAddHandler(context_manager), thread_pool)

    dispatcher.add_handler(
        validator_pb2.Message.TP_STATE_DELETE_REQUEST,
        tp_state_handlers.TpStateDeleteHandler(context_manager), thread_pool)

    dispatcher.add_handler(
        validator_pb2.Message.TP_STATE_GET_REQUEST,
        tp_state_handlers.TpStateGetHandler(context_manager), thread_pool)

    dispatcher.add_handler(
        validator_pb2.Message.TP_STATE_SET_REQUEST,
        tp_state_handlers.TpStateSetHandler(context_manager), thread_pool)

    dispatcher.add_handler(
        validator_pb2.Message.TP_REGISTER_REQUEST,
        processor_handlers.ProcessorRegisterHandler(executor.processors),
        thread_pool)

    dispatcher.add_handler(
        validator_pb2.Message.TP_UNREGISTER_REQUEST,
        processor_handlers.ProcessorUnRegisterHandler(executor.processors),
        thread_pool)

    # -- Client -- #
    dispatcher.add_handler(
        validator_pb2.Message.CLIENT_BATCH_SUBMIT_REQUEST,
        BatchListPermissionVerifier(permission_verifier=permission_verifier),
        sig_pool)

    # Submit
    dispatcher.add_handler(
        validator_pb2.Message.CLIENT_BATCH_SUBMIT_REQUEST,
        ClientBatchSubmitBackpressureHandler(
            block_publisher.can_accept_batch,
            block_publisher.get_current_queue_info,
            metrics_registry=metrics_registry), thread_pool)

    dispatcher.add_handler(validator_pb2.Message.CLIENT_BATCH_SUBMIT_REQUEST,
                           signature_verifier.BatchListSignatureVerifier(),
                           sig_pool)

    dispatcher.add_handler(validator_pb2.Message.CLIENT_BATCH_SUBMIT_REQUEST,
                           structure_verifier.BatchListStructureVerifier(),
                           thread_pool)

    dispatcher.add_handler(
        validator_pb2.Message.CLIENT_BATCH_SUBMIT_REQUEST,
        CompleterBatchListBroadcastHandler(completer, gossip), thread_pool)

    dispatcher.add_handler(validator_pb2.Message.CLIENT_BATCH_SUBMIT_REQUEST,
                           client_handlers.BatchSubmitFinisher(batch_tracker),
                           thread_pool)

    dispatcher.add_handler(validator_pb2.Message.CLIENT_BATCH_STATUS_REQUEST,
                           client_handlers.BatchStatusRequest(batch_tracker),
                           thread_pool)

    # State
    dispatcher.add_handler(
        validator_pb2.Message.CLIENT_STATE_LIST_REQUEST,
        client_handlers.StateListRequest(merkle_db, block_store), thread_pool)

    dispatcher.add_handler(
        validator_pb2.Message.CLIENT_STATE_GET_REQUEST,
        client_handlers.StateGetRequest(merkle_db, block_store), thread_pool)

    # Blocks
    dispatcher.add_handler(validator_pb2.Message.CLIENT_BLOCK_LIST_REQUEST,
                           client_handlers.BlockListRequest(block_store),
                           thread_pool)

    dispatcher.add_handler(
        validator_pb2.Message.CLIENT_BLOCK_GET_BY_ID_REQUEST,
        client_handlers.BlockGetByIdRequest(block_store), thread_pool)

    dispatcher.add_handler(
        validator_pb2.Message.CLIENT_BLOCK_GET_BY_NUM_REQUEST,
        client_handlers.BlockGetByNumRequest(block_store), thread_pool)

    # Batches
    dispatcher.add_handler(validator_pb2.Message.CLIENT_BATCH_LIST_REQUEST,
                           client_handlers.BatchListRequest(block_store),
                           thread_pool)

    dispatcher.add_handler(validator_pb2.Message.CLIENT_BATCH_GET_REQUEST,
                           client_handlers.BatchGetRequest(block_store),
                           thread_pool)

    # Transactions
    dispatcher.add_handler(
        validator_pb2.Message.CLIENT_TRANSACTION_LIST_REQUEST,
        client_handlers.TransactionListRequest(block_store), thread_pool)

    dispatcher.add_handler(
        validator_pb2.Message.CLIENT_TRANSACTION_GET_REQUEST,
        client_handlers.TransactionGetRequest(block_store), thread_pool)

    # Receipts
    dispatcher.add_handler(validator_pb2.Message.CLIENT_RECEIPT_GET_REQUEST,
                           ClientReceiptGetRequestHandler(receipt_store),
                           thread_pool)

    # Events
    dispatcher.add_handler(
        validator_pb2.Message.CLIENT_EVENTS_SUBSCRIBE_REQUEST,
        ClientEventsSubscribeValidationHandler(event_broadcaster), thread_pool)

    dispatcher.add_handler(
        validator_pb2.Message.CLIENT_EVENTS_SUBSCRIBE_REQUEST,
        ClientEventsSubscribeHandler(event_broadcaster), thread_pool)

    dispatcher.add_handler(
        validator_pb2.Message.CLIENT_EVENTS_UNSUBSCRIBE_REQUEST,
        ClientEventsUnsubscribeHandler(event_broadcaster), thread_pool)

    dispatcher.add_handler(validator_pb2.Message.CLIENT_EVENTS_GET_REQUEST,
                           ClientEventsGetRequestHandler(event_broadcaster),
                           thread_pool)

    dispatcher.add_handler(validator_pb2.Message.CLIENT_PEERS_GET_REQUEST,
                           client_handlers.PeersGetRequest(gossip),
                           thread_pool)
Ejemplo n.º 2
0
def verify_state(global_state_db, blockstore, bind_component, scheduler_type):
    """
    Verify the state root hash of all blocks is in state and if not,
    reconstruct the missing state. Assumes that there are no "holes" in
    state, ie starting from genesis, state is present for all blocks up to some
    point and then not at all. If persist is False, this recomputes state in
    memory for all blocks in the blockstore and verifies the state root
    hashes.

    Raises:
        InvalidChainError: The chain in the blockstore is not valid.
        ExecutionError: An unrecoverable error was encountered during batch
            execution.
    """
    state_view_factory = StateViewFactory(global_state_db)

    # Check if we should do state verification
    start_block, prev_state_root = search_for_present_state_root(
        blockstore, state_view_factory)

    if start_block is None:
        LOGGER.info(
            "Skipping state verification: chain head's state root is present")
        return

    LOGGER.info(
        "Recomputing missing state from block %s with %s scheduler",
        start_block, scheduler_type)

    component_thread_pool = InstrumentedThreadPoolExecutor(
        max_workers=10,
        name='Component')

    component_dispatcher = Dispatcher()
    component_service = Interconnect(
        bind_component,
        component_dispatcher,
        secured=False,
        heartbeat=False,
        max_incoming_connections=20,
        monitor=True,
        max_future_callback_workers=10)

    context_manager = ContextManager(global_state_db)

    transaction_executor = TransactionExecutor(
        service=component_service,
        context_manager=context_manager,
        settings_view_factory=SettingsViewFactory(state_view_factory),
        scheduler_type=scheduler_type,
        invalid_observers=[])

    component_service.set_check_connections(
        transaction_executor.check_connections)

    component_dispatcher.add_handler(
        validator_pb2.Message.TP_RECEIPT_ADD_DATA_REQUEST,
        tp_state_handlers.TpReceiptAddDataHandler(context_manager),
        component_thread_pool)

    component_dispatcher.add_handler(
        validator_pb2.Message.TP_EVENT_ADD_REQUEST,
        tp_state_handlers.TpEventAddHandler(context_manager),
        component_thread_pool)

    component_dispatcher.add_handler(
        validator_pb2.Message.TP_STATE_DELETE_REQUEST,
        tp_state_handlers.TpStateDeleteHandler(context_manager),
        component_thread_pool)

    component_dispatcher.add_handler(
        validator_pb2.Message.TP_STATE_GET_REQUEST,
        tp_state_handlers.TpStateGetHandler(context_manager),
        component_thread_pool)

    component_dispatcher.add_handler(
        validator_pb2.Message.TP_STATE_SET_REQUEST,
        tp_state_handlers.TpStateSetHandler(context_manager),
        component_thread_pool)

    component_dispatcher.add_handler(
        validator_pb2.Message.TP_REGISTER_REQUEST,
        processor_handlers.ProcessorRegisterValidationHandler(),
        component_thread_pool)

    component_dispatcher.add_handler(
        validator_pb2.Message.TP_REGISTER_REQUEST,
        processor_handlers.ProcessorRegisterHandler(
            transaction_executor.processor_manager),
        component_thread_pool)

    component_dispatcher.add_handler(
        validator_pb2.Message.TP_UNREGISTER_REQUEST,
        processor_handlers.ProcessorUnRegisterHandler(
            transaction_executor.processor_manager),
        component_thread_pool)

    component_dispatcher.start()
    component_service.start()

    process_blocks(
        initial_state_root=prev_state_root,
        blocks=blockstore.get_block_iter(
            start_block=start_block, reverse=False),
        transaction_executor=transaction_executor,
        context_manager=context_manager,
        state_view_factory=state_view_factory)

    component_dispatcher.stop()
    component_service.stop()
    component_thread_pool.shutdown(wait=True)
    transaction_executor.stop()
    context_manager.stop()