Search in sources :

Example 1 with ChainConfiguration

use of org.aion.zero.impl.blockchain.ChainConfiguration in project aion by aionnetwork.

the class BlockchainForkingTest method testHigherDifficultyBlockFork.

/*-
     * Test the general forking case, where an incoming block (b) has a greater total
     * difficulty than our current block. In this scenario, we should switch to
     * the branch (sub-tree) that has (b) at its tip.
     *
     * This is the simplest case, where the distance between (a) (our current head) and
     * (b) is 2. This implies that the common ancestor is directly adjacent to both blocks.
     *
     *          (common ancestor)
     *          /               \
     *         /                 \
     *        /                   \
     *       (a)x(low td)           (b)o(higher td)
     *
     * In this simple case:
     * b.td > a.td
     * a_worldState === b_worldState
     *
     */
@Test
public void testHigherDifficultyBlockFork() {
    StandaloneBlockchain.Builder builder = new StandaloneBlockchain.Builder();
    StandaloneBlockchain.Bundle b = builder.withValidatorConfiguration("simple").build();
    StandaloneBlockchain bc = b.bc;
    AionBlock bestBlock = bc.getBestBlock();
    AionBlock standardBlock = bc.createNewBlock(bc.getBestBlock(), Collections.emptyList(), true);
    ChainConfiguration cc = new ChainConfiguration();
    AionBlock higherDifficultyBlock = new AionBlock(standardBlock);
    higherDifficultyBlock.getHeader().setTimestamp(bestBlock.getTimestamp() + 1);
    BigInteger difficulty = cc.getDifficultyCalculator().calculateDifficulty(higherDifficultyBlock.getHeader(), bestBlock.getHeader());
    assertThat(difficulty).isGreaterThan(standardBlock.getDifficultyBI());
    higherDifficultyBlock.getHeader().setDifficulty(difficulty.toByteArray());
    System.out.println("before any processing: " + new ByteArrayWrapper(bc.getRepository().getRoot()));
    System.out.println("trie: " + ((AionRepositoryImpl) bc.getRepository()).getWorldState().getTrieDump());
    ImportResult result = bc.tryToConnect(standardBlock);
    assertThat(result).isEqualTo(ImportResult.IMPORTED_BEST);
    // assert that the block we just inserted (best) is the instance that is returned
    assertThat(bc.getBestBlock() == standardBlock).isTrue();
    System.out.println(new ByteArrayWrapper(bc.getRepository().getRoot()));
    ImportResult higherDifficultyResult = bc.tryToConnect(higherDifficultyBlock);
    assertThat(higherDifficultyResult).isEqualTo(ImportResult.IMPORTED_BEST);
    assertThat(bc.getBestBlockHash()).isEqualTo(higherDifficultyBlock.getHash());
    // the object reference here is intentional
    assertThat(bc.getBestBlock() == higherDifficultyBlock).isTrue();
}
Also used : ImportResult(org.aion.mcf.core.ImportResult) ByteArrayWrapper(org.aion.base.util.ByteArrayWrapper) ChainConfiguration(org.aion.zero.impl.blockchain.ChainConfiguration) BigInteger(java.math.BigInteger) AionBlock(org.aion.zero.impl.types.AionBlock) Test(org.junit.Test)

Example 2 with ChainConfiguration

use of org.aion.zero.impl.blockchain.ChainConfiguration in project aion by aionnetwork.

the class ChainConfigurationTest method testRampUpFunctionBoundaries.

