Skip to content

Commit

Permalink
base on GreatVoyage-v4.1.1
Browse files Browse the repository at this point in the history
  • Loading branch information
Yrp committed Nov 13, 2020
1 parent 53ea878 commit c6aa01d
Show file tree
Hide file tree
Showing 19 changed files with 627 additions and 22 deletions.
13 changes: 6 additions & 7 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ subprojects {
maven { url 'https://jitpack.io' }
}
dependencies {
classpath 'com.google.protobuf:protobuf-gradle-plugin:0.8.3'
classpath 'com.github.jengelman.gradle.plugins:shadow:2.0.2'
classpath 'com.google.protobuf:protobuf-gradle-plugin:0.8.12'
classpath 'com.github.jengelman.gradle.plugins:shadow:5.2.0'
}
}

Expand All @@ -38,7 +38,10 @@ subprojects {
compile group: 'org.slf4j', name: 'jcl-over-slf4j', version: '1.7.25'
compile "org.slf4j:jcl-over-slf4j:1.7.25"
compile group: 'ch.qos.logback', name: 'logback-classic', version: '1.2.3'
compile group: 'org.projectlombok', name: 'lombok', version: '1.18.2'
compileOnly 'org.projectlombok:lombok:1.18.12'
annotationProcessor 'org.projectlombok:lombok:1.18.12'
testCompileOnly 'org.projectlombok:lombok:1.18.12'
testAnnotationProcessor 'org.projectlombok:lombok:1.18.12'
compile group: 'com.google.guava', name: 'guava', version: '24.1-jre'
compile "com.google.code.findbugs:jsr305:3.0.0"
compile group: 'org.springframework', name: 'spring-context', version: '4.2.4.RELEASE'
Expand All @@ -55,10 +58,6 @@ subprojects {
from sourceSets.main.allSource
}

artifacts {
// archives jar
archives sourcesJar
}

tasks.withType(AbstractArchiveTask) {
preserveFileTimestamps = false
Expand Down
5 changes: 5 additions & 0 deletions chainbase/src/main/java/org/tron/core/ChainBaseManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import org.tron.core.store.AccountStore;
import org.tron.core.store.AssetIssueStore;
import org.tron.core.store.AssetIssueV2Store;
import org.tron.core.store.BalanceTraceStore;
import org.tron.core.store.CodeStore;
import org.tron.core.store.ContractStore;
import org.tron.core.store.DelegatedResourceAccountIndexStore;
Expand Down Expand Up @@ -189,6 +190,10 @@ public class ChainBaseManager {
@Getter
private PbftSignDataStore pbftSignDataStore;

@Autowired
@Getter
private BalanceTraceStore balanceTraceStore;

@Getter
private ForkController forkController = ForkController.instance();

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package org.tron.core.capsule;

import com.google.protobuf.InvalidProtocolBufferException;
import java.util.List;
import java.util.Objects;
import org.tron.common.utils.StringUtil;
import org.tron.core.exception.BadItemException;
import org.tron.protos.contract.BalanceContract;
import org.tron.protos.contract.BalanceContract.AccountTrace;
import org.tron.protos.contract.BalanceContract.TransactionBalanceTrace;

public class AccountTraceCapsule implements ProtoCapsule<AccountTrace> {
private BalanceContract.AccountTrace accountTrace;

public AccountTraceCapsule() {
accountTrace = AccountTrace.newBuilder().build();
}

public AccountTraceCapsule(long balance) {
this();
accountTrace = accountTrace.toBuilder().setBalance(balance).build();
}

public AccountTraceCapsule(AccountTrace accountTrace) {
this.accountTrace = accountTrace;
}

public AccountTraceCapsule(byte[] data) throws BadItemException {
try {
this.accountTrace = AccountTrace.parseFrom(data);
} catch (InvalidProtocolBufferException e) {
throw new BadItemException("AccountTraceCapsule proto data parse exception");
}
}

public Long getBalance() {
return accountTrace.getBalance();
}

@Override
public byte[] getData() {
if (Objects.isNull(accountTrace)) {
return null;
}

if (accountTrace.getBalance() == 0) {
accountTrace = accountTrace.toBuilder().setPlaceholder(1).build();
}

return accountTrace.toByteArray();
}

@Override
public AccountTrace getInstance() {
return accountTrace;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package org.tron.core.capsule;

import com.google.protobuf.InvalidProtocolBufferException;
import java.util.List;
import java.util.Map;
import org.tron.common.utils.StringUtil;
import org.tron.core.exception.BadItemException;
import org.tron.protos.contract.BalanceContract.BlockBalanceTrace;
import org.tron.protos.contract.BalanceContract.TransactionBalanceTrace;

import java.util.Objects;

public class BlockBalanceTraceCapsule implements ProtoCapsule<BlockBalanceTrace> {
private BlockBalanceTrace balanceTrace;

public BlockBalanceTraceCapsule() {
balanceTrace = BlockBalanceTrace.newBuilder().build();
}

public BlockBalanceTraceCapsule(BlockCapsule blockCapsule) {
this();
BlockBalanceTrace.BlockIdentifier blockIdentifier = BlockBalanceTrace.BlockIdentifier.newBuilder()
.setHash(blockCapsule.getBlockId().getByteString())
.setNumber(blockCapsule.getNum())
.build();

balanceTrace = balanceTrace.toBuilder()
.setBlockIdentifier(blockIdentifier)
.setTimestamp(blockCapsule.getTimeStamp())
.build();
}

public BlockBalanceTraceCapsule(byte[] data) throws BadItemException {
try {
this.balanceTrace = BlockBalanceTrace.parseFrom(data);
} catch (InvalidProtocolBufferException e) {
throw new BadItemException("TransactionInfoCapsule proto data parse exception");
}
}

public BlockBalanceTraceCapsule(BlockBalanceTrace blockBalanceTrace) {
this.balanceTrace = blockBalanceTrace;
}

public void addTransactionBalanceTrace(TransactionBalanceTrace transactionBalanceTrace) {
balanceTrace = balanceTrace.toBuilder()
.addTransactionBalanceTrace(transactionBalanceTrace)
.build();
}

public void setTransactionBalanceTrace(int index, TransactionBalanceTrace transactionBalanceTrace) {
balanceTrace = balanceTrace.toBuilder()
.setTransactionBalanceTrace(index, transactionBalanceTrace)
.build();
}

@Override
public byte[] getData() {
if (Objects.isNull(balanceTrace)) {
return null;
}
return balanceTrace.toByteArray();
}

@Override
public BlockBalanceTrace getInstance() {
return balanceTrace;
}

public BlockBalanceTrace.BlockIdentifier getBlockIdentifier() {
return balanceTrace.getBlockIdentifier();
}

public long getTimestamp() {
return balanceTrace.getTimestamp();
}

public List<TransactionBalanceTrace> getTransactions() {
return balanceTrace.getTransactionBalanceTraceList();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -503,6 +503,13 @@ public void setTimestamp() {
this.transaction = this.transaction.toBuilder().setRawData(rawData).build();
}

public void setTimestamp(long timestamp) {
Transaction.raw rawData = this.transaction.getRawData().toBuilder()
.setTimestamp(timestamp)
.build();
this.transaction = this.transaction.toBuilder().setRawData(rawData).build();
}

public long getTimestamp() {
return transaction.getRawData().getTimestamp();
}
Expand Down
3 changes: 2 additions & 1 deletion chainbase/src/main/java/org/tron/core/db2/common/HashDB.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class HashDB implements DB<Key, Value> {

private Map<Key, Value> db = new HashMap<>();
private Map<Key, Value> db = new ConcurrentHashMap<>();
private String name;

public HashDB(String name) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.tron.core.db2.common;

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

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

default Map<byte[], byte[]> getNext(byte[] key, long limit) {
return Collections.emptyMap();
}

}
41 changes: 41 additions & 0 deletions chainbase/src/main/java/org/tron/core/db2/core/Chainbase.java
Original file line number Diff line number Diff line change
Expand Up @@ -288,4 +288,45 @@ private synchronized Set<byte[]> getlatestValues(Snapshot head, long limit) {

return result;
}

// for accout-trace
@Override
public Map<byte[], byte[]> getNext(byte[] key, long limit) {
return getNext(head(), key, limit);
}

// for accout-trace
private Map<byte[], byte[]> getNext(Snapshot head, byte[] key, long limit) {
if (limit <= 0) {
return Collections.emptyMap();
}

Map<WrappedByteArray, WrappedByteArray> collection = new HashMap<>();
if (head.getPrevious() != null) {
((SnapshotImpl) head).collect(collection);
}

Map<WrappedByteArray, WrappedByteArray> levelDBMap = new HashMap<>();

if (((SnapshotRoot) head.getRoot()).db.getClass() == LevelDB.class) {
((LevelDB) ((SnapshotRoot) head.getRoot()).db).getDb().getNext(key, limit).entrySet().stream()
.map(e -> Maps
.immutableEntry(WrappedByteArray.of(e.getKey()), WrappedByteArray.of(e.getValue())))
.forEach(e -> levelDBMap.put(e.getKey(), e.getValue()));
} else if (((SnapshotRoot) head.getRoot()).db.getClass() == RocksDB.class) {
((RocksDB) ((SnapshotRoot) head.getRoot()).db).getDb().getNext(key, limit).entrySet().stream()
.map(e -> Maps
.immutableEntry(WrappedByteArray.of(e.getKey()), WrappedByteArray.of(e.getValue())))
.forEach(e -> levelDBMap.put(e.getKey(), e.getValue()));
}

levelDBMap.putAll(collection);

return levelDBMap.entrySet().stream()
.map(e -> Maps.immutableEntry(e.getKey().getBytes(), e.getValue().getBytes()))
.sorted((e1, e2) -> ByteUtil.compare(e1.getKey(), e2.getKey()))
.filter(e -> ByteUtil.greaterOrEquals(e.getKey(), key))
.limit(limit)
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,13 @@ public class SnapshotImpl extends AbstractSnapshot<Key, Value> {
protected Snapshot root;

SnapshotImpl(Snapshot snapshot) {
root = snapshot.getRoot();
previous = snapshot;
snapshot.setNext(this);
synchronized (this) {
db = new HashDB(SnapshotImpl.class.getSimpleName());
}

root = snapshot.getRoot();
previous = snapshot;
snapshot.setNext(this);
}

@Override
Expand Down
Loading

0 comments on commit c6aa01d

Please sign in to comment.