|
4 | 4 | import static org.tron.core.config.Parameter.NodeConstant.MAX_BLOCKS_IN_PROCESS; |
5 | 5 | import static org.tron.core.config.Parameter.NodeConstant.MAX_BLOCKS_SYNC_FROM_ONE_PEER; |
6 | 6 |
|
| 7 | +import com.google.common.cache.Cache; |
| 8 | +import com.google.common.cache.CacheBuilder; |
7 | 9 | import com.google.common.collect.Iterables; |
8 | 10 | import io.netty.util.internal.ConcurrentSet; |
9 | 11 | import java.util.ArrayList; |
@@ -70,6 +72,14 @@ public class NodeImpl extends PeerConnectionDelegate implements Node { |
70 | 72 | @Lazy |
71 | 73 | private SyncPool pool; |
72 | 74 |
|
| 75 | + Cache<Sha256Hash, TransactionMessage> TrxCache = CacheBuilder.newBuilder() |
| 76 | + .maximumSize(10000).expireAfterWrite(60, TimeUnit.SECONDS) |
| 77 | + .recordStats().build(); |
| 78 | + |
| 79 | + Cache<Sha256Hash, BlockMessage> BlockCache = CacheBuilder.newBuilder() |
| 80 | + .maximumSize(10).expireAfterWrite(60, TimeUnit.SECONDS) |
| 81 | + .recordStats().build(); |
| 82 | + |
73 | 83 | class InvToSend { |
74 | 84 |
|
75 | 85 | private HashMap<PeerConnection, HashMap<InventoryType, LinkedList<Sha256Hash>>> send |
@@ -241,10 +251,10 @@ public void broadcast(Message msg) { |
241 | 251 | if (msg instanceof BlockMessage) { |
242 | 252 | logger.info("Ready to broadcast a block, Its hash is " + msg.getMessageId()); |
243 | 253 | freshBlockId.offer(((BlockMessage) msg).getBlockId()); |
244 | | - blockToAdvertise.add(((BlockMessage) msg).getBlockId()); |
| 254 | + BlockCache.put(msg.getMessageId(), (BlockMessage) msg); |
245 | 255 | type = InventoryType.BLOCK; |
246 | 256 | } else if (msg instanceof TransactionMessage) { |
247 | | - trxToAdvertise.add(msg.getMessageId()); |
| 257 | + TrxCache.put(msg.getMessageId(), (TransactionMessage)msg); |
248 | 258 | type = InventoryType.TRX; |
249 | 259 | } else { |
250 | 260 | return; |
@@ -776,24 +786,36 @@ private void onHandleFetchDataMessage(PeerConnection peer, FetchInvDataMessage f |
776 | 786 | MessageTypes type = fetchInvDataMsg.getInvMessageType(); |
777 | 787 |
|
778 | 788 | //TODO:maybe can use message cache here |
779 | | - final BlockCapsule[] blocks = {del.getGenesisBlock()}; |
| 789 | + BlockCapsule block = null; |
780 | 790 | //get data and send it one by one |
781 | | - fetchInvDataMsg.getHashList() |
782 | | - .forEach(hash -> { |
783 | | - if (del.contain(hash, type)) { |
784 | | - Message msg = del.getData(hash, type); |
785 | | - if (type.equals(MessageTypes.BLOCK)) { |
786 | | - blocks[0] = ((BlockMessage) msg).getBlockCapsule(); |
787 | | - } |
788 | | - peer.sendMessage(msg); |
789 | | - } else { |
790 | | - peer.sendMessage(new ItemNotFound()); |
791 | | - } |
792 | | - }); |
| 791 | + for (Sha256Hash hash : fetchInvDataMsg.getHashList()){ |
| 792 | + |
| 793 | + Message msg; |
| 794 | + |
| 795 | + if (type == MessageTypes.BLOCK){ |
| 796 | + msg = BlockCache.getIfPresent(hash); |
| 797 | + }else { |
| 798 | + msg = TrxCache.getIfPresent(hash); |
| 799 | + } |
| 800 | + |
| 801 | + if (msg == null){ |
| 802 | + msg = del.getData(hash, type); |
| 803 | + } |
| 804 | + |
| 805 | + if (msg != null) { |
| 806 | + if (type.equals(MessageTypes.BLOCK)) { |
| 807 | + block = ((BlockMessage) msg).getBlockCapsule(); |
| 808 | + } |
| 809 | + peer.sendMessage(msg); |
| 810 | + } else { |
| 811 | + logger.error("fetch message {} {} failed.", type, hash); |
| 812 | + peer.sendMessage(new ItemNotFound()); |
| 813 | + } |
| 814 | + } |
793 | 815 |
|
794 | | - if (blocks[0] != null) { |
795 | | - peer.setHeadBlockWeBothHave(blocks[0].getBlockId()); |
796 | | - peer.setHeadBlockTimeWeBothHave(blocks[0].getTimeStamp()); |
| 816 | + if (block != null) { |
| 817 | + peer.setHeadBlockWeBothHave(block.getBlockId()); |
| 818 | + peer.setHeadBlockTimeWeBothHave(block.getTimeStamp()); |
797 | 819 | } |
798 | 820 | } |
799 | 821 |
|
|
0 commit comments