Search in sources :

Example 11 with ImportResult

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

the class FvmExploitsTest method testBlockCreationExploit.

@Test
public void testBlockCreationExploit() throws InterruptedException {
    String testerByteCode = "0x605060405234156100105760006000fd5b5b3360006000508282909180600101839055555050505b61002c565b6103828061003b6000396000f30060506040526000356c01000000000000000000000000900463ffffffff1680634f2be91f14610049578063b4b65ae01461005f578063cd909c75146100cb57610043565b60006000fd5b34156100555760006000fd5b61005d6100e1565b005b341561006b5760006000fd5b6100736100e7565b6040518080601001828103825283818151815260100191508051906010019060200280838360005b838110156100b75780820151818401525b60108101905061009b565b505050509050019250505060405180910390f35b34156100d75760006000fd5b6100df61015d565b005b6002505b565b6100ef6101cf565b600260005080548060200260100160405190810160405280929190818152601001828054801561014e576020028201919060005260106000209050905b816000508060010154905482528160100152602001906002019080831161012c575b5050505050905061015a565b90565b600060006101696101e6565b604051809103906000f08015821516156101835760006000fd5b915091506002600050805480600101828161019e91906101f5565b91909060005260106000209050906002020160005b84849091929091925091909091806001018390555550505b5050565b601060405190810160405280600081526010015090565b60405160fa8061025d83390190565b8154818355818115116102245760020281600202836000526010600020905091820191016102239190610229565b5b505050565b6102599190610233565b8082111561025557600081815080600090556001016000905550600201610233565b5090565b905600605060405234156100105760006000fd5b5b4260006000508190909055507fd4fc977b8ac41e3fa318bb6650de6044046ea9e8cda72be27b6b0c458726c1666000600050546040518082815260100191505060405180910390a15b61005f565b608d8061006d6000396000f30060506040526000356c01000000000000000000000000900463ffffffff16806316ada54714603157602b565b60006000fd5b3415603c5760006000fd5b60426058565b6040518082815260100191505060405180910390f35b600060005054815600a165627a7a723058206919d683bc89f37f2bf6a52877fe0997e5d9b83057967fa1fd4a420b5da707b20029a165627a7a723058202d4cb48cf45eb1f4907e249b6060d84330669ff7f27d967554eb3a20e1c1f6840029";
    StandaloneBlockchain.Bundle bundle = (new StandaloneBlockchain.Builder()).withValidatorConfiguration("simple").withDefaultAccounts().build();
    StandaloneBlockchain bc = bundle.bc;
    ECKey deployerAccount = bundle.privateKeys.get(0);
    BigInteger nonce = BigInteger.ZERO;
    /* byte[] nonce, Address to, byte[] value, byte[] data, long nrg, long nrgPrice */
    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);
    // try to connect the deployment block
    ImportResult result = bc.tryToConnect(context.block);
    assertThat(result).isEqualTo(ImportResult.IMPORTED_BEST);
    AionAddress contractAddress = TxUtil.calculateContractAddress(tx);
    System.out.println("xxx = " + bc.getRepository().getNonce(new AionAddress(deployerAccount.getAddress())));
    Thread.sleep(1000L);
    // try executing a makeTest() call
    byte[] funcSig = ByteUtil.hexStringToBytes("0xcd909c75");
    nonce = nonce.add(BigInteger.ONE);
    AionTransaction makeTestCallTx = AionTransaction.create(deployerAccount, nonce.toByteArray(), contractAddress, new byte[0], funcSig, 1_000_000L, energyPrice, TransactionTypes.DEFAULT, null);
    BlockContext context2 = bc.createNewMiningBlockContext(bc.getBestBlock(), Collections.singletonList(makeTestCallTx), false);
    ImportResult result2 = bc.tryToConnect(context2.block);
    assertThat(result2).isEqualTo(ImportResult.IMPORTED_BEST);
    System.out.println("xxx = " + bc.getRepository().getNonce(new AionAddress(deployerAccount.getAddress())));
    System.out.println("yyy = " + bc.getRepository().getNonce(contractAddress));
    Thread.sleep(1000L);
    // try executing another makeTest() call
    funcSig = ByteUtil.hexStringToBytes("0xcd909c75");
    nonce = nonce.add(BigInteger.ONE);
    AionTransaction anotherMakeTestCall = AionTransaction.create(deployerAccount, nonce.toByteArray(), contractAddress, new byte[0], funcSig, 1_000_000L, energyPrice, TransactionTypes.DEFAULT, null);
    BlockContext context3 = bc.createNewMiningBlockContext(bc.getBestBlock(), Collections.singletonList(anotherMakeTestCall), false);
    ImportResult result3 = bc.tryToConnect(context3.block);
    assertThat(result3).isEqualTo(ImportResult.IMPORTED_BEST);
    System.out.println("xxx = " + bc.getRepository().getNonce(new AionAddress(deployerAccount.getAddress())));
    System.out.println("yyy = " + bc.getRepository().getNonce(contractAddress));
    assertEquals(BigInteger.valueOf(3), bc.getRepository().getNonce(new AionAddress(deployerAccount.getAddress())));
    assertEquals(BigInteger.valueOf(2), bc.getRepository().getNonce(contractAddress));
}
Also used : ImportResult(org.aion.zero.impl.core.ImportResult) AionAddress(org.aion.types.AionAddress) BlockContext(org.aion.zero.impl.types.BlockContext) BigInteger(java.math.BigInteger) StandaloneBlockchain(org.aion.zero.impl.blockchain.StandaloneBlockchain) ECKey(org.aion.crypto.ECKey) AionTransaction(org.aion.base.AionTransaction) Test(org.junit.Test)

