Search in sources :

Example 61 with Block

use of org.aion.zero.impl.types.Block in project aion by aionnetwork.

the class StandaloneBlockchainTest method testBlockChainShutdownhook.

@Test
public void testBlockChainShutdownhook() {
    exit.expectSystemExitWithStatus(SystemExitCodes.NORMAL);
    StandaloneBlockchain.Builder builder = new StandaloneBlockchain.Builder();
    StandaloneBlockchain.Bundle bundle = builder.withValidatorConfiguration("simple").withDefaultAccounts().build();
    StandaloneBlockchain sb = bundle.bc;
    AionBlockchainImpl.shutdownHook = 2;
    Block block1 = sb.createBlock(sb.genesis, Collections.EMPTY_LIST, false, TimeUnit.MILLISECONDS.toSeconds(System.currentTimeMillis()));
    ImportResult result = sb.tryToConnect(block1);
    assertThat(result.isBest()).isTrue();
    Block block2 = sb.createBlock(block1, Collections.EMPTY_LIST, false, TimeUnit.MILLISECONDS.toSeconds(System.currentTimeMillis()));
    // expect the system will be shutdown when import the block2
    sb.tryToConnect(new BlockWrapper(block2));
}
Also used : ImportResult(org.aion.zero.impl.core.ImportResult) Block(org.aion.zero.impl.types.Block) Test(org.junit.Test)

Example 62 with Block

use of org.aion.zero.impl.types.Block in project aion by aionnetwork.

the class BlockchainImplementationTest method testGetListOfHeadersEndWith_wBestBlock.

@Test
public void testGetListOfHeadersEndWith_wBestBlock() {
    StandaloneBlockchain.Builder builder = new StandaloneBlockchain.Builder();
    StandaloneBlockchain.Bundle bundle = builder.withValidatorConfiguration("simple").withDefaultAccounts(accounts).build();
    StandaloneBlockchain chain = bundle.bc;
    // populate chain at random
    generateRandomChain(chain, 12, 2, accounts, MAX_TX_PER_BLOCK);
    Block best = chain.getBestBlock();
    assertThat(best.getNumber()).isGreaterThan(0L);
    byte[] bestHash = best.getHash();
    // expected result with qty >= chain height
    List<ByteArrayWrapper> expectedAll = new ArrayList<>();
    Block block = best;
    while (block.getNumber() > 0) {
        expectedAll.add(ByteArrayWrapper.wrap(block.getHash()));
        block = chain.getBlockByHash(block.getParentHash());
    }
    int qty;
    // get one block
    qty = 1;
    assertEndWith_expectedOne(chain, bestHash, qty);
    // get list of chain height from best block
    // chain height
    qty = (int) best.getNumber();
    assertEndWith(chain, bestHash, qty, expectedAll);
    // get list of half from best block
    qty = (int) (best.getNumber() / 2);
    List<ByteArrayWrapper> expectedHalf = new ArrayList<>();
    block = best;
    for (int i = 0; i < qty; i++) {
        expectedHalf.add(ByteArrayWrapper.wrap(block.getHash()));
        block = chain.getBlockByHash(block.getParentHash());
    }
    assertEndWith(chain, bestHash, qty, expectedHalf);
    // get list of 20 from genesis
    // larger than block height
    qty = 24;
    expectedAll.add(ByteArrayWrapper.wrap(chain.getGenesis().getHash()));
    assertEndWith(chain, bestHash, qty, expectedAll);
    // get list of -10 from best block
    // negative value
    qty = -10;
    assertEndWith_expectedOne(chain, bestHash, qty);
    // get list of 0 from best block
    // zero blocks requested
    qty = 0;
    assertEndWith_expectedOne(chain, bestHash, qty);
}
Also used : ByteArrayWrapper(org.aion.util.types.ByteArrayWrapper) ArrayList(java.util.ArrayList) MiningBlock(org.aion.zero.impl.types.MiningBlock) Block(org.aion.zero.impl.types.Block) Test(org.junit.Test)

Example 63 with Block

use of org.aion.zero.impl.types.Block in project aion by aionnetwork.

the class BlockchainImplementationTest method testGetListOfHeadersStartFromBlock_wGenesisBlock.

