Search in sources :

Example 1 with UInt256

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);
}
Also used : ByteArrayOutputStream(java.io.ByteArrayOutputStream) UInt256(neo.model.bytes.UInt256)

Example 2 with UInt256

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]));
}
Also used : InvPayload(neo.model.network.InvPayload) UInt256(neo.model.bytes.UInt256)

Example 3 with UInt256

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);
    }
}
Also used : Header(neo.model.core.Header) Block(neo.model.core.Block) UInt256(neo.model.bytes.UInt256)

Example 4 with UInt256

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()));
}
Also used : Message(neo.model.network.Message) InvPayload(neo.model.network.InvPayload) UInt256(neo.model.bytes.UInt256)

Example 5 with UInt256

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;
    }
}
Also used : JSONObject(org.json.JSONObject) Block(neo.model.core.Block) BlockDb(neo.model.db.BlockDb) UInt256(neo.model.bytes.UInt256)

Aggregations

UInt256 (neo.model.bytes.UInt256)36 TransactionOutput (neo.model.core.TransactionOutput)15 JSONObject (org.json.JSONObject)14 TreeMap (java.util.TreeMap)13 Fixed8 (neo.model.bytes.Fixed8)13 Transaction (neo.model.core.Transaction)13 Map (java.util.Map)11 UInt160 (neo.model.bytes.UInt160)11 CoinReference (neo.model.core.CoinReference)11 Block (neo.model.core.Block)8 JSONArray (org.json.JSONArray)7 UInt16 (neo.model.bytes.UInt16)5 BlockDb (neo.model.db.BlockDb)5 ArrayList (java.util.ArrayList)3 Header (neo.model.core.Header)3 BTreeMap (org.mapdb.BTreeMap)3 JdbcTemplate (org.springframework.jdbc.core.JdbcTemplate)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 InvPayload (neo.model.network.InvPayload)2 Color (java.awt.Color)1