Example 12 with ImportResult

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

the class InternalTransactionTest method testNestedCreateWithExistedAccount.

@Test
public void testNestedCreateWithExistedAccount() 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);
    AionAddress firstContractAddr = TxUtil.calculateContractAddress(deployerAccount.getAddress(), BigInteger.ONE);
    AionAddress internalContractAddress = TxUtil.calculateContractAddress(firstContractAddr.toByteArray(), BigInteger.ZERO);
    BigInteger nonce = BigInteger.ZERO;
    // ======================
    // Transfer balance to the internal contract address
    // ======================
    AionTransaction tx = AionTransaction.create(deployerAccount, nonce.toByteArray(), internalContractAddress, BigInteger.ONE.toByteArray(), new byte[0], 1_000_000L, minEnergyPrice, TransactionTypes.DEFAULT, null);
    Block parentBlock = bc.getBestBlock();
    MiningBlock newBlock = bc.createBlock(parentBlock, Collections.singletonList(tx), false, parentBlock.getTimestamp());
    Pair<ImportResult, AionBlockSummary> result = bc.tryToConnectAndFetchSummary(newBlock);
    assertTrue(result.getLeft().isSuccessful());
    nonce = nonce.add(BigInteger.ONE);
    bc.forkUtility.enable040Fork(1000);
    // ======================
    // DEPLOY Failed
    // ======================
    tx = AionTransaction.create(deployerAccount, nonce.toByteArray(), null, new byte[0], ByteUtil.hexStringToBytes(contractA), 1_000_000L, minEnergyPrice, TransactionTypes.DEFAULT, null);
    parentBlock = bc.getBestBlock();
    newBlock = bc.createBlock(parentBlock, Collections.singletonList(tx), false, parentBlock.getTimestamp());
    result = bc.tryToConnectAndFetchSummary(newBlock);
    assertTrue(result.getLeft().isSuccessful());
    assertEquals("reverted", result.getRight().getReceipts().get(0).getError());
    nonce = nonce.add(BigInteger.ONE);
    bc.forkUtility.enable040Fork(0);
    // ======================
    // DEPLOY
    // ======================
    tx = AionTransaction.create(deployerAccount, nonce.toByteArray(), null, new byte[0], ByteUtil.hexStringToBytes(contractA), 1_000_000L, minEnergyPrice, TransactionTypes.DEFAULT, null);
    System.out.println("contractaddr: " + TxUtil.calculateContractAddress(tx));
    BlockContext context = bc.createNewMiningBlockContext(bc.getBestBlock(), List.of(tx), false);
    AionTxExecSummary summary = executeTransaction(bc, context, tx);
    System.out.println(summary.getReceipt());
    boolean firstItx = true;
    for (InternalTransaction itx : summary.getInternalTransactions()) {
        System.out.println(itx);
        if (firstItx) {
            AionAddress contractAddress = TxUtil.calculateContractAddress(tx);
            assertNotNull(contractAddress);
            assertTrue(bc.getRepository().hasAccountState(contractAddress));
            assertTrue(bc.getRepository().hasAccountState(TxUtil.calculateContractAddress(contractAddress.toByteArray(), BigInteger.ZERO)));
            firstItx = false;
        }
    }
}
Also used : AionAddress(org.aion.types.AionAddress) ImportResult(org.aion.zero.impl.core.ImportResult) BlockContext(org.aion.zero.impl.types.BlockContext) AionTxExecSummary(org.aion.base.AionTxExecSummary) StandaloneBlockchain(org.aion.zero.impl.blockchain.StandaloneBlockchain) ECKey(org.aion.crypto.ECKey) AionTransaction(org.aion.base.AionTransaction) MiningBlock(org.aion.zero.impl.types.MiningBlock) InternalTransaction(org.aion.types.InternalTransaction) 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) Test(org.junit.Test)

