forked from tronprotocol/java-tron
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBlockLoader.java
More file actions
117 lines (96 loc) · 3.95 KB
/
BlockLoader.java
File metadata and controls
117 lines (96 loc) · 3.95 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
/*
* java-tron is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* java-tron is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.tron.manager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.spongycastle.util.encoders.Hex;
import org.springframework.beans.factory.annotation.Autowired;
import org.tron.config.SystemProperties;
import org.tron.core.TransactionUtils;
import org.tron.core.TronBlockChainImpl;
import org.tron.protos.core.TronBlock;
import org.tron.protos.core.TronTransaction;
import org.tron.utils.ExecutorPipeline;
import org.tron.utils.Functional;
import java.io.FileInputStream;
import java.util.Scanner;
public class BlockLoader {
private static final Logger logger = LoggerFactory.getLogger("BlockLoader");
@Autowired
private TronBlockChainImpl blockchain;
@Autowired
SystemProperties config;
Scanner scanner = null;
ExecutorPipeline<TronBlock.Block, TronBlock.Block> exce1;
ExecutorPipeline<TronBlock.Block, ?> exce2;
public void loadBlocks() {
exce1 = new ExecutorPipeline(8, 1000, true, new Functional
.Function<TronBlock.Block, TronBlock.Block>() {
public TronBlock.Block apply(TronBlock.Block block) {
if (block.getBlockHeader().getNumber() >= blockchain
.getBlockStore().getBestBlock().getBlockHeader()
.getNumber()) {
for (TronTransaction.Transaction tx : block
.getTransactionsList()) {
TransactionUtils.getSender(tx);
}
}
return block;
}
}, new Functional.Consumer<Throwable>() {
@Override
public void accept(Throwable throwable) {
logger.error("Unhandled exception: ", throwable);
}
});
exce2 = exce1.add(1, 1000, new Functional.Consumer<TronBlock.Block>() {
public void accept(TronBlock.Block block) {
try {
blockWork(block);
} catch (Exception e) {
e.printStackTrace();
}
}
});
String fileSrc = config.blocksLoader();
try {
final String blocksFormat = config.getConfig().hasPath("blocks" +
".format") ? config.getConfig().getString
("blocks.format") : null;
System.out.println("Loading blocks: " + fileSrc + ", format: " +
blocksFormat);
FileInputStream inputStream = new FileInputStream(fileSrc);
scanner = new Scanner(inputStream, "UTF-8");
while (scanner.hasNext()) {
byte[] blockBytes = Hex.decode(scanner.nextLine());
TronBlock.Block block = TronBlock.Block.parseFrom(blockBytes);
exce1.push(block);
}
} catch (Exception e) {
e.printStackTrace();
System.exit(1);
}
}
private void blockWork(TronBlock.Block block) {
if (block.getBlockHeader().getNumber() >= blockchain.getBlockStore()
.getBestBlock().getBlockHeader().getNumber()
|| blockchain.getBlockStore().getBlockByHash(block
.getBlockHeader().getHash().toByteArray()) == null) {
if (block.getBlockHeader().getNumber() > 0) {
throw new RuntimeException();
}
}
}
}