From 2bd81b70447b1b07fba65439e92eef52ed2493a2 Mon Sep 17 00:00:00 2001 From: Craig Raw Date: Tue, 2 Jun 2020 09:51:42 +0200 Subject: [PATCH] refactor transactionpart to message and childmessage --- .../drongo/protocol/BlockHeader.java | 4 +++ .../drongo/protocol/ChildMessage.java | 30 +++++++++++++++++++ .../{TransactionPart.java => Message.java} | 20 ++----------- .../drongo/protocol/Transaction.java | 3 +- .../drongo/protocol/TransactionInput.java | 2 +- .../drongo/protocol/TransactionOutPoint.java | 4 +-- .../drongo/protocol/TransactionOutput.java | 2 +- .../drongo/protocol/TransactionWitness.java | 2 +- 8 files changed, 43 insertions(+), 24 deletions(-) create mode 100644 src/main/java/com/sparrowwallet/drongo/protocol/BlockHeader.java create mode 100644 src/main/java/com/sparrowwallet/drongo/protocol/ChildMessage.java rename src/main/java/com/sparrowwallet/drongo/protocol/{TransactionPart.java => Message.java} (89%) diff --git a/src/main/java/com/sparrowwallet/drongo/protocol/BlockHeader.java b/src/main/java/com/sparrowwallet/drongo/protocol/BlockHeader.java new file mode 100644 index 0000000..01fa265 --- /dev/null +++ b/src/main/java/com/sparrowwallet/drongo/protocol/BlockHeader.java @@ -0,0 +1,4 @@ +package com.sparrowwallet.drongo.protocol; + +public class BlockHeader { +} diff --git a/src/main/java/com/sparrowwallet/drongo/protocol/ChildMessage.java b/src/main/java/com/sparrowwallet/drongo/protocol/ChildMessage.java new file mode 100644 index 0000000..e99e634 --- /dev/null +++ b/src/main/java/com/sparrowwallet/drongo/protocol/ChildMessage.java @@ -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); + } + } +} diff --git a/src/main/java/com/sparrowwallet/drongo/protocol/TransactionPart.java b/src/main/java/com/sparrowwallet/drongo/protocol/Message.java similarity index 89% rename from src/main/java/com/sparrowwallet/drongo/protocol/TransactionPart.java rename to src/main/java/com/sparrowwallet/drongo/protocol/Message.java index 73d011a..f00bfd1 100644 --- a/src/main/java/com/sparrowwallet/drongo/protocol/TransactionPart.java +++ b/src/main/java/com/sparrowwallet/drongo/protocol/Message.java @@ -7,8 +7,8 @@ import org.slf4j.LoggerFactory; import java.io.IOException; import java.io.OutputStream; -public abstract class TransactionPart { - private static final Logger log = LoggerFactory.getLogger(TransactionPart.class); +public abstract class Message { + private static final Logger log = LoggerFactory.getLogger(Message.class); public static final int MAX_SIZE = 0x02000000; // 32MB 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. protected int cursor; - protected TransactionPart parent; - protected int length = UNKNOWN_LENGTH; - public TransactionPart(byte[] rawtx, int offset) { + public Message(byte[] rawtx, int offset) { this.rawtx = rawtx; this.cursor = this.offset = offset; @@ -34,14 +32,6 @@ public abstract class TransactionPart { protected abstract void parse() throws ProtocolException; - public TransactionPart getParent() { - return parent; - } - - public final void setParent(TransactionPart parent) { - this.parent = parent; - } - public int getOffset() { 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. else if (newArraySize != 0) length += VarInt.sizeOf(newArraySize) - VarInt.sizeOf(newArraySize - 1); - - if (parent != null) { - parent.adjustLength(newArraySize, adjustment); - } } } diff --git a/src/main/java/com/sparrowwallet/drongo/protocol/Transaction.java b/src/main/java/com/sparrowwallet/drongo/protocol/Transaction.java index 42e3a67..e4d8748 100644 --- a/src/main/java/com/sparrowwallet/drongo/protocol/Transaction.java +++ b/src/main/java/com/sparrowwallet/drongo/protocol/Transaction.java @@ -2,7 +2,6 @@ package com.sparrowwallet.drongo.protocol; import com.sparrowwallet.drongo.Utils; import com.sparrowwallet.drongo.address.Address; -import com.sparrowwallet.drongo.address.P2PKHAddress; import com.sparrowwallet.drongo.crypto.ECKey; 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.uint64ToByteStreamLE; -public class Transaction extends TransactionPart { +public class Transaction extends ChildMessage { public static final int MAX_BLOCK_SIZE = 1000 * 1000; public static final long MAX_BITCOIN = 21 * 1000 * 1000L; public static final long SATOSHIS_PER_BITCOIN = 100 * 1000 * 1000L; diff --git a/src/main/java/com/sparrowwallet/drongo/protocol/TransactionInput.java b/src/main/java/com/sparrowwallet/drongo/protocol/TransactionInput.java index 49cc2c4..450ef79 100644 --- a/src/main/java/com/sparrowwallet/drongo/protocol/TransactionInput.java +++ b/src/main/java/com/sparrowwallet/drongo/protocol/TransactionInput.java @@ -5,7 +5,7 @@ import com.sparrowwallet.drongo.Utils; import java.io.IOException; 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_RBF_ENABLED = 4294967293L; public static final long MAX_RELATIVE_TIMELOCK = 2147483647L; diff --git a/src/main/java/com/sparrowwallet/drongo/protocol/TransactionOutPoint.java b/src/main/java/com/sparrowwallet/drongo/protocol/TransactionOutPoint.java index 2002eb9..f7c60d5 100644 --- a/src/main/java/com/sparrowwallet/drongo/protocol/TransactionOutPoint.java +++ b/src/main/java/com/sparrowwallet/drongo/protocol/TransactionOutPoint.java @@ -7,7 +7,7 @@ import java.io.IOException; import java.io.OutputStream; import java.util.Objects; -public class TransactionOutPoint extends TransactionPart { +public class TransactionOutPoint extends ChildMessage { static final int MESSAGE_LENGTH = 36; @@ -18,7 +18,7 @@ public class TransactionOutPoint extends TransactionPart { 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); setParent(parent); } diff --git a/src/main/java/com/sparrowwallet/drongo/protocol/TransactionOutput.java b/src/main/java/com/sparrowwallet/drongo/protocol/TransactionOutput.java index 0fd4f58..c9d44a1 100644 --- a/src/main/java/com/sparrowwallet/drongo/protocol/TransactionOutput.java +++ b/src/main/java/com/sparrowwallet/drongo/protocol/TransactionOutput.java @@ -7,7 +7,7 @@ import java.io.ByteArrayOutputStream; import java.io.IOException; 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. private long value; diff --git a/src/main/java/com/sparrowwallet/drongo/protocol/TransactionWitness.java b/src/main/java/com/sparrowwallet/drongo/protocol/TransactionWitness.java index f2632e6..5aeb2b0 100644 --- a/src/main/java/com/sparrowwallet/drongo/protocol/TransactionWitness.java +++ b/src/main/java/com/sparrowwallet/drongo/protocol/TransactionWitness.java @@ -10,7 +10,7 @@ import java.util.Arrays; import java.util.Collections; import java.util.List; -public class TransactionWitness extends TransactionPart { +public class TransactionWitness extends ChildMessage { private List pushes; public TransactionWitness(Transaction parent, byte[] rawtx, int offset) {