Search in sources :

Example 41 with Block

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();
}
Also used : MiningBlock(org.aion.zero.impl.types.MiningBlock) Block(org.aion.zero.impl.types.Block) Test(org.junit.Test)

Example 42 with Block

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();
}
Also used : MiningBlock(org.aion.zero.impl.types.MiningBlock) Block(org.aion.zero.impl.types.Block) Test(org.junit.Test)

Example 43 with Block

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);
}
Also used : MiningBlock(org.aion.zero.impl.types.MiningBlock) Block(org.aion.zero.impl.types.Block) Test(org.junit.Test)

Example 44 with Block

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);
}
Also used : MiningBlock(org.aion.zero.impl.types.MiningBlock) Block(org.aion.zero.impl.types.Block) Test(org.junit.Test)

Example 45 with Block

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());
}
Also used : MiningBlock(org.aion.zero.impl.types.MiningBlock) Block(org.aion.zero.impl.types.Block) Test(org.junit.Test)

Aggregations

Block (org.aion.zero.impl.types.Block)283 MiningBlock (org.aion.zero.impl.types.MiningBlock)155 Test (org.junit.Test)148 AionTransaction (org.aion.base.AionTransaction)106 ImportResult (org.aion.zero.impl.core.ImportResult)86 ArrayList (java.util.ArrayList)63 AionAddress (org.aion.types.AionAddress)61 StakingBlock (org.aion.zero.impl.types.StakingBlock)58 AionBlockSummary (org.aion.zero.impl.types.AionBlockSummary)57 BigInteger (java.math.BigInteger)55 AionRepositoryImpl (org.aion.zero.impl.db.AionRepositoryImpl)34 ByteArrayWrapper (org.aion.util.types.ByteArrayWrapper)30 AionTxReceipt (org.aion.base.AionTxReceipt)29 Hex.toHexString (org.aion.util.conversions.Hex.toHexString)28 AccountState (org.aion.base.AccountState)26 EventBlock (org.aion.evtmgr.impl.evt.EventBlock)26 JSONArray (org.json.JSONArray)26 JSONObject (org.json.JSONObject)26 MiningBlockHeader (org.aion.zero.impl.types.MiningBlockHeader)22 AionTxExecSummary (org.aion.base.AionTxExecSummary)20