Search in sources :

Example 6 with RepositoryCache

use of org.aion.base.db.RepositoryCache in project aion by aionnetwork.

the class ContractIntegTest method tellFvmContractCallBalanceTransfer.

@Test
public void tellFvmContractCallBalanceTransfer() throws Exception {
    if (txType == TransactionTypes.AVM_CREATE_CODE) {
        return;
    }
    String contractName = "InternalCallContract";
    byte[] deployCode = getDeployCode(contractName);
    long nrg = Constants.NRG_TRANSACTION_MAX;
    long nrgPrice = energyPrice;
    BigInteger value = BigInteger.ZERO;
    BigInteger nonce = BigInteger.ZERO;
    AionTransaction tx = AionTransaction.create(deployerKey, nonce.toByteArray(), null, value.toByteArray(), deployCode, nrg, nrgPrice, txType, null);
    RepositoryCache repo = blockchain.getRepository().startTracking();
    nonce = nonce.add(BigInteger.ONE);
    AionAddress contract = deployContract(repo, tx, contractName, null, value, nrg, nrgPrice, nonce, true);
    assertNotNull(contract);
    repo = blockchain.getRepository().startTracking();
    byte[] input = Arrays.copyOfRange(HashUtil.keccak256("sendValueToContract()".getBytes()), 0, 4);
    // input = ByteUtil.merge(input, new DataWordImpl(numRecurses + 1).copyOfData());
    tx = AionTransaction.create(deployerKey, nonce.toByteArray(), contract, BigInteger.TEN.toByteArray(), input, nrg, nrgPrice, txType, null);
    assertFalse(tx.isContractCreationTransaction());
    BigInteger senderBalance = repo.getBalance(deployer);
    MiningBlock block = makeBlock(tx);
    AionTxExecSummary summary = executeTransaction(tx, block, repo);
    assertEquals("", summary.getReceipt().getError());
    assertNotEquals(nrg, summary.getNrgUsed().longValue());
    Pair<ImportResult, AionBlockSummary> result = blockchain.tryToConnectAndFetchSummary(block);
    assertTrue(result.getLeft().isSuccessful());
    assertEquals(BigInteger.TEN, blockchain.getRepository().getBalance(contract));
    assertEquals(senderBalance.subtract(BigInteger.TEN).subtract(BigInteger.valueOf(summary.getNrgUsed().longValue()).multiply(BigInteger.valueOf(nrgPrice))), blockchain.getRepository().getBalance(deployer));
    input = Arrays.copyOfRange(HashUtil.keccak256("callBalanceTransfer(address)".getBytes()), 0, 4);
    AionAddress receiver = AddressUtils.wrapAddress("0x000000000000000000000000000000000000000000000000000000000000000a");
    input = ByteUtil.merge(input, receiver.toByteArray());
    nonce = nonce.add(BigInteger.ONE);
    tx = AionTransaction.create(deployerKey, nonce.toByteArray(), contract, BigInteger.ZERO.toByteArray(), input, nrg, nrgPrice, txType, null);
    assertFalse(tx.isContractCreationTransaction());
    repo = blockchain.getRepository().startTracking();
    block = makeBlock(tx);
    summary = executeTransaction(tx, block, repo);
    assertEquals("", summary.getReceipt().getError());
    assertNotEquals(nrg, summary.getNrgUsed().longValue());
    result = blockchain.tryToConnectAndFetchSummary(block);
    assertTrue(result.getLeft().isSuccessful());
    assertEquals(1, result.getRight().getSummaries().get(0).getInternalTransactions().size());
    assertEquals(BigInteger.TEN.subtract(BigInteger.ONE), blockchain.getRepository().getBalance(contract));
    assertEquals(BigInteger.ONE, blockchain.getRepository().getBalance(receiver));
}
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) BigInteger(java.math.BigInteger) AionRepositoryCache(org.aion.zero.impl.db.AionRepositoryCache) RepositoryCache(org.aion.base.db.RepositoryCache) AionTransaction(org.aion.base.AionTransaction) MiningBlock(org.aion.zero.impl.types.MiningBlock) Test(org.junit.Test)

Example 7 with RepositoryCache

use of org.aion.base.db.RepositoryCache in project aion by aionnetwork.

the class ContractIntegTest method testContractDeployCodeIsNonsensical.

