Search in sources :

Example 56 with ImportResult

use of org.aion.zero.impl.core.ImportResult in project aion by aionnetwork.

the class TransactionCreateSpecificationTests method deployInternalAvmContractOnTopOfAddressWithStorageUsingAvmVersion1_DeployAndRequireSuccess.

@Test
public void deployInternalAvmContractOnTopOfAddressWithStorageUsingAvmVersion1_DeployAndRequireSuccess() throws VmFatalException {
    AvmTestConfig.clearConfigurations();
    AvmTestConfig.supportOnlyAvmVersion1();
    // Deploy AVM contract.
    AionTransaction deployTxAvm = BlockchainTestUtils.deployAvmContractTransaction(AvmContract.DEPLOY_INTERNAL, resourceProvider.factoryForVersion1, SENDER_KEY, BigInteger.ZERO);
    AionAddress contract = TxUtil.calculateContractAddress(deployTxAvm);
    Pair<Block, ImportResult> resultImport = BlockchainTestUtils.addMiningBlock(blockchain, blockchain.getBestBlock(), List.of(deployTxAvm));
    assertThat(resultImport.getRight()).isEqualTo(ImportResult.IMPORTED_BEST);
    // Call AVM contract to deploy new internal AVM contract (version with required success).
    long internalLimit = 1_000_000;
    AionTransaction deployInternal = BlockchainTestUtils.callSimpleAvmContractTransaction(resourceProvider.factoryForVersion1, SENDER_KEY, BigInteger.ONE, contract, "deployAndRequireSuccess", deployTxAvm.getData(), internalLimit);
    AionAddress internalContract = new AionAddress(Hex.decode("a0268090998a99666b72cc452b9307438a34341047d9e0d7b92c9207bf413655"));
    assertThat(blockchain.getRepository().hasAccountState(internalContract)).isFalse();
    // Manipulate the repository to have a non-default storage value.
    RepositoryCache cache = blockchain.getRepository().startTracking();
    cache.addStorageRow(internalContract, ByteArrayWrapper.wrap(RandomUtils.nextBytes(16)), ByteArrayWrapper.wrap(RandomUtils.nextBytes(16)));
    cache.saveVmType(internalContract, InternalVmType.AVM);
    cache.flushTo(cache.getParent(), true);
    // Check assumptions about contract state.
    AccountState contractState = (AccountState) blockchain.getRepository().startTracking().getAccountState(internalContract);
    assertThat(contractState.getBalance()).isEqualTo(BigInteger.ZERO);
    assertThat(contractState.getNonce()).isEqualTo(BigInteger.ZERO);
    assertThat(contractState.getStateRoot()).isNotEqualTo(EMPTY_TRIE_HASH);
    assertThat(contractState.getCodeHash()).isEqualTo(EMPTY_DATA_HASH);
    byte[] oldRoot = contractState.getStateRoot();
    // Next, process the deploy transaction with fork040 enabled.
    AionTxExecSummary result = executeTransaction(deployInternal, true);
    assertThat(result.isFailed()).isFalse();
    assertThat(result.isRejected()).isFalse();
    assertThat(result.getReceipt().getError()).isEmpty();
    assertThat(result.getNrgUsed()).isLessThan(BigInteger.valueOf(deployInternal.getEnergyLimit()));
    assertThat(result.getLogs()).isEmpty();
    InternalTransaction itx = result.getInternalTransactions().get(0);
    assertThat(itx.isCreate).isTrue();
    assertThat(TxUtil.calculateContractAddress(itx)).isEqualTo(internalContract);
    assertThat(itx.isRejected).isFalse();
    assertThat(itx.energyLimit).isEqualTo(internalLimit);
    assertThat(result.getNrgUsed()).isLessThan(BigInteger.valueOf(itx.energyLimit));
    contractState = (AccountState) blockchain.getRepository().startTracking().getAccountState(internalContract);
    assertThat(contractState.getBalance()).isEqualTo(BigInteger.ZERO);
    assertThat(contractState.getNonce()).isEqualTo(BigInteger.ZERO);
    assertThat(contractState.getStateRoot()).isNotEqualTo(EMPTY_TRIE_HASH);
    assertThat(contractState.getStateRoot()).isNotEqualTo(oldRoot);
    assertThat(contractState.getCodeHash()).isNotEqualTo(EMPTY_DATA_HASH);
}
Also used : AionAddress(org.aion.types.AionAddress) ImportResult(org.aion.zero.impl.core.ImportResult) AionTxExecSummary(org.aion.base.AionTxExecSummary) AionBlockchainImpl.getPostExecutionWorkForGeneratePreBlock(org.aion.zero.impl.blockchain.AionBlockchainImpl.getPostExecutionWorkForGeneratePreBlock) Block(org.aion.zero.impl.types.Block) RepositoryCache(org.aion.base.db.RepositoryCache) AionTransaction(org.aion.base.AionTransaction) AccountState(org.aion.base.AccountState) InternalTransaction(org.aion.types.InternalTransaction) Test(org.junit.Test)

