Search in sources :

Example 96 with RepositoryCache

use of org.aion.base.db.RepositoryCache in project aion by aionnetwork.

the class SolidityTypeTest method testBool.

@Test
public void testBool() throws Exception {
    byte[] params = new BoolType().encode(true);
    System.out.println(Hex.toHexString(params));
    AionTransaction tx = createTransaction(ByteUtil.merge(Hex.decode("e8dde232"), params));
    MiningBlock block = createDummyBlock();
    RepositoryCache repo = createRepository(tx);
    AionTxReceipt receipt = executeTransaction(tx, block, repo).getReceipt();
    System.out.println(receipt);
    assertArrayEquals(params, receipt.getTransactionOutput());
    assertEquals(Boolean.TRUE, new BoolType().decode(receipt.getTransactionOutput()));
}
Also used : BoolType(org.aion.solidity.SolidityType.BoolType) RepositoryCache(org.aion.base.db.RepositoryCache) AionTransaction(org.aion.base.AionTransaction) AionTxReceipt(org.aion.base.AionTxReceipt) MiningBlock(org.aion.zero.impl.types.MiningBlock) Test(org.junit.Test)

Example 97 with RepositoryCache

use of org.aion.base.db.RepositoryCache in project aion by aionnetwork.

the class SolidityTypeTest method testAddress.

@Test
public void testAddress() throws Exception {
    byte[] x = Hex.decode("1122334455667788112233445566778811223344556677881122334455667788");
    byte[] params = new AddressType().encode(x);
    System.out.println(Hex.toHexString(params));
    AionTransaction tx = createTransaction(ByteUtil.merge(Hex.decode("42f45790"), params));
    MiningBlock block = createDummyBlock();
    RepositoryCache repo = createRepository(tx);
    AionTxReceipt receipt = executeTransaction(tx, block, repo).getReceipt();
    System.out.println(receipt);
    assertArrayEquals(params, receipt.getTransactionOutput());
    assertArrayEquals(x, (byte[]) new AddressType().decode(receipt.getTransactionOutput()));
}
Also used : RepositoryCache(org.aion.base.db.RepositoryCache) AionTransaction(org.aion.base.AionTransaction) AionTxReceipt(org.aion.base.AionTxReceipt) AddressType(org.aion.solidity.SolidityType.AddressType) MiningBlock(org.aion.zero.impl.types.MiningBlock) Test(org.junit.Test)

Example 98 with RepositoryCache

use of org.aion.base.db.RepositoryCache in project aion by aionnetwork.

the class SolidityTypeTest method testInt.

@Test
public void testInt() throws Exception {
    BigInteger bi = new BigInteger(1, Hex.decode("ffffffffffffffffffffffff"));
    byte[] params = new IntType("int96").encode(bi);
    System.out.println(Hex.toHexString(params));
    AionTransaction tx = createTransaction(ByteUtil.merge(Hex.decode("6761755c"), params));
    MiningBlock block = createDummyBlock();
    RepositoryCache repo = createRepository(tx);
    AionTxReceipt receipt = executeTransaction(tx, block, repo).getReceipt();
    System.out.println(receipt);
    assertArrayEquals(Hex.decode("00000000ffffffffffffffffffffffff"), receipt.getTransactionOutput());
    assertEquals(bi, new IntType("int96").decode(receipt.getTransactionOutput()));
}
Also used : BigInteger(java.math.BigInteger) RepositoryCache(org.aion.base.db.RepositoryCache) AionTransaction(org.aion.base.AionTransaction) AionTxReceipt(org.aion.base.AionTxReceipt) MiningBlock(org.aion.zero.impl.types.MiningBlock) IntType(org.aion.solidity.SolidityType.IntType) Test(org.junit.Test)

Example 99 with RepositoryCache

use of org.aion.base.db.RepositoryCache in project aion by aionnetwork.

the class TransactionCreateSpecificationTests method deployInternalAvmContractOnTopOfAddressWithNonceUsingAvmVersion2.

@Test
public void deployInternalAvmContractOnTopOfAddressWithNonceUsingAvmVersion2() 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 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()).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.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 100 with RepositoryCache

use of org.aion.base.db.RepositoryCache in project aion by aionnetwork.

the class TransactionCreateSpecificationTests method deployAvmContractOnTopOfAddressWithNonceUsingAvmVersion2.

@Test
public void deployAvmContractOnTopOfAddressWithNonceUsingAvmVersion2() throws VmFatalException {
    // Deploy AVM contract.
    AionTransaction deployTxAvm = BlockchainTestUtils.deployAvmContractTransaction(AvmContract.HELLO_WORLD, resourceProvider.factoryForVersion2, SENDER_KEY, BigInteger.ZERO);
    AionAddress contract = TxUtil.calculateContractAddress(deployTxAvm);
    // Manipulate the repository to have a non-default nonce value.
    RepositoryCache cache = blockchain.getRepository().startTracking();
    cache.createAccount(contract);
    cache.setNonce(contract, BigInteger.TEN);
    cache.flushTo(cache.getParent(), true);
    // Check assumptions about contract state.
    AccountState contractState = (AccountState) blockchain.getRepository().startTracking().getAccountState(contract);
    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(deployTxAvm, true);
    assertThat(result.isFailed()).isTrue();
    assertThat(result.getReceipt().getError()).isEqualTo("Failed: destination address has a non-default state");
    assertThat(result.getNrgUsed()).isEqualTo(BigInteger.valueOf(deployTxAvm.getEnergyLimit()));
    assertThat(result.getLogs()).isEmpty();
    contractState = (AccountState) blockchain.getRepository().startTracking().getAccountState(contract);
    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) AionTxExecSummary(org.aion.base.AionTxExecSummary) RepositoryCache(org.aion.base.db.RepositoryCache) AionTransaction(org.aion.base.AionTransaction) AccountState(org.aion.base.AccountState) Test(org.junit.Test)

Aggregations

RepositoryCache (org.aion.base.db.RepositoryCache)120 Test (org.junit.Test)102 AionAddress (org.aion.types.AionAddress)90 AionTransaction (org.aion.base.AionTransaction)89 AionTxExecSummary (org.aion.base.AionTxExecSummary)72 BigInteger (java.math.BigInteger)43 MiningBlock (org.aion.zero.impl.types.MiningBlock)41 AccountState (org.aion.base.AccountState)38 DataWord (org.aion.util.types.DataWord)23 AionRepositoryCache (org.aion.zero.impl.db.AionRepositoryCache)23 ImportResult (org.aion.zero.impl.core.ImportResult)22 InternalTransaction (org.aion.types.InternalTransaction)18 Block (org.aion.zero.impl.types.Block)18 AionTxReceipt (org.aion.base.AionTxReceipt)17 AionBlockchainImpl.getPostExecutionWorkForGeneratePreBlock (org.aion.zero.impl.blockchain.AionBlockchainImpl.getPostExecutionWorkForGeneratePreBlock)16 BlockContext (org.aion.zero.impl.types.BlockContext)15 SolidityType (org.aion.solidity.SolidityType)10 ArrayList (java.util.ArrayList)8 AionBlockSummary (org.aion.zero.impl.types.AionBlockSummary)7 AionRepositoryImpl (org.aion.zero.impl.db.AionRepositoryImpl)6