Skip to content

Commit

Permalink
Add tests for getTwoGenerationBlocksByHashWithInfo
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexandraRoatis committed Mar 12, 2020
1 parent d4fbdd4 commit be87c4a
Show file tree
Hide file tree
Showing 2 changed files with 105 additions and 8 deletions.
15 changes: 8 additions & 7 deletions modAionImpl/src/org/aion/zero/impl/db/AionBlockStore.java
Original file line number Diff line number Diff line change
Expand Up @@ -1429,18 +1429,19 @@ public final Block[] getThreeGenerationBlocksByHashWithInfo(byte[] hash) {
}

/**
* Retrieve two generation blocks with unity protocol info with one lock.
* Retrieves two generation blocks with unity protocol info.
* <p>
* Always returns a 2-element array. If the blocks cannot be retrieved the array will contain null values.
* Block[0] is the parent block and has the given hash. Block[1] is the grandparent block.
*
* @param hash given hash of the block
* @return the 2 generation block data have matched hash with unity protocol info. Block[0] is
* the parent block, Block[1] is the grandParent block. The return might only contain the
* parent block and still return the 2-elements array.
* @param hash the hash of the parent block
* @return the retrieved two generation blocks with unity protocol info
*/
public final Block[] getTwoGenerationBlocksByHashWithInfo(byte[] hash) {
Block[] blockFamily = new Block[] { null, null};
if (hash == null) {
return null;
return blockFamily;
}
Block[] blockFamily = new Block[] { null, null};

lock.lock();

Expand Down
98 changes: 97 additions & 1 deletion modAionImpl/test/org/aion/zero/impl/db/AionBlockStoreTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import org.aion.mcf.blockchain.Block;
import org.aion.util.TestResources;
import org.aion.util.types.AddressUtils;
import org.aion.util.types.ByteArrayWrapper;
import org.aion.zero.impl.types.AionBlock;
import org.aion.zero.impl.types.BlockUtil;
import org.apache.commons.lang3.RandomUtils;
Expand Down Expand Up @@ -466,4 +467,99 @@ public static void assertConcurrent(
message + "failed with " + exceptions.size() + " exception(s):" + exceptions,
exceptions.isEmpty());
}
}

@Test
public void testGetTwoGenerationBlocksByHashWithInfo_withNullInput() {
AionBlockStore store = new AionBlockStore(index, blocks, false);
Block[] blocks = store.getTwoGenerationBlocksByHashWithInfo(null);
assertThat(blocks.length).isEqualTo(2);
assertThat(blocks[0]).isNull();
assertThat(blocks[1]).isNull();
}

@Test
public void testGetTwoGenerationBlocksByHashWithInfo_withMissingParent() {
byte[] parentHash = RandomUtils.nextBytes(32);

AionBlockStore store = new AionBlockStore(index, blocks, false);
assertThat(index.isEmpty()).isTrue();
assertThat(blocks.isEmpty()).isTrue();

Block[] blocks = store.getTwoGenerationBlocksByHashWithInfo(parentHash);
assertThat(blocks.length).isEqualTo(2);
assertThat(blocks[0]).isNull();
assertThat(blocks[1]).isNull();
}

@Test
public void testGetTwoGenerationBlocksByHashWithInfo_withMissingGrandParent() {
Block parent = consecutiveBlocks.get(0);

AionBlockStore store = new AionBlockStore(index, blocks, false);
// does not require accurate total difficulty
store.saveBlock(parent, BigInteger.TEN, true);

Block[] blocks = store.getTwoGenerationBlocksByHashWithInfo(parent.getHash());
assertThat(blocks.length).isEqualTo(2);
assertThat(blocks[0]).isEqualTo(parent);
assertThat(blocks[0].getTotalDifficulty()).isEqualTo(BigInteger.TEN);
assertThat(blocks[0].isMainChain()).isTrue();
assertThat(blocks[1]).isNull();
}

@Test
public void testGetTwoGenerationBlocksByHashWithInfo() {
Block grandparent = consecutiveBlocks.get(0);
Block parent = consecutiveBlocks.get(1);

AionBlockStore store = new AionBlockStore(index, blocks, false);
// does not require accurate total difficulty
store.saveBlock(grandparent, BigInteger.TWO, true);
store.saveBlock(parent, BigInteger.TEN, true);

Block[] blocks = store.getTwoGenerationBlocksByHashWithInfo(parent.getHash());
assertThat(blocks.length).isEqualTo(2);
assertThat(blocks[0]).isEqualTo(parent);
assertThat(blocks[0].getTotalDifficulty()).isEqualTo(BigInteger.TEN);
assertThat(blocks[0].isMainChain()).isTrue();
assertThat(blocks[1]).isEqualTo(grandparent);
assertThat(blocks[1].getTotalDifficulty()).isEqualTo(BigInteger.TWO);
assertThat(blocks[1].isMainChain()).isTrue();
}

@Test
public void testGetTwoGenerationBlocksByHashWithInfo_withSidechainGrandparent() {
Block grandparent = consecutiveBlocks.get(0);
Block parent = consecutiveBlocks.get(1);

Block sideGrandparent = spy(grandparent);
byte[] newHash = RandomUtils.nextBytes(32);
when(sideGrandparent.getHash()).thenReturn(newHash);
when(sideGrandparent.getHashWrapper()).thenReturn(ByteArrayWrapper.wrap(newHash));
assertThat(grandparent.getHash()).isNotEqualTo(sideGrandparent.getHash());

Block sideParent = spy(parent);
newHash = RandomUtils.nextBytes(32);
when(sideParent.getHash()).thenReturn(newHash);
when(sideParent.getHashWrapper()).thenReturn(ByteArrayWrapper.wrap(newHash));
assertThat(parent.getHash()).isNotEqualTo(sideParent.getHash());

AionBlockStore store = new AionBlockStore(index, blocks, false);
// does not require accurate total difficulty
store.saveBlock(grandparent, BigInteger.TWO, false);
store.saveBlock(sideGrandparent, sideGrandparent.getTotalDifficulty(), true);
store.saveBlock(parent, BigInteger.TEN, false);
store.saveBlock(sideParent, sideParent.getTotalDifficulty(), true);

Block[] blocks = store.getTwoGenerationBlocksByHashWithInfo(parent.getHash());
assertThat(blocks.length).isEqualTo(2);
assertThat(blocks[0]).isEqualTo(parent);
assertThat(blocks[0].getHash()).isEqualTo(parent.getHash());
assertThat(blocks[0].getTotalDifficulty()).isEqualTo(BigInteger.TEN);
assertThat(blocks[0].isMainChain()).isFalse();
assertThat(blocks[1]).isEqualTo(grandparent);
assertThat(blocks[1].getHash()).isEqualTo(grandparent.getHash());
assertThat(blocks[1].getTotalDifficulty()).isEqualTo(BigInteger.TWO);
assertThat(blocks[1].isMainChain()).isFalse();
}
}

0 comments on commit be87c4a

Please sign in to comment.