Search in sources :

Example 11 with NulsDigestData

use of io.nuls.kernel.model.NulsDigestData in project nuls by nuls-io.

the class GetSmallBlockHandler method onMessage.

@Override
public void onMessage(GetSmallBlockMessage message, Node fromNode) {
    if (message == null || fromNode == null || null == message.getMsgBody()) {
        return;
    }
    NulsDigestData blockHash = message.getMsgBody();
    SmallBlock smallBlock = cacheManager.getSmallBlockByHash(blockHash);
    if (null == smallBlock) {
        return;
    }
    SmallBlockMessage smallBlockMessage = new SmallBlockMessage();
    smallBlockMessage.setMsgBody(smallBlock);
    Result result = messageBusService.sendToNode(smallBlockMessage, fromNode, true);
    if (!result.isSuccess()) {
        Log.error("---send smallBlockMessage fail, height:" + smallBlock.getHeader().getHeight() + "hash: " + smallBlock.getHeader().getHash().getDigestHex());
        Log.error("----" + result.getMsg());
    }
}
Also used : SmallBlock(io.nuls.protocol.model.SmallBlock) NulsDigestData(io.nuls.kernel.model.NulsDigestData) Result(io.nuls.kernel.model.Result)

Example 12 with NulsDigestData

use of io.nuls.kernel.model.NulsDigestData in project nuls by nuls-io.

the class DownloadProcessor method getNetworkNewestBlock.

public NetworkNewestBlockInfos getNetworkNewestBlock() {
    Collection<Node> nodeList = networkService.getAvailableNodes();
    Map<NulsDigestData, Integer> statisticsMaps = new HashMap<>();
    Map<NulsDigestData, List<Node>> nodeMaps = new HashMap<>();
    // System.out.println("--------------start download-------------------");
    for (Node node : nodeList) {
        // System.out.println(node.getId() + " : " + node.getBestBlockHeight() + " : " + node.getBestBlockHash());
        NulsDigestData hash = node.getBestBlockHash();
        Integer statistics = statisticsMaps.get(hash);
        if (statistics == null) {
            statisticsMaps.put(hash, 0);
        }
        statisticsMaps.put(hash, statisticsMaps.get(hash) + 1);
        List<Node> nodes = nodeMaps.get(hash);
        if (nodes == null) {
            nodes = new ArrayList<>();
            nodeMaps.put(hash, nodes);
        }
        nodes.add(node);
    }
    // max number
    int max = 0;
    long bestHeight = 0;
    NulsDigestData bestHash = null;
    List<Node> nodes = null;
    for (Map.Entry<NulsDigestData, Integer> entry : statisticsMaps.entrySet()) {
        int count = entry.getValue();
        NulsDigestData hash = entry.getKey();
        List<Node> tempNodes = nodeMaps.get(hash);
        long height = tempNodes.get(0).getBestBlockHeight();
        if (count > max || (count == max && bestHeight < height)) {
            max = count;
            bestHash = hash;
            bestHeight = height;
            nodes = tempNodes;
        }
    }
    if (nodes == null || nodes.size() == 0) {
        throw new NulsRuntimeException(NetworkErrorCode.NET_NODE_NOT_FOUND);
    }
    return new NetworkNewestBlockInfos(bestHeight, bestHash, nodes);
}
Also used : Node(io.nuls.network.model.Node) NulsRuntimeException(io.nuls.kernel.exception.NulsRuntimeException) NetworkNewestBlockInfos(io.nuls.protocol.base.download.entity.NetworkNewestBlockInfos) NulsDigestData(io.nuls.kernel.model.NulsDigestData)

Example 13 with NulsDigestData

use of io.nuls.kernel.model.NulsDigestData in project nuls by nuls-io.

the class BlockHeaderPo method size.

@Override
public int size() {
    int size = 0;
    size += SerializeUtils.sizeOfNulsData(preHash);
    size += SerializeUtils.sizeOfNulsData(merkleHash);
    size += SerializeUtils.sizeOfVarInt(time);
    size += SerializeUtils.sizeOfVarInt(height);
    size += SerializeUtils.sizeOfVarInt(txCount);
    size += SerializeUtils.sizeOfBytes(extend);
    size += SerializeUtils.sizeOfNulsData(scriptSign);
    for (NulsDigestData hash : txHashList) {
        size += SerializeUtils.sizeOfNulsData(hash);
    }
    if (NulsContext.MAIN_NET_VERSION > 1) {
        size += SerializeUtils.sizeOfBytes(stateRoot);
    }
    return size;
}
Also used : NulsDigestData(io.nuls.kernel.model.NulsDigestData)

Example 14 with NulsDigestData

use of io.nuls.kernel.model.NulsDigestData in project nuls by nuls-io.

