use of org.aion.base.db.RepositoryCache in project aion by aionnetwork.
the class TransactionExecutorTest method testExecutor.
@Test
public void testExecutor() throws Exception {
byte[] deployCode = ContractUtils.getContractDeployer("ByteArrayMap.sol", "ByteArrayMap");
long nrg = 1_000_000;
long nrgPrice = energyPrice;
BigInteger value = BigInteger.ZERO;
BigInteger nonce = BigInteger.ZERO;
AionTransaction tx = AionTransaction.create(deployerKey, nonce.toByteArray(), null, value.toByteArray(), deployCode, nrg, nrgPrice, TransactionTypes.DEFAULT, null);
assertTrue(tx.isContractCreationTransaction());
assertEquals(Builder.DEFAULT_BALANCE, blockchain.getRepository().getBalance(deployer));
assertEquals(BigInteger.ZERO, blockchain.getRepository().getNonce(deployer));
BlockContext context = blockchain.createNewMiningBlockContext(blockchain.getBestBlock(), Collections.singletonList(tx), false);
RepositoryCache repo = blockchain.getRepository().startTracking();
AionTxExecSummary summary = executeTransaction(repo, context, tx);
BigInteger refund = summary.getRefund();
// We expect that there is a new account created, the contract, with 0 balance and 0 nonce
// and that its code is the contract body. We also expect that the deployer (sender) has
// its nonce incremented and its balance is now equal to its old balance minus the
// transaction
// fee plus the refund
byte[] body = ContractUtils.getContractBody("ByteArrayMap.sol", "ByteArrayMap");
assertEquals("", summary.getReceipt().getError());
assertArrayEquals(body, summary.getResult());
AionAddress contract = TxUtil.calculateContractAddress(summary.getTransaction());
assertArrayEquals(body, repo.getCode(contract));
assertEquals(BigInteger.ZERO, repo.getBalance(contract));
assertEquals(BigInteger.ZERO, repo.getNonce(contract));
BigInteger txFee = BigInteger.valueOf(nrg).multiply(BigInteger.valueOf(nrgPrice));
assertEquals(Builder.DEFAULT_BALANCE.subtract(txFee).add(refund), repo.getBalance(deployer));
assertEquals(BigInteger.ONE, repo.getNonce(deployer));
}
use of org.aion.base.db.RepositoryCache in project aion by aionnetwork.
the class TransactionExecutorTest method testDeployedCodeFunctionality.
@Test
public void testDeployedCodeFunctionality() throws Exception {
AionAddress contract = deployByteArrayContract();
byte[] callingCode = Hex.decode(f_func);
BigInteger nonce = blockchain.getRepository().getNonce(deployer);
AionTransaction tx = AionTransaction.create(deployerKey, nonce.toByteArray(), contract, BigInteger.ZERO.toByteArray(), callingCode, 1_000_000, energyPrice, TransactionTypes.DEFAULT, null);
assertFalse(tx.isContractCreationTransaction());
BlockContext context = blockchain.createNewMiningBlockContext(blockchain.getBestBlock(), Collections.singletonList(tx), false);
RepositoryCache repo = blockchain.getRepository().startTracking();
AionTxExecSummary summary = executeTransaction(repo, context, tx);
assertEquals("", summary.getReceipt().getError());
System.out.println(Hex.toHexString(summary.getResult()));
// We called the function f() which returns nothing.
assertEquals(0, summary.getReceipt().getTransactionOutput().length);
byte[] body = ContractUtils.getContractBody("ByteArrayMap.sol", "ByteArrayMap");
assertArrayEquals(body, blockchain.getRepository().getCode(contract));
// Now we call the g() function, which returns a byte array of 1024 bytes that starts with
// 'a' and ends with 'b'
callingCode = Hex.decode(g_func);
nonce = repo.getNonce(deployer);
tx = AionTransaction.create(deployerKey, nonce.toByteArray(), contract, BigInteger.ZERO.toByteArray(), callingCode, 1_000_000, energyPrice, TransactionTypes.DEFAULT, null);
assertFalse(tx.isContractCreationTransaction());
context = blockchain.createNewMiningBlockContext(blockchain.getBestBlock(), Collections.singletonList(tx), false);
summary = executeTransaction(repo, context, tx);
System.out.println(Hex.toHexString(summary.getResult()));
System.out.println(summary.getResult().length);
// // I'm guessing: first data word is the number of bytes that follows. Then those
// following
// // bytes denote the size of the output, which follows these last bytes.
// int len = new DataWordImpl(Arrays.copyOfRange(output, 0,
// DataWordImpl.BYTES)).intValue();
// byte[] outputLen = new byte[len];
// System.arraycopy(output, DataWordImpl.BYTES, outputLen, 0, len);
// int outputSize = new BigInteger(outputLen).intValue();
//
// byte[] expected = new byte[1024];
// expected[0] = 'a';
// expected[1023] = 'b';
//
// byte[] out = new byte[outputSize];
// System.arraycopy(output, DataWordImpl.BYTES + len, out, 0, outputSize);
byte[] expected = new byte[1024];
expected[0] = 'a';
expected[1023] = 'b';
byte[] out = extractActualOutput(summary.getResult());
assertArrayEquals(expected, out);
}
use of org.aion.base.db.RepositoryCache in project aion by aionnetwork.
the class TransactionExecutorTest method testGfunction.
@Test
public void testGfunction() throws Exception {
AionAddress contract = deployByteArrayContract();
byte[] callingCode = Hex.decode(g_func);
BigInteger nonce = blockchain.getRepository().getNonce(deployer);
AionTransaction tx = AionTransaction.create(deployerKey, nonce.toByteArray(), contract, BigInteger.ZERO.toByteArray(), callingCode, 1_000_000, energyPrice, TransactionTypes.DEFAULT, null);
assertFalse(tx.isContractCreationTransaction());
BlockContext context = blockchain.createNewMiningBlockContext(blockchain.getBestBlock(), Collections.singletonList(tx), false);
RepositoryCache repo = blockchain.getRepository().startTracking();
AionTxExecSummary summary = executeTransaction(repo, context, tx);
System.out.println(summary.getReceipt());
// System.out.println(Hex.toHexString(res.getOutput()));
// System.out.println(res.getOutput().length);
byte[] out = extractActualOutput(summary.getResult());
assertEquals(0, out.length);
}
use of org.aion.base.db.RepositoryCache in project aion by aionnetwork.
the class AvmProviderTest method testBalanceTransferTransactionVersion1.
@Test
public void testBalanceTransferTransactionVersion1() throws Exception {
Assert.assertTrue(AvmProvider.tryAcquireLock(1, TimeUnit.MINUTES));
AvmProvider.enableAvmVersion(AvmVersion.VERSION_1, this.projectRootDir);
AvmProvider.startAvm(AvmVersion.VERSION_1);
// Set up the repo and give the sender account some balance.
RepositoryCache repository = newRepository();
AionAddress sender = randomAddress();
AionAddress recipient = randomAddress();
addBalance(repository, sender, BigInteger.valueOf(1_000_000));
// Run the transaction.
RepositoryCache repositoryChild = repository.startTracking();
IAvmExternalState externalState = newExternalState(AvmVersion.VERSION_1, repositoryChild, newEnergyRules());
Transaction transaction = newBalanceTransferTransaction(sender, recipient, BigInteger.TEN);
IAionVirtualMachine avm = AvmProvider.getAvm(AvmVersion.VERSION_1);
IAvmFutureResult[] futures = avm.run(externalState, new Transaction[] { transaction }, AvmExecutionType.MINING, 0);
// Assert the result and state changes we expect.
Assert.assertEquals(1, futures.length);
TransactionResult result = futures[0].getResult();
Assert.assertTrue(result.transactionStatus.isSuccess());
Assert.assertEquals(BigInteger.TEN, repositoryChild.getBalance(recipient));
AvmProvider.shutdownAvm(AvmVersion.VERSION_1);
AvmProvider.disableAvmVersion(AvmVersion.VERSION_1);
AvmProvider.releaseLock();
}
use of org.aion.base.db.RepositoryCache in project aion by aionnetwork.
the class ContractIntegTest method testFvmOverWithdrawFromContract.
@Test
public void testFvmOverWithdrawFromContract() throws Exception {
String contractName = "MultiFeatureContract";
byte[] deployCode = getDeployCode(contractName);
long nrg = 1_000_000;
long nrgPrice = energyPrice;
BigInteger value = BigInteger.ZERO;
BigInteger nonce = BigInteger.ZERO;
AionTransaction tx = AionTransaction.create(deployerKey, nonce.toByteArray(), null, value.toByteArray(), deployCode, nrg, nrgPrice, txType, null);
RepositoryCache repo = blockchain.getRepository().startTracking();
nonce = nonce.add(BigInteger.ONE);
AionAddress contract = deployContract(repo, tx, contractName, null, value, nrg, nrgPrice, nonce);
if (txType == TransactionTypes.AVM_CREATE_CODE) {
assertNull(contract);
return;
}
BigInteger deployerBalance = repo.getBalance(deployer);
repo.flushTo(blockchain.getRepository(), true);
repo = blockchain.getRepository().startTracking();
// Contract has no funds, try to withdraw just 1 coin.
byte[] input = ByteUtil.merge(Hex.decode("9424bba3"), new DataWord(1).getData());
tx = AionTransaction.create(deployerKey, nonce.toByteArray(), contract, BigInteger.ZERO.toByteArray(), input, nrg, nrgPrice, txType, null);
assertFalse(tx.isContractCreationTransaction());
MiningBlock block = makeBlock(tx);
AionTxExecSummary summary = executeTransaction(tx, block, repo);
assertEquals("reverted", summary.getReceipt().getError());
assertNotEquals(nrg, summary.getNrgUsed().longValue());
System.out.println("DEP: " + deployerBalance);
BigInteger txCost = BigInteger.valueOf(summary.getNrgUsed().longValue()).multiply(BigInteger.valueOf(nrgPrice));
assertEquals(deployerBalance.subtract(txCost), repo.getBalance(deployer));
}
Aggregations