Skip to content

Commit

Permalink
BitcoinSerializer: Don't use the payload hash to set the tx hash for …
Browse files Browse the repository at this point in the history
…transactions with witnesses.

When we receive a tx message from a peer, the hash is the hash of the payload.
If the tx is a segwit tx it is not equal to its bitcoin hash (which uses the
pre-segwit serialization format) so we don't set it, it will be computed later.
  • Loading branch information
sstone authored and Andreas Schildbach committed Aug 25, 2017
1 parent 2ae564c commit f9577eb
Showing 1 changed file with 6 additions and 4 deletions.
10 changes: 6 additions & 4 deletions core/src/main/java/org/bitcoinj/core/BitcoinSerializer.java
Expand Up @@ -305,11 +305,13 @@ public InventoryMessage makeInventoryMessage(byte[] payloadBytes, int length) th
* serialization format support.
*/
@Override
public Transaction makeTransaction(byte[] payloadBytes, int offset,
int length, byte[] hash) throws ProtocolException {
public Transaction makeTransaction(byte[] payloadBytes, int offset, int length, byte[] payloadHash)
throws ProtocolException {
Transaction tx = new Transaction(params, payloadBytes, offset, null, this, length);
if (hash != null)
tx.setHash(Sha256Hash.wrapReversed(hash));
if (payloadHash != null && !tx.hasWitness())
// We can only use this optimization if payloadHash equals the transaction hash (aka txid). This is not the
// case for transactions with witnesses. In this case, the transaction hash will be computed later.
tx.setHash(Sha256Hash.wrapReversed(payloadHash));
return tx;
}

Expand Down

0 comments on commit f9577eb

Please sign in to comment.