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

added Sha256Hash.wrapReversed/getReversedBytes methods to further deduplicate and simplify code

This commit is contained in:
Amichai Rothman 2015-06-24 02:57:37 +03:00 committed by Mike Hearn
parent faf92971dd
commit 9c8f6fbb20
10 changed files with 35 additions and 17 deletions

View File

@ -214,7 +214,7 @@ public class BitcoinSerializer {
} else if (command.equals("tx")) {
Transaction tx = new Transaction(params, payloadBytes, null, parseLazy, parseRetain, length);
if (hash != null)
tx.setHash(Sha256Hash.wrap(Utils.reverseBytes(hash)));
tx.setHash(Sha256Hash.wrapReversed(hash));
message = tx;
} else if (command.equals("addr")) {
message = new AddressMessage(params, payloadBytes, parseLazy, parseRetain, length);

View File

@ -191,7 +191,7 @@ public class Block extends Message {
difficultyTarget = readUint32();
nonce = readUint32();
hash = Sha256Hash.wrap(Utils.reverseBytes(Sha256Hash.hashTwice(payload, offset, cursor)));
hash = Sha256Hash.wrapReversed(Sha256Hash.hashTwice(payload, offset, cursor));
headerParsed = true;
headerBytesValid = parseRetain;
@ -385,8 +385,8 @@ public class Block extends Message {
// fall back to manual write
maybeParseHeader();
Utils.uint32ToByteStreamLE(version, stream);
stream.write(Utils.reverseBytes(prevBlockHash.getBytes()));
stream.write(Utils.reverseBytes(getMerkleRoot().getBytes()));
stream.write(prevBlockHash.getReversedBytes());
stream.write(getMerkleRoot().getReversedBytes());
Utils.uint32ToByteStreamLE(time, stream);
Utils.uint32ToByteStreamLE(difficultyTarget, stream);
Utils.uint32ToByteStreamLE(nonce, stream);
@ -511,7 +511,7 @@ public class Block extends Message {
try {
ByteArrayOutputStream bos = new UnsafeByteArrayOutputStream(HEADER_SIZE);
writeHeader(bos);
return Sha256Hash.wrap(Utils.reverseBytes(Sha256Hash.hashTwice(bos.toByteArray())));
return Sha256Hash.wrapReversed(Sha256Hash.hashTwice(bos.toByteArray()));
} catch (IOException e) {
throw new RuntimeException(e); // Cannot happen.
}

View File

@ -95,10 +95,10 @@ public class GetBlocksMessage extends Message {
stream.write(new VarInt(locator.size()).encode());
for (Sha256Hash hash : locator) {
// Have to reverse as wire format is little endian.
stream.write(Utils.reverseBytes(hash.getBytes()));
stream.write(hash.getReversedBytes());
}
// Next, a block ID to stop at.
stream.write(Utils.reverseBytes(stopHash.getBytes()));
stream.write(stopHash.getReversedBytes());
}
@Override

View File

@ -116,7 +116,7 @@ public abstract class ListMessage extends Message {
// Write out the type code.
Utils.uint32ToByteStreamLE(i.type.ordinal(), stream);
// And now the hash.
stream.write(Utils.reverseBytes(i.hash.getBytes()));
stream.write(i.hash.getReversedBytes());
}
}

View File

@ -411,11 +411,10 @@ public abstract class Message implements Serializable {
try {
byte[] hash = new byte[32];
System.arraycopy(payload, cursor, hash, 0, 32);
cursor += 32;
// We have to flip it around, as it's been read off the wire in little endian.
// Not the most efficient way to do this but the clearest.
hash = Utils.reverseBytes(hash);
cursor += 32;
return Sha256Hash.wrap(hash);
return Sha256Hash.wrapReversed(hash);
} catch (IndexOutOfBoundsException e) {
throw new ProtocolException(e);
}

View File

@ -101,7 +101,7 @@ public class PartialMerkleTree extends Message {
stream.write(new VarInt(hashes.size()).encode());
for (Sha256Hash hash : hashes)
stream.write(reverseBytes(hash.getBytes()));
stream.write(hash.getReversedBytes());
stream.write(new VarInt(matchedChildBits.length).encode());
stream.write(matchedChildBits);
@ -214,9 +214,9 @@ public class PartialMerkleTree extends Message {
}
private static Sha256Hash combineLeftRight(byte[] left, byte[] right) {
return Sha256Hash.wrap(reverseBytes(Sha256Hash.hashTwice(
return Sha256Hash.wrapReversed(Sha256Hash.hashTwice(
reverseBytes(left), 0, 32,
reverseBytes(right), 0, 32)));
reverseBytes(right), 0, 32));
}
/**

View File

@ -107,7 +107,7 @@ public class RejectMessage extends Message {
stream.write(new VarInt(reasonBytes.length).encode());
stream.write(reasonBytes);
if (message.equals("block") || message.equals("tx"))
stream.write(Utils.reverseBytes(messageHash.getBytes()));
stream.write(messageHash.getReversedBytes());
}
/**

View File

@ -81,6 +81,18 @@ public class Sha256Hash implements Serializable, Comparable<Sha256Hash> {
return wrap(Utils.HEX.decode(hexString));
}
/**
* Creates a new instance that wraps the given hash value, but with byte order reversed.
*
* @param rawHashBytes the raw hash bytes to wrap
* @return a new instance
* @throws IllegalArgumentException if the given array length is not exactly 32
*/
@SuppressWarnings("deprecation") // the constructor will be made private in the future
public static Sha256Hash wrapReversed(byte[] rawHashBytes) {
return wrap(Utils.reverseBytes(rawHashBytes));
}
/** Use {@link #of(byte[])} instead: this old name is ambiguous. */
@Deprecated
public static Sha256Hash create(byte[] contents) {
@ -244,6 +256,13 @@ public class Sha256Hash implements Serializable, Comparable<Sha256Hash> {
return bytes;
}
/**
* Returns a reversed copy of the internal byte array.
*/
public byte[] getReversedBytes() {
return Utils.reverseBytes(bytes);
}
@Override
public int compareTo(Sha256Hash o) {
return this.hashCode() - o.hashCode();

View File

@ -226,7 +226,7 @@ public class Transaction extends ChildMessage implements Serializable {
public Sha256Hash getHash() {
if (hash == null) {
byte[] bits = bitcoinSerialize();
hash = Sha256Hash.wrap(reverseBytes(Sha256Hash.hashTwice(bits)));
hash = Sha256Hash.wrapReversed(Sha256Hash.hashTwice(bits));
}
return hash;
}

View File

@ -112,7 +112,7 @@ public class TransactionOutPoint extends ChildMessage implements Serializable {
@Override
protected void bitcoinSerializeToStream(OutputStream stream) throws IOException {
stream.write(Utils.reverseBytes(hash.getBytes()));
stream.write(hash.getReversedBytes());
Utils.uint32ToByteStreamLE(index, stream);
}