Skip to content

Commit

Permalink
Remove lazy parsing of messages.
Browse files Browse the repository at this point in the history
In particular:

* Message.parse(): is now always called once, in the Message constructor
* Message.parsed: removed
* Message.parseLite(): folded into Message.parse()
* Message.maybeParse(): removed
* Message.ensureParsed(): removed
* Message.isParsed(): removed
* Block.parseHeader(): folded into Block.parse()
* Block.parseTransactions(): folded into Block.parse()
* Block.maybeParseHeader(): removed
* Block.maybeParseTransactions(): removed
* Block.ensureParsedHeader(): removed
* Block.ensureParsedTransactions(): removed
* Block.isParsedHeader(): removed
* Block.isParsedTransasctions(): removed
* MessageSerializer.isParseLazyMode(): removed
* BitcoinSerializer.parseLazy: removed
* BitcoinSerializer.getSerializer(): parseLazy parameter removed
* LazyParseException: removed
* LazyParseByteCacheTest: renamed to ParseByteCacheTest
  • Loading branch information
schildbach committed Jul 28, 2015
1 parent d753d28 commit 7744a00
Show file tree
Hide file tree
Showing 32 changed files with 211 additions and 816 deletions.
43 changes: 21 additions & 22 deletions core/src/main/java/org/bitcoinj/core/AddressMessage.java
Original file line number Diff line number Diff line change
@@ -1,3 +1,20 @@
/*
* Copyright 2011 Google Inc.
* Copyright 2014 Andreas Schildbach
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.bitcoinj.core;

import java.io.IOException;
Expand All @@ -20,7 +37,6 @@ public class AddressMessage extends Message {
* Contruct a new 'addr' message.
* @param params NetworkParameters object.
* @param offset The location of the first payload byte within the array.
* @param parseLazy Whether to perform a full parse immediately or delay until a read is requested.
* @param parseRetain Whether to retain the backing byte array for quick reserialization.
* If true and the backing byte array is invalidated due to modification of a field then
* the cached bytes may be repopulated and retained if the message is serialized again in the future.
Expand Down Expand Up @@ -53,11 +69,7 @@ public class AddressMessage extends Message {
}

@Override
protected void parseLite() throws ProtocolException {
}

@Override
void parse() throws ProtocolException {
protected void parse() throws ProtocolException {
long numAddresses = readVarInt();
// Guard against ultra large messages that will crash us.
if (numAddresses > MAX_ADDRESSES)
Expand All @@ -68,7 +80,9 @@ void parse() throws ProtocolException {
addresses.add(addr);
cursor += addr.getMessageSize();
}
length = cursor - offset;
length = new VarInt(addresses.size()).getSizeInBytes();
// The 4 byte difference is the uint32 timestamp that was introduced in version 31402
length += addresses.size() * (protocolVersion > 31402 ? PeerAddress.MESSAGE_SIZE : PeerAddress.MESSAGE_SIZE - 4);
}

/* (non-Javadoc)
Expand All @@ -84,29 +98,15 @@ void bitcoinSerializeToStream(OutputStream stream) throws IOException {
}
}

@Override
public int getMessageSize() {
if (length != UNKNOWN_LENGTH)
return length;
if (addresses != null) {
length = new VarInt(addresses.size()).getSizeInBytes();
// The 4 byte difference is the uint32 timestamp that was introduced in version 31402
length += addresses.size() * (protocolVersion > 31402 ? PeerAddress.MESSAGE_SIZE : PeerAddress.MESSAGE_SIZE - 4);
}
return length;
}

/**
* @return An unmodifiableList view of the backing List of addresses. Addresses contained within the list may be safely modified.
*/
public List<PeerAddress> getAddresses() {
maybeParse();
return Collections.unmodifiableList(addresses);
}

public void addAddress(PeerAddress address) {
unCache();
maybeParse();
address.setParent(this);
addresses.add(address);
if (length == UNKNOWN_LENGTH)
Expand All @@ -129,5 +129,4 @@ public void removeAddress(int index) {
public String toString() {
return "addr: " + Utils.join(addresses);
}

}
8 changes: 2 additions & 6 deletions core/src/main/java/org/bitcoinj/core/AlertMessage.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/*
* Copyright 2011 Google Inc.
* Copyright 2015 Andreas Schildbach
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -61,7 +62,7 @@ public String toString() {
}

@Override
void parse() throws ProtocolException {
protected void parse() throws ProtocolException {
// Alerts are formatted in two levels. The top level contains two byte arrays: a signature, and a serialized
// data structure containing the actual alert data.
int startPos = cursor;
Expand Down Expand Up @@ -115,11 +116,6 @@ public boolean isSignatureValid() {
return ECKey.verify(Sha256Hash.hashTwice(content), signature, params.getAlertSigningKey());
}

@Override
protected void parseLite() throws ProtocolException {
// Do nothing, lazy parsing isn't useful for alerts.
}

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Field accessors.

Expand Down
29 changes: 1 addition & 28 deletions core/src/main/java/org/bitcoinj/core/BitcoinSerializer.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ public class BitcoinSerializer implements MessageSerializer {
private static final int COMMAND_LEN = 12;

private final NetworkParameters params;
private final boolean parseLazy;
private final boolean parseRetain;

private static final Map<Class<? extends Message>, String> names = new HashMap<Class<? extends Message>, String>();
Expand Down Expand Up @@ -74,32 +73,14 @@ public class BitcoinSerializer implements MessageSerializer {
names.put(UTXOsMessage.class, "utxos");
}

/**
* Constructs a partial BitcoinSerializer with the given behavior. This is
* intended for use by messages which do not understand the network they
* belong to.
*
* @param parseLazy deserialize messages in lazy mode.
* @param parseRetain retain the backing byte array of a message for fast reserialization.
* @deprecated use BitcoinSerializer(NetworkParameters, boolean, boolean) instead.
*/
@Deprecated
BitcoinSerializer(boolean parseLazy, boolean parseRetain) {
this.params = null;
this.parseLazy = parseLazy;
this.parseRetain = parseRetain;
}

/**
* Constructs a BitcoinSerializer with the given behavior.
*
* @param params networkParams used to create Messages instances and termining packetMagic
* @param parseLazy deserialize messages in lazy mode.
* @param parseRetain retain the backing byte array of a message for fast reserialization.
*/
public BitcoinSerializer(NetworkParameters params, boolean parseLazy, boolean parseRetain) {
public BitcoinSerializer(NetworkParameters params, boolean parseRetain) {
this.params = params;
this.parseLazy = parseLazy;
this.parseRetain = parseRetain;
}

Expand Down Expand Up @@ -370,14 +351,6 @@ public void seekPastMagicBytes(ByteBuffer in) throws BufferUnderflowException {
}
}

/**
* Whether the serializer will produce lazy parse mode Messages
*/
@Override
public boolean isParseLazyMode() {
return parseLazy;
}

/**
* Whether the serializer will produce cached mode Messages
*/
Expand Down

0 comments on commit 7744a00

Please sign in to comment.