mirror of
https://github.com/bitcoinj/bitcoinj.git
synced 2025-01-18 21:32:35 +01:00
Sha256Hash: migrate to byte[] serialize()
from byte[] getReversedBytes()
This commit is contained in:
parent
edf889306b
commit
4248804f82
@ -269,9 +269,11 @@ public class Sha256Hash implements Comparable<Sha256Hash> {
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a reversed copy of the internal byte array.
|
||||
* Allocates a byte array and writes the hash into it, in reversed order.
|
||||
*
|
||||
* @return byte array containing the hash
|
||||
*/
|
||||
public byte[] getReversedBytes() {
|
||||
public byte[] serialize() {
|
||||
return ByteUtils.reverseBytes(bytes);
|
||||
}
|
||||
|
||||
@ -284,7 +286,7 @@ public class Sha256Hash implements Comparable<Sha256Hash> {
|
||||
*/
|
||||
public ByteBuffer write(ByteBuffer buf) throws BufferOverflowException {
|
||||
// we have to flip it around, as on the wire it's in little endian
|
||||
buf.put(getReversedBytes());
|
||||
buf.put(serialize());
|
||||
return buf;
|
||||
}
|
||||
|
||||
|
@ -269,8 +269,8 @@ public class Block extends Message {
|
||||
// default for testing
|
||||
void writeHeader(OutputStream stream) throws IOException {
|
||||
ByteUtils.writeInt32LE(version, stream);
|
||||
stream.write(prevBlockHash.getReversedBytes());
|
||||
stream.write(getMerkleRoot().getReversedBytes());
|
||||
stream.write(prevBlockHash.serialize());
|
||||
stream.write(getMerkleRoot().serialize());
|
||||
ByteUtils.writeInt32LE(time.getEpochSecond(), stream);
|
||||
ByteUtils.writeInt32LE(difficultyTarget, stream);
|
||||
ByteUtils.writeInt32LE(nonce, stream);
|
||||
@ -520,7 +520,7 @@ public class Block extends Message {
|
||||
if (witnessReserved.length != 32)
|
||||
throw new VerificationException("Coinbase witness reserved invalid: length");
|
||||
|
||||
Sha256Hash witnessRootHash = Sha256Hash.twiceOf(getWitnessRoot().getReversedBytes(), witnessReserved);
|
||||
Sha256Hash witnessRootHash = Sha256Hash.twiceOf(getWitnessRoot().serialize(), witnessReserved);
|
||||
if (!witnessRootHash.equals(witnessCommitment))
|
||||
throw new VerificationException("Witness merkle root invalid. Expected " + witnessCommitment.toString()
|
||||
+ " but got " + witnessRootHash.toString());
|
||||
@ -594,8 +594,8 @@ public class Block extends Message {
|
||||
Sha256Hash leftHash = tree.get(levelOffset + left);
|
||||
Sha256Hash rightHash = tree.get(levelOffset + right);
|
||||
tree.add(Sha256Hash.wrapReversed(hashTwice(
|
||||
leftHash.getReversedBytes(),
|
||||
rightHash.getReversedBytes())));
|
||||
leftHash.serialize(),
|
||||
rightHash.serialize())));
|
||||
}
|
||||
// Move to the next level.
|
||||
levelOffset += levelSize;
|
||||
|
@ -84,10 +84,10 @@ public class GetBlocksMessage extends Message {
|
||||
stream.write(VarInt.of(locator.size()).serialize());
|
||||
for (Sha256Hash hash : locator.getHashes()) {
|
||||
// Have to reverse as wire format is little endian.
|
||||
stream.write(hash.getReversedBytes());
|
||||
stream.write(hash.serialize());
|
||||
}
|
||||
// Next, a block ID to stop at.
|
||||
stream.write(stopHash.getReversedBytes());
|
||||
stream.write(stopHash.serialize());
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -91,7 +91,7 @@ public abstract class ListMessage extends Message {
|
||||
// Write out the type code.
|
||||
ByteUtils.writeInt32LE(i.type.code, stream);
|
||||
// And now the hash.
|
||||
stream.write(i.hash.getReversedBytes());
|
||||
stream.write(i.hash.serialize());
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -116,7 +116,7 @@ public class PartialMerkleTree extends Message {
|
||||
|
||||
stream.write(VarInt.of(hashes.size()).serialize());
|
||||
for (Sha256Hash hash : hashes)
|
||||
stream.write(hash.getReversedBytes());
|
||||
stream.write(hash.serialize());
|
||||
|
||||
stream.write(VarInt.of(matchedChildBits.length).serialize());
|
||||
stream.write(matchedChildBits);
|
||||
|
@ -113,7 +113,7 @@ public class RejectMessage extends Message {
|
||||
stream.write(VarInt.of(reasonBytes.length).serialize());
|
||||
stream.write(reasonBytes);
|
||||
if ("block".equals(message) || "tx".equals(message))
|
||||
stream.write(messageHash.getReversedBytes());
|
||||
stream.write(messageHash.serialize());
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -1430,7 +1430,7 @@ public class Transaction extends Message {
|
||||
if (!anyoneCanPay) {
|
||||
ByteArrayOutputStream bosHashPrevouts = new ByteArrayOutputStream(256);
|
||||
for (TransactionInput input : this.inputs) {
|
||||
bosHashPrevouts.write(input.getOutpoint().getHash().getReversedBytes());
|
||||
bosHashPrevouts.write(input.getOutpoint().getHash().serialize());
|
||||
writeInt32LE(input.getOutpoint().getIndex(), bosHashPrevouts);
|
||||
}
|
||||
hashPrevouts = Sha256Hash.hashTwice(bosHashPrevouts.toByteArray());
|
||||
@ -1468,7 +1468,7 @@ public class Transaction extends Message {
|
||||
writeInt32LE(version, bos);
|
||||
bos.write(hashPrevouts);
|
||||
bos.write(hashSequence);
|
||||
bos.write(inputs.get(inputIndex).getOutpoint().getHash().getReversedBytes());
|
||||
bos.write(inputs.get(inputIndex).getOutpoint().getHash().serialize());
|
||||
writeInt32LE(inputs.get(inputIndex).getOutpoint().getIndex(), bos);
|
||||
bos.write(VarInt.of(scriptCode.length).serialize());
|
||||
bos.write(scriptCode);
|
||||
|
@ -105,7 +105,7 @@ public class TransactionOutPoint extends Message {
|
||||
|
||||
@Override
|
||||
protected void bitcoinSerializeToStream(OutputStream stream) throws IOException {
|
||||
stream.write(hash.getReversedBytes());
|
||||
stream.write(hash.serialize());
|
||||
ByteUtils.writeInt32LE(index, stream);
|
||||
}
|
||||
|
||||
|
@ -315,8 +315,8 @@ public class BlockTest {
|
||||
@Override
|
||||
protected void bitcoinSerializeToStream(OutputStream stream) throws IOException {
|
||||
ByteUtils.writeInt32LE(getVersion(), stream);
|
||||
stream.write(getPrevBlockHash().getReversedBytes());
|
||||
stream.write(getMerkleRoot().getReversedBytes());
|
||||
stream.write(getPrevBlockHash().serialize());
|
||||
stream.write(getMerkleRoot().serialize());
|
||||
ByteUtils.writeInt32LE(getTimeSeconds(), stream);
|
||||
ByteUtils.writeInt32LE(getDifficultyTarget(), stream);
|
||||
ByteUtils.writeInt32LE(getNonce(), stream);
|
||||
|
@ -241,7 +241,7 @@ public class FilteredBlockAndPartialMerkleTreeTest extends TestWithPeerGroup {
|
||||
stream.write(VarInt.of(Integer.MAX_VALUE).serialize());
|
||||
//stream.write(VarInt.of(hashes.size()).encode());
|
||||
for (Sha256Hash hash : hashes)
|
||||
stream.write(hash.getReversedBytes());
|
||||
stream.write(hash.serialize());
|
||||
|
||||
stream.write(VarInt.of(bits.length).serialize());
|
||||
stream.write(bits);
|
||||
|
Loading…
Reference in New Issue
Block a user