Search in sources :

Example 11 with BlockHeader

use of org.aion.zero.impl.types.BlockHeader in project aion by aionnetwork.

the class SyncMgrTest method testRequestBodies_withTwoRequests.

@Test
public void testRequestBodies_withTwoRequests() {
    BlockHeader header1 = mock(BlockHeader.class);
    when(header1.getNumber()).thenReturn(101L);
    byte[] hash1 = Hex.decode("6fd8dae3304a9864f460ec7aec21bc94e14e34876e5dddd0a74d9c68ac7bc9ed");
    when(header1.getHash()).thenReturn(hash1);
    when(header1.getTxTrieRootWrapper()).thenReturn(EMPTY_TRIE_HASH);
    List<BlockHeader> list1 = new ArrayList<>();
    list1.add(header1);
    syncMgr.syncHeaderRequestManager.storeHeaders(1, list1);
    BlockHeader header2 = mock(BlockHeader.class);
    when(header2.getNumber()).thenReturn(102L);
    byte[] hash2 = Hex.decode("f2652dde61042e9306dce95ecdc41a1be2be7eb374f19427aef2a79101b471ea");
    when(header2.getHash()).thenReturn(hash2);
    when(header2.getTxTrieRootWrapper()).thenReturn(EMPTY_TRIE_HASH);
    List<BlockHeader> list2 = new ArrayList<>();
    list2.add(header1);
    list2.add(header2);
    syncMgr.syncHeaderRequestManager.storeHeaders(1, list2);
    syncMgr.requestBodies(1, "peer1");
    // ensure that 1 request was sent
    verify(p2pMgr, times(2)).send(anyInt(), anyString(), any(ReqBlocksBodies.class));
    // both lists are still stored and can be retrieved in order
    assertThat(syncMgr.syncHeaderRequestManager.matchAndDropHeaders(1, 1, EMPTY_TRIE_HASH)).isEqualTo(list1);
    assertThat(syncMgr.syncHeaderRequestManager.matchAndDropHeaders(1, 2, EMPTY_TRIE_HASH)).isEqualTo(list2);
}
Also used : ArrayList(java.util.ArrayList) BlockHeader(org.aion.zero.impl.types.BlockHeader) MiningBlockHeader(org.aion.zero.impl.types.MiningBlockHeader) ReqBlocksBodies(org.aion.zero.impl.sync.msg.ReqBlocksBodies) Test(org.junit.Test)

Example 12 with BlockHeader

use of org.aion.zero.impl.types.BlockHeader in project aion by aionnetwork.

the class SyncMgrTest method testRequestBodies_withFilteringOnBlockNumber.

@Test
public void testRequestBodies_withFilteringOnBlockNumber() {
    BlockHeader header = mock(BlockHeader.class);
    when(header.getNumber()).thenReturn(bestBlockNumber);
    byte[] hash = Hex.decode("6fd8dae3304a9864f460ec7aec21bc94e14e34876e5dddd0a74d9c68ac7bc9ed");
    when(header.getHash()).thenReturn(hash);
    when(header.getTxTrieRootWrapper()).thenReturn(EMPTY_TRIE_HASH);
    List<BlockHeader> list = new ArrayList<>();
    list.add(header);
    INode peer1 = mock(INode.class);
    when(peer1.getIdHash()).thenReturn(1);
    when(peer1.getIdShort()).thenReturn("peer1");
    when(peer1.getBestBlockNumber()).thenReturn(2 * bestBlockNumber);
    // ensure that peer1 exists in the syncHeaderRequestManager
    syncMgr.syncHeaderRequestManager.assertUpdateActiveNodes(Map.of(1, peer1), null, null, Set.of(1), Set.of(1), 2 * bestBlockNumber);
    syncMgr.syncHeaderRequestManager.storeHeaders(1, list);
    syncMgr.syncHeaderRequestManager.runInMode(1, SyncMode.NORMAL);
    syncMgr.requestBodies(1, "peer1");
    // ensure that 1 request was sent
    verify(p2pMgr, never()).send(anyInt(), anyString(), any(ReqBlocksBodies.class));
    assertThat(syncMgr.syncHeaderRequestManager.matchAndDropHeaders(1, 1, EMPTY_TRIE_HASH)).isNull();
}
Also used : INode(org.aion.p2p.INode) ArrayList(java.util.ArrayList) BlockHeader(org.aion.zero.impl.types.BlockHeader) MiningBlockHeader(org.aion.zero.impl.types.MiningBlockHeader) ReqBlocksBodies(org.aion.zero.impl.sync.msg.ReqBlocksBodies) Test(org.junit.Test)

Example 13 with BlockHeader

use of org.aion.zero.impl.types.BlockHeader in project aion by aionnetwork.

