forked from tronprotocol/java-tron
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathManager.java
More file actions
243 lines (186 loc) · 6.53 KB
/
Manager.java
File metadata and controls
243 lines (186 loc) · 6.53 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
238
239
240
241
242
243
package org.tron.core.db;
import static org.tron.common.crypto.Hash.sha3;
import com.carrotsearch.sizeof.RamUsageEstimator;
import com.google.protobuf.ByteString;
import java.util.ArrayList;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.tron.core.capsule.TransactionCapsule;
import org.tron.core.capsule.WitnessCapsule;
import org.tron.protos.Protocal;
import org.tron.protos.Protocal.Block;
import org.tron.protos.Protocal.Block.Builder;
import org.tron.protos.Protocal.BlockHeader;
import org.tron.protos.Protocal.Transaction;
public class Manager {
private static final Logger logger = LoggerFactory.getLogger("Manager");
private static final long BLOCK_INTERVAL_SEC = 1;
private static final long TRXS_SIZE = 2_000_000; // < 2MiB
private AccountStore accountStore;
private TransactionStore transactionStore;
private BlockStore blockStore;
private UtxoStore utxoStore;
private WitnessStore witnessStore;
private DynamicPropertiesStore dynamicPropertiesStore;
public WitnessStore getWitnessStore() {
return witnessStore;
}
private void setWitnessStore(WitnessStore witnessStore) {
this.witnessStore = witnessStore;
}
public DynamicPropertiesStore getDynamicPropertiesStore() {
return dynamicPropertiesStore;
}
public void setDynamicPropertiesStore(DynamicPropertiesStore dynamicPropertiesStore) {
this.dynamicPropertiesStore = dynamicPropertiesStore;
}
public List<Transaction> getPendingTrxs() {
return pendingTrxs;
}
// transaction cache
private List<Transaction> pendingTrxs;
// witness
/**
* get witnessCapsule List.
*/
public List<WitnessCapsule> getWitnesses() {
List<WitnessCapsule> wits = new ArrayList<WitnessCapsule>();
wits.add(new WitnessCapsule(ByteString.copyFromUtf8("0x11")));
wits.add(new WitnessCapsule(ByteString.copyFromUtf8("0x12")));
wits.add(new WitnessCapsule(ByteString.copyFromUtf8("0x13")));
wits.add(new WitnessCapsule(ByteString.copyFromUtf8("0x14")));
return wits;
}
public List<WitnessCapsule> getCurrentShuffledWitnesses() {
return getWitnesses();
}
/**
* get ScheduledWitness by slot.
*/
public ByteString getScheduledWitness(long slot) {
long currentSlot = blockStore.currentASlot() + slot;
if (currentSlot < 0) {
throw new RuntimeException("currentSlot should be positive.");
}
List<WitnessCapsule> currentShuffledWitnesses = getShuffledWitnesses();
if (currentShuffledWitnesses == null || currentShuffledWitnesses.size() == 0) {
throw new RuntimeException("ShuffledWitnesses is null.");
}
int witnessIndex = (int) currentSlot % currentShuffledWitnesses.size();
ByteString scheduledWitness = currentShuffledWitnesses.get(witnessIndex).getAddress();
logger.info("scheduled_witness:" + scheduledWitness.toStringUtf8() + ",slot:" + currentSlot);
return scheduledWitness;
}
public List<WitnessCapsule> getShuffledWitnesses() {
return getWitnesses();
}
/**
* all db should be init here.
*/
public void init() {
setAccountStore(AccountStore.create("account"));
setTransactionStore(TransactionStore.create("trans"));
setBlockStore(BlockStore.create("block"));
setUtxoStore(UtxoStore.create("utxo"));
setWitnessStore(WitnessStore.create("witness"));
setDynamicPropertiesStore(DynamicPropertiesStore.create("properties"));
pendingTrxs = new ArrayList<>();
}
public AccountStore getAccountStore() {
return accountStore;
}
public void pushTrx(Transaction trx) {
this.pendingTrxs.add(trx);
}
/**
* Process transaction.
*/
public boolean processTrx(TransactionCapsule trxCap) {
if (!trxCap.validate()) {
return false;
}
Transaction trx = trxCap.getTransaction();
switch (trx.getType()) {
case Transfer:
break;
case VoteWitess:
break;
case CreateAccount:
break;
case DeployContract:
break;
default:
break;
}
return true;
}
/**
* Generate a block.
*/
public Protocal.Block generateBlock(WitnessCapsule witnessCapsule,
long when) {
final long timestamp = this.dynamicPropertiesStore.getLatestBlockHeaderTimestamp();
final long number = this.dynamicPropertiesStore.getLatestBlockHeaderNumber();
final ByteString hash = this.dynamicPropertiesStore.getLatestBlockHeaderHash();
// judge create block time
if (when < timestamp) {
throw new IllegalArgumentException("generate block timestamp is invalid.");
}
long currentTrxSize = 0;
long postponedTrxCount = 0;
Builder blockBuilder = Block.newBuilder();
for (Transaction trx : pendingTrxs) {
currentTrxSize += RamUsageEstimator.sizeOf(trx);
// judge block size
if (currentTrxSize > TRXS_SIZE) {
postponedTrxCount++;
continue;
}
// apply transaction
if (processTrx(new TransactionCapsule(trx))) {
// push into block
blockBuilder.addTransactions(trx);
pendingTrxs.remove(trx);
}
}
if (postponedTrxCount > 0) {
logger.info("postponed {} transactions due to block size limit", postponedTrxCount);
}
// generate block
BlockHeader.Builder blockHeaderBuilder = BlockHeader.newBuilder()
.setNumber(number + 1)
.setParentHash(hash)
.setTimestamp(when)
.setWitnessAddress(witnessCapsule.getAddress());
blockBuilder.setBlockHeader(blockHeaderBuilder.build());
blockHeaderBuilder.setHash(ByteString.copyFrom(sha3(blockBuilder.build().toByteArray())));
blockBuilder.setBlockHeader(blockHeaderBuilder.build());
Block block = blockBuilder.build();
dynamicPropertiesStore.saveLatestBlockHeaderHash(block.getBlockHeader().getHash());
dynamicPropertiesStore.saveLatestBlockHeaderNumber(block.getBlockHeader().getNumber());
dynamicPropertiesStore.saveLatestBlockHeaderTimestamp(block.getBlockHeader().getTimestamp());
return block;
}
private void setAccountStore(AccountStore accountStore) {
this.accountStore = accountStore;
}
public TransactionStore getTransactionStore() {
return transactionStore;
}
private void setTransactionStore(TransactionStore transactionStore) {
this.transactionStore = transactionStore;
}
public BlockStore getBlockStore() {
return blockStore;
}
private void setBlockStore(BlockStore blockStore) {
this.blockStore = blockStore;
}
public UtxoStore getUtxoStore() {
return utxoStore;
}
private void setUtxoStore(UtxoStore utxoStore) {
this.utxoStore = utxoStore;
}
}