refactor transactionpart to message and childmessage

This commit is contained in:
Craig Raw 2020-06-02 09:51:42 +02:00
parent a25d020e54
commit 2bd81b7044
8 changed files with 43 additions and 24 deletions

View file

@ -0,0 +1,4 @@
package com.sparrowwallet.drongo.protocol;
public class BlockHeader {
}

View file

@ -0,0 +1,30 @@
package com.sparrowwallet.drongo.protocol;
public abstract class ChildMessage extends Message {
protected Message parent;
public ChildMessage(byte[] rawtx, int offset) {
super(rawtx, offset);
}
public Message getParent() {
return parent;
}
public final void setParent(Message parent) {
this.parent = parent;
}
protected void adjustLength(int adjustment) {
adjustLength(0, adjustment);
}
protected void adjustLength(int newArraySize, int adjustment) {
super.adjustLength(newArraySize, adjustment);
if(parent != null) {
parent.adjustLength(newArraySize, adjustment);
}
}
}

View file

@ -7,8 +7,8 @@ import org.slf4j.LoggerFactory;
import java.io.IOException; import java.io.IOException;
import java.io.OutputStream; import java.io.OutputStream;
public abstract class TransactionPart { public abstract class Message {
private static final Logger log = LoggerFactory.getLogger(TransactionPart.class); private static final Logger log = LoggerFactory.getLogger(Message.class);
public static final int MAX_SIZE = 0x02000000; // 32MB public static final int MAX_SIZE = 0x02000000; // 32MB
public static final int UNKNOWN_LENGTH = Integer.MIN_VALUE; public static final int UNKNOWN_LENGTH = Integer.MIN_VALUE;
@ -21,11 +21,9 @@ public abstract class TransactionPart {
// Note that it's relative to the start of the array NOT the start of the message payload. // Note that it's relative to the start of the array NOT the start of the message payload.
protected int cursor; protected int cursor;
protected TransactionPart parent;
protected int length = UNKNOWN_LENGTH; protected int length = UNKNOWN_LENGTH;
public TransactionPart(byte[] rawtx, int offset) { public Message(byte[] rawtx, int offset) {
this.rawtx = rawtx; this.rawtx = rawtx;
this.cursor = this.offset = offset; this.cursor = this.offset = offset;
@ -34,14 +32,6 @@ public abstract class TransactionPart {
protected abstract void parse() throws ProtocolException; protected abstract void parse() throws ProtocolException;
public TransactionPart getParent() {
return parent;
}
public final void setParent(TransactionPart parent) {
this.parent = parent;
}
public int getOffset() { public int getOffset() {
return offset; return offset;
} }
@ -137,9 +127,5 @@ public abstract class TransactionPart {
length++; // The assumption here is we never call adjustLength with the same arraySize as before. length++; // The assumption here is we never call adjustLength with the same arraySize as before.
else if (newArraySize != 0) else if (newArraySize != 0)
length += VarInt.sizeOf(newArraySize) - VarInt.sizeOf(newArraySize - 1); length += VarInt.sizeOf(newArraySize) - VarInt.sizeOf(newArraySize - 1);
if (parent != null) {
parent.adjustLength(newArraySize, adjustment);
}
} }
} }

View file

@ -2,7 +2,6 @@ package com.sparrowwallet.drongo.protocol;
import com.sparrowwallet.drongo.Utils; import com.sparrowwallet.drongo.Utils;
import com.sparrowwallet.drongo.address.Address; import com.sparrowwallet.drongo.address.Address;
import com.sparrowwallet.drongo.address.P2PKHAddress;
import com.sparrowwallet.drongo.crypto.ECKey; import com.sparrowwallet.drongo.crypto.ECKey;
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
@ -17,7 +16,7 @@ import java.util.List;
import static com.sparrowwallet.drongo.Utils.uint32ToByteStreamLE; import static com.sparrowwallet.drongo.Utils.uint32ToByteStreamLE;
import static com.sparrowwallet.drongo.Utils.uint64ToByteStreamLE; import static com.sparrowwallet.drongo.Utils.uint64ToByteStreamLE;
public class Transaction extends TransactionPart { public class Transaction extends ChildMessage {
public static final int MAX_BLOCK_SIZE = 1000 * 1000; public static final int MAX_BLOCK_SIZE = 1000 * 1000;
public static final long MAX_BITCOIN = 21 * 1000 * 1000L; public static final long MAX_BITCOIN = 21 * 1000 * 1000L;
public static final long SATOSHIS_PER_BITCOIN = 100 * 1000 * 1000L; public static final long SATOSHIS_PER_BITCOIN = 100 * 1000 * 1000L;

View file

@ -5,7 +5,7 @@ import com.sparrowwallet.drongo.Utils;
import java.io.IOException; import java.io.IOException;
import java.io.OutputStream; import java.io.OutputStream;
public class TransactionInput extends TransactionPart { public class TransactionInput extends ChildMessage {
public static final long SEQUENCE_LOCKTIME_DISABLED = 4294967295L; public static final long SEQUENCE_LOCKTIME_DISABLED = 4294967295L;
public static final long SEQUENCE_RBF_ENABLED = 4294967293L; public static final long SEQUENCE_RBF_ENABLED = 4294967293L;
public static final long MAX_RELATIVE_TIMELOCK = 2147483647L; public static final long MAX_RELATIVE_TIMELOCK = 2147483647L;

View file

@ -7,7 +7,7 @@ import java.io.IOException;
import java.io.OutputStream; import java.io.OutputStream;
import java.util.Objects; import java.util.Objects;
public class TransactionOutPoint extends TransactionPart { public class TransactionOutPoint extends ChildMessage {
static final int MESSAGE_LENGTH = 36; static final int MESSAGE_LENGTH = 36;
@ -18,7 +18,7 @@ public class TransactionOutPoint extends TransactionPart {
private Address[] addresses = new Address[0]; private Address[] addresses = new Address[0];
public TransactionOutPoint(byte[] rawtx, int offset, TransactionPart parent) { public TransactionOutPoint(byte[] rawtx, int offset, Message parent) {
super(rawtx, offset); super(rawtx, offset);
setParent(parent); setParent(parent);
} }

View file

@ -7,7 +7,7 @@ import java.io.ByteArrayOutputStream;
import java.io.IOException; import java.io.IOException;
import java.io.OutputStream; import java.io.OutputStream;
public class TransactionOutput extends TransactionPart { public class TransactionOutput extends ChildMessage {
// The output's value is kept as a native type in order to save class instances. // The output's value is kept as a native type in order to save class instances.
private long value; private long value;

View file

@ -10,7 +10,7 @@ import java.util.Arrays;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
public class TransactionWitness extends TransactionPart { public class TransactionWitness extends ChildMessage {
private List<byte[]> pushes; private List<byte[]> pushes;
public TransactionWitness(Transaction parent, byte[] rawtx, int offset) { public TransactionWitness(Transaction parent, byte[] rawtx, int offset) {