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)));
}
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));
}
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)));
}
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);
}
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);
}
Aggregations