Search in sources :

Example 6 with BlockContext

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

the class OpcodeIntegTest method testRevertAtBottomLevel.

@Test
public void testRevertAtBottomLevel() throws Exception {
    RepositoryCache repo = blockchain.getRepository().startTracking();
    AionAddress D = deployContract(repo, "F", "F.sol", BigInteger.ZERO);
    long nrg = 1_000_000;
    long nrgPrice = 1;
    BigInteger nonce = BigInteger.ONE;
    byte[] input = ByteUtil.merge(Hex.decode("8256cff3"), new DataWord(5).getData());
    AionTransaction tx = AionTransaction.create(deployerKey, nonce.toByteArray(), D, 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());
    // Check that the logs from our internal transactions are as we expect.
    List<Log> logs = summary.getReceipt().getLogInfoList();
    assertEquals(8, logs.size());
    assertArrayEquals(new DataWord(0).getData(), logs.get(0).copyOfData());
    assertArrayEquals(new DataWord(5).getData(), logs.get(1).copyOfData());
    assertArrayEquals(new DataWord(4).getData(), logs.get(2).copyOfData());
    assertArrayEquals(new DataWord(3).getData(), logs.get(3).copyOfData());
    assertArrayEquals(new DataWord(2).getData(), logs.get(4).copyOfData());
    assertArrayEquals(new DataWord(2).getData(), logs.get(5).copyOfData());
    assertArrayEquals(new DataWord(2).getData(), logs.get(6).copyOfData());
    assertArrayEquals(new DataWord(2).getData(), logs.get(7).copyOfData());
}
Also used : AionAddress(org.aion.types.AionAddress) Log(org.aion.types.Log) BlockContext(org.aion.zero.impl.types.BlockContext) AionTxExecSummary(org.aion.base.AionTxExecSummary) RepositoryCache(org.aion.base.db.RepositoryCache) BigInteger(java.math.BigInteger) DataWord(org.aion.util.types.DataWord) AionTransaction(org.aion.base.AionTransaction) Test(org.junit.Test)

Example 7 with BlockContext

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

the class OpcodeIntegTest method testCallcodeValueTransfer.

@Test
public void testCallcodeValueTransfer() throws Exception {
    RepositoryCache repo = blockchain.getRepository().startTracking();
    AionAddress D = deployContract(repo, "D", "D.sol", BigInteger.ZERO);
    AionAddress E = deployContract(repo, "E", "D.sol", BigInteger.ZERO);
    BigInteger balanceDeployer = repo.getBalance(deployer);
    BigInteger balanceD = repo.getBalance(D);
    BigInteger balanceE = repo.getBalance(E);
    // Deployer calls contract D which performs CALLCODE to call contract E.
    long nrg = 1_000_000;
    long nrgPrice = 1;
    BigInteger value = new BigInteger("2387653");
    BigInteger nonce = BigInteger.TWO;
    byte[] input = // use CALLCODE on E.
    ByteUtil.merge(Hex.decode("5cce9fc2"), E.toByteArray());
    // pass in 'n' also.
    input = ByteUtil.merge(input, new DataWord(0).getData());
    AionTransaction tx = AionTransaction.create(deployerKey, nonce.toByteArray(), D, value.toByteArray(), input, nrg, nrgPrice, TransactionTypes.DEFAULT, null);
    assertEquals(deployer, tx.getSenderAddress());
    assertEquals(D, tx.getDestinationAddress());
    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 txCost and sent value. We expect that D received value.
    // We expect E had no value change.
    BigInteger txCost = BigInteger.valueOf(summary.getNrgUsed().longValue() * nrgPrice);
    assertEquals(balanceDeployer.subtract(value).subtract(txCost), repo.getBalance(deployer));
    assertEquals(balanceD.add(value), repo.getBalance(D));
    assertEquals(balanceE, repo.getBalance(E));
}
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) DataWord(org.aion.util.types.DataWord) AionTransaction(org.aion.base.AionTransaction) Test(org.junit.Test)

