|
| 1 | +package org.tron.core.net.node; |
| 2 | + |
| 3 | +import com.google.protobuf.ByteString; |
| 4 | +import java.io.File; |
| 5 | +import lombok.extern.slf4j.Slf4j; |
| 6 | +import org.junit.AfterClass; |
| 7 | +import org.junit.Assert; |
| 8 | +import org.junit.BeforeClass; |
| 9 | +import org.junit.Test; |
| 10 | +import org.springframework.context.annotation.AnnotationConfigApplicationContext; |
| 11 | +import org.tron.common.application.Application; |
| 12 | +import org.tron.common.application.ApplicationFactory; |
| 13 | +import org.tron.common.crypto.ECKey; |
| 14 | +import org.tron.common.overlay.client.DatabaseGrpcClient; |
| 15 | +import org.tron.common.utils.ByteArray; |
| 16 | +import org.tron.core.Constant; |
| 17 | +import org.tron.core.capsule.BlockCapsule; |
| 18 | +import org.tron.core.capsule.utils.BlockUtil; |
| 19 | +import org.tron.core.capsule.utils.TransactionUtil; |
| 20 | +import org.tron.core.config.DefaultConfig; |
| 21 | +import org.tron.core.config.args.Args; |
| 22 | +import org.tron.core.db.Manager; |
| 23 | +import org.tron.core.net.message.BlockMessage; |
| 24 | +import org.tron.core.net.peer.PeerConnection; |
| 25 | +import org.tron.core.services.RpcApiService; |
| 26 | +import org.tron.protos.Protocol.Block; |
| 27 | +import org.tron.protos.Protocol.BlockHeader; |
| 28 | +import org.tron.protos.Protocol.DynamicProperties; |
| 29 | +import org.tron.protos.Protocol.Transaction; |
| 30 | + |
| 31 | +@Slf4j |
| 32 | +public class NodeImplTest { |
| 33 | + |
| 34 | + private static AnnotationConfigApplicationContext context; |
| 35 | + |
| 36 | + private static Application appT; |
| 37 | + private static String dbPath = "output_nodeimpl_test"; |
| 38 | + private static NodeImpl nodeImpl; |
| 39 | + private static Manager dbManager; |
| 40 | + private static NodeDelegateImpl nodeDelegate; |
| 41 | + |
| 42 | + static { |
| 43 | + Args.setParam(new String[]{"-d", dbPath}, Constant.TEST_CONF); |
| 44 | + context = new AnnotationConfigApplicationContext(DefaultConfig.class); |
| 45 | + Args.getInstance().setSolidityNode(true); |
| 46 | + appT = ApplicationFactory.create(context); |
| 47 | + } |
| 48 | + |
| 49 | + /** |
| 50 | + * init db. |
| 51 | + */ |
| 52 | + @BeforeClass |
| 53 | + public static void init() { |
| 54 | + nodeImpl = context.getBean(NodeImpl.class); |
| 55 | + dbManager = context.getBean(Manager.class); |
| 56 | + nodeDelegate = new NodeDelegateImpl(dbManager); |
| 57 | + nodeImpl.setNodeDelegate(nodeDelegate); |
| 58 | + } |
| 59 | + |
| 60 | + /** |
| 61 | + * remo db when after test. |
| 62 | + */ |
| 63 | + @AfterClass |
| 64 | + public static void removeDb() { |
| 65 | + Args.clearParam(); |
| 66 | + |
| 67 | + File dbFolder = new File(dbPath); |
| 68 | + if (deleteFolder(dbFolder)) { |
| 69 | + logger.info("Release resources successful."); |
| 70 | + } else { |
| 71 | + logger.info("Release resources failure."); |
| 72 | + } |
| 73 | + context.destroy(); |
| 74 | + } |
| 75 | + |
| 76 | + private static Boolean deleteFolder(File index) { |
| 77 | + if (!index.isDirectory() || index.listFiles().length <= 0) { |
| 78 | + return index.delete(); |
| 79 | + } |
| 80 | + for (File file : index.listFiles()) { |
| 81 | + if (null != file && !deleteFolder(file)) { |
| 82 | + return false; |
| 83 | + } |
| 84 | + } |
| 85 | + return index.delete(); |
| 86 | + } |
| 87 | + |
| 88 | + @Test |
| 89 | + public void testSyncBlockMessage() { |
| 90 | + PeerConnection peer = new PeerConnection(); |
| 91 | + BlockCapsule genesisBlockCapsule = BlockUtil.newGenesisBlockCapsule(); |
| 92 | + |
| 93 | + ByteString witnessAddress = ByteString.copyFrom( |
| 94 | + ECKey.fromPrivate( |
| 95 | + ByteArray.fromHexString( |
| 96 | + Args.getInstance().getLocalWitnesses().getPrivateKey())) |
| 97 | + .getAddress()); |
| 98 | + BlockHeader.raw raw = BlockHeader.raw.newBuilder() |
| 99 | + .setTimestamp(System.currentTimeMillis()) |
| 100 | + .setParentHash(genesisBlockCapsule.getParentHash().getByteString()) |
| 101 | + .setNumber(genesisBlockCapsule.getNum() + 1) |
| 102 | + .setWitnessAddress(witnessAddress) |
| 103 | + .setWitnessId(1).build(); |
| 104 | + BlockHeader blockHeader = BlockHeader.newBuilder() |
| 105 | + .setRawData(raw) |
| 106 | + .build(); |
| 107 | + |
| 108 | + Block block = Block.newBuilder().setBlockHeader(blockHeader).build(); |
| 109 | + |
| 110 | + BlockCapsule blockCapsule = new BlockCapsule(block); |
| 111 | + blockCapsule.sign( |
| 112 | + ByteArray.fromHexString(Args.getInstance().getLocalWitnesses().getPrivateKey())); |
| 113 | + blockCapsule.setMerkleRoot(); |
| 114 | + BlockMessage blockMessage = new BlockMessage(blockCapsule); |
| 115 | + peer.getSyncBlockRequested().put(blockMessage.getBlockId(), System.currentTimeMillis()); |
| 116 | + nodeImpl.onMessage(peer, blockMessage); |
| 117 | + Assert.assertEquals(peer.getSyncBlockRequested().size(), 0); |
| 118 | + } |
| 119 | + |
| 120 | + @Test |
| 121 | + public void testAdvBlockMessage() { |
| 122 | + PeerConnection peer = new PeerConnection(); |
| 123 | + BlockCapsule genesisBlockCapsule = BlockUtil.newGenesisBlockCapsule(); |
| 124 | + |
| 125 | + ByteString witnessAddress = ByteString.copyFrom( |
| 126 | + ECKey.fromPrivate( |
| 127 | + ByteArray.fromHexString( |
| 128 | + Args.getInstance().getLocalWitnesses().getPrivateKey())) |
| 129 | + .getAddress()); |
| 130 | + BlockHeader.raw raw = BlockHeader.raw.newBuilder() |
| 131 | + .setTimestamp(System.currentTimeMillis()) |
| 132 | + .setParentHash(genesisBlockCapsule.getParentHash().getByteString()) |
| 133 | + .setNumber(genesisBlockCapsule.getNum() + 1) |
| 134 | + .setWitnessAddress(witnessAddress) |
| 135 | + .setWitnessId(1).build(); |
| 136 | + BlockHeader blockHeader = BlockHeader.newBuilder() |
| 137 | + .setRawData(raw) |
| 138 | + .build(); |
| 139 | + |
| 140 | + Block block = Block.newBuilder().setBlockHeader(blockHeader).build(); |
| 141 | + |
| 142 | + BlockCapsule blockCapsule = new BlockCapsule(block); |
| 143 | + blockCapsule.sign( |
| 144 | + ByteArray.fromHexString(Args.getInstance().getLocalWitnesses().getPrivateKey())); |
| 145 | + blockCapsule.setMerkleRoot(); |
| 146 | + BlockMessage blockMessage = new BlockMessage(blockCapsule); |
| 147 | + peer.getAdvObjWeRequested().put(blockMessage.getBlockId(), System.currentTimeMillis()); |
| 148 | + nodeImpl.onMessage(peer, blockMessage); |
| 149 | + Assert.assertEquals(peer.getAdvObjWeRequested().size(), 0); |
| 150 | + } |
| 151 | + |
| 152 | +} |
0 commit comments