Search in sources :

Example 21 with MiningBlock

use of org.aion.zero.impl.types.MiningBlock in project aion by aionnetwork.

the class ContractIntegTest method deployContract.

private AionAddress deployContract(RepositoryCache repo, AionTransaction tx, String contractName, String contractFilename, BigInteger value, long nrg, long nrgPrice, BigInteger nonce, boolean addToBlockChain) throws IOException, VmFatalException {
    assertTrue(tx.isContractCreationTransaction());
    assertEquals(deployerBalance, repo.getBalance(deployer));
    assertEquals(deployerNonce, repo.getNonce(deployer));
    MiningBlock block = makeBlock(tx);
    AionTxExecSummary summary = executeTransaction(tx, block, repo);
    if (!summary.getReceipt().getError().equals("")) {
        return null;
    }
    assertNotEquals(nrg, summary.getNrgUsed().longValue());
    AionAddress contract = TxUtil.calculateContractAddress(tx);
    if (contractFilename == null) {
        checkStateOfNewContract(repo, contractName, contract, summary.getResult(), FastVmResultCode.SUCCESS, value);
    } else {
        checkStateOfNewContract(repo, contractName, contractFilename, contract, summary.getResult(), FastVmResultCode.SUCCESS, value);
    }
    checkStateOfDeployer(repo, summary, nrgPrice, value, nonce);
    if (addToBlockChain) {
        Pair<ImportResult, AionBlockSummary> result = blockchain.tryToConnectAndFetchSummary(block);
        assertTrue(result.getLeft().isSuccessful());
    }
    return contract;
}
Also used : AionAddress(org.aion.types.AionAddress) ImportResult(org.aion.zero.impl.core.ImportResult) AionBlockSummary(org.aion.zero.impl.types.AionBlockSummary) AionTxExecSummary(org.aion.base.AionTxExecSummary) MiningBlock(org.aion.zero.impl.types.MiningBlock)

Example 22 with MiningBlock

use of org.aion.zero.impl.types.MiningBlock in project aion by aionnetwork.

the class ContractIntegTest method testFvmConstructorIsCalledOnCodeDeployment.

@Test
public void testFvmConstructorIsCalledOnCodeDeployment() throws Exception {
    String contractName = "MultiFeatureContract";
    byte[] deployCode = ContractUtils.getContractDeployer("MultiFeatureContract.sol", "MultiFeatureContract");
    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.DEFAULT) {
        // Now call the contract and check that the constructor message was set.
        String getMsgFunctionHash = "ce6d41de";
        tx = AionTransaction.create(deployerKey, nonce.toByteArray(), contract, BigInteger.ZERO.toByteArray(), Hex.decode(getMsgFunctionHash), 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());
        String expectedMsg = "Im alive!";
        assertEquals(expectedMsg, new String(extractOutput(summary.getResult())));
    } else if (txType == TransactionTypes.AVM_CREATE_CODE) {
        assertNull(contract);
    }
}
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) MiningBlock(org.aion.zero.impl.types.MiningBlock) Test(org.junit.Test)

Example 23 with MiningBlock

use of org.aion.zero.impl.types.MiningBlock in project aion by aionnetwork.

the class ContractIntegTest method tellFvmContractCallWithinDeployingBlock.

@Test
public void tellFvmContractCallWithinDeployingBlock() throws IOException {
    if (txType == TransactionTypes.AVM_CREATE_CODE) {
        return;
    }
    String contractName = "InternalCallContract";
    byte[] deployCode = getDeployCode(contractName);
    long nrg = Constants.NRG_TRANSACTION_MAX;
    long nrgPrice = energyPrice;
    BigInteger value = BigInteger.ZERO;
    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);
    assertTrue(tx.isContractCreationTransaction());
    assertEquals(deployerBalance, repo.getBalance(deployer));
    assertEquals(deployerNonce, repo.getNonce(deployer));
    List<AionTransaction> ls = new ArrayList<>();
    ls.add(tx);
    byte[] input = Arrays.copyOfRange(HashUtil.keccak256("sendValueToContract()".getBytes()), 0, 4);
    AionTransaction tx2 = AionTransaction.create(deployerKey, nonce.toByteArray(), TxUtil.calculateContractAddress(tx), BigInteger.TEN.toByteArray(), input, nrg, nrgPrice, txType, null);
    assertFalse(tx2.isContractCreationTransaction());
    ls.add(tx2);
    BigInteger senderBalance = repo.getBalance(deployer);
    Block parent = blockchain.getBestBlock();
    MiningBlock block = blockchain.createBlock(parent, ls, false, parent.getTimestamp());
    Pair<ImportResult, AionBlockSummary> result = blockchain.tryToConnectAndFetchSummary(block);
    AionBlockSummary summary = result.getRight();
    assertTrue(result.getLeft().isSuccessful());
    AionAddress contractAddress = TxUtil.calculateContractAddress(tx);
    assertEquals(BigInteger.TEN, blockchain.getRepository().getBalance(contractAddress));
    assertEquals(senderBalance.subtract(BigInteger.TEN).subtract(BigInteger.valueOf(summary.getReceipts().get(0).getEnergyUsed()).multiply(BigInteger.valueOf(nrgPrice))).subtract(BigInteger.valueOf(summary.getReceipts().get(1).getEnergyUsed()).multiply(BigInteger.valueOf(nrgPrice))), blockchain.getRepository().getBalance(deployer));
    repo = blockchain.getRepository().startTracking();
    assertEquals(BigInteger.TWO, repo.getNonce(deployer));
}
Also used : ImportResult(org.aion.zero.impl.core.ImportResult) AionAddress(org.aion.types.AionAddress) ArrayList(java.util.ArrayList) AionTransaction(org.aion.base.AionTransaction) MiningBlock(org.aion.zero.impl.types.MiningBlock) AionBlockSummary(org.aion.zero.impl.types.AionBlockSummary) BigInteger(java.math.BigInteger) AionRepositoryCache(org.aion.zero.impl.db.AionRepositoryCache) RepositoryCache(org.aion.base.db.RepositoryCache) MiningBlock(org.aion.zero.impl.types.MiningBlock) Block(org.aion.zero.impl.types.Block) Test(org.junit.Test)

