Search in sources :

Example 6 with BlockCapsule

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

Example 7 with BlockCapsule

use of org.tron.core.capsule.BlockCapsule 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);
        }
    }
}
Also used : Autowired(org.springframework.beans.factory.annotation.Autowired) BlockCapsule(org.tron.core.capsule.BlockCapsule) ConcurrentSet(io.netty.util.internal.ConcurrentSet) BlockId(org.tron.core.capsule.BlockCapsule.BlockId) NodeHandler(org.tron.common.overlay.discover.NodeHandler) TransactionMessage(org.tron.core.net.message.TransactionMessage) NodeConstant(org.tron.core.config.Parameter.NodeConstant) Pair(javafx.util.Pair) InventoryMessage(org.tron.core.net.message.InventoryMessage) Collection(java.util.Collection) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Set(java.util.Set) MessageTypes(org.tron.core.net.message.MessageTypes) UnReachBlockException(org.tron.core.exception.UnReachBlockException) ExecutorLoop(org.tron.common.utils.ExecutorLoop) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) Executors(java.util.concurrent.Executors) List(java.util.List) Slf4j(lombok.extern.slf4j.Slf4j) Entry(java.util.Map.Entry) Queue(java.util.Queue) TronException(org.tron.core.exception.TronException) BlockConstant(org.tron.core.config.Parameter.BlockConstant) BadTransactionException(org.tron.core.exception.BadTransactionException) Iterables(com.google.common.collect.Iterables) SyncBlockChainMessage(org.tron.core.net.message.SyncBlockChainMessage) ItemNotFound(org.tron.core.net.message.ItemNotFound) HashMap(java.util.HashMap) Deque(java.util.Deque) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) TronState(org.tron.common.overlay.server.Channel.TronState) PeerConnectionDelegate(org.tron.core.net.peer.PeerConnectionDelegate) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) BadBlockException(org.tron.core.exception.BadBlockException) LinkedList(java.util.LinkedList) Sha256Hash(org.tron.common.utils.Sha256Hash) BlockMessage(org.tron.core.net.message.BlockMessage) Message(org.tron.common.overlay.message.Message) BlockInventoryMessage(org.tron.core.net.message.BlockInventoryMessage) InventoryType(org.tron.protos.Protocol.Inventory.InventoryType) NetConstants(org.tron.core.config.Parameter.NetConstants) SyncPool(org.tron.common.overlay.server.SyncPool) FetchInvDataMessage(org.tron.core.net.message.FetchInvDataMessage) TraitorPeerException(org.tron.core.exception.TraitorPeerException) ChainInventoryMessage(org.tron.core.net.message.ChainInventoryMessage) TimeUnit(java.util.concurrent.TimeUnit) TronMessage(org.tron.core.net.message.TronMessage) PeerConnection(org.tron.core.net.peer.PeerConnection) Component(org.springframework.stereotype.Component) ReasonCode(org.tron.common.overlay.message.ReasonCode) Time(org.tron.common.utils.Time) UnLinkedBlockException(org.tron.core.exception.UnLinkedBlockException) BadBlockException(org.tron.core.exception.BadBlockException) BlockMessage(org.tron.core.net.message.BlockMessage) Sha256Hash(org.tron.common.utils.Sha256Hash) UnLinkedBlockException(org.tron.core.exception.UnLinkedBlockException)

Example 8 with BlockCapsule

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

the class NodeImpl method onHandleFetchDataMessage.

