use of org.tron.core.capsule.TransactionCapsule 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.capsule.TransactionCapsule in project java-tron by tronprotocol.
the class Manager method eraseBlock.
/**
* when switch fork need erase blocks on fork branch.
*/
public void eraseBlock() {
dialog.reset();
BlockCapsule oldHeadBlock = getBlockStore().get(head.getBlockId().getBytes());
try {
revokingStore.pop();
head = getBlockStore().get(getBlockIdByNum(oldHeadBlock.getNum() - 1).getBytes());
} catch (RevokingStoreIllegalStateException e) {
logger.debug(e.getMessage(), e);
}
khaosDb.pop();
for (TransactionCapsule trx : oldHeadBlock.getTransactions()) {
popedTransactions.add(trx);
}
// todo process the trans in the poped block.
}
use of org.tron.core.capsule.TransactionCapsule in project java-tron by tronprotocol.
the class Manager method processBlock.
/**
* process block.
*/
public void processBlock(BlockCapsule block) throws ValidateSignatureException, ContractValidateException, ContractExeException {
for (TransactionCapsule transactionCapsule : block.getTransactions()) {
processTransaction(transactionCapsule);
}
// todo set reverking db max size.
this.updateDynamicProperties(block);
this.updateSignedWitness(block);
this.updateLatestSolidifiedBlock();
if (needMaintenance(block.getTimeStamp())) {
if (block.getNum() == 1) {
this.dynamicPropertiesStore.updateNextMaintenanceTime(block.getTimeStamp());
} else {
this.processMaintenance(block);
}
}
}
use of org.tron.core.capsule.TransactionCapsule in project java-tron by tronprotocol.
the class Wallet method broadcastTransaction.
/**
* Broadcast a transaction.
*/
public boolean broadcastTransaction(Transaction signaturedTransaction) {
TransactionCapsule trx = new TransactionCapsule(signaturedTransaction);
try {
if (trx.validateSignature()) {
Message message = new TransactionMessage(signaturedTransaction);
dbManager.pushTransactions(trx);
p2pnode.broadcast(message);
return true;
}
} 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 (Exception e) {
logger.debug(e.getMessage(), e);
}
return false;
}
use of org.tron.core.capsule.TransactionCapsule in project java-tron by tronprotocol.
the class ManagerTest method testPushTransactions.
@Test
public void testPushTransactions() {
TransactionCapsule transactionCapsule = new TransactionCapsule("2c0937534dd1b3832d05d865e8e6f2bf23218300b33a992740d45ccab7d4f519", 123);
try {
dbManager.pushTransactions(transactionCapsule);
} catch (Exception e) {
Assert.assertTrue("pushTransaction is error", false);
}
Assert.assertEquals("pushTransaction is error", 123, transactionCapsule.getInstance().getRawData().getVout(0).getValue());
}
Aggregations