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()));
}
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;
}
}
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();
}
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;
}
Aggregations