Search in sources :

Example 11 with IAvmResourceFactory

use of org.aion.avm.stub.IAvmResourceFactory in project aion by aionnetwork.

the class AvmLogAndInternalTransactionTest method callFireLogs.

public Pair<ImportResult, AionBlockSummary> callFireLogs(AvmVersion version, BigInteger nonce, AionAddress address, AionAddress addressToCall, String methodName) {
    IAvmResourceFactory factory = (version == AvmVersion.VERSION_1) ? resourceProvider.factoryForVersion1 : resourceProvider.factoryForVersion2;
    byte[] data = factory.newStreamingEncoder().encodeOneString(methodName).encodeOneAddress(addressToCall).getEncoding();
    AionTransaction transaction = AionTransaction.create(deployerKey, nonce.toByteArray(), address, new byte[0], data, 2_000_000, minEnergyPrice, TransactionTypes.DEFAULT, null);
    MiningBlock block = this.blockchain.createBlock(this.blockchain.getBestBlock(), Collections.singletonList(transaction), false, this.blockchain.getBestBlock().getTimestamp());
    return this.blockchain.tryToConnectAndFetchSummary(block);
}
Also used : IAvmResourceFactory(org.aion.avm.stub.IAvmResourceFactory) AionTransaction(org.aion.base.AionTransaction) MiningBlock(org.aion.zero.impl.types.MiningBlock)

Example 12 with IAvmResourceFactory

use of org.aion.avm.stub.IAvmResourceFactory in project aion by aionnetwork.

the class AvmBulkTransactionTest method getDeployedStatefulnessCountValue.

private int getDeployedStatefulnessCountValue(AvmVersion version, ECKey sender, BigInteger nonce, AionAddress contract) {
    AionTransaction transaction = AionTransaction.create(sender, nonce.toByteArray(), contract, new byte[0], abiEncodeMethodCall(AvmVersion.VERSION_1, "getCount"), 2_000_000, this.energyPrice, TransactionTypes.DEFAULT, null);
    AionBlockSummary summary = sendTransactionsInBulkInSingleBlock(Collections.singletonList(transaction));
    IAvmResourceFactory factory = (version == AvmVersion.VERSION_1) ? resourceProvider.factoryForVersion1 : resourceProvider.factoryForVersion2;
    return factory.newDecoder(summary.getReceipts().get(0).getTransactionOutput()).decodeOneInteger();
}
Also used : AionBlockSummary(org.aion.zero.impl.types.AionBlockSummary) IAvmResourceFactory(org.aion.avm.stub.IAvmResourceFactory) AionTransaction(org.aion.base.AionTransaction)

Example 13 with IAvmResourceFactory

use of org.aion.avm.stub.IAvmResourceFactory in project aion by aionnetwork.

the class AvmInternalTxTest method testDeployAndCallContract.

@Test
public void testDeployAndCallContract() {
    AvmVersion version = AvmVersion.VERSION_1;
    TransactionTypeRule.allowAVMContractTransaction();
    // Deploy the contract.
    byte[] jar = getJarBytes(version);
    AionTransaction transaction = AionTransaction.create(deployerKey, new byte[0], null, new byte[0], jar, 5_000_000, minEnergyPrice, TransactionTypes.AVM_CREATE_CODE, null);
    MiningBlock block = this.blockchain.createNewMiningBlock(this.blockchain.getBestBlock(), Collections.singletonList(transaction), false);
    Pair<ImportResult, AionBlockSummary> connectResult = this.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);
    assertThat(receipt.getTransactionOutput()[0]).isEqualTo(AddressSpecs.A0_IDENTIFIER);
    assertThat(receipt.isSuccessful()).isTrue();
    AionAddress contract = new AionAddress(receipt.getTransactionOutput());
    // verify that the output is indeed the contract address
    assertThat(TxUtil.calculateContractAddress(transaction)).isEqualTo(contract);
    IAvmResourceFactory factory = (version == AvmVersion.VERSION_1) ? resourceProvider.factoryForVersion1 : resourceProvider.factoryForVersion2;
    byte[] call = factory.newStreamingEncoder().encodeOneString("recursivelyGetValue").getEncoding();
    makeCall(BigInteger.ONE, contract, call);
}
Also used : ImportResult(org.aion.zero.impl.core.ImportResult) AionBlockSummary(org.aion.zero.impl.types.AionBlockSummary) AionAddress(org.aion.types.AionAddress) IAvmResourceFactory(org.aion.avm.stub.IAvmResourceFactory) AionTransaction(org.aion.base.AionTransaction) AionTxReceipt(org.aion.base.AionTxReceipt) MiningBlock(org.aion.zero.impl.types.MiningBlock) AvmVersion(org.aion.avm.stub.AvmVersion) Test(org.junit.Test)

Example 14 with IAvmResourceFactory

use of org.aion.avm.stub.IAvmResourceFactory in project aion by aionnetwork.

the class TestResourceProvider method initializeAndCreateNewProvider.

/**
 * Initializes the test resources for the two avm versions and creates a new provider with
 * those loaded resources.
 *
 * @return a new test provider.
 */
public static TestResourceProvider initializeAndCreateNewProvider(String projectRootDirectory) throws IOException, ClassNotFoundException, InstantiationException, IllegalAccessException {
    URLClassLoader version1 = newClassLoaderForAvmVersion(projectRootDirectory, AvmVersion.VERSION_1);
    URLClassLoader version2 = newClassLoaderForAvmVersion(projectRootDirectory, AvmVersion.VERSION_2);
    IAvmResourceFactory factory1 = loadAvmResourceFactory(version1, AvmVersion.VERSION_1);
    IAvmResourceFactory factory2 = loadAvmResourceFactory(version2, AvmVersion.VERSION_2);
    return new TestResourceProvider(version1, factory1, version2, factory2);
}
Also used : IAvmResourceFactory(org.aion.avm.stub.IAvmResourceFactory) URLClassLoader(java.net.URLClassLoader)

Example 15 with IAvmResourceFactory

use of org.aion.avm.stub.IAvmResourceFactory in project aion by aionnetwork.

the class TestResourceProvider 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, AvmVersion version) throws IllegalAccessException, InstantiationException, ClassNotFoundException, IOException {
    Class<?> factory = classLoader.loadClass(version == AvmVersion.VERSION_1 ? AvmDependencyInfo.avmResourceFactoryClassNameVersion1 : AvmDependencyInfo.avmResourceFactoryClassNameVersion2);
    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)

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