Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -102,22 +102,24 @@ public LevelDbDataSourceImpl(String parentPath, String dataBaseName) {
public void initDB() {
resetDbLock.writeLock().lock();
try {
logger.debug("~> LevelDbDataSourceImpl.initDB(): " + dataBaseName);
logger.debug("Init DB: {}.", dataBaseName);

if (isAlive()) {
return;
}

if (dataBaseName == null) {
throw new NullPointerException("no name set to the dbStore");
throw new IllegalArgumentException("No name set to the dbStore");
}

try {
openDatabase(options);
alive = true;
} catch (IOException ioe) {
throw new RuntimeException("Can't initialize database", ioe);
throw new RuntimeException(String.format("Can't initialize database, %s", dataBaseName),
ioe);
}
logger.debug("Init DB {} done.", dataBaseName);
} finally {
resetDbLock.writeLock().unlock();
}
Expand All @@ -126,19 +128,21 @@ public void initDB() {
private void openDatabase(Options dbOptions) throws IOException {
final Path dbPath = getDbPath();
if (dbPath == null || dbPath.getParent() == null) {
return;
throw new IOException(String.format("db path is illegal, %s/%s", parentPath, dataBaseName));
Comment thread
halibobo1205 marked this conversation as resolved.
}
if (!Files.isSymbolicLink(dbPath.getParent())) {
Files.createDirectories(dbPath.getParent());
}
try {
database = factory.open(dbPath.toFile(), dbOptions);
logger.info("DB {} open success with : writeBufferSize {}M,cacheSize {}M,maxOpenFiles {}.",
logger.info("DB {} open success with writeBufferSize {} M, cacheSize {} M, maxOpenFiles {}.",
this.getDBName(), dbOptions.writeBufferSize() / 1024 / 1024,
dbOptions.cacheSize() / 1024 / 1024, dbOptions.maxOpenFiles());
} catch (IOException e) {
if (e.getMessage().contains("Corruption:")) {
logger.warn("DB {} corruption detected, try to repair it.", this.getDBName());
Comment thread
halibobo1205 marked this conversation as resolved.
factory.repair(dbPath.toFile(), dbOptions);
logger.warn("DB {} corruption detected, repair done.", this.getDBName());
database = factory.open(dbPath.toFile(), dbOptions);
} else {
throw e;
Expand Down Expand Up @@ -460,7 +464,7 @@ public void closeDB() {
database.close();
alive = false;
} catch (IOException e) {
logger.error("Failed to find the dbStore file on the closeDB: {} ", dataBaseName);
logger.error("Failed to find the dbStore file on the closeDB: {}.", dataBaseName, e);
} finally {
resetDbLock.writeLock().unlock();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,12 @@
import org.tron.core.db2.common.WrappedByteArray;


@Slf4j
@Slf4j(topic = "DB")
@NoArgsConstructor
public class RocksDbDataSourceImpl extends DbStat implements DbSourceInter<byte[]>,
Iterable<Map.Entry<byte[], byte[]>>, Instance<RocksDbDataSourceImpl> {

ReadOptions readOpts;
private static final String FAIL_TO_INIT_DATABASE = "Failed to initialize database";
private String dataBaseName;
private RocksDB database;
private boolean alive;
Expand Down Expand Up @@ -102,6 +101,7 @@ public void closeDB() {
database.close();
alive = false;
} catch (Exception e) {
logger.error("Failed to find the dbStore file on the closeDB: {}.", dataBaseName, e);
Comment thread
halibobo1205 marked this conversation as resolved.
} finally {
resetDbLock.writeLock().unlock();
}
Expand All @@ -116,7 +116,7 @@ public void resetDb() {

private boolean quitIfNotAlive() {
if (!isAlive()) {
logger.warn("db is not alive");
logger.warn("DB {} is not alive.", dataBaseName);
}
return !isAlive();
}
Expand Down Expand Up @@ -181,8 +181,8 @@ public boolean checkOrInitEngine() {

public void initDB() {
if (!checkOrInitEngine()) {
logger.error("database engine do not match");
throw new RuntimeException(FAIL_TO_INIT_DATABASE);
throw new RuntimeException(
String.format("failed to check database: %s, engine do not match", dataBaseName));
Comment thread
halibobo1205 marked this conversation as resolved.
}
initDB(RocksDbSettings.getSettings());
}
Expand All @@ -194,7 +194,7 @@ public void initDB(RocksDbSettings settings) {
return;
}
if (dataBaseName == null) {
throw new NullPointerException("no name set to the dbStore");
throw new IllegalArgumentException("No name set to the dbStore");
}

try (Options options = new Options()) {
Expand Down Expand Up @@ -238,7 +238,7 @@ public void initDB(RocksDbSettings settings) {
.setVerifyChecksums(false);

try {
logger.debug("Opening database");
logger.debug("Opening database {}.", dataBaseName);
final Path dbPath = getDbPath();

if (!Files.isSymbolicLink(dbPath.getParent())) {
Expand All @@ -248,17 +248,17 @@ public void initDB(RocksDbSettings settings) {
try {
database = RocksDB.open(options, dbPath.toString());
} catch (RocksDBException e) {
logger.error(e.getMessage(), e);
throw new RuntimeException(FAIL_TO_INIT_DATABASE, e);
throw new RuntimeException(
String.format("failed to open database: %s", dataBaseName), e);
Comment thread
halibobo1205 marked this conversation as resolved.
}

alive = true;
} catch (IOException ioe) {
logger.error(ioe.getMessage(), ioe);
throw new RuntimeException(FAIL_TO_INIT_DATABASE, ioe);
throw new RuntimeException(
String.format("failed to init database: %s", dataBaseName), ioe);
Comment thread
halibobo1205 marked this conversation as resolved.
}

logger.debug("<~ RocksDbDataSource.initDB(): " + dataBaseName);
logger.debug("Init DB {} done.", dataBaseName);
}
} finally {
resetDbLock.writeLock().unlock();
Expand All @@ -274,7 +274,7 @@ public void putData(byte[] key, byte[] value) {
try {
database.put(key, value);
} catch (RocksDBException e) {
throw new RuntimeException(e);
throw new RuntimeException(dataBaseName, e);
Comment thread
halibobo1205 marked this conversation as resolved.
} finally {
resetDbLock.readLock().unlock();
}
Expand All @@ -289,7 +289,7 @@ public byte[] getData(byte[] key) {
try {
return database.get(key);
} catch (RocksDBException e) {
throw new RuntimeException(e);
throw new RuntimeException(dataBaseName, e);
Comment thread
halibobo1205 marked this conversation as resolved.
} finally {
resetDbLock.readLock().unlock();
}
Expand All @@ -304,7 +304,7 @@ public void deleteData(byte[] key) {
try {
database.delete(key);
} catch (RocksDBException e) {
throw new RuntimeException(e);
throw new RuntimeException(dataBaseName, e);
Comment thread
halibobo1205 marked this conversation as resolved.
} finally {
resetDbLock.readLock().unlock();
}
Expand Down Expand Up @@ -365,7 +365,7 @@ public void updateByBatch(Map<byte[], byte[]> rows, WriteOptionsWrapper optionsW
try {
updateByBatchInner(rows);
} catch (Exception e1) {
throw new RuntimeException(e);
throw new RuntimeException(dataBaseName, e1);
Comment thread
halibobo1205 marked this conversation as resolved.
}
} finally {
resetDbLock.readLock().unlock();
Expand All @@ -384,7 +384,7 @@ public void updateByBatch(Map<byte[], byte[]> rows) {
try {
updateByBatchInner(rows);
} catch (Exception e1) {
throw new RuntimeException(e);
throw new RuntimeException(dataBaseName, e1);
Comment thread
halibobo1205 marked this conversation as resolved.
}
} finally {
resetDbLock.readLock().unlock();
Expand Down
14 changes: 10 additions & 4 deletions chainbase/src/main/java/org/tron/common/utils/Commons.java
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,8 @@ public static void adjustBalance(AccountStore accountStore, AccountCapsule accou

if (amount < 0 && balance < -amount) {
throw new BalanceInsufficientException(
StringUtil.createReadableString(account.createDbKey()) + " insufficient balance");
String.format("%s insufficient balance, balance: %d, amount: %d",
StringUtil.createReadableString(account.createDbKey()), balance, -amount));
}
account.setBalance(Math.addExact(balance, amount));
accountStore.put(account.getAddress().toByteArray(), account);
Expand Down Expand Up @@ -120,12 +121,16 @@ public static void adjustAssetBalanceV2(AccountCapsule account, String AssetID,
if (amount < 0) {
if (!account.reduceAssetAmountV2(AssetID.getBytes(), -amount, dynamicPropertiesStore,
assetIssueStore)) {
throw new BalanceInsufficientException("reduceAssetAmount failed !");
throw new BalanceInsufficientException(
String.format("reduceAssetAmount failed! account: %s",
Comment thread
halibobo1205 marked this conversation as resolved.
StringUtil.encode58Check(account.createDbKey())));
}
} else if (amount > 0 &&
!account.addAssetAmountV2(AssetID.getBytes(), amount, dynamicPropertiesStore,
assetIssueStore)) {
throw new BalanceInsufficientException("addAssetAmount failed !");
throw new BalanceInsufficientException(
String.format("addAssetAmount failed! account: %s",
Comment thread
halibobo1205 marked this conversation as resolved.
StringUtil.encode58Check(account.createDbKey())));
}
accountStore.put(account.getAddress().toByteArray(), account);
}
Expand All @@ -135,7 +140,8 @@ public static void adjustTotalShieldedPoolValue(long valueBalance,
long totalShieldedPoolValue = Math
.subtractExact(dynamicPropertiesStore.getTotalShieldedPoolValue(), valueBalance);
if (totalShieldedPoolValue < 0) {
throw new BalanceInsufficientException("Total shielded pool value can not below 0");
throw new BalanceInsufficientException(String.format(
"total shielded pool value can not below 0, actual: %d", totalShieldedPoolValue));
Comment thread
halibobo1205 marked this conversation as resolved.
}
dynamicPropertiesStore.saveTotalShieldedPoolValue(totalShieldedPoolValue);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ private boolean passOld(int version) {
private boolean passNew(int version) {
ForkBlockVersionEnum versionEnum = ForkBlockVersionEnum.getForkBlockVersionEnum(version);
if (versionEnum == null) {
logger.error("not exist block version: {}", version);
logger.error("Not exist block version: {}.", version);
return false;
}
long latestBlockTime = manager.getDynamicPropertiesStore().getLatestBlockHeaderTimestamp();
Expand Down Expand Up @@ -167,7 +167,7 @@ public synchronized void update(BlockCapsule blockCapsule) {
stats[slot] = VERSION_UPGRADE;
manager.getDynamicPropertiesStore().statsByVersion(version, stats);
logger.info(
"*******update hard fork:{}, witness size:{}, solt:{}, witness:{}, version:{}",
"******* Update hard fork: {}, witness size: {}, solt: {}, witness: {}, version: {}.",
Streams.zip(witnesses.stream(), Stream.of(ArrayUtils.toObject(stats)), Maps::immutableEntry)
.map(e -> Maps
.immutableEntry(encode58Check(e.getKey().toByteArray()), e.getValue()))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,8 @@ private void validate(String privateKey) {
if (StringUtils.isNotBlank(privateKey)
&& privateKey.length() != ChainConstant.PRIVATE_KEY_LENGTH) {
throw new IllegalArgumentException(
"Private key(" + privateKey + ") must be " + ChainConstant.PRIVATE_KEY_LENGTH
+ "-bits hex string.");
String.format("private key must be %d-bits hex string, actual: %d",
ChainConstant.PRIVATE_KEY_LENGTH, privateKey.length()));
}
}

Expand All @@ -101,15 +101,15 @@ public void addPrivateKeys(String privateKey) {
//get the first one recently
public String getPrivateKey() {
if (CollectionUtils.isEmpty(privateKeys)) {
logger.warn("privateKey is null");
logger.warn("PrivateKey is null.");
return null;
}
return privateKeys.get(0);
}

public byte[] getPublicKey() {
if (CollectionUtils.isEmpty(privateKeys)) {
logger.warn("privateKey is null");
logger.warn("PrivateKey is null.");
return null;
}
byte[] privateKey = ByteArray.fromHexString(getPrivateKey());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ public static boolean checkPermissionOperations(Permission permission, Contract
throws PermissionException {
ByteString operations = permission.getOperations();
if (operations.size() != 32) {
throw new PermissionException("operations size must 32");
throw new PermissionException(String.format("operations size must 32, actual: %d",
Comment thread
halibobo1205 marked this conversation as resolved.
operations.size()));
}
int contractType = contract.getTypeValue();
boolean b = (operations.byteAt(contractType / 8) & (1 << (contractType % 8))) != 0;
Expand Down
9 changes: 4 additions & 5 deletions chainbase/src/main/java/org/tron/core/ChainBaseManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -233,13 +233,13 @@ public class ChainBaseManager {
private DbStatService dbStatService;

public void closeOneStore(ITronChainBase database) {
logger.info("******** begin to close " + database.getName() + " ********");
logger.info("******** Begin to close {}. ********", database.getName());
try {
database.close();
} catch (Exception e) {
logger.info("failed to close " + database.getName() + ". " + e);
logger.info("Failed to close {}.", database.getName(), e);
Comment thread
halibobo1205 marked this conversation as resolved.
} finally {
logger.info("******** end to close " + database.getName() + " ********");
logger.info("******** End to close {}. ********", database.getName());
}
}

Expand Down Expand Up @@ -297,8 +297,7 @@ public BlockCapsule getHead() throws HeaderNotFound {
if (CollectionUtils.isNotEmpty(blocks)) {
return blocks.get(0);
} else {
logger.info("Header block Not Found");
throw new HeaderNotFound("Header block Not Found");
throw new HeaderNotFound("header block not found");
Comment thread
halibobo1205 marked this conversation as resolved.
}
}

Expand Down
5 changes: 3 additions & 2 deletions chainbase/src/main/java/org/tron/core/db/BlockIndexStore.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public BlockId get(Long num)
throws ItemNotFoundException {
BytesCapsule value = getUnchecked(ByteArray.fromLong(num));
if (value == null || value.getData() == null) {
throw new ItemNotFoundException("number: " + num + " is not found!");
throw new ItemNotFoundException(String.format("number: %d is not found!", num));
Comment thread
halibobo1205 marked this conversation as resolved.
}
return new BlockId(Sha256Hash.wrap(value.getData()), num);
}
Expand All @@ -39,7 +39,8 @@ public BytesCapsule get(byte[] key)
throws ItemNotFoundException {
byte[] value = revokingDB.getUnchecked(key);
if (ArrayUtils.isEmpty(value)) {
throw new ItemNotFoundException("number: " + Arrays.toString(key) + " is not found!");
throw new ItemNotFoundException(String.format("number: %d is not found!",
Comment thread
halibobo1205 marked this conversation as resolved.
ByteArray.toLong(key)));
}
return new BytesCapsule(value);
}
Expand Down
41 changes: 17 additions & 24 deletions chainbase/src/main/java/org/tron/core/db/BlockStore.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,15 @@

package org.tron.core.db;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
import java.util.Set;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.tron.common.error.TronDBException;
import org.tron.common.utils.Sha256Hash;
import org.tron.core.capsule.BlockCapsule;
import org.tron.core.capsule.BlockCapsule.BlockId;
Expand All @@ -39,31 +40,23 @@ private BlockStore(@Value("block") String dbName) {

public List<BlockCapsule> getLimitNumber(long startNumber, long limit) {
BlockId startBlockId = new BlockId(Sha256Hash.ZERO_HASH, startNumber);
return revokingDB.getValuesNext(startBlockId.getBytes(), limit).stream()
.map(bytes -> {
try {
return new BlockCapsule(bytes);
} catch (BadItemException ignored) {
}
return null;
})
.filter(Objects::nonNull)
.sorted(Comparator.comparing(BlockCapsule::getNum))
.collect(Collectors.toList());
return pack(revokingDB.getValuesNext(startBlockId.getBytes(), limit));
}

public List<BlockCapsule> getBlockByLatestNum(long getNum) {
return pack(revokingDB.getlatestValues(getNum));
}

return revokingDB.getlatestValues(getNum).stream()
.map(bytes -> {
try {
return new BlockCapsule(bytes);
} catch (BadItemException ignored) {
}
return null;
})
.filter(Objects::nonNull)
.sorted(Comparator.comparing(BlockCapsule::getNum))
.collect(Collectors.toList());
private List<BlockCapsule> pack(Set<byte[]> values) {
List<BlockCapsule> blocks = new ArrayList<>();
for (byte[] bytes : values) {
try {
blocks.add(new BlockCapsule(bytes));
} catch (BadItemException e) {
throw new TronDBException(e);
Comment thread
halibobo1205 marked this conversation as resolved.
}
}
blocks.sort(Comparator.comparing(BlockCapsule::getNum));
return blocks;
}
}
Loading