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

PeerAddress: Require NetworkParameters in constructors.

This commit is contained in:
Andreas Schildbach 2017-01-10 11:23:25 +01:00
parent 3177bd52a2
commit 9cbc45aeea
9 changed files with 15 additions and 51 deletions

View File

@ -71,7 +71,8 @@ public class PeerAddress extends ChildMessage {
/**
* Construct a peer address from a memorized or hardcoded address.
*/
public PeerAddress(InetAddress addr, int port, int protocolVersion) {
public PeerAddress(NetworkParameters params, InetAddress addr, int port, int protocolVersion) {
super(params);
this.addr = checkNotNull(addr);
this.port = port;
this.protocolVersion = protocolVersion;
@ -80,46 +81,20 @@ public class PeerAddress extends ChildMessage {
}
/**
* Constructs a peer address from the given IP address and port. Protocol version is the default
* for Bitcoin.
*/
public PeerAddress(InetAddress addr, int port) {
this(addr, port, NetworkParameters.ProtocolVersion.CURRENT.getBitcoinProtocolVersion());
}
/**
* Constructs a peer address from the given IP address and port.
* Constructs a peer address from the given IP address and port. Version number is default for the given parameters.
*/
public PeerAddress(NetworkParameters params, InetAddress addr, int port) {
this(addr, port, params.getProtocolVersionNum(NetworkParameters.ProtocolVersion.CURRENT));
this(params, addr, port, params.getProtocolVersionNum(NetworkParameters.ProtocolVersion.CURRENT));
}
/**
* Constructs a peer address from the given IP address. Port and version number
* are default for Bitcoin mainnet.
*/
public PeerAddress(InetAddress addr) {
this(addr, MainNetParams.get().getPort());
}
/**
* Constructs a peer address from the given IP address. Port is default for
* Bitcoin mainnet, version number is default for the given parameters.
* Constructs a peer address from the given IP address. Port and version number are default for the given
* parameters.
*/
public PeerAddress(NetworkParameters params, InetAddress addr) {
this(params, addr, MainNetParams.get().getPort());
}
/**
* Constructs a peer address from an {@link InetSocketAddress}. An InetSocketAddress can take in as parameters an
* InetAddress or a String hostname. If you want to connect to a .onion, set the hostname to the .onion address.
* Protocol version is the default. Protocol version is the default
* for Bitcoin.
*/
public PeerAddress(InetSocketAddress addr) {
this(addr.getAddress(), addr.getPort(), NetworkParameters.ProtocolVersion.CURRENT.getBitcoinProtocolVersion());
}
/**
* Constructs a peer address from an {@link InetSocketAddress}. An InetSocketAddress can take in as parameters an
* InetAddress or a String hostname. If you want to connect to a .onion, set the hostname to the .onion address.
@ -128,17 +103,6 @@ public class PeerAddress extends ChildMessage {
this(params, addr.getAddress(), addr.getPort());
}
/**
* Constructs a peer address from a stringified hostname+port. Use this if you want to connect to a Tor .onion address.
* Protocol version is the default for Bitcoin.
*/
public PeerAddress(String hostname, int port) {
this.hostname = hostname;
this.port = port;
this.protocolVersion = NetworkParameters.ProtocolVersion.CURRENT.getBitcoinProtocolVersion();
this.services = BigInteger.ZERO;
}
/**
* Constructs a peer address from a stringified hostname+port. Use this if you want to connect to a Tor .onion address.
*/

View File

@ -101,8 +101,8 @@ public class VersionMessage extends Message {
// We hard-code the IPv4 localhost address here rather than use InetAddress.getLocalHost() because some
// mobile phones have broken localhost DNS entries, also, this is faster.
final byte[] localhost = { 127, 0, 0, 1 };
myAddr = new PeerAddress(InetAddress.getByAddress(localhost), params.getPort(), 0);
theirAddr = new PeerAddress(InetAddress.getByAddress(localhost), params.getPort(), 0);
myAddr = new PeerAddress(params, InetAddress.getByAddress(localhost), params.getPort(), 0);
theirAddr = new PeerAddress(params, InetAddress.getByAddress(localhost), params.getPort(), 0);
} catch (UnknownHostException e) {
throw new RuntimeException(e); // Cannot happen (illegal IP length).
}

View File

@ -39,7 +39,7 @@ public class PeerAddressTest
@Test
public void testBitcoinSerialize() throws Exception {
PeerAddress pa = new PeerAddress(InetAddress.getByName(null), 8333, 0);
PeerAddress pa = new PeerAddress(MainNetParams.get(), InetAddress.getByName(null), 8333, 0);
assertEquals("000000000000000000000000000000000000ffff7f000001208d",
Utils.HEX.encode(pa.bitcoinSerialize()));
}

View File

@ -39,7 +39,7 @@ public class FetchBlock {
BlockChain chain = new BlockChain(params, blockStore);
PeerGroup peerGroup = new PeerGroup(params, chain);
peerGroup.start();
PeerAddress addr = new PeerAddress(InetAddress.getLocalHost(), params.getPort());
PeerAddress addr = new PeerAddress(params, InetAddress.getLocalHost());
peerGroup.addAddress(addr);
peerGroup.waitForPeers(1).get();
Peer peer = peerGroup.getConnectedPeers().get(0);

View File

@ -40,7 +40,7 @@ public class FetchTransactions {
BlockChain chain = new BlockChain(params, blockStore);
PeerGroup peerGroup = new PeerGroup(params, chain);
peerGroup.start();
peerGroup.addAddress(new PeerAddress(InetAddress.getLocalHost(), params.getPort()));
peerGroup.addAddress(new PeerAddress(params, InetAddress.getLocalHost()));
peerGroup.waitForPeers(1).get();
Peer peer = peerGroup.getConnectedPeers().get(0);

View File

@ -82,7 +82,7 @@ public class PrintPeers {
NioClientManager clientManager = new NioClientManager();
for (final InetAddress addr : addrs) {
InetSocketAddress address = new InetSocketAddress(addr, params.getPort());
final Peer peer = new Peer(params, new VersionMessage(params, 0), null, new PeerAddress(address));
final Peer peer = new Peer(params, new VersionMessage(params, 0), null, new PeerAddress(params, address));
final SettableFuture<Void> future = SettableFuture.create();
// Once the connection has completed version handshaking ...
peer.addConnectedEventListener(new PeerConnectedEventListener() {

View File

@ -63,7 +63,7 @@ public class PrivateKeys {
BlockChain chain = new BlockChain(params, wallet, blockStore);
final PeerGroup peerGroup = new PeerGroup(params, chain);
peerGroup.addAddress(new PeerAddress(InetAddress.getLocalHost()));
peerGroup.addAddress(new PeerAddress(params, InetAddress.getLocalHost()));
peerGroup.startAsync();
peerGroup.downloadBlockChain();
peerGroup.stopAsync();

View File

@ -228,7 +228,7 @@ public class BuildCheckpoints {
}
private static void startPeerGroup(PeerGroup peerGroup, InetAddress ipAddress) {
final PeerAddress peerAddress = new PeerAddress(ipAddress, params.getPort());
final PeerAddress peerAddress = new PeerAddress(params, ipAddress);
System.out.println("Connecting to " + peerAddress + "...");
peerGroup.addAddress(peerAddress);
peerGroup.start();

View File

@ -1211,7 +1211,7 @@ public class WalletTool {
String[] peerAddrs = peersFlag.split(",");
for (String peer : peerAddrs) {
try {
peers.addAddress(new PeerAddress(InetAddress.getByName(peer), params.getPort()));
peers.addAddress(new PeerAddress(params, InetAddress.getByName(peer)));
} catch (UnknownHostException e) {
System.err.println("Could not understand peer domain name/IP address: " + peer + ": " + e.getMessage());
System.exit(1);