Repository navigation
Expand file tree
/
Copy pathTronNetDelegate.java
More file actions
237 lines (211 loc) · 7.56 KB
/
Copy pathTronNetDelegate.java
File metadata and controls
237 lines (211 loc) · 7.56 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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
package org.tron.core.net;
import java.util.Collection;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;
import java.util.concurrent.ConcurrentLinkedQueue;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.tron.common.overlay.message.Message;
import org.tron.common.overlay.server.SyncPool;
import org.tron.common.utils.Sha256Hash;
import org.tron.core.capsule.BlockCapsule;
import org.tron.core.capsule.BlockCapsule.BlockId;
import org.tron.core.capsule.TransactionCapsule;
import org.tron.core.capsule.WitnessCapsule;
import org.tron.core.db.Manager;
import org.tron.core.db.WitnessStore;
import org.tron.core.exception.AccountResourceInsufficientException;
import org.tron.core.exception.BadBlockException;
import org.tron.core.exception.BadItemException;
import org.tron.core.exception.BadNumberBlockException;
import org.tron.core.exception.ContractExeException;
import org.tron.core.exception.ContractSizeNotEqualToOneException;
import org.tron.core.exception.ContractValidateException;
import org.tron.core.exception.DupTransactionException;
import org.tron.core.exception.ItemNotFoundException;
import org.tron.core.exception.NonCommonBlockException;
import org.tron.core.exception.P2pException;
import org.tron.core.exception.P2pException.TypeEnum;
import org.tron.core.exception.ReceiptCheckErrException;
import org.tron.core.exception.StoreException;
import org.tron.core.exception.TaposException;
import org.tron.core.exception.TooBigTransactionException;
import org.tron.core.exception.TooBigTransactionResultException;
import org.tron.core.exception.TransactionExpirationException;
import org.tron.core.exception.UnLinkedBlockException;
import org.tron.core.exception.VMIllegalException;
import org.tron.core.exception.ValidateScheduleException;
import org.tron.core.exception.ValidateSignatureException;
import org.tron.core.net.message.BlockMessage;
import org.tron.core.net.message.MessageTypes;
import org.tron.core.net.message.TransactionMessage;
import org.tron.core.net.peer.PeerConnection;
import org.tron.protos.Protocol.Inventory.InventoryType;
@Slf4j
@Component
public class TronNetDelegate {
@Autowired
private SyncPool syncPool;
@Autowired
private Manager dbManager;
@Autowired
private WitnessStore witnessStore;
@Getter
private Object blockLock = new Object();
private int blockIdCacheSize = 100;
private Queue<BlockId> freshBlockId = new ConcurrentLinkedQueue<BlockId>() {
@Override
public boolean offer(BlockId blockId) {
if (size() > blockIdCacheSize) {
super.poll();
}
return super.offer(blockId);
}
};
public Collection<PeerConnection> getActivePeer() {
return syncPool.getActivePeers();
}
public long getSyncBeginNumber() {
return dbManager.getSyncBeginNumber();
}
public long getBlockTime(BlockId id) throws P2pException {
try {
return dbManager.getBlockById(id).getTimeStamp();
} catch (BadItemException | ItemNotFoundException e) {
throw new P2pException(TypeEnum.DB_ITEM_NOT_FOUND, id.getString());
}
}
public BlockId getHeadBlockId() {
return dbManager.getHeadBlockId();
}
public BlockId getSolidBlockId() {
return dbManager.getSolidBlockId();
}
public BlockId getGenesisBlockId() {
return dbManager.getGenesisBlockId();
}
public BlockId getBlockIdByNum(long num) throws P2pException {
try {
return dbManager.getBlockIdByNum(num);
} catch (ItemNotFoundException e) {
throw new P2pException(TypeEnum.DB_ITEM_NOT_FOUND, "num: " + num);
}
}
public BlockCapsule getGenesisBlock() {
return dbManager.getGenesisBlock();
}
public long getHeadBlockTimeStamp() {
return dbManager.getHeadBlockTimeStamp();
}
public boolean containBlock(BlockId id) {
return dbManager.containBlock(id);
}
public boolean containBlockInMainChain(BlockId id) {
return dbManager.containBlockInMainChain(id);
}
public LinkedList<BlockId> getBlockChainHashesOnFork(BlockId forkBlockHash) throws P2pException {
try {
return dbManager.getBlockChainHashesOnFork(forkBlockHash);
} catch (NonCommonBlockException e) {
throw new P2pException(TypeEnum.HARD_FORKED, forkBlockHash.getString());
}
}
public boolean canChainRevoke(long num) {
return num >= dbManager.getSyncBeginNumber();
}
public boolean contain(Sha256Hash hash, MessageTypes type) {
if (type.equals(MessageTypes.BLOCK)) {
return dbManager.containBlock(hash);
} else if (type.equals(MessageTypes.TRX)) {
return dbManager.getTransactionStore().has(hash.getBytes());
}
return false;
}
public Message getData(Sha256Hash hash, InventoryType type) throws P2pException {
try {
switch (type) {
case BLOCK:
return new BlockMessage(dbManager.getBlockById(hash));
case TRX:
TransactionCapsule tx = dbManager.getTransactionStore().get(hash.getBytes());
if (tx != null) {
return new TransactionMessage(tx.getData());
}
throw new StoreException();
default:
throw new StoreException();
}
} catch (StoreException e) {
throw new P2pException(TypeEnum.DB_ITEM_NOT_FOUND,
"type: " + type + ", hash: " + hash.getByteString());
}
}
public void processBlock(BlockCapsule block) throws P2pException {
synchronized (blockLock) {
try {
if (!freshBlockId.contains(block.getBlockId())) {
dbManager.pushBlock(block);
freshBlockId.add(block.getBlockId());
logger.info("Success process block {}.", block.getBlockId().getString());
}
} catch (ValidateSignatureException
| ContractValidateException
| ContractExeException
| UnLinkedBlockException
| ValidateScheduleException
| AccountResourceInsufficientException
| TaposException
| TooBigTransactionException
| TooBigTransactionResultException
| DupTransactionException
| TransactionExpirationException
| BadNumberBlockException
| BadBlockException
| NonCommonBlockException
| ReceiptCheckErrException
| VMIllegalException e) {
throw new P2pException(TypeEnum.BAD_BLOCK, e);
}
}
}
public void pushTransaction(TransactionCapsule trx) throws P2pException {
try {
dbManager.pushTransaction(trx);
} catch (ContractSizeNotEqualToOneException
| VMIllegalException e) {
throw new P2pException(TypeEnum.BAD_TRX, e);
} catch (ContractValidateException
| ValidateSignatureException
| ContractExeException
| DupTransactionException
| TaposException
| TooBigTransactionException
| TransactionExpirationException
| ReceiptCheckErrException
| TooBigTransactionResultException
| AccountResourceInsufficientException e) {
throw new P2pException(TypeEnum.TRX_EXE_FAILED, e);
}
}
public boolean validBlock(BlockCapsule block) throws P2pException {
try {
if (!block.validateSignature(dbManager)) {
return false;
}
boolean flag = false;
List<WitnessCapsule> witnesses = witnessStore.getAllWitnesses();
for (WitnessCapsule witness : witnesses) {
if (witness.getAddress().equals(block.getWitnessAddress())) {
flag = true;
break;
}
}
return flag;
} catch (ValidateSignatureException e) {
throw new P2pException(TypeEnum.BAD_BLOCK, e);
}
}
}