Search in sources :

Example 1 with IAvmResourceFactory

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();
}
Also used : AionAddress(org.aion.types.AionAddress) ImportResult(org.aion.zero.impl.core.ImportResult) AionTransaction(org.aion.base.AionTransaction) MiningBlock(org.aion.zero.impl.types.MiningBlock) AionBlockSummary(org.aion.zero.impl.types.AionBlockSummary) IAvmResourceFactory(org.aion.avm.stub.IAvmResourceFactory) BigInteger(java.math.BigInteger) AionRepositoryCache(org.aion.zero.impl.db.AionRepositoryCache) RepositoryCache(org.aion.base.db.RepositoryCache) AionTxReceipt(org.aion.base.AionTxReceipt) AvmVersion(org.aion.avm.stub.AvmVersion) Test(org.junit.Test)

Example 2 with IAvmResourceFactory

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;
}
Also used : IAvmResourceFactory(org.aion.avm.stub.IAvmResourceFactory) URLClassLoader(java.net.URLClassLoader)

Example 3 with IAvmResourceFactory

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);
}
Also used : IAvmResourceFactory(org.aion.avm.stub.IAvmResourceFactory) URLClassLoader(java.net.URLClassLoader)

Example 4 with IAvmResourceFactory

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);
}
Also used : IAvmResourceFactory(org.aion.avm.stub.IAvmResourceFactory) URLClassLoader(java.net.URLClassLoader)

Example 5 with IAvmResourceFactory

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);
}
Also used : IAvmResourceFactory(org.aion.avm.stub.IAvmResourceFactory) ImportResult(org.aion.zero.impl.core.ImportResult) AionBlockSummary(org.aion.zero.impl.types.AionBlockSummary) AionAddress(org.aion.types.AionAddress) AionTransaction(org.aion.base.AionTransaction) AionTxReceipt(org.aion.base.AionTxReceipt) MiningBlock(org.aion.zero.impl.types.MiningBlock) TestResourceProvider(org.aion.zero.impl.vm.TestResourceProvider) Test(org.junit.Test)

Aggregations

IAvmResourceFactory (org.aion.avm.stub.IAvmResourceFactory)16 AionTransaction (org.aion.base.AionTransaction)9 URLClassLoader (java.net.URLClassLoader)6 AionTxReceipt (org.aion.base.AionTxReceipt)5 AionBlockSummary (org.aion.zero.impl.types.AionBlockSummary)5 MiningBlock (org.aion.zero.impl.types.MiningBlock)5 Test (org.junit.Test)5 AionAddress (org.aion.types.AionAddress)4 ImportResult (org.aion.zero.impl.core.ImportResult)4 BigInteger (java.math.BigInteger)3 TestResourceProvider (org.aion.zero.impl.vm.TestResourceProvider)3 AvmVersion (org.aion.avm.stub.AvmVersion)2 ArrayList (java.util.ArrayList)1 RepositoryCache (org.aion.base.db.RepositoryCache)1 ECKey (org.aion.crypto.ECKey)1 AionRepositoryCache (org.aion.zero.impl.db.AionRepositoryCache)1 ContractInformation (org.aion.zero.impl.db.ContractInformation)1 MiningBlockHeader (org.aion.zero.impl.types.MiningBlockHeader)1 VmFatalException (org.aion.zero.impl.vm.common.VmFatalException)1