Example 57 with ImportResult

use of org.aion.zero.impl.core.ImportResult in project aion by aionnetwork.

the class TransactionCreateSpecificationTests method deployInternalAvmContractOnTopOfAddressWithNonceUsingAvmVersion2_DeployAndRequireSuccess.

@Test
public void deployInternalAvmContractOnTopOfAddressWithNonceUsingAvmVersion2_DeployAndRequireSuccess() throws VmFatalException {
    // Deploy AVM contract.
    AionTransaction deployTxAvm = BlockchainTestUtils.deployAvmContractTransaction(AvmContract.DEPLOY_INTERNAL, resourceProvider.factoryForVersion2, SENDER_KEY, BigInteger.ZERO);
    AionAddress contract = TxUtil.calculateContractAddress(deployTxAvm);
    Pair<Block, ImportResult> resultImport = BlockchainTestUtils.addMiningBlock(blockchain, blockchain.getBestBlock(), List.of(deployTxAvm));
    assertThat(resultImport.getRight()).isEqualTo(ImportResult.IMPORTED_BEST);
    // Call AVM contract to deploy new internal AVM contract (version with required success).
    long internalLimit = 1_000_000;
    AionTransaction deployInternal = BlockchainTestUtils.callSimpleAvmContractTransaction(resourceProvider.factoryForVersion2, SENDER_KEY, BigInteger.ONE, contract, "deployAndRequireSuccess", deployTxAvm.getData(), internalLimit);
    AionAddress internalContract = new AionAddress(Hex.decode("a0268090998a99666b72cc452b9307438a34341047d9e0d7b92c9207bf413655"));
    assertThat(blockchain.getRepository().hasAccountState(internalContract)).isFalse();
    // Manipulate the repository to have a non-default nonce value.
    RepositoryCache cache = blockchain.getRepository().startTracking();
    cache.createAccount(internalContract);
    cache.setNonce(internalContract, BigInteger.TEN);
    cache.flushTo(cache.getParent(), true);
    // Check assumptions about contract state.
    AccountState contractState = (AccountState) blockchain.getRepository().startTracking().getAccountState(internalContract);
    assertThat(contractState.getBalance()).isEqualTo(BigInteger.ZERO);
    assertThat(contractState.getNonce()).isEqualTo(BigInteger.TEN);
    assertThat(contractState.getStateRoot()).isEqualTo(EMPTY_TRIE_HASH);
    assertThat(contractState.getCodeHash()).isEqualTo(EMPTY_DATA_HASH);
    // Next, process the deploy transaction with fork040 enabled.
    AionTxExecSummary result = executeTransaction(deployInternal, true);
    assertThat(result.isFailed()).isTrue();
    assertThat(result.getReceipt().getError()).isEqualTo("reverted");
    assertThat(result.getNrgUsed()).isLessThan(BigInteger.valueOf(deployInternal.getEnergyLimit()));
    assertThat(result.getLogs()).isEmpty();
    InternalTransaction itx = result.getInternalTransactions().get(0);
    assertThat(itx.isCreate).isTrue();
    assertThat(TxUtil.calculateContractAddress(itx)).isEqualTo(internalContract);
    assertThat(itx.isRejected).isTrue();
    assertThat(itx.energyLimit).isEqualTo(internalLimit);
    assertThat(result.getNrgUsed()).isGreaterThan(BigInteger.valueOf(itx.energyLimit));
    contractState = (AccountState) blockchain.getRepository().startTracking().getAccountState(internalContract);
    assertThat(contractState.getBalance()).isEqualTo(BigInteger.ZERO);
    assertThat(contractState.getNonce()).isEqualTo(BigInteger.TEN);
    assertThat(contractState.getStateRoot()).isEqualTo(EMPTY_TRIE_HASH);
    assertThat(contractState.getCodeHash()).isEqualTo(EMPTY_DATA_HASH);
}
Also used : AionAddress(org.aion.types.AionAddress) ImportResult(org.aion.zero.impl.core.ImportResult) AionTxExecSummary(org.aion.base.AionTxExecSummary) AionBlockchainImpl.getPostExecutionWorkForGeneratePreBlock(org.aion.zero.impl.blockchain.AionBlockchainImpl.getPostExecutionWorkForGeneratePreBlock) Block(org.aion.zero.impl.types.Block) RepositoryCache(org.aion.base.db.RepositoryCache) AionTransaction(org.aion.base.AionTransaction) AccountState(org.aion.base.AccountState) InternalTransaction(org.aion.types.InternalTransaction) Test(org.junit.Test)

