Search in sources :

Example 26 with ImportResult

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

the class AionImpl method addNewBlock.

/**
 * @implNote import a new block from the api server or the internal PoW miner, the kernel will
 * reject to import a new block has the same or less than the kernel block height to reduce the orphan
 * block happens (AKI-707)
 */
public ImportResult addNewBlock(Block block) {
    getLock().lock();
    try {
        Block bestBlock = getAionHub().getBlockchain().getBestBlock();
        ImportResult importResult;
        if (bestBlock.getNumber() >= block.getNumber()) {
            importResult = ImportResult.INVALID_BLOCK;
        } else {
            importResult = this.aionHub.getBlockchain().tryToConnect(new BlockWrapper(block, true, false, false, false));
        }
        LOG_GEN.debug("ImportResult:{} new block:{}", importResult, block);
        if (importResult == ImportResult.IMPORTED_BEST) {
            this.aionHub.getPropHandler().propagateNewBlock(block);
        }
        return importResult;
    } finally {
        getLock().unlock();
    }
}
Also used : ImportResult(org.aion.zero.impl.core.ImportResult) StakingBlock(org.aion.zero.impl.types.StakingBlock) Block(org.aion.zero.impl.types.Block)

Example 27 with ImportResult

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

the class PendingStateTest method repayTransactionTest.

@Test
public void repayTransactionTest() {
    AionTransaction tx = AionTransaction.create(deployerKey, BigInteger.ZERO.toByteArray(), new AionAddress(new byte[32]), BigInteger.ZERO.toByteArray(), ByteUtils.fromHexString("1"), 21_000L * 10, energyPrice, TransactionTypes.DEFAULT, null);
    AionTransaction repayTx = AionTransaction.create(deployerKey, BigInteger.ZERO.toByteArray(), new AionAddress(new byte[32]), BigInteger.ZERO.toByteArray(), ByteUtils.fromHexString("1"), 21_000L * 10, energyPrice * 2, TransactionTypes.DEFAULT, null);
    assertEquals(TxResponse.SUCCESS, pendingState.addTransactionFromApiServer(tx));
    assertEquals(1, pendingState.getPendingTxSize());
    assertEquals(TxResponse.REPAID, pendingState.addTransactionFromApiServer(repayTx));
    assertEquals(1, pendingState.getPendingTxSize());
    assertEquals(tx, pendingState.getPendingTransactions().get(0));
    MiningBlock block = blockchain.createNewMiningBlock(blockchain.getBestBlock(), Collections.emptyList(), false);
    Pair<ImportResult, AionBlockSummary> connectResult = blockchain.tryToConnectAndFetchSummary(block);
    assertEquals(connectResult.getLeft(), ImportResult.IMPORTED_BEST);
    assertEquals(1, pendingState.getPendingTxSize());
    assertEquals(repayTx, pendingState.getPendingTransactions().get(0));
}
Also used : AionAddress(org.aion.types.AionAddress) ImportResult(org.aion.zero.impl.core.ImportResult) AionBlockSummary(org.aion.zero.impl.types.AionBlockSummary) AionTransaction(org.aion.base.AionTransaction) MiningBlock(org.aion.zero.impl.types.MiningBlock) Test(org.junit.Test)

Example 28 with ImportResult

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

the class PendingStateTest method updateCacheTransactionsTest2.

@Test
public void updateCacheTransactionsTest2() {
    List<AionTransaction> transactions = getMockTransaction(0, 2, 0);
    List<AionTransaction> cachedTx = getMockTransaction(2, 2, 0);
    assertEquals(TxResponse.SUCCESS, pendingState.addTransactionFromApiServer(transactions.get(0)));
    assertEquals(1, pendingState.getPendingTxSize());
    pendingState.addTransactionsFromNetwork(cachedTx);
    assertEquals(1, pendingState.getPendingTxSize());
    assertEquals(2, pendingState.getCachePoolSize());
    transactions.add(cachedTx.get(0));
    MiningBlock block = blockchain.createNewMiningBlock(blockchain.getBestBlock(), transactions, false);
    Pair<ImportResult, AionBlockSummary> connectResult = blockchain.tryToConnectAndFetchSummary(block);
    assertEquals(connectResult.getLeft(), ImportResult.IMPORTED_BEST);
    assertEquals(1, pendingState.getPendingTxSize());
    assertEquals(0, pendingState.getCachePoolSize());
    assertEquals(cachedTx.get(1), pendingState.getPendingTransactions().get(0));
}
Also used : ImportResult(org.aion.zero.impl.core.ImportResult) AionBlockSummary(org.aion.zero.impl.types.AionBlockSummary) AionTransaction(org.aion.base.AionTransaction) MiningBlock(org.aion.zero.impl.types.MiningBlock) Test(org.junit.Test)

