mirror of
https://github.com/Qortal/altcoinj.git
synced 2025-02-14 19:25:51 +00:00
PeerAddress: Require NetworkParameters in constructors.
This commit is contained in:
parent
3177bd52a2
commit
9cbc45aeea
@ -71,7 +71,8 @@ public class PeerAddress extends ChildMessage {
|
|||||||
/**
|
/**
|
||||||
* Construct a peer address from a memorized or hardcoded address.
|
* 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.addr = checkNotNull(addr);
|
||||||
this.port = port;
|
this.port = port;
|
||||||
this.protocolVersion = protocolVersion;
|
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
|
* Constructs a peer address from the given IP address and port. Version number is default for the given parameters.
|
||||||
* 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.
|
|
||||||
*/
|
*/
|
||||||
public PeerAddress(NetworkParameters params, InetAddress addr, int port) {
|
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
|
* Constructs a peer address from the given IP address. Port and version number are default for the given
|
||||||
* are default for Bitcoin mainnet.
|
* parameters.
|
||||||
*/
|
|
||||||
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.
|
|
||||||
*/
|
*/
|
||||||
public PeerAddress(NetworkParameters params, InetAddress addr) {
|
public PeerAddress(NetworkParameters params, InetAddress addr) {
|
||||||
this(params, addr, MainNetParams.get().getPort());
|
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
|
* 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.
|
* 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());
|
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.
|
* Constructs a peer address from a stringified hostname+port. Use this if you want to connect to a Tor .onion address.
|
||||||
*/
|
*/
|
||||||
|
@ -101,8 +101,8 @@ public class VersionMessage extends Message {
|
|||||||
// We hard-code the IPv4 localhost address here rather than use InetAddress.getLocalHost() because some
|
// 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.
|
// mobile phones have broken localhost DNS entries, also, this is faster.
|
||||||
final byte[] localhost = { 127, 0, 0, 1 };
|
final byte[] localhost = { 127, 0, 0, 1 };
|
||||||
myAddr = new PeerAddress(InetAddress.getByAddress(localhost), params.getPort(), 0);
|
myAddr = new PeerAddress(params, InetAddress.getByAddress(localhost), params.getPort(), 0);
|
||||||
theirAddr = new PeerAddress(InetAddress.getByAddress(localhost), params.getPort(), 0);
|
theirAddr = new PeerAddress(params, InetAddress.getByAddress(localhost), params.getPort(), 0);
|
||||||
} catch (UnknownHostException e) {
|
} catch (UnknownHostException e) {
|
||||||
throw new RuntimeException(e); // Cannot happen (illegal IP length).
|
throw new RuntimeException(e); // Cannot happen (illegal IP length).
|
||||||
}
|
}
|
||||||
|
@ -39,7 +39,7 @@ public class PeerAddressTest
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testBitcoinSerialize() throws Exception {
|
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",
|
assertEquals("000000000000000000000000000000000000ffff7f000001208d",
|
||||||
Utils.HEX.encode(pa.bitcoinSerialize()));
|
Utils.HEX.encode(pa.bitcoinSerialize()));
|
||||||
}
|
}
|
||||||
|
@ -39,7 +39,7 @@ public class FetchBlock {
|
|||||||
BlockChain chain = new BlockChain(params, blockStore);
|
BlockChain chain = new BlockChain(params, blockStore);
|
||||||
PeerGroup peerGroup = new PeerGroup(params, chain);
|
PeerGroup peerGroup = new PeerGroup(params, chain);
|
||||||
peerGroup.start();
|
peerGroup.start();
|
||||||
PeerAddress addr = new PeerAddress(InetAddress.getLocalHost(), params.getPort());
|
PeerAddress addr = new PeerAddress(params, InetAddress.getLocalHost());
|
||||||
peerGroup.addAddress(addr);
|
peerGroup.addAddress(addr);
|
||||||
peerGroup.waitForPeers(1).get();
|
peerGroup.waitForPeers(1).get();
|
||||||
Peer peer = peerGroup.getConnectedPeers().get(0);
|
Peer peer = peerGroup.getConnectedPeers().get(0);
|
||||||
|
@ -40,7 +40,7 @@ public class FetchTransactions {
|
|||||||
BlockChain chain = new BlockChain(params, blockStore);
|
BlockChain chain = new BlockChain(params, blockStore);
|
||||||
PeerGroup peerGroup = new PeerGroup(params, chain);
|
PeerGroup peerGroup = new PeerGroup(params, chain);
|
||||||
peerGroup.start();
|
peerGroup.start();
|
||||||
peerGroup.addAddress(new PeerAddress(InetAddress.getLocalHost(), params.getPort()));
|
peerGroup.addAddress(new PeerAddress(params, InetAddress.getLocalHost()));
|
||||||
peerGroup.waitForPeers(1).get();
|
peerGroup.waitForPeers(1).get();
|
||||||
Peer peer = peerGroup.getConnectedPeers().get(0);
|
Peer peer = peerGroup.getConnectedPeers().get(0);
|
||||||
|
|
||||||
|
@ -82,7 +82,7 @@ public class PrintPeers {
|
|||||||
NioClientManager clientManager = new NioClientManager();
|
NioClientManager clientManager = new NioClientManager();
|
||||||
for (final InetAddress addr : addrs) {
|
for (final InetAddress addr : addrs) {
|
||||||
InetSocketAddress address = new InetSocketAddress(addr, params.getPort());
|
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();
|
final SettableFuture<Void> future = SettableFuture.create();
|
||||||
// Once the connection has completed version handshaking ...
|
// Once the connection has completed version handshaking ...
|
||||||
peer.addConnectedEventListener(new PeerConnectedEventListener() {
|
peer.addConnectedEventListener(new PeerConnectedEventListener() {
|
||||||
|
@ -63,7 +63,7 @@ public class PrivateKeys {
|
|||||||
BlockChain chain = new BlockChain(params, wallet, blockStore);
|
BlockChain chain = new BlockChain(params, wallet, blockStore);
|
||||||
|
|
||||||
final PeerGroup peerGroup = new PeerGroup(params, chain);
|
final PeerGroup peerGroup = new PeerGroup(params, chain);
|
||||||
peerGroup.addAddress(new PeerAddress(InetAddress.getLocalHost()));
|
peerGroup.addAddress(new PeerAddress(params, InetAddress.getLocalHost()));
|
||||||
peerGroup.startAsync();
|
peerGroup.startAsync();
|
||||||
peerGroup.downloadBlockChain();
|
peerGroup.downloadBlockChain();
|
||||||
peerGroup.stopAsync();
|
peerGroup.stopAsync();
|
||||||
|
@ -228,7 +228,7 @@ public class BuildCheckpoints {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private static void startPeerGroup(PeerGroup peerGroup, InetAddress ipAddress) {
|
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 + "...");
|
System.out.println("Connecting to " + peerAddress + "...");
|
||||||
peerGroup.addAddress(peerAddress);
|
peerGroup.addAddress(peerAddress);
|
||||||
peerGroup.start();
|
peerGroup.start();
|
||||||
|
@ -1211,7 +1211,7 @@ public class WalletTool {
|
|||||||
String[] peerAddrs = peersFlag.split(",");
|
String[] peerAddrs = peersFlag.split(",");
|
||||||
for (String peer : peerAddrs) {
|
for (String peer : peerAddrs) {
|
||||||
try {
|
try {
|
||||||
peers.addAddress(new PeerAddress(InetAddress.getByName(peer), params.getPort()));
|
peers.addAddress(new PeerAddress(params, InetAddress.getByName(peer)));
|
||||||
} catch (UnknownHostException e) {
|
} catch (UnknownHostException e) {
|
||||||
System.err.println("Could not understand peer domain name/IP address: " + peer + ": " + e.getMessage());
|
System.err.println("Could not understand peer domain name/IP address: " + peer + ": " + e.getMessage());
|
||||||
System.exit(1);
|
System.exit(1);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user