3
0
mirror of https://github.com/Qortal/altcoinj.git synced 2025-02-12 10:15:52 +00:00

Misc optimizations, the profiles look much better now.

This commit is contained in:
Mike Hearn 2013-02-27 16:22:45 +01:00 committed by Mike Hearn
parent 121695fa13
commit 50323391e5
5 changed files with 24 additions and 19 deletions

View File

@ -379,7 +379,7 @@ public abstract class AbstractBlockChain {
TransactionOutputChanges txOutChanges = null; TransactionOutputChanges txOutChanges = null;
if (shouldVerifyTransactions()) if (shouldVerifyTransactions())
txOutChanges = connectTransactions(storedPrev.getHeight() + 1, block); txOutChanges = connectTransactions(storedPrev.getHeight() + 1, block);
StoredBlock newStoredBlock = addToBlockStore(storedPrev, block.cloneAsHeader(), txOutChanges); StoredBlock newStoredBlock = addToBlockStore(storedPrev, block, txOutChanges);
setChainHead(newStoredBlock); setChainHead(newStoredBlock);
log.debug("Chain is now {} blocks high", newStoredBlock.getHeight()); log.debug("Chain is now {} blocks high", newStoredBlock.getHeight());
// Notify the listeners of the new block, so the depth and workDone of stored transactions can be updated // Notify the listeners of the new block, so the depth and workDone of stored transactions can be updated

View File

@ -72,7 +72,7 @@ public class HeadersMessage extends Message {
byte[] blockHeader = readBytes(81); byte[] blockHeader = readBytes(81);
if (blockHeader[80] != 00) if (blockHeader[80] != 00)
throw new ProtocolException("Block header does not end with a null byte"); throw new ProtocolException("Block header does not end with a null byte");
Block newBlockHeader = new Block(this.params, blockHeader); Block newBlockHeader = new Block(this.params, blockHeader, true, true, 81);
blockHeaders.add(newBlockHeader); blockHeaders.add(newBlockHeader);
} }

View File

@ -95,7 +95,7 @@ public class StoredBlock implements Serializable {
// the largest amount of work done not the tallest. // the largest amount of work done not the tallest.
BigInteger chainWork = this.chainWork.add(block.getWork()); BigInteger chainWork = this.chainWork.add(block.getWork());
int height = this.height + 1; int height = this.height + 1;
return new StoredBlock(block.cloneAsHeader(), chainWork, height); return new StoredBlock(block, chainWork, height);
} }
/** /**

View File

@ -28,9 +28,7 @@ import java.nio.charset.Charset;
import java.security.MessageDigest; import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException; import java.security.NoSuchAlgorithmException;
import java.util.Date; import java.util.Date;
import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantLock; import java.util.concurrent.locks.ReentrantLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;
import static com.google.common.base.Preconditions.checkArgument; import static com.google.common.base.Preconditions.checkArgument;
@ -40,6 +38,17 @@ import static com.google.common.base.Preconditions.checkArgument;
*/ */
public class Utils { public class Utils {
private static CycleDetectingLockFactory lockFactory = CycleDetectingLockFactory.newInstance(CycleDetectingLockFactory.Policies.THROW); private static CycleDetectingLockFactory lockFactory = CycleDetectingLockFactory.newInstance(CycleDetectingLockFactory.Policies.THROW);
private static MessageDigest digest;
static {
try {
digest = MessageDigest.getInstance("SHA-256");
} catch (NoSuchAlgorithmException e) {
throw new RuntimeException(e); // Can't happen.
}
}
/** Returns a cycle detecting re-entrant named lock. */ /** Returns a cycle detecting re-entrant named lock. */
public static ReentrantLock lock(String name) { public static ReentrantLock lock(String name) {
return lockFactory.newReentrantLock(name); return lockFactory.newReentrantLock(name);
@ -167,23 +176,19 @@ public class Utils {
* standard procedure in Bitcoin. The resulting hash is in big endian form. * standard procedure in Bitcoin. The resulting hash is in big endian form.
*/ */
public static byte[] doubleDigest(byte[] input, int offset, int length) { public static byte[] doubleDigest(byte[] input, int offset, int length) {
try { synchronized (digest) {
MessageDigest digest = MessageDigest.getInstance("SHA-256"); digest.reset();
digest.update(input, offset, length); digest.update(input, offset, length);
byte[] first = digest.digest(); byte[] first = digest.digest();
return digest.digest(first); return digest.digest(first);
} catch (NoSuchAlgorithmException e) {
throw new RuntimeException(e); // Cannot happen.
} }
} }
public static byte[] singleDigest(byte[] input, int offset, int length) { public static byte[] singleDigest(byte[] input, int offset, int length) {
try { synchronized (digest) {
MessageDigest digest = MessageDigest.getInstance("SHA-256"); digest.reset();
digest.update(input, offset, length); digest.update(input, offset, length);
return digest.digest(); return digest.digest();
} catch (NoSuchAlgorithmException e) {
throw new RuntimeException(e); // Cannot happen.
} }
} }
@ -192,14 +197,12 @@ public class Utils {
*/ */
public static byte[] doubleDigestTwoBuffers(byte[] input1, int offset1, int length1, public static byte[] doubleDigestTwoBuffers(byte[] input1, int offset1, int length1,
byte[] input2, int offset2, int length2) { byte[] input2, int offset2, int length2) {
try { synchronized (digest) {
MessageDigest digest = MessageDigest.getInstance("SHA-256"); digest.reset();
digest.update(input1, offset1, length1); digest.update(input1, offset1, length1);
digest.update(input2, offset2, length2); digest.update(input2, offset2, length2);
byte[] first = digest.digest(); byte[] first = digest.digest();
return digest.digest(first); return digest.digest(first);
} catch (NoSuchAlgorithmException e) {
throw new RuntimeException(e); // Cannot happen.
} }
} }

View File

@ -181,8 +181,10 @@ public class SPVBlockStore implements BlockStore {
} }
buffer.put(chainWorkBytes); buffer.put(chainWorkBytes);
buffer.putInt(block.getHeight()); buffer.putInt(block.getHeight());
byte[] header = block.getHeader().bitcoinSerialize(); // Using unsafeBitcoinSerialize here can give us direct access to the same bytes we read off the wire,
buffer.put(header, 0, Block.HEADER_SIZE); // Trim the trailing 00 byte (zero transactions). // avoiding serialization round-trips.
byte[] bytes = block.getHeader().unsafeBitcoinSerialize();
buffer.put(bytes, 0, Block.HEADER_SIZE); // Trim the trailing 00 byte (zero transactions).
setRingCursor(buffer, buffer.position()); setRingCursor(buffer, buffer.position());
blockCache.put(hash, block); blockCache.put(hash, block);
} finally { lock.unlock(); } } finally { lock.unlock(); }