/** Adds a transaction to this block, with or without checking the sanity of doing so */ void addTransaction(Transaction t, boolean runSanityChecks) { unCacheTransactions(); if (transactions == null) { transactions = new ArrayList<Transaction>(); } t.setParent(this); if (runSanityChecks && transactions.size() == 0 && !t.isCoinBase()) throw new RuntimeException( "Attempted to add a non-coinbase transaction as the first transaction: " + t); else if (runSanityChecks && transactions.size() > 0 && t.isCoinBase()) throw new RuntimeException( "Attempted to add a coinbase transaction when there already is one: " + t); transactions.add(t); adjustLength(transactions.size(), t.length); // Force a recalculation next time the values are needed. merkleRoot = null; hash = null; }
/** Adds a coinbase transaction to the block. This exists for unit tests. */ void addCoinbaseTransaction(byte[] pubKeyTo, BigInteger value) { unCacheTransactions(); transactions = new ArrayList<Transaction>(); Transaction coinbase = new Transaction(params); // A real coinbase transaction has some stuff in the scriptSig like the extraNonce and // difficulty. The // transactions are distinguished by every TX output going to a different key. // // Here we will do things a bit differently so a new address isn't needed every time. We'll put // a simple // counter in the scriptSig so every transaction has a different hash. coinbase.addInput( new TransactionInput(params, coinbase, new byte[] {(byte) txCounter++, (byte) 1})); coinbase.addOutput( new TransactionOutput(params, coinbase, value, Script.createOutputScript(pubKeyTo))); transactions.add(coinbase); coinbase.setParent(this); coinbase.length = coinbase.litecoinSerialize().length; adjustLength(transactions.size(), coinbase.length); }
protected void unCache() { // Since we have alternate uncache methods to use internally this will only ever be called by a // child // transaction so we only need to invalidate that part of the cache. unCacheTransactions(); }