Search in sources :

Example 11 with BlockCapsule

use of org.tron.core.capsule.BlockCapsule in project java-tron by tronprotocol.

the class ManagerTest method init.

@BeforeClass
public static void init() {
    Args.setParam(new String[] { "-d", dbPath, "-w" }, Configuration.getByPath(Constant.TEST_CONF));
    dbManager.init();
    blockCapsule2 = new BlockCapsule(1, ByteString.copyFrom(ByteArray.fromHexString("0304f784e4e7bae517bcab94c3e0c9214fb4ac7ff9d7d5a937d1f40031f87b81")), 0, ByteString.copyFrom(ECKey.fromPrivate(ByteArray.fromHexString(Args.getInstance().getLocalWitnesses().getPrivateKey())).getAddress()));
    blockCapsule2.setMerkleRoot();
    blockCapsule2.sign(ByteArray.fromHexString(Args.getInstance().getLocalWitnesses().getPrivateKey()));
}
Also used : BlockCapsule(org.tron.core.capsule.BlockCapsule) BeforeClass(org.junit.BeforeClass)

Example 12 with BlockCapsule

use of org.tron.core.capsule.BlockCapsule 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)

Example 13 with BlockCapsule

use of org.tron.core.capsule.BlockCapsule in project java-tron by tronprotocol.

the class Manager method deleteBlock.

/**
 * Delete a block.
 */
public void deleteBlock(final Sha256Hash blockHash) {
    final BlockCapsule block = this.getBlockById(blockHash);
    this.khaosDb.removeBlk(blockHash);
    blockStore.delete(blockHash.getBytes());
    this.numHashCache.deleteData(ByteArray.fromLong(block.getNum()));
    this.head = this.khaosDb.getHead();
}
Also used : BlockCapsule(org.tron.core.capsule.BlockCapsule)

Example 14 with BlockCapsule

use of org.tron.core.capsule.BlockCapsule in project java-tron by tronprotocol.

the class BlockUtil method newGenesisBlockCapsule.

/**
 * create genesis block from transactions.
 */
public static BlockCapsule newGenesisBlockCapsule() {
    Args args = Args.getInstance();
    GenesisBlock genesisBlockArg = args.getGenesisBlock();
    List<Transaction> transactionList = genesisBlockArg.getAssets().stream().map(key -> {
        String address = key.getAddress();
        long balance = key.getBalance();
        return TransactionUtil.newGenesisTransaction(address, balance);
    }).collect(Collectors.toList());
    long timestamp = Long.parseLong(genesisBlockArg.getTimestamp());
    ByteString parentHash = ByteString.copyFrom(ByteArray.fromHexString(genesisBlockArg.getParentHash()));
    long number = Long.parseLong(genesisBlockArg.getNumber());
    BlockCapsule blockCapsule = new BlockCapsule(timestamp, parentHash, number, transactionList);
    blockCapsule.setMerkleRoot();
    blockCapsule.generatedByMyself = true;
    return blockCapsule;
}
Also used : GenesisBlock(org.tron.core.config.args.GenesisBlock) ByteString(com.google.protobuf.ByteString) Transaction(org.tron.protos.Protocol.Transaction) List(java.util.List) GenesisBlock(org.tron.core.config.args.GenesisBlock) Args(org.tron.core.config.args.Args) ByteArray(org.tron.common.utils.ByteArray) BlockCapsule(org.tron.core.capsule.BlockCapsule) Collectors(java.util.stream.Collectors) Args(org.tron.core.config.args.Args) Transaction(org.tron.protos.Protocol.Transaction) ByteString(com.google.protobuf.ByteString) ByteString(com.google.protobuf.ByteString) BlockCapsule(org.tron.core.capsule.BlockCapsule)

Aggregations

BlockCapsule (org.tron.core.capsule.BlockCapsule)14 ByteString (com.google.protobuf.ByteString)4 Test (org.junit.Test)4 ContractExeException (org.tron.core.exception.ContractExeException)4 ContractValidateException (org.tron.core.exception.ContractValidateException)4 RevokingStoreIllegalStateException (org.tron.core.exception.RevokingStoreIllegalStateException)4 Dialog (org.tron.core.db.AbstractRevokingStore.Dialog)3 UnLinkedBlockException (org.tron.core.exception.UnLinkedBlockException)3 ValidateSignatureException (org.tron.core.exception.ValidateSignatureException)3 LinkedList (java.util.LinkedList)2 List (java.util.List)2 Message (org.tron.common.overlay.message.Message)2 Sha256Hash (org.tron.common.utils.Sha256Hash)2 TransactionCapsule (org.tron.core.capsule.TransactionCapsule)2 TronException (org.tron.core.exception.TronException)2 BlockInventoryMessage (org.tron.core.net.message.BlockInventoryMessage)2 BlockMessage (org.tron.core.net.message.BlockMessage)2 ChainInventoryMessage (org.tron.core.net.message.ChainInventoryMessage)2 FetchInvDataMessage (org.tron.core.net.message.FetchInvDataMessage)2 InventoryMessage (org.tron.core.net.message.InventoryMessage)2