3
0
mirror of https://github.com/Qortal/altcoinj.git synced 2025-02-07 14:54:15 +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;
if (shouldVerifyTransactions())
txOutChanges = connectTransactions(storedPrev.getHeight() + 1, block);
StoredBlock newStoredBlock = addToBlockStore(storedPrev, block.cloneAsHeader(), txOutChanges);
StoredBlock newStoredBlock = addToBlockStore(storedPrev, block, txOutChanges);
setChainHead(newStoredBlock);
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

View File

@ -72,7 +72,7 @@ public class HeadersMessage extends Message {
byte[] blockHeader = readBytes(81);
if (blockHeader[80] != 00)
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);
}

View File

@ -95,7 +95,7 @@ public class StoredBlock implements Serializable {
// the largest amount of work done not the tallest.
BigInteger chainWork = this.chainWork.add(block.getWork());
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.NoSuchAlgorithmException;
import java.util.Date;
import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;
import static com.google.common.base.Preconditions.checkArgument;
@ -40,6 +38,17 @@ import static com.google.common.base.Preconditions.checkArgument;
*/
public class Utils {
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. */
public static ReentrantLock lock(String name) {
return lockFactory.newReentrantLock(name);
@ -167,23 +176,19 @@ public class Utils {
* standard procedure in Bitcoin. The resulting hash is in big endian form.
*/
public static byte[] doubleDigest(byte[] input, int offset, int length) {
try {
MessageDigest digest = MessageDigest.getInstance("SHA-256");
synchronized (digest) {
digest.reset();
digest.update(input, offset, length);
byte[] first = digest.digest();
return digest.digest(first);
} catch (NoSuchAlgorithmException e) {
throw new RuntimeException(e); // Cannot happen.
}
}
public static byte[] singleDigest(byte[] input, int offset, int length) {
try {
MessageDigest digest = MessageDigest.getInstance("SHA-256");
synchronized (digest) {
digest.reset();
digest.update(input, offset, length);
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,
byte[] input2, int offset2, int length2) {
try {
MessageDigest digest = MessageDigest.getInstance("SHA-256");
synchronized (digest) {
digest.reset();
digest.update(input1, offset1, length1);
digest.update(input2, offset2, length2);
byte[] first = digest.digest();
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.putInt(block.getHeight());
byte[] header = block.getHeader().bitcoinSerialize();
buffer.put(header, 0, Block.HEADER_SIZE); // Trim the trailing 00 byte (zero transactions).
// Using unsafeBitcoinSerialize here can give us direct access to the same bytes we read off the wire,
// 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());
blockCache.put(hash, block);
} finally { lock.unlock(); }