mirror of
https://github.com/Qortal/altcoinj.git
synced 2025-11-13 02:48:34 +00:00
Patch 6 from Steves lazy parsing patchset:
Deduping related optimizations. This code will be removed later.
This commit is contained in:
3
TODO
3
TODO
@@ -50,6 +50,9 @@ Impacts from Steves changes:
|
|||||||
- Dead code in Transaction.parse
|
- Dead code in Transaction.parse
|
||||||
- javadocs on Transaction{Input,Output,} getters
|
- 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:
|
Block.java:
|
||||||
- Reformat
|
- Reformat
|
||||||
|
|||||||
@@ -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
|
// 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.
|
// 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)) {
|
if (dedupeList != null && canDedupeMessageType(header.command)) {
|
||||||
// We use a secure hash here rather than the faster and simpler array hashes because otherwise a malicious
|
// 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
|
// 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) {
|
synchronized (dedupeList) {
|
||||||
// Calculate hash inside the lock to avoid unnecessary battery power spent on hashing messages arriving
|
// Calculate hash inside the lock to avoid unnecessary battery power spent on hashing messages arriving
|
||||||
// on different threads simultaneously.
|
// on different threads simultaneously.
|
||||||
Sha256Hash hash = Sha256Hash.create(payloadBytes);
|
singleHash = Sha256Hash.create(payloadBytes);
|
||||||
Integer count = dedupeList.get(hash);
|
Integer count = dedupeList.get(singleHash);
|
||||||
if (count != null) {
|
if (count != null) {
|
||||||
int newCount = count + 1;
|
int newCount = count + 1;
|
||||||
log.info("Received duplicate {} message, now seen {} times", header.command, newCount);
|
log.info("Received duplicate {} message, now seen {} times", header.command, newCount);
|
||||||
dedupeList.put(hash, newCount);
|
dedupeList.put(singleHash, newCount);
|
||||||
return null;
|
return null;
|
||||||
} else {
|
} else {
|
||||||
dedupeList.put(hash, 1);
|
dedupeList.put(singleHash, 1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -266,7 +271,11 @@ public class BitcoinSerializer {
|
|||||||
// Verify the checksum.
|
// Verify the checksum.
|
||||||
byte[] hash = null;
|
byte[] hash = null;
|
||||||
if (usesChecksumming) {
|
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] ||
|
if (header.checksum[0] != hash[0] || header.checksum[1] != hash[1] ||
|
||||||
header.checksum[2] != hash[2] || header.checksum[3] != hash[3]) {
|
header.checksum[2] != hash[2] || header.checksum[3] != hash[3]) {
|
||||||
throw new ProtocolException("Checksum failed to verify, actual " +
|
throw new ProtocolException("Checksum failed to verify, actual " +
|
||||||
|
|||||||
@@ -128,6 +128,16 @@ public class Utils {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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)).
|
* Calculates SHA256(SHA256(byte range 1 + byte range 2)).
|
||||||
*/
|
*/
|
||||||
|
|||||||
@@ -85,8 +85,6 @@ public class SpeedTest {
|
|||||||
test.start(50000, 50000, false);
|
test.start(50000, 50000, false);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static final boolean RECACHE = false;
|
|
||||||
|
|
||||||
public void start(int warmupIterations, int iterations, boolean pauseForKeyPress) {
|
public void start(int warmupIterations, int iterations, boolean pauseForKeyPress) {
|
||||||
|
|
||||||
if (pauseForKeyPress) {
|
if (pauseForKeyPress) {
|
||||||
|
|||||||
Reference in New Issue
Block a user