> iterator() {
75 | return db.iterator();
76 | }
77 |
78 | @Override
79 | public void close() {
80 | ((Flusher) db).close();
81 | }
82 |
83 | @Override
84 | public void reset() {
85 | ((Flusher) db).reset();
86 | }
87 |
88 | @Override
89 | public void resetSolidity() {
90 | solidity = this;
91 | }
92 |
93 | @Override
94 | public void updateSolidity() {
95 | solidity = solidity.getNext();
96 | }
97 |
98 | @Override
99 | public String getDbName() {
100 | return db.getDbName();
101 | }
102 |
103 | @Override
104 | public Snapshot newInstance() {
105 | return new SnapshotRoot(db.newInstance());
106 | }
107 | }
108 |
--------------------------------------------------------------------------------
/src/main/java/org/tron/core/exception/ItemNotFoundException.java:
--------------------------------------------------------------------------------
1 | package org.tron.core.exception;
2 |
3 | public class ItemNotFoundException extends StoreException {
4 |
5 | public ItemNotFoundException(String message) {
6 | super(message);
7 | }
8 |
9 | public ItemNotFoundException() {
10 | super();
11 | }
12 |
13 | public ItemNotFoundException(String message, Throwable cause) {
14 | super(message, cause);
15 | }
16 | }
17 |
--------------------------------------------------------------------------------
/src/main/java/org/tron/core/exception/RevokingStoreIllegalStateException.java:
--------------------------------------------------------------------------------
1 | package org.tron.core.exception;
2 |
3 | public class RevokingStoreIllegalStateException extends RuntimeException {
4 |
5 | /**
6 | * Constructs an RevokingStoreIllegalStateException with no detail message. A detail message is a
7 | * String that describes this particular exception.
8 | */
9 | public RevokingStoreIllegalStateException() {
10 | super();
11 | }
12 |
13 | /**
14 | * Constructs an RevokingStoreIllegalStateException with the specified detail message. A detail
15 | * message is a String that describes this particular exception.
16 | *
17 | * @param s the String that contains a detailed message
18 | */
19 | public RevokingStoreIllegalStateException(String s) {
20 | super(s);
21 | }
22 |
23 | /**
24 | * Constructs a new exception with the specified detail message and cause.
25 | *
26 | * Note that the detail message associated with cause
is not automatically
27 | * incorporated in this exception's detail message.
28 | *
29 | * @param message the detail message (which is saved for later retrieval by the {@link
30 | * Throwable#getMessage()} method).
31 | * @param cause the cause (which is saved for later retrieval by the {@link Throwable#getCause()}
32 | * method). (A null value is permitted, and indicates that the cause is nonexistent or
33 | * unknown.)
34 | */
35 | public RevokingStoreIllegalStateException(String message, Throwable cause) {
36 | super(message, cause);
37 | }
38 |
39 | /**
40 | * Constructs a new exception with the specified cause and a detail message of (cause==null ?
41 | * null : cause.toString()) (which typically contains the class and detail message of
42 | * cause). This constructor is useful for exceptions that are little more than wrappers
43 | * for other throwables (for example, {@link java.security.PrivilegedActionException}).
44 | *
45 | * @param cause the cause (which is saved for later retrieval by the {@link Throwable#getCause()}
46 | * method). (A null value is permitted, and indicates that the cause is nonexistent or
47 | * unknown.)
48 | */
49 | public RevokingStoreIllegalStateException(Throwable cause) {
50 | super("", cause);
51 | }
52 |
53 | static final long serialVersionUID = -1848914673093119416L;
54 | }
55 |
--------------------------------------------------------------------------------
/src/main/java/org/tron/core/exception/StoreException.java:
--------------------------------------------------------------------------------
1 | package org.tron.core.exception;
2 |
3 | public class StoreException extends Exception {
4 |
5 | public StoreException() {
6 | super();
7 | }
8 |
9 | public StoreException(String message) {
10 | super(message);
11 | }
12 |
13 | public StoreException(String message, Throwable cause) {
14 | super(message, cause);
15 | }
16 |
17 | public StoreException(Throwable cause) {
18 | super(cause);
19 | }
20 | }
21 |
--------------------------------------------------------------------------------
/src/main/java/org/tron/utils/ByteUtil.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) [2016] [ ]
3 | * This file is part of the ethereumJ library.
4 | *
5 | * The ethereumJ library is free software: you can redistribute it and/or modify
6 | * it under the terms of the GNU Lesser General Public License as published by
7 | * the Free Software Foundation, either version 3 of the License, or
8 | * (at your option) any later version.
9 | *
10 | * The ethereumJ library is distributed in the hope that it will be useful,
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 | * GNU Lesser General Public License for more details.
14 | *
15 | * You should have received a copy of the GNU Lesser General Public License
16 | * along with the ethereumJ library. If not, see .
17 | */
18 |
19 | package org.tron.utils;
20 |
21 | import com.google.common.base.Preconditions;
22 | import com.google.common.primitives.UnsignedBytes;
23 | import java.math.BigInteger;
24 | import java.nio.ByteBuffer;
25 | import java.util.Arrays;
26 |
27 | public class ByteUtil {
28 |
29 | public static final byte[] EMPTY_BYTE_ARRAY = new byte[0];
30 | public static final byte[] ZERO_BYTE_ARRAY = new byte[]{0};
31 |
32 |
33 | /**
34 | * return a cloned byte array.
35 | * return null if parameter data is null
36 | * @param data
37 | * @return
38 | */
39 | public static byte[] cloneBytes(byte[] data){
40 | if (data == null){
41 | return null;
42 | }
43 |
44 | int length = data.length;
45 | byte[] rc = new byte[length];
46 | if (length > 0){
47 | System.arraycopy(data, 0, rc, 0, length);
48 | }
49 | return rc;
50 | }
51 |
52 | /**
53 | * The regular {@link BigInteger#toByteArray()} method isn't quite what we often need:
54 | * it appends a leading zero to indicate that the number is positive and may need padding.
55 | *
56 | * @param b the integer to format into a byte array
57 | * @param numBytes the desired size of the resulting byte array
58 | * @return numBytes byte long array.
59 | */
60 | public static byte[] bigIntegerToBytes(BigInteger b, int numBytes) {
61 | if (b == null) {
62 | return null;
63 | }
64 | byte[] bytes = new byte[numBytes];
65 | byte[] biBytes = b.toByteArray();
66 | int start = (biBytes.length == numBytes + 1) ? 1 : 0;
67 | int length = Math.min(biBytes.length, numBytes);
68 | System.arraycopy(biBytes, start, bytes, numBytes - length, length);
69 | return bytes;
70 | }
71 |
72 | public static byte[] bigIntegerToBytes(BigInteger value) {
73 | if (value == null) {
74 | return null;
75 | }
76 |
77 | byte[] data = value.toByteArray();
78 |
79 | if (data.length != 1 && data[0] == 0) {
80 | byte[] tmp = new byte[data.length - 1];
81 | System.arraycopy(data, 1, tmp, 0, tmp.length);
82 | data = tmp;
83 | }
84 | return data;
85 | }
86 |
87 | /**
88 | * merge arrays.
89 | *
90 | * @param arrays - arrays to merge
91 | * @return - merged array
92 | */
93 | public static byte[] merge(byte[]... arrays) {
94 | int count = 0;
95 | for (byte[] array : arrays) {
96 | count += array.length;
97 | }
98 |
99 | // Create new array and copy all array contents
100 | byte[] mergedArray = new byte[count];
101 | int start = 0;
102 | for (byte[] array : arrays) {
103 | System.arraycopy(array, 0, mergedArray, start, array.length);
104 | start += array.length;
105 | }
106 | return mergedArray;
107 | }
108 |
109 | /**
110 | * Creates a copy of bytes and appends b to the end of it.
111 | */
112 | public static byte[] appendByte(byte[] bytes, byte b) {
113 | byte[] result = Arrays.copyOf(bytes, bytes.length + 1);
114 | result[result.length - 1] = b;
115 | return result;
116 | }
117 |
118 | /**
119 | * Turn nibbles to a pretty looking output string Example. [ 1, 2, 3, 4, 5 ] becomes
120 | * '\x11\x23\x45'
121 | *
122 | * @param nibbles - getting byte of data [ 04 ] and turning it to a '\x04' representation
123 | * @return pretty string of nibbles
124 | */
125 | public static String nibblesToPrettyString(byte[] nibbles) {
126 | StringBuilder builder = new StringBuilder();
127 | for (byte nibble : nibbles) {
128 | final String nibbleString = oneByteToHexString(nibble);
129 | builder.append("\\x").append(nibbleString);
130 | }
131 | return builder.toString();
132 | }
133 |
134 | /**
135 | * get hex string data from byte data.
136 | */
137 | public static String oneByteToHexString(byte value) {
138 | String retVal = Integer.toString(value & 0xFF, 16);
139 | if (retVal.length() == 1) {
140 | retVal = "0" + retVal;
141 | }
142 | return retVal;
143 | }
144 |
145 | /**
146 | * Cast hex encoded value from byte[] to int Limited to Integer.MAX_VALUE: 2^32-1 (4 bytes)
147 | *
148 | * @param b array contains the values
149 | * @return unsigned positive int value.
150 | */
151 | public static int byteArrayToInt(byte[] b) {
152 | if (b == null || b.length == 0) {
153 | return 0;
154 | }
155 | return new BigInteger(1, b).intValue();
156 | }
157 |
158 | public static boolean isSingleZero(byte[] array) {
159 | return (array.length == 1 && array[0] == 0);
160 | }
161 |
162 | /**
163 | * Converts a int value into a byte array.
164 | *
165 | * @param val - int value to convert
166 | * @return value with leading byte that are zeroes striped
167 | */
168 | public static byte[] intToBytesNoLeadZeroes(int val) {
169 |
170 | if (val == 0) {
171 | return EMPTY_BYTE_ARRAY;
172 | }
173 |
174 | int lenght = 0;
175 |
176 | int tmpVal = val;
177 | while (tmpVal != 0) {
178 | tmpVal = tmpVal >>> 8;
179 | ++lenght;
180 | }
181 |
182 | byte[] result = new byte[lenght];
183 |
184 | int index = result.length - 1;
185 | while (val != 0) {
186 |
187 | result[index] = (byte) (val & 0xFF);
188 | val = val >>> 8;
189 | index -= 1;
190 | }
191 |
192 | return result;
193 | }
194 |
195 | /**
196 | * Converts int value into a byte array.
197 | *
198 | * @param val - int value to convert
199 | * @return byte[]
of length 4, representing the int value
200 | */
201 | public static byte[] intToBytes(int val) {
202 | return ByteBuffer.allocate(4).putInt(val).array();
203 | }
204 |
205 | /**
206 | * Cast hex encoded value from byte[] to BigInteger null is parsed like byte[0]
207 | *
208 | * @param bb byte array contains the values
209 | * @return unsigned positive BigInteger value.
210 | */
211 | public static BigInteger bytesToBigInteger(byte[] bb) {
212 | return (bb == null || bb.length == 0) ? BigInteger.ZERO : new BigInteger(1, bb);
213 | }
214 |
215 | /**
216 | * Cast hex encoded value from byte[] to long null is parsed like byte[0]
217 | *
218 | * Limited to Long.MAX_VALUE: 263-1 (8 bytes)
219 | *
220 | * @param b array contains the values
221 | * @return unsigned positive long value.
222 | */
223 | public static long byteArrayToLong(byte[] b) {
224 | if (b == null || b.length == 0) {
225 | return 0;
226 | }
227 | return new BigInteger(1, b).longValueExact();
228 | }
229 |
230 | public static int firstNonZeroByte(byte[] data) {
231 | for (int i = 0; i < data.length; ++i) {
232 | if (data[i] != 0) {
233 | return i;
234 | }
235 | }
236 | return -1;
237 | }
238 |
239 | public static byte[] stripLeadingZeroes(byte[] data) {
240 |
241 | if (data == null) {
242 | return null;
243 | }
244 |
245 | final int firstNonZero = firstNonZeroByte(data);
246 | switch (firstNonZero) {
247 | case -1:
248 | return ZERO_BYTE_ARRAY;
249 |
250 | case 0:
251 | return data;
252 |
253 | default:
254 | byte[] result = new byte[data.length - firstNonZero];
255 | System.arraycopy(data, firstNonZero, result, 0, data.length - firstNonZero);
256 |
257 | return result;
258 | }
259 | }
260 |
261 | /**
262 | * Utility function to copy a byte array into a new byte array with given size. If the src length
263 | * is smaller than the given size, the result will be left-padded with zeros.
264 | *
265 | * @param value - a BigInteger with a maximum value of 2^256-1
266 | * @return Byte array of given size with a copy of the src
267 | */
268 | public static byte[] copyToArray(BigInteger value) {
269 | byte[] dest = ByteBuffer.allocate(32).array();
270 | byte[] src = ByteUtil.bigIntegerToBytes(value);
271 | if (src != null) {
272 | System.arraycopy(src, 0, dest, dest.length - src.length, src.length);
273 | }
274 | return dest;
275 | }
276 |
277 | /**
278 | * Returns a number of zero bits preceding the highest-order ("leftmost") one-bit interpreting
279 | * input array as a big-endian integer value
280 | */
281 | public static int numberOfLeadingZeros(byte[] bytes) {
282 |
283 | int i = firstNonZeroByte(bytes);
284 |
285 | if (i == -1) {
286 | return bytes.length * 8;
287 | } else {
288 | int byteLeadingZeros = Integer.numberOfLeadingZeros((int) bytes[i] & 0xff) - 24;
289 | return i * 8 + byteLeadingZeros;
290 | }
291 | }
292 |
293 | /**
294 | * Parses fixed number of bytes starting from {@code offset} in {@code input} array. If {@code
295 | * input} has not enough bytes return array will be right padded with zero bytes. I.e. if {@code
296 | * offset} is higher than {@code input.length} then zero byte array of length {@code len} will be
297 | * returned
298 | */
299 | public static byte[] parseBytes(byte[] input, int offset, int len) {
300 |
301 | if (offset >= input.length || len == 0) {
302 | return EMPTY_BYTE_ARRAY;
303 | }
304 |
305 | byte[] bytes = new byte[len];
306 | System.arraycopy(input, offset, bytes, 0, Math.min(input.length - offset, len));
307 | return bytes;
308 | }
309 |
310 | /**
311 | * Parses 32-bytes word from given input. Uses {@link #parseBytes(byte[], int, int)} method, thus,
312 | * result will be right-padded with zero bytes if there is not enough bytes in {@code input}
313 | *
314 | * @param idx an index of the word starting from {@code 0}
315 | */
316 | public static byte[] parseWord(byte[] input, int idx) {
317 | return parseBytes(input, 32 * idx, 32);
318 | }
319 |
320 | /**
321 | * Parses 32-bytes word from given input. Uses {@link #parseBytes(byte[], int, int)} method, thus,
322 | * result will be right-padded with zero bytes if there is not enough bytes in {@code input}
323 | *
324 | * @param idx an index of the word starting from {@code 0}
325 | * @param offset an offset in {@code input} array to start parsing from
326 | */
327 | public static byte[] parseWord(byte[] input, int offset, int idx) {
328 | return parseBytes(input, offset + 32 * idx, 32);
329 | }
330 |
331 | public static boolean greater(byte[] bytes1, byte[] bytes2) {
332 | return compare(bytes1, bytes2) > 0;
333 | }
334 |
335 | public static boolean greaterOrEquals(byte[] bytes1, byte[] bytes2) {
336 | return compare(bytes1, bytes2) >= 0;
337 | }
338 |
339 | public static boolean less(byte[] bytes1, byte[] bytes2) {
340 | return compare(bytes1, bytes2) < 0;
341 | }
342 |
343 | public static boolean lessOrEquals(byte[] bytes1, byte[] bytes2) {
344 | return compare(bytes1, bytes2) <= 0;
345 | }
346 |
347 | public static boolean equals(byte[] bytes1, byte[] bytes2) {
348 | return compare(bytes1, bytes2) == 0;
349 | }
350 |
351 | // lexicographical order
352 | public static int compare(byte[] bytes1, byte[] bytes2) {
353 | Preconditions.checkNotNull(bytes1);
354 | Preconditions.checkNotNull(bytes2);
355 | Preconditions.checkArgument(bytes1.length == bytes2.length);
356 | int length = bytes1.length;
357 | for (int i = 0; i < length; ++i) {
358 | int ret = UnsignedBytes.compare(bytes1[i], bytes2[i]);
359 | if (ret != 0) {
360 | return ret;
361 | }
362 | }
363 |
364 | return 0;
365 | }
366 |
367 | }
--------------------------------------------------------------------------------
/src/main/java/org/tron/utils/FileUtil.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) [2016] [ ] This file is part of the ethereumJ library.
3 | *
4 | * The ethereumJ library is free software: you can redistribute it and/or modify it under the terms
5 | * of the GNU Lesser General Public License as published by the Free Software Foundation, either
6 | * version 3 of the License, or (at your option) any later version.
7 | *
8 | * The ethereumJ library is distributed in the hope that it will be useful, but WITHOUT ANY
9 | * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
10 | * PURPOSE. See the GNU Lesser General Public License for more details.
11 | *
12 | * You should have received a copy of the GNU Lesser General Public License along with the ethereumJ
13 | * library. If not, see .
14 | */
15 |
16 | package org.tron.utils;
17 |
18 | import java.io.BufferedReader;
19 | import java.io.BufferedWriter;
20 | import java.io.File;
21 | import java.io.FileReader;
22 | import java.io.FileWriter;
23 | import java.io.IOException;
24 | import java.nio.file.FileVisitResult;
25 | import java.nio.file.FileVisitor;
26 | import java.nio.file.Files;
27 | import java.nio.file.Path;
28 | import java.nio.file.Paths;
29 | import java.nio.file.attribute.BasicFileAttributes;
30 | import java.util.ArrayList;
31 | import java.util.Arrays;
32 | import java.util.List;
33 | import java.util.Objects;
34 | import lombok.extern.slf4j.Slf4j;
35 |
36 | @Slf4j(topic = "utils")
37 | public class FileUtil {
38 |
39 | public static List recursiveList(String path) throws IOException {
40 |
41 | final List files = new ArrayList<>();
42 |
43 | Files.walkFileTree(Paths.get(path), new FileVisitor() {
44 | @Override
45 | public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) {
46 | return FileVisitResult.CONTINUE;
47 | }
48 |
49 | @Override
50 | public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) {
51 | files.add(file.toString());
52 | return FileVisitResult.CONTINUE;
53 | }
54 |
55 | @Override
56 | public FileVisitResult visitFileFailed(Path file, IOException exc) {
57 | return FileVisitResult.CONTINUE;
58 | }
59 |
60 | @Override
61 | public FileVisitResult postVisitDirectory(Path dir, IOException exc) {
62 | return FileVisitResult.CONTINUE;
63 | }
64 | });
65 |
66 | return files;
67 | }
68 |
69 | public static boolean recursiveDelete(String fileName) {
70 | File file = new File(fileName);
71 | if (file.exists()) {
72 | // check if the file is a directory
73 | if (file.isDirectory()) {
74 | // call deletion of file individually
75 | Arrays.stream(Objects.requireNonNull(file.list()))
76 | .map(s -> fileName + System.getProperty("file.separator") + s)
77 | .forEachOrdered(FileUtil::recursiveDelete);
78 | }
79 |
80 | file.setWritable(true);
81 | return file.delete();
82 | }
83 | return false;
84 | }
85 |
86 | public static void saveData(String filePath, String data, boolean append) {
87 | File priFile = new File(filePath);
88 | try {
89 | priFile.createNewFile();
90 | try (BufferedWriter bw = new BufferedWriter(new FileWriter(priFile, append))) {
91 | bw.write(data);
92 | bw.flush();
93 | }
94 | } catch (IOException e) {
95 | logger.debug(e.getMessage(), e);
96 | }
97 | }
98 |
99 | public static int readData(String filePath, char[] buf) {
100 | int len;
101 | File file = new File(filePath);
102 | try (BufferedReader bufRead = new BufferedReader(new FileReader(file))) {
103 | len = bufRead.read(buf, 0, buf.length);
104 | } catch (IOException ex) {
105 | ex.printStackTrace();
106 | return 0;
107 | }
108 | return len;
109 | }
110 |
111 | /**
112 | * delete directory.
113 | */
114 | public static boolean deleteDir(File dir) {
115 | if (dir.isDirectory()) {
116 | String[] children = dir.list();
117 | for (int i = 0; i < children.length; i++) {
118 | boolean success = deleteDir(new File(dir, children[i]));
119 | if (!success) {
120 | logger.warn("can't delete dir:" + dir);
121 | return false;
122 | }
123 | }
124 | }
125 | return dir.delete();
126 | }
127 | }
128 |
--------------------------------------------------------------------------------