@Test
public void testContractDeployCodeIsNonsensical() throws Exception {
    byte[] deployCode = new byte[1];
    deployCode[0] = 0x1;
    long nrg = 1_000_000;
    long nrgPrice = energyPrice;
    BigInteger value = BigInteger.ZERO;
    BigInteger nonce = BigInteger.ZERO;
    // to == null  signals that this is contract creation.
    AionTransaction tx = AionTransaction.create(deployerKey, nonce.toByteArray(), null, value.toByteArray(), deployCode, nrg, nrgPrice, txType, null);
    assertTrue(tx.isContractCreationTransaction());
    assertEquals(Builder.DEFAULT_BALANCE, blockchain.getRepository().getBalance(deployer));
    assertEquals(BigInteger.ZERO, blockchain.getRepository().getNonce(deployer));
    MiningBlock block = makeBlock(tx);
    RepositoryCache repo = blockchain.getRepository().startTracking();
    AionTxExecSummary summary = executeTransaction(tx, block, repo);
    if (txType == TransactionTypes.DEFAULT) {
        assertEquals("OUT_OF_NRG", summary.getReceipt().getError());
        assertEquals(nrg, summary.getNrgUsed().longValue());
        AionAddress contract = TxUtil.calculateContractAddress(tx);
        assertArrayEquals(new byte[0], summary.getResult());
        assertArrayEquals(new byte[0], repo.getCode(contract));
        assertEquals(BigInteger.ZERO, repo.getBalance(contract));
        assertEquals(BigInteger.ZERO, repo.getNonce(contract));
        assertEquals(BigInteger.ONE, repo.getNonce(deployer));
    } else if (txType == TransactionTypes.AVM_CREATE_CODE) {
        assertEquals("Failed: invalid data", summary.getReceipt().getError());
        nonce = nonce.add(BigInteger.ONE);
        checkStateOfDeployer(repo, summary, nrgPrice, value, nonce);
    }
    BigInteger txCost = summary.getNrgUsed().multiply(BigInteger.valueOf(nrgPrice));
    assertEquals(Builder.DEFAULT_BALANCE.subtract(txCost), repo.getBalance(deployer));
}
Also used : AionAddress(org.aion.types.AionAddress) AionTxExecSummary(org.aion.base.AionTxExecSummary) BigInteger(java.math.BigInteger) AionRepositoryCache(org.aion.zero.impl.db.AionRepositoryCache) RepositoryCache(org.aion.base.db.RepositoryCache) AionTransaction(org.aion.base.AionTransaction) MiningBlock(org.aion.zero.impl.types.MiningBlock) Test(org.junit.Test)

Example 8 with RepositoryCache

use of org.aion.base.db.RepositoryCache in project aion by aionnetwork.

the class ContractIntegTest method testFvmEmptyContractWith226160EnergyAfterUnity.

@Test
public void testFvmEmptyContractWith226160EnergyAfterUnity() throws IOException, VmFatalException {
    long unityForkBlock = 2;
    blockchain.forkUtility.enableUnityFork(unityForkBlock);
    ECKey stakingRegistryOwner = accounts.get(1);
    List<ECKey> stakers = new ArrayList<>(accounts);
    stakers.remove(deployerKey);
    stakers.remove(stakingRegistryOwner);
    assertThat(stakers.size()).isEqualTo(8);
    // the default configuration does not apply to this test case
    AvmTestConfig.clearConfigurations();
    AvmTestConfig.supportBothAvmVersions(0, unityForkBlock, 0);
    // populating the chain to be above the Unity for point
    BlockchainTestUtils.generateRandomUnityChain(blockchain, resourceProvider, unityForkBlock + 1, 1, stakers, stakingRegistryOwner, 10);
    String contractName = "EmptyContract";
    byte[] deployCode = getDeployCode(contractName);
    long nrg = 226160;
    long nrgPrice = energyPrice;
    BigInteger value = BigInteger.ZERO;
    BigInteger nonce = BigInteger.ZERO;
    // to == null  signals that this is contract creation.
    AionTransaction tx = AionTransaction.create(deployerKey, nonce.toByteArray(), null, value.toByteArray(), deployCode, nrg, nrgPrice, txType, null);
    assertTrue(tx.isContractCreationTransaction());
    assertEquals(Builder.DEFAULT_BALANCE, blockchain.getRepository().getBalance(deployer));
    assertEquals(BigInteger.ZERO, blockchain.getRepository().getNonce(deployer));
    // The transaction has invalid energylimit settings
    MiningBlock block = makeBlock(tx);
    RepositoryCache repo = blockchain.getRepository().startTracking();
    AionTxExecSummary summary = BulkExecutor.executeTransactionWithNoPostExecutionWork(block.getDifficulty(), block.getNumber(), block.getTimestamp(), block.getNrgLimit(), block.getCoinbase(), tx, repo, false, true, false, false, LOGGER_VM, BlockCachingContext.PENDING, block.getNumber() - 1, false, false);
    if (txType == TransactionTypes.DEFAULT) {
        assertEquals("OUT_OF_NRG", summary.getReceipt().getError());
        checkStateOfDeployer(repo, summary, nrgPrice, value, nonce.add(BigInteger.ONE));
        assertEquals(nrg, summary.getReceipt().getEnergyUsed());
    } else if (txType == TransactionTypes.AVM_CREATE_CODE) {
        assertEquals("Failed: invalid data", summary.getReceipt().getError());
        nonce = nonce.add(BigInteger.ONE);
        checkStateOfDeployer(repo, summary, nrgPrice, value, nonce);
        assertEquals(nrg, summary.getReceipt().getEnergyUsed());
    }
}
Also used : AionTxExecSummary(org.aion.base.AionTxExecSummary) ArrayList(java.util.ArrayList) BigInteger(java.math.BigInteger) AionRepositoryCache(org.aion.zero.impl.db.AionRepositoryCache) RepositoryCache(org.aion.base.db.RepositoryCache) ECKey(org.aion.crypto.ECKey) AionTransaction(org.aion.base.AionTransaction) MiningBlock(org.aion.zero.impl.types.MiningBlock) Test(org.junit.Test)

