use of neo.model.core.Block in project neo-java by coranos.
the class RemoteNodeControllerRunnable method run.
/**
* the run method.
*/
@Override
public void run() {
LOG.debug("STARTED RemoteNodeControllerRunnable run {}", data.getHostAddress());
final long startTimeMs = System.currentTimeMillis();
final LocalNodeData localNodeData = localControllerNode.getLocalNodeData();
final long readTimeOut = localNodeData.getRpcServerTimeoutMillis();
final long magic = localNodeData.getMagic();
final int localPort = localNodeData.getTcpPort();
final int nonce = localNodeData.getNonce();
final Block maxStartHeightBlock = localNodeData.getBlockDb().getHeaderOfBlockWithMaxIndex();
final long startHeight;
if (maxStartHeightBlock == null) {
startHeight = 0;
} else {
startHeight = maxStartHeightBlock.getIndexAsLong();
}
data.getSendQueue().add(new Message(magic, CommandEnum.VERSION, PayloadUtil.getVersionPayload(localPort, nonce, startHeight).toByteArray()));
data.getSendQueue().add(new Message(magic, CommandEnum.VERACK));
try {
try (SocketWrapper s = localNodeData.getSocketFactory().newSocketWrapper()) {
s.setSoTimeout(2000);
s.connect(data.getTcpAddressAndPort(), 2000);
try (OutputStream out = s.getOutputStream();
InputStream in = s.getInputStream()) {
data.setGoodPeer(true);
while (data.isGoodPeer()) {
sendMessages(out);
out.flush();
try {
Thread.sleep(data.getSleepIntervalMs());
} catch (final InterruptedException e) {
LOG.debug("InterruptedException[1], stopping. {}", e.getMessage());
data.setGoodPeer(false);
}
if (data.isGoodPeer()) {
recieveMessages(readTimeOut, magic, in);
}
final long currTimeMs = System.currentTimeMillis();
final long recycleTimeMs = startTimeMs + data.getRecycleIntervalMs();
if (recycleTimeMs < currTimeMs) {
LOG.debug("recycling remote node {}", data.getHostAddress());
data.setGoodPeer(false);
}
if (data.isGoodPeer()) {
try {
Thread.sleep(data.getSleepIntervalMs());
} catch (final InterruptedException e) {
LOG.debug("InterruptedException[2], stopping. {}", e.getMessage());
data.setGoodPeer(false);
}
}
}
}
} catch (final SocketTimeoutException e) {
LOG.trace("SocketTimeoutException[2] from {}, closing peer", data.getHostAddress());
LOG.trace("SocketTimeoutException[2]", e);
data.setGoodPeer(false);
} catch (final ConnectException e) {
LOG.trace("ConnectException from {}, closing peer", data.getHostAddress());
LOG.trace("ConnectException", e);
data.setGoodPeer(false);
} catch (final MessageFormatException e) {
LOG.trace("MessageFormatException from {}, closing peer", data.getHostAddress());
LOG.trace("MessageFormatException", e);
data.setGoodPeer(false);
} catch (final SocketException e) {
if (e.getMessage().equals("Broken pipe (Write failed)")) {
LOG.trace("SocketException from {}, broken pipe, closing peer", data.getHostAddress());
} else if (e.getMessage().equals("Operation timed out (Read failed)")) {
LOG.trace("SocketException from {}, timeout, closing peer", data.getHostAddress());
} else if (e.getMessage().equals("Connection reset")) {
LOG.trace("SocketException from {}, connection reset, closing peer", data.getHostAddress());
} else if (e.getMessage().equals("Network is unreachable (connect failed)")) {
LOG.trace("SocketException from {}, unreachable network, closing peer", data.getHostAddress());
} else if (e.getMessage().equals("Protocol wrong type for socket (Write failed)")) {
LOG.trace("SocketException from {}, wrong protocol, closing peer", data.getHostAddress());
} else {
LOG.error("SocketException from {}, closing peer", data.getHostAddress());
LOG.error("SocketException", e);
}
data.setGoodPeer(false);
} catch (final ClosedChannelException e) {
LOG.trace("ClosedChannelException from {}, closing peer", data.getHostAddress());
data.setGoodPeer(false);
}
} catch (final Exception e) {
LOG.error("error", e);
LOG.debug("FAILURE RemoteNodeControllerRunnable run");
localControllerNode.onSocketClose(RemoteNodeControllerRunnable.this);
return;
}
localControllerNode.onSocketClose(RemoteNodeControllerRunnable.this);
LOG.debug("SUCCESS RemoteNodeControllerRunnable run");
}
use of neo.model.core.Block 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;
}
}
use of neo.model.core.Block in project neo-java by coranos.
the class RpcServerUtil method onGetBlockCount.
/**
* responds to a "getblockcount" command.
*
* @param controller
* the controller to use.
* @param id
* the request id to use.
* @return the response.
*/
private static JSONObject onGetBlockCount(final LocalControllerNode controller, final int id) {
final Block block = controller.getLocalNodeData().getBlockDb().getHeaderOfBlockWithMaxIndex();
if (block == null) {
final JSONObject response = new JSONObject();
response.put(RESULT, 0);
response.put(ID, id);
response.put(JSONRPC, VERSION_2_0);
return response;
} else {
final JSONObject response = new JSONObject();
final long index = block.getIndexAsLong();
response.put(RESULT, index + 1);
response.put(ID, id);
response.put(JSONRPC, VERSION_2_0);
return response;
}
}
use of neo.model.core.Block in project neo-java by coranos.
the class RpcServerUtil method onGetBlockHash.
/**
* responds to a "getblockhash" 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 onGetBlockHash(final LocalControllerNode controller, final int id, final JSONArray params) {
if (params.length() == 0) {
final JSONObject response = new JSONObject();
response.put(ERROR, "no parameters, expected an index");
response.put(EXPECTED, 0);
response.put(ACTUAL, NULL);
return response;
} else {
final long index = params.getLong(0);
try {
final Block block = controller.getLocalNodeData().getBlockDb().getHeaderOfBlockFromHeight(index);
final JSONObject response = new JSONObject();
response.put(ID, id);
response.put(JSONRPC, VERSION_2_0);
response.put(RESULT, block.hash.toHexString());
return response;
} catch (final RuntimeException e) {
final JSONObject response = new JSONObject();
response.put(ERROR, e.getMessage());
response.put(EXPECTED, 0);
response.put(ACTUAL, NULL);
return response;
}
}
}
use of neo.model.core.Block in project neo-java by coranos.
the class BlockDbH2Impl method getHeaderOfBlockWithMaxIndex.
/**
* return the block with the maximum value in the index column.
*
* @return the block with the maximum value in the index column.
*/
@Override
public Block getHeaderOfBlockWithMaxIndex() {
synchronized (this) {
if (closed) {
return null;
}
}
final JdbcTemplate t = new JdbcTemplate(ds);
final String sql = getSql("getBlockWithMaxIndex");
final List<byte[]> data = t.queryForList(sql, byte[].class);
if (data.isEmpty()) {
return null;
}
final Block block = new Block(ByteBuffer.wrap(data.get(0)));
return block;
}
Aggregations