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);
}
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);
}
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);
}
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);
}
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);
}
Aggregations