// assuming 100000 block ramp
@Test
public void testRampUpFunctionBoundaries() {
    long upperBound = 259200L;
    ChainConfiguration config = new ChainConfiguration();
    BigInteger increment = config.getConstants().getBlockReward().divide(BigInteger.valueOf(upperBound));
    // UPPER BOUND
    when(header.getNumber()).thenReturn(upperBound);
    BigInteger blockReward100000 = config.getRewardsCalculator().calculateReward(header);
    when(header.getNumber()).thenReturn(upperBound + 1);
    BigInteger blockReward100001 = config.getRewardsCalculator().calculateReward(header);
    // check that at the upper bound of our range (which is not included) blockReward is capped
    assertEquals(blockReward100000.doubleValue(), config.getConstants().getBlockReward().doubleValue(), 10000);
    // check that for the block after, the block reward is still the same
    assertThat(blockReward100001).isEqualTo(config.getConstants().getBlockReward());
    // check that for an arbitrarily large block, the block reward is still the same
    when(header.getNumber()).thenReturn(upperBound + 100000);
    BigInteger blockReward6700000 = config.getRewardsCalculator().calculateReward(header);
    assertThat(blockReward6700000).isEqualTo(blockReward100001);
    // LOWER BOUNDS
    when(header.getNumber()).thenReturn(0l);
    BigInteger blockReward0 = config.getRewardsCalculator().calculateReward(header);
    assertThat(blockReward0).isEqualTo(BigInteger.ZERO);
    // first block (should have gas value of increment)
    when(header.getNumber()).thenReturn(1l);
    BigInteger blockReward1 = config.getRewardsCalculator().calculateReward(header);
    assertThat(blockReward1).isEqualTo(increment);
}
Also used : ChainConfiguration(org.aion.zero.impl.blockchain.ChainConfiguration) BigInteger(java.math.BigInteger) Test(org.junit.Test)

Example 3 with ChainConfiguration

use of org.aion.zero.impl.blockchain.ChainConfiguration in project aion by aionnetwork.

the class ChainConfigurationTest method testIntegrationEnergyLimitCalc.

@Test
public void testIntegrationEnergyLimitCalc() {
    when(header.getEnergyLimit()).thenReturn(10000000L);
    ChainConfiguration config = new ChainConfiguration();
    long out = config.calcEnergyLimit(header);
    assertThat(out).isEqualTo(10000000L);
}
Also used : ChainConfiguration(org.aion.zero.impl.blockchain.ChainConfiguration) Test(org.junit.Test)

Example 4 with ChainConfiguration

use of org.aion.zero.impl.blockchain.ChainConfiguration in project aion by aionnetwork.

the class ChainConfigurationTest method testValidation.

@Test
public void testValidation() {
    int n = 210;
    int k = 9;
    byte[] nonce = { 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
    // setup mock
    A0BlockHeader.Builder builder = new A0BlockHeader.Builder();
    builder.withDifficulty(BigInteger.valueOf(1).toByteArray());
    builder.withNonce(nonce);
    A0BlockHeader header = builder.build();
    Equihash equihash = new Equihash(n, k);
    int[][] solutions;
    while (true) {
        solutions = equihash.getSolutionsForNonce(header.getHeaderBytes(true), header.getNonce());
        if (solutions.length > 0)
            break;
    }
    // compress solution
    byte[] compressedSolution = EquiUtils.getMinimalFromIndices(solutions[0], n / (k + 1));
    header.setSolution(compressedSolution);
    ChainConfiguration chainConfig = new ChainConfiguration();
    BlockHeaderValidator<A0BlockHeader> blockHeaderValidator = chainConfig.createBlockHeaderValidator();
    blockHeaderValidator.validate(header, log);
}
Also used : A0BlockHeader(org.aion.zero.types.A0BlockHeader) ChainConfiguration(org.aion.zero.impl.blockchain.ChainConfiguration) Equihash(org.aion.equihash.Equihash) Test(org.junit.Test)

Aggregations

ChainConfiguration (org.aion.zero.impl.blockchain.ChainConfiguration)4 Test (org.junit.Test)4 BigInteger (java.math.BigInteger)2 ByteArrayWrapper (org.aion.base.util.ByteArrayWrapper)1 Equihash (org.aion.equihash.Equihash)1 ImportResult (org.aion.mcf.core.ImportResult)1 AionBlock (org.aion.zero.impl.types.AionBlock)1 A0BlockHeader (org.aion.zero.types.A0BlockHeader)1