Search in sources :

Example 11 with MiningBlockHeader

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

the class BlockchainTestUtils method generateNextBlock.

/**
 * @param chain blockchain implementation to be populated
 * @param accounts existing accounts
 * @param txCount maximum number of transactions per block
 */
public static Block generateNextBlock(StandaloneBlockchain chain, List<ECKey> accounts, int txCount) {
    Block block, parent = chain.getBestBlock();
    AionRepositoryImpl repo = chain.getRepository();
    List<AionTransaction> txs = generateTransactions(txCount, accounts, repo);
    long time = System.currentTimeMillis();
    block = chain.createNewMiningBlockInternal(parent, txs, true, time / TEN_THOUSAND_MS).block;
    MiningBlockHeader newBlockHeader = MiningBlockHeader.Builder.newInstance().withHeader((MiningBlockHeader) block.getHeader()).withExtraData(String.valueOf(time).getBytes()).build();
    block.updateHeader(newBlockHeader);
    return block;
}
Also used : MiningBlockHeader(org.aion.zero.impl.types.MiningBlockHeader) StakingBlock(org.aion.zero.impl.types.StakingBlock) MiningBlock(org.aion.zero.impl.types.MiningBlock) Block(org.aion.zero.impl.types.Block) AionRepositoryImpl(org.aion.zero.impl.db.AionRepositoryImpl) AionTransaction(org.aion.base.AionTransaction)

Example 12 with MiningBlockHeader

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

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

the class PendingBlockStoreTest method testDropPendingQueues_wException.

@Test
public void testDropPendingQueues_wException() {
    Properties props = new Properties();
    props.setProperty(Props.DB_TYPE, DBVendor.MOCKDB.toValue());
    PendingBlockStore pb = null;
    try {
        pb = new PendingBlockStore(props);
    } catch (IOException e) {
        e.printStackTrace();
    }
    assertThat(pb.isOpen()).isTrue();
    // add first queue
    List<Block> blocks = TestResources.consecutiveBlocks(6);
    Block first = blocks.get(0);
    pb.addBlockRange(blocks);
    // add second queue
    MiningBlock altBlock = (MiningBlock) BlockUtil.newBlockFromRlp(first.getEncoded());
    MiningBlockHeader newHeader = MiningBlockHeader.Builder.newInstance().withHeader(altBlock.getHeader()).withExtraData("random".getBytes()).build();
    altBlock.updateHeader(newHeader);
    List<Block> sideChain = new ArrayList<>();
    sideChain.add(altBlock);
    pb.addBlockRange(sideChain);
    // check storage updates
    assertThat(pb.getIndexSize()).isEqualTo(7);
    assertThat(pb.getLevelSize()).isEqualTo(1);
    assertThat(pb.getQueueSize()).isEqualTo(2);
    // closing the pending block store to cause exception
    pb.close();
    // test drop functionality
    Map<ByteArrayWrapper, List<Block>> actual = pb.loadBlockRange(first.getNumber());
    pb.dropPendingQueues(first.getNumber(), actual.keySet(), actual);
}
Also used : MiningBlockHeader(org.aion.zero.impl.types.MiningBlockHeader) ByteArrayWrapper(org.aion.util.types.ByteArrayWrapper) ArrayList(java.util.ArrayList) MiningBlock(org.aion.zero.impl.types.MiningBlock) Block(org.aion.zero.impl.types.Block) ArrayList(java.util.ArrayList) List(java.util.List) IOException(java.io.IOException) Properties(java.util.Properties) MiningBlock(org.aion.zero.impl.types.MiningBlock) Test(org.junit.Test)

Example 14 with MiningBlockHeader

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

the class SyncMgrTest method testValidateAndAddHeaders_withInvalidHeader.

@Test
public void testValidateAndAddHeaders_withInvalidHeader() {
    int nodeId = 1;
    String displayId = "peer1";
    List<BlockHeader> sequentialHeaders = new ArrayList<>();
    sequentialHeaders.add(consecutiveHeaders.get(0));
    sequentialHeaders.add(consecutiveHeaders.get(1));
    // Break energy consumed rule.
    List<BlockHeader> invalidHeaderList = new ArrayList<>(sequentialHeaders);
    assertThat(consecutiveHeaders.get(2).getEnergyConsumed()).isGreaterThan(0L);
    invalidHeaderList.add(MiningBlockHeader.Builder.newInstance().withHeader((MiningBlockHeader) consecutiveHeaders.get(2)).withEnergyConsumed(0L).build());
    syncMgr.validateAndAddHeaders(nodeId, displayId, invalidHeaderList);
    verify(p2pMgr, never()).errCheck(nodeId, displayId);
    // Check that the sequential subset of headers was stored.
    assertThat(syncMgr.syncHeaderRequestManager.matchAndDropHeaders(nodeId, invalidHeaderList.size(), invalidHeaderList.get(0).getTxTrieRootWrapper())).isNull();
    List<BlockHeader> stored = syncMgr.syncHeaderRequestManager.matchAndDropHeaders(nodeId, sequentialHeaders.size(), sequentialHeaders.get(0).getTxTrieRootWrapper());
    assertThat(stored.size()).isEqualTo(sequentialHeaders.size());
    assertThat(stored).containsAllIn(sequentialHeaders);
}
Also used : MiningBlockHeader(org.aion.zero.impl.types.MiningBlockHeader) ArrayList(java.util.ArrayList) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) BlockHeader(org.aion.zero.impl.types.BlockHeader) MiningBlockHeader(org.aion.zero.impl.types.MiningBlockHeader) Test(org.junit.Test)

