Search in sources :

Example 6 with AionAddress

use of org.aion.types.AionAddress 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 7 with AionAddress

use of org.aion.types.AionAddress in project aion by aionnetwork.

the class ContractIntegTest method deployAvmContract.

private AionAddress deployAvmContract(AvmVersion version, BigInteger nonce) {
    byte[] jar = getJarBytes(version);
    AionTransaction transaction = AionTransaction.create(deployerKey, nonce.toByteArray(), null, BigInteger.ZERO.toByteArray(), jar, 5_000_000L, energyPrice, 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();
    // verify that the output is indeed the contract address
    AionAddress contractAddress = TxUtil.calculateContractAddress(transaction);
    assertThat(contractAddress.toByteArray()).isEqualTo(receipt.getTransactionOutput());
    return contractAddress;
}
Also used : 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)

Example 8 with AionAddress

use of org.aion.types.AionAddress in project aion by aionnetwork.

the class ContractIntegTest method testFvmCallFunction.

@Test
public void testFvmCallFunction() throws Exception {
    String contractName = "MultiFeatureContract";
    byte[] deployCode = getDeployCode(contractName);
    long nrg = 1_000_000;
    long nrgPrice = energyPrice;
    BigInteger value = BigInteger.ONE;
    BigInteger nonce = BigInteger.ZERO;
    AionTransaction tx = AionTransaction.create(deployerKey, nonce.toByteArray(), null, value.toByteArray(), deployCode, nrg, nrgPrice, txType, null);
    RepositoryCache repo = blockchain.getRepository().startTracking();
    nonce = nonce.add(BigInteger.ONE);
    AionAddress contract = deployContract(repo, tx, contractName, null, value, nrg, nrgPrice, nonce);
    if (txType == TransactionTypes.AVM_CREATE_CODE) {
        assertNull(contract);
        return;
    }
    // ---------- This command will perform addition. ----------
    int num = 53475374;
    byte[] input = ByteUtil.merge(Hex.decode("f601704f"), new DataWord(num).getData());
    input = ByteUtil.merge(input, new DataWord(1).getData());
    tx = AionTransaction.create(deployerKey, nonce.toByteArray(), contract, BigInteger.ZERO.toByteArray(), input, nrg, nrgPrice, txType, null);
    assertFalse(tx.isContractCreationTransaction());
    MiningBlock block = makeBlock(tx);
    AionTxExecSummary summary = executeTransaction(tx, block, repo);
    assertEquals("", summary.getReceipt().getError());
    assertNotEquals(nrg, summary.getNrgUsed().longValue());
    // Since input takes in uint8 we only want the last byte of num. Output size is well-defined
    // at 128 bits, or 16 bytes.
    int expectedResult = 1111 + (num & 0xFF);
    assertEquals(expectedResult, new DataWord(summary.getResult()).intValue());
    // --------- This command will perform subtraction. ----------
    input = ByteUtil.merge(Hex.decode("f601704f"), new DataWord(num).getData());
    input = ByteUtil.merge(input, new DataWord(0).getData());
    nonce = nonce.add(BigInteger.ONE);
    tx = AionTransaction.create(deployerKey, nonce.toByteArray(), contract, BigInteger.ZERO.toByteArray(), input, nrg, nrgPrice, TransactionTypes.DEFAULT, null);
    assertFalse(tx.isContractCreationTransaction());
    block = makeBlock(tx);
    summary = executeTransaction(tx, block, repo);
    assertEquals("", summary.getReceipt().getError());
    assertNotEquals(nrg, summary.getNrgUsed().longValue());
    // Since input takes in uint8 we only want the last byte of num. Output size is well-defined
    // at 128 bits, or 16 bytes.
    expectedResult = 1111 - (num & 0xFF);
    assertEquals(expectedResult, new DataWord(summary.getResult()).intValue());
}
Also used : AionAddress(org.aion.types.AionAddress) AionTxExecSummary(org.aion.base.AionTxExecSummary) BigInteger(java.math.BigInteger) AionRepositoryCache(org.aion.zero.impl.db.AionRepositoryCache) RepositoryCache(org.aion.base.db.RepositoryCache) AionTransaction(org.aion.base.AionTransaction) DataWord(org.aion.util.types.DataWord) MiningBlock(org.aion.zero.impl.types.MiningBlock) Test(org.junit.Test)

