Skip to content

Commit f6611a9

Browse files
author
Alessandro Polverini
committed
More refactoring, using BitcoinFactory on more classes and tests.
1 parent bd38c19 commit f6611a9

File tree

21 files changed

+311
-259
lines changed

21 files changed

+311
-259
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package hu.netmind.bitcoin;
2+
3+
/**
4+
*
5+
* @author Alessandro Polverini <[email protected]>
6+
*/
7+
public class Constants {
8+
9+
public final static long PRODNET_MESSAGE_MAGIC = 0xf9beb4d9L;
10+
public final static long TESTNET2_MESSAGE_MAGIC = 0xfabfb5daL;
11+
public final static long TESTNET3_MESSAGE_MAGIC = 0x0b110907;
12+
}

core/block/src/main/java/hu/netmind/bitcoin/block/BitcoinFactory.java

-3
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,8 @@
1919

2020
import hu.netmind.bitcoin.Block;
2121
import hu.netmind.bitcoin.ScriptFactory;
22-
import hu.netmind.bitcoin.net.InventoryItem;
23-
import hu.netmind.bitcoin.net.Message;
2422
import hu.netmind.bitcoin.net.NetworkMessageFactory;
2523
import java.math.BigDecimal;
26-
import java.util.List;
2724

2825
/**
2926
*

core/block/src/main/java/hu/netmind/bitcoin/block/StandardBitcoinFactory.java core/block/src/main/java/hu/netmind/bitcoin/block/ProdnetBitcoinFactory.java

+10-9
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
import hu.netmind.bitcoin.BitcoinException;
2121
import hu.netmind.bitcoin.Block;
22+
import hu.netmind.bitcoin.Constants;
2223
import hu.netmind.bitcoin.ScriptFactory;
2324
import hu.netmind.bitcoin.ScriptFragment;
2425
import hu.netmind.bitcoin.net.NetworkMessageFactory;
@@ -35,26 +36,26 @@
3536
*
3637
* @author Alessandro Polverini
3738
*/
38-
public class StandardBitcoinFactory implements BitcoinFactory
39+
public class ProdnetBitcoinFactory implements BitcoinFactory
3940
{
4041

4142
protected Logger logger = LoggerFactory.getLogger(this.getClass());
42-
protected static final DifficultyTarget MAX_PRODNET_TARGET =
43+
protected static final DifficultyTarget MAX_TARGET =
4344
new DifficultyTarget(new BigInteger("FFFF0000000000000000000000000000000000000000000000000000", 16));
4445
private Block GENESIS_BLOCK;
4546
protected ScriptFactory scriptFactory;
4647
protected NetworkMessageFactory messageFactory;
4748
protected long compressedTarget;
48-
protected long messageMagic;
49+
private long protocolMessageMagic;
4950

50-
protected StandardBitcoinFactory()
51+
protected ProdnetBitcoinFactory()
5152
{
5253
}
5354

54-
public StandardBitcoinFactory(ScriptFactory scriptFactory) throws BitcoinException
55+
public ProdnetBitcoinFactory(ScriptFactory scriptFactory) throws BitcoinException
5556
{
5657
this.scriptFactory = scriptFactory;
57-
setNetworkParams(0xf9beb4d9L, 1231006505000l, 2083236893l, 0x1d00ffffL, BtcUtil.hexIn("000000000019D6689C085AE165831E934FF763AE46A2A6C172B3F1B60A8CE26F"));
58+
setNetworkParams(Constants.PRODNET_MESSAGE_MAGIC, 1231006505000l, 2083236893l, 0x1d00ffffL, BtcUtil.hexIn("000000000019D6689C085AE165831E934FF763AE46A2A6C172B3F1B60A8CE26F"));
5859
}
5960

6061
@Override
@@ -66,7 +67,7 @@ public ScriptFactory getScriptFactory()
6667
@Override
6768
public long getMessageMagic()
6869
{
69-
return messageMagic;
70+
return protocolMessageMagic;
7071
}
7172

7273
@Override
@@ -96,7 +97,7 @@ public Difficulty newDifficulty(DifficultyTarget target)
9697
@Override
9798
public DifficultyTarget maxDifficultyTarget()
9899
{
99-
return MAX_PRODNET_TARGET;
100+
return MAX_TARGET;
100101
}
101102

102103
@Override
@@ -136,7 +137,7 @@ protected final void setNetworkParams(
136137
long genesisCompressedTarget,
137138
byte[] genesisHash) throws BitcoinException
138139
{
139-
this.messageMagic = messageMagic;
140+
this.protocolMessageMagic = messageMagic;
140141
this.compressedTarget = genesisCompressedTarget;
141142
this.messageFactory = new NetworkMessageFactory(messageMagic);
142143
GENESIS_BLOCK = new BlockImpl(getGenesisTransactions(), genesisCreationTime, genesisNonce, genesisCompressedTarget,

core/block/src/main/java/hu/netmind/bitcoin/block/Testnet2BitcoinFactory.java

+6-4
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
package hu.netmind.bitcoin.block;
1919

2020
import hu.netmind.bitcoin.BitcoinException;
21+
import hu.netmind.bitcoin.Constants;
2122
import hu.netmind.bitcoin.ScriptFactory;
2223
import it.nibbles.bitcoin.utils.BtcUtil;
2324
import java.math.BigInteger;
@@ -26,15 +27,16 @@
2627
*
2728
* @author Alessandro Polverini
2829
*/
29-
public class Testnet2BitcoinFactory extends StandardBitcoinFactory
30+
public class Testnet2BitcoinFactory extends ProdnetBitcoinFactory
3031
{
31-
public static final DifficultyTarget MAX_TESTNET_TARGET =
32+
33+
public static final DifficultyTarget MAX_TARGET =
3234
new DifficultyTarget(new BigInteger("FFFFF0000000000000000000000000000000000000000000000000000", 16));
3335

3436
public Testnet2BitcoinFactory(ScriptFactory scriptFactory) throws BitcoinException
3537
{
3638
this.scriptFactory = scriptFactory;
37-
setNetworkParams(0xfabfb5daL, 1296688602000l, 384568319l, 0x1d07fff8L, BtcUtil.hexIn("00000007199508e34a9ff81e6ec0c477a4cccff2a4767a8eee39c11db367b008"));
39+
setNetworkParams(Constants.TESTNET2_MESSAGE_MAGIC, 1296688602000l, 384568319l, 0x1d07fff8L, BtcUtil.hexIn("00000007199508e34a9ff81e6ec0c477a4cccff2a4767a8eee39c11db367b008"));
3840
}
3941

4042
@Override
@@ -46,6 +48,6 @@ public boolean isTestnet2()
4648
@Override
4749
public DifficultyTarget maxDifficultyTarget()
4850
{
49-
return MAX_TESTNET_TARGET;
51+
return MAX_TARGET;
5052
}
5153
}

core/block/src/main/java/hu/netmind/bitcoin/block/Testnet3BitcoinFactory.java

+3-2
Original file line numberDiff line numberDiff line change
@@ -18,20 +18,21 @@
1818
package hu.netmind.bitcoin.block;
1919

2020
import hu.netmind.bitcoin.BitcoinException;
21+
import hu.netmind.bitcoin.Constants;
2122
import hu.netmind.bitcoin.ScriptFactory;
2223
import it.nibbles.bitcoin.utils.BtcUtil;
2324

2425
/**
2526
*
2627
* @author Alessandro Polverini
2728
*/
28-
public class Testnet3BitcoinFactory extends StandardBitcoinFactory
29+
public class Testnet3BitcoinFactory extends ProdnetBitcoinFactory
2930
{
3031

3132
public Testnet3BitcoinFactory(ScriptFactory scriptFactory) throws BitcoinException
3233
{
3334
this.scriptFactory = scriptFactory;
34-
setNetworkParams(0x0b110907, 1296688602000L, 414098458L, 0x1d00ffffL, BtcUtil.hexIn("000000000933ea01ad0ee984209779baaec3ced90fa3f408719526f8d77f4943"));
35+
setNetworkParams(Constants.TESTNET3_MESSAGE_MAGIC, 1296688602000L, 414098458L, 0x1d00ffffL, BtcUtil.hexIn("000000000933ea01ad0ee984209779baaec3ced90fa3f408719526f8d77f4943"));
3536
}
3637

3738
@Override

core/block/src/test/java/hu/netmind/bitcoin/block/DifficultyTests.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public class DifficultyTests
3636
static {
3737
try
3838
{
39-
prodnetFactory = new StandardBitcoinFactory(null);
39+
prodnetFactory = new ProdnetBitcoinFactory(null);
4040
testnet2Factory = new Testnet2BitcoinFactory(null);
4141
} catch (BitcoinException ex)
4242
{

core/block/src/test/java/hu/netmind/bitcoin/block/TesterBitcoinFactory.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
* @author Alessandro Polverini
1010
*/
11-
public class TesterBitcoinFactory extends StandardBitcoinFactory
11+
public class TesterBitcoinFactory extends ProdnetBitcoinFactory
1212
{
1313
private Block genesisBlock;
1414

core/net-p2p/src/main/java/hu/netmind/bitcoin/net/p2p/Node.java

+7-3
Original file line numberDiff line numberDiff line change
@@ -70,24 +70,28 @@ public class Node
7070
private List<MessageHandler> handlers = new ArrayList<>();
7171
private final List<NodeWorker> workers = new ArrayList<>();
7272
private NodeListener nodeListener;
73+
protected long messageMagic;
7374

75+
// TODO: We need to pass the
7476
/**
7577
* Create this node which will then listen for incoming connections from
7678
* other nodes. Note that node will not work until it's started.
7779
*
7880
* @param port The port to listen on instead of the default port.
7981
*/
80-
public Node(int port)
82+
public Node(long messageMagic, int port)
8183
{
84+
this.messageMagic = messageMagic;
8285
this.port = port;
8386
}
8487

8588
/**
8689
* Create this node which will then listen for incoming connections from
8790
* other nodes. Note that node will not work until it's started.
8891
*/
89-
public Node()
92+
public Node(long messageMagic)
9093
{
94+
this.messageMagic = messageMagic;
9195
}
9296

9397
/**
@@ -373,7 +377,7 @@ private class NodeWorker implements Runnable
373377
private BitCoinOutputStream output;
374378
private boolean running;
375379
private Thread workerThread;
376-
private MessageMarshaller marshaller = new MessageMarshaller();
380+
private MessageMarshaller marshaller = new MessageMarshaller(messageMagic);
377381
private Connection connection;
378382
private long lastIncomingTime = 0;
379383
private long creationTime = 0;

core/net-p2p/src/test/java/hu/netmind/bitcoin/net/p2p/NodeTests.java

+27-15
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,15 @@
1818

1919
package hu.netmind.bitcoin.net.p2p;
2020

21+
import hu.netmind.bitcoin.BitcoinException;
22+
import hu.netmind.bitcoin.block.BitcoinFactory;
23+
import hu.netmind.bitcoin.block.Testnet3BitcoinFactory;
2124
import hu.netmind.bitcoin.net.AlertMessage;
2225
import hu.netmind.bitcoin.net.BitCoinInputStream;
2326
import hu.netmind.bitcoin.net.BitCoinOutputStream;
2427
import hu.netmind.bitcoin.net.Message;
2528
import hu.netmind.bitcoin.net.MessageMarshaller;
29+
import hu.netmind.bitcoin.net.NetworkMessageFactory;
2630
import hu.netmind.bitcoin.net.NodeAddress;
2731
import hu.netmind.bitcoin.net.PingMessage;
2832
import hu.netmind.bitcoin.net.VerackMessage;
@@ -38,6 +42,7 @@
3842
import java.util.Map;
3943
import java.util.concurrent.Semaphore;
4044
import java.util.concurrent.TimeUnit;
45+
import java.util.logging.Level;
4146
import org.easymock.EasyMock;
4247
import org.slf4j.Logger;
4348
import org.slf4j.LoggerFactory;
@@ -57,13 +62,20 @@ public class NodeTests
5762

5863
private List<DummyNode> dummyNodes = new ArrayList<>();
5964
private Node savedNode = null;
65+
private BitcoinFactory bitcoinFactory;
66+
private NetworkMessageFactory messageFactory;
6067

68+
public NodeTests() throws BitcoinException
69+
{
70+
bitcoinFactory = new Testnet3BitcoinFactory(null);
71+
messageFactory = bitcoinFactory.getMessageFactory();
72+
}
6173
/**
6274
* Create a node which will be destroyed at the end of test.
6375
*/
6476
public Node createNode()
6577
{
66-
savedNode = new Node();
78+
savedNode = new Node(bitcoinFactory.getMessageMagic());
6779
return savedNode;
6880
}
6981

@@ -137,7 +149,7 @@ public void testStartStop()
137149
throws IOException
138150
{
139151
// Create node with defaults
140-
Node node = new Node();
152+
Node node = new Node(bitcoinFactory.getMessageMagic());
141153
// Check that there are no node threads running before start
142154
Assert.assertFalse(isNodeThreadActive(),"there is a node thread before starting");
143155
// Start
@@ -181,9 +193,9 @@ public void testDetectConnectioToSelf()
181193
EasyMock.replay(source);
182194

183195
final NodeAddress dummyNodeAddress = new NodeAddress(1, new InetSocketAddress(51234));
184-
final VersionMessage verMsg1 = new VersionMessage(Message.MAGIC_TEST, BC_PROTOCOL_VERSION, 0, System.currentTimeMillis() / 1000,
196+
final VersionMessage verMsg1 = messageFactory.newVersionMessage(BC_PROTOCOL_VERSION, 0, System.currentTimeMillis() / 1000,
185197
dummyNodeAddress, dummyNodeAddress, 123, "test", 1);
186-
final VersionMessage verMsg2 = new VersionMessage(Message.MAGIC_TEST, BC_PROTOCOL_VERSION, 0, System.currentTimeMillis() / 1000,
198+
final VersionMessage verMsg2 = messageFactory.newVersionMessage(BC_PROTOCOL_VERSION, 0, System.currentTimeMillis() / 1000,
187199
dummyNodeAddress, dummyNodeAddress, 124, "test", 1);
188200

189201
// Create node
@@ -238,7 +250,7 @@ public void testCommunicationConcept()
238250
dummyNode.accept();
239251

240252
// Send a message to node
241-
dummyNode.send(new AlertMessage(Message.MAGIC_TEST,"Message"));
253+
dummyNode.send(messageFactory.newAlertMessage("Message"));
242254
// Get the repeated message right back
243255
AlertMessage answer = (AlertMessage) dummyNode.read();
244256
// Check
@@ -267,7 +279,7 @@ public void testBroadcast()
267279
for ( int i=0; i<dummyNodes.length; i++ )
268280
dummyNodes[i].accept();
269281
// Broadcast a message to all connected nodes
270-
node.broadcast(new AlertMessage(Message.MAGIC_TEST,"Message"));
282+
node.broadcast(messageFactory.newAlertMessage("Message"));
271283
// Get the messages from all nodes
272284
for ( int i=0; i<dummyNodes.length; i++ )
273285
{
@@ -296,8 +308,8 @@ public void testSameAddressTwice()
296308
// Accept the connection from node
297309
dummyNode.accept();
298310
// Broadcast 2 messages
299-
node.broadcast(new AlertMessage(Message.MAGIC_TEST,"Message1"));
300-
node.broadcast(new AlertMessage(Message.MAGIC_TEST,"Message2"));
311+
node.broadcast(messageFactory.newAlertMessage("Message1"));
312+
node.broadcast(messageFactory.newAlertMessage("Message2"));
301313
// Now check that the 1st message arrives only once
302314
AlertMessage message = (AlertMessage) dummyNode.read();
303315
Assert.assertEquals(message.getMessage(),"Message1");
@@ -318,7 +330,7 @@ public void testAcceptExternalConnection()
318330
// Wait unilt the node really connected to the node
319331
waiter.waitForJoin();
320332
// Check connection
321-
node.broadcast(new AlertMessage(Message.MAGIC_TEST,"Message"));
333+
node.broadcast(messageFactory.newAlertMessage("Message"));
322334
AlertMessage message = (AlertMessage) dummyNode.read();
323335
Assert.assertEquals(message.getMessage(),"Message");
324336
}
@@ -388,7 +400,7 @@ public void onLeave(Connection conn)
388400
Assert.fail("didn't receive the 'leave' event of closed node in time");
389401
// Now try to connect new node and communicate
390402
DummyNode dummyNode2 = createDummyNode(new InetSocketAddress(node.getPort()));
391-
dummyNode2.send(new PingMessage(Message.MAGIC_TEST));
403+
dummyNode2.send(messageFactory.newPingMessage());
392404
Message message = dummyNode2.read();
393405
}
394406

@@ -423,8 +435,8 @@ public void testMultiHandlerReply()
423435
// Accept the connection from node
424436
dummyNode.accept();
425437
// Send two messages to node
426-
dummyNode.send(new AlertMessage(Message.MAGIC_TEST,"Message1"));
427-
dummyNode.send(new AlertMessage(Message.MAGIC_TEST,"Message2"));
438+
dummyNode.send(bitcoinFactory.getMessageFactory().newAlertMessage("Message1"));
439+
dummyNode.send(bitcoinFactory.getMessageFactory().newAlertMessage("Message2"));
428440
// Check that easy is only replied once by the node, meaning that only one repeater handler
429441
// is allowed to answer.
430442
AlertMessage incoming = (AlertMessage) dummyNode.read();
@@ -462,7 +474,7 @@ public void onJoin(Connection conn)
462474
{
463475
try
464476
{
465-
conn.send(new VerackMessage(Message.MAGIC_TEST));
477+
conn.send(messageFactory.newVerackMessage());
466478
} catch (IOException ex)
467479
{
468480
}
@@ -530,7 +542,7 @@ public DummyNode(InetSocketAddress address)
530542
socket.connect(address,1000); // 0.5 seconds to connect
531543
input = new BitCoinInputStream(new BufferedInputStream(socket.getInputStream()));
532544
output = new BitCoinOutputStream(socket.getOutputStream());
533-
marshaller = new MessageMarshaller();
545+
marshaller = new MessageMarshaller(bitcoinFactory.getMessageMagic());
534546
}
535547

536548
/**
@@ -542,7 +554,7 @@ public DummyNode()
542554
serverSocket = new ServerSocket(0);
543555
address = (InetSocketAddress) serverSocket.getLocalSocketAddress();
544556
serverSocket.setSoTimeout(1000); // Wait for incoming for 0.5 sec
545-
marshaller = new MessageMarshaller();
557+
marshaller = new MessageMarshaller(bitcoinFactory.getMessageMagic());
546558
}
547559

548560
public InetSocketAddress getAddress()

0 commit comments

Comments
 (0)