Repository navigation
Expand file tree
/
Copy pathBroadTest.java
More file actions
282 lines (257 loc) · 10.3 KB
/
Copy pathBroadTest.java
File metadata and controls
282 lines (257 loc) · 10.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
package org.tron.core.net.node;
import com.google.common.cache.Cache;
import java.io.File;
import java.util.Collection;
import java.util.Map;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ExecutorService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.MapUtils;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.tron.common.application.Application;
import org.tron.common.application.ApplicationFactory;
import org.tron.common.overlay.client.PeerClient;
import org.tron.common.overlay.discover.node.Node;
import org.tron.common.overlay.message.Message;
import org.tron.common.overlay.server.Channel;
import org.tron.common.overlay.server.ChannelManager;
import org.tron.common.overlay.server.MessageQueue;
import org.tron.common.overlay.server.SyncPool;
import org.tron.common.utils.FileUtil;
import org.tron.common.utils.ReflectUtils;
import org.tron.common.utils.Sha256Hash;
import org.tron.core.capsule.BlockCapsule;
import org.tron.core.config.DefaultConfig;
import org.tron.core.config.args.Args;
import org.tron.core.db.ByteArrayWrapper;
import org.tron.core.db.Manager;
import org.tron.core.net.message.BlockMessage;
import org.tron.core.net.message.MessageTypes;
import org.tron.core.net.message.TransactionMessage;
import org.tron.core.net.node.NodeImpl.PriorItem;
import org.tron.core.net.peer.PeerConnection;
import org.tron.core.services.RpcApiService;
import org.tron.core.services.WitnessService;
import org.tron.protos.Protocol.Block;
import org.tron.protos.Protocol.Inventory.InventoryType;
import org.tron.protos.Protocol.Transaction;
@Slf4j
public class BroadTest {
private static AnnotationConfigApplicationContext context;
private NodeImpl node;
RpcApiService rpcApiService;
PeerClient peerClient;
ChannelManager channelManager;
SyncPool pool;
Application appT;
private static final String dbPath = "output-nodeImplTest/broad";
private static final String dbDirectory = "db_Broad_test";
private static final String indexDirectory = "index_Broad_test";
private class Condition {
private Sha256Hash blockId;
private Sha256Hash transactionId;
public Condition(Sha256Hash blockId, Sha256Hash transactionId) {
this.blockId = blockId;
this.transactionId = transactionId;
}
public Sha256Hash getBlockId() {
return blockId;
}
public Sha256Hash getTransactionId() {
return transactionId;
}
}
private Sha256Hash testBlockBroad() {
Block block = Block.getDefaultInstance();
BlockMessage blockMessage = new BlockMessage(new BlockCapsule(block));
node.broadcast(blockMessage);
ConcurrentHashMap<Sha256Hash, InventoryType> advObjToSpread = ReflectUtils
.getFieldValue(node, "advObjToSpread");
Assert.assertEquals(advObjToSpread.get(blockMessage.getMessageId()), InventoryType.BLOCK);
return blockMessage.getMessageId();
}
private Sha256Hash testTransactionBroad() {
Transaction transaction = Transaction.getDefaultInstance();
TransactionMessage transactionMessage = new TransactionMessage(transaction);
node.broadcast(transactionMessage);
ConcurrentHashMap<Sha256Hash, InventoryType> advObjToSpread = ReflectUtils
.getFieldValue(node, "advObjToSpread");
Assert.assertEquals(advObjToSpread.get(transactionMessage.getMessageId()), InventoryType.TRX);
return transactionMessage.getMessageId();
}
private Condition testConsumerAdvObjToSpread() {
Sha256Hash blockId = testBlockBroad();
Sha256Hash transactionId = testTransactionBroad();
//remove the tx and block
removeTheTxAndBlock(blockId, transactionId);
ReflectUtils.invokeMethod(node, "consumerAdvObjToSpread");
Collection<PeerConnection> activePeers = ReflectUtils.invokeMethod(node, "getActivePeer");
boolean result = true;
for (PeerConnection peerConnection : activePeers) {
if (!peerConnection.getAdvObjWeSpread().containsKey(blockId)) {
result &= false;
}
if (!peerConnection.getAdvObjWeSpread().containsKey(transactionId)) {
result &= false;
}
}
for (PeerConnection peerConnection : activePeers) {
peerConnection.getAdvObjWeSpread().clear();
}
Assert.assertTrue(result);
return new Condition(blockId, transactionId);
}
private void removeTheTxAndBlock(Sha256Hash blockId, Sha256Hash transactionId) {
Cache<Sha256Hash, TransactionMessage> trxCache = ReflectUtils.getFieldValue(node, "TrxCache");
Cache<Sha256Hash, BlockMessage> blockCache = ReflectUtils.getFieldValue(node, "BlockCache");
trxCache.invalidate(transactionId);
blockCache.invalidate(blockId);
}
@Test
public void testConsumerAdvObjToFetch() throws InterruptedException {
Condition condition = testConsumerAdvObjToSpread();
Thread.sleep(1000);
//
Map<Sha256Hash, PriorItem> advObjToFetch = ReflectUtils
.getFieldValue(node, "advObjToFetch");
logger.info("advObjToFetch:{}", advObjToFetch);
logger.info("advObjToFetchSize:{}", advObjToFetch.size());
//Assert.assertEquals(advObjToFetch.get(condition.getBlockId()), InventoryType.BLOCK);
//Assert.assertEquals(advObjToFetch.get(condition.getTransactionId()), InventoryType.TRX);
//To avoid writing the database, manually stop the sending of messages.
Collection<PeerConnection> activePeers = ReflectUtils.invokeMethod(node, "getActivePeer");
for (PeerConnection peerConnection : activePeers) {
MessageQueue messageQueue = ReflectUtils.getFieldValue(peerConnection, "msgQueue");
ReflectUtils.setFieldValue(messageQueue, "sendMsgFlag", false);
}
//
ReflectUtils.invokeMethod(node, "consumerAdvObjToFetch");
Thread.sleep(1000);
boolean result = true;
int count = 0;
for (PeerConnection peerConnection : activePeers) {
if (peerConnection.getAdvObjWeRequested()
.containsKey(new Item(condition.getTransactionId(), InventoryType.TRX))) {
++count;
}
if (peerConnection.getAdvObjWeRequested()
.containsKey(new Item(condition.getBlockId(), InventoryType.BLOCK))) {
++count;
}
MessageQueue messageQueue = ReflectUtils.getFieldValue(peerConnection, "msgQueue");
BlockingQueue<Message> msgQueue = ReflectUtils.getFieldValue(messageQueue, "msgQueue");
for (Message message : msgQueue) {
if (message.getType() == MessageTypes.BLOCK) {
Assert.assertEquals(message.getMessageId(), condition.getBlockId());
}
if (message.getType() == MessageTypes.TRX) {
Assert.assertEquals(message.getMessageId(), condition.getTransactionId());
}
}
}
Assert.assertTrue(count >= 2);
}
private static boolean go = false;
@Before
public void init() {
new Thread(new Runnable() {
@Override
public void run() {
logger.info("Full node running.");
Args.setParam(
new String[]{
"--output-directory", dbPath,
"--storage-db-directory", dbDirectory,
"--storage-index-directory", indexDirectory
},
"config.conf"
);
Args cfgArgs = Args.getInstance();
cfgArgs.setNodeListenPort(17889);
cfgArgs.setNodeDiscoveryEnable(false);
cfgArgs.getSeedNode().getIpList().clear();
cfgArgs.setNeedSyncCheck(false);
cfgArgs.setNodeExternalIp("127.0.0.1");
context = new AnnotationConfigApplicationContext(DefaultConfig.class);
if (cfgArgs.isHelp()) {
logger.info("Here is the help message.");
return;
}
appT = ApplicationFactory.create(context);
rpcApiService = context.getBean(RpcApiService.class);
appT.addService(rpcApiService);
if (cfgArgs.isWitness()) {
appT.addService(new WitnessService(appT, context));
}
// appT.initServices(cfgArgs);
// appT.startServices();
// appT.startup();
node = context.getBean(NodeImpl.class);
peerClient = context.getBean(PeerClient.class);
channelManager = context.getBean(ChannelManager.class);
pool = context.getBean(SyncPool.class);
Manager dbManager = context.getBean(Manager.class);
NodeDelegate nodeDelegate = new NodeDelegateImpl(dbManager);
node.setNodeDelegate(nodeDelegate);
pool.init(node);
prepare();
rpcApiService.blockUntilShutdown();
}
}).start();
int tryTimes = 1;
while (tryTimes <= 30 && (node == null || peerClient == null
|| channelManager == null || pool == null || !go)) {
try {
logger.info("node:{},peerClient:{},channelManager:{},pool:{},{}", node, peerClient,
channelManager, pool, go);
Thread.sleep(1000 * tryTimes);
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
++tryTimes;
}
}
}
private void prepare() {
try {
ExecutorService advertiseLoopThread = ReflectUtils.getFieldValue(node, "broadPool");
advertiseLoopThread.shutdownNow();
ReflectUtils.setFieldValue(node, "isAdvertiseActive", false);
ReflectUtils.setFieldValue(node, "isFetchActive", false);
Node node = new Node(
"enode://e437a4836b77ad9d9ffe73ee782ef2614e6d8370fcf62191a6e488276e23717147073a7ce0b444d485fff5a0c34c4577251a7a990cf80d8542e21b95aa8c5e6c@127.0.0.1:17889");
new Thread(new Runnable() {
@Override
public void run() {
peerClient.connect(node.getHost(), node.getPort(), node.getHexId());
}
}).start();
Thread.sleep(2000);
Map<ByteArrayWrapper, Channel> activePeers = ReflectUtils
.getFieldValue(channelManager, "activePeers");
int tryTimes = 0;
while (MapUtils.isEmpty(activePeers) && ++tryTimes < 10) {
Thread.sleep(1000);
}
go = true;
} catch (Exception e) {
e.printStackTrace();
}
}
@After
public void destroy() {
Args.clearParam();
FileUtil.deleteDir(new File("output-nodeImplTest"));
Collection<PeerConnection> peerConnections = ReflectUtils.invokeMethod(node, "getActivePeer");
for (PeerConnection peer : peerConnections) {
peer.close();
}
peerClient.close();
appT.shutdown();
}
}