Search in sources :

Example 51 with ImportResult

use of org.aion.zero.impl.core.ImportResult 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 52 with ImportResult

use of org.aion.zero.impl.core.ImportResult in project aion by aionnetwork.

the class BlockchainIntegrationTest method testBlockContextCreation.

@Test
public void testBlockContextCreation() {
    StandaloneBlockchain.Bundle bundle = (new StandaloneBlockchain.Builder()).withValidatorConfiguration("simple").withDefaultAccounts().build();
    StandaloneBlockchain bc = bundle.bc;
    BlockContext context = bc.createNewMiningBlockContext(bc.getBestBlock(), Collections.emptyList(), false);
    ChainConfiguration configuration = new ChainConfiguration();
    // check some basic fields match first
    assertThat(context.block).isNotNull();
    assertThat(context.baseBlockReward).isNotNull();
    assertThat(context.transactionFee).isNotNull();
    // no transaction so fee should be zero
    assertThat(context.transactionFee).isEqualTo(BigInteger.ZERO);
    AionAddress beneficiary = context.block.getCoinbase();
    ImportResult result = bc.tryToConnect(context.block);
    // check that the correct amount was stored
    assertThat(result).isEqualTo(ImportResult.IMPORTED_BEST);
    assertThat(bc.getRepository().getBalance(beneficiary)).isEqualTo(context.baseBlockReward);
    // check that the correct amount was calculated
    assertThat(configuration.getRewardsCalculatorBeforeSignatureSchemeSwap(false).calculateReward(context.block.getHeader().getNumber())).isEqualTo(context.baseBlockReward);
}
Also used : AionAddress(org.aion.types.AionAddress) ImportResult(org.aion.zero.impl.core.ImportResult) BlockContext(org.aion.zero.impl.types.BlockContext) Test(org.junit.Test)

Example 53 with ImportResult

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

Example 54 with ImportResult

use of org.aion.zero.impl.core.ImportResult in project aion by aionnetwork.

the class BlockchainRewardTest method testBlockchainRewardMonotonicallyIncreasing.

/**
 * Test that blocks between the lower and upper bounds follow a certain function [0, 259200]
 *
 * <p>Note: this test is resource consuming!
 *
 * <p>Check {@link org.aion.zero.impl.core.RewardsCalculator} for algorithm related to the
 * ramp-up block time
 */
@Ignore
@Test
public void testBlockchainRewardMonotonicallyIncreasing() {
    StandaloneBlockchain.Bundle bundle = new StandaloneBlockchain.Builder().withDefaultAccounts().withValidatorConfiguration("simple").build();
    StandaloneBlockchain bc = bundle.bc;
    MiningBlock block = bc.createNewMiningBlock(bc.getBestBlock(), Collections.EMPTY_LIST, true);
    ImportResult res = bc.tryToConnect(block);
    assertThat(res).isEqualTo(ImportResult.IMPORTED_BEST);
    AionAddress coinbase = block.getCoinbase();
    BigInteger previousBalance = bc.getRepository().getBalance(coinbase);
    // first block already sealed
    for (int i = 2; i < 99999; i++) {
        MiningBlock b = bc.createNewMiningBlock(bc.getBestBlock(), Collections.EMPTY_LIST, true);
        ImportResult r = bc.tryToConnect(b);
        assertThat(r).isEqualTo(ImportResult.IMPORTED_BEST);
        // note the assumption here that blocks are mined by one coinbase
        BigInteger balance = bc.getRepository().getBalance(coinbase);
        assertThat(balance).isGreaterThan(previousBalance);
        previousBalance = balance;
        if (b.getNumber() % 1000 == 0)
            System.out.println("added block #: " + i);
    }
}
Also used : ImportResult(org.aion.zero.impl.core.ImportResult) AionAddress(org.aion.types.AionAddress) BigInteger(java.math.BigInteger) MiningBlock(org.aion.zero.impl.types.MiningBlock) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 55 with ImportResult

use of org.aion.zero.impl.core.ImportResult in project aion by aionnetwork.

the class TransactionCreateSpecificationTests method deployInternalAvmContractOnTopOfAddressWithStorageUsingAvmVersion1.

