Search in sources :

Example 16 with BlockHeader

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

the class SyncMgrTest method testValidateAndAddHeaders_withInvalidHeader.

@Test
public void testValidateAndAddHeaders_withInvalidHeader() {
    int nodeId = 1;
    String displayId = "peer1";
    List<BlockHeader> sequentialHeaders = new ArrayList<>();
    sequentialHeaders.add(consecutiveHeaders.get(0));
    sequentialHeaders.add(consecutiveHeaders.get(1));
    // Break energy consumed rule.
    List<BlockHeader> invalidHeaderList = new ArrayList<>(sequentialHeaders);
    assertThat(consecutiveHeaders.get(2).getEnergyConsumed()).isGreaterThan(0L);
    invalidHeaderList.add(MiningBlockHeader.Builder.newInstance().withHeader((MiningBlockHeader) consecutiveHeaders.get(2)).withEnergyConsumed(0L).build());
    syncMgr.validateAndAddHeaders(nodeId, displayId, invalidHeaderList);
    verify(p2pMgr, never()).errCheck(nodeId, displayId);
    // Check that the sequential subset of headers was stored.
    assertThat(syncMgr.syncHeaderRequestManager.matchAndDropHeaders(nodeId, invalidHeaderList.size(), invalidHeaderList.get(0).getTxTrieRootWrapper())).isNull();
    List<BlockHeader> stored = syncMgr.syncHeaderRequestManager.matchAndDropHeaders(nodeId, sequentialHeaders.size(), sequentialHeaders.get(0).getTxTrieRootWrapper());
    assertThat(stored.size()).isEqualTo(sequentialHeaders.size());
    assertThat(stored).containsAllIn(sequentialHeaders);
}
Also used : MiningBlockHeader(org.aion.zero.impl.types.MiningBlockHeader) ArrayList(java.util.ArrayList) 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 17 with BlockHeader

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

the class SyncMgrTest method testValidateAndAddHeaders_withNonSequentialHeaders.

@Test
public void testValidateAndAddHeaders_withNonSequentialHeaders() {
    int nodeId = 1;
    String displayId = "peer1";
    List<BlockHeader> sequentialHeaders = new ArrayList<>();
    sequentialHeaders.add(consecutiveHeaders.get(0));
    sequentialHeaders.add(consecutiveHeaders.get(1));
    BlockHeader outOfOrder = consecutiveHeaders.get(3);
    List<BlockHeader> nonSequentialHeaders = new ArrayList<>(sequentialHeaders);
    nonSequentialHeaders.add(outOfOrder);
    syncMgr.validateAndAddHeaders(nodeId, displayId, nonSequentialHeaders);
    verify(p2pMgr, never()).errCheck(nodeId, displayId);
    // Check that the sequential subset of headers was stored.
    assertThat(syncMgr.syncHeaderRequestManager.matchAndDropHeaders(nodeId, nonSequentialHeaders.size(), nonSequentialHeaders.get(0).getTxTrieRootWrapper())).isNull();
    List<BlockHeader> stored = syncMgr.syncHeaderRequestManager.matchAndDropHeaders(nodeId, sequentialHeaders.size(), sequentialHeaders.get(0).getTxTrieRootWrapper());
    assertThat(stored.size()).isEqualTo(sequentialHeaders.size());
    assertThat(stored).containsAllIn(sequentialHeaders);
    assertThat(stored).doesNotContain(outOfOrder);
}
Also used : ArrayList(java.util.ArrayList) 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 18 with BlockHeader

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

the class SyncMgrTest method testRequestBodies_withFilteringOnBlockHashAndReadHeaders.

@Test
public void testRequestBodies_withFilteringOnBlockHashAndReadHeaders() {
    BlockHeader header1 = mock(BlockHeader.class);
    when(header1.getNumber()).thenReturn(100L);
    byte[] hash1 = Hex.decode("6fd8dae3304a9864f460ec7aec21bc94e14e34876e5dddd0a74d9c68ac7bc9ed");
    when(header1.getHash()).thenReturn(hash1);
    when(header1.getTxTrieRootWrapper()).thenReturn(EMPTY_TRIE_HASH);
    BlockHeader header2 = mock(BlockHeader.class);
    when(header2.getNumber()).thenReturn(101L);
    byte[] hash2 = Hex.decode("f2652dde61042e9306dce95ecdc41a1be2be7eb374f19427aef2a79101b471ea");
    when(header2.getHash()).thenReturn(hash2);
    when(header2.getTxTrieRootWrapper()).thenReturn(EMPTY_TRIE_HASH);
    List<BlockHeader> list = new ArrayList<>();
    list.add(header1);
    list.add(header2);
    syncMgr.syncHeaderRequestManager.storeHeaders(1, list);
    syncMgr.importedBlockHashes.put(ByteArrayWrapper.wrap(hash1), true);
    syncMgr.requestBodies(1, "peer1");
    // ensure that 1 request was sent
    verify(p2pMgr, never()).send(anyInt(), anyString(), any(ReqBlocksBodies.class));
    // a subset of the list is re-added for future requests
    assertThat(syncMgr.syncHeaderRequestManager.matchAndDropHeaders(1, 1, EMPTY_TRIE_HASH)).contains(header2);
}
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 19 with BlockHeader

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

the class SyncMgrTest method testRequestBodies_withOneRequestFromMultipleOptions.

@Test
public void testRequestBodies_withOneRequestFromMultipleOptions() {
    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(header2);
    syncMgr.syncHeaderRequestManager.storeHeaders(1, list2);
    syncMgr.requestBodies(1, "peer1");
    // ensure that 1 request was sent
    verify(p2pMgr, times(1)).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, 1, 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 20 with BlockHeader

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

the class SyncMgrTest method testRequestBodies_withFilteringOnBlockNumberAndRemainingHeaders.

@Test
public void testRequestBodies_withFilteringOnBlockNumberAndRemainingHeaders() {
    BlockHeader header1 = mock(BlockHeader.class);
    when(header1.getNumber()).thenReturn(bestBlockNumber);
    byte[] hash1 = Hex.decode("6fd8dae3304a9864f460ec7aec21bc94e14e34876e5dddd0a74d9c68ac7bc9ed");
    when(header1.getHash()).thenReturn(hash1);
    when(header1.getTxTrieRootWrapper()).thenReturn(EMPTY_TRIE_HASH);
    BlockHeader header2 = mock(BlockHeader.class);
    when(header2.getNumber()).thenReturn(bestBlockNumber + 1);
    when(header2.getTxTrieRootWrapper()).thenReturn(EMPTY_TRIE_HASH);
    byte[] hash2 = Hex.decode("f2652dde61042e9306dce95ecdc41a1be2be7eb374f19427aef2a79101b471ea");
    when(header2.getHash()).thenReturn(hash2);
    List<BlockHeader> list = new ArrayList<>();
    list.add(header1);
    list.add(header2);
    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)).contains(header2);
}
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)

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