From 2d7ea57916d4622eb01b9f942fe47cd17f6e9d99 Mon Sep 17 00:00:00 2001 From: langerhans Date: Sun, 25 Oct 2015 17:09:11 +0100 Subject: [PATCH] Enable parsing of merkleblock messages Needs to construct the parts of the FilteredBlock manually at this time, but has potential for optimzations --- .../java/org/bitcoinj/core/AltcoinBlock.java | 4 ++ .../org/libdohj/core/AltcoinSerializer.java | 43 ++++++++++++++----- 2 files changed, 36 insertions(+), 11 deletions(-) diff --git a/src/main/java/org/bitcoinj/core/AltcoinBlock.java b/src/main/java/org/bitcoinj/core/AltcoinBlock.java index d90feccb..42f70d0a 100644 --- a/src/main/java/org/bitcoinj/core/AltcoinBlock.java +++ b/src/main/java/org/bitcoinj/core/AltcoinBlock.java @@ -126,6 +126,10 @@ public class AltcoinBlock extends org.bitcoinj.core.Block { return this.auxpow; } + public void setAuxPoW(AuxPoW auxpow) { + this.auxpow = auxpow; + } + /** * Returns the Scrypt hash of the block (which for a valid, solved block should be * below the target). Big endian. diff --git a/src/main/java/org/libdohj/core/AltcoinSerializer.java b/src/main/java/org/libdohj/core/AltcoinSerializer.java index 1a00b4d3..53d3848c 100644 --- a/src/main/java/org/libdohj/core/AltcoinSerializer.java +++ b/src/main/java/org/libdohj/core/AltcoinSerializer.java @@ -1,18 +1,9 @@ - /* - * To change this license header, choose License Headers in Project Properties. - * To change this template file, choose Tools | Templates - * and open the template in the editor. - */ package org.libdohj.core; -import org.bitcoinj.core.AltcoinBlock; -import org.bitcoinj.core.BitcoinSerializer; -import org.bitcoinj.core.Block; -import org.bitcoinj.core.NetworkParameters; -import org.bitcoinj.core.ProtocolException; +import org.bitcoinj.core.*; +import org.bitcoinj.core.Utils; /** - * * @author jrn */ public class AltcoinSerializer extends BitcoinSerializer { @@ -25,4 +16,34 @@ public class AltcoinSerializer extends BitcoinSerializer { public Block makeBlock(final byte[] payloadBytes, final int offset, final int length) throws ProtocolException { return new AltcoinBlock(getParameters(), payloadBytes, offset, this, length); } + + @Override + public FilteredBlock makeFilteredBlock(byte[] payloadBytes) throws ProtocolException { + long blockVersion = Utils.readUint32(payloadBytes, 0); + int headerSize = Block.HEADER_SIZE; + + byte[] headerBytes = new byte[Block.HEADER_SIZE + 1]; + System.arraycopy(payloadBytes, 0, headerBytes, 0, headerSize); + headerBytes[80] = 0; // Need to provide 0 transactions so the block header can be constructed + + if (this.getParameters() instanceof AuxPoWNetworkParameters) { + final AuxPoWNetworkParameters auxPoWParams = (AuxPoWNetworkParameters) this.getParameters(); + if (auxPoWParams.isAuxPoWBlockVersion(blockVersion)) { + final AltcoinBlock header = (AltcoinBlock) makeBlock(headerBytes, 0, Message.UNKNOWN_LENGTH); + final AuxPoW auxpow = new AuxPoW(this.getParameters(), payloadBytes, Block.HEADER_SIZE, null, this); + header.setAuxPoW(auxpow); + + int pmtOffset = headerSize + auxpow.getMessageSize(); + int pmtLength = payloadBytes.length - pmtOffset; + byte[] pmtBytes = new byte[pmtLength]; + System.arraycopy(payloadBytes, pmtOffset, pmtBytes, 0, pmtLength); + PartialMerkleTree pmt = new PartialMerkleTree(this.getParameters(), pmtBytes, 0); + + return new FilteredBlock(this.getParameters(), header, pmt); + } + } + + // We are either not in AuxPoW mode, or the block is not an AuxPoW block. + return super.makeFilteredBlock(payloadBytes); + } }