Skip to content

Commit

Permalink
Merge pull request #4714 from lxcmyf/feature/new_unfreeze
Browse files Browse the repository at this point in the history
feat(freezeV2): optimize usage merging
  • Loading branch information
halibobo1205 authored Oct 18, 2022
2 parents 899bf29 + 8077c22 commit c7ab5a2
Show file tree
Hide file tree
Showing 105 changed files with 1,940 additions and 1,070 deletions.
83 changes: 0 additions & 83 deletions .github/workflows/docker-publish.yml

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import lombok.extern.slf4j.Slf4j;
import org.tron.common.utils.Commons;
import org.tron.common.utils.DecodeUtil;
import org.tron.common.utils.StringUtil;
import org.tron.core.capsule.AccountCapsule;
import org.tron.core.capsule.ContractCapsule;
import org.tron.core.capsule.TransactionResultCapsule;
Expand Down Expand Up @@ -156,6 +157,8 @@ public boolean validate() throws ContractValidateException {
}

if (balance < Math.addExact(amount, fee)) {
logger.warn("Balance is not sufficient. Account: {}, balance: {}, amount: {}, fee: {}.",
StringUtil.encode58Check(ownerAddress), balance, amount, fee);
throw new ContractValidateException(
"Validate TransferContract error, balance is not sufficient.");
}
Expand Down
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 @@ -133,12 +135,16 @@ private void openDatabase(Options dbOptions) throws IOException {
}
try {
database = factory.open(dbPath.toFile(), dbOptions);
logger.info("DB {} open success with : writeBufferSize {}M,cacheSize {}M,maxOpenFiles {}.",
this.getDBName(), dbOptions.writeBufferSize() / 1024 / 1024,
dbOptions.cacheSize() / 1024 / 1024, dbOptions.maxOpenFiles());
if (!this.getDBName().startsWith("checkpoint")) {
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());
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 +466,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);
} 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));
}
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);
}

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);
}

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);
} 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);
} 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);
} 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);
}
} 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);
}
} 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",
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",
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));
}
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",
operations.size()));
}
int contractType = contract.getTypeValue();
boolean b = (operations.byteAt(contractType / 8) & (1 << (contractType % 8))) != 0;
Expand Down
Loading

0 comments on commit c7ab5a2

Please sign in to comment.