Search in sources :

Example 51 with Address

use of org.aion.base.type.Address in project aion by aionnetwork.

the class AionRepositoryImplTest method testSyncToPreviousRootNoFlush.

/**
 * Tests behaviour for trie when trying to revert to a previous root without
 * first flushing. Note the behaviour here. Interestingly enough, it seems like
 * the trie must first be flushed, so that the root node is in the caching/db layer.
 *
 * Otherwise the retrieval methods will not be able to find the temporal root value.
 */
@Test
public void testSyncToPreviousRootNoFlush() {
    final Address FIRST_ACC = Address.wrap("CAFECAFECAFECAFECAFECAFECAFECAFECAFECAFECAFECAFECAFECAFECAFECAFE");
    final Address SECOND_ACC = Address.wrap("BEEFBEEFBEEFBEEFBEEFBEEFBEEFBEEFBEEFBEEFBEEFBEEFBEEFBEEFBEEFBEEF");
    final AionRepositoryImpl repository = AionRepositoryImpl.createForTesting(repoConfig);
    byte[] originalRoot = repository.getRoot();
    // now create a new account
    IRepositoryCache track = repository.startTracking();
    track.addBalance(FIRST_ACC, BigInteger.ONE);
    track.flush();
    System.out.println("after first account added");
    System.out.println(repository.getWorldState().getTrieDump());
    byte[] firstRoot = repository.getRoot();
    track = repository.startTracking();
    track.addBalance(SECOND_ACC, BigInteger.TWO);
    track.flush();
    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);
    repository.syncToRoot(firstRoot);
    assertThat(repository.getRoot()).isEqualTo(firstRoot);
    BigInteger balance = repository.getBalance(FIRST_ACC);
    // notice that the first blocks balance is also zero
    assertThat(balance).isEqualTo(BigInteger.ZERO);
}
Also used : Address(org.aion.base.type.Address) IRepositoryCache(org.aion.base.db.IRepositoryCache) BigInteger(java.math.BigInteger) AionRepositoryImpl(org.aion.zero.impl.db.AionRepositoryImpl) Test(org.junit.Test)

Example 52 with Address

use of org.aion.base.type.Address 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 IRepositoryCache<AccountState, DataWord, IBlockStoreBase<?, ?>> repoTrack = repository.startTracking();
    final Address defaultAccount = Address.wrap(ByteUtil.hexStringToBytes("CAF3CAF3CAF3CAF3CAF3CAF3CAF3CAF3CAF3CAF3CAF3CAF3CAF3CAF3CAF3CAF3"));
    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), new DataWord(value));
    DataWord retrievedStorageValue = repoTrack.getStorageValue(defaultAccount, new DataWord(key));
    assertThat(retrievedStorageValue).isEqualTo(new DataWord(value));
    // commit changes, then check that the root has updated
    repoTrack.flush();
    assertThat(repository.getStorageValue(defaultAccount, new DataWord(key))).isEqualTo(retrievedStorageValue);
    final byte[] newRoot = repository.getRoot();
    assertThat(newRoot).isNotEqualTo(originalRoot);
}
Also used : Address(org.aion.base.type.Address) AionRepositoryImpl(org.aion.zero.impl.db.AionRepositoryImpl) DataWord(org.aion.mcf.vm.types.DataWord) AccountState(org.aion.mcf.core.AccountState) IBlockStoreBase(org.aion.mcf.db.IBlockStoreBase) Test(org.junit.Test)

Example 53 with Address

use of org.aion.base.type.Address in project aion by aionnetwork.

the class AionRepositoryImplTest method testAccountStateUpdateStorageRow.

@Test
public void testAccountStateUpdateStorageRow() {
    AionRepositoryImpl repository = AionRepositoryImpl.createForTesting(repoConfig);
    IRepositoryCache track = repository.startTracking();
    Address defaultAccount = Address.wrap(ByteUtil.hexStringToBytes("CAF3CAF3CAF3CAF3CAF3CAF3CAF3CAF3CAF3CAF3CAF3CAF3CAF3CAF3CAF3CAF3"));
    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), new DataWord(value));
    track.flush();
    byte[] retrievedValue = repository.getStorageValue(defaultAccount, new DataWord(key)).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 : Address(org.aion.base.type.Address) IRepositoryCache(org.aion.base.db.IRepositoryCache) AionRepositoryImpl(org.aion.zero.impl.db.AionRepositoryImpl) DataWord(org.aion.mcf.vm.types.DataWord) Test(org.junit.Test)

Example 54 with Address

