Search in sources :

Example 66 with RepositoryCache

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

the class AionRepositoryImplTest method testSyncToPreviousRootNoFlush.

@Test
public void testSyncToPreviousRootNoFlush() {
    final AionAddress FIRST_ACC = AddressUtils.wrapAddress(value2);
    final AionAddress SECOND_ACC = AddressUtils.wrapAddress(value3);
    final AionRepositoryImpl repository = AionRepositoryImpl.createForTesting(repoConfig);
    byte[] originalRoot = repository.getRoot();
    // now create a new account
    RepositoryCache track = repository.startTracking();
    track.addBalance(FIRST_ACC, BigInteger.ONE);
    track.flushTo(repository, true);
    System.out.println("after first account added");
    System.out.println(repository.getWorldState().getTrieDump());
    // check the update on the repo
    BigInteger balance = repository.getBalance(FIRST_ACC);
    assertThat(balance).isEqualTo(BigInteger.ONE);
    byte[] firstRoot = repository.getRoot();
    track = repository.startTracking();
    track.addBalance(SECOND_ACC, BigInteger.TWO);
    track.flushTo(repository, true);
    byte[] secondRoot = repository.getRoot();
    System.out.println("after second account added");
    System.out.println(repository.getWorldState().getTrieDump());
    assertThat(firstRoot).isNotEqualTo(originalRoot);
    assertThat(secondRoot).isNotEqualTo(firstRoot);
    System.out.println("after sync to after first account added");
    repository.syncToRoot(firstRoot);
    assertThat(repository.isValidRoot(firstRoot)).isTrue();
    System.out.println(repository.getWorldState().getTrieDump());
    assertThat(repository.getRoot()).isEqualTo(firstRoot);
    balance = repository.getBalance(FIRST_ACC);
    // notice that the first blocks balance is also zero
    assertThat(balance).isEqualTo(BigInteger.ONE);
}
Also used : AionAddress(org.aion.types.AionAddress) RepositoryCache(org.aion.base.db.RepositoryCache) BigInteger(java.math.BigInteger) Test(org.junit.Test)

Example 67 with RepositoryCache

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

the class AionRepositoryImplTest method testAccountStateUpdateStorageRowFlush.

@Test
public void testAccountStateUpdateStorageRowFlush() {
    AionRepositoryImpl repository = AionRepositoryImpl.createForTesting(repoConfig);
    RepositoryCache track = repository.startTracking();
    AionAddress defaultAccount = new AionAddress(ByteUtil.hexStringToBytes(value1));
    track.addBalance(defaultAccount, BigInteger.valueOf(1));
    // Consider the original root the one after an account has been added
    byte[] key = HashUtil.blake128("hello".getBytes());
    byte[] value = HashUtil.blake128("world".getBytes());
    track.addStorageRow(defaultAccount, new DataWord(key).toWrapper(), new DataWord(value).toWrapper());
    track.saveVmType(defaultAccount, InternalVmType.FVM);
    // does not call parent's flush
    track.flushTo(repository, true);
    repository.flush();
    /**
     * Verify that the account has been flushed
     */
    StoredContractDetails snapshot = repository.getContractDetails(defaultAccount);
    assertThat(snapshot instanceof FvmContractDetails).isTrue();
    FvmContractDetails details = (FvmContractDetails) snapshot;
    assertThat(details.get(new DataWord(key).toWrapper())).isEqualTo(new DataWord(value).toWrapper());
}
Also used : AionAddress(org.aion.types.AionAddress) RepositoryCache(org.aion.base.db.RepositoryCache) DataWord(org.aion.util.types.DataWord) Test(org.junit.Test)

Example 68 with RepositoryCache

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

the class OldTxExecutorTest method testBasicTransactionCost.

