mirror of
https://github.com/Qortal/altcoinj.git
synced 2025-02-16 04:05:50 +00:00
Updated bitcoinj API calls to bitcoinj 0.14
This commit is contained in:
parent
38e9c4ae1d
commit
0f9910c6d8
2
pom.xml
2
pom.xml
@ -3,7 +3,7 @@
|
|||||||
<modelVersion>4.0.0</modelVersion>
|
<modelVersion>4.0.0</modelVersion>
|
||||||
<groupId>org.altcoinj</groupId>
|
<groupId>org.altcoinj</groupId>
|
||||||
<artifactId>altcoinj</artifactId>
|
<artifactId>altcoinj</artifactId>
|
||||||
<version>0.13-SNAPSHOT</version>
|
<version>0.14-SNAPSHOT</version>
|
||||||
<packaging>jar</packaging>
|
<packaging>jar</packaging>
|
||||||
<dependencies>
|
<dependencies>
|
||||||
<dependency>
|
<dependency>
|
||||||
|
@ -17,8 +17,8 @@ import org.bitcoinj.core.ProtocolException;
|
|||||||
*/
|
*/
|
||||||
public class AltcoinSerializer extends BitcoinSerializer {
|
public class AltcoinSerializer extends BitcoinSerializer {
|
||||||
|
|
||||||
public AltcoinSerializer(NetworkParameters params, boolean parseLazy, boolean parseRetain) {
|
public AltcoinSerializer(NetworkParameters params, boolean parseRetain) {
|
||||||
super(params, parseLazy, parseRetain);
|
super(params, parseRetain);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -109,7 +109,7 @@ public abstract class AbstractDogecoinParams extends NetworkParameters implement
|
|||||||
}
|
}
|
||||||
|
|
||||||
private static AltcoinBlock createGenesis(NetworkParameters params) {
|
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);
|
Transaction t = new Transaction(params);
|
||||||
try {
|
try {
|
||||||
byte[] bytes = Utils.HEX.decode
|
byte[] bytes = Utils.HEX.decode
|
||||||
@ -305,8 +305,8 @@ public abstract class AbstractDogecoinParams extends NetworkParameters implement
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public AltcoinSerializer getSerializer(boolean parseLazy, boolean parseRetain) {
|
public AltcoinSerializer getSerializer(boolean parseRetain) {
|
||||||
return new AltcoinSerializer(this, parseLazy, parseRetain);
|
return new AltcoinSerializer(this, parseRetain);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -58,8 +58,8 @@ public class AltcoinBlock extends org.bitcoinj.core.Block {
|
|||||||
private ScryptHash scryptHash;
|
private ScryptHash scryptHash;
|
||||||
|
|
||||||
/** Special case constructor, used for the genesis node, cloneAsHeader and unit tests. */
|
/** Special case constructor, used for the genesis node, cloneAsHeader and unit tests. */
|
||||||
public AltcoinBlock(NetworkParameters params) {
|
public AltcoinBlock(final NetworkParameters params, final long version) {
|
||||||
super(params);
|
super(params, version);
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Special case constructor, used for the genesis node, cloneAsHeader and unit tests. */
|
/** 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
|
@Override
|
||||||
void parse() throws ProtocolException {
|
protected void parseTransactions(final int offset) {
|
||||||
parseHeader();
|
|
||||||
parseAuxPoW();
|
parseAuxPoW();
|
||||||
parseTransactions();
|
|
||||||
length = cursor - offset;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
protected void parseTransactions() {
|
|
||||||
if (null != this.auxpow) {
|
if (null != this.auxpow) {
|
||||||
parseTransactions(HEADER_SIZE + auxpow.getMessageSize());
|
super.parseTransactions(offset + auxpow.getMessageSize());
|
||||||
} else {
|
} 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
|
@Override
|
||||||
void writeHeader(OutputStream stream) throws IOException {
|
void writeHeader(OutputStream stream) throws IOException {
|
||||||
super.writeHeader(stream);
|
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. */
|
/** Returns a copy of the block, but without any transactions. */
|
||||||
@Override
|
@Override
|
||||||
public Block cloneAsHeader() {
|
public Block cloneAsHeader() {
|
||||||
AltcoinBlock block = new AltcoinBlock(params);
|
AltcoinBlock block = new AltcoinBlock(params, getVersion());
|
||||||
super.copyBitcoinHeaderTo(block);
|
super.copyBitcoinHeaderTo(block);
|
||||||
block.auxpow = auxpow;
|
block.auxpow = auxpow;
|
||||||
return block;
|
return block;
|
||||||
|
@ -35,7 +35,7 @@ import java.util.*;
|
|||||||
* NetworkParameters for AuxPoW networks <b>must</b> implement AltcoinNetworkParameters
|
* NetworkParameters for AuxPoW networks <b>must</b> implement AltcoinNetworkParameters
|
||||||
* in order for AuxPoW to work.</p>
|
* 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[] {
|
public static final byte[] MERGED_MINING_HEADER = new byte[] {
|
||||||
(byte) 0xfa, (byte) 0xbe, "m".getBytes()[0], "m".getBytes()[0]
|
(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);
|
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) {
|
protected static int calcLength(byte[] buf, int offset) {
|
||||||
VarInt varint;
|
VarInt varint;
|
||||||
// jump past transaction
|
// jump past transaction
|
||||||
@ -120,11 +114,7 @@ public class AuxPoW extends ChildMessage implements Serializable {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
void parse() throws ProtocolException {
|
protected void parse() throws ProtocolException {
|
||||||
|
|
||||||
if (parsed)
|
|
||||||
return;
|
|
||||||
|
|
||||||
cursor = offset;
|
cursor = offset;
|
||||||
transaction = new Transaction(params, payload, cursor, this, serializer, Message.UNKNOWN_LENGTH);
|
transaction = new Transaction(params, payload, cursor, this, serializer, Message.UNKNOWN_LENGTH);
|
||||||
cursor += transaction.getOptimalEncodingMessageSize();
|
cursor += transaction.getOptimalEncodingMessageSize();
|
||||||
@ -153,7 +143,6 @@ public class AuxPoW extends ChildMessage implements Serializable {
|
|||||||
public int getOptimalEncodingMessageSize() {
|
public int getOptimalEncodingMessageSize() {
|
||||||
if (optimalEncodingMessageSize != 0)
|
if (optimalEncodingMessageSize != 0)
|
||||||
return optimalEncodingMessageSize;
|
return optimalEncodingMessageSize;
|
||||||
maybeParse();
|
|
||||||
if (optimalEncodingMessageSize != 0)
|
if (optimalEncodingMessageSize != 0)
|
||||||
return optimalEncodingMessageSize;
|
return optimalEncodingMessageSize;
|
||||||
optimalEncodingMessageSize = getMessageSize();
|
optimalEncodingMessageSize = getMessageSize();
|
||||||
@ -208,16 +197,6 @@ public class AuxPoW extends ChildMessage implements Serializable {
|
|||||||
return result;
|
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
|
* 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
|
* 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;
|
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
|
* Check the proof of work for this AuxPoW header meets the target
|
||||||
* difficulty.
|
* difficulty.
|
||||||
|
@ -19,14 +19,11 @@ package org.bitcoinj.core;
|
|||||||
|
|
||||||
import javax.annotation.Nullable;
|
import javax.annotation.Nullable;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.ObjectOutputStream;
|
|
||||||
import java.io.OutputStream;
|
import java.io.OutputStream;
|
||||||
import java.io.Serializable;
|
|
||||||
import java.security.MessageDigest;
|
import java.security.MessageDigest;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import static org.bitcoinj.core.Sha256Hash.newDigest;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A Merkle branch contains the hashes from a leaf of a Merkle tree
|
* 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
|
* TODO: Has a lot of similarity to PartialMerkleTree, should attempt to merge
|
||||||
* the two.
|
* the two.
|
||||||
*/
|
*/
|
||||||
public class MerkleBranch extends ChildMessage implements Serializable {
|
public class MerkleBranch extends ChildMessage {
|
||||||
private static final long serialVersionUID = 2;
|
private static final long serialVersionUID = 2;
|
||||||
|
|
||||||
// Merkle branches can be encoded in a way that will use more bytes than is optimal
|
// 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;
|
this.index = branchSideMask;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
protected void parseLite() throws ProtocolException {
|
|
||||||
length = calcLength(payload, offset);
|
|
||||||
cursor = offset + length;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static int calcLength(byte[] buf, int offset) {
|
public static int calcLength(byte[] buf, int offset) {
|
||||||
VarInt varint = new VarInt(buf, offset);
|
VarInt varint = new VarInt(buf, offset);
|
||||||
|
|
||||||
@ -102,10 +93,7 @@ public class MerkleBranch extends ChildMessage implements Serializable {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
void parse() throws ProtocolException {
|
protected void parse() throws ProtocolException {
|
||||||
if (parsed)
|
|
||||||
return;
|
|
||||||
|
|
||||||
cursor = offset;
|
cursor = offset;
|
||||||
|
|
||||||
final int hashCount = (int) readVarInt();
|
final int hashCount = (int) readVarInt();
|
||||||
@ -117,6 +105,7 @@ public class MerkleBranch extends ChildMessage implements Serializable {
|
|||||||
optimalEncodingMessageSize += 32 * hashCount;
|
optimalEncodingMessageSize += 32 * hashCount;
|
||||||
setIndex(readUint32());
|
setIndex(readUint32());
|
||||||
optimalEncodingMessageSize += 4;
|
optimalEncodingMessageSize += 4;
|
||||||
|
length = cursor - offset;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -196,7 +185,6 @@ public class MerkleBranch extends ChildMessage implements Serializable {
|
|||||||
public int getOptimalEncodingMessageSize() {
|
public int getOptimalEncodingMessageSize() {
|
||||||
if (optimalEncodingMessageSize != 0)
|
if (optimalEncodingMessageSize != 0)
|
||||||
return optimalEncodingMessageSize;
|
return optimalEncodingMessageSize;
|
||||||
maybeParse();
|
|
||||||
if (optimalEncodingMessageSize != 0)
|
if (optimalEncodingMessageSize != 0)
|
||||||
return optimalEncodingMessageSize;
|
return optimalEncodingMessageSize;
|
||||||
optimalEncodingMessageSize = getMessageSize();
|
optimalEncodingMessageSize = getMessageSize();
|
||||||
@ -211,26 +199,6 @@ public class MerkleBranch extends ChildMessage implements Serializable {
|
|||||||
return "Merkle branch";
|
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
|
@Override
|
||||||
public boolean equals(Object o) {
|
public boolean equals(Object o) {
|
||||||
if (this == o) return true;
|
if (this == o) return true;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user