the class BlockHeaderStorageServiceImpl method getBlockHeaderPo.

/**
 * 根据区块hash查询区块头数据
 * Query block header data according to block hash.
 *
 * @param hashBytes 区块头摘要/block hash
 * @return BlockHeaderPo 区块头数据
 */
private BlockHeaderPo getBlockHeaderPo(byte[] hashBytes) {
    byte[] bytes = dbService.get(ProtocolStorageConstant.DB_NAME_BLOCK_HEADER, hashBytes);
    if (null == bytes) {
        return null;
    }
    BlockHeaderPo po = new BlockHeaderPo();
    try {
        po.parse(bytes, 0);
    } catch (NulsException e) {
        Log.error(e);
    }
    NulsDigestData hash = new NulsDigestData();
    try {
        hash.parse(hashBytes, 0);
    } catch (NulsException e) {
        Log.error(e);
    }
    po.setHash(hash);
    return po;
}
Also used : NulsException(io.nuls.kernel.exception.NulsException) NulsDigestData(io.nuls.kernel.model.NulsDigestData) BlockHeaderPo(io.nuls.protocol.storage.po.BlockHeaderPo)

Example 15 with NulsDigestData

use of io.nuls.kernel.model.NulsDigestData in project nuls by nuls-io.

the class BlockHeaderStorageServiceImplTest method init.

@Before
public void init() {
    MicroKernelBootstrap mk = MicroKernelBootstrap.getInstance();
    mk.init();
    mk.start();
    LevelDbModuleBootstrap bootstrap = new LevelDbModuleBootstrap();
    bootstrap.init();
    bootstrap.start();
    service = NulsContext.getServiceBean(BlockHeaderStorageService.class);
    BlockHeaderPo po = new BlockHeaderPo();
    po.setHash(NulsDigestData.calcDigestData("hashhash".getBytes()));
    po.setHeight(1286L);
    po.setExtend("extends".getBytes());
    po.setMerkleHash(NulsDigestData.calcDigestData("merkleHash".getBytes()));
    po.setPreHash(NulsDigestData.calcDigestData("prehash".getBytes()));
    try {
        po.setPackingAddress("address".getBytes());
    } catch (Exception e) {
        e.printStackTrace();
        assertTrue(false);
    }
    po.setScriptSign(new BlockSignature());
    po.setTime(12345678901L);
    po.setTxCount(3);
    List<NulsDigestData> txHashList = new ArrayList<>();
    txHashList.add(NulsDigestData.calcDigestData("first-tx-hash".getBytes()));
    txHashList.add(NulsDigestData.calcDigestData("second-tx-hash".getBytes()));
    txHashList.add(NulsDigestData.calcDigestData("third-tx-hash".getBytes()));
    po.setTxHashList(txHashList);
    this.entity = po;
}
Also used : LevelDbModuleBootstrap(io.nuls.db.module.impl.LevelDbModuleBootstrap) BlockSignature(io.nuls.kernel.script.BlockSignature) ArrayList(java.util.ArrayList) NulsDigestData(io.nuls.kernel.model.NulsDigestData) BlockHeaderStorageService(io.nuls.protocol.storage.service.BlockHeaderStorageService) BlockHeaderPo(io.nuls.protocol.storage.po.BlockHeaderPo) MicroKernelBootstrap(io.nuls.kernel.MicroKernelBootstrap) Before(org.junit.Before)

Aggregations

NulsDigestData (io.nuls.kernel.model.NulsDigestData)54 ArrayList (java.util.ArrayList)16 Transaction (io.nuls.kernel.model.Transaction)12 Test (org.junit.Test)12 Block (io.nuls.kernel.model.Block)9 AgentPo (io.nuls.consensus.poc.storage.po.AgentPo)8 NulsException (io.nuls.kernel.exception.NulsException)8 BlockHeader (io.nuls.kernel.model.BlockHeader)7 IOException (java.io.IOException)7 NulsRuntimeException (io.nuls.kernel.exception.NulsRuntimeException)5 Result (io.nuls.kernel.model.Result)5 BaseTest (io.nuls.consensus.poc.storage.BaseTest)4 HashSet (java.util.HashSet)4 DepositPo (io.nuls.consensus.poc.storage.po.DepositPo)3 MicroKernelBootstrap (io.nuls.kernel.MicroKernelBootstrap)3 BlockSignature (io.nuls.kernel.script.BlockSignature)3 ValidateResult (io.nuls.kernel.validate.ValidateResult)3 Node (io.nuls.network.model.Node)3 TransferTransaction (io.nuls.protocol.model.tx.TransferTransaction)3 Deposit (io.nuls.consensus.poc.protocol.entity.Deposit)2