Search in sources :

Example 91 with ImportResult

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

the class PendingStateTest method updateCacheTransactionsTest.

@Test
public void updateCacheTransactionsTest() {
    List<AionTransaction> transactions = getMockTransaction(0, 2, 0);
    List<AionTransaction> cachedTx = getMockTransaction(2, 1, 0);
    assertEquals(TxResponse.SUCCESS, pendingState.addTransactionFromApiServer(transactions.get(0)));
    assertEquals(1, pendingState.getPendingTxSize());
    assertEquals(TxResponse.CACHED_NONCE, pendingState.addTransactionFromApiServer(cachedTx.get(0)));
    assertEquals(1, pendingState.getPendingTxSize());
    assertEquals(1, pendingState.getCachePoolSize());
    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(0), 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 92 with ImportResult

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

the class FvmExploitsTest method testAddInvalidAndThenValidBlocks.

@Test
public void testAddInvalidAndThenValidBlocks() throws InterruptedException {
    String testerByteCode = "0x60506040525b3360006000508282909180600101839055555050505b610020565b6107ab8061002f6000396000f30060506040523615610096576000356c01000000000000000000000000900463ffffffff16806306c8dcde1461009a578063590e1ae3146100c657806367a3914e146100dc5780636aee4cac1461010857806379ba5097146101525780638135ceea146101685780638da5cb5b146101d9578063a6f9dae11461020a578063b22fce4014610236578063d4ee1d901461027a57610096565b5b5b005b34156100a65760006000fd5b6100c4600480808060100135903590916020019091929050506102ab565b005b34156100d25760006000fd5b6100da610308565b005b34156100e85760006000fd5b6101066004808080601001359035909160200190919290505061035a565b005b34156101145760006000fd5b610138600480808060100135903590600019169090916020019091929050506103b7565b604051808215151515815260100191505060405180910390f35b341561015e5760006000fd5b6101666103e3565b005b34156101745760006000fd5b6101d760048080806010013590359060001916909091602001909192908080601001359035906000191690909160200190919290803590601001908201803590601001919091929080359060100190820180359060100191909192905050610470565b005b34156101e55760006000fd5b6101ed61062b565b604051808383825281601001526020019250505060405180910390f35b34156102165760006000fd5b6102346004808080601001359035909160200190919290505061063a565b005b34156102425760006000fd5b61026060048080806010013590359091602001909192905050610676565b604051808215151515815260100191505060405180910390f35b34156102865760006000fd5b61028e6106a2565b604051808383825281601001526020019250505060405180910390f35b600060005080600101549054339091149190141615156102cb5760006000fd5b600160046000506000848482528160100152602001908152601001600020905060006101000a81548160ff0219169083151502179055505b5b5050565b600060005080600101549054339091149190141615156103285760006000fd5b336108fc3031908115029060405160006040518083038185898989f194505050505015156103565760006000fd5b5b5b565b6000600050806001015490543390911491901416151561037a5760006000fd5b600060046000506000848482528160100152602001908152601001600020905060006101000a81548160ff0219169083151502179055505b5b5050565b60056000506020528181600052601052603060002090506000915091509054906101000a900460ff1681565b60026000508060010154905433909114919014161561046d5760026000508060010154905460006000508282909180600101839055555050506000600060026000508282909180600101839055555050506000600050806001015490547fa701229f4b9ddf00aa1c7228d248e6320ee7c581d856ddfba036e73947cd0d1360405160405180910390a25b5b565b60006000600050806001015490543390911491901416806104b85750600460005060003382528160100152602001908152601001600020905060009054906101000a900460ff165b15156104c45760006000fd5b868660056000506000838390600019169090600019169082528160100152602001908152601001600020905060009054906101000a900460ff1615151561050b5760006000fd5b600092506000925082505b8686905083101561057157610563878785818110151561053257fe5b905090906020020180601001359035878787818110151561054f57fe5b9050909060100201356106b163ffffffff16565b5b8280600101935050610516565b6001600560005060008b8b90600019169090600019169082528160100152602001908152601001600020905060006101000a81548160ff0219169083151502179055507f1fa305c7f8521af161de570532762ed7a60199cde79e18e1d259af34595625218c8c8c8c6040518085859060001916909060001916908252816010015260200183839060001916909060001916908252816010015260200194505050505060405180910390a15b5b50505b505050505050505050565b60006000508060010154905482565b6000600050806001015490543390911491901416151561065a5760006000fd5b818160026000508282909180600101839055555050505b5b5050565b60046000506020528181600052601052603060002090506000915091509054906101000a900460ff1681565b60026000508060010154905482565b6000600050806001015490543390911491901416806106f75750600460005060003382528160100152602001908152601001600020905060009054906101000a900460ff165b15156107035760006000fd5b82826108fc83908115029060405160006040518083038185898989f194505050505015156107315760006000fd5b7fdc3b8ebc415c945740a70187f1d472ad2d64a9e7a87047f38023aec56516976b84848460405180848482528160100152602001828152601001935050505060405180910390a15b5b5050505600a165627a7a7230582042b26e68ff40177f10fcfc05c70e102d5d14648a2134ffeec77c7138bf8d27dd0029";
    StandaloneBlockchain.Bundle bundle = (new StandaloneBlockchain.Builder()).withValidatorConfiguration("simple").withDefaultAccounts().build();
    StandaloneBlockchain bc = bundle.bc;
    ECKey deployerAccount = bundle.privateKeys.get(0);
    // =======================================================================
    BigInteger nonce = BigInteger.ZERO;
    AionTransaction tx = AionTransaction.create(deployerAccount, nonce.toByteArray(), null, new byte[0], ByteUtil.hexStringToBytes(testerByteCode), 1_000_000L, energyPrice, TransactionTypes.DEFAULT, null);
    assertThat(tx.isContractCreationTransaction()).isTrue();
    BlockContext context = bc.createNewMiningBlockContext(bc.getBestBlock(), Collections.singletonList(tx), false);
    ImportResult result = bc.tryToConnect(context.block);
    assertThat(result).isEqualTo(ImportResult.IMPORTED_BEST);
    AionAddress contractAddress = TxUtil.calculateContractAddress(tx);
    Thread.sleep(1000L);
    // =======================================================================
    nonce = nonce.add(BigInteger.ONE);
    AionTransaction tx2 = AionTransaction.create(deployerAccount, nonce.toByteArray(), contractAddress, new BigInteger("100000000000000000000").toByteArray(), new byte[0], 1_000_000L, energyPrice, TransactionTypes.DEFAULT, null);
    BlockContext context2 = bc.createNewMiningBlockContext(bc.getBestBlock(), Collections.singletonList(tx2), false);
    MiningBlock block = context2.block;
    byte[] originalReceiptsHash = block.getReceiptsRoot();
    // try importing an invalid block
    MiningBlockHeader newHeader = MiningBlockHeader.Builder.newInstance().withHeader(block.getHeader()).withReceiptTrieRoot(new byte[32]).build();
    block.updateHeader(newHeader);
    ImportResult result2 = bc.tryToConnect(context2.block);
    assertThat(result2).isEqualTo(ImportResult.INVALID_BLOCK);
    // try importing the correct block
    newHeader = MiningBlockHeader.Builder.newInstance().withHeader(block.getHeader()).withReceiptTrieRoot(originalReceiptsHash).build();
    block.updateHeader(newHeader);
    ImportResult result3 = bc.tryToConnect(context2.block);
    assertThat(result3).isEqualTo(ImportResult.IMPORTED_BEST);
}
Also used : ImportResult(org.aion.zero.impl.core.ImportResult) AionAddress(org.aion.types.AionAddress) BlockContext(org.aion.zero.impl.types.BlockContext) StandaloneBlockchain(org.aion.zero.impl.blockchain.StandaloneBlockchain) ECKey(org.aion.crypto.ECKey) AionTransaction(org.aion.base.AionTransaction) MiningBlock(org.aion.zero.impl.types.MiningBlock) MiningBlockHeader(org.aion.zero.impl.types.MiningBlockHeader) BigInteger(java.math.BigInteger) Test(org.junit.Test)

Example 93 with ImportResult

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

the class InternalTransactionTest method testRecursiveCall.

/*
    pragma solidity ^0.4.0;

    contract A {
        function f(int n) {
            if (n > 0) {
                this.f(n - 1);
            }
        }
    }
         */
@Test
public void testRecursiveCall() throws Exception {
    String contractA = "0x605060405234156100105760006000fd5b610015565b60e9806100236000396000f30060506040526000356c01000000000000000000000000900463ffffffff168063ec77996414603157602b565b60006000fd5b3415603c5760006000fd5b605060048080359060100190919050506052565b005b600081131560b9573063ec779964600184036040518263ffffffff166c01000000000000000000000000028152600401808281526010019150506000604051808303816000888881813b151560a75760006000fd5b5af1151560b45760006000fd5b505050505b5b505600a165627a7a7230582033f76d593b80b3468bfb0f873882bc00903a790a9b996cb8ca3bac51295994cd0029";
    StandaloneBlockchain.Bundle bundle = (new StandaloneBlockchain.Builder()).withValidatorConfiguration("simple").withDefaultAccounts().build();
    StandaloneBlockchain bc = bundle.bc;
    ECKey deployerAccount = bundle.privateKeys.get(0);
    // ======================
    // DEPLOY
    // ======================
    BigInteger nonce = BigInteger.ZERO;
    AionTransaction tx1 = AionTransaction.create(deployerAccount, nonce.toByteArray(), null, new byte[0], ByteUtil.hexStringToBytes(contractA), 1_000_000L, minEnergyPrice, TransactionTypes.DEFAULT, null);
    BlockContext context = bc.createNewMiningBlockContext(bc.getBestBlock(), List.of(tx1), false);
    ImportResult result = bc.tryToConnect(context.block);
    assertThat(result).isEqualTo(ImportResult.IMPORTED_BEST);
    AionAddress addressA = TxUtil.calculateContractAddress(tx1);
    System.out.println("contract A = " + addressA);
    Thread.sleep(1000);
    // ======================
    // CALL
    // ======================
    nonce = nonce.add(BigInteger.ONE);
    AionTransaction tx2 = AionTransaction.create(deployerAccount, nonce.toByteArray(), addressA, new byte[0], ByteUtil.merge(ByteUtil.hexStringToBytes("0xec779964"), new DataWord(2).getData()), 1_000_000L, minEnergyPrice, TransactionTypes.DEFAULT, null);
    context = bc.createNewMiningBlockContext(bc.getBestBlock(), List.of(tx2), false);
    AionTxExecSummary summary = executeTransaction(bc, context, tx2);
    assertEquals(2, summary.getInternalTransactions().size());
}
Also used : ImportResult(org.aion.zero.impl.core.ImportResult) AionAddress(org.aion.types.AionAddress) BlockContext(org.aion.zero.impl.types.BlockContext) AionTxExecSummary(org.aion.base.AionTxExecSummary) BigInteger(java.math.BigInteger) StandaloneBlockchain(org.aion.zero.impl.blockchain.StandaloneBlockchain) ECKey(org.aion.crypto.ECKey) AionTransaction(org.aion.base.AionTransaction) DataWord(org.aion.util.types.DataWord) Test(org.junit.Test)

Example 94 with ImportResult

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

the class InvalidBlockTest method test.

@Test
public void test() {
    BigInteger nonce = this.blockchain.getRepository().getNonce(new AionAddress(this.deployerKey.getAddress()));
    List<AionTransaction> transactions = makeTransactions(AvmVersion.VERSION_1, 20, nonce);
    Block parent = this.blockchain.getBestBlock();
    MiningBlock block = this.blockchain.createNewMiningBlock(parent, transactions, false);
    Pair<ImportResult, AionBlockSummary> res = this.blockchain.tryToConnectAndFetchSummary(block);
    // A correct block is produced instead of an invalid one. But the correct block only
    // contains the valid transactions, not all of them.
    assertEquals(ImportResult.IMPORTED_BEST, res.getLeft());
    assertEquals(13, res.getRight().getReceipts().size());
}
Also used : AionAddress(org.aion.types.AionAddress) ImportResult(org.aion.zero.impl.core.ImportResult) AionBlockSummary(org.aion.zero.impl.types.AionBlockSummary) BigInteger(java.math.BigInteger) MiningBlock(org.aion.zero.impl.types.MiningBlock) Block(org.aion.zero.impl.types.Block) AionTransaction(org.aion.base.AionTransaction) MiningBlock(org.aion.zero.impl.types.MiningBlock) Test(org.junit.Test)

Example 95 with ImportResult

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

the class MultiVersionAvmTest method testDeployInBothAvmVersions.

/**
 * We test deploying the same contract in version 1 and 2. We expect the same result each time.
 */
@Test
public void testDeployInBothAvmVersions() {
    Assert.assertEquals(0, this.blockchain.getBestBlock().getNumber());
    // Ensure we are at a block height where avm version 1 is enabled, then deploy.
    buildBlockchainToHeight(BLOCK_VERSION1_ENABLED);
    AionTransaction transactionForVersion1 = makeHelloWorldDeployTransaction(AvmVersion.VERSION_1, BigInteger.ZERO);
    Block parentBlock = this.blockchain.getBestBlock();
    MiningBlock block = this.blockchain.createBlock(parentBlock, Collections.singletonList(transactionForVersion1), false, parentBlock.getTimestamp());
    Pair<ImportResult, AionBlockSummary> connectResult = this.blockchain.tryToConnectAndFetchSummary(block);
    Assert.assertEquals(ImportResult.IMPORTED_BEST, connectResult.getLeft());
    Assert.assertEquals(1, connectResult.getRight().getReceipts().size());
    Assert.assertTrue(connectResult.getRight().getReceipts().get(0).isSuccessful());
    // Now, climb to a block height where avm version 2 is enabled and deploy.
    buildBlockchainToHeight(BLOCK_VERSION2_ENABLED);
    AionTransaction transactionForVersion2 = makeHelloWorldDeployTransaction(AvmVersion.VERSION_2, BigInteger.ONE);
    parentBlock = this.blockchain.getBestBlock();
    block = this.blockchain.createBlock(parentBlock, Collections.singletonList(transactionForVersion2), false, parentBlock.getTimestamp());
    connectResult = this.blockchain.tryToConnectAndFetchSummary(block);
    Assert.assertEquals(ImportResult.IMPORTED_BEST, connectResult.getLeft());
    Assert.assertEquals(1, connectResult.getRight().getReceipts().size());
    Assert.assertTrue(connectResult.getRight().getReceipts().get(0).isSuccessful());
}
Also used : ImportResult(org.aion.zero.impl.core.ImportResult) AionBlockSummary(org.aion.zero.impl.types.AionBlockSummary) MiningBlock(org.aion.zero.impl.types.MiningBlock) Block(org.aion.zero.impl.types.Block) AionTransaction(org.aion.base.AionTransaction) MiningBlock(org.aion.zero.impl.types.MiningBlock) 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