use of org.tron.core.exception.UnLinkedBlockException 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);
}
}
use of org.tron.core.exception.UnLinkedBlockException in project java-tron by tronprotocol.
the class KhaosDatabaseTest method testPushGetBlock.
@Test
public void testPushGetBlock() {
BlockCapsule blockCapsule = new BlockCapsule(Block.newBuilder().setBlockHeader(BlockHeader.newBuilder().setRawData(raw.newBuilder().setParentHash(ByteString.copyFrom(ByteArray.fromHexString("0304f784e4e7bae517bcab94c3e0c9214fb4ac7ff9d7d5a937d1f40031f87b81"))))).build());
BlockCapsule blockCapsule2 = new BlockCapsule(Block.newBuilder().setBlockHeader(BlockHeader.newBuilder().setRawData(raw.newBuilder().setParentHash(ByteString.copyFrom(ByteArray.fromHexString("9938a342238077182498b464ac029222ae169360e540d1fd6aee7c2ae9575a06"))))).build());
khaosDatabase.start(blockCapsule);
try {
khaosDatabase.push(blockCapsule2);
} catch (UnLinkedBlockException e) {
}
Assert.assertEquals(blockCapsule2, khaosDatabase.getBlock(blockCapsule2.getBlockId()));
Assert.assertTrue("conatain is error", khaosDatabase.containBlock(blockCapsule2.getBlockId()));
khaosDatabase.removeBlk(blockCapsule2.getBlockId());
Assert.assertNull("removeBlk is error", khaosDatabase.getBlock(blockCapsule2.getBlockId()));
}
use of org.tron.core.exception.UnLinkedBlockException 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();
}
use of org.tron.core.exception.UnLinkedBlockException in project java-tron by tronprotocol.
the class NodeImpl method processAdvBlock.
private void processAdvBlock(PeerConnection peer, BlockCapsule block) {
// TODO: lack the complete flow.
if (!freshBlockId.contains(block.getBlockId())) {
try {
LinkedList<Sha256Hash> trxIds = del.handleBlock(block, false);
freshBlockId.offer(block.getBlockId());
trxIds.forEach(trxId -> advObjToFetch.remove(trxId));
// TODO:save message cache again.
getActivePeer().stream().filter(p -> p.getAdvObjSpreadToUs().containsKey(block.getBlockId())).forEach(p -> {
p.setHeadBlockWeBothHave(block.getBlockId());
p.setHeadBlockTimeWeBothHave(block.getTimeStamp());
});
getActivePeer().forEach(p -> p.cleanInvGarbage());
// rebroadcast
broadcast(new BlockMessage(block));
} catch (BadBlockException e) {
badAdvObj.put(block.getBlockId(), System.currentTimeMillis());
} catch (UnLinkedBlockException e) {
// reSync
startSyncWithPeer(peer);
}
}
}
Aggregations