Example 9 with AionAddress

use of org.aion.types.AionAddress in project aion by aionnetwork.

the class ContractIntegTest method setup.

@Before
public void setup() throws Exception {
    // reduce default logging levels
    AionLoggerFactory.initAll();
    StandaloneBlockchain.Bundle bundle = (new Builder()).withValidatorConfiguration("simple").withDefaultAccounts().build();
    TransactionTypeRule.allowAVMContractTransaction();
    blockchain = bundle.bc;
    deployerKey = bundle.privateKeys.get(0);
    accounts = bundle.privateKeys;
    deployer = new AionAddress(deployerKey.getAddress());
    deployerBalance = Builder.DEFAULT_BALANCE;
    deployerNonce = BigInteger.ZERO;
    senderKey = bundle.privateKeys.get(1);
    sender = new AionAddress(senderKey.getAddress());
    senderBalance = Builder.DEFAULT_BALANCE;
    senderNonce = BigInteger.ZERO;
    resourceProvider = TestResourceProvider.initializeAndCreateNewProvider(AvmPathManager.getPathOfProjectRootDirectory());
    AvmTestConfig.supportOnlyAvmVersion1();
    blockchain.forkUtility.disableUnityFork();
}
Also used : AionAddress(org.aion.types.AionAddress) Builder(org.aion.zero.impl.blockchain.StandaloneBlockchain.Builder) StandaloneBlockchain(org.aion.zero.impl.blockchain.StandaloneBlockchain) Before(org.junit.Before)

Example 10 with AionAddress

use of org.aion.types.AionAddress in project aion by aionnetwork.

the class ContractIntegTest method testCallContractViaAnotherContract.