use of org.aion.base.type.Address in project aion by aionnetwork.

the class AionRepositoryImplTest method testAccountStateUpdateStorageRowFlush.

@Test
public void testAccountStateUpdateStorageRowFlush() {
    AionRepositoryImpl repository = AionRepositoryImpl.createForTesting(repoConfig);
    IRepositoryCache track = repository.startTracking();
    Address defaultAccount = Address.wrap(ByteUtil.hexStringToBytes("CAF3CAF3CAF3CAF3CAF3CAF3CAF3CAF3CAF3CAF3CAF3CAF3CAF3CAF3CAF3CAF3"));
    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), new DataWord(value));
    // does not call parent's flush
    track.flush();
    repository.flush();
    /**
     * Verify that the account has been flushed
     */
    IByteArrayKeyValueDatabase detailsDB = repository.getDetailsDatabase();
    Optional<byte[]> serializedDetails = detailsDB.get(defaultAccount.toBytes());
    assertThat(serializedDetails.isPresent()).isEqualTo(true);
    AionContractDetailsImpl details = new AionContractDetailsImpl(0, 1000000);
    details.decode(serializedDetails.get());
    assertThat(details.get(new DataWord(key))).isEqualTo(new DataWord(value));
}
Also used : Address(org.aion.base.type.Address) IByteArrayKeyValueDatabase(org.aion.base.db.IByteArrayKeyValueDatabase) IRepositoryCache(org.aion.base.db.IRepositoryCache) AionRepositoryImpl(org.aion.zero.impl.db.AionRepositoryImpl) DataWord(org.aion.mcf.vm.types.DataWord) AionContractDetailsImpl(org.aion.zero.db.AionContractDetailsImpl) Test(org.junit.Test)

Example 55 with Address

use of org.aion.base.type.Address in project aion by aionnetwork.

the class AionRepositoryImplTest method testAccountAddCodeStorage.

@Test
public void testAccountAddCodeStorage() {
    AionRepositoryImpl repository = AionRepositoryImpl.createForTesting(repoConfig);
    IRepositoryCache track = repository.startTracking();
    Address defaultAccount = Address.wrap(ByteUtil.hexStringToBytes("CAF3CAF3CAF3CAF3CAF3CAF3CAF3CAF3CAF3CAF3CAF3CAF3CAF3CAF3CAF3CAF3"));
    track.addBalance(defaultAccount, BigInteger.valueOf(1));
    byte[] originalRoot = repository.getRoot();
    track.saveCode(defaultAccount, defaultAccount.toBytes());
    track.flush();
    byte[] newRoot = repository.getRoot();
    assertThat(newRoot).isNotEqualTo(originalRoot);
    assertThat(repository.getCode(defaultAccount)).isEqualTo(defaultAccount.toBytes());
    System.out.println(String.format("originalRoot: %s", ByteUtil.toHexString(originalRoot)));
    System.out.println(String.format("newRoot: %s", ByteUtil.toHexString(originalRoot)));
}
Also used : Address(org.aion.base.type.Address) IRepositoryCache(org.aion.base.db.IRepositoryCache) AionRepositoryImpl(org.aion.zero.impl.db.AionRepositoryImpl) Test(org.junit.Test)

Aggregations

Address (org.aion.base.type.Address)61 Test (org.junit.Test)34 BigInteger (java.math.BigInteger)29 AionTransaction (org.aion.zero.types.AionTransaction)23 ITransaction (org.aion.base.type.ITransaction)14 ECKey (org.aion.crypto.ECKey)13 DataWord (org.aion.mcf.vm.types.DataWord)11 AionRepositoryImpl (org.aion.zero.impl.db.AionRepositoryImpl)10 TxPoolA0 (org.aion.txpool.zero.TxPoolA0)9 IRepositoryCache (org.aion.base.db.IRepositoryCache)8 HashMap (java.util.HashMap)6 ByteArrayWrapper (org.aion.base.util.ByteArrayWrapper)5 ByteUtil.toHexString (org.aion.base.util.ByteUtil.toHexString)5 AccountState (org.aion.mcf.core.AccountState)5 AionBlock (org.aion.zero.impl.types.AionBlock)5 ArrayList (java.util.ArrayList)4 ImportResult (org.aion.mcf.core.ImportResult)4 IByteArrayKeyValueDatabase (org.aion.base.db.IByteArrayKeyValueDatabase)3 IRepository (org.aion.base.db.IRepository)3 AionContractDetailsImpl (org.aion.zero.db.AionContractDetailsImpl)3