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

Updated bitcoinj API calls to bitcoinj 0.14

This commit is contained in:
Ross Nicoll 2015-08-01 11:36:58 +01:00
parent 38e9c4ae1d
commit 0f9910c6d8
6 changed files with 17 additions and 115 deletions

View File

@ -3,7 +3,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>org.altcoinj</groupId>
<artifactId>altcoinj</artifactId>
<version>0.13-SNAPSHOT</version>
<version>0.14-SNAPSHOT</version>
<packaging>jar</packaging>
<dependencies>
<dependency>

View File

@ -17,8 +17,8 @@ import org.bitcoinj.core.ProtocolException;
*/
public class AltcoinSerializer extends BitcoinSerializer {
public AltcoinSerializer(NetworkParameters params, boolean parseLazy, boolean parseRetain) {
super(params, parseLazy, parseRetain);
public AltcoinSerializer(NetworkParameters params, boolean parseRetain) {
super(params, parseRetain);
}
@Override

View File

@ -109,7 +109,7 @@ public abstract class AbstractDogecoinParams extends NetworkParameters implement
}
private static AltcoinBlock createGenesis(NetworkParameters params) {
AltcoinBlock genesisBlock = new AltcoinBlock(params);
AltcoinBlock genesisBlock = new AltcoinBlock(params, Block.BLOCK_VERSION_GENESIS);
Transaction t = new Transaction(params);
try {
byte[] bytes = Utils.HEX.decode
@ -305,8 +305,8 @@ public abstract class AbstractDogecoinParams extends NetworkParameters implement
}
@Override
public AltcoinSerializer getSerializer(boolean parseLazy, boolean parseRetain) {
return new AltcoinSerializer(this, parseLazy, parseRetain);
public AltcoinSerializer getSerializer(boolean parseRetain) {
return new AltcoinSerializer(this, parseRetain);
}
@Override

View File

@ -58,8 +58,8 @@ public class AltcoinBlock extends org.bitcoinj.core.Block {
private ScryptHash scryptHash;
/** Special case constructor, used for the genesis node, cloneAsHeader and unit tests. */
public AltcoinBlock(NetworkParameters params) {
super(params);
public AltcoinBlock(final NetworkParameters params, final long version) {
super(params, version);
}
/** Special case constructor, used for the genesis node, cloneAsHeader and unit tests. */
@ -156,39 +156,15 @@ public class AltcoinBlock extends org.bitcoinj.core.Block {
}
@Override
void parse() throws ProtocolException {
parseHeader();
protected void parseTransactions(final int offset) {
parseAuxPoW();
parseTransactions();
length = cursor - offset;
}
@Override
protected void parseTransactions() {
if (null != this.auxpow) {
parseTransactions(HEADER_SIZE + auxpow.getMessageSize());
super.parseTransactions(offset + auxpow.getMessageSize());
} else {
parseTransactions(HEADER_SIZE);
super.parseTransactions(offset);
}
}
@Override
protected void parseLite() throws ProtocolException {
// Ignore the header since it has fixed length. If length is not provided we will have to
// invoke a light parse of transactions to calculate the length.
if (length == UNKNOWN_LENGTH) {
Preconditions.checkState(serializer.isParseLazyMode(),
"Performing lite parse of block transaction as block was initialised from byte array " +
"without providing length. This should never need to happen.");
parseAuxPoW();
parseTransactions();
length = cursor - offset;
} else {
transactionBytesValid = !transactionsParsed || serializer.isParseRetainMode() && length > HEADER_SIZE;
}
headerBytesValid = !headerParsed || serializer.isParseRetainMode() && length >= HEADER_SIZE;
}
@Override
void writeHeader(OutputStream stream) throws IOException {
super.writeHeader(stream);
@ -200,7 +176,7 @@ public class AltcoinBlock extends org.bitcoinj.core.Block {
/** Returns a copy of the block, but without any transactions. */
@Override
public Block cloneAsHeader() {
AltcoinBlock block = new AltcoinBlock(params);
AltcoinBlock block = new AltcoinBlock(params, getVersion());
super.copyBitcoinHeaderTo(block);
block.auxpow = auxpow;
return block;

View File

@ -35,7 +35,7 @@ import java.util.*;
* NetworkParameters for AuxPoW networks <b>must</b> implement AltcoinNetworkParameters
* in order for AuxPoW to work.</p>
*/
public class AuxPoW extends ChildMessage implements Serializable {
public class AuxPoW extends ChildMessage {
public static final byte[] MERGED_MINING_HEADER = new byte[] {
(byte) 0xfa, (byte) 0xbe, "m".getBytes()[0], "m".getBytes()[0]
@ -93,12 +93,6 @@ public class AuxPoW extends ChildMessage implements Serializable {
super(params, payload, 0, parent, serializer, Message.UNKNOWN_LENGTH);
}
@Override
protected void parseLite() throws ProtocolException {
length = calcLength(payload, offset);
cursor = offset + length;
}
protected static int calcLength(byte[] buf, int offset) {
VarInt varint;
// jump past transaction
@ -120,11 +114,7 @@ public class AuxPoW extends ChildMessage implements Serializable {
}
@Override
void parse() throws ProtocolException {
if (parsed)
return;
protected void parse() throws ProtocolException {
cursor = offset;
transaction = new Transaction(params, payload, cursor, this, serializer, Message.UNKNOWN_LENGTH);
cursor += transaction.getOptimalEncodingMessageSize();
@ -153,7 +143,6 @@ public class AuxPoW extends ChildMessage implements Serializable {
public int getOptimalEncodingMessageSize() {
if (optimalEncodingMessageSize != 0)
return optimalEncodingMessageSize;
maybeParse();
if (optimalEncodingMessageSize != 0)
return optimalEncodingMessageSize;
optimalEncodingMessageSize = getMessageSize();
@ -208,16 +197,6 @@ public class AuxPoW extends ChildMessage implements Serializable {
return result;
}
/**
* Ensure object is fully parsed before invoking java serialization. The backing byte array
* is transient so if the object has parseLazy = true and hasn't invoked checkParse yet
* then data will be lost during serialization.
*/
private void writeObject(ObjectOutputStream out) throws IOException {
maybeParse();
out.defaultWriteObject();
}
/**
* Get the block header from the parent blockchain. The hash of the header
* is the value which should match the difficulty target. Note that blocks are
@ -251,27 +230,6 @@ public class AuxPoW extends ChildMessage implements Serializable {
return coinbaseBranch;
}
/**
* <p>Checks the transaction contents for sanity, in ways that can be done in a standalone manner.
* Does <b>not</b> perform all checks on a transaction such as whether the inputs are already spent.
* Specifically this method verifies:</p>
*
* <ul>
* <li>That there is at least one input and output.</li>
* <li>That the serialized size is not larger than the max block size.</li>
* <li>That no outputs have negative value.</li>
* <li>That the outputs do not sum to larger than the max allowed quantity of coin in the system.</li>
* <li>If the tx is a coinbase tx, the coinbase scriptSig size is within range. Otherwise that there are no
* coinbase inputs in the tx.</li>
* </ul>
*
* @throws VerificationException
*/
public void verify() throws VerificationException {
maybeParse();
// TODO: Verify the AuxPoW data
}
/**
* Check the proof of work for this AuxPoW header meets the target
* difficulty.

View File

@ -19,14 +19,11 @@ package org.bitcoinj.core;
import javax.annotation.Nullable;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.io.Serializable;
import java.security.MessageDigest;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import static org.bitcoinj.core.Sha256Hash.newDigest;
/**
* A Merkle branch contains the hashes from a leaf of a Merkle tree
@ -37,7 +34,7 @@ import static org.bitcoinj.core.Sha256Hash.newDigest;
* TODO: Has a lot of similarity to PartialMerkleTree, should attempt to merge
* the two.
*/
public class MerkleBranch extends ChildMessage implements Serializable {
public class MerkleBranch extends ChildMessage {
private static final long serialVersionUID = 2;
// Merkle branches can be encoded in a way that will use more bytes than is optimal
@ -89,12 +86,6 @@ public class MerkleBranch extends ChildMessage implements Serializable {
this.index = branchSideMask;
}
@Override
protected void parseLite() throws ProtocolException {
length = calcLength(payload, offset);
cursor = offset + length;
}
public static int calcLength(byte[] buf, int offset) {
VarInt varint = new VarInt(buf, offset);
@ -102,10 +93,7 @@ public class MerkleBranch extends ChildMessage implements Serializable {
}
@Override
void parse() throws ProtocolException {
if (parsed)
return;
protected void parse() throws ProtocolException {
cursor = offset;
final int hashCount = (int) readVarInt();
@ -117,6 +105,7 @@ public class MerkleBranch extends ChildMessage implements Serializable {
optimalEncodingMessageSize += 32 * hashCount;
setIndex(readUint32());
optimalEncodingMessageSize += 4;
length = cursor - offset;
}
@Override
@ -196,7 +185,6 @@ public class MerkleBranch extends ChildMessage implements Serializable {
public int getOptimalEncodingMessageSize() {
if (optimalEncodingMessageSize != 0)
return optimalEncodingMessageSize;
maybeParse();
if (optimalEncodingMessageSize != 0)
return optimalEncodingMessageSize;
optimalEncodingMessageSize = getMessageSize();
@ -211,26 +199,6 @@ public class MerkleBranch extends ChildMessage implements Serializable {
return "Merkle branch";
}
/**
* Ensure object is fully parsed before invoking java serialization. The backing byte array
* is transient so if the object has parseLazy = true and hasn't invoked checkParse yet
* then data will be lost during serialization.
*/
private void writeObject(ObjectOutputStream out) throws IOException {
maybeParse();
out.defaultWriteObject();
}
/**
* Should check that the merkle branch side bits are not wider than the
* provided hashes.
* @throws VerificationException If the branch is invalid.
*/
public void verify() throws VerificationException {
maybeParse();
// TODO: Check the flags make sense for the inputs
}
@Override
public boolean equals(Object o) {
if (this == o) return true;