3
0
mirror of https://github.com/Qortal/altcoinj.git synced 2025-02-12 10:15:52 +00:00

Implement some (de)serialization (so we can roundtrip msgs in test)

Implements deserialization for getblocks/getheaders and supports
FilteredBlock entries in ListMessage (for getdata).
Implements serialization for FilteredBlock, HeadersMessage, and
PartialMerkleTree.
This commit is contained in:
Matt Corallo 2013-07-15 22:58:57 +02:00 committed by Mike Hearn
parent a724bcada5
commit 7011d68165
7 changed files with 48 additions and 3 deletions

View File

@ -213,6 +213,10 @@ public class BitcoinSerializer {
message = new FilteredBlock(params, payloadBytes); message = new FilteredBlock(params, payloadBytes);
} else if (command.equals("getdata")) { } else if (command.equals("getdata")) {
message = new GetDataMessage(params, payloadBytes, parseLazy, parseRetain, length); message = new GetDataMessage(params, payloadBytes, parseLazy, parseRetain, length);
} else if (command.equals("getblocks")) {
message = new GetBlocksMessage(params, payloadBytes);
} else if (command.equals("getheaders")) {
message = new GetHeadersMessage(params, payloadBytes);
} else if (command.equals("tx")) { } else if (command.equals("tx")) {
Transaction tx = new Transaction(params, payloadBytes, null, parseLazy, parseRetain, length); Transaction tx = new Transaction(params, payloadBytes, null, parseLazy, parseRetain, length);
if (hash != null) if (hash != null)

View File

@ -42,7 +42,11 @@ public class FilteredBlock extends Message {
} }
public void bitcoinSerializeToStream(OutputStream stream) throws IOException { public void bitcoinSerializeToStream(OutputStream stream) throws IOException {
throw new RuntimeException("Not implemented"); if (header.transactions == null)
header.bitcoinSerializeToStream(stream);
else
header.cloneAsHeader().bitcoinSerializeToStream(stream);
merkleTree.bitcoinSerializeToStream(stream);
} }
@Override @Override

View File

@ -38,8 +38,17 @@ public class GetBlocksMessage extends Message {
this.stopHash = stopHash; this.stopHash = stopHash;
} }
public GetBlocksMessage(NetworkParameters params, byte[] msg) throws ProtocolException {
super(params, msg, 0);
}
protected void parseLite() throws ProtocolException { protected void parseLite() throws ProtocolException {
// NOP. This is a root level message and should always be provided with a length. cursor = offset;
version = readUint32();
int startCount = (int) readVarInt();
if (startCount > 500)
throw new ProtocolException("Number of locators cannot be > 500, received: " + startCount);
length = (int) (cursor - offset + ((startCount + 1) * 32));
} }
public void parse() throws ProtocolException { public void parse() throws ProtocolException {

View File

@ -29,6 +29,10 @@ public class GetHeadersMessage extends GetBlocksMessage {
super(params, locator, stopHash); super(params, locator, stopHash);
} }
public GetHeadersMessage(NetworkParameters params, byte[] msg) throws ProtocolException {
super(params, msg);
}
@Override @Override
public String toString() { public String toString() {
StringBuffer b = new StringBuffer(); StringBuffer b = new StringBuffer();

View File

@ -19,6 +19,8 @@ package com.google.bitcoin.core;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.List; import java.util.List;
@ -45,6 +47,18 @@ public class HeadersMessage extends Message {
blockHeaders = Arrays.asList(headers); blockHeaders = Arrays.asList(headers);
} }
@Override
public void bitcoinSerializeToStream(OutputStream stream) throws IOException {
stream.write(new VarInt(blockHeaders.size()).encode());
for (Block header : blockHeaders) {
if (header.transactions == null)
header.bitcoinSerializeToStream(stream);
else
header.cloneAsHeader().bitcoinSerializeToStream(stream);
stream.write(0);
}
}
@Override @Override
protected void parseLite() throws ProtocolException { protected void parseLite() throws ProtocolException {
if (length == UNKNOWN_LENGTH) { if (length == UNKNOWN_LENGTH) {

View File

@ -99,6 +99,9 @@ public abstract class ListMessage extends Message {
case 2: case 2:
type = InventoryItem.Type.Block; type = InventoryItem.Type.Block;
break; break;
case 3:
type = InventoryItem.Type.FilteredBlock;
break;
default: default:
throw new ProtocolException("Unknown CInv type: " + typeCode); throw new ProtocolException("Unknown CInv type: " + typeCode);
} }

View File

@ -65,7 +65,14 @@ public class PartialMerkleTree extends Message {
} }
public void bitcoinSerializeToStream(OutputStream stream) throws IOException { public void bitcoinSerializeToStream(OutputStream stream) throws IOException {
throw new RuntimeException("Not implemented"); Utils.uint32ToByteStreamLE(transactionCount, stream);
stream.write(new VarInt(hashes.size()).encode());
for (Sha256Hash hash : hashes)
stream.write(Utils.reverseBytes(hash.getBytes()));
stream.write(new VarInt(matchedChildBits.length).encode());
stream.write(matchedChildBits);
} }
@Override @Override