Search in sources :

Example 61 with RepositoryCache

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

the class AionRepositoryImplTest method testAccountStateUpdateStorageRow.

@Test
public void testAccountStateUpdateStorageRow() {
    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[] originalRoot = repository.getRoot();
    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);
    track.flushTo(repository, true);
    byte[] retrievedValue = repository.getStorageValue(defaultAccount, new DataWord(key).toWrapper()).getNoLeadZeroesData();
    assertThat(retrievedValue).isEqualTo(value);
    byte[] newRoot = repository.getRoot();
    System.out.println(String.format("original root: %s", ByteUtil.toHexString(originalRoot)));
    System.out.println(String.format("new root: %s", ByteUtil.toHexString(newRoot)));
}
Also used : AionAddress(org.aion.types.AionAddress) RepositoryCache(org.aion.base.db.RepositoryCache) DataWord(org.aion.util.types.DataWord) Test(org.junit.Test)

Example 62 with RepositoryCache

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

the class AionRepositoryImplTest method testGetSnapshotToRoot.

@Test
public void testGetSnapshotToRoot() {
    AionRepositoryImpl repository = AionRepositoryImpl.createForTesting(repoConfig);
    // make some changes to the repository
    final AionAddress account1 = AddressUtils.wrapAddress(value1);
    final AionAddress account2 = AddressUtils.wrapAddress(value2);
    final AionAddress account3 = AddressUtils.wrapAddress(value3);
    RepositoryCache track = repository.startTracking();
    track.addBalance(account1, BigInteger.ONE);
    track.addBalance(account2, BigInteger.TWO);
    track.addBalance(account3, BigInteger.TEN);
    track.flushTo(repository, true);
    repository.flush();
    // get snapshot to root
    Repository snapshot = repository.getSnapshotTo(repository.getRoot());
    // check that the same values are retrieved
    assertThat(repository.getBalance(account1)).isEqualTo(snapshot.getBalance(account1));
    assertThat(repository.getBalance(account2)).isEqualTo(snapshot.getBalance(account2));
    assertThat(repository.getBalance(account3)).isEqualTo(snapshot.getBalance(account3));
    // make more changes to the initial repo
    track = repository.startTracking();
    track.addBalance(account1, BigInteger.TWO);
    track.addBalance(account2, BigInteger.TWO);
    track.addBalance(account3, BigInteger.TWO);
    track.flushTo(repository, true);
    // check that the values from the repo are larger
    assertThat(repository.getBalance(account1)).isGreaterThan(snapshot.getBalance(account1));
    assertThat(repository.getBalance(account2)).isGreaterThan(snapshot.getBalance(account2));
    assertThat(repository.getBalance(account3)).isGreaterThan(snapshot.getBalance(account3));
    // make the same changes to the snapshot
    track = snapshot.startTracking();
    track.addBalance(account1, BigInteger.TWO);
    track.addBalance(account2, BigInteger.TWO);
    track.addBalance(account3, BigInteger.TWO);
    track.flushTo(snapshot, true);
    // check that the values are again equal
    assertThat(repository.getBalance(account1)).isEqualTo(snapshot.getBalance(account1));
    assertThat(repository.getBalance(account2)).isEqualTo(snapshot.getBalance(account2));
    assertThat(repository.getBalance(account3)).isEqualTo(snapshot.getBalance(account3));
    // make more changes on the snapshot
    track = snapshot.startTracking();
    track.addBalance(account1, BigInteger.TEN);
    track.addBalance(account2, BigInteger.TEN);
    track.addBalance(account3, BigInteger.TEN);
    track.flushTo(snapshot, true);
    // check that the values from the snapshot are larger
    assertThat(repository.getBalance(account1)).isLessThan(snapshot.getBalance(account1));
    assertThat(repository.getBalance(account2)).isLessThan(snapshot.getBalance(account2));
    assertThat(repository.getBalance(account3)).isLessThan(snapshot.getBalance(account3));
}
Also used : Repository(org.aion.base.db.Repository) AionAddress(org.aion.types.AionAddress) RepositoryCache(org.aion.base.db.RepositoryCache) Test(org.junit.Test)

Example 63 with RepositoryCache

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