@Test
public void deployInternalAvmContractOnTopOfAddressWithStorageUsingAvmVersion1() throws VmFatalException {
    AvmTestConfig.clearConfigurations();
    AvmTestConfig.supportOnlyAvmVersion1();
    // Deploy AVM contract.
    AionTransaction deployTxAvm = BlockchainTestUtils.deployAvmContractTransaction(AvmContract.DEPLOY_INTERNAL, resourceProvider.factoryForVersion1, SENDER_KEY, BigInteger.ZERO);
    AionAddress contract = TxUtil.calculateContractAddress(deployTxAvm);
    Pair<Block, ImportResult> resultImport = BlockchainTestUtils.addMiningBlock(blockchain, blockchain.getBestBlock(), List.of(deployTxAvm));
    assertThat(resultImport.getRight()).isEqualTo(ImportResult.IMPORTED_BEST);
    // Call AVM contract to deploy new internal AVM contract (version without required success).
    long internalLimit = 1_000_000;
    AionTransaction deployInternal = BlockchainTestUtils.callSimpleAvmContractTransaction(resourceProvider.factoryForVersion1, SENDER_KEY, BigInteger.ONE, contract, "deploy", deployTxAvm.getData(), internalLimit);
    AionAddress internalContract = new AionAddress(Hex.decode("a0268090998a99666b72cc452b9307438a34341047d9e0d7b92c9207bf413655"));
    assertThat(blockchain.getRepository().hasAccountState(internalContract)).isFalse();
    // Manipulate the repository to have a non-default storage value.
    RepositoryCache cache = blockchain.getRepository().startTracking();
    cache.addStorageRow(internalContract, ByteArrayWrapper.wrap(RandomUtils.nextBytes(16)), ByteArrayWrapper.wrap(RandomUtils.nextBytes(16)));
    cache.saveVmType(internalContract, InternalVmType.AVM);
    cache.flushTo(cache.getParent(), true);
    // Check assumptions about contract state.
    AccountState contractState = (AccountState) blockchain.getRepository().startTracking().getAccountState(internalContract);
    assertThat(contractState.getBalance()).isEqualTo(BigInteger.ZERO);
    assertThat(contractState.getNonce()).isEqualTo(BigInteger.ZERO);
    assertThat(contractState.getStateRoot()).isNotEqualTo(EMPTY_TRIE_HASH);
    assertThat(contractState.getCodeHash()).isEqualTo(EMPTY_DATA_HASH);
    byte[] oldRoot = contractState.getStateRoot();
    // Next, process the deploy transaction with fork040 enabled.
    AionTxExecSummary result = executeTransaction(deployInternal, true);
    assertThat(result.isFailed()).isFalse();
    assertThat(result.isRejected()).isFalse();
    assertThat(result.getReceipt().getError()).isEmpty();
    assertThat(result.getNrgUsed()).isLessThan(BigInteger.valueOf(deployInternal.getEnergyLimit()));
    assertThat(result.getLogs()).isEmpty();
    InternalTransaction itx = result.getInternalTransactions().get(0);
    assertThat(itx.isCreate).isTrue();
    assertThat(TxUtil.calculateContractAddress(itx)).isEqualTo(internalContract);
    assertThat(itx.isRejected).isFalse();
    assertThat(itx.energyLimit).isEqualTo(internalLimit);
    assertThat(result.getNrgUsed()).isLessThan(BigInteger.valueOf(itx.energyLimit));
    contractState = (AccountState) blockchain.getRepository().startTracking().getAccountState(internalContract);
    assertThat(contractState.getBalance()).isEqualTo(BigInteger.ZERO);
    assertThat(contractState.getNonce()).isEqualTo(BigInteger.ZERO);
    assertThat(contractState.getStateRoot()).isNotEqualTo(EMPTY_TRIE_HASH);
    assertThat(contractState.getStateRoot()).isNotEqualTo(oldRoot);
    assertThat(contractState.getCodeHash()).isNotEqualTo(EMPTY_DATA_HASH);
}
Also used : AionAddress(org.aion.types.AionAddress) ImportResult(org.aion.zero.impl.core.ImportResult) AionTxExecSummary(org.aion.base.AionTxExecSummary) AionBlockchainImpl.getPostExecutionWorkForGeneratePreBlock(org.aion.zero.impl.blockchain.AionBlockchainImpl.getPostExecutionWorkForGeneratePreBlock) Block(org.aion.zero.impl.types.Block) RepositoryCache(org.aion.base.db.RepositoryCache) AionTransaction(org.aion.base.AionTransaction) AccountState(org.aion.base.AccountState) InternalTransaction(org.aion.types.InternalTransaction) Test(org.junit.Test)

Aggregations

ImportResult (org.aion.zero.impl.core.ImportResult)166 Test (org.junit.Test)127 AionTransaction (org.aion.base.AionTransaction)114 AionAddress (org.aion.types.AionAddress)106 AionBlockSummary (org.aion.zero.impl.types.AionBlockSummary)95 MiningBlock (org.aion.zero.impl.types.MiningBlock)90 Block (org.aion.zero.impl.types.Block)80 BigInteger (java.math.BigInteger)75 AionTxReceipt (org.aion.base.AionTxReceipt)50 AionTxExecSummary (org.aion.base.AionTxExecSummary)31 ECKey (org.aion.crypto.ECKey)28 RepositoryCache (org.aion.base.db.RepositoryCache)22 StandaloneBlockchain (org.aion.zero.impl.blockchain.StandaloneBlockchain)22 InternalTransaction (org.aion.types.InternalTransaction)20 AccountState (org.aion.base.AccountState)18 ArrayList (java.util.ArrayList)17 AionBlockchainImpl.getPostExecutionWorkForGeneratePreBlock (org.aion.zero.impl.blockchain.AionBlockchainImpl.getPostExecutionWorkForGeneratePreBlock)16 Builder (org.aion.zero.impl.blockchain.StandaloneBlockchain.Builder)14 AionRepositoryImpl (org.aion.zero.impl.db.AionRepositoryImpl)10 BlockContext (org.aion.zero.impl.types.BlockContext)9