Beispiel #1
0
  /**
   * 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.
   */
  Block createNextBlock(
      Address to, TransactionOutPoint prevOut, long time, byte[] pubKey, BigInteger coinbaseValue) {
    Block b = new Block(params);
    b.setDifficultyTarget(difficultyTarget);
    b.addCoinbaseTransaction(pubKey, coinbaseValue);

    if (to != null) {
      // Add a transaction paying 50 coins to the "to" address.
      Transaction t = new Transaction(params);
      t.addOutput(new TransactionOutput(params, t, Utils.toNanoCoins(50, 0), 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] = 1;
        input.getOutpoint().setHash(new Sha256Hash(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.
    }
    return b;
  }
Beispiel #2
0
 /** Verifies both the header and that the transactions hash to the merkle root. */
 public void verify() throws VerificationException {
   verifyHeader();
   verifyTransactions();
 }