use of org.aion.base.db.RepositoryCache in project aion by aionnetwork.
the class OpcodeIntegTest method testCallcodeValueTransfer.
@Test
public void testCallcodeValueTransfer() throws Exception {
RepositoryCache repo = blockchain.getRepository().startTracking();
AionAddress D = deployContract(repo, "D", "D.sol", BigInteger.ZERO);
AionAddress E = deployContract(repo, "E", "D.sol", BigInteger.ZERO);
BigInteger balanceDeployer = repo.getBalance(deployer);
BigInteger balanceD = repo.getBalance(D);
BigInteger balanceE = repo.getBalance(E);
// Deployer calls contract D which performs CALLCODE to call contract E.
long nrg = 1_000_000;
long nrgPrice = 1;
BigInteger value = new BigInteger("2387653");
BigInteger nonce = BigInteger.TWO;
byte[] input = // use CALLCODE on E.
ByteUtil.merge(Hex.decode("5cce9fc2"), E.toByteArray());
// pass in 'n' also.
input = ByteUtil.merge(input, new DataWord(0).getData());
AionTransaction tx = AionTransaction.create(deployerKey, nonce.toByteArray(), D, value.toByteArray(), input, nrg, nrgPrice, TransactionTypes.DEFAULT, null);
assertEquals(deployer, tx.getSenderAddress());
assertEquals(D, tx.getDestinationAddress());
BlockContext context = blockchain.createNewMiningBlockContext(blockchain.getBestBlock(), Collections.singletonList(tx), false);
AionTxExecSummary summary = executeTransaction(tx, context.block, repo);
assertEquals("", summary.getReceipt().getError());
assertEquals(summary.getNrgUsed().longValue(), summary.getNrgUsed().longValue());
// We expect that deployer paid the txCost and sent value. We expect that D received value.
// We expect E had no value change.
BigInteger txCost = BigInteger.valueOf(summary.getNrgUsed().longValue() * nrgPrice);
assertEquals(balanceDeployer.subtract(value).subtract(txCost), repo.getBalance(deployer));
assertEquals(balanceD.add(value), repo.getBalance(D));
assertEquals(balanceE, repo.getBalance(E));
}
use of org.aion.base.db.RepositoryCache in project aion by aionnetwork.
the class InternalTransactionTest method executeTransaction.
private AionTxExecSummary executeTransaction(StandaloneBlockchain bc, BlockContext context, AionTransaction transaction) throws VmFatalException {
RepositoryCache cache = bc.getRepository().startTracking();
MiningBlock block = context.block;
AionTxExecSummary summary = BulkExecutor.executeTransactionWithNoPostExecutionWork(block.getDifficulty(), block.getNumber(), block.getTimestamp(), block.getNrgLimit(), block.getCoinbase(), transaction, cache, false, true, false, false, LOGGER_VM, BlockCachingContext.PENDING, block.getNumber() - 1, false, false);
cache.flushTo(bc.getRepository(), true);
return summary;
}
use of org.aion.base.db.RepositoryCache in project aion by aionnetwork.
the class AvmProviderTest method testBalanceTransferTransactionVersion2.
@Test
public void testBalanceTransferTransactionVersion2() throws Exception {
Assert.assertTrue(AvmProvider.tryAcquireLock(1, TimeUnit.MINUTES));
AvmProvider.enableAvmVersion(AvmVersion.VERSION_2, this.projectRootDir);
AvmProvider.startAvm(AvmVersion.VERSION_2);
// 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_2, repositoryChild, newEnergyRules());
Transaction transaction = newBalanceTransferTransaction(sender, recipient, BigInteger.TEN);
IAionVirtualMachine avm = AvmProvider.getAvm(AvmVersion.VERSION_2);
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_2);
AvmProvider.disableAvmVersion(AvmVersion.VERSION_2);
AvmProvider.releaseLock();
}
use of org.aion.base.db.RepositoryCache in project aion by aionnetwork.
the class AvmProviderTest method addBalance.
private void addBalance(RepositoryCache repository, AionAddress account, BigInteger amount) {
RepositoryCache cache = repository.startTracking();
cache.addBalance(account, amount);
cache.flushTo(repository, true);
}
use of org.aion.base.db.RepositoryCache in project aion by aionnetwork.
the class ContractIntegTest method testFvmConstructorIsCalledOnCodeDeployment.
@Test
public void testFvmConstructorIsCalledOnCodeDeployment() throws Exception {
String contractName = "MultiFeatureContract";
byte[] deployCode = ContractUtils.getContractDeployer("MultiFeatureContract.sol", "MultiFeatureContract");
long nrg = 1_000_000;
long nrgPrice = energyPrice;
BigInteger value = BigInteger.ONE;
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.DEFAULT) {
// Now call the contract and check that the constructor message was set.
String getMsgFunctionHash = "ce6d41de";
tx = AionTransaction.create(deployerKey, nonce.toByteArray(), contract, BigInteger.ZERO.toByteArray(), Hex.decode(getMsgFunctionHash), nrg, nrgPrice, txType, null);
assertFalse(tx.isContractCreationTransaction());
MiningBlock block = makeBlock(tx);
AionTxExecSummary summary = executeTransaction(tx, block, repo);
assertEquals("", summary.getReceipt().getError());
assertNotEquals(nrg, summary.getNrgUsed().longValue());
String expectedMsg = "Im alive!";
assertEquals(expectedMsg, new String(extractOutput(summary.getResult())));
} else if (txType == TransactionTypes.AVM_CREATE_CODE) {
assertNull(contract);
}
}
Aggregations