Example 29 with ImportResult

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

the class PendingStateTest method energyLimitMinimum.

@Test
public void energyLimitMinimum() {
    AionTransaction tx = AionTransaction.create(deployerKey, BigInteger.ZERO.toByteArray(), new AionAddress(new byte[32]), BigInteger.ZERO.toByteArray(), ByteUtils.fromHexString("1"), 21_000L, energyPrice, TransactionTypes.DEFAULT, null);
    assertEquals(pendingState.addTransactionFromApiServer(tx), TxResponse.DROPPED);
    MiningBlock block = blockchain.createNewMiningBlock(blockchain.getBestBlock(), pendingState.getPendingTransactions(), false);
    Pair<ImportResult, AionBlockSummary> connectResult = blockchain.tryToConnectAndFetchSummary(block);
    assertEquals(connectResult.getLeft(), ImportResult.IMPORTED_BEST);
}
Also used : AionAddress(org.aion.types.AionAddress) ImportResult(org.aion.zero.impl.core.ImportResult) AionBlockSummary(org.aion.zero.impl.types.AionBlockSummary) AionTransaction(org.aion.base.AionTransaction) MiningBlock(org.aion.zero.impl.types.MiningBlock) Test(org.junit.Test)

Example 30 with ImportResult

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

the class PendingStateTest method testAddPendingTransaction_AVMContractCall_Success.

@Test
public void testAddPendingTransaction_AVMContractCall_Success() throws Exception {
    TestResourceProvider resourceProvider = TestResourceProvider.initializeAndCreateNewProvider(AvmPathManager.getPathOfProjectRootDirectory());
    IAvmResourceFactory resourceFactory = resourceProvider.factoryForVersion1;
    // Successful transaction
    byte[] jar = resourceFactory.newContractFactory().getDeploymentBytes(AvmContract.HELLO_WORLD);
    AionTransaction createTransaction = AionTransaction.create(deployerKey, BigInteger.ZERO.toByteArray(), null, BigInteger.ZERO.toByteArray(), jar, 5_000_000, energyPrice, TransactionTypes.AVM_CREATE_CODE, null);
    assertEquals(pendingState.addTransactionFromApiServer(createTransaction), TxResponse.SUCCESS);
    MiningBlock block = blockchain.createNewMiningBlock(blockchain.getBestBlock(), pendingState.getPendingTransactions(), false);
    Pair<ImportResult, AionBlockSummary> connectResult = blockchain.tryToConnectAndFetchSummary(block);
    AionTxReceipt receipt = connectResult.getRight().getReceipts().get(0);
    // Check the block was imported, the contract has the Avm prefix, and deployment succeeded.
    assertThat(connectResult.getLeft()).isEqualTo(ImportResult.IMPORTED_BEST);
    // verify that the output is indeed the contract address
    AionAddress contractAddress = TxUtil.calculateContractAddress(createTransaction);
    assertThat(contractAddress.toByteArray()).isEqualTo(receipt.getTransactionOutput());
    AionAddress contract = new AionAddress(receipt.getTransactionOutput());
    byte[] call = resourceFactory.newStreamingEncoder().encodeOneString("sayHello").getEncoding();
    AionTransaction callTransaction = AionTransaction.create(deployerKey, BigInteger.ONE.toByteArray(), contract, BigInteger.ZERO.toByteArray(), call, 2_000_000, energyPrice, TransactionTypes.DEFAULT, null);
    assertEquals(pendingState.addTransactionFromApiServer(callTransaction), TxResponse.SUCCESS);
}
Also used : IAvmResourceFactory(org.aion.avm.stub.IAvmResourceFactory) ImportResult(org.aion.zero.impl.core.ImportResult) AionBlockSummary(org.aion.zero.impl.types.AionBlockSummary) AionAddress(org.aion.types.AionAddress) AionTransaction(org.aion.base.AionTransaction) AionTxReceipt(org.aion.base.AionTxReceipt) MiningBlock(org.aion.zero.impl.types.MiningBlock) TestResourceProvider(org.aion.zero.impl.vm.TestResourceProvider) 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