From 228f30f66374dbf24a7012d57b71b3a0df72ffcb Mon Sep 17 00:00:00 2001 From: "Miron Cuperman (devrandom)" Date: Tue, 18 Oct 2011 17:06:12 +0000 Subject: [PATCH] Fix length and parseLazy handling. Resolves issue 92 --- src/com/google/bitcoin/core/Block.java | 2 +- src/com/google/bitcoin/core/Message.java | 17 +++++++++-------- 2 files changed, 10 insertions(+), 9 deletions(-) diff --git a/src/com/google/bitcoin/core/Block.java b/src/com/google/bitcoin/core/Block.java index b00b21073..3c1d6e7fc 100644 --- a/src/com/google/bitcoin/core/Block.java +++ b/src/com/google/bitcoin/core/Block.java @@ -179,7 +179,7 @@ public class Block extends Message { // provided we will have to // invoke a light parse of transactions to calculate the length. if (length == UNKNOWN_LENGTH) { - log.warn("Performing lite parse of block transaction as block was initialised from byte array without providing length. This should never need to happen."); + assert !parseLazy : "Performing lite parse of block transaction as block was initialised from byte array without providing length. This should never need to happen." + " parseLazy: " + parseLazy; parseTransactions(); length = cursor - offset; } else { diff --git a/src/com/google/bitcoin/core/Message.java b/src/com/google/bitcoin/core/Message.java index cc3d68e45..112245edc 100644 --- a/src/com/google/bitcoin/core/Message.java +++ b/src/com/google/bitcoin/core/Message.java @@ -100,11 +100,8 @@ public abstract class Message implements Serializable { parse(); parsed = true; } - -// assert (parseLazy ? !parsed : parsed && (parseRetain ? bytes != null : bytes == null)) -// : "parseLazy : " + parseLazy + " parsed: " + parsed -// + " parseRetain:" + parseRetain -// + " bytes == null" + bytes == null; + + assert this.length != UNKNOWN_LENGTH: "Length field has not been set in constructor for " + getClass().getSimpleName() + " after " + (parseLazy ? "lite" : "full") + " parse. Refer to Message.parseLite() for detail of required Length field contract."; if (SELF_CHECK && !this.getClass().getSimpleName().equals("VersionMessage")) { checkParse(); @@ -140,7 +137,10 @@ public abstract class Message implements Serializable { * This is only required for subclasses of ChildClass as root level messages will have their length passed * into the constructor. *

- * It is expected that the length field will be set before this method returns. + * Implementations should adhere to the following contract: If parseLazy = true the 'length' + * field must be set before returning. If parseLazy = false the length field must be set either + * within the parseLite() method OR the parse() method. The overriding requirement is that length + * must be set to non UNKNOWN_MESSAGE value by the time the constructor exits. * * @return * @throws ProtocolException @@ -372,8 +372,9 @@ public abstract class Message implements Serializable { if (length != UNKNOWN_LENGTH) return length; checkParse(); - if (length != UNKNOWN_LENGTH) - length = cursor - offset; + assert length != UNKNOWN_LENGTH: "Length field has not been set in " + getClass().getSimpleName() + " after full parse."; + //if (length == UNKNOWN_LENGTH) + // length = cursor - offset; return length; }