Skip to content

Commit

Permalink
Use wrapper when comparing txTrieRoot during sync
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexandraRoatis committed Apr 24, 2020
1 parent d6d4cb4 commit 998e0e9
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
import java.util.stream.Collectors;
import org.aion.mcf.blockchain.Block;
import org.aion.mcf.blockchain.BlockHeader;
import org.aion.p2p.INode;
import org.aion.p2p.IP2pMgr;
Expand Down Expand Up @@ -622,7 +621,7 @@ public List<List<BlockHeader>> getHeadersForBodiesRequests(int peerId) {
/**
* Returns the headers received for the given size. The headers are removed from the internal storage.
*/
public List<BlockHeader> matchAndDropHeaders(int peerId, int size, byte[] firstNodeRoot) {
public List<BlockHeader> matchAndDropHeaders(int peerId, int size, ByteArrayWrapper firstNodeRoot) {
lock.lock();

try {
Expand All @@ -634,7 +633,7 @@ public List<BlockHeader> matchAndDropHeaders(int peerId, int size, byte[] firstN
LinkedList<List<BlockHeader>> allHeaders = storedHeaders.get(peerId).get(size);
for (Iterator<List<BlockHeader>> it = allHeaders.iterator(); it.hasNext(); ) {
headers = it.next();
if (Arrays.equals(headers.get(0).getTxTrieRoot(), firstNodeRoot)) {
if (firstNodeRoot.equals(headers.get(0).getTxTrieRootWrapper())) {
it.remove();
break;
} else {
Expand Down
4 changes: 2 additions & 2 deletions modAionImpl/src/org/aion/zero/impl/sync/SyncMgr.java
Original file line number Diff line number Diff line change
Expand Up @@ -379,10 +379,10 @@ public void validateAndAddBlocks(
log.debug("<received-bodies size={} node={}>", _bodies.size(), _displayId);

// the requests are made such that the size varies to better map headers to bodies
byte[] firstNodeRoot = BlockUtil.getTxTrieRootFromUnsafeSource(_bodies.get(0));
ByteArrayWrapper firstNodeRoot = ByteArrayWrapper.wrap(BlockUtil.getTxTrieRootFromUnsafeSource(_bodies.get(0)));
List<BlockHeader> headers = syncHeaderRequestManager.matchAndDropHeaders(_nodeIdHashcode, _bodies.size(), firstNodeRoot);
if (headers == null) {
log.debug("<assemble-and-validate-blocks could not match headers for node={} size={} txTrieRoot={}>", _displayId, _bodies.size(), ByteArrayWrapper.wrap(firstNodeRoot));
log.debug("<assemble-and-validate-blocks could not match headers for node={} size={} txTrieRoot={}>", _displayId, _bodies.size(), firstNodeRoot);
return;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import static com.google.common.truth.Truth.assertThat;
import static com.google.common.truth.Truth.assertWithMessage;
import static org.aion.base.ConstantUtil.EMPTY_TRIE_HASH;
import static org.aion.zero.impl.sync.SyncHeaderRequestManager.CLOSE_OVERLAPPING_BLOCKS;
import static org.aion.zero.impl.sync.SyncHeaderRequestManager.FAR_OVERLAPPING_BLOCKS;
import static org.aion.zero.impl.sync.SyncHeaderRequestManager.MAX_REQUEST_SIZE;
Expand All @@ -16,8 +15,10 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.aion.base.ConstantUtil;
import org.aion.mcf.blockchain.BlockHeader;
import org.aion.p2p.INode;
import org.aion.util.types.ByteArrayWrapper;
import org.apache.commons.lang3.tuple.Pair;
import org.junit.Before;
import org.junit.Test;
Expand All @@ -32,6 +33,8 @@
*/
public class SyncHeaderRequestManagerTest {

private static final ByteArrayWrapper EMPTY_TRIE_HASH = ByteArrayWrapper.wrap(ConstantUtil.EMPTY_TRIE_HASH);

Logger syncLog, surveyLog;
SyncHeaderRequestManager srm;

Expand Down Expand Up @@ -182,7 +185,7 @@ public void test_receivedHeaderManagement() {
List<BlockHeader> list = mock(List.class);
when(list.size()).thenReturn(10);
BlockHeader header = mock(BlockHeader.class);
when(header.getTxTrieRoot()).thenReturn(EMPTY_TRIE_HASH);
when(header.getTxTrieRootWrapper()).thenReturn(EMPTY_TRIE_HASH);
when(list.get(0)).thenReturn(header);

srm.storeHeaders(1, list);
Expand All @@ -195,7 +198,7 @@ public void test_receivedHeaderManagement() {
@Test
public void test_storeThenRetrieveEachWithSameSize() {
BlockHeader header = mock(BlockHeader.class);
when(header.getTxTrieRoot()).thenReturn(EMPTY_TRIE_HASH);
when(header.getTxTrieRootWrapper()).thenReturn(EMPTY_TRIE_HASH);
List<BlockHeader> list = mock(List.class);
when(list.size()).thenReturn(10);
when(list.get(0)).thenReturn(header);
Expand All @@ -219,7 +222,7 @@ public void test_storeThenRetrieveEachWithSameSize() {
@Test
public void test_storeMultipleThenRetrieveAllWithSameSize() {
BlockHeader header = mock(BlockHeader.class);
when(header.getTxTrieRoot()).thenReturn(EMPTY_TRIE_HASH);
when(header.getTxTrieRootWrapper()).thenReturn(EMPTY_TRIE_HASH);
List<BlockHeader> list = mock(List.class);
when(list.size()).thenReturn(10);
when(list.get(0)).thenReturn(header);
Expand All @@ -243,7 +246,7 @@ public void test_multipleSizeHeaderResponses() {
List<BlockHeader> list = mock(List.class);
when(list.size()).thenReturn(10);
BlockHeader header = mock(BlockHeader.class);
when(header.getTxTrieRoot()).thenReturn(EMPTY_TRIE_HASH);
when(header.getTxTrieRootWrapper()).thenReturn(EMPTY_TRIE_HASH);
when(list.get(0)).thenReturn(header);

srm.storeHeaders(1, list);
Expand All @@ -265,11 +268,11 @@ public void test_missingHeadersTrieRoot() {
List<BlockHeader> list = mock(List.class);
when(list.size()).thenReturn(10);
BlockHeader header = mock(BlockHeader.class);
when(header.getTxTrieRoot()).thenReturn(EMPTY_TRIE_HASH);
when(header.getTxTrieRootWrapper()).thenReturn(EMPTY_TRIE_HASH);
when(list.get(0)).thenReturn(header);

srm.storeHeaders(1, list);
assertThat(srm.matchAndDropHeaders(1, 10, new byte[32])).isNull();
assertThat(srm.matchAndDropHeaders(1, 10, ByteArrayWrapper.wrap(new byte[32]))).isNull();
}

@Test
Expand All @@ -283,7 +286,7 @@ public void test_getHeadersForBodiesRequests() {
List<BlockHeader> list1 = mock(List.class);
when(list1.size()).thenReturn(10);
BlockHeader header = mock(BlockHeader.class);
when(header.getTxTrieRoot()).thenReturn(EMPTY_TRIE_HASH);
when(header.getTxTrieRootWrapper()).thenReturn(EMPTY_TRIE_HASH);
when(list1.get(0)).thenReturn(header);
List<BlockHeader> list2 = mock(List.class);
when(list2.size()).thenReturn(10);
Expand Down Expand Up @@ -334,7 +337,7 @@ public void testDropHeaders_withEmptyList() {
List<BlockHeader> list = mock(List.class);
when(list.size()).thenReturn(10);
BlockHeader header = mock(BlockHeader.class);
when(header.getTxTrieRoot()).thenReturn(EMPTY_TRIE_HASH);
when(header.getTxTrieRootWrapper()).thenReturn(EMPTY_TRIE_HASH);
when(list.get(0)).thenReturn(header);

// headers stored for size 10
Expand Down
44 changes: 22 additions & 22 deletions modAionImpl/test/org/aion/zero/impl/sync/SyncMgrTest.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package org.aion.zero.impl.sync;

import static com.google.common.truth.Truth.assertThat;
import static org.aion.base.ConstantUtil.EMPTY_TRIE_HASH;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.ArgumentMatchers.anyString;
Expand All @@ -17,6 +15,7 @@
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;
import org.aion.base.ConstantUtil;
import org.aion.evtmgr.IEventMgr;
import org.aion.log.AionLoggerFactory;
import org.aion.log.LogEnum;
Expand All @@ -32,7 +31,6 @@
import org.aion.zero.impl.sync.SyncHeaderRequestManager.SyncMode;
import org.aion.zero.impl.sync.msg.ReqBlocksBodies;
import org.aion.zero.impl.types.A0BlockHeader;
import org.aion.zero.impl.types.AionBlock;
import org.junit.Before;
import org.junit.Test;
import org.mockito.Mock;
Expand All @@ -45,6 +43,8 @@
*/
public class SyncMgrTest {

private static final ByteArrayWrapper EMPTY_TRIE_HASH = ByteArrayWrapper.wrap(ConstantUtil.EMPTY_TRIE_HASH);

@Mock
AionBlockchainImpl chain;
@Mock
Expand Down Expand Up @@ -82,7 +82,7 @@ public void testRequestBodies_withOneRequest() {
when(header.getNumber()).thenReturn(101L);
byte[] hash = Hex.decode("6fd8dae3304a9864f460ec7aec21bc94e14e34876e5dddd0a74d9c68ac7bc9ed");
when(header.getHash()).thenReturn(hash);
when(header.getTxTrieRoot()).thenReturn(EMPTY_TRIE_HASH);
when(header.getTxTrieRootWrapper()).thenReturn(EMPTY_TRIE_HASH);
List<BlockHeader> list = new ArrayList<>();
list.add(header);
syncMgr.syncHeaderRequestManager.storeHeaders(1, list);
Expand All @@ -101,7 +101,7 @@ public void testRequestBodies_withOneRequestFromMultipleOptions() {
when(header1.getNumber()).thenReturn(101L);
byte[] hash1 = Hex.decode("6fd8dae3304a9864f460ec7aec21bc94e14e34876e5dddd0a74d9c68ac7bc9ed");
when(header1.getHash()).thenReturn(hash1);
when(header1.getTxTrieRoot()).thenReturn(EMPTY_TRIE_HASH);
when(header1.getTxTrieRootWrapper()).thenReturn(EMPTY_TRIE_HASH);
List<BlockHeader> list1 = new ArrayList<>();
list1.add(header1);
syncMgr.syncHeaderRequestManager.storeHeaders(1, list1);
Expand All @@ -110,7 +110,7 @@ public void testRequestBodies_withOneRequestFromMultipleOptions() {
when(header2.getNumber()).thenReturn(102L);
byte[] hash2 = Hex.decode("f2652dde61042e9306dce95ecdc41a1be2be7eb374f19427aef2a79101b471ea");
when(header2.getHash()).thenReturn(hash2);
when(header2.getTxTrieRoot()).thenReturn(EMPTY_TRIE_HASH);
when(header2.getTxTrieRootWrapper()).thenReturn(EMPTY_TRIE_HASH);
List<BlockHeader> list2 = new ArrayList<>();
list2.add(header2);
syncMgr.syncHeaderRequestManager.storeHeaders(1, list2);
Expand All @@ -130,7 +130,7 @@ public void testRequestBodies_withTwoRequests() {
when(header1.getNumber()).thenReturn(101L);
byte[] hash1 = Hex.decode("6fd8dae3304a9864f460ec7aec21bc94e14e34876e5dddd0a74d9c68ac7bc9ed");
when(header1.getHash()).thenReturn(hash1);
when(header1.getTxTrieRoot()).thenReturn(EMPTY_TRIE_HASH);
when(header1.getTxTrieRootWrapper()).thenReturn(EMPTY_TRIE_HASH);
List<BlockHeader> list1 = new ArrayList<>();
list1.add(header1);
syncMgr.syncHeaderRequestManager.storeHeaders(1, list1);
Expand All @@ -139,7 +139,7 @@ public void testRequestBodies_withTwoRequests() {
when(header2.getNumber()).thenReturn(102L);
byte[] hash2 = Hex.decode("f2652dde61042e9306dce95ecdc41a1be2be7eb374f19427aef2a79101b471ea");
when(header2.getHash()).thenReturn(hash2);
when(header2.getTxTrieRoot()).thenReturn(EMPTY_TRIE_HASH);
when(header2.getTxTrieRootWrapper()).thenReturn(EMPTY_TRIE_HASH);
List<BlockHeader> list2 = new ArrayList<>();
list2.add(header1);
list2.add(header2);
Expand All @@ -160,7 +160,7 @@ public void testRequestBodies_withFilteringOnBlockHash() {
when(header.getNumber()).thenReturn(100L);
byte[] hash = Hex.decode("6fd8dae3304a9864f460ec7aec21bc94e14e34876e5dddd0a74d9c68ac7bc9ed");
when(header.getHash()).thenReturn(hash);
when(header.getTxTrieRoot()).thenReturn(EMPTY_TRIE_HASH);
when(header.getTxTrieRootWrapper()).thenReturn(EMPTY_TRIE_HASH);
List<BlockHeader> list = new ArrayList<>();
list.add(header);
syncMgr.syncHeaderRequestManager.storeHeaders(1, list);
Expand All @@ -179,7 +179,7 @@ public void testRequestBodies_withFilteringOnBlockNumber() {
when(header.getNumber()).thenReturn(bestBlockNumber);
byte[] hash = Hex.decode("6fd8dae3304a9864f460ec7aec21bc94e14e34876e5dddd0a74d9c68ac7bc9ed");
when(header.getHash()).thenReturn(hash);
when(header.getTxTrieRoot()).thenReturn(EMPTY_TRIE_HASH);
when(header.getTxTrieRootWrapper()).thenReturn(EMPTY_TRIE_HASH);
List<BlockHeader> list = new ArrayList<>();
list.add(header);
INode peer1 = mock(INode.class);
Expand All @@ -204,10 +204,10 @@ public void testRequestBodies_withFilteringOnBlockNumberAndRemainingHeaders() {
when(header1.getNumber()).thenReturn(bestBlockNumber);
byte[] hash1 = Hex.decode("6fd8dae3304a9864f460ec7aec21bc94e14e34876e5dddd0a74d9c68ac7bc9ed");
when(header1.getHash()).thenReturn(hash1);
when(header1.getTxTrieRoot()).thenReturn(EMPTY_TRIE_HASH);
when(header1.getTxTrieRootWrapper()).thenReturn(EMPTY_TRIE_HASH);
BlockHeader header2 = mock(BlockHeader.class);
when(header2.getNumber()).thenReturn(bestBlockNumber + 1);
when(header2.getTxTrieRoot()).thenReturn(EMPTY_TRIE_HASH);
when(header2.getTxTrieRootWrapper()).thenReturn(EMPTY_TRIE_HASH);
byte[] hash2 = Hex.decode("f2652dde61042e9306dce95ecdc41a1be2be7eb374f19427aef2a79101b471ea");
when(header2.getHash()).thenReturn(hash2);
List<BlockHeader> list = new ArrayList<>();
Expand Down Expand Up @@ -236,12 +236,12 @@ public void testRequestBodies_withFilteringOnBlockHashAndReadHeaders() {
when(header1.getNumber()).thenReturn(100L);
byte[] hash1 = Hex.decode("6fd8dae3304a9864f460ec7aec21bc94e14e34876e5dddd0a74d9c68ac7bc9ed");
when(header1.getHash()).thenReturn(hash1);
when(header1.getTxTrieRoot()).thenReturn(EMPTY_TRIE_HASH);
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.getTxTrieRoot()).thenReturn(EMPTY_TRIE_HASH);
when(header2.getTxTrieRootWrapper()).thenReturn(EMPTY_TRIE_HASH);

List<BlockHeader> list = new ArrayList<>();
list.add(header1);
Expand Down Expand Up @@ -286,7 +286,7 @@ public void testValidateAndAddHeaders_withCorrectHeaders() {
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).getTxTrieRoot());
List<BlockHeader> stored = syncMgr.syncHeaderRequestManager.matchAndDropHeaders(nodeId, consecutiveHeaders.size(), consecutiveHeaders.get(0).getTxTrieRootWrapper());
assertThat(stored.size()).isEqualTo(consecutiveHeaders.size());
assertThat(stored).containsAllIn(consecutiveHeaders);
}
Expand All @@ -307,8 +307,8 @@ public void testValidateAndAddHeaders_withNonSequentialHeaders() {
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).getTxTrieRoot())).isNull();
List<BlockHeader> stored = syncMgr.syncHeaderRequestManager.matchAndDropHeaders(nodeId, sequentialHeaders.size(), sequentialHeaders.get(0).getTxTrieRoot());
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);
Expand All @@ -331,8 +331,8 @@ public void testValidateAndAddHeaders_withInvalidHeader() {
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).getTxTrieRoot())).isNull();
List<BlockHeader> stored = syncMgr.syncHeaderRequestManager.matchAndDropHeaders(nodeId, sequentialHeaders.size(), sequentialHeaders.get(0).getTxTrieRoot());
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);
}
Expand All @@ -359,9 +359,9 @@ public void testValidateAndAddHeaders_withImportedBlocks() {
verify(p2pMgr, never()).errCheck(nodeId, displayId);

// Check that the sequential subset of headers was stored.
assertThat(syncMgr.syncHeaderRequestManager.matchAndDropHeaders(nodeId, importedBlocks.size(), importedBlocks.get(0).getTxTrieRoot())).isNull();
assertThat(syncMgr.syncHeaderRequestManager.matchAndDropHeaders(nodeId, headers.size(), headers.get(0).getTxTrieRoot())).isNull();
List<BlockHeader> stored = syncMgr.syncHeaderRequestManager.matchAndDropHeaders(nodeId, newHeaders.size(), newHeaders.get(0).getTxTrieRoot());
assertThat(syncMgr.syncHeaderRequestManager.matchAndDropHeaders(nodeId, importedBlocks.size(), importedBlocks.get(0).getTxTrieRootWrapper())).isNull();
assertThat(syncMgr.syncHeaderRequestManager.matchAndDropHeaders(nodeId, headers.size(), headers.get(0).getTxTrieRootWrapper())).isNull();
List<BlockHeader> stored = syncMgr.syncHeaderRequestManager.matchAndDropHeaders(nodeId, newHeaders.size(), newHeaders.get(0).getTxTrieRootWrapper());
assertThat(stored.size()).isEqualTo(newHeaders.size());
assertThat(stored).containsAllIn(newHeaders);
}
Expand Down

0 comments on commit 998e0e9

Please sign in to comment.