Search in sources :

Example 1 with ValidateSignatureException

use of org.tron.core.exception.ValidateSignatureException 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 ValidateSignatureException

use of org.tron.core.exception.ValidateSignatureException 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 3 with ValidateSignatureException

use of org.tron.core.exception.ValidateSignatureException 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 4 with ValidateSignatureException

use of org.tron.core.exception.ValidateSignatureException 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 ValidateSignatureException

use of org.tron.core.exception.ValidateSignatureException in project java-tron by tronprotocol.

the class ManagerTest method fork.

@Test
public void fork() {
    Args.setParam(new String[] { "--witness" }, Configuration.getByPath(Constant.NORMAL_CONF));
    long size = dbManager.getBlockStore().dbSource.allKeys().size();
    String key = "00f31db24bfbd1a2ef19beddca0a0fa37632eded9ac666a05d3bd925f01dde1f62";
    byte[] privateKey = ByteArray.fromHexString(key);
    final ECKey ecKey = ECKey.fromPrivate(privateKey);
    byte[] address = ecKey.getAddress();
    WitnessCapsule witnessCapsule = new WitnessCapsule(ByteString.copyFrom(address));
    dbManager.addWitness(witnessCapsule);
    dbManager.addWitness(witnessCapsule);
    dbManager.addWitness(witnessCapsule);
    IntStream.range(0, 5).forEach(i -> {
        try {
            dbManager.generateBlock(witnessCapsule, System.currentTimeMillis(), privateKey);
        } catch (ValidateSignatureException | ContractValidateException | ContractExeException | UnLinkedBlockException e) {
            logger.debug(e.getMessage(), e);
        }
    });
    try {
        long num = dbManager.getDynamicPropertiesStore().getLatestBlockHeaderNumber();
        BlockCapsule blockCapsule1 = new BlockCapsule(num, dbManager.getHead().getParentHash().getByteString(), System.currentTimeMillis(), witnessCapsule.getAddress());
        blockCapsule1.generatedByMyself = true;
        BlockCapsule blockCapsule2 = new BlockCapsule(num + 1, blockCapsule1.getBlockId().getByteString(), System.currentTimeMillis(), witnessCapsule.getAddress());
        blockCapsule2.generatedByMyself = true;
        logger.error("******1*******" + "block1 id:" + blockCapsule1.getBlockId());
        logger.error("******2*******" + "block2 id:" + blockCapsule2.getBlockId());
        dbManager.pushBlock(blockCapsule1);
        dbManager.pushBlock(blockCapsule2);
        logger.error("******in blockStore block size:" + dbManager.getBlockStore().dbSource.allKeys().size());
        logger.error("******in blockStore block:" + dbManager.getBlockStore().dbSource.allKeys().stream().map(ByteArray::toHexString).collect(Collectors.toList()));
        Assert.assertNotNull(dbManager.getBlockStore().get(blockCapsule1.getBlockId().getBytes()));
        Assert.assertNotNull(dbManager.getBlockStore().get(blockCapsule2.getBlockId().getBytes()));
        Assert.assertEquals(dbManager.getBlockStore().get(blockCapsule2.getBlockId().getBytes()).getParentHash(), blockCapsule1.getBlockId());
        Assert.assertEquals(dbManager.getBlockStore().dbSource.allKeys().size(), size + 6);
        Assert.assertEquals(dbManager.getBlockIdByNum(dbManager.getHead().getNum() - 1), blockCapsule1.getBlockId());
        Assert.assertEquals(dbManager.getBlockIdByNum(dbManager.getHead().getNum() - 2), blockCapsule1.getParentHash());
        Assert.assertEquals(blockCapsule2.getBlockId().getByteString(), dbManager.getDynamicPropertiesStore().getLatestBlockHeaderHash());
        Assert.assertEquals(dbManager.getHead().getBlockId().getByteString(), dbManager.getDynamicPropertiesStore().getLatestBlockHeaderHash());
    } catch (ValidateSignatureException | ContractValidateException | ContractExeException | UnLinkedBlockException e) {
        logger.debug(e.getMessage(), e);
    }
    dbManager.getWitnesses().clear();
}
Also used : WitnessCapsule(org.tron.core.capsule.WitnessCapsule) ValidateSignatureException(org.tron.core.exception.ValidateSignatureException) ContractValidateException(org.tron.core.exception.ContractValidateException) ECKey(org.tron.common.crypto.ECKey) ByteString(com.google.protobuf.ByteString) UnLinkedBlockException(org.tron.core.exception.UnLinkedBlockException) BlockCapsule(org.tron.core.capsule.BlockCapsule) ContractExeException(org.tron.core.exception.ContractExeException) Test(org.junit.Test)

Aggregations

ValidateSignatureException (org.tron.core.exception.ValidateSignatureException)6 ContractExeException (org.tron.core.exception.ContractExeException)4 ContractValidateException (org.tron.core.exception.ContractValidateException)4 BlockCapsule (org.tron.core.capsule.BlockCapsule)3 Dialog (org.tron.core.db.AbstractRevokingStore.Dialog)3 RevokingStoreIllegalStateException (org.tron.core.exception.RevokingStoreIllegalStateException)3 UnLinkedBlockException (org.tron.core.exception.UnLinkedBlockException)2 ByteString (com.google.protobuf.ByteString)1 SignatureException (java.security.SignatureException)1 LinkedList (java.util.LinkedList)1 Test (org.junit.Test)1 NumberMessage (org.tron.api.GrpcAPI.NumberMessage)1 ECKey (org.tron.common.crypto.ECKey)1 Message (org.tron.common.overlay.message.Message)1 TransactionCapsule (org.tron.core.capsule.TransactionCapsule)1 WitnessCapsule (org.tron.core.capsule.WitnessCapsule)1 BalanceInsufficientException (org.tron.core.exception.BalanceInsufficientException)1 HighFreqException (org.tron.core.exception.HighFreqException)1 TransactionMessage (org.tron.core.net.message.TransactionMessage)1 AccountCreateContract (org.tron.protos.Contract.AccountCreateContract)1