private void onHandleFetchDataMessage(PeerConnection peer, FetchInvDataMessage fetchInvDataMsg) {
    logger.info("on handle fetch block message");
    MessageTypes type = fetchInvDataMsg.getInvMessageType();
    // TODO:maybe can use message cache here
    final BlockCapsule[] blocks = { del.getGenesisBlock() };
    // get data and send it one by one
    fetchInvDataMsg.getHashList().forEach(hash -> {
        if (del.contain(hash, type)) {
            Message msg = del.getData(hash, type);
            if (type.equals(MessageTypes.BLOCK)) {
                blocks[0] = ((BlockMessage) msg).getBlockCapsule();
            }
            peer.sendMessage(msg);
        } else {
            peer.sendMessage(new ItemNotFound());
        }
    });
    if (blocks[0] != null) {
        peer.setHeadBlockWeBothHave(blocks[0].getBlockId());
        peer.setHeadBlockTimeWeBothHave(blocks[0].getTimeStamp());
    }
}
Also used : TransactionMessage(org.tron.core.net.message.TransactionMessage) InventoryMessage(org.tron.core.net.message.InventoryMessage) SyncBlockChainMessage(org.tron.core.net.message.SyncBlockChainMessage) BlockMessage(org.tron.core.net.message.BlockMessage) Message(org.tron.common.overlay.message.Message) BlockInventoryMessage(org.tron.core.net.message.BlockInventoryMessage) FetchInvDataMessage(org.tron.core.net.message.FetchInvDataMessage) ChainInventoryMessage(org.tron.core.net.message.ChainInventoryMessage) TronMessage(org.tron.core.net.message.TronMessage) ItemNotFound(org.tron.core.net.message.ItemNotFound) MessageTypes(org.tron.core.net.message.MessageTypes) BlockCapsule(org.tron.core.capsule.BlockCapsule)

Example 9 with BlockCapsule

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

the class BlockUtilTest method testBlockUtil.

@Test
public void testBlockUtil() {
    // test create GenesisBlockCapsule
    BlockCapsule blockCapsule1 = BlockUtil.newGenesisBlockCapsule();
    Sha256Hash sha256Hash = Sha256Hash.wrap(ByteArray.fromHexString("0x0000000000000000000000000000000000000000000000000000000000000000"));
    Assert.assertEquals(0, blockCapsule1.getTimeStamp());
    Assert.assertEquals(sha256Hash, blockCapsule1.getParentHash());
    Assert.assertEquals(0, blockCapsule1.getNum());
    // test isParentOf method: create blockCapsule2 and blockCapsule3
    // blockCapsule3.setParentHash() equals blockCapsule2.getBlockId
    BlockCapsule blockCapsule2 = new BlockCapsule(Block.newBuilder().setBlockHeader(BlockHeader.newBuilder().setRawData(raw.newBuilder().setParentHash(ByteString.copyFrom(ByteArray.fromHexString("0304f784e4e7bae517bcab94c3e0c9214fb4ac7ff9d7d5a937d1f40031f87b81"))))).build());
    BlockCapsule blockCapsule3 = new BlockCapsule(Block.newBuilder().setBlockHeader(BlockHeader.newBuilder().setRawData(raw.newBuilder().setParentHash(ByteString.copyFrom(ByteArray.fromHexString(blockCapsule2.getBlockId().toString()))))).build());
    Assert.assertEquals(false, BlockUtil.isParentOf(blockCapsule1, blockCapsule2));
    Assert.assertFalse(BlockUtil.isParentOf(blockCapsule1, blockCapsule2));
    Assert.assertEquals(true, BlockUtil.isParentOf(blockCapsule2, blockCapsule3));
    Assert.assertTrue(BlockUtil.isParentOf(blockCapsule2, blockCapsule3));
}
Also used : Sha256Hash(org.tron.common.utils.Sha256Hash) BlockCapsule(org.tron.core.capsule.BlockCapsule) Test(org.junit.Test)

Example 10 with BlockCapsule

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

the class KhaosDatabaseTest method testStartBlock.

@Test
public void testStartBlock() {
    BlockCapsule blockCapsule = new BlockCapsule(Block.newBuilder().setBlockHeader(BlockHeader.newBuilder().setRawData(raw.newBuilder().setParentHash(ByteString.copyFrom(ByteArray.fromHexString("0304f784e4e7bae517bcab94c3e0c9214fb4ac7ff9d7d5a937d1f40031f87b81"))))).build());
    khaosDatabase.start(blockCapsule);
    Assert.assertEquals(blockCapsule, khaosDatabase.getBlock(blockCapsule.getBlockId()));
}
Also used : BlockCapsule(org.tron.core.capsule.BlockCapsule) Test(org.junit.Test)

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