the class AionRepositoryImplTest method testAccountAddCodeStorage.

@Test
public void testAccountAddCodeStorage() {
    AionRepositoryImpl repository = AionRepositoryImpl.createForTesting(repoConfig);
    RepositoryCache track = repository.startTracking();
    AionAddress defaultAccount = new AionAddress(ByteUtil.hexStringToBytes(value1));
    track.addBalance(defaultAccount, BigInteger.valueOf(1));
    byte[] originalRoot = repository.getRoot();
    track.saveCode(defaultAccount, defaultAccount.toByteArray());
    track.saveVmType(defaultAccount, InternalVmType.FVM);
    track.flushTo(repository, true);
    byte[] newRoot = repository.getRoot();
    assertThat(newRoot).isNotEqualTo(originalRoot);
    assertThat(repository.getCode(defaultAccount)).isEqualTo(defaultAccount.toByteArray());
    System.out.println(String.format("originalRoot: %s", ByteUtil.toHexString(originalRoot)));
    System.out.println(String.format("newRoot: %s", ByteUtil.toHexString(originalRoot)));
}
Also used : AionAddress(org.aion.types.AionAddress) RepositoryCache(org.aion.base.db.RepositoryCache) Test(org.junit.Test)

Example 64 with RepositoryCache

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

the class AionRepositoryImplTest method testAccountStateUpdate.

@Test
public void testAccountStateUpdate() {
    AionRepositoryImpl repository = AionRepositoryImpl.createForTesting(repoConfig);
    byte[] originalRoot = repository.getRoot();
    AionAddress defaultAccount = new AionAddress(ByteUtil.hexStringToBytes(value1));
    RepositoryCache track = repository.startTracking();
    track.addBalance(defaultAccount, BigInteger.valueOf(1));
    track.flushTo(repository, true);
    byte[] newRoot = repository.getRoot();
    System.out.println(String.format("original root: %s", ByteUtil.toHexString(originalRoot)));
    System.out.println(String.format("new root: %s", ByteUtil.toHexString(newRoot)));
    assertThat(newRoot).isNotEqualTo(originalRoot);
}
Also used : AionAddress(org.aion.types.AionAddress) RepositoryCache(org.aion.base.db.RepositoryCache) Test(org.junit.Test)

Example 65 with RepositoryCache

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

the class AionRepositoryImplTest method testRepoTrackUpdateStorageRow.

/**
 * Repo track test suite
 */
/**
 * This test confirms that updates done on the repo track are successfully translated into the
 * root repository.
 */
@Test
public void testRepoTrackUpdateStorageRow() {
    final AionRepositoryImpl repository = AionRepositoryImpl.createForTesting(repoConfig);
    final RepositoryCache repoTrack = repository.startTracking();
    final AionAddress defaultAccount = new AionAddress(ByteUtil.hexStringToBytes(value1));
    final byte[] key = HashUtil.blake128("hello".getBytes());
    final byte[] value = HashUtil.blake128("world".getBytes());
    repoTrack.addBalance(defaultAccount, BigInteger.valueOf(1));
    final byte[] originalRoot = repository.getRoot();
    repoTrack.addStorageRow(defaultAccount, new DataWord(key).toWrapper(), new DataWord(value).toWrapper());
    repoTrack.saveVmType(defaultAccount, InternalVmType.FVM);
    ByteArrayWrapper retrievedStorageValue = repoTrack.getStorageValue(defaultAccount, new DataWord(key).toWrapper());
    assertThat(retrievedStorageValue).isEqualTo(new DataWord(value).toWrapper());
    // commit changes, then check that the root has updated
    repoTrack.flushTo(repository, true);
    assertThat(repository.getStorageValue(defaultAccount, new DataWord(key).toWrapper())).isEqualTo(retrievedStorageValue);
    final byte[] newRoot = repository.getRoot();
    assertThat(newRoot).isNotEqualTo(originalRoot);
}
Also used : AionAddress(org.aion.types.AionAddress) ByteArrayWrapper(org.aion.util.types.ByteArrayWrapper) RepositoryCache(org.aion.base.db.RepositoryCache) DataWord(org.aion.util.types.DataWord) 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