the class SyncMgrTest method testValidateAndAddHeaders_withCorrectHeaders.

@Test
public void testValidateAndAddHeaders_withCorrectHeaders() {
    int nodeId = 1;
    String displayId = "peer1";
    syncMgr.validateAndAddHeaders(nodeId, displayId, consecutiveHeaders);
    verify(p2pMgr, never()).errCheck(nodeId, displayId);
    // Check that all the headers were stored.
    List<BlockHeader> stored = syncMgr.syncHeaderRequestManager.matchAndDropHeaders(nodeId, consecutiveHeaders.size(), consecutiveHeaders.get(0).getTxTrieRootWrapper());
    assertThat(stored.size()).isEqualTo(consecutiveHeaders.size());
    assertThat(stored).containsAllIn(consecutiveHeaders);
}
Also used : ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) BlockHeader(org.aion.zero.impl.types.BlockHeader) MiningBlockHeader(org.aion.zero.impl.types.MiningBlockHeader) Test(org.junit.Test)

Example 14 with BlockHeader

use of org.aion.zero.impl.types.BlockHeader in project aion by aionnetwork.

the class AionBlockStore method getListHeadersEndWith.

public List<BlockHeader> getListHeadersEndWith(byte[] hash, long qty) {
    if (hash == null || qty < 0) {
        return null;
    }
    lock.lock();
    try {
        List<Block> blocks = getListBlocksEndWith(hash, qty);
        List<BlockHeader> headers = new ArrayList<>(blocks.size());
        for (Block b : blocks) {
            headers.add(b.getHeader());
        }
        return headers;
    } finally {
        lock.unlock();
    }
}
Also used : ArrayList(java.util.ArrayList) Block(org.aion.zero.impl.types.Block) BlockHeader(org.aion.zero.impl.types.BlockHeader)

Example 15 with BlockHeader

use of org.aion.zero.impl.types.BlockHeader in project aion by aionnetwork.

the class SyncMgrTest method testRequestBodies_withOneRequest.

@Test
public void testRequestBodies_withOneRequest() {
    BlockHeader header = mock(BlockHeader.class);
    when(header.getNumber()).thenReturn(101L);
    byte[] hash = Hex.decode("6fd8dae3304a9864f460ec7aec21bc94e14e34876e5dddd0a74d9c68ac7bc9ed");
    when(header.getHash()).thenReturn(hash);
    when(header.getTxTrieRootWrapper()).thenReturn(EMPTY_TRIE_HASH);
    List<BlockHeader> list = new ArrayList<>();
    list.add(header);
    syncMgr.syncHeaderRequestManager.storeHeaders(1, list);
    syncMgr.requestBodies(1, "peer1");
    // ensure that 1 request was sent
    verify(p2pMgr, times(1)).send(anyInt(), anyString(), any(ReqBlocksBodies.class));
    // the list is still stored
    assertThat(syncMgr.syncHeaderRequestManager.matchAndDropHeaders(1, 1, EMPTY_TRIE_HASH)).isEqualTo(list);
}
Also used : ArrayList(java.util.ArrayList) BlockHeader(org.aion.zero.impl.types.BlockHeader) MiningBlockHeader(org.aion.zero.impl.types.MiningBlockHeader) ReqBlocksBodies(org.aion.zero.impl.sync.msg.ReqBlocksBodies) Test(org.junit.Test)

Aggregations

BlockHeader (org.aion.zero.impl.types.BlockHeader)29 Test (org.junit.Test)18 ArrayList (java.util.ArrayList)17 MiningBlockHeader (org.aion.zero.impl.types.MiningBlockHeader)14 ReqBlocksBodies (org.aion.zero.impl.sync.msg.ReqBlocksBodies)8 Block (org.aion.zero.impl.types.Block)7 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)4 BigInteger (java.math.BigInteger)3 EventBlock (org.aion.evtmgr.impl.evt.EventBlock)3 INode (org.aion.p2p.INode)3 SharedRLPList (org.aion.rlp.SharedRLPList)3 GenesisStakingBlock (org.aion.zero.impl.types.GenesisStakingBlock)3 MiningBlock (org.aion.zero.impl.types.MiningBlock)3 RetValidPreBlock (org.aion.zero.impl.types.RetValidPreBlock)3 StakingBlock (org.aion.zero.impl.types.StakingBlock)3 StakingBlockHeader (org.aion.zero.impl.types.StakingBlockHeader)3 BlockDetailsValidator.isValidBlock (org.aion.zero.impl.valid.BlockDetailsValidator.isValidBlock)3 List (java.util.List)2 ByteArrayWrapper (org.aion.util.types.ByteArrayWrapper)2 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1