Example 24 with MiningBlock

use of org.aion.zero.impl.types.MiningBlock in project aion by aionnetwork.

the class ContractIntegTest method tellFvmContractCallAvmContract.

@Test
public void tellFvmContractCallAvmContract() throws Exception {
    if (txType == TransactionTypes.AVM_CREATE_CODE) {
        return;
    }
    String contractName = "InternalCallContract";
    byte[] deployCode = getDeployCode(contractName);
    long nrg = Constants.NRG_TRANSACTION_MAX;
    long nrgPrice = energyPrice;
    BigInteger value = BigInteger.ZERO;
    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, true);
    assertNotNull(contract);
    repo = blockchain.getRepository().startTracking();
    AionAddress avmAddress = deployAvmContract(AvmVersion.VERSION_1, nonce);
    assertNotNull(avmAddress);
    nonce = nonce.add(BigInteger.ONE);
    byte[] input = Arrays.copyOfRange(HashUtil.keccak256("callAVM(address)".getBytes()), 0, 4);
    input = ByteUtil.merge(input, avmAddress.toByteArray());
    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);
    // The evmjit only return the the transaction success or failed when performing the function
    // call.
    assertEquals("reverted", summary.getReceipt().getError());
    assertNotEquals(nrg, summary.getNrgUsed().longValue());
    Pair<ImportResult, AionBlockSummary> result = blockchain.tryToConnectAndFetchSummary(block);
    assertTrue(result.getLeft().isSuccessful());
    assertTrue(result.getRight().getSummaries().get(0).isFailed());
}
Also used : AionAddress(org.aion.types.AionAddress) ImportResult(org.aion.zero.impl.core.ImportResult) AionBlockSummary(org.aion.zero.impl.types.AionBlockSummary) 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) MiningBlock(org.aion.zero.impl.types.MiningBlock) Test(org.junit.Test)

Example 25 with MiningBlock

use of org.aion.zero.impl.types.MiningBlock in project aion by aionnetwork.

the class ContractIntegTest method testDeployWithOutCode.

@Test
public void testDeployWithOutCode() throws Exception {
    long nrg = 1_000_000;
    long nrgPrice = energyPrice;
    // attempt to transfer value to new contract.
    BigInteger value = BigInteger.ZERO;
    BigInteger nonce = BigInteger.ZERO;
    // to == null  signals that this is contract creation.
    AionTransaction tx = AionTransaction.create(deployerKey, nonce.toByteArray(), null, value.toByteArray(), new byte[0], nrg, nrgPrice, txType, null);
    assertTrue(tx.isContractCreationTransaction());
    assertEquals(Builder.DEFAULT_BALANCE, blockchain.getRepository().getBalance(deployer));
    assertEquals(BigInteger.ZERO, blockchain.getRepository().getNonce(deployer));
    if (txType == TransactionTypes.DEFAULT) {
        MiningBlock block = makeBlock(tx);
        RepositoryCache repo = blockchain.getRepository().startTracking();
        AionTxExecSummary summary = executeTransaction(tx, block, repo);
        assertEquals("", summary.getReceipt().getError());
        // all energy is not used up.
        assertNotEquals(nrg, summary.getNrgUsed().longValue());
        AionAddress contract = TxUtil.calculateContractAddress(tx);
        checkStateOfDeployer(repo, summary, nrgPrice, BigInteger.ZERO, nonce.add(BigInteger.ONE));
        byte[] code = repo.getCode(contract);
        assertNotNull(code);
    } else {
        blockchain.forkUtility.enable040Fork(0);
        MiningBlock block = makeBlock(tx);
        RepositoryCache repo = blockchain.getRepository().startTracking();
        AionTxExecSummary summary = executeTransaction(tx, block, repo);
        assertEquals("Failed: invalid data", summary.getReceipt().getError());
    }
}
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) MiningBlock(org.aion.zero.impl.types.MiningBlock) Test(org.junit.Test)

Aggregations

MiningBlock (org.aion.zero.impl.types.MiningBlock)185 Test (org.junit.Test)136 AionTransaction (org.aion.base.AionTransaction)128 ImportResult (org.aion.zero.impl.core.ImportResult)81 AionAddress (org.aion.types.AionAddress)79 BigInteger (java.math.BigInteger)64 Block (org.aion.zero.impl.types.Block)63 AionBlockSummary (org.aion.zero.impl.types.AionBlockSummary)59 RepositoryCache (org.aion.base.db.RepositoryCache)41 AionTxReceipt (org.aion.base.AionTxReceipt)37 ArrayList (java.util.ArrayList)36 ECKey (org.aion.crypto.ECKey)27 AionRepositoryImpl (org.aion.zero.impl.db.AionRepositoryImpl)27 AionTxExecSummary (org.aion.base.AionTxExecSummary)25 AionRepositoryCache (org.aion.zero.impl.db.AionRepositoryCache)24 MiningBlockHeader (org.aion.zero.impl.types.MiningBlockHeader)22 BlockContext (org.aion.zero.impl.types.BlockContext)18 DataWord (org.aion.util.types.DataWord)13 MockDB (org.aion.db.impl.mockdb.MockDB)12 StakingBlock (org.aion.zero.impl.types.StakingBlock)11