Example 58 with ImportResult

use of org.aion.zero.impl.core.ImportResult in project aion by aionnetwork.

the class TransactionCreateSpecificationTests method deployInternalAvmContractOnTopOfAddressWithStorageUsingAvmVersion2.

@Test
public void deployInternalAvmContractOnTopOfAddressWithStorageUsingAvmVersion2() throws VmFatalException {
    // Deploy AVM contract.
    AionTransaction deployTxAvm = BlockchainTestUtils.deployAvmContractTransaction(AvmContract.DEPLOY_INTERNAL, resourceProvider.factoryForVersion2, SENDER_KEY, BigInteger.ZERO);
    AionAddress contract = TxUtil.calculateContractAddress(deployTxAvm);
    Pair<Block, ImportResult> resultImport = BlockchainTestUtils.addMiningBlock(blockchain, blockchain.getBestBlock(), List.of(deployTxAvm));
    assertThat(resultImport.getRight()).isEqualTo(ImportResult.IMPORTED_BEST);
    // Call AVM contract to deploy new internal AVM contract (version without required success).
    long internalLimit = 1_000_000;
    AionTransaction deployInternal = BlockchainTestUtils.callSimpleAvmContractTransaction(resourceProvider.factoryForVersion2, SENDER_KEY, BigInteger.ONE, contract, "deploy", deployTxAvm.getData(), internalLimit);
    AionAddress internalContract = new AionAddress(Hex.decode("a0268090998a99666b72cc452b9307438a34341047d9e0d7b92c9207bf413655"));
    assertThat(blockchain.getRepository().hasAccountState(internalContract)).isFalse();
    // Manipulate the repository to have a non-default storage value.
    RepositoryCache cache = blockchain.getRepository().startTracking();
    cache.addStorageRow(internalContract, ByteArrayWrapper.wrap(RandomUtils.nextBytes(16)), ByteArrayWrapper.wrap(RandomUtils.nextBytes(16)));
    cache.saveVmType(internalContract, InternalVmType.AVM);
    cache.flushTo(cache.getParent(), true);
    // Check assumptions about contract state.
    AccountState contractState = (AccountState) blockchain.getRepository().startTracking().getAccountState(internalContract);
    assertThat(contractState.getBalance()).isEqualTo(BigInteger.ZERO);
    assertThat(contractState.getNonce()).isEqualTo(BigInteger.ZERO);
    assertThat(contractState.getStateRoot()).isNotEqualTo(EMPTY_TRIE_HASH);
    assertThat(contractState.getCodeHash()).isEqualTo(EMPTY_DATA_HASH);
    byte[] oldRoot = contractState.getStateRoot();
    // Next, process the deploy transaction with fork040 enabled.
    AionTxExecSummary result = executeTransaction(deployInternal, true);
    assertThat(result.isFailed()).isFalse();
    assertThat(result.isRejected()).isFalse();
    assertThat(result.getReceipt().getError()).isEmpty();
    assertThat(result.getNrgUsed()).isLessThan(BigInteger.valueOf(deployInternal.getEnergyLimit()));
    assertThat(result.getLogs()).isEmpty();
    InternalTransaction itx = result.getInternalTransactions().get(0);
    assertThat(itx.isCreate).isTrue();
    assertThat(TxUtil.calculateContractAddress(itx)).isEqualTo(internalContract);
    assertThat(itx.isRejected).isFalse();
    assertThat(itx.energyLimit).isEqualTo(internalLimit);
    assertThat(result.getNrgUsed()).isGreaterThan(BigInteger.valueOf(itx.energyLimit));
    contractState = (AccountState) blockchain.getRepository().startTracking().getAccountState(internalContract);
    assertThat(contractState.getBalance()).isEqualTo(BigInteger.ZERO);
    assertThat(contractState.getNonce()).isEqualTo(BigInteger.ZERO);
    assertThat(contractState.getStateRoot()).isEqualTo(oldRoot);
    assertThat(contractState.getCodeHash()).isEqualTo(EMPTY_DATA_HASH);
}
Also used : AionAddress(org.aion.types.AionAddress) ImportResult(org.aion.zero.impl.core.ImportResult) AionTxExecSummary(org.aion.base.AionTxExecSummary) AionBlockchainImpl.getPostExecutionWorkForGeneratePreBlock(org.aion.zero.impl.blockchain.AionBlockchainImpl.getPostExecutionWorkForGeneratePreBlock) Block(org.aion.zero.impl.types.Block) RepositoryCache(org.aion.base.db.RepositoryCache) AionTransaction(org.aion.base.AionTransaction) AccountState(org.aion.base.AccountState) InternalTransaction(org.aion.types.InternalTransaction) Test(org.junit.Test)

