use of org.tron.core.exception.ContractExeException in project java-tron by tronprotocol.
the class Manager method generateBlock.
/**
* Generate a block.
*/
public synchronized BlockCapsule generateBlock(final WitnessCapsule witnessCapsule, final long when, final byte[] privateKey) throws ValidateSignatureException, ContractValidateException, ContractExeException, UnLinkedBlockException {
final long timestamp = this.dynamicPropertiesStore.getLatestBlockHeaderTimestamp();
final long number = this.dynamicPropertiesStore.getLatestBlockHeaderNumber();
final ByteString preHash = this.dynamicPropertiesStore.getLatestBlockHeaderHash();
// judge create block time
if (when < timestamp) {
throw new IllegalArgumentException("generate block timestamp is invalid.");
}
long currentTrxSize = 0;
long postponedTrxCount = 0;
final BlockCapsule blockCapsule = new BlockCapsule(number + 1, preHash, when, witnessCapsule.getAddress());
dialog.reset();
dialog = DialogOptional.of(revokingStore.buildDialog());
Iterator iterator = pendingTransactions.iterator();
while (iterator.hasNext()) {
TransactionCapsule trx = (TransactionCapsule) iterator.next();
currentTrxSize += RamUsageEstimator.sizeOf(trx);
// judge block size
if (currentTrxSize > TRXS_SIZE) {
postponedTrxCount++;
continue;
}
// apply transaction
try (Dialog tmpDialog = revokingStore.buildDialog()) {
processTransaction(trx);
tmpDialog.merge();
// push into block
blockCapsule.addTransaction(trx);
iterator.remove();
} catch (ContractExeException e) {
logger.info("contract not processed during execute");
logger.debug(e.getMessage(), e);
} catch (ContractValidateException e) {
logger.info("contract not processed during validate");
logger.debug(e.getMessage(), e);
} catch (RevokingStoreIllegalStateException e) {
logger.debug(e.getMessage(), e);
}
}
dialog.reset();
if (postponedTrxCount > 0) {
logger.info("{} transactions over the block size limit", postponedTrxCount);
}
logger.info("postponedTrxCount[" + postponedTrxCount + "],TrxLeft[" + pendingTransactions.size() + "]");
blockCapsule.setMerkleRoot();
blockCapsule.sign(privateKey);
blockCapsule.generatedByMyself = true;
this.pushBlock(blockCapsule);
return blockCapsule;
}
use of org.tron.core.exception.ContractExeException in project java-tron by tronprotocol.
the class Manager method pushBlock.
/**
* save a block.
*/
public void pushBlock(final BlockCapsule block) throws ValidateSignatureException, ContractValidateException, ContractExeException, UnLinkedBlockException {
try (PendingManager pm = new PendingManager(this)) {
// todo: check block's validity
if (!block.generatedByMyself) {
if (!block.validateSignature()) {
logger.info("The siganature is not validated.");
// TODO: throw exception here.
return;
}
if (!block.calcMerkleRoot().equals(block.getMerkleRoot())) {
logger.info("The merkler root doesn't match, Calc result is " + block.calcMerkleRoot() + " , the headers is " + block.getMerkleRoot());
// TODO:throw exception here.
return;
}
}
try {
// direct return ,need test
validateWitnessSchedule(block);
} catch (Exception ex) {
logger.error("validateWitnessSchedule error", ex);
}
BlockCapsule newBlock = this.khaosDb.push(block);
// DB don't need lower block
if (head == null) {
if (newBlock.getNum() != 0) {
return;
}
} else {
if (newBlock.getNum() <= head.getNum()) {
return;
}
// switch fork
if (!newBlock.getParentHash().equals(head.getBlockId())) {
switchFork(newBlock);
}
try (Dialog tmpDialog = revokingStore.buildDialog()) {
this.processBlock(newBlock);
tmpDialog.commit();
} catch (RevokingStoreIllegalStateException e) {
logger.debug(e.getMessage(), e);
}
}
blockStore.put(block.getBlockId().getBytes(), block);
this.numHashCache.putData(ByteArray.fromLong(block.getNum()), block.getBlockId().getBytes());
// refreshHead(newBlock);
logger.info("save block: " + newBlock);
}
}
use of org.tron.core.exception.ContractExeException in project java-tron by tronprotocol.
the class Manager method switchFork.
private void switchFork(BlockCapsule newHead) {
Pair<LinkedList<BlockCapsule>, LinkedList<BlockCapsule>> binaryTree = khaosDb.getBranch(newHead.getBlockId(), head.getBlockId());
if (CollectionUtils.isNotEmpty(binaryTree.getValue())) {
while (!head.getBlockId().equals(binaryTree.getValue().peekLast().getParentHash())) {
eraseBlock();
}
}
if (CollectionUtils.isNotEmpty(binaryTree.getKey())) {
LinkedList<BlockCapsule> branch = binaryTree.getKey();
Collections.reverse(branch);
branch.forEach(item -> {
// todo process the exception carefully later
try (Dialog tmpDialog = revokingStore.buildDialog()) {
processBlock(item);
blockStore.put(item.getBlockId().getBytes(), item);
this.numHashCache.putData(ByteArray.fromLong(item.getNum()), item.getBlockId().getBytes());
tmpDialog.commit();
head = item;
} catch (ValidateSignatureException e) {
logger.debug(e.getMessage(), e);
} catch (ContractValidateException e) {
logger.debug(e.getMessage(), e);
} catch (ContractExeException e) {
logger.debug(e.getMessage(), e);
} catch (RevokingStoreIllegalStateException e) {
logger.debug(e.getMessage(), e);
}
});
return;
}
}
use of org.tron.core.exception.ContractExeException in project java-tron by tronprotocol.
the class TransferAssetActuator method execute.
@Override
public boolean execute(TransactionResultCapsule ret) throws ContractExeException {
long fee = calcFee();
if (!this.contract.is(TransferAssetContract.class)) {
throw new ContractExeException();
}
if (this.dbManager == null) {
throw new ContractExeException();
}
try {
TransferAssetContract transferAssetContract = this.contract.unpack(TransferAssetContract.class);
AccountStore accountStore = this.dbManager.getAccountStore();
byte[] ownerKey = transferAssetContract.getOwnerAddress().toByteArray();
byte[] toKey = transferAssetContract.getToAddress().toByteArray();
ByteString assertName = transferAssetContract.getAssetName();
long amount = transferAssetContract.getAmount();
AccountCapsule ownerAccountCapsule = accountStore.get(ownerKey);
if (!ownerAccountCapsule.reduceAssetAmount(assertName, amount)) {
throw new ContractExeException("reduceAssetAmount failed !");
}
accountStore.put(ownerKey, ownerAccountCapsule);
AccountCapsule toAccountCapsule = accountStore.get(toKey);
toAccountCapsule.addAssetAmount(assertName, amount);
accountStore.put(toKey, toAccountCapsule);
ret.setStatus(fee, code.SUCESS);
} catch (InvalidProtocolBufferException e) {
ret.setStatus(fee, code.FAILED);
throw new ContractExeException();
}
return true;
}
use of org.tron.core.exception.ContractExeException in project java-tron by tronprotocol.
the class WitnessCreateActuator method execute.
@Override
public boolean execute(TransactionResultCapsule ret) throws ContractExeException {
long fee = calcFee();
try {
final WitnessCreateContract witnessCreateContract = this.contract.unpack(WitnessCreateContract.class);
this.createWitness(witnessCreateContract);
ret.setStatus(fee, code.SUCESS);
} catch (final InvalidProtocolBufferException e) {
logger.debug(e.getMessage(), e);
ret.setStatus(fee, code.FAILED);
throw new ContractExeException(e.getMessage());
}
return true;
}
Aggregations