Example 9 with RepositoryCache

use of org.aion.base.db.RepositoryCache in project aion by aionnetwork.

the class OldTxExecutorTest method testCallTransaction.

@Test
public void testCallTransaction() throws Exception {
    Compiler.Result r = Compiler.getInstance().compile(ContractUtils.readContract("Ticker.sol"), Options.ABI, Options.BIN);
    CompilationResult cr = CompilationResult.parse(r.output);
    // deployer
    String deployer = cr.contracts.get("Ticker").bin;
    // contract
    String contract = deployer.substring(deployer.indexOf("60506040", 1));
    byte[] txNonce = BigInteger.ZERO.toByteArray();
    AionAddress to = AddressUtils.wrapAddress("2222222222222222222222222222222222222222222222222222222222222222");
    byte[] value = BigInteger.ZERO.toByteArray();
    byte[] data = Hex.decode("c0004213");
    long nrg = new DataWord(100000L).longValue();
    long nrgPrice = DataWord.ONE.longValue();
    AionTransaction tx = AionTransaction.create(deployerKey, txNonce, to, value, data, nrg, nrgPrice, TransactionTypes.DEFAULT, null);
    MiningBlock block = createDummyBlock();
    AionRepositoryImpl repo = blockchain.getRepository();
    RepositoryCache cache = repo.startTracking();
    cache.addBalance(tx.getSenderAddress(), BigInteger.valueOf(100_000).multiply(BigInteger.valueOf(tx.getEnergyPrice())));
    cache.createAccount(to);
    cache.saveCode(to, Hex.decode(contract));
    cache.saveVmType(to, InternalVmType.FVM);
    cache.flushTo(repo, true);
    AionTxReceipt receipt = executeTransaction(repo, block, tx).getReceipt();
    System.out.println(receipt);
    assertArrayEquals(Hex.decode("00000000000000000000000000000000"), receipt.getTransactionOutput());
}
Also used : Compiler(org.aion.solidity.Compiler) AionAddress(org.aion.types.AionAddress) DataWord(org.aion.util.types.DataWord) AionTransaction(org.aion.base.AionTransaction) AionRepositoryImpl(org.aion.zero.impl.db.AionRepositoryImpl) MiningBlock(org.aion.zero.impl.types.MiningBlock) RepositoryCache(org.aion.base.db.RepositoryCache) CompilationResult(org.aion.solidity.CompilationResult) AionTxReceipt(org.aion.base.AionTxReceipt) Test(org.junit.Test)

Example 10 with RepositoryCache

use of org.aion.base.db.RepositoryCache in project aion by aionnetwork.

the class OpcodeIntegTest method testCallcodeActors.

@Test
public void testCallcodeActors() 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);
    // Deployer calls contract D which performs CALLCODE to call contract E. From the
    // perspective
    // of the internal transaction, however, it looks like D calls D.
    long nrg = 1_000_000;
    long nrgPrice = 1;
    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, BigInteger.ZERO.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 the internal transaction is sent from D to D.
    List<InternalTransaction> internalTxs = summary.getInternalTransactions();
    assertEquals(1, internalTxs.size());
    assertEquals(D, internalTxs.get(0).sender);
    assertEquals(D, internalTxs.get(0).destination);
}
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) InternalTransaction(org.aion.types.InternalTransaction) Test(org.junit.Test)

Aggregations

RepositoryCache (org.aion.base.db.RepositoryCache)120 Test (org.junit.Test)102 AionAddress (org.aion.types.AionAddress)90 AionTransaction (org.aion.base.AionTransaction)89 AionTxExecSummary (org.aion.base.AionTxExecSummary)72 BigInteger (java.math.BigInteger)43 MiningBlock (org.aion.zero.impl.types.MiningBlock)41 AccountState (org.aion.base.AccountState)38 DataWord (org.aion.util.types.DataWord)23 AionRepositoryCache (org.aion.zero.impl.db.AionRepositoryCache)23 ImportResult (org.aion.zero.impl.core.ImportResult)22 InternalTransaction (org.aion.types.InternalTransaction)18 Block (org.aion.zero.impl.types.Block)18 AionTxReceipt (org.aion.base.AionTxReceipt)17 AionBlockchainImpl.getPostExecutionWorkForGeneratePreBlock (org.aion.zero.impl.blockchain.AionBlockchainImpl.getPostExecutionWorkForGeneratePreBlock)16 BlockContext (org.aion.zero.impl.types.BlockContext)15 SolidityType (org.aion.solidity.SolidityType)10 ArrayList (java.util.ArrayList)8 AionBlockSummary (org.aion.zero.impl.types.AionBlockSummary)7 AionRepositoryImpl (org.aion.zero.impl.db.AionRepositoryImpl)6