Example 13 with ImportResult

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

the class InternalTransactionTest method testLogs.

@Test
public void testLogs() throws InterruptedException {
    String contractA = "0x605060405234156100105760006000fd5b610015565b61013c806100246000396000f30060506040526000356c01000000000000000000000000900463ffffffff1680632d7df21a146100335761002d565b60006000fd5b341561003f5760006000fd5b61006660048080806010013590359091602001909192908035906010019091905050610068565b005b7fc1599bd9a91e57420b9b93745d7475dc054736a3f2becd4f08b450b7012e125760405160405180910390a1828282600060405180806f662829000000000000000000000000008152601001506003019050604051809103902090506c01000000000000000000000000900491906040518363ffffffff166c01000000000000000000000000028152600401600060405180830381858a8a89f195505050505050505b5050505600a165627a7a723058205e51c42347e4353247e8419ef6cda02250d358868e2cb3782d0d5d74065f2ef70029";
    String contractB = "0x605060405234156100105760006000fd5b610015565b60cb806100236000396000f30060506040526000356c01000000000000000000000000900463ffffffff16806326121ff014603157602b565b60006000fd5b3415603c5760006000fd5b60426044565b005b600060007f45b3fe4256d6d198dc4c34457a04e8c048ce54df933a93061f1a0e386b52f7a260405160405180910390a160009150600090505b6103e8811015609a57808201915081505b8080600101915050607d565b5b50505600a165627a7a72305820b2bf8aef36001079d347d250e50b098ad52629336644a841d19db288f30667470029";
    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);
    nonce = nonce.add(BigInteger.ONE);
    AionTransaction tx2 = AionTransaction.create(deployerAccount, nonce.toByteArray(), null, new byte[0], ByteUtil.hexStringToBytes(contractB), 1_000_000L, minEnergyPrice, TransactionTypes.DEFAULT, null);
    BlockContext context = bc.createNewMiningBlockContext(bc.getBestBlock(), List.of(tx1, tx2), false);
    ImportResult result = bc.tryToConnect(context.block);
    assertThat(result).isEqualTo(ImportResult.IMPORTED_BEST);
    AionAddress addressA = TxUtil.calculateContractAddress(tx1);
    System.out.println("contract A = " + addressA);
    AionAddress addressB = TxUtil.calculateContractAddress(tx2);
    System.out.println("contract B = " + addressB);
    Thread.sleep(1000);
    // ======================
    // CALL B
    // ======================
    nonce = nonce.add(BigInteger.ONE);
    AionTransaction tx3 = AionTransaction.create(deployerAccount, nonce.toByteArray(), addressB, new byte[0], ByteUtil.hexStringToBytes("0x26121ff0"), 1_000_000L, minEnergyPrice, TransactionTypes.DEFAULT, null);
    context = bc.createNewMiningBlockContext(bc.getBestBlock(), List.of(tx3), false);
    result = bc.tryToConnect(context.block);
    assertThat(result).isEqualTo(ImportResult.IMPORTED_BEST);
    AionTxInfo info = bc.getTransactionInfo(tx3.getTransactionHash());
    System.out.println(info.getReceipt());
    assertEquals(1, info.getReceipt().getLogInfoList().size());
    Thread.sleep(1000);
    // ======================
    // CALL A (calls B, 80k)
    // ======================
    nonce = nonce.add(BigInteger.ONE);
    AionTransaction tx4 = AionTransaction.create(deployerAccount, nonce.toByteArray(), addressA, new byte[0], ByteUtil.merge(ByteUtil.hexStringToBytes("0x2d7df21a"), addressB.toByteArray(), new DataWord(80_000).getData()), 1_000_000L, minEnergyPrice, TransactionTypes.DEFAULT, null);
    context = bc.createNewMiningBlockContext(bc.getBestBlock(), List.of(tx4), false);
    result = bc.tryToConnect(context.block);
    assertThat(result).isEqualTo(ImportResult.IMPORTED_BEST);
    info = bc.getTransactionInfo(tx4.getTransactionHash());
    System.out.println(info.getReceipt());
    assertEquals(2, info.getReceipt().getLogInfoList().size());
    Thread.sleep(1000);
    // ======================
    // CALL A (calls B, 20k)
    // ======================
    nonce = nonce.add(BigInteger.ONE);
    AionTransaction tx6 = AionTransaction.create(deployerAccount, nonce.toByteArray(), addressA, new byte[0], ByteUtil.merge(ByteUtil.hexStringToBytes("0x2d7df21a"), addressB.toByteArray(), new DataWord(20_000).getData()), 1_000_000L, minEnergyPrice, TransactionTypes.DEFAULT, null);
    context = bc.createNewMiningBlockContext(bc.getBestBlock(), List.of(tx6), false);
    result = bc.tryToConnect(context.block);
    assertThat(result).isEqualTo(ImportResult.IMPORTED_BEST);
    info = bc.getTransactionInfo(tx6.getTransactionHash());
    System.out.println(info.getReceipt());
    assertEquals(1, info.getReceipt().getLogInfoList().size());
    Thread.sleep(1000);
}
Also used : ImportResult(org.aion.zero.impl.core.ImportResult) AionAddress(org.aion.types.AionAddress) AionTxInfo(org.aion.zero.impl.types.AionTxInfo) BlockContext(org.aion.zero.impl.types.BlockContext) 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 14 with ImportResult

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