Example 59 with ImportResult

use of org.aion.zero.impl.core.ImportResult in project aion by aionnetwork.

the class TransactionCreateSpecificationTests method deployInternalAvmContractOnTopOfAddressWithBalanceUsingAvmVersion2.

@Test
public void deployInternalAvmContractOnTopOfAddressWithBalanceUsingAvmVersion2() throws VmFatalException {
    // Deploy AVM contract.
    AionTransaction deployTxAvm = BlockchainTestUtils.deployAvmContractTransaction(AvmContract.DEPLOY_INTERNAL, resourceProvider.factoryForVersion2, SENDER_KEY, BigInteger.ZERO);
    AionAddress contract = TxUtil.calculateContractAddress(deployTxAvm);
    Pair<Block, ImportResult> resultImport = BlockchainTestUtils.addMiningBlock(blockchain, blockchain.getBestBlock(), List.of(deployTxAvm));
    assertThat(resultImport.getRight()).isEqualTo(ImportResult.IMPORTED_BEST);
    // Call AVM contract to deploy new internal AVM contract (version without required success).
    long internalLimit = 1_000_000;
    AionTransaction deployInternal = BlockchainTestUtils.callSimpleAvmContractTransaction(resourceProvider.factoryForVersion2, SENDER_KEY, BigInteger.ONE, contract, "deploy", deployTxAvm.getData(), internalLimit);
    AionAddress internalContract = new AionAddress(Hex.decode("a0268090998a99666b72cc452b9307438a34341047d9e0d7b92c9207bf413655"));
    assertThat(blockchain.getRepository().hasAccountState(internalContract)).isFalse();
    // Manipulate the repository to have a non-default balance value.
    RepositoryCache cache = blockchain.getRepository().startTracking();
    cache.createAccount(internalContract);
    cache.addBalance(internalContract, BigInteger.TEN);
    cache.flushTo(cache.getParent(), true);
    // Check assumptions about contract state.
    AccountState contractState = (AccountState) blockchain.getRepository().startTracking().getAccountState(internalContract);
    assertThat(contractState.getBalance()).isEqualTo(BigInteger.TEN);
    assertThat(contractState.getNonce()).isEqualTo(BigInteger.ZERO);
    assertThat(contractState.getStateRoot()).isEqualTo(EMPTY_TRIE_HASH);
    assertThat(contractState.getCodeHash()).isEqualTo(EMPTY_DATA_HASH);
    // Next, process the deploy transaction with fork040 enabled.
    AionTxExecSummary result = executeTransaction(deployInternal, true);
    assertThat(result.isFailed()).isFalse();
    assertThat(result.isRejected()).isFalse();
    assertThat(result.getReceipt().getError()).isEmpty();
    assertThat(result.getNrgUsed()).isLessThan(BigInteger.valueOf(deployInternal.getEnergyLimit()));
    assertThat(result.getLogs()).isEmpty();
    InternalTransaction itx = result.getInternalTransactions().get(0);
    assertThat(itx.isCreate).isTrue();
    assertThat(TxUtil.calculateContractAddress(itx)).isEqualTo(internalContract);
    assertThat(itx.isRejected).isFalse();
    assertThat(itx.energyLimit).isEqualTo(internalLimit);
    assertThat(result.getNrgUsed()).isLessThan(BigInteger.valueOf(itx.energyLimit));
    contractState = (AccountState) blockchain.getRepository().startTracking().getAccountState(internalContract);
    assertThat(contractState.getBalance()).isEqualTo(BigInteger.TEN);
    assertThat(contractState.getNonce()).isEqualTo(BigInteger.ZERO);
    assertThat(contractState.getStateRoot()).isNotEqualTo(EMPTY_TRIE_HASH);
    assertThat(contractState.getCodeHash()).isNotEqualTo(EMPTY_DATA_HASH);
}
Also used : AionAddress(org.aion.types.AionAddress) ImportResult(org.aion.zero.impl.core.ImportResult) AionTxExecSummary(org.aion.base.AionTxExecSummary) AionBlockchainImpl.getPostExecutionWorkForGeneratePreBlock(org.aion.zero.impl.blockchain.AionBlockchainImpl.getPostExecutionWorkForGeneratePreBlock) Block(org.aion.zero.impl.types.Block) RepositoryCache(org.aion.base.db.RepositoryCache) AionTransaction(org.aion.base.AionTransaction) AccountState(org.aion.base.AccountState) InternalTransaction(org.aion.types.InternalTransaction) Test(org.junit.Test)

