forked from tronprotocol/java-tron
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolidityNode.java
More file actions
182 lines (163 loc) · 6.9 KB
/
SolidityNode.java
File metadata and controls
182 lines (163 loc) · 6.9 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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
package org.tron.program;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.util.StringUtils;
import org.tron.common.application.Application;
import org.tron.common.application.ApplicationFactory;
import org.tron.common.overlay.client.DatabaseGrpcClient;
import org.tron.common.overlay.discover.DiscoverServer;
import org.tron.common.overlay.discover.node.NodeManager;
import org.tron.common.overlay.server.ChannelManager;
import org.tron.core.Constant;
import org.tron.core.capsule.BlockCapsule;
import org.tron.core.config.DefaultConfig;
import org.tron.core.config.args.Args;
import org.tron.core.db.Manager;
import org.tron.core.exception.AccountResourceInsufficientException;
import org.tron.core.exception.BadBlockException;
import org.tron.core.exception.BadNumberBlockException;
import org.tron.core.exception.ContractExeException;
import org.tron.core.exception.ContractValidateException;
import org.tron.core.exception.DupTransactionException;
import org.tron.core.exception.TaposException;
import org.tron.core.exception.TooBigTransactionException;
import org.tron.core.exception.TransactionExpirationException;
import org.tron.core.exception.UnLinkedBlockException;
import org.tron.core.exception.ValidateScheduleException;
import org.tron.core.exception.ValidateSignatureException;
import org.tron.core.services.RpcApiService;
import org.tron.protos.Protocol.Block;
import org.tron.protos.Protocol.DynamicProperties;
@Slf4j
public class SolidityNode {
private DatabaseGrpcClient databaseGrpcClient;
private Manager dbManager;
private ScheduledExecutorService syncExecutor = Executors.newSingleThreadScheduledExecutor();
public void setDbManager(Manager dbManager) {
this.dbManager = dbManager;
}
private void initGrpcClient(String addr) {
try {
databaseGrpcClient = new DatabaseGrpcClient(addr);
} catch (Exception e) {
logger.error("Failed to create database grpc client {}", addr);
System.exit(0);
}
}
private void shutdownGrpcClient() {
if (databaseGrpcClient != null) {
databaseGrpcClient.shutdown();
}
}
private void syncLoop(Args args) {
// while (true) {
// try {
// initGrpcClient(args.getTrustNodeAddr());
// syncSolidityBlock();
// shutdownGrpcClient();
// } catch (Exception e) {
// logger.error("Error in sync solidity block " + e.getMessage(), e);
// }
// try {
// Thread.sleep(5000);
// } catch (InterruptedException e) {
// Thread.currentThread().interrupt();
// e.printStackTrace();
// }
// }
}
private void syncSolidityBlock() throws BadBlockException {
DynamicProperties remoteDynamicProperties = databaseGrpcClient.getDynamicProperties();
long remoteLastSolidityBlockNum = remoteDynamicProperties.getLastSolidityBlockNum();
while (true) {
long lastSolidityBlockNum = dbManager.getDynamicPropertiesStore()
.getLatestSolidifiedBlockNum();
logger.info("sync solidity block, lastSolidityBlockNum:{}, remoteLastSolidityBlockNum:{}", lastSolidityBlockNum, remoteLastSolidityBlockNum);
if (lastSolidityBlockNum < remoteLastSolidityBlockNum) {
Block block = databaseGrpcClient.getBlock(lastSolidityBlockNum + 1);
try {
BlockCapsule blockCapsule = new BlockCapsule(block);
dbManager.pushBlock(blockCapsule);
dbManager.getDynamicPropertiesStore()
.saveLatestSolidifiedBlockNum(lastSolidityBlockNum + 1);
} catch (AccountResourceInsufficientException e) {
throw new BadBlockException("validate AccountResource exception");
} catch (ValidateScheduleException e) {
throw new BadBlockException("validate schedule exception");
} catch (ValidateSignatureException e) {
throw new BadBlockException("validate signature exception");
} catch (ContractValidateException e) {
throw new BadBlockException("ContractValidate exception");
} catch (ContractExeException | UnLinkedBlockException e) {
throw new BadBlockException("Contract Execute exception");
} catch (TaposException e) {
throw new BadBlockException("tapos exception");
} catch (DupTransactionException e) {
throw new BadBlockException("dup exception");
} catch (TooBigTransactionException e) {
throw new BadBlockException("too big exception");
} catch (TransactionExpirationException e) {
throw new BadBlockException("expiration exception");
} catch (BadNumberBlockException e) {
throw new BadBlockException("bad number exception");
}
} else {
break;
}
}
logger.info("Sync with trust node completed!!!");
}
private void start(Args cfgArgs) {
syncExecutor.scheduleWithFixedDelay(() -> {
try {
initGrpcClient(cfgArgs.getTrustNodeAddr());
syncSolidityBlock();
shutdownGrpcClient();
} catch (Throwable t) {
logger.error("Error in sync solidity block " + t.getMessage(), t);
}
}, 5000, 5000, TimeUnit.MILLISECONDS);
//new Thread(() -> syncLoop(cfgArgs), logger.getName()).start();
}
/**
* Start the SolidityNode.
*/
public static void main(String[] args) throws InterruptedException {
logger.info("Solidity node running.");
Args.setParam(args, Constant.TESTNET_CONF);
Args cfgArgs = Args.getInstance();
if (StringUtils.isEmpty(cfgArgs.getTrustNodeAddr())) {
logger.error("Trust node not set.");
return;
}
cfgArgs.setSolidityNode(true);
ApplicationContext context = new AnnotationConfigApplicationContext(DefaultConfig.class);
if (cfgArgs.isHelp()) {
logger.info("Here is the help message.");
return;
}
Application appT = ApplicationFactory.create(context);
FullNode.shutdown(appT);
//appT.init(cfgArgs);
RpcApiService rpcApiService = context.getBean(RpcApiService.class);
appT.addService(rpcApiService);
appT.initServices(cfgArgs);
appT.startServices();
// appT.startup();
//Disable peer discovery for solidity node
DiscoverServer discoverServer = context.getBean(DiscoverServer.class);
discoverServer.close();
ChannelManager channelManager = context.getBean(ChannelManager.class);
channelManager.close();
NodeManager nodeManager = context.getBean(NodeManager.class);
nodeManager.close();
SolidityNode node = new SolidityNode();
node.setDbManager(appT.getDbManager());
node.start(cfgArgs);
rpcApiService.blockUntilShutdown();
}
}