diff --git a/src/com/google/bitcoin/core/Block.java b/src/com/google/bitcoin/core/Block.java index e75e851b..1e7cbb54 100644 --- a/src/com/google/bitcoin/core/Block.java +++ b/src/com/google/bitcoin/core/Block.java @@ -33,16 +33,12 @@ import static com.google.bitcoin.core.Utils.doubleDigest; import static com.google.bitcoin.core.Utils.doubleDigestTwoBuffers; /** - * A block is the foundation of the BitCoin system. It records a set of - * {@link Transaction}s together with some data that links it into a place in - * the global block chain, and proves that a difficult calculation was done over - * its contents. See the BitCoin technical paper for more detail on blocks. - *
- * - * To get a block, you can either build one from the raw bytes you can get from - * another implementation, or request one specifically using - * {@link Peer#getBlock(Sha256Hash)}, or grab one from a downloaded - * {@link BlockChain}. + * A block is the foundation of the BitCoin system. It records a set of {@link Transaction}s together with some data + * that links it into a place in the global block chain, and proves that a difficult calculation was done over its + * contents. See the BitCoin technical paper for more detail on blocks. + * + * To get a block, you can either build one from the raw bytes you can get from another implementation, or request one + * specifically using {@link Peer#getBlock(Sha256Hash)}, or grab one from a downloaded {@link BlockChain}. */ public class Block extends Message { private static final Logger log = LoggerFactory.getLogger(Block.class); @@ -53,10 +49,7 @@ public class Block extends Message { static final long ALLOWED_TIME_DRIFT = 2 * 60 * 60; // Same value as official client. - /** - * A value for difficultyTarget (nBits) that allows half of all possible - * hash solutions. Used in unit testing. - */ + /** A value for difficultyTarget (nBits) that allows half of all possible hash solutions. Used in unit testing. */ static final long EASIEST_DIFFICULTY_TARGET = 0x207fFFFFL; // For unit testing. If not zero, use this instead of the current time. @@ -82,10 +75,7 @@ public class Block extends Message { private transient boolean headerBytesValid; private transient boolean transactionBytesValid; - /** - * Special case constructor, used for the genesis node, cloneAsHeader and - * unit tests. - */ + /** Special case constructor, used for the genesis node, cloneAsHeader and unit tests. */ Block(NetworkParameters params) { super(params); // Set up a few basic things. We are not complete after this though. @@ -122,10 +112,8 @@ public class Block extends Message { private void readObject(ObjectInputStream ois) throws ClassNotFoundException, IOException { ois.defaultReadObject(); - // This code is not actually necessary, as transient fields are - // initialized to the default value which is in - // this case null. However it clears out a FindBugs warning and makes it - // explicit what we're doing. + // This code is not actually necessary, as transient fields are initialized to the default value which is in + // this case null. However it clears out a FindBugs warning and makes it explicit what we're doing. hash = null; } @@ -168,11 +156,8 @@ public class Block extends Message { transactions.add(tx); cursor += tx.getMessageSize(); } - // no need to set length here. If length was not provided then it should - // be set at the end - // of parseLight(). If this is a genuine lazy parse then length must - // have been provided to - // the constructor. + // No need to set length here. If length was not provided then it should be set at the end of parseLight(). + // If this is a genuine lazy parse then length must have been provided to the constructor. transactionsParsed = true; transactionBytesValid = parseRetain; } @@ -184,11 +169,11 @@ public class Block extends Message { } protected void parseLite() throws ProtocolException { - // ignore the header since it has fixed length. If length is not - // provided we will have to + // Ignore the header since it has fixed length. If length is not provided we will have to // invoke a light parse of transactions to calculate the length. if (length == UNKNOWN_LENGTH) { - assert !parseLazy : "Performing lite parse of block transaction as block was initialised from byte array without providing length. This should never need to happen." + " parseLazy: " + parseLazy; + assert !parseLazy : "Performing lite parse of block transaction as block was initialised from byte array " + + "without providing length. This should never need to happen. parseLazy: " + parseLazy; parseTransactions(); length = cursor - offset; } else { @@ -198,19 +183,16 @@ public class Block extends Message { } /* - * Block uses some special handling for lazy parsing and retention of cached - * bytes. Parsing and serializing the block header and the transaction list - * are both non-trivial so there are good efficiency gains to be had by - * separating them. There are many cases where a user may need access to - * access or change one or the other but not both. + * Block uses some special handling for lazy parsing and retention of cached bytes. Parsing and serializing the + * block header and the transaction list are both non-trivial so there are good efficiency gains to be had by + * separating them. There are many cases where a user may need access to access or change one or the other but not both. * - * With this in mind we ignore the inherited checkParse() and unCache() - * methods and implement a separate version of them for both header and transactions. + * With this in mind we ignore the inherited checkParse() and unCache() methods and implement a separate version + * of them for both header and transactions. * - * Serializing methods are also handled in their own way. Whilst they deal - * with separate parts of the block structure there are some - * interdependencies. For example altering a tx requires invalidating the - * Merkle root and therefore the cached header bytes. + * Serializing methods are also handled in their own way. Whilst they deal with separate parts of the block structure + * there are some interdependencies. For example altering a tx requires invalidating the Merkle root and therefore + * the cached header bytes. */ private synchronized void maybeParseHeader() { if (headerParsed || bytes == null) @@ -238,9 +220,8 @@ public class Block extends Message { } /** - * Ensure the object is parsed if needed. This should be called in every - * getter before returning a value. If the lazy parse flag is not set this - * is a method returns immediately. + * Ensure the object is parsed if needed. This should be called in every getter before returning a value. If the + * lazy parse flag is not set this is a method returns immediately. */ protected synchronized void maybeParse() { throw new LazyParseException( @@ -248,9 +229,10 @@ public class Block extends Message { } /** - * In lazy parsing mode access to getters and setters may throw an unchecked LazyParseException. If guaranteed safe access is required - * this method will force parsing to occur immediately thus ensuring LazyParseExeption will never be thrown from this Message. - * If the Message contains child messages (e.g. a Block containing Transaction messages) this will not force child messages to parse. + * In lazy parsing mode access to getters and setters may throw an unchecked LazyParseException. If guaranteed + * safe access is required this method will force parsing to occur immediately thus ensuring LazyParseExeption will + * never be thrown from this Message. If the Message contains child messages (e.g. a Block containing Transaction + * messages) this will not force child messages to parse. * * This method ensures parsing of both headers and transactions. * @@ -268,9 +250,10 @@ public class Block extends Message { } /** - * In lazy parsing mode access to getters and setters may throw an unchecked LazyParseException. If guaranteed safe access is required - * this method will force parsing to occur immediately thus ensuring LazyParseExeption will never be thrown from this Message. - * If the Message contains child messages (e.g. a Block containing Transaction messages) this will not force child messages to parse. + * In lazy parsing mode access to getters and setters may throw an unchecked LazyParseException. If guaranteed + * safe access is required this method will force parsing to occur immediately thus ensuring LazyParseExeption + * will never be thrown from this Message. If the Message contains child messages (e.g. a Block containing + * Transaction messages) this will not force child messages to parse. * * This method ensures parsing of headers only. * @@ -287,9 +270,10 @@ public class Block extends Message { } /** - * In lazy parsing mode access to getters and setters may throw an unchecked LazyParseException. If guaranteed safe access is required - * this method will force parsing to occur immediately thus ensuring LazyParseExeption will never be thrown from this Message. - * If the Message contains child messages (e.g. a Block containing Transaction messages) this will not force child messages to parse. + * In lazy parsing mode access to getters and setters may throw an unchecked LazyParseException. If guaranteed + * safe access is required this method will force parsing to occur immediately thus ensuring LazyParseExeption will + * never be thrown from this Message. If the Message contains child messages (e.g. a Block containing Transaction + * messages) this will not force child messages to parse. * * This method ensures parsing of transactions only. * @@ -384,11 +368,11 @@ public class Block extends Message { /** * Provides a reasonable guess at the byte length of the transactions part of the block. - * The returned value will be accurate in 99% of cases and in those cases where not will probably - * slightly oversize. - * - * This is used to preallocate the underlying byte array for a ByteArrayOutputStream. If the size - * is under the real value the only penalty is resizing of the underlying byte array. + * The returned value will be accurate in 99% of cases and in those cases where not will probably slightly + * oversize. + * + * This is used to preallocate the underlying byte array for a ByteArrayOutputStream. If the size is under the + * real value the only penalty is resizing of the underlying byte array. */ private int guessTransactionsLength() { if (transactionBytesValid) @@ -404,10 +388,8 @@ public class Block extends Message { } 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. + // 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(); } @@ -425,14 +407,11 @@ public class Block extends Message { transactionBytesValid = false; if (!headerBytesValid) bytes = null; - // current implementation has to uncache headers as well as any change - // to a tx - // will alter the merkle root. In future we can go more granular and - // cache merkle root - // Separately so rest of the header does not need to be rewritten + // Current implementation has to uncache headers as well as any change to a tx will alter the merkle root. In + // future we can go more granular and cache merkle root separately so rest of the header does not need to be + // rewritten. unCacheHeader(); - // clear merkleRoot last as it may end up being parsed during - // unCacheHeader(). + // Clear merkleRoot last as it may end up being parsed during unCacheHeader(). merkleRoot = null; } @@ -451,10 +430,9 @@ public class Block extends Message { } /** - * Returns the hash of the block (which for a valid, solved block should be - * below the target) in the form seen on the block explorer. If you call - * this on block 1 in the production chain, you will get - * "00000000839a8e6886ab5951d76f411475428afc90947ee320161bbf18eb6048". + * Returns the hash of the block (which for a valid, solved block should be below the target) in the form seen on + * the block explorer. If you call this on block 1 in the production chain + * you will get "00000000839a8e6886ab5951d76f411475428afc90947ee320161bbf18eb6048". */ public String getHashAsString() { return getHash().toString(); @@ -524,13 +502,11 @@ public class Block extends Message { } /** - * Finds a value of nonce that makes the blocks hash lower than the - * difficulty target. This is called mining, but solve() is far too slow to - * do real mining with. It exists only for unit testing purposes and is not - * a part of the public API. + * Finds a value of nonce that makes the blocks hash lower than the difficulty target. This is called mining, but + * solve() is far too slow to do real mining with. It exists only for unit testing purposes and is not a part of + * the public API. * - * This can loop forever if a solution cannot be found solely by - * incrementing nonce. It doesn't change extraNonce. + * This can loop forever if a solution cannot be found solely by incrementing nonce. It doesn't change extraNonce. */ void solve() { maybeParseHeader(); @@ -548,9 +524,8 @@ public class Block extends Message { } /** - * Returns the difficulty target as a 256 bit value that can be compared to - * a SHA-256 hash. Inside a block the target is represented using a compact - * form. If this form decodes to a value that is out of bounds, an exception + * Returns the difficulty target as a 256 bit value that can be compared to a SHA-256 hash. Inside a block the + * target is represented using a compact form. If this form decodes to a value that is out of bounds, an exception * is thrown. */ public BigInteger getDifficultyTargetAsInteger() throws VerificationException { @@ -561,25 +536,16 @@ public class Block extends Message { return target; } - /** - * Returns true if the hash of the block is OK (lower than difficulty - * target). - */ + /** Returns true if the hash of the block is OK (lower than difficulty target). */ private boolean checkProofOfWork(boolean throwException) throws VerificationException { - // This part is key - it is what proves the block was as difficult to - // make as it claims - // to be. Note however that in the context of this function, the block - // can claim to be - // as difficult as it wants to be .... if somebody was able to take - // control of our network - // connection and fork us onto a different chain, they could send us - // valid blocks with + // This part is key - it is what proves the block was as difficult to make as it claims + // to be. Note however that in the context of this function, the block can claim to be + // as difficult as it wants to be .... if somebody was able to take control of our network + // connection and fork us onto a different chain, they could send us valid blocks with // ridiculously easy difficulty and this function would accept them. // - // To prevent this attack from being possible, elsewhere we check that - // the difficultyTarget - // field is of the right value. This requires us to have the preceeding - // blocks. + // To prevent this attack from being possible, elsewhere we check that the difficultyTarget + // field is of the right value. This requires us to have the preceeding blocks. BigInteger target = getDifficultyTargetAsInteger(); BigInteger h = getHash().toBigInteger(); @@ -616,8 +582,7 @@ public class Block extends Message { } private List