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