Example 8 with BlockContext

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

the class FvmExploitsTest method testModifer.

@Ignore
@Test
public void testModifer() throws InterruptedException {
    String testerByteCode = "0x605060405234156100105760006000fd5b5b3360006000508282909180600101839055555050505b61002c565b61016e8061003b6000396000f30060506040526000356c01000000000000000000000000900463ffffffff1680630acf8c2e14610049578063421b2d8b14610073578063e61a60bb1461009f57610043565b60006000fd5b34156100555760006000fd5b61005d6100c9565b6040518082815260100191505060405180910390f35b341561007f5760006000fd5b61009d600480808060100135903590916020019091929050506100d7565b005b34156100ab5760006000fd5b6100b3610113565b6040518082815260100191505060405180910390f35b6000600a90506100d4565b90565b600060005080600101549054339091149190141615156100f75760006000fd5b818160026000508282909180600101839055555050505b5b5050565b6000600260005080600101549054339091149190141615156101355760006000fd5b600b905061013e565b5b905600a165627a7a723058201d8c8bf193120213679831363ac65fecb0dcb5be8b65e6c0a1c97f4a7d3d3ef20029";
    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 byte[0], // getEleven()
    ByteUtil.hexStringToBytes("0xe61a60bb"), 1_000_000L, energyPrice, TransactionTypes.DEFAULT, null);
    BlockContext context2 = bc.createNewMiningBlockContext(bc.getBestBlock(), Collections.singletonList(tx2), false);
    ImportResult result2 = bc.tryToConnect(context2.block);
    assertThat(result2).isEqualTo(ImportResult.IMPORTED_BEST);
    Thread.sleep(1000L);
    // assert failure
    AionTxInfo info2 = bc.getTransactionInfo(context2.block.getTransactionsList().get(0).getTransactionHash());
    assertEquals("REVERT", info2.getReceipt().getError());
    // =======================================================================
    nonce = nonce.add(BigInteger.ONE);
    AionTransaction tx3 = AionTransaction.create(deployerAccount, nonce.toByteArray(), contractAddress, new byte[0], ByteUtil.hexStringToBytes("0x421b2d8b" + Hex.toHexString(// addUser(address)
    deployerAccount.getAddress())), 1_000_000L, energyPrice, TransactionTypes.DEFAULT, null);
    BlockContext context3 = bc.createNewMiningBlockContext(bc.getBestBlock(), Collections.singletonList(tx3), false);
    ImportResult result3 = bc.tryToConnect(context3.block);
    assertThat(result3).isEqualTo(ImportResult.IMPORTED_BEST);
    Thread.sleep(1000L);
    // assert failure
    AionTxInfo info3 = bc.getTransactionInfo(context3.block.getTransactionsList().get(0).getTransactionHash());
    assertEquals("", info3.getReceipt().getError());
    Thread.sleep(1000L);
    // =======================================================================
    nonce = nonce.add(BigInteger.ONE);
    AionTransaction tx4 = AionTransaction.create(deployerAccount, nonce.toByteArray(), contractAddress, new byte[0], // getEleven()
    ByteUtil.hexStringToBytes("0xe61a60bb"), 1_000_000L, energyPrice, TransactionTypes.DEFAULT, null);
    BlockContext context4 = bc.createNewMiningBlockContext(bc.getBestBlock(), Collections.singletonList(tx4), false);
    ImportResult result4 = bc.tryToConnect(context4.block);
    assertThat(result4).isEqualTo(ImportResult.IMPORTED_BEST);
    Thread.sleep(1000L);
    // assert failure
    AionTxInfo info4 = bc.getTransactionInfo(context4.block.getTransactionsList().get(0).getTransactionHash());
    assertEquals("", info4.getReceipt().getError());
    assertEquals(11, FvmDataWord.fromBytes(info4.getReceipt().getTransactionOutput()).toInt());
}
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) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 9 with BlockContext

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

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

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