forked from tronprotocol/java-tron
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBlockUtils.java
More file actions
228 lines (189 loc) · 6.94 KB
/
BlockUtils.java
File metadata and controls
228 lines (189 loc) · 6.94 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
/*
* 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.core;
import com.google.protobuf.ByteString;
import com.google.protobuf.InvalidProtocolBufferException;
import org.spongycastle.util.Arrays;
import org.spongycastle.util.BigIntegers;
import org.tron.peer.Validator;
import org.tron.protos.core.TronBlock;
import org.tron.protos.core.TronBlock.Block;
import org.tron.protos.core.TronBlockHeader.BlockHeader;
import org.tron.protos.core.TronTransaction.Transaction;
import org.tron.utils.ByteArray;
import java.math.BigInteger;
import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.List;
import static org.tron.core.Constant.LAST_HASH;
import static org.tron.crypto.Hash.sha3;
public class BlockUtils {
private static Block.Builder block;
private byte[] serializEncode;
/**
* get a new block
*
* @return {@link Block} block
*/
public static Block newBlock(List<Transaction> transactions, ByteString
parentHash, ByteString difficulty, long number) {
Block.Builder block = Block.newBuilder();
for (int i = 0; transactions != null && i < transactions.size(); i++) {
Transaction transaction = transactions.get(i);
block.addTransactions(i, transaction);
}
//Chain programming
// BlockHeader.Builder builder = BlockHeader.newBuilder()
// .setParentHash(parentHash).setDifficulty(difficulty)
// .setNumber(number).setTimestamp(System.currentTimeMillis());
BlockHeader.Builder blockHeaderBuilder = BlockHeader.newBuilder();
blockHeaderBuilder.setParentHash(parentHash);
blockHeaderBuilder.setDifficulty(difficulty);
blockHeaderBuilder.setNumber(number);
blockHeaderBuilder.setTimestamp(System.currentTimeMillis());
block.setBlockHeader(blockHeaderBuilder.build());
blockHeaderBuilder.setHash(ByteString.copyFrom(sha3(prepareData(block
.build()))));
block.setBlockHeader(blockHeaderBuilder.build());
return block.build();
}
/**
* new genesis block
*
* @return {@link Block} block
*/
public static Block newGenesisBlock(Transaction coinbase) {
Block.Builder genesisBlock = Block.newBuilder();
genesisBlock.addTransactions(coinbase);
BlockHeader.Builder builder = BlockHeader.newBuilder();
builder.setDifficulty(ByteString.copyFrom(ByteArray.fromHexString
("2001")));
genesisBlock.setBlockHeader(builder.build());
builder.setHash(ByteString.copyFrom(sha3(prepareData
(genesisBlock.build()))));
genesisBlock.setBlockHeader(builder.build());
return genesisBlock.build();
}
/**
* get prepare data of the block
*
* @param block {@link Block} block
* @return byte[] data
*/
public static byte[] prepareData(Block block) {
Block.Builder tmp = block.toBuilder();
BlockHeader.Builder blockHeader = tmp.getBlockHeaderBuilder();
blockHeader.clearHash();
blockHeader.clearIsPoW();
blockHeader.clearNonce();
tmp.setBlockHeader(blockHeader.build());
return tmp.build().toByteArray();
}
/**
* the proof block
*
* @param block {@link Block} block
* @return boolean is it the proof block
*/
public static boolean isValidate(Block block) {
return Validator.validate(block);
}
/**
* get print string of the block
*
* @param block {@link Block} block
* @return String format string of the block
*/
public static String toPrintString(Block block) {
if (block == null) {
return "";
}
DateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
return "\nBlock {\n" +
"\ttimestamp=" + sdf.format(new Timestamp(block
.getBlockHeader().getTimestamp
())) +
", \n\tparentHash=" + ByteArray.toHexString(block
.getBlockHeader()
.getParentHash().toByteArray()) +
", \n\thash=" + ByteArray.toHexString(block.getBlockHeader()
.getHash()
.toByteArray()) +
", \n\tnonce=" + ByteArray.toHexString(block.getBlockHeader()
.getNonce()
.toByteArray()) +
", \n\tdifficulty=" + ByteArray.toHexString(block
.getBlockHeader()
.getDifficulty().toByteArray()) +
", \n\tnumber=" + block.getBlockHeader().getNumber() +
"\n}\n";
}
/**
* get mine value
*
* @param block {@link Block} block
* @return byte[] mine value
*/
public static byte[] getMineValue(Block block) {
byte[] concat = Arrays.concatenate(prepareData(block), block
.getBlockHeader().getNonce
().toByteArray());
return sha3(concat);
}
/**
* get Verified boundary
*
* @param block {@link Block} block
* @return byte[] boundary
*/
public static byte[] getPowBoundary(Block block) {
return BigIntegers.asUnsignedByteArray(32, BigInteger.ONE.shiftLeft
(256).divide(new BigInteger(1, block.getBlockHeader()
.getDifficulty()
.toByteArray())));
}
/**
* get increase number + 1
*
* @return long number
*/
public static long getIncreaseNumber(Blockchain blockchain) {
byte[] lastHash = blockchain.getBlockDB().get(LAST_HASH);
if (lastHash == null) {
return 0;
}
byte[] value = blockchain.getBlockDB().get(lastHash);
if (value == null) {
return 0;
}
long number = 0;
try {
Block bpRead = Block.parseFrom(value).toBuilder().build();
number = bpRead.getBlockHeader().getNumber();
number += 1;
} catch (InvalidProtocolBufferException e) {
e.printStackTrace();
}
return number;
}
// Whether the hash of the judge block is equal to the hash of the parent
// block
public static boolean isParentOf(TronBlock.Block block1, TronBlock.Block
block2) {
return (block1.getBlockHeader().getParentHash() == block2.getBlockHeader
().getHash());
}
}