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
142 lines (126 loc) · 5.08 KB
/
SolidityNode.java
File metadata and controls
142 lines (126 loc) · 5.08 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
package org.tron.program;
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.NodeManager;
import org.tron.common.overlay.discover.UDPListener;
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.BadBlockException;
import org.tron.core.exception.ContractExeException;
import org.tron.core.exception.ContractValidateException;
import org.tron.core.exception.UnLinkedBlockException;
import org.tron.core.exception.ValidateBandwidthException;
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;
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 syncLoop(Args args) {
while (true) {
try {
initGrpcClient(args.getTrustNodeAddr());
syncSolidityBlock();
} catch (Exception e) {
logger.error("Error in sync solidity block {}", e.getMessage());
}
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
private void syncSolidityBlock() throws BadBlockException {
DynamicProperties remoteDynamicProperties = databaseGrpcClient.getDynamicProperties();
long remoteLastSolidityBlockNum = remoteDynamicProperties.getLastSolidityBlockNum();
while (true) {
long lastSolidityBlockNum = dbManager.getDynamicPropertiesStore()
.getLatestSolidifiedBlockNum();
if (lastSolidityBlockNum < remoteLastSolidityBlockNum) {
Block block = databaseGrpcClient.getBlock(lastSolidityBlockNum + 1);
try {
BlockCapsule blockCapsule = new BlockCapsule(block);
dbManager.pushBlock(blockCapsule);
dbManager.getDynamicPropertiesStore()
.saveLatestSolidifiedBlockNum(lastSolidityBlockNum + 1);
} catch (ValidateBandwidthException e) {
throw new BadBlockException("validate Bandwidth 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 Exectute exception");
}
} else {
break;
}
}
logger.info("Sync with trust node completed!!!");
}
private void start(Args cfgArgs) {
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);
//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
UDPListener udpListener = context.getBean(UDPListener.class);
udpListener.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();
}
}