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