3
0
mirror of https://github.com/Qortal/altcoinj.git synced 2025-02-11 17:55:53 +00:00

Enable parsing of merkleblock messages

Needs to construct the parts of the FilteredBlock manually at this time, but has potential for optimzations
This commit is contained in:
langerhans 2015-10-25 17:09:11 +01:00 committed by Ross Nicoll
parent 2c3466866c
commit 2d7ea57916
2 changed files with 36 additions and 11 deletions

View File

@ -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.

View File

@ -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);
}
}