the class MultiVersionAvmTest method testCallInBothAvmVersions.

/**
 * We test deploying & calling the same contract in version 1 and 2. We expect the same result
 * each time.
 */
@Test
public void testCallInBothAvmVersions() {
    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);
    AionAddress contractForVersion1 = deployHelloWorldContract(AvmVersion.VERSION_1, BigInteger.ZERO);
    AionTransaction transactionForVersion1 = makeHelloWorldCallTransaction(AvmVersion.VERSION_1, BigInteger.ONE, contractForVersion1);
    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);
    AionAddress contractForVersion2 = deployHelloWorldContract(AvmVersion.VERSION_2, BigInteger.TWO);
    AionTransaction transactionForVersion2 = makeHelloWorldCallTransaction(AvmVersion.VERSION_2, BigInteger.valueOf(3), contractForVersion2);
    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 : AionAddress(org.aion.types.AionAddress) 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)

Example 15 with ImportResult

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

the class ContractIntegTest method deployContract.

private AionAddress deployContract(RepositoryCache repo, AionTransaction tx, String contractName, String contractFilename, BigInteger value, long nrg, long nrgPrice, BigInteger nonce, boolean addToBlockChain) throws IOException, VmFatalException {
    assertTrue(tx.isContractCreationTransaction());
    assertEquals(deployerBalance, repo.getBalance(deployer));
    assertEquals(deployerNonce, repo.getNonce(deployer));
    MiningBlock block = makeBlock(tx);
    AionTxExecSummary summary = executeTransaction(tx, block, repo);
    if (!summary.getReceipt().getError().equals("")) {
        return null;
    }
    assertNotEquals(nrg, summary.getNrgUsed().longValue());
    AionAddress contract = TxUtil.calculateContractAddress(tx);
    if (contractFilename == null) {
        checkStateOfNewContract(repo, contractName, contract, summary.getResult(), FastVmResultCode.SUCCESS, value);
    } else {
        checkStateOfNewContract(repo, contractName, contractFilename, contract, summary.getResult(), FastVmResultCode.SUCCESS, value);
    }
    checkStateOfDeployer(repo, summary, nrgPrice, value, nonce);
    if (addToBlockChain) {
        Pair<ImportResult, AionBlockSummary> result = blockchain.tryToConnectAndFetchSummary(block);
        assertTrue(result.getLeft().isSuccessful());
    }
    return contract;
}
Also used : AionAddress(org.aion.types.AionAddress) ImportResult(org.aion.zero.impl.core.ImportResult) AionBlockSummary(org.aion.zero.impl.types.AionBlockSummary) AionTxExecSummary(org.aion.base.AionTxExecSummary) MiningBlock(org.aion.zero.impl.types.MiningBlock)

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