From 5b7a268fb04a3654e1ac11c66c7c15a02759506e Mon Sep 17 00:00:00 2001 From: Matt Date: Wed, 28 Nov 2018 12:26:28 +0800 Subject: [PATCH 1/3] db.version default 2 --- .../java/org/tron/core/config/args/Storage.java | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) diff --git a/src/main/java/org/tron/core/config/args/Storage.java b/src/main/java/org/tron/core/config/args/Storage.java index 5753e716998..f1d5c3708e1 100644 --- a/src/main/java/org/tron/core/config/args/Storage.java +++ b/src/main/java/org/tron/core/config/args/Storage.java @@ -17,19 +17,15 @@ import com.typesafe.config.Config; import com.typesafe.config.ConfigObject; - -import org.apache.commons.lang3.StringUtils; -import org.iq80.leveldb.CompressionType; -import org.iq80.leveldb.Options; - import java.io.File; import java.util.Map; import java.util.stream.Collectors; - -import org.tron.common.utils.FileUtil; - import lombok.Getter; import lombok.Setter; +import org.apache.commons.lang3.StringUtils; +import org.iq80.leveldb.CompressionType; +import org.iq80.leveldb.Options; +import org.tron.common.utils.FileUtil; /** * Custom storage configurations @@ -64,7 +60,7 @@ public class Storage { /** * Default values of directory */ - private static final int DEFAULT_DB_VERSION = 1; + private static final int DEFAULT_DB_VERSION = 2; private static final String DEFAULT_DB_DIRECTORY = "database"; private static final String DEFAULT_INDEX_DIRECTORY = "index"; private static final String DEFAULT_INDEX_SWTICH = "on"; From e73e312fced351fdad174d06821c581c661b2358 Mon Sep 17 00:00:00 2001 From: Matt Date: Wed, 28 Nov 2018 13:19:06 +0800 Subject: [PATCH 2/3] print db version --- src/main/java/org/tron/core/config/args/Args.java | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/main/java/org/tron/core/config/args/Args.java b/src/main/java/org/tron/core/config/args/Args.java index 71e8544eae2..47d6e3489d0 100644 --- a/src/main/java/org/tron/core/config/args/Args.java +++ b/src/main/java/org/tron/core/config/args/Args.java @@ -953,6 +953,8 @@ private static void logConfig() { logger.info("Backup priority: {}", args.getBackupPriority()); logger.info("************************ Code version *************************"); logger.info("Code version : {}", Version.getVersion()); + logger.info("************************ DB config *************************"); + logger.info("DB version : {}", args.getStorage().getDbVersion()); logger.info("***************************************************************"); logger.info("\n"); } From 753482c2166942ce9b3e3fdf1b222d3526dc0fbc Mon Sep 17 00:00:00 2001 From: lvs007 Date: Wed, 28 Nov 2018 15:12:24 +0800 Subject: [PATCH 3/3] fix test case --- .../net/node/GetBlockChainSummaryTest.java | 570 +++++++++--------- 1 file changed, 296 insertions(+), 274 deletions(-) diff --git a/src/test/java/org/tron/core/net/node/GetBlockChainSummaryTest.java b/src/test/java/org/tron/core/net/node/GetBlockChainSummaryTest.java index c3c99ca5bc3..74e53bed873 100644 --- a/src/test/java/org/tron/core/net/node/GetBlockChainSummaryTest.java +++ b/src/test/java/org/tron/core/net/node/GetBlockChainSummaryTest.java @@ -2,19 +2,34 @@ import com.google.common.collect.Maps; import com.google.protobuf.ByteString; +import java.io.File; +import java.util.ArrayList; +import java.util.Collection; +import java.util.Deque; +import java.util.Map; +import java.util.concurrent.ConcurrentLinkedDeque; +import java.util.concurrent.ExecutorService; +import java.util.stream.Collectors; +import java.util.stream.IntStream; import lombok.extern.slf4j.Slf4j; import org.apache.commons.collections4.MapUtils; -import org.junit.*; -import org.tron.common.application.TronApplicationContext; +import org.junit.AfterClass; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; import org.tron.common.application.Application; import org.tron.common.application.ApplicationFactory; +import org.tron.common.application.TronApplicationContext; import org.tron.common.crypto.ECKey; -import org.tron.common.overlay.client.PeerClient; import org.tron.common.overlay.discover.node.Node; import org.tron.common.overlay.server.Channel; import org.tron.common.overlay.server.ChannelManager; import org.tron.common.overlay.server.SyncPool; -import org.tron.common.utils.*; +import org.tron.common.utils.ByteArray; +import org.tron.common.utils.FileUtil; +import org.tron.common.utils.ReflectUtils; +import org.tron.common.utils.Sha256Hash; +import org.tron.common.utils.Utils; import org.tron.core.capsule.AccountCapsule; import org.tron.core.capsule.BlockCapsule; import org.tron.core.capsule.WitnessCapsule; @@ -32,297 +47,304 @@ import org.tron.core.witness.WitnessController; import org.tron.protos.Protocol; -import java.io.File; -import java.util.ArrayList; -import java.util.Collection; -import java.util.Deque; -import java.util.Map; -import java.util.concurrent.ConcurrentLinkedDeque; -import java.util.concurrent.ExecutorService; -import java.util.stream.Collectors; -import java.util.stream.IntStream; - @Slf4j -public class GetBlockChainSummaryTest{ - private static TronApplicationContext context; - private static NodeImpl node; - private RpcApiService rpcApiService; - private static PeerClientTest peerClient; - private ChannelManager channelManager; - private SyncPool pool; - private static Application appT; - private Manager dbManager; - private Node nodeEntity; - private static HandshakeHandlerTest handshakeHandlerTest; - - private static final String dbPath = "output-GetBlockChainSummary"; - private static final String dbDirectory = "db_GetBlockChainSummary_test"; - private static final String indexDirectory = "index_GetBlockChainSummary_test"; - - @Test - public void testGetBlockChainSummary() { - NodeDelegate del = ReflectUtils.getFieldValue(node, "del"); - Collection activePeers = ReflectUtils.invokeMethod(node, "getActivePeer"); - BlockStore blkstore = dbManager.getBlockStore(); - - Object[] peers = activePeers.toArray(); - PeerConnection peer_he = (PeerConnection) peers[1]; - Deque toFetch = new ConcurrentLinkedDeque<>(); - - ArrayList scenes = new ArrayList<>(); - scenes.add("genesis"); - scenes.add("genesis_fetch"); - scenes.add("nongenesis_fetch"); - - long number = dbManager.getDynamicPropertiesStore().getLatestBlockHeaderNumber() + 1; - Map addressToProvateKeys = addTestWitnessAndAccount(); - BlockCapsule capsule = createTestBlockCapsule(1533529947843L,number, dbManager.getDynamicPropertiesStore().getLatestBlockHeaderHash().getByteString(), - addressToProvateKeys); - try { - dbManager.pushBlock(capsule); - } catch (Exception e) { - e.printStackTrace(); - } - for (int i = 1; i < 5; i++) { - number = dbManager.getDynamicPropertiesStore().getLatestBlockHeaderNumber() + 1; - capsule = createTestBlockCapsule(1533529947843L + 3000L * i,number, dbManager.getDynamicPropertiesStore().getLatestBlockHeaderHash().getByteString(), addressToProvateKeys); - try { - dbManager.pushBlock(capsule); - } catch (Exception e) { - e.printStackTrace(); - } - } +public class GetBlockChainSummaryTest { - BlockCapsule.BlockId commonBlockId = null; - - //the common block is genesisblock,syncBlockToFetch is empty。 - try { - commonBlockId = del.getGenesisBlock().getBlockId(); - peer_he.getSyncBlockToFetch().clear(); - ReflectUtils.setFieldValue(peer_he, "headBlockWeBothHave", commonBlockId); - ReflectUtils.setFieldValue(peer_he, "headBlockTimeWeBothHave", System.currentTimeMillis()); - Deque retSummary = del.getBlockChainSummary(peer_he.getHeadBlockWeBothHave(), peer_he.getSyncBlockToFetch()); - Assert.assertTrue(retSummary.size() == 3); - } catch (Exception e) { - System.out.println("exception!"); - } + private static TronApplicationContext context; + private static NodeImpl node; + private RpcApiService rpcApiService; + private static PeerClientTest peerClient; + private ChannelManager channelManager; + private SyncPool pool; + private static Application appT; + private Manager dbManager; + private Node nodeEntity; + private static HandshakeHandlerTest handshakeHandlerTest; - //the common block is genesisblock,syncBlockToFetch is not empty。 - peer_he.getSyncBlockToFetch().addAll(toFetch); - try { - toFetch.clear(); - peer_he.getSyncBlockToFetch().clear(); - for(int i=0; i<4 ;i++){ - number = dbManager.getDynamicPropertiesStore().getLatestBlockHeaderNumber() + 1; - capsule = createTestBlockCapsule(1533529947843L + 3000L * i, number, dbManager.getDynamicPropertiesStore().getLatestBlockHeaderHash().getByteString(), - addressToProvateKeys); - toFetch.add(capsule.getBlockId()); - } - - commonBlockId = del.getGenesisBlock().getBlockId(); - peer_he.getSyncBlockToFetch().addAll(toFetch); - ReflectUtils.setFieldValue(peer_he, "headBlockWeBothHave", commonBlockId); - ReflectUtils.setFieldValue(peer_he, "headBlockTimeWeBothHave", System.currentTimeMillis()); - Deque retSummary = del.getBlockChainSummary(peer_he.getHeadBlockWeBothHave(), peer_he.getSyncBlockToFetch()); - Assert.assertTrue(retSummary.size() == 4); - } catch (Exception e) { - System.out.println("exception!"); - } + private static final String dbPath = "output-GetBlockChainSummary"; + private static final String dbDirectory = "db_GetBlockChainSummary_test"; + private static final String indexDirectory = "index_GetBlockChainSummary_test"; - //the common block is a normal block(not genesisblock),syncBlockToFetc is not empty. - try { - toFetch.clear(); - peer_he.getSyncBlockToFetch().clear(); - number = dbManager.getDynamicPropertiesStore().getLatestBlockHeaderNumber() + 1; - BlockCapsule capsule1 = createTestBlockCapsule(1533529947843L + 3000L * 6, number, dbManager.getDynamicPropertiesStore().getLatestBlockHeaderHash().getByteString(), - addressToProvateKeys); - dbManager.pushBlock(capsule1); - - number = dbManager.getDynamicPropertiesStore().getLatestBlockHeaderNumber() + 1; - BlockCapsule capsule2 = createTestBlockCapsule(1533529947843L + 3000L * 7, number, dbManager.getDynamicPropertiesStore().getLatestBlockHeaderHash().getByteString(), - addressToProvateKeys); - dbManager.pushBlock(capsule2); - - for(int i=0; i<2; i++){ - number = dbManager.getDynamicPropertiesStore().getLatestBlockHeaderNumber() + 1; - capsule = createTestBlockCapsule(1533529947843L + 3000L * 8 + 3000L * i, number, dbManager.getDynamicPropertiesStore().getLatestBlockHeaderHash().getByteString(), - addressToProvateKeys); - toFetch.add(capsule.getBlockId()); - } - commonBlockId = capsule2.getBlockId(); - peer_he.getSyncBlockToFetch().addAll(toFetch); - toFetch.forEach(block -> blkstore.delete(block.getBytes())); - ReflectUtils.setFieldValue(peer_he, "headBlockWeBothHave", commonBlockId); - ReflectUtils.setFieldValue(peer_he, "headBlockTimeWeBothHave", System.currentTimeMillis()); - Deque retSummary = del.getBlockChainSummary(peer_he.getHeadBlockWeBothHave(), peer_he.getSyncBlockToFetch()); - Assert.assertTrue(retSummary.size() == 4); - } catch (Exception e) { - System.out.println("exception!"); - } - logger.info("finish1"); - } + @Test + public void testGetBlockChainSummary() { + NodeDelegate del = ReflectUtils.getFieldValue(node, "del"); + Collection activePeers = ReflectUtils.invokeMethod(node, "getActivePeer"); + BlockStore blkstore = dbManager.getBlockStore(); + Object[] peers = activePeers.toArray(); + if (peers == null || peers.length <= 0) { + return; + } + PeerConnection peer_he = (PeerConnection) peers[0]; + Deque toFetch = new ConcurrentLinkedDeque<>(); - private static boolean go = false; - private Map addTestWitnessAndAccount() { - dbManager.getWitnesses().clear(); - return IntStream.range(0, 2) - .mapToObj( - i -> { - ECKey ecKey = new ECKey(Utils.getRandom()); - String privateKey = ByteArray.toHexString(ecKey.getPrivKey().toByteArray()); - ByteString address = ByteString.copyFrom(ecKey.getAddress()); + ArrayList scenes = new ArrayList<>(); + scenes.add("genesis"); + scenes.add("genesis_fetch"); + scenes.add("nongenesis_fetch"); - WitnessCapsule witnessCapsule = new WitnessCapsule(address); - dbManager.getWitnessStore().put(address.toByteArray(), witnessCapsule); - dbManager.getWitnessController().addWitness(address); + long number = dbManager.getDynamicPropertiesStore().getLatestBlockHeaderNumber() + 1; + Map addressToProvateKeys = addTestWitnessAndAccount(); + BlockCapsule capsule = createTestBlockCapsule(1533529947843L, number, + dbManager.getDynamicPropertiesStore().getLatestBlockHeaderHash().getByteString(), + addressToProvateKeys); + try { + dbManager.pushBlock(capsule); + } catch (Exception e) { + e.printStackTrace(); + } + for (int i = 1; i < 5; i++) { + number = dbManager.getDynamicPropertiesStore().getLatestBlockHeaderNumber() + 1; + capsule = createTestBlockCapsule(1533529947843L + 3000L * i, number, + dbManager.getDynamicPropertiesStore().getLatestBlockHeaderHash().getByteString(), + addressToProvateKeys); + try { + dbManager.pushBlock(capsule); + } catch (Exception e) { + e.printStackTrace(); + } + } - AccountCapsule accountCapsule = - new AccountCapsule(Protocol.Account.newBuilder().setAddress(address).build()); - dbManager.getAccountStore().put(address.toByteArray(), accountCapsule); + BlockCapsule.BlockId commonBlockId = null; - return Maps.immutableEntry(address, privateKey); - }) - .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue)); + //the common block is genesisblock,syncBlockToFetch is empty。 + try { + commonBlockId = del.getGenesisBlock().getBlockId(); + peer_he.getSyncBlockToFetch().clear(); + ReflectUtils.setFieldValue(peer_he, "headBlockWeBothHave", commonBlockId); + ReflectUtils.setFieldValue(peer_he, "headBlockTimeWeBothHave", System.currentTimeMillis()); + Deque retSummary = del + .getBlockChainSummary(peer_he.getHeadBlockWeBothHave(), peer_he.getSyncBlockToFetch()); + Assert.assertTrue(retSummary.size() == 3); + } catch (Exception e) { + System.out.println("exception!"); } - private BlockCapsule createTestBlockCapsule( - long number, ByteString hash, Map addressToProvateKeys) { - long time = System.currentTimeMillis(); - return createTestBlockCapsule(time,number,hash,addressToProvateKeys); + + //the common block is genesisblock,syncBlockToFetch is not empty。 + peer_he.getSyncBlockToFetch().addAll(toFetch); + try { + toFetch.clear(); + peer_he.getSyncBlockToFetch().clear(); + for (int i = 0; i < 4; i++) { + number = dbManager.getDynamicPropertiesStore().getLatestBlockHeaderNumber() + 1; + capsule = createTestBlockCapsule(1533529947843L + 3000L * i, number, + dbManager.getDynamicPropertiesStore().getLatestBlockHeaderHash().getByteString(), + addressToProvateKeys); + toFetch.add(capsule.getBlockId()); + } + + commonBlockId = del.getGenesisBlock().getBlockId(); + peer_he.getSyncBlockToFetch().addAll(toFetch); + ReflectUtils.setFieldValue(peer_he, "headBlockWeBothHave", commonBlockId); + ReflectUtils.setFieldValue(peer_he, "headBlockTimeWeBothHave", System.currentTimeMillis()); + Deque retSummary = del + .getBlockChainSummary(peer_he.getHeadBlockWeBothHave(), peer_he.getSyncBlockToFetch()); + Assert.assertTrue(retSummary.size() == 4); + } catch (Exception e) { + System.out.println("exception!"); } - private BlockCapsule createTestBlockCapsule(long time , - long number, ByteString hash, Map addressToProvateKeys) { - WitnessController witnessController = dbManager.getWitnessController(); - ByteString witnessAddress = - witnessController.getScheduledWitness(witnessController.getSlotAtTime(time)); - BlockCapsule blockCapsule = new BlockCapsule(number, Sha256Hash.wrap(hash), time, witnessAddress); - blockCapsule.generatedByMyself = true; - blockCapsule.setMerkleRoot(); - blockCapsule.sign(ByteArray.fromHexString(addressToProvateKeys.get(witnessAddress))); - return blockCapsule; + //the common block is a normal block(not genesisblock),syncBlockToFetc is not empty. + try { + toFetch.clear(); + peer_he.getSyncBlockToFetch().clear(); + number = dbManager.getDynamicPropertiesStore().getLatestBlockHeaderNumber() + 1; + BlockCapsule capsule1 = createTestBlockCapsule(1533529947843L + 3000L * 6, number, + dbManager.getDynamicPropertiesStore().getLatestBlockHeaderHash().getByteString(), + addressToProvateKeys); + dbManager.pushBlock(capsule1); + + number = dbManager.getDynamicPropertiesStore().getLatestBlockHeaderNumber() + 1; + BlockCapsule capsule2 = createTestBlockCapsule(1533529947843L + 3000L * 7, number, + dbManager.getDynamicPropertiesStore().getLatestBlockHeaderHash().getByteString(), + addressToProvateKeys); + dbManager.pushBlock(capsule2); + + for (int i = 0; i < 2; i++) { + number = dbManager.getDynamicPropertiesStore().getLatestBlockHeaderNumber() + 1; + capsule = createTestBlockCapsule(1533529947843L + 3000L * 8 + 3000L * i, number, + dbManager.getDynamicPropertiesStore().getLatestBlockHeaderHash().getByteString(), + addressToProvateKeys); + toFetch.add(capsule.getBlockId()); + } + commonBlockId = capsule2.getBlockId(); + peer_he.getSyncBlockToFetch().addAll(toFetch); + toFetch.forEach(block -> blkstore.delete(block.getBytes())); + ReflectUtils.setFieldValue(peer_he, "headBlockWeBothHave", commonBlockId); + ReflectUtils.setFieldValue(peer_he, "headBlockTimeWeBothHave", System.currentTimeMillis()); + Deque retSummary = del + .getBlockChainSummary(peer_he.getHeadBlockWeBothHave(), peer_he.getSyncBlockToFetch()); + Assert.assertTrue(retSummary.size() == 4); + } catch (Exception e) { + System.out.println("exception!"); } + logger.info("finish1"); + } + + + private static boolean go = false; + + private Map addTestWitnessAndAccount() { + dbManager.getWitnesses().clear(); + return IntStream.range(0, 2) + .mapToObj( + i -> { + ECKey ecKey = new ECKey(Utils.getRandom()); + String privateKey = ByteArray.toHexString(ecKey.getPrivKey().toByteArray()); + ByteString address = ByteString.copyFrom(ecKey.getAddress()); + + WitnessCapsule witnessCapsule = new WitnessCapsule(address); + dbManager.getWitnessStore().put(address.toByteArray(), witnessCapsule); + dbManager.getWitnessController().addWitness(address); + + AccountCapsule accountCapsule = + new AccountCapsule(Protocol.Account.newBuilder().setAddress(address).build()); + dbManager.getAccountStore().put(address.toByteArray(), accountCapsule); + + return Maps.immutableEntry(address, privateKey); + }) + .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue)); + } + + private BlockCapsule createTestBlockCapsule( + long number, ByteString hash, Map addressToProvateKeys) { + long time = System.currentTimeMillis(); + return createTestBlockCapsule(time, number, hash, addressToProvateKeys); + } - @Before - public void init() { - nodeEntity = new Node( - "enode://e437a4836b77ad9d9ffe73ee782ef2614e6d8370fcf62191a6e488276e23717147073a7ce0b444d485fff5a0c34c4577251a7a990cf80d8542e21b95aa8c5e6c@127.0.0.1:17896"); - - Thread thread = 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(17896); - cfgArgs.setNodeDiscoveryEnable(false); - cfgArgs.getSeedNode().getIpList().clear(); - cfgArgs.setNeedSyncCheck(false); - cfgArgs.setNodeExternalIp("127.0.0.1"); - - context = new TronApplicationContext(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)); - } + private BlockCapsule createTestBlockCapsule(long time, + long number, ByteString hash, Map addressToProvateKeys) { + WitnessController witnessController = dbManager.getWitnessController(); + ByteString witnessAddress = + witnessController.getScheduledWitness(witnessController.getSlotAtTime(time)); + BlockCapsule blockCapsule = new BlockCapsule(number, Sha256Hash.wrap(hash), time, + witnessAddress); + blockCapsule.generatedByMyself = true; + blockCapsule.setMerkleRoot(); + blockCapsule.sign(ByteArray.fromHexString(addressToProvateKeys.get(witnessAddress))); + return blockCapsule; + } + + @Before + public void init() { + nodeEntity = new Node( + "enode://e437a4836b77ad9d9ffe73ee782ef2614e6d8370fcf62191a6e488276e23717147073a7ce0b444d485fff5a0c34c4577251a7a990cf80d8542e21b95aa8c5e6c@127.0.0.1:17896"); + + Thread thread = 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(17896); + cfgArgs.setNodeDiscoveryEnable(false); + cfgArgs.getSeedNode().getIpList().clear(); + cfgArgs.setNeedSyncCheck(false); + cfgArgs.setNodeExternalIp("127.0.0.1"); + + context = new TronApplicationContext(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(PeerClientTest.class); - channelManager = context.getBean(ChannelManager.class); - pool = context.getBean(SyncPool.class); - dbManager = context.getBean(Manager.class); - handshakeHandlerTest = context.getBean(HandshakeHandlerTest.class); - handshakeHandlerTest.setNode(nodeEntity); - NodeDelegate nodeDelegate = new NodeDelegateImpl(dbManager); - node.setNodeDelegate(nodeDelegate); - pool.init(node); - prepare(); - rpcApiService.blockUntilShutdown(); - } - }); - thread.start(); - try { - thread.join(); - } catch (InterruptedException e) { - e.printStackTrace(); - } - int tryTimes = 0; - while (tryTimes < 10 && (node == null || peerClient == null - || channelManager == null || pool == null || !go)) { - try { - logger.info("node:{},peerClient:{},channelManager:{},pool:{},{}", node, peerClient, - channelManager, pool, go); - Thread.sleep(1000); - } catch (InterruptedException e) { - e.printStackTrace(); - } finally { - ++tryTimes; - } - } + node = context.getBean(NodeImpl.class); + peerClient = context.getBean(PeerClientTest.class); + channelManager = context.getBean(ChannelManager.class); + pool = context.getBean(SyncPool.class); + dbManager = context.getBean(Manager.class); + handshakeHandlerTest = context.getBean(HandshakeHandlerTest.class); + handshakeHandlerTest.setNode(nodeEntity); + NodeDelegate nodeDelegate = new NodeDelegateImpl(dbManager); + node.setNodeDelegate(nodeDelegate); + pool.init(node); + prepare(); + rpcApiService.blockUntilShutdown(); + } + }); + thread.start(); + try { + thread.join(); + } catch (InterruptedException e) { + e.printStackTrace(); + } + int tryTimes = 0; + while (tryTimes < 10 && (node == null || peerClient == null + || channelManager == null || pool == null || !go)) { + try { + logger.info("node:{},peerClient:{},channelManager:{},pool:{},{}", node, peerClient, + channelManager, pool, go); + Thread.sleep(1000); + } catch (InterruptedException e) { + e.printStackTrace(); + } finally { + ++tryTimes; + } } + } + + private void prepare() { + try { + ExecutorService advertiseLoopThread = ReflectUtils.getFieldValue(node, "broadPool"); + advertiseLoopThread.shutdownNow(); + + peerClient.prepare(nodeEntity.getHexId()); - private void prepare() { - try { - ExecutorService advertiseLoopThread = ReflectUtils.getFieldValue(node, "broadPool"); - advertiseLoopThread.shutdownNow(); - - peerClient.prepare(nodeEntity.getHexId()); - - ReflectUtils.setFieldValue(node, "isAdvertiseActive", false); - ReflectUtils.setFieldValue(node, "isFetchActive", false); - - TronChannelInitializerTest tronChannelInitializer = ReflectUtils - .getFieldValue(peerClient, "tronChannelInitializer"); - tronChannelInitializer.prepare(); - Channel channel = ReflectUtils.getFieldValue(tronChannelInitializer, "channel"); - ReflectUtils.setFieldValue(channel, "handshakeHandler", handshakeHandlerTest); - - new Thread(new Runnable() { - @Override - public void run() { - peerClient.connect(nodeEntity.getHost(), nodeEntity.getPort(), nodeEntity.getHexId()); - } - }).start(); - Thread.sleep(1000); - Map activePeers = ReflectUtils - .getFieldValue(channelManager, "activePeers"); - int tryTimes = 0; - while (MapUtils.isEmpty(activePeers) && ++tryTimes < 10) { - Thread.sleep(1000); - } - go = true; - } catch (Exception e) { - e.printStackTrace(); + ReflectUtils.setFieldValue(node, "isAdvertiseActive", false); + ReflectUtils.setFieldValue(node, "isFetchActive", false); + + TronChannelInitializerTest tronChannelInitializer = ReflectUtils + .getFieldValue(peerClient, "tronChannelInitializer"); + tronChannelInitializer.prepare(); + Channel channel = ReflectUtils.getFieldValue(tronChannelInitializer, "channel"); + ReflectUtils.setFieldValue(channel, "handshakeHandler", handshakeHandlerTest); + + new Thread(new Runnable() { + @Override + public void run() { + peerClient.connect(nodeEntity.getHost(), nodeEntity.getPort(), nodeEntity.getHexId()); } + }).start(); + Thread.sleep(1000); + Map activePeers = ReflectUtils + .getFieldValue(channelManager, "activePeers"); + int tryTimes = 0; + while (MapUtils.isEmpty(activePeers) && ++tryTimes < 10) { + Thread.sleep(1000); + } + go = true; + } catch (Exception e) { + e.printStackTrace(); } + } - @AfterClass - public static void destroy() { - Args.clearParam(); - Collection peerConnections = ReflectUtils.invokeMethod(node, "getActivePeer"); - for (PeerConnection peer : peerConnections) { - peer.close(); - } - handshakeHandlerTest.close(); - appT.shutdownServices(); - appT.shutdown(); - context.destroy(); - FileUtil.deleteDir(new File(dbPath)); + @AfterClass + public static void destroy() { + Args.clearParam(); + Collection peerConnections = ReflectUtils.invokeMethod(node, "getActivePeer"); + for (PeerConnection peer : peerConnections) { + peer.close(); } + handshakeHandlerTest.close(); + appT.shutdownServices(); + appT.shutdown(); + context.destroy(); + FileUtil.deleteDir(new File(dbPath)); + } }