diff --git a/core/src/main/java/com/google/bitcoin/core/Base58.java b/core/src/main/java/com/google/bitcoin/core/Base58.java index 8584e76b..8718d301 100644 --- a/core/src/main/java/com/google/bitcoin/core/Base58.java +++ b/core/src/main/java/com/google/bitcoin/core/Base58.java @@ -35,54 +35,54 @@ import java.util.Arrays; * */ public class Base58 { - private static final char[] ALPHABET = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz" - .toCharArray(); - private static final int BASE_58 = ALPHABET.length; - private static final int BASE_256 = 256; + private static final char[] ALPHABET = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz" + .toCharArray(); + private static final int BASE_58 = ALPHABET.length; + private static final int BASE_256 = 256; - private static final int[] INDEXES = new int[128]; - static { - for (int i = 0; i < INDEXES.length; i++) { - INDEXES[i] = -1; - } - for (int i = 0; i < ALPHABET.length; i++) { - INDEXES[ALPHABET[i]] = i; - } - } - - public static String encode(byte[] input) { - if (input.length == 0) { - return ""; - } - input = copyOfRange(input, 0, input.length); - // Count leading zeroes. - int zeroCount = 0; - while (zeroCount < input.length && input[zeroCount] == 0) { - ++zeroCount; - } - // The actual encoding. - byte[] temp = new byte[input.length * 2]; - int j = temp.length; + private static final int[] INDEXES = new int[128]; + static { + for (int i = 0; i < INDEXES.length; i++) { + INDEXES[i] = -1; + } + for (int i = 0; i < ALPHABET.length; i++) { + INDEXES[ALPHABET[i]] = i; + } + } + + public static String encode(byte[] input) { + if (input.length == 0) { + return ""; + } + input = copyOfRange(input, 0, input.length); + // Count leading zeroes. + int zeroCount = 0; + while (zeroCount < input.length && input[zeroCount] == 0) { + ++zeroCount; + } + // The actual encoding. + byte[] temp = new byte[input.length * 2]; + int j = temp.length; - int startAt = zeroCount; - while (startAt < input.length) { - byte mod = divmod58(input, startAt); - if (input[startAt] == 0) { - ++startAt; - } - temp[--j] = (byte) ALPHABET[mod]; - } + int startAt = zeroCount; + while (startAt < input.length) { + byte mod = divmod58(input, startAt); + if (input[startAt] == 0) { + ++startAt; + } + temp[--j] = (byte) ALPHABET[mod]; + } - // Strip extra '1' if there are some after decoding. - while (j < temp.length && temp[j] == ALPHABET[0]) { - ++j; - } - // Add as many leading '1' as there were leading zeros. - while (--zeroCount >= 0) { - temp[--j] = (byte) ALPHABET[0]; - } + // Strip extra '1' if there are some after decoding. + while (j < temp.length && temp[j] == ALPHABET[0]) { + ++j; + } + // Add as many leading '1' as there were leading zeros. + while (--zeroCount >= 0) { + temp[--j] = (byte) ALPHABET[0]; + } - byte[] output = copyOfRange(temp, j, temp.length); + byte[] output = copyOfRange(temp, j, temp.length); try { return new String(output, "US-ASCII"); } catch (UnsupportedEncodingException e) { @@ -90,69 +90,69 @@ public class Base58 { } } - public static byte[] decode(String input) throws AddressFormatException { - if (input.length() == 0) { - return new byte[0]; - } + public static byte[] decode(String input) throws AddressFormatException { + if (input.length() == 0) { + return new byte[0]; + } - byte[] input58 = new byte[input.length()]; - // - // Transform the String to a base58 byte sequence - // - for (int i = 0; i < input.length(); ++i) { - char c = input.charAt(i); + byte[] input58 = new byte[input.length()]; + // + // Transform the String to a base58 byte sequence + // + for (int i = 0; i < input.length(); ++i) { + char c = input.charAt(i); - int digit58 = -1; - if (c >= 0 && c < 128) { - digit58 = INDEXES[c]; - } - if (digit58 < 0) { - throw new AddressFormatException("Illegal character " + c + " at " + i); - } + int digit58 = -1; + if (c >= 0 && c < 128) { + digit58 = INDEXES[c]; + } + if (digit58 < 0) { + throw new AddressFormatException("Illegal character " + c + " at " + i); + } - input58[i] = (byte) digit58; - } + input58[i] = (byte) digit58; + } - // - // Count leading zeroes - // - int zeroCount = 0; - while (zeroCount < input58.length && input58[zeroCount] == 0) { - ++zeroCount; - } + // + // Count leading zeroes + // + int zeroCount = 0; + while (zeroCount < input58.length && input58[zeroCount] == 0) { + ++zeroCount; + } - // - // The encoding - // - byte[] temp = new byte[input.length()]; - int j = temp.length; + // + // The encoding + // + byte[] temp = new byte[input.length()]; + int j = temp.length; - int startAt = zeroCount; - while (startAt < input58.length) { - byte mod = divmod256(input58, startAt); - if (input58[startAt] == 0) { - ++startAt; - } + int startAt = zeroCount; + while (startAt < input58.length) { + byte mod = divmod256(input58, startAt); + if (input58[startAt] == 0) { + ++startAt; + } - temp[--j] = mod; - } + temp[--j] = mod; + } - // - // Do no add extra leading zeroes, move j to first non null byte. - // - while (j < temp.length && temp[j] == 0) { - ++j; - } + // + // Do no add extra leading zeroes, move j to first non null byte. + // + while (j < temp.length && temp[j] == 0) { + ++j; + } - return copyOfRange(temp, j - zeroCount, temp.length); - } - - public static BigInteger decodeToBigInteger(String input) throws AddressFormatException { - byte[] bytes = decode(input); - - // always return a positive BigInteger - return new BigInteger(1, bytes); - } + return copyOfRange(temp, j - zeroCount, temp.length); + } + + public static BigInteger decodeToBigInteger(String input) throws AddressFormatException { + byte[] bytes = decode(input); + + // always return a positive BigInteger + return new BigInteger(1, bytes); + } /** * Uses the checksum in the last 4 bytes of the decoded data to verify the rest are correct. The checksum is @@ -160,60 +160,60 @@ public class Base58 { * * @throws AddressFormatException if the input is not base 58 or the checksum does not validate. */ - public static byte[] decodeChecked(String input) throws AddressFormatException { - byte tmp [] = decode(input); - if (tmp.length < 4) - throw new AddressFormatException("Input to short"); - byte[] bytes = copyOfRange(tmp, 0, tmp.length - 4); - byte[] checksum = copyOfRange(tmp, tmp.length - 4, tmp.length); - - tmp = Utils.doubleDigest(bytes); - byte[] hash = copyOfRange(tmp, 0, 4); - if (!Arrays.equals(checksum, hash)) - throw new AddressFormatException("Checksum does not validate"); - - return bytes; - } - - // - // number -> number / 58, returns number % 58 - // - private static byte divmod58(byte[] number, int startAt) { - int remainder = 0; - for (int i = startAt; i < number.length; i++) { - int digit256 = (int) number[i] & 0xFF; - int temp = remainder * BASE_256 + digit256; + public static byte[] decodeChecked(String input) throws AddressFormatException { + byte tmp [] = decode(input); + if (tmp.length < 4) + throw new AddressFormatException("Input to short"); + byte[] bytes = copyOfRange(tmp, 0, tmp.length - 4); + byte[] checksum = copyOfRange(tmp, tmp.length - 4, tmp.length); + + tmp = Utils.doubleDigest(bytes); + byte[] hash = copyOfRange(tmp, 0, 4); + if (!Arrays.equals(checksum, hash)) + throw new AddressFormatException("Checksum does not validate"); + + return bytes; + } + + // + // number -> number / 58, returns number % 58 + // + private static byte divmod58(byte[] number, int startAt) { + int remainder = 0; + for (int i = startAt; i < number.length; i++) { + int digit256 = (int) number[i] & 0xFF; + int temp = remainder * BASE_256 + digit256; - number[i] = (byte) (temp / BASE_58); + number[i] = (byte) (temp / BASE_58); - remainder = temp % BASE_58; - } + remainder = temp % BASE_58; + } - return (byte) remainder; - } + return (byte) remainder; + } - // - // number -> number / 256, returns number % 256 - // - private static byte divmod256(byte[] number58, int startAt) { - int remainder = 0; - for (int i = startAt; i < number58.length; i++) { - int digit58 = (int) number58[i] & 0xFF; - int temp = remainder * BASE_58 + digit58; + // + // number -> number / 256, returns number % 256 + // + private static byte divmod256(byte[] number58, int startAt) { + int remainder = 0; + for (int i = startAt; i < number58.length; i++) { + int digit58 = (int) number58[i] & 0xFF; + int temp = remainder * BASE_58 + digit58; - number58[i] = (byte) (temp / BASE_256); + number58[i] = (byte) (temp / BASE_256); - remainder = temp % BASE_256; - } + remainder = temp % BASE_256; + } - return (byte) remainder; - } + return (byte) remainder; + } - private static byte[] copyOfRange(byte[] source, int from, int to) { - byte[] range = new byte[to - from]; - System.arraycopy(source, from, range, 0, range.length); + private static byte[] copyOfRange(byte[] source, int from, int to) { + byte[] range = new byte[to - from]; + System.arraycopy(source, from, range, 0, range.length); - return range; - } - + return range; + } + } diff --git a/core/src/main/java/com/google/bitcoin/core/Block.java b/core/src/main/java/com/google/bitcoin/core/Block.java index 5c8f9432..84bb0325 100644 --- a/core/src/main/java/com/google/bitcoin/core/Block.java +++ b/core/src/main/java/com/google/bitcoin/core/Block.java @@ -47,7 +47,7 @@ public class Block extends Message { private static final Logger log = LoggerFactory.getLogger(Block.class); private static final long serialVersionUID = 2738848929966035281L; - /** How many bytes are required to represent a block header. */ + /** How many bytes are required to represent a block header. */ public static final int HEADER_SIZE = 80; static final long ALLOWED_TIME_DRIFT = 2 * 60 * 60; // Same value as official client. @@ -79,7 +79,7 @@ public class Block extends Message { private long difficultyTarget; // "nBits" private long nonce; - /** If null, it means this object holds only the headers. */ + /** If null, it means this object holds only the headers. */ List transactions; /** Stores the hash of the block. If null, getHash() will recalculate it. */ @@ -103,7 +103,7 @@ public class Block extends Message { length = 80; } - /** Constructs a block object from the Bitcoin wire format. */ + /** Constructs a block object from the Bitcoin wire format. */ public Block(NetworkParameters params, byte[] payloadBytes) throws ProtocolException { super(params, payloadBytes, 0, false, false, payloadBytes.length); } @@ -145,7 +145,6 @@ public class Block extends Message { } private void parseHeader() { - if (headerParsed) return; @@ -164,7 +163,6 @@ public class Block extends Message { } private void parseTransactions() throws ProtocolException { - if (transactionsParsed) return; @@ -409,7 +407,7 @@ public class Block extends Message { return 0; int len = VarInt.sizeOf(transactions.size()); for (Transaction tx : transactions) { - // 255 is just a guess at an average tx length + // 255 is just a guess at an average tx length len += tx.length == UNKNOWN_LENGTH ? 255 : tx.length; } return len; @@ -495,7 +493,7 @@ public class Block extends Message { return LARGEST_HASH.divide(target.add(BigInteger.ONE)); } - /** Returns a copy of the block, but without any transactions. */ + /** Returns a copy of the block, but without any transactions. */ public Block cloneAsHeader() { maybeParseHeader(); Block block = new Block(params); @@ -533,7 +531,7 @@ 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. - * + * * This can loop forever if a solution cannot be found solely by incrementing nonce. It doesn't change extraNonce. */ void solve() { @@ -595,7 +593,7 @@ public class Block extends Message { if (time > currentTime + ALLOWED_TIME_DRIFT) throw new VerificationException("Block too far in future"); } - + private void checkSigOps() throws VerificationException { // Check there aren't too many signature verifications in the block. This is an anti-DoS measure, see the // comments for MAX_BLOCK_SIGOPS. @@ -763,18 +761,18 @@ public class Block extends Message { return merkleRoot; } - /** Exists only for unit testing. */ + /** Exists only for unit testing. */ void setMerkleRoot(Sha256Hash value) { unCacheHeader(); merkleRoot = value; hash = null; } - /** Adds a transaction to this block. The nonce and merkle root are invalid after this. */ + /** Adds a transaction to this block. The nonce and merkle root are invalid after this. */ public void addTransaction(Transaction t) { addTransaction(t, true); } - + /** Adds a transaction to this block, with or without checking the sanity of doing so */ void addTransaction(Transaction t, boolean runSanityChecks) { unCacheTransactions(); @@ -877,7 +875,7 @@ public class Block extends Message { // Used to make transactions unique. static private int txCounter; - /** Adds a coinbase transaction to the block. This exists for unit tests. */ + /** Adds a coinbase transaction to the block. This exists for unit tests. */ void addCoinbaseTransaction(byte[] pubKeyTo, BigInteger value) { unCacheTransactions(); transactions = new ArrayList(); @@ -953,12 +951,12 @@ public class Block extends Message { public Block createNextBlock(Address to, TransactionOutPoint prevOut) { return createNextBlock(to, prevOut, Utils.now().getTime() / 1000, EMPTY_BYTES, Utils.toNanoCoins(50, 0)); } - + // Visible for testing. public Block createNextBlock(Address to) { return createNextBlock(to, null, Utils.now().getTime() / 1000, EMPTY_BYTES, Utils.toNanoCoins(50, 0)); } - + // Visible for testing. public Block createNextBlockWithCoinbase(byte[] pubKey, BigInteger coinbaseValue) { return createNextBlock(null, null, Utils.now().getTime() / 1000, pubKey, coinbaseValue); diff --git a/core/src/main/java/com/google/bitcoin/core/InventoryItem.java b/core/src/main/java/com/google/bitcoin/core/InventoryItem.java index 7c79da3c..99dafa2e 100644 --- a/core/src/main/java/com/google/bitcoin/core/InventoryItem.java +++ b/core/src/main/java/com/google/bitcoin/core/InventoryItem.java @@ -18,12 +18,12 @@ package com.google.bitcoin.core; public class InventoryItem { - /** - * 4 byte uint32 type field + 32 byte hash - */ - static final int MESSAGE_LENGTH = 36; - - public enum Type { + /** + * 4 byte uint32 type field + 32 byte hash + */ + static final int MESSAGE_LENGTH = 36; + + public enum Type { Error, Transaction, Block diff --git a/core/src/main/java/com/google/bitcoin/core/Peer.java b/core/src/main/java/com/google/bitcoin/core/Peer.java index a6eacec6..e4e7ae38 100644 --- a/core/src/main/java/com/google/bitcoin/core/Peer.java +++ b/core/src/main/java/com/google/bitcoin/core/Peer.java @@ -201,7 +201,7 @@ public class Peer { @Override public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) throws Exception { Message m = (Message)e.getMessage(); - + // Allow event listeners to filter the message stream. Listeners are allowed to drop messages by // returning null. synchronized (Peer.this) { @@ -277,7 +277,7 @@ public class Peer { log.error("Failed to check signature: bug in platform libraries?", t); } } - + /** Returns the Netty Pipeline stage handling the high level Bitcoin protocol. */ public PeerHandler getHandler() { return handler; @@ -325,7 +325,7 @@ public class Peer { throw new RuntimeException(e); } } - + private synchronized void processGetData(GetDataMessage getdata) throws IOException { log.info("{}: Received getdata message: {}", address, getdata.toString()); ArrayList items = new ArrayList(); @@ -546,7 +546,7 @@ public class Peer { synchronized (pendingGetBlockFutures) { pendingGetBlockFutures.add(future); } - + sendMessage(getdata); return future; } @@ -574,7 +574,7 @@ public class Peer { } } } - + /** * Links the given wallet to this peer. If you have multiple peers, you should use a {@link PeerGroup} to manage * them and use the {@link PeerGroup#addWallet(Wallet)} method instead of registering the wallet with each peer @@ -881,14 +881,14 @@ public class Peer { public void setDownloadData(boolean downloadData) { this.downloadData = downloadData; } - + /** * @return the IP address and port of peer. */ public PeerAddress getAddress() { return address; } - + /** * @return various version numbers claimed by peer. */ diff --git a/core/src/main/java/com/google/bitcoin/core/PeerAddress.java b/core/src/main/java/com/google/bitcoin/core/PeerAddress.java index cac93104..16bd5804 100644 --- a/core/src/main/java/com/google/bitcoin/core/PeerAddress.java +++ b/core/src/main/java/com/google/bitcoin/core/PeerAddress.java @@ -153,7 +153,7 @@ public class PeerAddress extends ChildMessage { */ @Override public int getMessageSize() { - // The 4 byte difference is the uint32 timestamp that was introduced in version 31402 + // The 4 byte difference is the uint32 timestamp that was introduced in version 31402 length = protocolVersion > 31402 ? MESSAGE_SIZE : MESSAGE_SIZE - 4; return length; } diff --git a/core/src/main/java/com/google/bitcoin/core/PeerGroup.java b/core/src/main/java/com/google/bitcoin/core/PeerGroup.java index bbdf8432..50157321 100644 --- a/core/src/main/java/com/google/bitcoin/core/PeerGroup.java +++ b/core/src/main/java/com/google/bitcoin/core/PeerGroup.java @@ -229,7 +229,7 @@ public class PeerGroup { ver.time = Utils.now().getTime() / 1000; ChannelPipeline p = Channels.pipeline(); - + Peer peer = new Peer(params, chain, ver); peer.addLifecycleListener(startupListener); pendingPeers.add(peer); @@ -376,7 +376,7 @@ public class PeerGroup { public synchronized boolean removeEventListener(PeerEventListener listener) { return peerEventListeners.remove(checkNotNull(listener)); } - + /** * Returns a newly allocated list containing the currently connected peers. If all you care about is the count, * use numConnectedPeers(). @@ -512,7 +512,7 @@ public class PeerGroup { * than the current chain head, the relevant parts of the chain won't be redownloaded for you.

*/ public synchronized void addWallet(Wallet wallet) { - Preconditions.checkNotNull(wallet); + Preconditions.checkNotNull(wallet); wallets.add(wallet); addEventListener(wallet.getPeerEventListener()); announcePendingWalletTransactions(Collections.singletonList(wallet), peers); @@ -602,11 +602,11 @@ public class PeerGroup { static public Peer peerFromChannelFuture(ChannelFuture future) { return peerFromChannel(future.getChannel()); } - + static public Peer peerFromChannel(Channel channel) { return ((PeerHandler)channel.getPipeline().get("peer")).getPeer(); } - + /** * Start downloading the blockchain from the first available peer. *

diff --git a/core/src/main/java/com/google/bitcoin/core/StoredUndoableBlock.java b/core/src/main/java/com/google/bitcoin/core/StoredUndoableBlock.java index 418824eb..19c42703 100644 --- a/core/src/main/java/com/google/bitcoin/core/StoredUndoableBlock.java +++ b/core/src/main/java/com/google/bitcoin/core/StoredUndoableBlock.java @@ -28,14 +28,14 @@ import java.util.List; * connected. */ public class StoredUndoableBlock implements Serializable { - private static final long serialVersionUID = 5127353027086786117L; - - Sha256Hash blockHash; - - // Only one of either txOutChanges or transactions will be set - private TransactionOutputChanges txOutChanges; - private List transactions; - + private static final long serialVersionUID = 5127353027086786117L; + + Sha256Hash blockHash; + + // Only one of either txOutChanges or transactions will be set + private TransactionOutputChanges txOutChanges; + private List transactions; + public StoredUndoableBlock(Sha256Hash hash, TransactionOutputChanges txOutChanges) { this.blockHash = hash; this.transactions = null; diff --git a/core/src/main/java/com/google/bitcoin/core/TCPNetworkConnection.java b/core/src/main/java/com/google/bitcoin/core/TCPNetworkConnection.java index 60359ff0..74f00c51 100644 --- a/core/src/main/java/com/google/bitcoin/core/TCPNetworkConnection.java +++ b/core/src/main/java/com/google/bitcoin/core/TCPNetworkConnection.java @@ -50,8 +50,8 @@ import static org.jboss.netty.channel.Channels.write; * */ public class TCPNetworkConnection implements NetworkConnection { - private static final Logger log = LoggerFactory.getLogger(TCPNetworkConnection.class); - + private static final Logger log = LoggerFactory.getLogger(TCPNetworkConnection.class); + // The IP address to which we are connecting. private InetAddress remoteIp; private final NetworkParameters params; diff --git a/core/src/main/java/com/google/bitcoin/core/Transaction.java b/core/src/main/java/com/google/bitcoin/core/Transaction.java index 9a475c33..aa158cd7 100644 --- a/core/src/main/java/com/google/bitcoin/core/Transaction.java +++ b/core/src/main/java/com/google/bitcoin/core/Transaction.java @@ -154,7 +154,7 @@ public class Transaction extends ChildMessage implements Serializable { /** * Used by BitcoinSerializer. The serializer has to calculate a hash for checksumming so to * avoid wasting the considerable effort a set method is provided so the serializer can set it. - * + * * No verification is performed on this hash. */ void setHash(Sha256Hash hash) { @@ -447,7 +447,7 @@ public class Transaction extends ChildMessage implements Serializable { cursor += varint.getSizeInBytes(); for (i = 0; i < txInCount; i++) { - // 36 = length of previous_outpoint + // 36 = length of previous_outpoint cursor += 36; varint = new VarInt(buf, cursor); scriptLen = varint.value; @@ -461,7 +461,7 @@ public class Transaction extends ChildMessage implements Serializable { for (i = 0; i < txOutCount; i++) { // 8 = length of tx value field (uint64) - cursor += 8; + cursor += 8; varint = new VarInt(buf, cursor); scriptLen = varint.value; cursor += scriptLen + varint.getSizeInBytes();