From 65bb4a20f8674e48abe1b09de249e3d1ef45ea84 Mon Sep 17 00:00:00 2001 From: Mike Hearn Date: Fri, 24 Jun 2011 13:03:23 +0000 Subject: [PATCH] Fix PeerAddress serialization and add a test. Patch from Noa Resare. Fixes issue 29. --- src/com/google/bitcoin/core/PeerAddress.java | 6 ++-- .../google/bitcoin/core/PeerAddressTest.java | 36 +++++++++++++++++++ 2 files changed, 39 insertions(+), 3 deletions(-) create mode 100644 tests/com/google/bitcoin/core/PeerAddressTest.java diff --git a/src/com/google/bitcoin/core/PeerAddress.java b/src/com/google/bitcoin/core/PeerAddress.java index f5c627a8..f4aae94b 100644 --- a/src/com/google/bitcoin/core/PeerAddress.java +++ b/src/com/google/bitcoin/core/PeerAddress.java @@ -53,7 +53,7 @@ public class PeerAddress extends Message { int secs = (int)(new Date().getTime() / 1000); uint32ToByteStreamLE(secs, stream); } - uint64ToByteStreamLE(BigInteger.ZERO, stream); // nServices. + uint64ToByteStreamLE(services, stream); // nServices. // Java does not provide any utility to map an IPv4 address into IPv6 space, so we have to do it by hand. byte[] ipBytes = addr.getAddress(); if (ipBytes.length == 4) { @@ -64,9 +64,9 @@ public class PeerAddress extends Message { ipBytes = v6addr; } stream.write(ipBytes); - // And write out the port. + // And write out the port. Unlike the rest of the protocol, address and port is in big endian byte order. + stream.write((byte) (0xFF & port >> 8)); stream.write((byte) (0xFF & port)); - stream.write((byte) (0xFF & (port >> 8))); } @Override diff --git a/tests/com/google/bitcoin/core/PeerAddressTest.java b/tests/com/google/bitcoin/core/PeerAddressTest.java new file mode 100644 index 00000000..9a6ee711 --- /dev/null +++ b/tests/com/google/bitcoin/core/PeerAddressTest.java @@ -0,0 +1,36 @@ +/** + * Copyright 2011 Noa Resare + * + * 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 com.google.bitcoin.core; + +import com.google.bitcoin.bouncycastle.util.encoders.Hex; +import org.junit.Test; + +import static org.junit.Assert.assertEquals; + +public class PeerAddressTest +{ + @Test + public void testPeerAddressSerialize() throws Exception + { + // copied verbatim from https://en.bitcoin.it/wiki/Protocol_specification#Network_address + String fromSpec = "010000000000000000000000000000000000ffff0a000001208d"; + PeerAddress pa = new PeerAddress(NetworkParameters.prodNet(), + Hex.decode(fromSpec),0,0); + String reserialized = Utils.bytesToHexString(pa.bitcoinSerialize()); + assertEquals(reserialized,fromSpec ); + } +}