Search in sources :

Example 21 with BlockContext

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

the class AionImpl method getNewMiningBlockTemplate.

// Returns a new template if a better parent block to mine on is found, or if the system time
// is ahead of the oldBlockTemplate
// Returns null if we're waiting on a Staking block, or if creating a new block template failed for some reason
@Override
public BlockContext getNewMiningBlockTemplate(BlockContext oldBlockTemplate, long systemTime) {
    lock.lock();
    try {
        Block bestBlock = getBlockchain().getBestBlock();
        if (getBlockchain().isUnityForkEnabledAtNextBlock() && bestBlock.getHeader().getSealType().equals(Seal.PROOF_OF_WORK)) {
            return null;
        } else {
            BlockContext context;
            byte[] bestBlockHash = bestBlock.getHash();
            if (oldBlockTemplate == null || !Arrays.equals(bestBlockHash, oldBlockTemplate.block.getParentHash()) || (systemTime > oldBlockTemplate.block.getTimestamp() && getBlockchain().isUnityForkEnabledAtNextBlock())) {
                TransactionSortedSet txSortSet = new TransactionSortedSet();
                txSortSet.addAll(getAionHub().getPendingState().getPendingTransactions());
                context = getBlockchain().createNewMiningBlockContext(bestBlock, new ArrayList<>(txSortSet), false);
            } else {
                context = oldBlockTemplate;
            }
            return context;
        }
    } finally {
        lock.unlock();
    }
}
Also used : BlockContext(org.aion.zero.impl.types.BlockContext) ArrayList(java.util.ArrayList) StakingBlock(org.aion.zero.impl.types.StakingBlock) Block(org.aion.zero.impl.types.Block) TransactionSortedSet(org.aion.zero.impl.blockchain.AionHub.TransactionSortedSet)

Example 22 with BlockContext

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

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

the class InternalTransactionTest method testNestedCreate.

/*
    pragma solidity ^0.4.0;

    contract B {}

    contract A {
        address b;

        function A() {
            b = new B();
        }
    }
         */
@Test
public void testNestedCreate() throws Exception {
    String contractA = "0x60506040523415600f5760006000fd5b5b60166048565b604051809103906000f0801582151615602f5760006000fd5b60006000508282909180600101839055555050505b6057565b604051605a8061009f83390190565b603a806100656000396000f30060506040526008565b60006000fd00a165627a7a72305820c0eea40d4778b01848164e58898e9e8c8ab068ed5ee36ed6f0582d119ecbbede002960506040523415600f5760006000fd5b6013565b603a8060206000396000f30060506040526008565b60006000fd00a165627a7a723058208c13bc92baf844f8574632dca44c49776516cb6cd537b10ed700bf61392b6ae80029";
    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);
    AionTxExecSummary summary = executeTransaction(bc, context, tx1);
    System.out.println(summary.getReceipt());
    for (InternalTransaction tx : summary.getInternalTransactions()) {
        System.out.println(tx);
    }
}
Also used : 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) InternalTransaction(org.aion.types.InternalTransaction) Test(org.junit.Test)

Example 24 with BlockContext

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

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

the class OpcodeIntegTest method testSuicideRecipientExists.

// ======================================= test SUICIDE ========================================
@Test
public void testSuicideRecipientExists() throws Exception {
    RepositoryCache repo = blockchain.getRepository().startTracking();
    BigInteger balance = new BigInteger("32522224");
    AionAddress recipient = new AionAddress(RandomUtils.nextBytes(AionAddress.LENGTH));
    repo.createAccount(recipient);
    AionAddress contract = deployContract(repo, "Suicide", "Suicide.sol", BigInteger.ZERO);
    repo.addBalance(contract, balance);
    BigInteger balanceDeployer = repo.getBalance(deployer);
    BigInteger balanceRecipient = repo.getBalance(recipient);
    assertEquals(balance, repo.getBalance(contract));
    long nrg = 1_000_000;
    long nrgPrice = 1;
    BigInteger nonce = BigInteger.ONE;
    byte[] input = ByteUtil.merge(Hex.decode("fc68521a"), recipient.toByteArray());
    AionTransaction tx = AionTransaction.create(deployerKey, nonce.toByteArray(), contract, BigInteger.ZERO.toByteArray(), input, nrg, nrgPrice, TransactionTypes.DEFAULT, null);
    BlockContext context = blockchain.createNewMiningBlockContext(blockchain.getBestBlock(), Collections.singletonList(tx), false);
    AionTxExecSummary summary = executeTransaction(tx, context.block, repo);
    assertEquals("", summary.getReceipt().getError());
    assertEquals(summary.getNrgUsed().longValue(), summary.getNrgUsed().longValue());
    // We expect that deployer paid the tx cost. We expect that all of the balance in the
    // contract has been transferred to recipient. We expect that the contract has been deleted.
    BigInteger txCost = BigInteger.valueOf(summary.getNrgUsed().longValue() * nrgPrice);
    assertEquals(balanceDeployer.subtract(txCost), repo.getBalance(deployer));
    assertEquals(balanceRecipient.add(balance), repo.getBalance(recipient));
    assertEquals(BigInteger.ZERO, repo.getBalance(contract));
    assertFalse(repo.hasAccountState(contract));
    assertEquals(1, summary.getDeletedAccounts().size());
    assertEquals(contract, summary.getDeletedAccounts().get(0));
}
Also used : AionAddress(org.aion.types.AionAddress) BlockContext(org.aion.zero.impl.types.BlockContext) AionTxExecSummary(org.aion.base.AionTxExecSummary) RepositoryCache(org.aion.base.db.RepositoryCache) BigInteger(java.math.BigInteger) AionTransaction(org.aion.base.AionTransaction) Test(org.junit.Test)

Aggregations

BlockContext (org.aion.zero.impl.types.BlockContext)49 AionTransaction (org.aion.base.AionTransaction)42 Test (org.junit.Test)42 BigInteger (java.math.BigInteger)27 AionAddress (org.aion.types.AionAddress)25 AionTxExecSummary (org.aion.base.AionTxExecSummary)19 MiningBlock (org.aion.zero.impl.types.MiningBlock)18 AionRepositoryImpl (org.aion.zero.impl.db.AionRepositoryImpl)16 RepositoryCache (org.aion.base.db.RepositoryCache)15 Block (org.aion.zero.impl.types.Block)15 ArrayList (java.util.ArrayList)13 MockDB (org.aion.db.impl.mockdb.MockDB)12 DataWord (org.aion.util.types.DataWord)11 ImportResult (org.aion.zero.impl.core.ImportResult)9 ECKey (org.aion.crypto.ECKey)8 StandaloneBlockchain (org.aion.zero.impl.blockchain.StandaloneBlockchain)8 TrieImpl (org.aion.zero.impl.trie.TrieImpl)6 HashMap (java.util.HashMap)5 Map (java.util.Map)5 Log (org.aion.types.Log)5