/**
   * To call when a submit request is received from a worker connection.
   *
   * @param workerConnection
   * @param workerRequest
   */
  public void onSubmitRequest(
      final WorkerConnection workerConnection, final MiningSubmitRequest workerRequest) {
    if (workerConnection.getPool() != null && workerConnection.getPool().isReady()) {
      for (int i = 0; i < workerConnection.getPool().getNumberOfSubmit(); i++) {
        workerConnection
            .getPool()
            .submitShare(
                workerRequest,
                new ResponseReceivedCallback<MiningSubmitRequest, MiningSubmitResponse>() {
                  public void onResponseReceived(
                      MiningSubmitRequest request, MiningSubmitResponse response) {
                    updateShareLists(workerRequest, response, workerConnection);
                    workerConnection.onPoolSubmitResponse(workerRequest, response);
                  }
                });
      }
    } else {
      LOGGER.warn(
          "REJECTED share. Share submit from {}@{} dropped since pool {} is not ready.",
          workerRequest.getWorkerName(),
          workerConnection.getConnectionName(),
          workerConnection.getPool());

      // Notify the worker that the target pool is no more ready
      MiningSubmitResponse fakePoolResponse = new MiningSubmitResponse();
      fakePoolResponse.setId(workerRequest.getId());
      fakePoolResponse.setIsAccepted(false);
      JsonRpcError error = new JsonRpcError();
      error.setCode(JsonRpcError.ErrorCode.UNKNOWN.getCode());
      error.setMessage("The target pool is no more ready.");
      fakePoolResponse.setErrorRpc(error);
      workerConnection.onPoolSubmitResponse(workerRequest, fakePoolResponse);
    }
  }
  /**
   * Update the share lists of all pools, users and worker connections.
   *
   * @param request
   * @param response
   * @param workerConnection
   */
  private void updateShareLists(
      MiningSubmitRequest request,
      MiningSubmitResponse response,
      WorkerConnection workerConnection) {
    if (workerConnection.getPool() != null) {
      Share share = new Share();
      share.setDifficulty(workerConnection.getPool().getDifficulty());
      share.setTime(System.currentTimeMillis());

      boolean isAccepted = response.getIsAccepted() != null && response.getIsAccepted();

      workerConnection.updateShareLists(share, isAccepted);

      workerConnection.getPool().updateShareLists(share, isAccepted);

      User user = users.get(request.getWorkerName());
      if (user != null) {
        user.updateShareLists(share, isAccepted);
      }
    }
  }