code: Block createNextBlock(@Nullable final Address to, final long version,
                            @Nullable TransactionOutPoint prevOut, final long time,
                            final byte[] pubKey, final Coin coinbaseValue,
                            final int height) {
          Block b = new Block(params, version);
          b.setDifficultyTarget(difficultyTarget);
          b.addCoinbaseTransaction(pubKey, coinbaseValue, height);
  
          if (to != null) {
              // Add a transaction paying 50 coins to the "to" address.
              Transaction t = new Transaction(params);
              t.addOutput(new TransactionOutput(params, t, FIFTY_COINS, to));
              // The input does not really need to be a valid signature, as long as it has the right general form.
              TransactionInput input;
              if (prevOut == null) {
                  input = new TransactionInput(params, t, Script.createInputScript(EMPTY_BYTES, EMPTY_BYTES));
                  // Importantly the outpoint hash cannot be zero as that's how we detect a coinbase transaction in isolation
                  // but it must be unique to avoid 'different' transactions looking the same.
                  byte[] counter = new byte[32];
                  counter[0] = (byte) txCounter;
                  counter[1] = (byte) (txCounter++ >> 8);
                  input.getOutpoint().setHash(Sha256Hash.wrap(counter));
              } else {
                  input = new TransactionInput(params, t, Script.createInputScript(EMPTY_BYTES, EMPTY_BYTES), prevOut);
              }
              t.addInput(input);
              b.addTransaction(t);
          }
  
          b.setPrevBlockHash(getHash());
          // Don't let timestamp go backwards
          if (getTimeSeconds() >= time)
              b.setTime(getTimeSeconds() + 1);
          else
              b.setTime(time);
          b.solve();
          try {
              b.verifyHeader();
          } catch (VerificationException e) {
              throw new RuntimeException(e); // Cannot happen.
          }
          if (b.getVersion() != version) {
              throw new RuntimeException();
          }
          return b;
      }
docs: Returns a solved block that builds on top of this one. This exists for unit tests.
  In this variant you can specify a public key (pubkey) for use in generating coinbase blocks.
  
  @param height block height, if known, or -1 otherwise.