Search in sources :

Example 6 with Header

use of neo.model.core.Header in project neo-java by coranos.

the class LocalNodeDataSynchronizedUtil method requestBlocksUnsynchronized.

/**
 * request blocks from the remote node, this is the unsynchronized helper
 * method..
 *
 * @param localNodeData
 *            the local node data to use.
 * @param remoteNodeData
 *            the remote node data to use.
 */
private static void requestBlocksUnsynchronized(final LocalNodeData localNodeData, final RemoteNodeData remoteNodeData) {
    if (!localNodeData.getVerifiedHeaderPoolMap().isEmpty()) {
        final List<UInt256> hashs = new ArrayList<>();
        final Iterator<Entry<Long, Header>> headerIt = localNodeData.getVerifiedHeaderPoolMap().entrySet().iterator();
        while ((hashs.size() < InvPayload.MAX_HASHES) && headerIt.hasNext()) {
            final Entry<Long, Header> headerElt = headerIt.next();
            final Header header = headerElt.getValue();
            final UInt256 hashRaw = header.hash;
            if (localNodeData.getBlockDb().containsBlockWithHash(hashRaw)) {
                headerIt.remove();
            } else {
                final byte[] ba = hashRaw.getBytesCopy();
                final UInt256 hash = new UInt256(ba);
                hashs.add(hash);
                if (LOG.isDebugEnabled()) {
                    LOG.debug("requestBlocks send {} getblocks {} {}", remoteNodeData.getHostAddress(), header.getIndexAsLong(), hash.toReverseHexString());
                }
            }
        }
        MessageUtil.sendGetData(remoteNodeData, localNodeData, InventoryType.BLOCK, hashs.toArray(new UInt256[0]));
    } else {
        if (localNodeData.getBlockDb().getHeaderOfBlockWithMaxIndex() == null) {
            if (LOG.isDebugEnabled()) {
                LOG.debug("requestBlocks send {} hash is genesis.", remoteNodeData.getHostAddress());
            }
            MessageUtil.sendGetData(remoteNodeData, localNodeData, InventoryType.BLOCK, GenesisBlockUtil.GENESIS_HASH);
        } else {
            LOG.info("SKIPPING requestBlocks, no hashes.");
        }
    }
}
Also used : Entry(java.util.Map.Entry) Header(neo.model.core.Header) ArrayList(java.util.ArrayList) UInt256(neo.model.bytes.UInt256)

Example 7 with Header

use of neo.model.core.Header in project neo-java by coranos.

the class TestCoreToJson method test008Header.

/**
 * test Header.
 */
@Test
public void test008Header() {
    final int minSize = (UInt32.SIZE * 3) + (UInt256.SIZE * 2) + (UInt64.SIZE) + (UInt160.SIZE) + 4;
    final String expectedStrRaw = TestUtil.getJsonTestResourceAsString(TEST_PACKAGE, getClass().getSimpleName(), "test008Header");
    final String expectedStr = new JSONObject(expectedStrRaw).toString();
    final String actualStr = new Header(ByteBuffer.wrap(new byte[minSize])).toString();
    Assert.assertEquals(TestUtil.RESPONSES_MUST_MATCH, expectedStr, actualStr);
}
Also used : JSONObject(org.json.JSONObject) Header(neo.model.core.Header) Test(org.junit.Test)

Aggregations

Header (neo.model.core.Header)7 UInt256 (neo.model.bytes.UInt256)3 Block (neo.model.core.Block)2 JSONObject (org.json.JSONObject)2 ArrayList (java.util.ArrayList)1 Entry (java.util.Map.Entry)1 HeadersPayload (neo.model.network.HeadersPayload)1 JSONArray (org.json.JSONArray)1 Test (org.junit.Test)1