3
0
mirror of https://github.com/Qortal/altcoinj.git synced 2025-01-31 07:12:17 +00:00

Fix PeerAddress serialization and add a test. Patch from Noa Resare. Fixes issue 29.

This commit is contained in:
Mike Hearn 2011-06-24 13:03:23 +00:00
parent d1385d50df
commit 65bb4a20f8
2 changed files with 39 additions and 3 deletions

View File

@ -53,7 +53,7 @@ public class PeerAddress extends Message {
int secs = (int)(new Date().getTime() / 1000); int secs = (int)(new Date().getTime() / 1000);
uint32ToByteStreamLE(secs, stream); 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. // 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(); byte[] ipBytes = addr.getAddress();
if (ipBytes.length == 4) { if (ipBytes.length == 4) {
@ -64,9 +64,9 @@ public class PeerAddress extends Message {
ipBytes = v6addr; ipBytes = v6addr;
} }
stream.write(ipBytes); 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));
stream.write((byte) (0xFF & (port >> 8)));
} }
@Override @Override

View File

@ -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 );
}
}