Search in sources :

Example 6 with AvmVersion

use of org.aion.avm.stub.AvmVersion 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 7 with AvmVersion

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

the class AvmTransactionExecutor method updateAvmsAndGetVersionToUse.

/**
 * Updates the state of the avm versions depending on the current block number.
 *
 * This method will ensure that any avm versions that are enabled but which are prohibited to
 * be enabled at this block number will be shutdown and disabled.
 *
 * It will also ensure that the avm version that is considered the canonical version at this
 * block number is enabled and that its avm is started.
 *
 * @implNote The projectRootPath is the path to the root directory of the aion project. This is
 * required so that we can find the resources to load.
 *
 * @param projectRootPath The path of the project root directory.
 * @param currentBlockNumber The current block number.
 * @param enableCoinbaseAddressLocking The flag to enable the coinbase addresslock
 * @return the version of the avm to use for the given block number.
 */
public static AvmVersion updateAvmsAndGetVersionToUse(String projectRootPath, long currentBlockNumber, boolean enableCoinbaseAddressLocking) throws IOException, IllegalAccessException, ClassNotFoundException, InstantiationException {
    AvmVersionSchedule schedule = AvmConfigurations.getAvmVersionSchedule();
    AvmVersion versionToUse = schedule.whichVersionToRunWith(currentBlockNumber);
    if (versionToUse == null) {
        throw new IllegalStateException("Attempted to invoke the avm at a block that has no avm support!");
    }
    if (versionToUse == AvmVersion.VERSION_1) {
        disableVersionIfEnabledAndProhibited(schedule, AvmVersion.VERSION_2, currentBlockNumber);
        ensureVersionIsEnabledAndStarted(AvmVersion.VERSION_1, projectRootPath);
    } else if (versionToUse == AvmVersion.VERSION_2) {
        disableVersionIfEnabledAndProhibited(schedule, AvmVersion.VERSION_1, currentBlockNumber);
        if (AvmConfigurations.isCoinbaseAddressLockingEnabled() != enableCoinbaseAddressLocking || // AKI-636 workaround
        currentBlockNumber == 4820587) {
            if (AvmProvider.isAvmRunning(versionToUse)) {
                AvmProvider.shutdownAvm(versionToUse);
            }
            boolean enable = (currentBlockNumber == 4820587) || enableCoinbaseAddressLocking;
            AionLoggerFactory.getLogger(LogEnum.VM.toString()).info("Set the CoinbaseAddressLocking to {}, block#{}", enable, currentBlockNumber);
            AvmConfigurations.setEnableCoinbaseAddressLocking(enable);
        }
        ensureVersionIsEnabledAndStarted(AvmVersion.VERSION_2, projectRootPath);
    } else {
        throw new IllegalStateException("Unknown avm version: " + versionToUse);
    }
    return versionToUse;
}
Also used : AvmVersionSchedule(org.aion.zero.impl.vm.avm.schedule.AvmVersionSchedule) AvmVersion(org.aion.avm.stub.AvmVersion)

Aggregations

AvmVersion (org.aion.avm.stub.AvmVersion)7 AionTxReceipt (org.aion.base.AionTxReceipt)5 AionAddress (org.aion.types.AionAddress)5 Test (org.junit.Test)5 ImportResult (org.aion.zero.impl.core.ImportResult)4 AionBlockSummary (org.aion.zero.impl.types.AionBlockSummary)4 AionTransaction (org.aion.base.AionTransaction)3 BigInteger (java.math.BigInteger)2 IAvmResourceFactory (org.aion.avm.stub.IAvmResourceFactory)2 InternalTransaction (org.aion.types.InternalTransaction)2 Log (org.aion.types.Log)2 MiningBlock (org.aion.zero.impl.types.MiningBlock)2 ArrayList (java.util.ArrayList)1 TimeoutException (java.util.concurrent.TimeoutException)1 IAvmFutureResult (org.aion.avm.stub.IAvmFutureResult)1 AionTxExecSummary (org.aion.base.AionTxExecSummary)1 RepositoryCache (org.aion.base.db.RepositoryCache)1 TransactionResult (org.aion.types.TransactionResult)1 AionRepositoryCache (org.aion.zero.impl.db.AionRepositoryCache)1 AvmVersionSchedule (org.aion.zero.impl.vm.avm.schedule.AvmVersionSchedule)1