Search in sources :

Example 1 with TronException

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

the class WitnessService method tryProduceBlock.

/**
 * Generate and broadcast blocks
 */
private BlockProductionCondition tryProduceBlock() throws InterruptedException {
    long now = DateTime.now().getMillis() + 50L;
    if (this.needSyncCheck) {
        long nexSlotTime = db.getSlotTime(1);
        if (nexSlotTime > now) {
            // check sync during first loop
            needSyncCheck = false;
            // Processing Time Drift later
            Thread.sleep(nexSlotTime - now);
            now = DateTime.now().getMillis();
        } else {
            logger.debug("Not sync ,now:{},headBlockTime:{},headBlockNumber:{},headBlockId:{}", new DateTime(now), new DateTime(db.getHead().getTimeStamp()), db.getHead().getNum(), db.getHead().getBlockId());
            return BlockProductionCondition.NOT_SYNCED;
        }
    }
    // if (db.isSyncMode()) {
    // return BlockProductionCondition.NOT_SYNCED;
    // }
    final int participation = this.db.calculateParticipationRate();
    if (participation < MIN_PARTICIPATION_RATE) {
        logger.warn("Participation[" + participation + "] <  MIN_PARTICIPATION_RATE[" + MIN_PARTICIPATION_RATE + "]");
        return BlockProductionCondition.LOW_PARTICIPATION;
    }
    long slot = db.getSlotAtTime(now);
    logger.debug("Slot:" + slot);
    if (slot == 0) {
        logger.info("Not time yet,now:{},headBlockTime:{},headBlockNumber:{},headBlockId:{}", new DateTime(now), new DateTime(db.getHead().getTimeStamp()), db.getHead().getNum(), db.getHead().getBlockId());
        return BlockProductionCondition.NOT_TIME_YET;
    }
    final ByteString scheduledWitness = db.getScheduledWitness(slot);
    if (!this.getLocalWitnessStateMap().containsKey(scheduledWitness)) {
        logger.info("It's not my turn,ScheduledWitness[{}],slot[{}],abSlot[{}],", ByteArray.toHexString(scheduledWitness.toByteArray()), slot, db.getAbSlotAtTime(now));
        logger.debug("headBlockNumber:{},headBlockId:{},headBlockTime:{}", db.getHead().getNum(), db.getHead().getBlockId(), new DateTime(db.getHead().getTimeStamp()));
        return BlockProductionCondition.NOT_MY_TURN;
    }
    long scheduledTime = db.getSlotTime(slot);
    if (scheduledTime - now > PRODUCE_TIME_OUT) {
        return BlockProductionCondition.LAG;
    }
    if (!privateKeyMap.containsKey(scheduledWitness)) {
        return BlockProductionCondition.NO_PRIVATE_KEY;
    }
    try {
        BlockCapsule block = generateBlock(scheduledTime, scheduledWitness);
        logger.info("Produce block successfully, blockNumber:{},abSlot[{}],blockId:{}, blockTime:{}, parentBlockId:{}", block.getNum(), db.getAbSlotAtTime(now), block.getBlockId(), new DateTime(block.getTimeStamp()), db.getHead().getBlockId());
        broadcastBlock(block);
        return BlockProductionCondition.PRODUCED;
    } catch (TronException e) {
        logger.debug(e.getMessage(), e);
        return BlockProductionCondition.EXCEPTION_PRODUCING_BLOCK;
    }
}
Also used : TronException(org.tron.core.exception.TronException) ByteString(com.google.protobuf.ByteString) BlockCapsule(org.tron.core.capsule.BlockCapsule) DateTime(org.joda.time.DateTime)

Aggregations

ByteString (com.google.protobuf.ByteString)1 DateTime (org.joda.time.DateTime)1 BlockCapsule (org.tron.core.capsule.BlockCapsule)1 TronException (org.tron.core.exception.TronException)1