Skip to content

Commit c6aa01d

Browse files
committed
base on GreatVoyage-v4.1.1
1 parent 53ea878 commit c6aa01d

File tree

19 files changed

+627
-22
lines changed

19 files changed

+627
-22
lines changed

build.gradle

+6-7
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ subprojects {
2121
maven { url 'https://jitpack.io' }
2222
}
2323
dependencies {
24-
classpath 'com.google.protobuf:protobuf-gradle-plugin:0.8.3'
25-
classpath 'com.github.jengelman.gradle.plugins:shadow:2.0.2'
24+
classpath 'com.google.protobuf:protobuf-gradle-plugin:0.8.12'
25+
classpath 'com.github.jengelman.gradle.plugins:shadow:5.2.0'
2626
}
2727
}
2828

@@ -38,7 +38,10 @@ subprojects {
3838
compile group: 'org.slf4j', name: 'jcl-over-slf4j', version: '1.7.25'
3939
compile "org.slf4j:jcl-over-slf4j:1.7.25"
4040
compile group: 'ch.qos.logback', name: 'logback-classic', version: '1.2.3'
41-
compile group: 'org.projectlombok', name: 'lombok', version: '1.18.2'
41+
compileOnly 'org.projectlombok:lombok:1.18.12'
42+
annotationProcessor 'org.projectlombok:lombok:1.18.12'
43+
testCompileOnly 'org.projectlombok:lombok:1.18.12'
44+
testAnnotationProcessor 'org.projectlombok:lombok:1.18.12'
4245
compile group: 'com.google.guava', name: 'guava', version: '24.1-jre'
4346
compile "com.google.code.findbugs:jsr305:3.0.0"
4447
compile group: 'org.springframework', name: 'spring-context', version: '4.2.4.RELEASE'
@@ -55,10 +58,6 @@ subprojects {
5558
from sourceSets.main.allSource
5659
}
5760

58-
artifacts {
59-
// archives jar
60-
archives sourcesJar
61-
}
6261

6362
tasks.withType(AbstractArchiveTask) {
6463
preserveFileTimestamps = false

chainbase/src/main/java/org/tron/core/ChainBaseManager.java

+5
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
import org.tron.core.store.AccountStore;
3636
import org.tron.core.store.AssetIssueStore;
3737
import org.tron.core.store.AssetIssueV2Store;
38+
import org.tron.core.store.BalanceTraceStore;
3839
import org.tron.core.store.CodeStore;
3940
import org.tron.core.store.ContractStore;
4041
import org.tron.core.store.DelegatedResourceAccountIndexStore;
@@ -189,6 +190,10 @@ public class ChainBaseManager {
189190
@Getter
190191
private PbftSignDataStore pbftSignDataStore;
191192

193+
@Autowired
194+
@Getter
195+
private BalanceTraceStore balanceTraceStore;
196+
192197
@Getter
193198
private ForkController forkController = ForkController.instance();
194199

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package org.tron.core.capsule;
2+
3+
import com.google.protobuf.InvalidProtocolBufferException;
4+
import java.util.List;
5+
import java.util.Objects;
6+
import org.tron.common.utils.StringUtil;
7+
import org.tron.core.exception.BadItemException;
8+
import org.tron.protos.contract.BalanceContract;
9+
import org.tron.protos.contract.BalanceContract.AccountTrace;
10+
import org.tron.protos.contract.BalanceContract.TransactionBalanceTrace;
11+
12+
public class AccountTraceCapsule implements ProtoCapsule<AccountTrace> {
13+
private BalanceContract.AccountTrace accountTrace;
14+
15+
public AccountTraceCapsule() {
16+
accountTrace = AccountTrace.newBuilder().build();
17+
}
18+
19+
public AccountTraceCapsule(long balance) {
20+
this();
21+
accountTrace = accountTrace.toBuilder().setBalance(balance).build();
22+
}
23+
24+
public AccountTraceCapsule(AccountTrace accountTrace) {
25+
this.accountTrace = accountTrace;
26+
}
27+
28+
public AccountTraceCapsule(byte[] data) throws BadItemException {
29+
try {
30+
this.accountTrace = AccountTrace.parseFrom(data);
31+
} catch (InvalidProtocolBufferException e) {
32+
throw new BadItemException("AccountTraceCapsule proto data parse exception");
33+
}
34+
}
35+
36+
public Long getBalance() {
37+
return accountTrace.getBalance();
38+
}
39+
40+
@Override
41+
public byte[] getData() {
42+
if (Objects.isNull(accountTrace)) {
43+
return null;
44+
}
45+
46+
if (accountTrace.getBalance() == 0) {
47+
accountTrace = accountTrace.toBuilder().setPlaceholder(1).build();
48+
}
49+
50+
return accountTrace.toByteArray();
51+
}
52+
53+
@Override
54+
public AccountTrace getInstance() {
55+
return accountTrace;
56+
}
57+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
package org.tron.core.capsule;
2+
3+
import com.google.protobuf.InvalidProtocolBufferException;
4+
import java.util.List;
5+
import java.util.Map;
6+
import org.tron.common.utils.StringUtil;
7+
import org.tron.core.exception.BadItemException;
8+
import org.tron.protos.contract.BalanceContract.BlockBalanceTrace;
9+
import org.tron.protos.contract.BalanceContract.TransactionBalanceTrace;
10+
11+
import java.util.Objects;
12+
13+
public class BlockBalanceTraceCapsule implements ProtoCapsule<BlockBalanceTrace> {
14+
private BlockBalanceTrace balanceTrace;
15+
16+
public BlockBalanceTraceCapsule() {
17+
balanceTrace = BlockBalanceTrace.newBuilder().build();
18+
}
19+
20+
public BlockBalanceTraceCapsule(BlockCapsule blockCapsule) {
21+
this();
22+
BlockBalanceTrace.BlockIdentifier blockIdentifier = BlockBalanceTrace.BlockIdentifier.newBuilder()
23+
.setHash(blockCapsule.getBlockId().getByteString())
24+
.setNumber(blockCapsule.getNum())
25+
.build();
26+
27+
balanceTrace = balanceTrace.toBuilder()
28+
.setBlockIdentifier(blockIdentifier)
29+
.setTimestamp(blockCapsule.getTimeStamp())
30+
.build();
31+
}
32+
33+
public BlockBalanceTraceCapsule(byte[] data) throws BadItemException {
34+
try {
35+
this.balanceTrace = BlockBalanceTrace.parseFrom(data);
36+
} catch (InvalidProtocolBufferException e) {
37+
throw new BadItemException("TransactionInfoCapsule proto data parse exception");
38+
}
39+
}
40+
41+
public BlockBalanceTraceCapsule(BlockBalanceTrace blockBalanceTrace) {
42+
this.balanceTrace = blockBalanceTrace;
43+
}
44+
45+
public void addTransactionBalanceTrace(TransactionBalanceTrace transactionBalanceTrace) {
46+
balanceTrace = balanceTrace.toBuilder()
47+
.addTransactionBalanceTrace(transactionBalanceTrace)
48+
.build();
49+
}
50+
51+
public void setTransactionBalanceTrace(int index, TransactionBalanceTrace transactionBalanceTrace) {
52+
balanceTrace = balanceTrace.toBuilder()
53+
.setTransactionBalanceTrace(index, transactionBalanceTrace)
54+
.build();
55+
}
56+
57+
@Override
58+
public byte[] getData() {
59+
if (Objects.isNull(balanceTrace)) {
60+
return null;
61+
}
62+
return balanceTrace.toByteArray();
63+
}
64+
65+
@Override
66+
public BlockBalanceTrace getInstance() {
67+
return balanceTrace;
68+
}
69+
70+
public BlockBalanceTrace.BlockIdentifier getBlockIdentifier() {
71+
return balanceTrace.getBlockIdentifier();
72+
}
73+
74+
public long getTimestamp() {
75+
return balanceTrace.getTimestamp();
76+
}
77+
78+
public List<TransactionBalanceTrace> getTransactions() {
79+
return balanceTrace.getTransactionBalanceTraceList();
80+
}
81+
}

chainbase/src/main/java/org/tron/core/capsule/TransactionCapsule.java

+7
Original file line numberDiff line numberDiff line change
@@ -503,6 +503,13 @@ public void setTimestamp() {
503503
this.transaction = this.transaction.toBuilder().setRawData(rawData).build();
504504
}
505505

506+
public void setTimestamp(long timestamp) {
507+
Transaction.raw rawData = this.transaction.getRawData().toBuilder()
508+
.setTimestamp(timestamp)
509+
.build();
510+
this.transaction = this.transaction.toBuilder().setRawData(rawData).build();
511+
}
512+
506513
public long getTimestamp() {
507514
return transaction.getRawData().getTimestamp();
508515
}

chainbase/src/main/java/org/tron/core/db2/common/HashDB.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,11 @@
33
import java.util.HashMap;
44
import java.util.Iterator;
55
import java.util.Map;
6+
import java.util.concurrent.ConcurrentHashMap;
67

78
public class HashDB implements DB<Key, Value> {
89

9-
private Map<Key, Value> db = new HashMap<>();
10+
private Map<Key, Value> db = new ConcurrentHashMap<>();
1011
private String name;
1112

1213
public HashDB(String name) {

chainbase/src/main/java/org/tron/core/db2/common/IRevokingDB.java

+5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package org.tron.core.db2.common;
22

3+
import java.util.Collections;
34
import java.util.List;
45
import java.util.Map;
56
import java.util.Set;
@@ -34,4 +35,8 @@ public interface IRevokingDB extends Iterable<Map.Entry<byte[], byte[]>> {
3435

3536
List<byte[]> getKeysNext(byte[] key, long limit);
3637

38+
default Map<byte[], byte[]> getNext(byte[] key, long limit) {
39+
return Collections.emptyMap();
40+
}
41+
3742
}

chainbase/src/main/java/org/tron/core/db2/core/Chainbase.java

+41
Original file line numberDiff line numberDiff line change
@@ -288,4 +288,45 @@ private synchronized Set<byte[]> getlatestValues(Snapshot head, long limit) {
288288

289289
return result;
290290
}
291+
292+
// for accout-trace
293+
@Override
294+
public Map<byte[], byte[]> getNext(byte[] key, long limit) {
295+
return getNext(head(), key, limit);
296+
}
297+
298+
// for accout-trace
299+
private Map<byte[], byte[]> getNext(Snapshot head, byte[] key, long limit) {
300+
if (limit <= 0) {
301+
return Collections.emptyMap();
302+
}
303+
304+
Map<WrappedByteArray, WrappedByteArray> collection = new HashMap<>();
305+
if (head.getPrevious() != null) {
306+
((SnapshotImpl) head).collect(collection);
307+
}
308+
309+
Map<WrappedByteArray, WrappedByteArray> levelDBMap = new HashMap<>();
310+
311+
if (((SnapshotRoot) head.getRoot()).db.getClass() == LevelDB.class) {
312+
((LevelDB) ((SnapshotRoot) head.getRoot()).db).getDb().getNext(key, limit).entrySet().stream()
313+
.map(e -> Maps
314+
.immutableEntry(WrappedByteArray.of(e.getKey()), WrappedByteArray.of(e.getValue())))
315+
.forEach(e -> levelDBMap.put(e.getKey(), e.getValue()));
316+
} else if (((SnapshotRoot) head.getRoot()).db.getClass() == RocksDB.class) {
317+
((RocksDB) ((SnapshotRoot) head.getRoot()).db).getDb().getNext(key, limit).entrySet().stream()
318+
.map(e -> Maps
319+
.immutableEntry(WrappedByteArray.of(e.getKey()), WrappedByteArray.of(e.getValue())))
320+
.forEach(e -> levelDBMap.put(e.getKey(), e.getValue()));
321+
}
322+
323+
levelDBMap.putAll(collection);
324+
325+
return levelDBMap.entrySet().stream()
326+
.map(e -> Maps.immutableEntry(e.getKey().getBytes(), e.getValue().getBytes()))
327+
.sorted((e1, e2) -> ByteUtil.compare(e1.getKey(), e2.getKey()))
328+
.filter(e -> ByteUtil.greaterOrEquals(e.getKey(), key))
329+
.limit(limit)
330+
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
331+
}
291332
}

chainbase/src/main/java/org/tron/core/db2/core/SnapshotImpl.java

+4-3
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,13 @@ public class SnapshotImpl extends AbstractSnapshot<Key, Value> {
2323
protected Snapshot root;
2424

2525
SnapshotImpl(Snapshot snapshot) {
26-
root = snapshot.getRoot();
27-
previous = snapshot;
28-
snapshot.setNext(this);
2926
synchronized (this) {
3027
db = new HashDB(SnapshotImpl.class.getSimpleName());
3128
}
29+
30+
root = snapshot.getRoot();
31+
previous = snapshot;
32+
snapshot.setNext(this);
3233
}
3334

3435
@Override

0 commit comments

Comments
 (0)