|
6 | 6 | import com.google.common.cache.Cache; |
7 | 7 | import com.google.common.cache.CacheBuilder; |
8 | 8 | import com.google.common.collect.Lists; |
| 9 | +import com.google.common.collect.Maps; |
| 10 | +import com.google.common.primitives.Longs; |
| 11 | +import com.google.common.util.concurrent.Futures; |
| 12 | +import com.google.common.util.concurrent.ListenableFuture; |
| 13 | +import com.google.common.util.concurrent.ListeningExecutorService; |
| 14 | +import com.google.common.util.concurrent.MoreExecutors; |
9 | 15 | import com.google.protobuf.ByteString; |
10 | 16 | import java.util.ArrayList; |
11 | 17 | import java.util.Arrays; |
12 | 18 | import java.util.Collections; |
13 | 19 | import java.util.Iterator; |
14 | 20 | import java.util.LinkedList; |
15 | 21 | import java.util.List; |
| 22 | +import java.util.Map; |
16 | 23 | import java.util.Objects; |
17 | 24 | import java.util.Set; |
18 | 25 | import java.util.concurrent.BlockingQueue; |
19 | 26 | import java.util.concurrent.Callable; |
| 27 | +import java.util.concurrent.ConcurrentHashMap; |
20 | 28 | import java.util.concurrent.CountDownLatch; |
21 | 29 | import java.util.concurrent.ExecutionException; |
22 | 30 | import java.util.concurrent.ExecutorService; |
23 | 31 | import java.util.concurrent.Executors; |
24 | 32 | import java.util.concurrent.Future; |
25 | 33 | import java.util.concurrent.LinkedBlockingQueue; |
26 | 34 | import java.util.concurrent.TimeUnit; |
| 35 | +import java.util.concurrent.atomic.AtomicLong; |
27 | 36 | import java.util.stream.Collectors; |
| 37 | +import java.util.stream.LongStream; |
28 | 38 | import javafx.util.Pair; |
29 | 39 | import javax.annotation.PostConstruct; |
30 | 40 | import lombok.Getter; |
|
35 | 45 | import org.spongycastle.util.encoders.Hex; |
36 | 46 | import org.springframework.beans.factory.annotation.Autowired; |
37 | 47 | import org.springframework.stereotype.Component; |
| 48 | +import org.tron.common.application.TronApplicationContext; |
38 | 49 | import org.tron.common.overlay.discover.node.Node; |
39 | 50 | import org.tron.common.runtime.config.VMConfig; |
40 | 51 | import org.tron.common.utils.ByteArray; |
|
58 | 69 | import org.tron.core.config.args.GenesisBlock; |
59 | 70 | import org.tron.core.db.KhaosDatabase.KhaosBlock; |
60 | 71 | import org.tron.core.db.api.AssetUpdateHelper; |
| 72 | +import org.tron.core.db2.common.Key; |
61 | 73 | import org.tron.core.db2.core.ISession; |
62 | 74 | import org.tron.core.db2.core.ITronChainBase; |
63 | 75 | import org.tron.core.db2.core.SnapshotManager; |
@@ -98,6 +110,8 @@ public class Manager { |
98 | 110 | @Autowired |
99 | 111 | private TransactionStore transactionStore; |
100 | 112 | @Autowired |
| 113 | + private TransactionCache transactionCache; |
| 114 | + @Autowired |
101 | 115 | private BlockStore blockStore; |
102 | 116 | @Autowired |
103 | 117 | private WitnessStore witnessStore; |
@@ -397,7 +411,7 @@ public void init() { |
397 | 411 | if (Args.getInstance().isNeedToUpdateAsset() && needToUpdateAsset()) { |
398 | 412 | new AssetUpdateHelper(this).doWork(); |
399 | 413 | } |
400 | | - |
| 414 | + initCacheTxs(); |
401 | 415 | revokingStore.enable(); |
402 | 416 | validateSignService = Executors |
403 | 417 | .newFixedThreadPool(Args.getInstance().getValidateSignThreadNum()); |
@@ -502,6 +516,41 @@ private void initWitness() { |
502 | 516 | }); |
503 | 517 | } |
504 | 518 |
|
| 519 | + public void initCacheTxs() { |
| 520 | + long start = System.currentTimeMillis(); |
| 521 | + long headNum = dynamicPropertiesStore.getLatestBlockHeaderNumber(); |
| 522 | + long recentBlockCount = 65536L; |
| 523 | + ListeningExecutorService service = MoreExecutors.listeningDecorator(Executors.newFixedThreadPool(50)); |
| 524 | + List<ListenableFuture<?>> futures = new ArrayList<>(); |
| 525 | + AtomicLong atomicLong = new AtomicLong(0); |
| 526 | + LongStream.rangeClosed(headNum - recentBlockCount + 1, headNum).forEach( |
| 527 | + blockNum -> futures.add(service.submit(() -> { |
| 528 | + try { |
| 529 | + atomicLong.incrementAndGet(); |
| 530 | + BlockCapsule blockCapsule = getBlockByNum(blockNum); |
| 531 | + blockCapsule.getTransactions().stream() |
| 532 | + .map(tc -> tc.getTransactionId().getBytes()) |
| 533 | + .map(bytes -> Maps.immutableEntry(bytes, Longs.toByteArray(blockNum))) |
| 534 | + .forEach(e -> transactionCache.put(e.getKey(), new BytesCapsule(e.getValue()))); |
| 535 | + } catch (ItemNotFoundException | BadItemException e) { |
| 536 | + logger.info("init txs cache error."); |
| 537 | + throw new IllegalStateException("init txs cache error."); |
| 538 | + } |
| 539 | + }))); |
| 540 | + ListenableFuture<?> future = Futures.allAsList(futures); |
| 541 | + try { |
| 542 | + future.get(); |
| 543 | + service.shutdown(); |
| 544 | + } catch (InterruptedException | ExecutionException e) { |
| 545 | + e.printStackTrace(); |
| 546 | + } |
| 547 | + System.out.println("trxids:" + transactionCache.size() |
| 548 | + + ", block count:" + atomicLong.get() |
| 549 | + + ", cost:" + (System.currentTimeMillis() - start) |
| 550 | + ); |
| 551 | + System.exit(0); |
| 552 | + } |
| 553 | + |
505 | 554 | public AccountStore getAccountStore() { |
506 | 555 | return this.accountStore; |
507 | 556 | } |
@@ -1060,6 +1109,8 @@ public boolean processTransaction(final TransactionCapsule trxCap, BlockCapsule |
1060 | 1109 | } |
1061 | 1110 | } |
1062 | 1111 | transactionStore.put(trxCap.getTransactionId().getBytes(), trxCap); |
| 1112 | + transactionCache.put(trxCap.getTransactionId().getBytes(), |
| 1113 | + new BytesCapsule(ByteArray.fromLong(trxCap.getBlockNum()))); |
1063 | 1114 |
|
1064 | 1115 | TransactionInfoCapsule transactionInfo = TransactionInfoCapsule |
1065 | 1116 | .buildInstance(trxCap, blockCap, trace); |
@@ -1597,7 +1648,7 @@ public synchronized void preValidateTransactionSign(BlockCapsule block) |
1597 | 1648 | } |
1598 | 1649 |
|
1599 | 1650 | public void rePush(TransactionCapsule tx) { |
1600 | | - if (transactionStore.has(tx.getTransactionId().getBytes())) { |
| 1651 | + if (transactionCache.has(tx.getTransactionId().getBytes())) { |
1601 | 1652 | return; |
1602 | 1653 | } |
1603 | 1654 |
|
|
0 commit comments