Search in sources :

Example 1 with TransactionCapsule

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;
}
Also used : TransactionCapsule(org.tron.core.capsule.TransactionCapsule) ByteString(com.google.protobuf.ByteString) Dialog(org.tron.core.db.AbstractRevokingStore.Dialog) ContractValidateException(org.tron.core.exception.ContractValidateException) Iterator(java.util.Iterator) BlockCapsule(org.tron.core.capsule.BlockCapsule) ContractExeException(org.tron.core.exception.ContractExeException) RevokingStoreIllegalStateException(org.tron.core.exception.RevokingStoreIllegalStateException)

Example 2 with TransactionCapsule

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.
}
Also used : TransactionCapsule(org.tron.core.capsule.TransactionCapsule) BlockCapsule(org.tron.core.capsule.BlockCapsule) RevokingStoreIllegalStateException(org.tron.core.exception.RevokingStoreIllegalStateException)

Example 3 with TransactionCapsule

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);
        }
    }
}
Also used : TransactionCapsule(org.tron.core.capsule.TransactionCapsule)

Example 4 with TransactionCapsule

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;
}
Also used : TransactionCapsule(org.tron.core.capsule.TransactionCapsule) TransactionMessage(org.tron.core.net.message.TransactionMessage) NumberMessage(org.tron.api.GrpcAPI.NumberMessage) TransactionMessage(org.tron.core.net.message.TransactionMessage) Message(org.tron.common.overlay.message.Message) ValidateSignatureException(org.tron.core.exception.ValidateSignatureException) ContractValidateException(org.tron.core.exception.ContractValidateException) ContractValidateException(org.tron.core.exception.ContractValidateException) ContractExeException(org.tron.core.exception.ContractExeException) ValidateSignatureException(org.tron.core.exception.ValidateSignatureException) ContractExeException(org.tron.core.exception.ContractExeException)

Example 5 with TransactionCapsule

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());
}
Also used : TransactionCapsule(org.tron.core.capsule.TransactionCapsule) ContractValidateException(org.tron.core.exception.ContractValidateException) ContractExeException(org.tron.core.exception.ContractExeException) ValidateSignatureException(org.tron.core.exception.ValidateSignatureException) UnLinkedBlockException(org.tron.core.exception.UnLinkedBlockException) Test(org.junit.Test)

Aggregations

TransactionCapsule (org.tron.core.capsule.TransactionCapsule)7 ContractExeException (org.tron.core.exception.ContractExeException)3 ContractValidateException (org.tron.core.exception.ContractValidateException)3 Test (org.junit.Test)2 BlockCapsule (org.tron.core.capsule.BlockCapsule)2 RevokingStoreIllegalStateException (org.tron.core.exception.RevokingStoreIllegalStateException)2 ValidateSignatureException (org.tron.core.exception.ValidateSignatureException)2 ByteString (com.google.protobuf.ByteString)1 Iterator (java.util.Iterator)1 BeforeClass (org.junit.BeforeClass)1 NumberMessage (org.tron.api.GrpcAPI.NumberMessage)1 Message (org.tron.common.overlay.message.Message)1 Dialog (org.tron.core.db.AbstractRevokingStore.Dialog)1 UnLinkedBlockException (org.tron.core.exception.UnLinkedBlockException)1 TransactionMessage (org.tron.core.net.message.TransactionMessage)1