3
0
mirror of https://github.com/Qortal/altcoinj.git synced 2025-02-14 11:15:51 +00:00

Patch 6 from Steves lazy parsing patchset:

Deduping related optimizations. This code will be removed later.
This commit is contained in:
Mike Hearn 2011-10-14 12:27:16 +00:00
parent ab8227882d
commit 06ad3e5bb1
4 changed files with 27 additions and 7 deletions

3
TODO
View File

@ -50,6 +50,9 @@ Impacts from Steves changes:
- Dead code in Transaction.parse
- javadocs on Transaction{Input,Output,} getters
- Delete the code related to deduping. The new network management code to be checked in later makes it unnecessary.
Also remove singleDigest as a result (it duplicates Sha256Hash).
Block.java:
- Reformat

View File

@ -243,6 +243,11 @@ public class BitcoinSerializer {
// Check for duplicates. This is to avoid the cost (cpu and memory) of parsing the message twice, which can
// be an issue on constrained devices.
//save this for reuse later. Hashing is expensive so checksumming starting with a single hash
//is a significant saving.
Sha256Hash singleHash = null;
if (dedupeList != null && canDedupeMessageType(header.command)) {
// We use a secure hash here rather than the faster and simpler array hashes because otherwise a malicious
// node on the network could broadcast a message designed to mask a different message. They would not
@ -250,15 +255,15 @@ public class BitcoinSerializer {
synchronized (dedupeList) {
// Calculate hash inside the lock to avoid unnecessary battery power spent on hashing messages arriving
// on different threads simultaneously.
Sha256Hash hash = Sha256Hash.create(payloadBytes);
Integer count = dedupeList.get(hash);
singleHash = Sha256Hash.create(payloadBytes);
Integer count = dedupeList.get(singleHash);
if (count != null) {
int newCount = count + 1;
log.info("Received duplicate {} message, now seen {} times", header.command, newCount);
dedupeList.put(hash, newCount);
dedupeList.put(singleHash, newCount);
return null;
} else {
dedupeList.put(hash, 1);
dedupeList.put(singleHash, 1);
}
}
}
@ -266,7 +271,11 @@ public class BitcoinSerializer {
// Verify the checksum.
byte[] hash = null;
if (usesChecksumming) {
hash = doubleDigest(payloadBytes);
if (singleHash != null) {
hash = singleDigest(singleHash.getBytes(), 0, 32);
} else {
hash = doubleDigest(payloadBytes);
}
if (header.checksum[0] != hash[0] || header.checksum[1] != hash[1] ||
header.checksum[2] != hash[2] || header.checksum[3] != hash[3]) {
throw new ProtocolException("Checksum failed to verify, actual " +

View File

@ -127,6 +127,16 @@ public class Utils {
throw new RuntimeException(e); // Cannot happen.
}
}
public static byte[] singleDigest(byte[] input, int offset, int length) {
try {
MessageDigest digest = MessageDigest.getInstance("SHA-256");
digest.update(input, offset, length);
return digest.digest();
} catch (NoSuchAlgorithmException e) {
throw new RuntimeException(e); // Cannot happen.
}
}
/**
* Calculates SHA256(SHA256(byte range 1 + byte range 2)).

View File

@ -85,8 +85,6 @@ public class SpeedTest {
test.start(50000, 50000, false);
}
public static final boolean RECACHE = false;
public void start(int warmupIterations, int iterations, boolean pauseForKeyPress) {
if (pauseForKeyPress) {