Example 60 with ImportResult

use of org.aion.zero.impl.core.ImportResult in project aion by aionnetwork.

the class TransactionCreateSpecificationTests method deployInternalAvmContractOnTopOfAddressWithBalanceUsingAvmVersion1_DeployAndRequireSuccess.

@Test
public void deployInternalAvmContractOnTopOfAddressWithBalanceUsingAvmVersion1_DeployAndRequireSuccess() throws VmFatalException {
    AvmTestConfig.clearConfigurations();
    AvmTestConfig.supportOnlyAvmVersion1();
    // Deploy AVM contract.
    AionTransaction deployTxAvm = BlockchainTestUtils.deployAvmContractTransaction(AvmContract.DEPLOY_INTERNAL, resourceProvider.factoryForVersion1, SENDER_KEY, BigInteger.ZERO);
    AionAddress contract = TxUtil.calculateContractAddress(deployTxAvm);
    Pair<Block, ImportResult> resultImport = BlockchainTestUtils.addMiningBlock(blockchain, blockchain.getBestBlock(), List.of(deployTxAvm));
    assertThat(resultImport.getRight()).isEqualTo(ImportResult.IMPORTED_BEST);
    // Call AVM contract to deploy new internal AVM contract (version with required success).
    long internalLimit = 1_000_000;
    AionTransaction deployInternal = BlockchainTestUtils.callSimpleAvmContractTransaction(resourceProvider.factoryForVersion1, SENDER_KEY, BigInteger.ONE, contract, "deployAndRequireSuccess", deployTxAvm.getData(), internalLimit);
    AionAddress internalContract = new AionAddress(Hex.decode("a0268090998a99666b72cc452b9307438a34341047d9e0d7b92c9207bf413655"));
    assertThat(blockchain.getRepository().hasAccountState(internalContract)).isFalse();
    // Manipulate the repository to have a non-default balance value.
    RepositoryCache cache = blockchain.getRepository().startTracking();
    cache.createAccount(internalContract);
    cache.addBalance(internalContract, BigInteger.TEN);
    cache.flushTo(cache.getParent(), true);
    // Check assumptions about contract state.
    AccountState contractState = (AccountState) blockchain.getRepository().startTracking().getAccountState(internalContract);
    assertThat(contractState.getBalance()).isEqualTo(BigInteger.TEN);
    assertThat(contractState.getNonce()).isEqualTo(BigInteger.ZERO);
    assertThat(contractState.getStateRoot()).isEqualTo(EMPTY_TRIE_HASH);
    assertThat(contractState.getCodeHash()).isEqualTo(EMPTY_DATA_HASH);
    // Next, process the deploy transaction with fork040 enabled.
    AionTxExecSummary result = executeTransaction(deployInternal, true);
    assertThat(result.isFailed()).isFalse();
    assertThat(result.isRejected()).isFalse();
    assertThat(result.getReceipt().getError()).isEmpty();
    assertThat(result.getNrgUsed()).isLessThan(BigInteger.valueOf(deployInternal.getEnergyLimit()));
    assertThat(result.getLogs()).isEmpty();
    InternalTransaction itx = result.getInternalTransactions().get(0);
    assertThat(itx.isCreate).isTrue();
    assertThat(TxUtil.calculateContractAddress(itx)).isEqualTo(internalContract);
    assertThat(itx.isRejected).isFalse();
    assertThat(itx.energyLimit).isEqualTo(internalLimit);
    assertThat(result.getNrgUsed()).isLessThan(BigInteger.valueOf(itx.energyLimit));
    contractState = (AccountState) blockchain.getRepository().startTracking().getAccountState(internalContract);
    assertThat(contractState.getBalance()).isEqualTo(BigInteger.TEN);
    assertThat(contractState.getNonce()).isEqualTo(BigInteger.ZERO);
    assertThat(contractState.getStateRoot()).isNotEqualTo(EMPTY_TRIE_HASH);
    assertThat(contractState.getCodeHash()).isNotEqualTo(EMPTY_DATA_HASH);
}
Also used : AionAddress(org.aion.types.AionAddress) ImportResult(org.aion.zero.impl.core.ImportResult) AionTxExecSummary(org.aion.base.AionTxExecSummary) AionBlockchainImpl.getPostExecutionWorkForGeneratePreBlock(org.aion.zero.impl.blockchain.AionBlockchainImpl.getPostExecutionWorkForGeneratePreBlock) Block(org.aion.zero.impl.types.Block) RepositoryCache(org.aion.base.db.RepositoryCache) AionTransaction(org.aion.base.AionTransaction) AccountState(org.aion.base.AccountState) InternalTransaction(org.aion.types.InternalTransaction) Test(org.junit.Test)

