public VersionMessage duplicate() { VersionMessage v = new VersionMessage(params, (int) bestHeight); v.clientVersion = clientVersion; v.localServices = localServices; v.time = time; v.myAddr = myAddr; v.theirAddr = theirAddr; v.subVer = subVer; v.relayTxesBeforeFilter = relayTxesBeforeFilter; return v; }
public VersionMessage duplicate() { VersionMessage v = new VersionMessage(params, (int) bestHeight); v.clientVersion = clientVersion; v.localServices = localServices; v.time = time; v.myAddr = myAddr; v.theirAddr = theirAddr; v.subVer = subVer; v.relayTxesBeforeFilter = relayTxesBeforeFilter; return v; }
public VersionMessage duplicate() { VersionMessage v = new VersionMessage(params, (int) bestHeight); v.clientVersion = clientVersion; v.localServices = localServices; v.time = time; v.myAddr = myAddr; v.theirAddr = theirAddr; v.subVer = subVer; v.relayTxesBeforeFilter = relayTxesBeforeFilter; return v; }
public VersionMessage duplicate() { VersionMessage v = new VersionMessage(params, (int) bestHeight); v.clientVersion = clientVersion; v.localServices = localServices; v.time = time; v.myAddr = myAddr; v.theirAddr = theirAddr; v.subVer = subVer; v.relayTxesBeforeFilter = relayTxesBeforeFilter; return v; }
/** * <p>Construct a peer that reads/writes from the given chain. Automatically creates a VersionMessage for you from * the given software name/version strings, which should be something like "MySimpleTool", "1.0" and which will tell * the remote node to relay transaction inv messages before it has received a filter.</p> * * <p>Note that this does <b>NOT</b> make a connection to the given remoteAddress, it only creates a handler for a * connection. If you want to create a one-off connection, create a Peer and pass it to * {@link org.bitcoinj.net.NioClientManager#openConnection(java.net.SocketAddress, StreamConnection)} * or * {@link org.bitcoinj.net.NioClient#NioClient(java.net.SocketAddress, StreamConnection, int)}.</p> * * <p>The remoteAddress provided should match the remote address of the peer which is being connected to, and is * used to keep track of which peers relayed transactions and offer more descriptive logging.</p> */ public Peer(NetworkParameters params, AbstractBlockChain blockChain, PeerAddress peerAddress, String thisSoftwareName, String thisSoftwareVersion) { this(params, new VersionMessage(params, blockChain.getBestChainHeight()), blockChain, peerAddress); this.versionMessage.appendToSubVer(thisSoftwareName, thisSoftwareVersion, null); }
/** * <p>Construct a peer that reads/writes from the given chain. Automatically creates a VersionMessage for you from * the given software name/version strings, which should be something like "MySimpleTool", "1.0" and which will tell * the remote node to relay transaction inv messages before it has received a filter.</p> * * <p>Note that this does <b>NOT</b> make a connection to the given remoteAddress, it only creates a handler for a * connection. If you want to create a one-off connection, create a Peer and pass it to * {@link org.bitcoinj.net.NioClientManager#openConnection(java.net.SocketAddress, StreamConnection)} * or * {@link org.bitcoinj.net.NioClient#NioClient(java.net.SocketAddress, StreamConnection, int)}.</p> * * <p>The remoteAddress provided should match the remote address of the peer which is being connected to, and is * used to keep track of which peers relayed transactions and offer more descriptive logging.</p> */ public Peer(NetworkParameters params, AbstractBlockChain blockChain, PeerAddress peerAddress, String thisSoftwareName, String thisSoftwareVersion) { this(params, new VersionMessage(params, blockChain.getBestChainHeight()), blockChain, peerAddress); this.versionMessage.appendToSubVer(thisSoftwareName, thisSoftwareVersion, null); }
/** * <p>Construct a peer that reads/writes from the given chain. Automatically creates a VersionMessage for you from * the given software name/version strings, which should be something like "MySimpleTool", "1.0" and which will tell * the remote node to relay transaction inv messages before it has received a filter.</p> * * <p>Note that this does <b>NOT</b> make a connection to the given remoteAddress, it only creates a handler for a * connection. If you want to create a one-off connection, create a Peer and pass it to * {@link org.bitcoinj.net.NioClientManager#openConnection(java.net.SocketAddress, StreamConnection)} * or * {@link org.bitcoinj.net.NioClient#NioClient(java.net.SocketAddress, StreamConnection, int)}.</p> * * <p>The remoteAddress provided should match the remote address of the peer which is being connected to, and is * used to keep track of which peers relayed transactions and offer more descriptive logging.</p> */ public Peer(NetworkParameters params, AbstractBlockChain blockChain, PeerAddress peerAddress, String thisSoftwareName, String thisSoftwareVersion) { this(params, new VersionMessage(params, blockChain.getBestChainHeight()), blockChain, peerAddress); this.versionMessage.appendToSubVer(thisSoftwareName, thisSoftwareVersion, null); }
/** * <p>Construct a peer that reads/writes from the given chain. Automatically creates a VersionMessage for you from * the given software name/version strings, which should be something like "MySimpleTool", "1.0" and which will tell * the remote node to relay transaction inv messages before it has received a filter.</p> * * <p>Note that this does <b>NOT</b> make a connection to the given remoteAddress, it only creates a handler for a * connection. If you want to create a one-off connection, create a Peer and pass it to * {@link org.bitcoinj.net.NioClientManager#openConnection(java.net.SocketAddress, StreamConnection)} * or * {@link org.bitcoinj.net.NioClient#NioClient(java.net.SocketAddress, StreamConnection, int)}.</p> * * <p>The remoteAddress provided should match the remote address of the peer which is being connected to, and is * used to keep track of which peers relayed transactions and offer more descriptive logging.</p> */ public Peer(NetworkParameters params, AbstractBlockChain blockChain, PeerAddress peerAddress, String thisSoftwareName, String thisSoftwareVersion) { this(params, new VersionMessage(params, blockChain.getBestChainHeight()), blockChain, peerAddress); this.versionMessage.appendToSubVer(thisSoftwareName, thisSoftwareVersion, null); }
private void connectWithVersion(int version, int flags) throws Exception { VersionMessage peerVersion = new VersionMessage(PARAMS, OTHER_PEER_CHAIN_HEIGHT); peerVersion.clientVersion = version; peerVersion.localServices = flags; writeTarget = connect(peer, peerVersion); }
/** * Sets information that identifies this software to remote nodes. This is a convenience wrapper for creating * a new {@link VersionMessage}, calling {@link VersionMessage#appendToSubVer(String, String, String)} on it, * and then calling {@link PeerGroup#setVersionMessage(VersionMessage)} on the result of that. See the docs for * {@link VersionMessage#appendToSubVer(String, String, String)} for information on what the fields should contain. */ public void setUserAgent(String name, String version, @Nullable String comments) { //TODO Check that height is needed here (it wasnt, but it should be, no?) int height = chain == null ? 0 : chain.getBestChainHeight(); VersionMessage ver = new VersionMessage(params, height); ver.relayTxesBeforeFilter = false; updateVersionMessageRelayTxesBeforeFilter(ver); ver.appendToSubVer(name, version, comments); setVersionMessage(ver); }
/** * Sets information that identifies this software to remote nodes. This is a convenience wrapper for creating * a new {@link VersionMessage}, calling {@link VersionMessage#appendToSubVer(String, String, String)} on it, * and then calling {@link PeerGroup#setVersionMessage(VersionMessage)} on the result of that. See the docs for * {@link VersionMessage#appendToSubVer(String, String, String)} for information on what the fields should contain. */ public void setUserAgent(String name, String version, @Nullable String comments) { //TODO Check that height is needed here (it wasnt, but it should be, no?) int height = chain == null ? 0 : chain.getBestChainHeight(); VersionMessage ver = new VersionMessage(params, height); ver.relayTxesBeforeFilter = false; updateVersionMessageRelayTxesBeforeFilter(ver); ver.appendToSubVer(name, version, comments); setVersionMessage(ver); }
/** * Sets information that identifies this software to remote nodes. This is a convenience wrapper for creating * a new {@link VersionMessage}, calling {@link VersionMessage#appendToSubVer(String, String, String)} on it, * and then calling {@link PeerGroup#setVersionMessage(VersionMessage)} on the result of that. See the docs for * {@link VersionMessage#appendToSubVer(String, String, String)} for information on what the fields should contain. */ public void setUserAgent(String name, String version, @Nullable String comments) { //TODO Check that height is needed here (it wasnt, but it should be, no?) int height = chain == null ? 0 : chain.getBestChainHeight(); VersionMessage ver = new VersionMessage(params, height); ver.relayTxesBeforeFilter = false; updateVersionMessageRelayTxesBeforeFilter(ver); ver.appendToSubVer(name, version, comments); setVersionMessage(ver); }
/** * Sets information that identifies this software to remote nodes. This is a convenience wrapper for creating * a new {@link VersionMessage}, calling {@link VersionMessage#appendToSubVer(String, String, String)} on it, * and then calling {@link PeerGroup#setVersionMessage(VersionMessage)} on the result of that. See the docs for * {@link VersionMessage#appendToSubVer(String, String, String)} for information on what the fields should contain. */ public void setUserAgent(String name, String version, @Nullable String comments) { //TODO Check that height is needed here (it wasnt, but it should be, no?) int height = chain == null ? 0 : chain.getBestChainHeight(); VersionMessage ver = new VersionMessage(params, height); ver.relayTxesBeforeFilter = false; updateVersionMessageRelayTxesBeforeFilter(ver); ver.appendToSubVer(name, version, comments); setVersionMessage(ver); }
@Override public void setUp(BlockStore blockStore) throws Exception { super.setUp(blockStore); remoteVersionMessage = new VersionMessage(PARAMS, 1); remoteVersionMessage.localServices = VersionMessage.NODE_NETWORK; remoteVersionMessage.clientVersion = NotFoundMessage.MIN_PROTOCOL_VERSION; blockJobs = false; initPeerGroup(); }
@Test // Test that we can decode version messages which miss data which some old nodes may not include public void testDecode() throws Exception { NetworkParameters params = UnitTestParams.get(); VersionMessage ver = new VersionMessage(params, HEX.decode("7111010000000000000000003334a85500000000000000000000000000000000000000000000ffff7f000001479d000000000000000000000000000000000000ffff7f000001479d00000000000000000f2f626974636f696e6a3a302e31332f0004000000")); assertFalse(ver.relayTxesBeforeFilter); assertEquals(1024, ver.bestHeight); assertEquals("/bitcoinj:0.13/", ver.subVer); ver = new VersionMessage(params, HEX.decode("711101000000000000000000a634a85500000000000000000000000000000000000000000000ffff7f000001479d000000000000000000000000000000000000ffff7f000001479d00000000000000000f2f626974636f696e6a3a302e31332f0004000001")); assertTrue(ver.relayTxesBeforeFilter); assertEquals(1024, ver.bestHeight); assertEquals("/bitcoinj:0.13/", ver.subVer); ver = new VersionMessage(params, HEX.decode("711101000000000000000000c334a85500000000000000000000000000000000000000000000ffff7f000001479d000000000000000000000000000000000000ffff7f000001479d00000000000000000f2f626974636f696e6a3a302e31332f0000000001")); assertTrue(ver.relayTxesBeforeFilter); assertEquals(0, ver.bestHeight); assertEquals("/bitcoinj:0.13/", ver.subVer); ver = new VersionMessage(params, HEX.decode("71110100000000000000000048e5e95000000000000000000000000000000000000000000000ffff7f000001479d000000000000000000000000000000000000ffff7f000001479d0000000000000000")); assertTrue(ver.relayTxesBeforeFilter); assertEquals(0, ver.bestHeight); assertEquals("", ver.subVer); } }
@Override @Before public void setUp() throws Exception { super.setUp(); VersionMessage ver = new VersionMessage(PARAMS, 100); InetSocketAddress address = new InetSocketAddress("127.0.0.1", 4000); peer = new Peer(PARAMS, ver, new PeerAddress(PARAMS, address), blockChain); peer.addWallet(wallet); }
@Test public void noPings() throws Exception { peerGroup.start(); peerGroup.setPingIntervalMsec(0); VersionMessage versionMessage = new VersionMessage(PARAMS, 2); versionMessage.clientVersion = NetworkParameters.ProtocolVersion.BLOOM_FILTER.getBitcoinProtocolVersion(); versionMessage.localServices = VersionMessage.NODE_NETWORK; connectPeer(1, versionMessage); peerGroup.waitForPeers(1).get(); assertFalse(peerGroup.getConnectedPeers().get(0).getLastPingTime() < Long.MAX_VALUE); }
@Test public void waitForPeersWithServiceFlags() throws Exception { ListenableFuture<List<Peer>> future = peerGroup.waitForPeersWithServiceMask(2, 3); VersionMessage ver1 = new VersionMessage(PARAMS, 10); ver1.clientVersion = 70000; ver1.localServices = VersionMessage.NODE_NETWORK; VersionMessage ver2 = new VersionMessage(PARAMS, 10); ver2.clientVersion = 70000; ver2.localServices = VersionMessage.NODE_NETWORK | 2; peerGroup.start(); assertFalse(future.isDone()); connectPeer(1, ver1); assertTrue(peerGroup.findPeersWithServiceMask(3).isEmpty()); assertFalse(future.isDone()); connectPeer(2, ver2); assertFalse(future.isDone()); assertEquals(1, peerGroup.findPeersWithServiceMask(3).size()); assertTrue(peerGroup.waitForPeersWithServiceMask(1, 0x3).isDone()); // Immediate completion. connectPeer(3, ver2); future.get(); assertTrue(future.isDone()); peerGroup.stop(); }
@Test public void waitForPeersOfVersion() throws Exception { final int baseVer = peerGroup.getMinRequiredProtocolVersion() + 3000; final int newVer = baseVer + 1000; ListenableFuture<List<Peer>> future = peerGroup.waitForPeersOfVersion(2, newVer); VersionMessage ver1 = new VersionMessage(PARAMS, 10); ver1.clientVersion = baseVer; ver1.localServices = VersionMessage.NODE_NETWORK; VersionMessage ver2 = new VersionMessage(PARAMS, 10); ver2.clientVersion = newVer; ver2.localServices = VersionMessage.NODE_NETWORK; peerGroup.start(); assertFalse(future.isDone()); connectPeer(1, ver1); assertFalse(future.isDone()); connectPeer(2, ver2); assertFalse(future.isDone()); assertTrue(peerGroup.waitForPeersOfVersion(1, newVer).isDone()); // Immediate completion. connectPeer(3, ver2); future.get(); assertTrue(future.isDone()); }
@Test public void pings() throws Exception { peerGroup.start(); peerGroup.setPingIntervalMsec(100); VersionMessage versionMessage = new VersionMessage(PARAMS, 2); versionMessage.clientVersion = NetworkParameters.ProtocolVersion.BLOOM_FILTER.getBitcoinProtocolVersion(); versionMessage.localServices = VersionMessage.NODE_NETWORK; InboundMessageQueuer p1 = connectPeer(1, versionMessage); Ping ping = (Ping) waitForOutbound(p1); inbound(p1, new Pong(ping.getNonce())); pingAndWait(p1); assertTrue(peerGroup.getConnectedPeers().get(0).getLastPingTime() < Long.MAX_VALUE); // The call to outbound should block until a ping arrives. ping = (Ping) waitForOutbound(p1); inbound(p1, new Pong(ping.getNonce())); assertTrue(peerGroup.getConnectedPeers().get(0).getLastPingTime() < Long.MAX_VALUE); }