@Test
public void testCallContractViaAnotherContract() throws Exception {
    // Deploy the MultiFeatureContract.
    String contractName = "MultiFeatureContract";
    byte[] deployCode = getDeployCode(contractName);
    long nrg = 1_000_000;
    long nrgPrice = energyPrice;
    BigInteger value = BigInteger.TWO.pow(20);
    BigInteger nonce = BigInteger.ZERO;
    AionTransaction tx = AionTransaction.create(deployerKey, nonce.toByteArray(), null, value.toByteArray(), deployCode, nrg, nrgPrice, txType, null);
    RepositoryCache repo = blockchain.getRepository().startTracking();
    nonce = nonce.add(BigInteger.ONE);
    AionAddress multiFeatureContract = deployContract(repo, tx, contractName, null, value, nrg, nrgPrice, nonce);
    if (txType == TransactionTypes.AVM_CREATE_CODE) {
        assertNull(multiFeatureContract);
        return;
    }
    deployerBalance = repo.getBalance(deployer);
    deployerNonce = repo.getNonce(deployer);
    // Deploy the MultiFeatureCaller contract.
    contractName = "MultiFeatureCaller";
    deployCode = getDeployCode(contractName);
    value = BigInteger.ZERO;
    tx = AionTransaction.create(deployerKey, nonce.toByteArray(), null, value.toByteArray(), deployCode, nrg, nrgPrice, txType, null);
    nonce = nonce.add(BigInteger.ONE);
    AionAddress callerContract = deployContract(repo, tx, contractName, null, value, nrg, nrgPrice, nonce);
    AionAddress recipient = new AionAddress(RandomUtils.nextBytes(AionAddress.LENGTH));
    deployerBalance = repo.getBalance(deployer);
    deployerNonce = repo.getNonce(deployer);
    // Set the MultiFeatureCaller to call the deployed MultiFeatureContract.
    byte[] input = ByteUtil.merge(Hex.decode("8c30ffe6"), multiFeatureContract.toByteArray());
    tx = AionTransaction.create(deployerKey, nonce.toByteArray(), callerContract, BigInteger.ZERO.toByteArray(), input, nrg, nrgPrice, txType, null);
    assertFalse(tx.isContractCreationTransaction());
    MiningBlock block = makeBlock(tx);
    AionTxExecSummary summary = executeTransaction(tx, block, repo);
    assertEquals("", summary.getReceipt().getError());
    assertNotEquals(nrg, summary.getNrgUsed().longValue());
    BigInteger txCost = BigInteger.valueOf(summary.getNrgUsed().longValue()).multiply(BigInteger.valueOf(nrgPrice));
    assertEquals(deployerBalance.subtract(txCost), repo.getBalance(deployer));
    deployerBalance = repo.getBalance(deployer);
    deployerNonce = repo.getNonce(deployer);
    // Now use the MultiFeatureCaller to call the MultiFeatureContract to send funds from that
    // contract to the recipient address.
    assertEquals(BigInteger.ZERO, repo.getBalance(recipient));
    value = BigInteger.TWO.pow(20);
    input = ByteUtil.merge(Hex.decode("57a60e6b"), recipient.toByteArray());
    input = ByteUtil.merge(input, new DataWord(value).getData());
    nonce = nonce.add(BigInteger.ONE);
    tx = AionTransaction.create(deployerKey, nonce.toByteArray(), callerContract, BigInteger.ZERO.toByteArray(), input, nrg, nrgPrice, txType, null);
    assertFalse(tx.isContractCreationTransaction());
    block = makeBlock(tx);
    summary = executeTransaction(tx, block, repo);
    assertEquals("", summary.getReceipt().getError());
    assertNotEquals(nrg, summary.getNrgUsed().longValue());
    txCost = BigInteger.valueOf(summary.getNrgUsed().longValue()).multiply(BigInteger.valueOf(nrgPrice));
    assertEquals(deployerBalance.subtract(txCost), repo.getBalance(deployer));
    assertEquals(value, repo.getBalance(recipient));
}
Also used : AionAddress(org.aion.types.AionAddress) AionTxExecSummary(org.aion.base.AionTxExecSummary) BigInteger(java.math.BigInteger) AionRepositoryCache(org.aion.zero.impl.db.AionRepositoryCache) RepositoryCache(org.aion.base.db.RepositoryCache) AionTransaction(org.aion.base.AionTransaction) DataWord(org.aion.util.types.DataWord) MiningBlock(org.aion.zero.impl.types.MiningBlock) Test(org.junit.Test)

Aggregations

AionAddress (org.aion.types.AionAddress)491 Test (org.junit.Test)364 AionTransaction (org.aion.base.AionTransaction)275 BigInteger (java.math.BigInteger)194 ImportResult (org.aion.zero.impl.core.ImportResult)110 AionTxExecSummary (org.aion.base.AionTxExecSummary)97 RepositoryCache (org.aion.base.db.RepositoryCache)90 MiningBlock (org.aion.zero.impl.types.MiningBlock)89 AionBlockSummary (org.aion.zero.impl.types.AionBlockSummary)80 ArrayList (java.util.ArrayList)76 ECKey (org.aion.crypto.ECKey)74 AionTxReceipt (org.aion.base.AionTxReceipt)60 Block (org.aion.zero.impl.types.Block)60 AccountState (org.aion.base.AccountState)56 HashMap (java.util.HashMap)42 DataWord (org.aion.util.types.DataWord)39 PrecompiledTransactionResult (org.aion.precompiled.PrecompiledTransactionResult)36 InternalTransaction (org.aion.types.InternalTransaction)32 ByteArrayWrapper (org.aion.util.types.ByteArrayWrapper)29 BlockContext (org.aion.zero.impl.types.BlockContext)29