Skip to content

Commit

Permalink
Merge branch 'develop' of https://github.com/tronprotocol/java-tron i…
Browse files Browse the repository at this point in the history
…nto develop
  • Loading branch information
xxo1shine committed Apr 13, 2018
2 parents 4957b22 + 0573941 commit 8e5685b
Show file tree
Hide file tree
Showing 11 changed files with 128 additions and 98 deletions.
48 changes: 28 additions & 20 deletions src/main/java/org/tron/common/utils/DialogOptional.java
Original file line number Diff line number Diff line change
@@ -1,42 +1,50 @@
package org.tron.common.utils;

import java.util.Optional;
import org.tron.core.db.AbstractRevokingStore.Dialog;

import java.util.Optional;
public final class DialogOptional {

public class DialogOptional<T extends Dialog> {
private static final DialogOptional<?> EMPTY = new DialogOptional<>();
private static final DialogOptional INSTANCE = OptionalEnum.INSTANCE.getInstance();

private Optional<T> value;
private Optional<Dialog> value;

private DialogOptional() {
this.value = Optional.empty();
}

public static <T extends Dialog> DialogOptional<T> empty() {
@SuppressWarnings("unchecked")
DialogOptional<T> t = (DialogOptional<T>) EMPTY;
return t;
public synchronized DialogOptional setValue(Dialog value) {
if (!this.value.isPresent()) {
this.value = Optional.of(value);
}
return this;
}

private DialogOptional(T value) {
this.value = Optional.of(value);
public synchronized boolean valid() {
return value.isPresent();
}

public static <T extends Dialog> DialogOptional<T> of(T value) {
return new DialogOptional<>(value);
public synchronized void reset() {
value.ifPresent(Dialog::destroy);
value = Optional.empty();
}

public static <T extends Dialog> DialogOptional<T> ofNullable(T value) {
return value == null ? empty() : of(value);
public static DialogOptional instance() {
return INSTANCE;
}

public boolean valid() {
return value.isPresent();
}
private enum OptionalEnum {
INSTANCE;

public void reset() {
value.ifPresent(Dialog::destroy);
value = Optional.empty();
private DialogOptional instance;

OptionalEnum() {
instance = new DialogOptional();
}

private DialogOptional getInstance() {
return instance;
}
}

}
6 changes: 3 additions & 3 deletions src/main/java/org/tron/core/db/Manager.java
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public class Manager {
private KhaosDatabase khaosDb;
private RevokingDatabase revokingStore;
@Getter
private DialogOptional<Dialog> dialog = DialogOptional.empty();
private DialogOptional dialog = DialogOptional.instance();

@Getter
@Setter
Expand Down Expand Up @@ -350,7 +350,7 @@ public synchronized boolean pushTransactions(final TransactionCapsule trx)
validateFreq(trx);

if (!dialog.valid()) {
dialog = DialogOptional.of(revokingStore.buildDialog());
dialog.setValue(revokingStore.buildDialog());
}

try (
Expand Down Expand Up @@ -697,7 +697,7 @@ public synchronized BlockCapsule generateBlock(final WitnessCapsule witnessCapsu
witnessCapsule.getAddress());

dialog.reset();
dialog = DialogOptional.of(revokingStore.buildDialog());
dialog.setValue(revokingStore.buildDialog());

Iterator iterator = pendingTransactions.iterator();
while (iterator.hasNext()) {
Expand Down
1 change: 1 addition & 0 deletions src/test/java/org/tron/core/actuator/AssetIssueActuatorTest.java
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -149,5 +149,6 @@ public static void destroy() {
} else {
logger.info("Release resources failure.");
}
dbManager.destory();
}
}
1 change: 1 addition & 0 deletions src/test/java/org/tron/core/actuator/CreateAccountActuatorTest.java
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -126,5 +126,6 @@ public static void destroy() {
} else {
logger.info("Release resources failure.");
}
dbManager.destory();
}
}
1 change: 1 addition & 0 deletions src/test/java/org/tron/core/actuator/ParticipateAssetIssueActuatorTest.java
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ public static void destroy() {
} else {
logger.info("Release resources failure.");
}
dbManager.destory();
}

/**
Expand Down
1 change: 1 addition & 0 deletions src/test/java/org/tron/core/actuator/TransferActuatorTest.java
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ public static void destroy() {
} else {
logger.info("Release resources failure.");
}
dbManager.destory();
}

/**
Expand Down
1 change: 1 addition & 0 deletions src/test/java/org/tron/core/actuator/TransferAssetActuatorTest.java
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -209,5 +209,6 @@ public static void destroy() {
} else {
logger.info("Release resources failure.");
}
dbManager.destory();
}
}
Empty file modified src/test/java/org/tron/core/db/AccountStoreTest.java
100644 → 100755
Empty file.
146 changes: 84 additions & 62 deletions src/test/java/org/tron/core/db/ManagerTest.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package org.tron.core.db;

import com.google.common.collect.Maps;
import com.google.protobuf.ByteString;
import java.io.File;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import lombok.extern.slf4j.Slf4j;
Expand All @@ -13,7 +15,9 @@
import org.tron.common.utils.ByteArray;
import org.tron.common.utils.FileUtil;
import org.tron.common.utils.Sha256Hash;
import org.tron.common.utils.Utils;
import org.tron.core.Constant;
import org.tron.core.capsule.AccountCapsule;
import org.tron.core.capsule.BlockCapsule;
import org.tron.core.capsule.TransactionCapsule;
import org.tron.core.capsule.WitnessCapsule;
Expand All @@ -25,7 +29,9 @@
import org.tron.core.exception.UnLinkedBlockException;
import org.tron.core.exception.ValidateScheduleException;
import org.tron.core.exception.ValidateSignatureException;
import org.tron.core.witness.WitnessController;
import org.tron.protos.Contract.TransferContract;
import org.tron.protos.Protocol.Account;
import org.tron.protos.Protocol.Transaction.Contract.ContractType;

@Slf4j
Expand Down Expand Up @@ -149,7 +155,7 @@ public void pushBlock() {
}


// @Test
// @Test
public void updateWits() {
int sizePrv = dbManager.getWitnesses().size();
dbManager.getWitnesses().forEach(witnessCapsule -> {
Expand Down Expand Up @@ -185,69 +191,85 @@ public void updateWits() {
}

@Test
public void fork() {
public void fork() throws ValidateSignatureException,
ContractValidateException,
ContractExeException,
UnLinkedBlockException,
ValidateScheduleException,
BadItemException,
ItemNotFoundException {
Args.setParam(new String[]{"--witness"}, Constant.TEST_CONF);
long size = dbManager.getBlockStore().dbSource.allKeys().size();
String key = "f31db24bfbd1a2ef19beddca0a0fa37632eded9ac666a05d3bd925f01dde1f62";
byte[] privateKey = ByteArray.fromHexString(key);
final ECKey ecKey = ECKey.fromPrivate(privateKey);
byte[] address = ecKey.getAddress();
WitnessCapsule witnessCapsule = new WitnessCapsule(ByteString.copyFrom(address));
dbManager.addWitness(witnessCapsule);
dbManager.addWitness(witnessCapsule);
dbManager.addWitness(witnessCapsule);
IntStream.range(0, 1).forEach(i -> {
try {
dbManager.generateBlock(witnessCapsule, System.currentTimeMillis(), privateKey);
} catch (Exception e) {
logger.debug(e.getMessage(), e);
}
});

try {
long num = dbManager.getDynamicPropertiesStore().getLatestBlockHeaderNumber();
BlockCapsule blockCapsule1 = new BlockCapsule(num,
dbManager.getHead().getParentHash().getByteString(),
System.currentTimeMillis(),
witnessCapsule.getAddress());
blockCapsule1.generatedByMyself = true;

BlockCapsule blockCapsule2 = new BlockCapsule(num + 1,
blockCapsule1.getBlockId().getByteString(),
System.currentTimeMillis(),
witnessCapsule.getAddress());
blockCapsule2.generatedByMyself = true;

logger.error("******1*******" + "block1 id:" + blockCapsule1.getBlockId());
logger.error("******2*******" + "block2 id:" + blockCapsule2.getBlockId());
dbManager.pushBlock(blockCapsule1);
dbManager.pushBlock(blockCapsule1);
logger.error("******in blockStore block size:"
+ dbManager.getBlockStore().dbSource.allKeys().size());
logger.error("******in blockStore block:"
+ dbManager.getBlockStore().dbSource.allKeys().stream().map(ByteArray::toHexString)
.collect(Collectors.toList()));

Assert.assertNotNull(dbManager.getBlockStore().get(blockCapsule1.getBlockId().getBytes()));
Assert.assertNotNull(dbManager.getBlockStore().get(blockCapsule2.getBlockId().getBytes()));

Assert.assertEquals(
dbManager.getBlockStore().get(blockCapsule2.getBlockId().getBytes()).getParentHash(),
blockCapsule1.getBlockId());

Assert.assertEquals(dbManager.getBlockStore().dbSource.allKeys().size(), size + 2);

Assert.assertEquals(dbManager.getBlockIdByNum(dbManager.getHead().getNum() - 1),
blockCapsule1.getBlockId());
Assert.assertEquals(dbManager.getBlockIdByNum(dbManager.getHead().getNum() - 2),
blockCapsule1.getParentHash());

Assert.assertEquals(blockCapsule2.getBlockId().getByteString(),
dbManager.getDynamicPropertiesStore().getLatestBlockHeaderHash());
Assert.assertEquals(dbManager.getHead().getBlockId().getByteString(),
dbManager.getDynamicPropertiesStore().getLatestBlockHeaderHash());
} catch (Exception e) {
logger.debug(e.getMessage(), e);
}
Map<ByteString, String> addressToProvateKeys = addTestWitnessAndAccount();

long num = dbManager.getDynamicPropertiesStore().getLatestBlockHeaderNumber();
BlockCapsule blockCapsule0 = createTestBlockCapsule(num + 1,
dbManager.getDynamicPropertiesStore().getLatestBlockHeaderHash().getByteString(),
addressToProvateKeys);

BlockCapsule blockCapsule1 = createTestBlockCapsule(num + 1,
dbManager.getDynamicPropertiesStore().getLatestBlockHeaderHash().getByteString(),
addressToProvateKeys);

BlockCapsule blockCapsule2 = createTestBlockCapsule(num + 2,
blockCapsule1.getBlockId().getByteString(), addressToProvateKeys);

dbManager.pushBlock(blockCapsule0);
dbManager.pushBlock(blockCapsule1);
dbManager.pushBlock(blockCapsule2);

Assert.assertNotNull(dbManager.getBlockStore().get(blockCapsule1.getBlockId().getBytes()));
Assert.assertNotNull(dbManager.getBlockStore().get(blockCapsule2.getBlockId().getBytes()));

Assert.assertEquals(
dbManager.getBlockStore().get(blockCapsule2.getBlockId().getBytes()).getParentHash(),
blockCapsule1.getBlockId());

Assert.assertEquals(dbManager.getBlockStore().dbSource.allKeys().size(), size + 2);

Assert.assertEquals(dbManager.getBlockIdByNum(dbManager.getHead().getNum() - 1),
blockCapsule1.getBlockId());
Assert.assertEquals(dbManager.getBlockIdByNum(dbManager.getHead().getNum() - 2),
blockCapsule1.getParentHash());

Assert.assertEquals(blockCapsule2.getBlockId(),
dbManager.getDynamicPropertiesStore().getLatestBlockHeaderHash());
Assert.assertEquals(dbManager.getHead().getBlockId(),
dbManager.getDynamicPropertiesStore().getLatestBlockHeaderHash());
}

private Map<ByteString, String> addTestWitnessAndAccount() {
dbManager.getWitnesses().clear();
return IntStream.range(0, 2).mapToObj(i -> {
ECKey ecKey = new ECKey(Utils.getRandom());
String privateKey = ByteArray.toHexString(ecKey.getPrivKey().toByteArray());
ByteString address = ByteString.copyFrom(ecKey.getAddress());

WitnessCapsule witnessCapsule = new WitnessCapsule(address);
dbManager.getWitnessStore().put(address.toByteArray(), witnessCapsule);
dbManager.getWitnessController().addWitness(witnessCapsule);

AccountCapsule accountCapsule =
new AccountCapsule(Account.newBuilder().setAddress(address).build());
dbManager.getAccountStore().put(address.toByteArray(), accountCapsule);

return Maps.immutableEntry(address, privateKey);
})
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
}

private BlockCapsule createTestBlockCapsule(long number, ByteString hash,
Map<ByteString, String> addressToProvateKeys) {
long time = System.currentTimeMillis();
WitnessController witnessController = dbManager.getWitnessController();
ByteString witnessAddress =
witnessController.getScheduledWitness(witnessController.getSlotAtTime(time));
BlockCapsule blockCapsule = new BlockCapsule(number, hash, time, witnessAddress);
blockCapsule.generatedByMyself = true;
blockCapsule.setMerkleRoot();
blockCapsule.sign(ByteArray.fromHexString(addressToProvateKeys.get(witnessAddress)));
return blockCapsule;
}

}
19 changes: 7 additions & 12 deletions src/test/java/org/tron/core/db/RevokingStoreTest.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package org.tron.core.db;

import java.io.File;
import java.util.stream.IntStream;
import lombok.extern.slf4j.Slf4j;
import org.junit.After;
import org.junit.Assert;
Expand Down Expand Up @@ -35,24 +34,22 @@ public void removeDb() {
}

@Test
public synchronized void testUndo() {
public synchronized void testUndo() throws RevokingStoreIllegalStateException {
revokingDatabase.getStack().clear();
TestRevokingTronStore tronDatabase = new TestRevokingTronStore(
"testrevokingtronstore-testUndo", revokingDatabase);
TestProtoCapsule testProtoCapsule = new TestProtoCapsule();

DialogOptional dialog = DialogOptional.of(revokingDatabase.buildDialog());
IntStream.range(0, 10).forEach(i -> {
DialogOptional dialog = DialogOptional.instance().setValue(revokingDatabase.buildDialog());
for (int i = 0; i < 10; i++) {
try (Dialog tmpDialog = revokingDatabase.buildDialog()) {
tronDatabase.put(testProtoCapsule.getData(), testProtoCapsule);
Assert.assertFalse(tronDatabase.getDbSource().allKeys().isEmpty());
Assert.assertEquals(revokingDatabase.getStack().size(), 2);
tmpDialog.merge();
Assert.assertEquals(revokingDatabase.getStack().size(), 1);
} catch (RevokingStoreIllegalStateException e) {
logger.debug(e.getMessage(), e);
}
});
}

Assert.assertEquals(revokingDatabase.getStack().size(), 1);

Expand All @@ -65,24 +62,22 @@ public synchronized void testUndo() {
}

@Test
public synchronized void testPop() {
public synchronized void testPop() throws RevokingStoreIllegalStateException {
revokingDatabase.getStack().clear();
TestRevokingTronStore tronDatabase = new TestRevokingTronStore(
"testrevokingtronstore-testPop", revokingDatabase);
TestProtoCapsule testProtoCapsule = new TestProtoCapsule();

IntStream.rangeClosed(1, 10).forEach(i -> {
for (int i = 1; i < 11; i++) {
try (Dialog tmpDialog = revokingDatabase.buildDialog()) {
tronDatabase.put(testProtoCapsule.getData(), testProtoCapsule);
Assert.assertFalse(tronDatabase.getDbSource().allKeys().isEmpty());
Assert.assertEquals(revokingDatabase.getActiveDialog(), 1);
tmpDialog.commit();
Assert.assertEquals(revokingDatabase.getStack().size(), i);
Assert.assertEquals(revokingDatabase.getActiveDialog(), 0);
} catch (RevokingStoreIllegalStateException e) {
logger.debug(e.getMessage(), e);
}
});
}

try {
revokingDatabase.pop();
Expand Down
2 changes: 1 addition & 1 deletion src/test/java/org/tron/program/AccountVoteWitnessTest.java
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public static void removeDb() {
} else {
logger.info("Release resources failure.");
}

dbManager.destory();
}

private static Boolean deleteFolder(File index) {
Expand Down

0 comments on commit 8e5685b

Please sign in to comment.