use of org.aion.zero.impl.types.Block in project aion by aionnetwork.
the class AionBlockStoreTest method testGetTwoGenerationBlocksByHashWithInfo_withSidechainGrandparent.
@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();
}
use of org.aion.zero.impl.types.Block in project aion by aionnetwork.
the class AionBlockStoreTest method testGetThreeGenerationBlocksByHashWithInfo_withSidechains.
@Test
public void testGetThreeGenerationBlocksByHashWithInfo_withSidechains() {
Block greatGrandparent = consecutiveBlocks.get(0);
Block grandparent = consecutiveBlocks.get(1);
Block parent = consecutiveBlocks.get(2);
Block sideGreatGrandparent = spy(greatGrandparent);
byte[] newHash = RandomUtils.nextBytes(32);
when(sideGreatGrandparent.getHash()).thenReturn(newHash);
when(sideGreatGrandparent.getHashWrapper()).thenReturn(ByteArrayWrapper.wrap(newHash));
assertThat(greatGrandparent.getHash()).isNotEqualTo(sideGreatGrandparent.getHash());
Block sideGrandparent = spy(grandparent);
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(greatGrandparent, BigInteger.ONE, false);
store.saveBlock(grandparent, BigInteger.TWO, false);
store.saveBlock(parent, BigInteger.TEN, false);
store.saveBlock(sideGreatGrandparent, sideGreatGrandparent.getTotalDifficulty(), true);
store.saveBlock(sideGrandparent, sideGrandparent.getTotalDifficulty(), true);
store.saveBlock(sideParent, sideParent.getTotalDifficulty(), true);
Block[] blocks = store.getThreeGenerationBlocksByHashWithInfo(parent.getHash());
assertThat(blocks.length).isEqualTo(3);
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();
assertThat(blocks[2]).isEqualTo(greatGrandparent);
assertThat(blocks[2].getHash()).isEqualTo(greatGrandparent.getHash());
assertThat(blocks[2].getTotalDifficulty()).isEqualTo(BigInteger.ONE);
assertThat(blocks[2].isMainChain()).isFalse();
}
use of org.aion.zero.impl.types.Block in project aion by aionnetwork.
the class AionBlockStoreTest method testGetBlocksByRange_withAscendingOrder.
@Test
public void testGetBlocksByRange_withAscendingOrder() {
Block first = consecutiveBlocks.get(0);
Block middle = consecutiveBlocks.get(1);
Block last = consecutiveBlocks.get(2);
AionBlockStore store = spy(new AionBlockStore(index, blocks, false));
when(store.getChainBlockByNumber(first.getNumber())).thenReturn(first);
when(store.getChainBlockByNumber(last.getNumber())).thenReturn(last);
when(store.getBlockByHashWithInfo(last.getParentHash())).thenReturn(middle);
when(store.getBlocksByRange(first.getNumber(), last.getNumber())).thenCallRealMethod();
List<Block> returned = store.getBlocksByRange(first.getNumber(), last.getNumber());
assertThat(returned.size()).isEqualTo(3);
assertThat(returned.get(0)).isEqualTo(first);
assertThat(returned.get(1)).isEqualTo(middle);
assertThat(returned.get(2)).isEqualTo(last);
}
use of org.aion.zero.impl.types.Block in project aion by aionnetwork.
the class AionBlockStoreTest method testGetBlocksByRange_withAscendingOrderAndNullLast.
@Test
public void testGetBlocksByRange_withAscendingOrderAndNullLast() {
Block first = consecutiveBlocks.get(0);
Block middle = consecutiveBlocks.get(1);
Block best = consecutiveBlocks.get(2);
Block last = consecutiveBlocks.get(3);
AionBlockStore store = spy(new AionBlockStore(index, blocks, false));
when(store.getChainBlockByNumber(first.getNumber())).thenReturn(first);
when(store.getChainBlockByNumber(last.getNumber())).thenReturn(null);
when(store.getBestBlock()).thenReturn(best);
when(store.getBlockByHashWithInfo(best.getParentHash())).thenReturn(middle);
when(store.getBlocksByRange(first.getNumber(), last.getNumber())).thenCallRealMethod();
List<Block> returned = store.getBlocksByRange(first.getNumber(), last.getNumber());
assertThat(returned.size()).isEqualTo(3);
assertThat(returned.get(0)).isEqualTo(first);
assertThat(returned.get(1)).isEqualTo(middle);
assertThat(returned.get(2)).isEqualTo(best);
}
use of org.aion.zero.impl.types.Block in project aion by aionnetwork.
the class AionBlockStoreTest method testGetBlocksByRange_withDescendingOrderAndGenesisLast.
@Test
public void testGetBlocksByRange_withDescendingOrderAndGenesisLast() {
// assigning it height 2
Block first = consecutiveBlocks.get(2);
// assumed height 1
Block middle = consecutiveBlocks.get(1);
// assumed height 0
Block last = consecutiveBlocks.get(0);
AionBlockStore store = spy(new AionBlockStore(index, blocks, false));
// returning the block at a different number than its height
when(store.getChainBlockByNumber(2L)).thenReturn(first);
when(store.getBlockByHashWithInfo(first.getParentHash())).thenReturn(middle);
when(store.getBlocksByRange(2L, 0L)).thenCallRealMethod();
// the returned list has only 2 elements due to the null
List<Block> returned = store.getBlocksByRange(2L, 0L);
assertThat(returned.size()).isEqualTo(2);
assertThat(returned.get(0)).isEqualTo(first);
assertThat(returned.get(1)).isEqualTo(middle);
// there should be no attempt to retrieve the genesis
verify(store, times(0)).getBlockByHashWithInfo(last.getParentHash());
verify(store, times(0)).getBlockByHashWithInfo(middle.getParentHash());
verify(store, times(1)).getBlockByHashWithInfo(first.getParentHash());
}
Aggregations