use of neo.model.bytes.UInt256 in project neo-java by coranos.
the class AbstractBlockBase method calculateHash.
/**
* return the hash, as calculated from the other parameters.
*
* @return the hash, as calculated from the other parameters.
*/
private UInt256 calculateHash() {
final ByteArrayOutputStream bout = new ByteArrayOutputStream();
NetworkUtil.write(bout, nextConsensus.toByteArray());
NetworkUtil.write(bout, consensusData.toByteArray());
NetworkUtil.write(bout, index.toByteArray());
NetworkUtil.write(bout, timestamp.toByteArray());
NetworkUtil.write(bout, merkleRoot.toByteArray());
final byte[] prevHashBa = prevHash.toByteArray();
ArrayUtils.reverse(prevHashBa);
NetworkUtil.write(bout, prevHashBa);
NetworkUtil.write(bout, version.toByteArray());
final byte[] hashDataBa = bout.toByteArray();
ArrayUtils.reverse(hashDataBa);
final byte[] hashBa = SHA256HashUtil.getDoubleSHA256Hash(hashDataBa);
return new UInt256(hashBa);
}
use of neo.model.bytes.UInt256 in project neo-java by coranos.
the class LocalControllerNode method onInv.
/**
* does something on a "inv" message.
*
* @param peer
* the peer that sent the message.
* @param message
* the message.
*/
private void onInv(final RemoteNodeControllerRunnable peer, final Message message) {
if (stopped) {
return;
}
// TODO: figure out what to do here.
final InvPayload invp = message.getPayload(InvPayload.class);
final List<UInt256> hashes = invp.getHashes();
switch(invp.getType()) {
case BLOCK:
break;
case CONSENSUS:
break;
case TRANSACTION:
break;
}
MessageUtil.sendGetData(peer.getData(), localNodeData, invp.getType(), hashes.toArray(new UInt256[0]));
}
use of neo.model.bytes.UInt256 in project neo-java by coranos.
the class LocalNodeDataSynchronizedUtil method requestHeaders.
/**
* request headers from the remote node.
*
* @param localNodeData
* the local node data to use.
* @param remoteNodeData
* the remote node data to use.
*/
public static void requestHeaders(final LocalNodeData localNodeData, final RemoteNodeData remoteNodeData) {
synchronized (localNodeData) {
final UInt256 hashRaw;
final long index;
if (localNodeData.getUnverifiedBlockPoolSet().isEmpty() && (!localNodeData.getVerifiedHeaderPoolMap().isEmpty())) {
final long highestHeaderIndex = localNodeData.getVerifiedHeaderPoolMap().lastKey();
final Header highestHeader = localNodeData.getVerifiedHeaderPoolMap().get(highestHeaderIndex);
LOG.debug("requestHeaders getVerifiedHeaderPoolMap height:{};hash:{};", highestHeaderIndex, highestHeader.hash);
hashRaw = highestHeader.hash;
index = highestHeader.getIndexAsLong();
} else {
final Block highestBlock = localNodeData.getBlockDb().getHeaderOfBlockWithMaxIndex();
if (highestBlock != null) {
LOG.debug("requestHeaders getHighestBlock height:{};hash:{};", highestBlock.getIndexAsLong(), highestBlock.hash);
hashRaw = highestBlock.hash;
index = highestBlock.getIndexAsLong();
} else {
LOG.debug("requestHeaders hash is genesis.");
hashRaw = GenesisBlockUtil.GENESIS_HASH;
index = GenesisBlockUtil.GENESIS_BLOCK.getIndexAsLong();
}
}
final byte[] ba = hashRaw.getBytesCopy();
final UInt256 hash = new UInt256(ba);
LOG.debug("requestHeaders index:{};hash:{};", index, hash);
// fixed bug at height 2000190
// final String goodHashStr =
// "8cb9fee28a48a45468e3c0a229fd4473288cdd9794c10cac7b8f4681ca404342";
// final UInt256 goodHash = new
// UInt256(ByteBuffer.wrap(Hex.decode(goodHashStr)));
// MessageUtil.sendGetHeaders(remoteNodeData, localNodeData, goodHash);
MessageUtil.sendGetHeaders(remoteNodeData, localNodeData, hash);
}
}
use of neo.model.bytes.UInt256 in project neo-java by coranos.
the class MessageUtil method sendGetData.
/**
* send a message to get block data.
*
* @param remoteNodeData
* the remote node data to use.
* @param localNodeData
* the local node data to use.
* @param type
* the inventory type.
* @param hashs
* the hashes to use.
*/
public static void sendGetData(final RemoteNodeData remoteNodeData, final LocalNodeData localNodeData, final InventoryType type, final UInt256... hashs) {
if (type.equals(InventoryType.BLOCK)) {
boolean hasDuplicates = false;
for (final UInt256 hash : hashs) {
if (localNodeData.getBlockDb().containsBlockWithHash(hash)) {
hasDuplicates = true;
LOG.debug("sendGetData requesting duplicate block hash:{}", hash);
}
}
if (hasDuplicates) {
MapUtil.increment(LocalNodeData.API_CALL_MAP, DUPLICATE_OUT_BLOCK);
}
}
remoteNodeData.send(new Message(localNodeData.getMagic(), CommandEnum.GETDATA, new InvPayload(type, hashs).toByteArray()));
}
use of neo.model.bytes.UInt256 in project neo-java by coranos.
the class RpcServerUtil method onGetBlock.
/**
* responds to a "getblock" command.
*
* @param controller
* the controller to use.
* @param id
* the request id to use.
* @param params
* the parameters to use.
* @return the response.
*/
private static JSONObject onGetBlock(final LocalControllerNode controller, final int id, final JSONArray params) {
if (params.length() == 0) {
final JSONObject response = new JSONObject();
response.put(ERROR, "no parameters, expected a hash or an index");
response.put(EXPECTED, 0);
response.put(ACTUAL, NULL);
return response;
} else {
final boolean verbose;
if (params.length() >= 2) {
if (params.get(1) instanceof Number) {
final long index = params.getLong(1);
verbose = index == 1;
} else {
verbose = false;
}
} else {
verbose = false;
}
final Block block;
final BlockDb blockDb = controller.getLocalNodeData().getBlockDb();
if (params.get(0) instanceof String) {
final String hashStr = params.getString(0);
final byte[] ba = ModelUtil.decodeHex(hashStr);
final UInt256 hash = new UInt256(ByteBuffer.wrap(ba));
try {
block = blockDb.getFullBlockFromHash(hash);
} catch (final RuntimeException e) {
final JSONObject response = new JSONObject();
response.put(ERROR, e.getMessage());
response.put(EXPECTED, EXPECTED_GENERIC_HEX);
response.put(ACTUAL, params.get(0));
return response;
}
} else if (params.get(0) instanceof Number) {
final long index = params.getLong(0);
try {
block = blockDb.getFullBlockFromHeight(index);
} catch (final RuntimeException e) {
final JSONObject response = new JSONObject();
response.put(ERROR, e.getMessage());
response.put(EXPECTED, 0);
response.put(ACTUAL, params.get(0));
return response;
}
} else {
final JSONObject response = new JSONObject();
response.put(ERROR, "bad parameters, expected a hash or an index");
response.put(EXPECTED, 0);
response.put(ACTUAL, params.get(0));
return response;
}
final JSONObject response = new JSONObject();
response.put(ID, id);
response.put(JSONRPC, VERSION_2_0);
if (verbose) {
response.put(RESULT, block.toJSONObject());
} else {
response.put(RESULT, Hex.encodeHexString(block.toByteArray()));
}
return response;
}
}
Aggregations