@Test
public void testBasicTransactionCost() throws Exception {
    byte[] txNonce = BigInteger.ZERO.toByteArray();
    AionAddress to = AddressUtils.wrapAddress("2222222222222222222222222222222222222222222222222222222222222222");
    byte[] value = BigInteger.ONE.toByteArray();
    byte[] data = new byte[0];
    long nrg = new DataWord(100000L).longValue();
    long nrgPrice = DataWord.ONE.longValue();
    ECKey key = ECKeyFac.inst().create();
    AionTransaction tx = AionTransaction.create(key, txNonce, to, value, data, nrg, nrgPrice, TransactionTypes.DEFAULT, null);
    MiningBlock block = createDummyBlock();
    AionRepositoryImpl repoTop = blockchain.getRepository();
    RepositoryCache repo = repoTop.startTracking();
    repo.addBalance(tx.getSenderAddress(), BigInteger.valueOf(1_000_000_000L));
    repo.flushTo(repoTop, true);
    AionTxReceipt receipt = executeTransaction(repo, block, tx).getReceipt();
    System.out.println(receipt);
    assertEquals(TxUtil.calculateTransactionCost(tx), receipt.getEnergyUsed());
}
Also used : AionAddress(org.aion.types.AionAddress) RepositoryCache(org.aion.base.db.RepositoryCache) DataWord(org.aion.util.types.DataWord) ECKey(org.aion.crypto.ECKey) AionTransaction(org.aion.base.AionTransaction) AionRepositoryImpl(org.aion.zero.impl.db.AionRepositoryImpl) AionTxReceipt(org.aion.base.AionTxReceipt) MiningBlock(org.aion.zero.impl.types.MiningBlock) Test(org.junit.Test)

Example 69 with RepositoryCache

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

the class OldTxExecutorTest method testPerformance.

@Test
public void testPerformance() 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();
    ECKey key = ECKeyFac.inst().create();
    AionTransaction tx = AionTransaction.create(key, txNonce, to, value, data, nrg, nrgPrice, TransactionTypes.DEFAULT, null);
    MiningBlock block = createDummyBlock();
    AionRepositoryImpl repoTop = blockchain.getRepository();
    RepositoryCache repo = repoTop.startTracking();
    repo.addBalance(tx.getSenderAddress(), BigInteger.valueOf(100_000).multiply(BigInteger.valueOf(tx.getEnergyPrice())));
    repo.createAccount(to);
    repo.saveCode(to, Hex.decode(contract));
    repo.saveVmType(to, InternalVmType.FVM);
    repo.flushTo(repoTop, true);
    long t1 = System.nanoTime();
    long repeat = 1000;
    for (int i = 0; i < repeat; i++) {
        executeTransaction(repo, block, tx);
    }
    long t2 = System.nanoTime();
    System.out.println((t2 - t1) / repeat);
}
Also used : Compiler(org.aion.solidity.Compiler) AionAddress(org.aion.types.AionAddress) DataWord(org.aion.util.types.DataWord) ECKey(org.aion.crypto.ECKey) 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) Test(org.junit.Test)

Example 70 with RepositoryCache

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

the class OldTxExecutorTest method testCreateTransaction.

@Test
public void testCreateTransaction() throws Exception {
    Compiler.Result r = Compiler.getInstance().compile(ContractUtils.readContract("Ticker.sol"), Options.ABI, Options.BIN);
    CompilationResult cr = CompilationResult.parse(r.output);
    String deployer = cr.contracts.get("Ticker").bin;
    System.out.println(deployer);
    byte[] txNonce = BigInteger.ZERO.toByteArray();
    AionAddress to = null;
    byte[] value = BigInteger.ZERO.toByteArray();
    byte[] data = Hex.decode(deployer);
    long nrg = 500_000L;
    long nrgPrice = 1;
    AionTransaction tx = AionTransaction.create(deployerKey, txNonce, to, value, data, nrg, nrgPrice, TransactionTypes.DEFAULT, null);
    MiningBlock block = createDummyBlock();
    AionRepositoryImpl repoTop = blockchain.getRepository();
    RepositoryCache repo = repoTop.startTracking();
    repo.addBalance(tx.getSenderAddress(), BigInteger.valueOf(500_000L).multiply(BigInteger.valueOf(tx.getEnergyPrice())));
    AionTxReceipt receipt = executeTransaction(repo, block, tx).getReceipt();
    System.out.println(receipt);
    assertArrayEquals(Hex.decode(deployer.substring(deployer.indexOf("60506040", 1))), receipt.getTransactionOutput());
}
Also used : Compiler(org.aion.solidity.Compiler) AionAddress(org.aion.types.AionAddress) RepositoryCache(org.aion.base.db.RepositoryCache) AionTransaction(org.aion.base.AionTransaction) AionRepositoryImpl(org.aion.zero.impl.db.AionRepositoryImpl) CompilationResult(org.aion.solidity.CompilationResult) AionTxReceipt(org.aion.base.AionTxReceipt) MiningBlock(org.aion.zero.impl.types.MiningBlock) 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