use of org.aion.avm.stub.IAvmResourceFactory in project aion by aionnetwork.
the class ContractIntegTest method testDeployAvmContractToAnExistedAccount.
@Test
public void testDeployAvmContractToAnExistedAccount() {
AvmVersion version = AvmVersion.VERSION_1;
if (txType == TransactionTypes.DEFAULT) {
return;
}
long nrg = 1_000_000;
long nrgPrice = energyPrice;
BigInteger value = BigInteger.ONE;
AionAddress avmAddress = TxUtil.calculateContractAddress(deployer.toByteArray(), deployerNonce);
// create a tx the sender send some balance to the account the deployer will deploy in the
// feature.
AionTransaction tx = AionTransaction.create(senderKey, senderNonce.toByteArray(), avmAddress, value.toByteArray(), new byte[0], nrg, nrgPrice, TransactionTypes.DEFAULT, null);
assertFalse(tx.isContractCreationTransaction());
MiningBlock block = makeBlock(tx);
assertEquals(1, block.getTransactionsList().size());
Pair<ImportResult, AionBlockSummary> result = blockchain.tryToConnectAndFetchSummary(block);
assertTrue(result.getLeft().isSuccessful());
RepositoryCache repo = blockchain.getRepository().startTracking();
assertEquals(BigInteger.ONE, repo.getBalance(avmAddress));
BigInteger txCost = BigInteger.valueOf(nrgPrice).multiply(BigInteger.valueOf(result.getRight().getReceipts().get(0).getEnergyUsed()));
assertEquals(senderBalance.subtract(BigInteger.ONE).subtract(txCost), repo.getBalance(sender));
senderNonce = senderNonce.add(BigInteger.ONE);
AionAddress avmDeployedAddress = deployAvmContract(version, deployerNonce);
assertNotNull(avmAddress);
assertEquals(avmAddress, avmDeployedAddress);
repo = blockchain.getRepository().startTracking();
IAvmResourceFactory factory = (version == AvmVersion.VERSION_1) ? this.resourceProvider.factoryForVersion1 : this.resourceProvider.factoryForVersion2;
byte[] avmCode = factory.newContractFactory().getJarBytes(AvmContract.HELLO_WORLD);
assertArrayEquals(avmCode, repo.getCode(avmAddress));
assertEquals(BigInteger.ONE, repo.getBalance(avmAddress));
byte[] call = getCallArguments(version);
tx = AionTransaction.create(deployerKey, deployerNonce.add(BigInteger.ONE).toByteArray(), avmAddress, BigInteger.ZERO.toByteArray(), call, 2_000_000, nrgPrice, TransactionTypes.DEFAULT, null);
block = this.blockchain.createNewMiningBlock(this.blockchain.getBestBlock(), Collections.singletonList(tx), false);
Pair<ImportResult, AionBlockSummary> connectResult = this.blockchain.tryToConnectAndFetchSummary(block);
AionTxReceipt receipt = connectResult.getRight().getReceipts().get(0);
// Check the block was imported and the transaction was successful.
assertThat(connectResult.getLeft()).isEqualTo(ImportResult.IMPORTED_BEST);
assertThat(receipt.isSuccessful()).isTrue();
}
use of org.aion.avm.stub.IAvmResourceFactory in project aion by aionnetwork.
the class AvmResourcesVersion1 method loadAvmResourceFactory.
/**
* Uses the provided classloader to load a new instance of {@link IAvmResourceFactory} defined
* in the avm version 1 module.
*/
private static IAvmResourceFactory loadAvmResourceFactory(URLClassLoader classLoader) throws IllegalAccessException, InstantiationException, ClassNotFoundException, IOException {
Class<?> factory = classLoader.loadClass(AvmDependencyInfo.avmResourceFactoryClassNameVersion1);
IAvmResourceFactory resourceFactory = (IAvmResourceFactory) factory.newInstance();
// Verify that the resources were loaded by the correct classloader.
ClassLoader resourceClassloader = resourceFactory.verifyAndReturnClassloader();
if (resourceClassloader != classLoader) {
classLoader.close();
throw new IllegalStateException("The avm resources were loaded using the wrong classloader: " + resourceClassloader);
}
return resourceFactory;
}
use of org.aion.avm.stub.IAvmResourceFactory in project aion by aionnetwork.
the class AvmResourcesVersion1 method loadResources.
/**
* Loads the resources associated with version 1 of the avm and returns a new instance of this
* resource-holder class.
*/
public static AvmResourcesVersion1 loadResources(String projectRootDir) throws IllegalAccessException, ClassNotFoundException, InstantiationException, IOException {
URLClassLoader classLoader = newClassLoaderForAvmVersion1(projectRootDir);
IAvmResourceFactory resourceFactory = loadAvmResourceFactory(classLoader);
return new AvmResourcesVersion1(classLoader, resourceFactory);
}
use of org.aion.avm.stub.IAvmResourceFactory in project aion by aionnetwork.
the class AvmResourcesVersion2 method loadResources.
/**
* Loads the resources associated with version 2 of the avm and returns a new instance of this
* resource-holder class.
*/
public static AvmResourcesVersion2 loadResources(String projectRootDir) throws IllegalAccessException, ClassNotFoundException, InstantiationException, IOException {
URLClassLoader classLoader = newClassLoaderForAvmVersion2(projectRootDir);
IAvmResourceFactory resourceFactory = loadAvmResourceFactory(classLoader);
return new AvmResourcesVersion2(classLoader, resourceFactory);
}
use of org.aion.avm.stub.IAvmResourceFactory in project aion by aionnetwork.
the class PendingStateTest method testAddPendingTransaction_AVMContractCall_Success.
@Test
public void testAddPendingTransaction_AVMContractCall_Success() throws Exception {
TestResourceProvider resourceProvider = TestResourceProvider.initializeAndCreateNewProvider(AvmPathManager.getPathOfProjectRootDirectory());
IAvmResourceFactory resourceFactory = resourceProvider.factoryForVersion1;
// Successful transaction
byte[] jar = resourceFactory.newContractFactory().getDeploymentBytes(AvmContract.HELLO_WORLD);
AionTransaction createTransaction = AionTransaction.create(deployerKey, BigInteger.ZERO.toByteArray(), null, BigInteger.ZERO.toByteArray(), jar, 5_000_000, energyPrice, TransactionTypes.AVM_CREATE_CODE, null);
assertEquals(pendingState.addTransactionFromApiServer(createTransaction), TxResponse.SUCCESS);
MiningBlock block = blockchain.createNewMiningBlock(blockchain.getBestBlock(), pendingState.getPendingTransactions(), false);
Pair<ImportResult, AionBlockSummary> connectResult = blockchain.tryToConnectAndFetchSummary(block);
AionTxReceipt receipt = connectResult.getRight().getReceipts().get(0);
// Check the block was imported, the contract has the Avm prefix, and deployment succeeded.
assertThat(connectResult.getLeft()).isEqualTo(ImportResult.IMPORTED_BEST);
// verify that the output is indeed the contract address
AionAddress contractAddress = TxUtil.calculateContractAddress(createTransaction);
assertThat(contractAddress.toByteArray()).isEqualTo(receipt.getTransactionOutput());
AionAddress contract = new AionAddress(receipt.getTransactionOutput());
byte[] call = resourceFactory.newStreamingEncoder().encodeOneString("sayHello").getEncoding();
AionTransaction callTransaction = AionTransaction.create(deployerKey, BigInteger.ONE.toByteArray(), contract, BigInteger.ZERO.toByteArray(), call, 2_000_000, energyPrice, TransactionTypes.DEFAULT, null);
assertEquals(pendingState.addTransactionFromApiServer(callTransaction), TxResponse.SUCCESS);
}
Aggregations