@Test
public void testGetListOfHeadersStartFromBlock_wGenesisBlock() {
    StandaloneBlockchain.Builder builder = new StandaloneBlockchain.Builder();
    StandaloneBlockchain.Bundle bundle = builder.withValidatorConfiguration("simple").withDefaultAccounts(accounts).build();
    StandaloneBlockchain chain = bundle.bc;
    // populate chain at random
    generateRandomChain(chain, 12, 2, accounts, MAX_TX_PER_BLOCK);
    MiningBlock genesis = chain.getGenesis();
    Block best = chain.getBestBlock();
    assertThat(best.getNumber()).isGreaterThan(0L);
    byte[] genesisHash = genesis.getHash();
    // expected result with qty >= chain height
    List<ByteArrayWrapper> expectedAll = new ArrayList<>();
    Block block = chain.getBlockByHash(best.getParentHash());
    while (block.getNumber() > 0) {
        expectedAll.add(0, ByteArrayWrapper.wrap(block.getHash()));
        block = chain.getBlockByHash(block.getParentHash());
    }
    expectedAll.add(0, ByteArrayWrapper.wrap(genesis.getHash()));
    int qty;
    // get one block
    qty = 1;
    assertStartFromBlock_expectedOne(chain, genesisHash, 0, qty);
    // get list of chain height from genesis block
    // chain height
    qty = (int) best.getNumber();
    assertStartFromBlock(chain, 0, qty, expectedAll);
    // get list of half from genesis block
    qty = (int) (best.getNumber() / 2);
    List<ByteArrayWrapper> expectedHalf = new ArrayList<>();
    block = chain.getBlockByNumber(qty - 1);
    for (int i = 0; i < qty; i++) {
        expectedHalf.add(0, ByteArrayWrapper.wrap(block.getHash()));
        block = chain.getBlockByHash(block.getParentHash());
    }
    assertStartFromBlock(chain, 0, qty, expectedHalf);
    // get list of 20 from genesis
    // larger than block height
    qty = 24;
    expectedAll.add(ByteArrayWrapper.wrap(best.getHash()));
    assertStartFromBlock(chain, 0, qty, expectedAll);
    // get list of -10 from genesis block
    // negative value
    qty = -10;
    assertStartFromBlock_expectedOne(chain, genesisHash, 0, qty);
    // get list of 0 from genesis block
    // zero blocks requested
    qty = 0;
    assertStartFromBlock_expectedOne(chain, genesisHash, 0, qty);
}
Also used : ByteArrayWrapper(org.aion.util.types.ByteArrayWrapper) ArrayList(java.util.ArrayList) MiningBlock(org.aion.zero.impl.types.MiningBlock) Block(org.aion.zero.impl.types.Block) MiningBlock(org.aion.zero.impl.types.MiningBlock) Test(org.junit.Test)

Example 64 with Block

use of org.aion.zero.impl.types.Block in project aion by aionnetwork.

the class BlockchainImplementationTest method testFindMissingAncestor_withFirstMissing.

@Test
public void testFindMissingAncestor_withFirstMissing() {
    StandaloneBlockchain.Builder builder = new StandaloneBlockchain.Builder();
    StandaloneBlockchain.Bundle bundle = builder.withValidatorConfiguration("simple").build();
    StandaloneBlockchain chain = bundle.bc;
    // populate chain at random
    generateRandomChainWithoutTransactions(chain, 2, 1);
    Block best = chain.getBestBlock();
    byte[] bestHash = best.getHash();
    // delete the block from the db
    ((MockDB) chain.getRepository().getBlockDatabase()).deleteAndCommit(bestHash);
    Pair<ByteArrayWrapper, Long> pair = chain.findMissingAncestor(best);
    assertThat(pair.getLeft()).isEqualTo(ByteArrayWrapper.wrap(bestHash));
    assertThat(pair.getRight()).isEqualTo(best.getNumber());
}
Also used : ByteArrayWrapper(org.aion.util.types.ByteArrayWrapper) MockDB(org.aion.db.impl.mockdb.MockDB) MiningBlock(org.aion.zero.impl.types.MiningBlock) Block(org.aion.zero.impl.types.Block) Test(org.junit.Test)

Example 65 with Block

use of org.aion.zero.impl.types.Block in project aion by aionnetwork.

the class BlockchainIntegrationTest method testDisconnection.

/**
 * This is a special case for testing that the blockchain stores disconnected blocks if we
 * violate the contract of changing the block contents after importing best
 *
 * <p>This behaviour was originally observed when running a pooled miner
 */
@Test
public void testDisconnection() {
    StandaloneBlockchain.Bundle bundle = (new StandaloneBlockchain.Builder()).withValidatorConfiguration("simple").withDefaultAccounts().build();
    StandaloneBlockchain bc = bundle.bc;
    // store this as the best block,
    MiningBlock block = bundle.bc.createNewMiningBlock(bc.getGenesis(), Collections.emptyList(), true);
    byte[] block1Hash = block.getHash();
    // now modify this block to have a different value after connecting
    ImportResult result = bundle.bc.tryToConnect(block);
    assertThat(result).isEqualTo(ImportResult.IMPORTED_BEST);
    // now modify the block in some way
    MiningBlockHeader newBlockHeader = MiningBlockHeader.Builder.newInstance().withHeader(block.getHeader()).withExtraData("other block".getBytes()).build();
    block.updateHeader(newBlockHeader);
    // now try submitting the block again
    result = bundle.bc.tryToConnect(block);
    assertThat(result).isEqualTo(ImportResult.IMPORTED_NOT_BEST);
    // now try creating a new block
    MiningBlock newBlock = bundle.bc.createNewMiningBlock(bundle.bc.getBestBlock(), Collections.emptyList(), true);
    // gets the first main-chain block
    Block storedBlock1 = bundle.bc.getBlockByNumber(1L);
    System.out.println(ByteUtil.toHexString(storedBlock1.getHash()));
    System.out.println(ByteUtil.toHexString(newBlock.getParentHash()));
    assertThat(newBlock.getParentHash()).isNotEqualTo(storedBlock1.getHash());
    assertThat(newBlock.getParentHash()).isEqualTo(block.getHash());
}
Also used : MiningBlockHeader(org.aion.zero.impl.types.MiningBlockHeader) ImportResult(org.aion.zero.impl.core.ImportResult) MiningBlock(org.aion.zero.impl.types.MiningBlock) Block(org.aion.zero.impl.types.Block) MiningBlock(org.aion.zero.impl.types.MiningBlock) 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