Search in sources :

Example 66 with MiningBlock

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

the class BlockchainImplementationTest method testGetListOfHeadersEndWith_wGenesisBlock.

@Test
public void testGetListOfHeadersEndWith_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, 6, 2, accounts, MAX_TX_PER_BLOCK);
    MiningBlock genesis = chain.getGenesis();
    byte[] genesisHash = genesis.getHash();
    int qty;
    // get one block
    qty = 1;
    assertEndWith_expectedOne(chain, genesisHash, qty);
    // get list of 10 from genesis
    // larger than block height
    qty = 10;
    assertEndWith_expectedOne(chain, genesisHash, qty);
    // get list of -10 from genesis
    // negative value
    qty = -10;
    assertEndWith_expectedOne(chain, genesisHash, qty);
    // get list of 0 from genesis
    // zero blocks requested
    qty = 0;
    assertEndWith_expectedOne(chain, genesisHash, qty);
}
Also used : MiningBlock(org.aion.zero.impl.types.MiningBlock) Test(org.junit.Test)

Example 67 with MiningBlock

use of org.aion.zero.impl.types.MiningBlock 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 68 with MiningBlock

use of org.aion.zero.impl.types.MiningBlock 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)

Example 69 with MiningBlock

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

the class BlockchainIntegrationTest method testCreateNewEmptyBlock.

@Test
public void testCreateNewEmptyBlock() {
    StandaloneBlockchain.Bundle bundle = (new StandaloneBlockchain.Builder()).withDefaultAccounts().build();
    StandaloneBlockchain bc = bundle.bc;
    MiningBlock block = bc.createNewMiningBlock(bc.getBestBlock(), Collections.EMPTY_LIST, true);
    assertThat(block.getParentHash()).isEqualTo(bc.getGenesis().getHash());
}
Also used : MiningBlock(org.aion.zero.impl.types.MiningBlock) Test(org.junit.Test)

Example 70 with MiningBlock

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

the class BlockchainIntegrationTest method testSimpleFailedTransactionInsufficientBalance.

@Test
public void testSimpleFailedTransactionInsufficientBalance() {
    // generate a recipient
    final AionAddress receiverAddress = AddressUtils.wrapAddress("CAFECAFECAFECAFECAFECAFECAFECAFECAFECAFECAFECAFECAFECAFECAFECAFE");
    StandaloneBlockchain.Bundle bundle = (new StandaloneBlockchain.Builder()).withValidatorConfiguration("simple").withDefaultAccounts().build();
    StandaloneBlockchain bc = bundle.bc;
    // (byte[] nonce, byte[] from, byte[] to, byte[] value, byte[] data)
    ECKey key = bundle.privateKeys.get(0);
    AionTransaction tx = AionTransaction.create(key, BigInteger.valueOf(1).toByteArray(), receiverAddress, BigInteger.valueOf(100).toByteArray(), ByteUtil.EMPTY_BYTE_ARRAY, 1L, energyPrice, TransactionTypes.DEFAULT, null);
    MiningBlock block = bc.createNewMiningBlock(bc.getBestBlock(), Collections.singletonList(tx), true);
    assertThat(block.getTransactionsList()).isEmpty();
    assertThat(block.getTxTrieRoot()).isEqualTo(ConstantUtil.EMPTY_TRIE_HASH);
    ImportResult connection = bc.tryToConnect(block);
    assertThat(connection).isEqualTo(ImportResult.IMPORTED_BEST);
}
Also used : AionAddress(org.aion.types.AionAddress) ImportResult(org.aion.zero.impl.core.ImportResult) ECKey(org.aion.crypto.ECKey) AionTransaction(org.aion.base.AionTransaction) MiningBlock(org.aion.zero.impl.types.MiningBlock) Test(org.junit.Test)

Aggregations

MiningBlock (org.aion.zero.impl.types.MiningBlock)185 Test (org.junit.Test)136 AionTransaction (org.aion.base.AionTransaction)128 ImportResult (org.aion.zero.impl.core.ImportResult)81 AionAddress (org.aion.types.AionAddress)79 BigInteger (java.math.BigInteger)64 Block (org.aion.zero.impl.types.Block)63 AionBlockSummary (org.aion.zero.impl.types.AionBlockSummary)59 RepositoryCache (org.aion.base.db.RepositoryCache)41 AionTxReceipt (org.aion.base.AionTxReceipt)37 ArrayList (java.util.ArrayList)36 ECKey (org.aion.crypto.ECKey)27 AionRepositoryImpl (org.aion.zero.impl.db.AionRepositoryImpl)27 AionTxExecSummary (org.aion.base.AionTxExecSummary)25 AionRepositoryCache (org.aion.zero.impl.db.AionRepositoryCache)24 MiningBlockHeader (org.aion.zero.impl.types.MiningBlockHeader)22 BlockContext (org.aion.zero.impl.types.BlockContext)18 DataWord (org.aion.util.types.DataWord)13 MockDB (org.aion.db.impl.mockdb.MockDB)12 StakingBlock (org.aion.zero.impl.types.StakingBlock)11