Aggregations

ImportResult (org.aion.zero.impl.core.ImportResult)166 Test (org.junit.Test)127 AionTransaction (org.aion.base.AionTransaction)114 AionAddress (org.aion.types.AionAddress)106 AionBlockSummary (org.aion.zero.impl.types.AionBlockSummary)95 MiningBlock (org.aion.zero.impl.types.MiningBlock)90 Block (org.aion.zero.impl.types.Block)80 BigInteger (java.math.BigInteger)75 AionTxReceipt (org.aion.base.AionTxReceipt)50 AionTxExecSummary (org.aion.base.AionTxExecSummary)31 ECKey (org.aion.crypto.ECKey)28 RepositoryCache (org.aion.base.db.RepositoryCache)22 StandaloneBlockchain (org.aion.zero.impl.blockchain.StandaloneBlockchain)22 InternalTransaction (org.aion.types.InternalTransaction)20 AccountState (org.aion.base.AccountState)18 ArrayList (java.util.ArrayList)17 AionBlockchainImpl.getPostExecutionWorkForGeneratePreBlock (org.aion.zero.impl.blockchain.AionBlockchainImpl.getPostExecutionWorkForGeneratePreBlock)16 Builder (org.aion.zero.impl.blockchain.StandaloneBlockchain.Builder)14 AionRepositoryImpl (org.aion.zero.impl.db.AionRepositoryImpl)10 BlockContext (org.aion.zero.impl.types.BlockContext)9