Example 15 with MiningBlockHeader

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

the class PendingBlockStoreTest method testLoadBlockRange.

@Test
public void testLoadBlockRange() {
    Properties props = new Properties();
    props.setProperty(Props.DB_TYPE, DBVendor.MOCKDB.toValue());
    PendingBlockStore pb = null;
    try {
        pb = new PendingBlockStore(props);
    } catch (IOException e) {
        e.printStackTrace();
    }
    assertThat(pb.isOpen()).isTrue();
    List<Block> allBlocks = TestResources.consecutiveBlocks(8);
    // 1. test with empty storage
    assertThat(pb.loadBlockRange(100)).isEmpty();
    // 2. test with valid range
    List<Block> blocks = allBlocks.subList(0, 6);
    Block first = blocks.get(0);
    assertThat(blocks.size()).isEqualTo(6);
    assertThat(pb.addBlockRange(blocks)).isEqualTo(6);
    Map<ByteArrayWrapper, List<Block>> actual = pb.loadBlockRange(first.getNumber());
    assertThat(actual.size()).isEqualTo(1);
    assertThat(actual.get(ByteArrayWrapper.wrap(first.getHash()))).isEqualTo(blocks);
    // 3. test with multiple queues
    // create side chain
    MiningBlock altBlock = (MiningBlock) BlockUtil.newBlockFromRlp(first.getEncoded());
    MiningBlockHeader newHeader = MiningBlockHeader.Builder.newInstance().withHeader(altBlock.getHeader()).withExtraData("random".getBytes()).build();
    altBlock.updateHeader(newHeader);
    assertThat(altBlock.equals(first)).isFalse();
    List<Block> sideChain = new ArrayList<>();
    sideChain.add(altBlock);
    assertThat(pb.addBlockRange(sideChain)).isEqualTo(1);
    // check functionality
    actual = pb.loadBlockRange(first.getNumber());
    assertThat(actual.size()).isEqualTo(2);
    assertThat(actual.get(ByteArrayWrapper.wrap(first.getHash()))).isEqualTo(blocks);
    assertThat(actual.get(ByteArrayWrapper.wrap(altBlock.getHash()))).isEqualTo(sideChain);
    // 4. test with empty level
    long level = first.getNumber();
    assertThat(pb.loadBlockRange(level - 1)).isEmpty();
    assertThat(pb.loadBlockRange(level + 1)).isEmpty();
}
Also used : MiningBlockHeader(org.aion.zero.impl.types.MiningBlockHeader) ByteArrayWrapper(org.aion.util.types.ByteArrayWrapper) ArrayList(java.util.ArrayList) MiningBlock(org.aion.zero.impl.types.MiningBlock) Block(org.aion.zero.impl.types.Block) ArrayList(java.util.ArrayList) List(java.util.List) IOException(java.io.IOException) Properties(java.util.Properties) MiningBlock(org.aion.zero.impl.types.MiningBlock) Test(org.junit.Test)

Aggregations

MiningBlockHeader (org.aion.zero.impl.types.MiningBlockHeader)29 MiningBlock (org.aion.zero.impl.types.MiningBlock)23 Test (org.junit.Test)20 Block (org.aion.zero.impl.types.Block)14 AionTransaction (org.aion.base.AionTransaction)9 ImportResult (org.aion.zero.impl.core.ImportResult)9 ArrayList (java.util.ArrayList)8 AionRepositoryImpl (org.aion.zero.impl.db.AionRepositoryImpl)8 List (java.util.List)5 IOException (java.io.IOException)4 Properties (java.util.Properties)4 ByteArrayWrapper (org.aion.util.types.ByteArrayWrapper)4 StakingBlock (org.aion.zero.impl.types.StakingBlock)4 BigInteger (java.math.BigInteger)3 ECKey (org.aion.crypto.ECKey)3 AionAddress (org.aion.types.AionAddress)3 BlockContext (org.aion.zero.impl.types.BlockContext)3 AionTxReceipt (org.aion.base.AionTxReceipt)2 MockDB (org.aion.db.impl.mockdb.MockDB)2 AionBlockStore (org.aion.zero.impl.db.AionBlockStore)2