Search in sources :

Example 1 with Dialog

use of org.tron.core.db.AbstractRevokingStore.Dialog in project java-tron by tronprotocol.

the class Manager method pushTransactions.

/**
 * push transaction into db.
 */
public synchronized boolean pushTransactions(final TransactionCapsule trx) throws ValidateSignatureException, ContractValidateException, ContractExeException, HighFreqException {
    logger.info("push transaction");
    if (!trx.validateSignature()) {
        throw new ValidateSignatureException("trans sig validate failed");
    }
    validateFreq(trx);
    if (!dialog.valid()) {
        dialog = DialogOptional.of(revokingStore.buildDialog());
    }
    try (RevokingStore.Dialog tmpDialog = revokingStore.buildDialog()) {
        processTransaction(trx);
        pendingTransactions.add(trx);
        tmpDialog.merge();
    } catch (RevokingStoreIllegalStateException e) {
        logger.debug(e.getMessage(), e);
    }
    return true;
}
Also used : ValidateSignatureException(org.tron.core.exception.ValidateSignatureException) Dialog(org.tron.core.db.AbstractRevokingStore.Dialog) RevokingStoreIllegalStateException(org.tron.core.exception.RevokingStoreIllegalStateException)

Example 2 with Dialog

use of org.tron.core.db.AbstractRevokingStore.Dialog 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 3 with Dialog

use of org.tron.core.db.AbstractRevokingStore.Dialog 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);
    }
}
Also used : Dialog(org.tron.core.db.AbstractRevokingStore.Dialog) BlockCapsule(org.tron.core.capsule.BlockCapsule) BalanceInsufficientException(org.tron.core.exception.BalanceInsufficientException) HighFreqException(org.tron.core.exception.HighFreqException) ContractExeException(org.tron.core.exception.ContractExeException) ValidateSignatureException(org.tron.core.exception.ValidateSignatureException) ContractValidateException(org.tron.core.exception.ContractValidateException) RevokingStoreIllegalStateException(org.tron.core.exception.RevokingStoreIllegalStateException) UnLinkedBlockException(org.tron.core.exception.UnLinkedBlockException) RevokingStoreIllegalStateException(org.tron.core.exception.RevokingStoreIllegalStateException)

Example 4 with Dialog

use of org.tron.core.db.AbstractRevokingStore.Dialog 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;
    }
}
Also used : Dialog(org.tron.core.db.AbstractRevokingStore.Dialog) ValidateSignatureException(org.tron.core.exception.ValidateSignatureException) ContractValidateException(org.tron.core.exception.ContractValidateException) BlockCapsule(org.tron.core.capsule.BlockCapsule) LinkedList(java.util.LinkedList) ContractExeException(org.tron.core.exception.ContractExeException) RevokingStoreIllegalStateException(org.tron.core.exception.RevokingStoreIllegalStateException)

Example 5 with Dialog

use of org.tron.core.db.AbstractRevokingStore.Dialog in project java-tron by tronprotocol.

the class RevokingStoreTest method testPop.

@Test
public synchronized void testPop() {
    revokingDatabase.getStack().clear();
    TestRevokingTronStore tronDatabase = new TestRevokingTronStore("testrevokingtronstore-testPop", revokingDatabase);
    TestProtoCapsule testProtoCapsule = new TestProtoCapsule();
    IntStream.rangeClosed(1, 10).forEach(i -> {
        try (Dialog tmpDialog = revokingDatabase.buildDialog()) {
            tronDatabase.put(testProtoCapsule.getData(), testProtoCapsule);
            Assert.assertFalse(tronDatabase.getDbSource().allKeys().isEmpty());
            Assert.assertEquals(revokingDatabase.getActiveDialog(), 1);
            tmpDialog.commit();
            Assert.assertEquals(revokingDatabase.getStack().size(), i);
            Assert.assertEquals(revokingDatabase.getActiveDialog(), 0);
        } catch (RevokingStoreIllegalStateException e) {
            logger.debug(e.getMessage(), e);
        }
    });
    try {
        revokingDatabase.pop();
    } catch (RevokingStoreIllegalStateException e) {
        logger.debug(e.getMessage(), e);
    }
    Assert.assertTrue(tronDatabase.getDbSource().allKeys().isEmpty());
    Assert.assertEquals(revokingDatabase.getStack().size(), 9);
    tronDatabase.close();
}
Also used : Dialog(org.tron.core.db.AbstractRevokingStore.Dialog) RevokingStoreIllegalStateException(org.tron.core.exception.RevokingStoreIllegalStateException) Test(org.junit.Test)

Aggregations

Dialog (org.tron.core.db.AbstractRevokingStore.Dialog)6 RevokingStoreIllegalStateException (org.tron.core.exception.RevokingStoreIllegalStateException)6 BlockCapsule (org.tron.core.capsule.BlockCapsule)3 ContractExeException (org.tron.core.exception.ContractExeException)3 ContractValidateException (org.tron.core.exception.ContractValidateException)3 ValidateSignatureException (org.tron.core.exception.ValidateSignatureException)3 Test (org.junit.Test)2 ByteString (com.google.protobuf.ByteString)1 Iterator (java.util.Iterator)1 LinkedList (java.util.LinkedList)1 DialogOptional (org.tron.common.utils.DialogOptional)1 TransactionCapsule (org.tron.core.capsule.TransactionCapsule)1 BalanceInsufficientException (org.tron.core.exception.BalanceInsufficientException)1 HighFreqException (org.tron.core.exception.HighFreqException)1 UnLinkedBlockException (org.tron.core.exception.UnLinkedBlockException)1