Search in sources :

Example 1 with CompilationResult

use of org.aion.solidity.CompilationResult in project aion by aionnetwork.

the class Api method contract_compileSolidity.

public Map<String, CompiledContr> contract_compileSolidity(final String _contract) {
    try {
        Map<String, CompiledContr> compiledContracts = new HashMap<String, CompiledContr>();
        Compiler.Result res = solc.compile(_contract.getBytes(), Compiler.Options.ABI, Compiler.Options.BIN);
        if (res.isFailed()) {
            LOG.info("contract compile error: [{}]", res.errors);
            CompiledContr ret = new CompiledContr();
            ret.error = res.errors;
            compiledContracts.put("compile-error", ret);
            return compiledContracts;
        }
        CompilationResult result = CompilationResult.parse(res.output);
        Iterator<Entry<String, CompilationResult.Contract>> entries = result.contracts.entrySet().iterator();
        while (entries.hasNext()) {
            CompiledContr ret = new CompiledContr();
            Entry<String, CompilationResult.Contract> entry = entries.next();
            CompilationResult.Contract Contract = entry.getValue();
            ret.code = TypeConverter.toJsonHex(Contract.bin);
            ret.info = new CompiContrInfo();
            ret.info.source = _contract;
            ret.info.language = "Solidity";
            ret.info.languageVersion = "0";
            ret.info.compilerVersion = result.version;
            ret.info.abiDefinition = Abi.fromJSON(Contract.abi).getEntries();
            compiledContracts.put(entry.getKey(), ret);
        }
        return compiledContracts;
    } catch (IOException | NoSuchElementException ex) {
        LOG.debug("contract compile error");
        return null;
    }
}
Also used : Compiler(org.aion.solidity.Compiler) IOException(java.io.IOException) CompiledContr(org.aion.api.server.types.CompiledContr) Entry(java.util.Map.Entry) CompiContrInfo(org.aion.api.server.types.CompiContrInfo) CompilationResult(org.aion.solidity.CompilationResult)

Example 2 with CompilationResult

use of org.aion.solidity.CompilationResult in project aion by aionnetwork.

the class OldTxExecutorTest method testCallTransaction.

@Test
public void testCallTransaction() throws Exception {
    Compiler.Result r = Compiler.getInstance().compile(ContractUtils.readContract("Ticker.sol"), Options.ABI, Options.BIN);
    CompilationResult cr = CompilationResult.parse(r.output);
    // deployer
    String deployer = cr.contracts.get("Ticker").bin;
    // contract
    String contract = deployer.substring(deployer.indexOf("60506040", 1));
    byte[] txNonce = BigInteger.ZERO.toByteArray();
    AionAddress to = AddressUtils.wrapAddress("2222222222222222222222222222222222222222222222222222222222222222");
    byte[] value = BigInteger.ZERO.toByteArray();
    byte[] data = Hex.decode("c0004213");
    long nrg = new DataWord(100000L).longValue();
    long nrgPrice = DataWord.ONE.longValue();
    AionTransaction tx = AionTransaction.create(deployerKey, txNonce, to, value, data, nrg, nrgPrice, TransactionTypes.DEFAULT, null);
    MiningBlock block = createDummyBlock();
    AionRepositoryImpl repo = blockchain.getRepository();
    RepositoryCache cache = repo.startTracking();
    cache.addBalance(tx.getSenderAddress(), BigInteger.valueOf(100_000).multiply(BigInteger.valueOf(tx.getEnergyPrice())));
    cache.createAccount(to);
    cache.saveCode(to, Hex.decode(contract));
    cache.saveVmType(to, InternalVmType.FVM);
    cache.flushTo(repo, true);
    AionTxReceipt receipt = executeTransaction(repo, block, tx).getReceipt();
    System.out.println(receipt);
    assertArrayEquals(Hex.decode("00000000000000000000000000000000"), receipt.getTransactionOutput());
}
Also used : Compiler(org.aion.solidity.Compiler) AionAddress(org.aion.types.AionAddress) DataWord(org.aion.util.types.DataWord) AionTransaction(org.aion.base.AionTransaction) AionRepositoryImpl(org.aion.zero.impl.db.AionRepositoryImpl) MiningBlock(org.aion.zero.impl.types.MiningBlock) RepositoryCache(org.aion.base.db.RepositoryCache) CompilationResult(org.aion.solidity.CompilationResult) AionTxReceipt(org.aion.base.AionTxReceipt) Test(org.junit.Test)

Example 3 with CompilationResult

use of org.aion.solidity.CompilationResult in project aion by aionnetwork.

the class ContractUtils method getContractDeployer.

/**
 * Compiles the given solidity source file and returns the deployer code for the given contract.
 *
 * @param fileName
 * @param contractName
 * @return
 * @throws IOException
 */
public static byte[] getContractDeployer(String fileName, String contractName) throws IOException {
    Compiler.Result r = Compiler.getInstance().compile(readContract(fileName), Options.BIN);
    CompilationResult cr = CompilationResult.parse(r.output);
    String deployer = cr.contracts.get(contractName).bin;
    return Hex.decode(deployer);
}
Also used : Compiler(org.aion.solidity.Compiler) CompilationResult(org.aion.solidity.CompilationResult)

Example 4 with CompilationResult

use of org.aion.solidity.CompilationResult in project aion by aionnetwork.

the class OldTxExecutorTest method testPerformance.

@Test
public void testPerformance() throws Exception {
    Compiler.Result r = Compiler.getInstance().compile(ContractUtils.readContract("Ticker.sol"), Options.ABI, Options.BIN);
    CompilationResult cr = CompilationResult.parse(r.output);
    // deployer
    String deployer = cr.contracts.get("Ticker").bin;
    // contract
    String contract = deployer.substring(deployer.indexOf("60506040", 1));
    byte[] txNonce = BigInteger.ZERO.toByteArray();
    AionAddress to = AddressUtils.wrapAddress("2222222222222222222222222222222222222222222222222222222222222222");
    byte[] value = BigInteger.ZERO.toByteArray();
    byte[] data = Hex.decode("c0004213");
    long nrg = new DataWord(100000L).longValue();
    long nrgPrice = DataWord.ONE.longValue();
    ECKey key = ECKeyFac.inst().create();
    AionTransaction tx = AionTransaction.create(key, txNonce, to, value, data, nrg, nrgPrice, TransactionTypes.DEFAULT, null);
    MiningBlock block = createDummyBlock();
    AionRepositoryImpl repoTop = blockchain.getRepository();
    RepositoryCache repo = repoTop.startTracking();
    repo.addBalance(tx.getSenderAddress(), BigInteger.valueOf(100_000).multiply(BigInteger.valueOf(tx.getEnergyPrice())));
    repo.createAccount(to);
    repo.saveCode(to, Hex.decode(contract));
    repo.saveVmType(to, InternalVmType.FVM);
    repo.flushTo(repoTop, true);
    long t1 = System.nanoTime();
    long repeat = 1000;
    for (int i = 0; i < repeat; i++) {
        executeTransaction(repo, block, tx);
    }
    long t2 = System.nanoTime();
    System.out.println((t2 - t1) / repeat);
}
Also used : Compiler(org.aion.solidity.Compiler) AionAddress(org.aion.types.AionAddress) DataWord(org.aion.util.types.DataWord) ECKey(org.aion.crypto.ECKey) AionTransaction(org.aion.base.AionTransaction) AionRepositoryImpl(org.aion.zero.impl.db.AionRepositoryImpl) MiningBlock(org.aion.zero.impl.types.MiningBlock) RepositoryCache(org.aion.base.db.RepositoryCache) CompilationResult(org.aion.solidity.CompilationResult) Test(org.junit.Test)

Example 5 with CompilationResult

use of org.aion.solidity.CompilationResult in project aion by aionnetwork.

the class OldTxExecutorTest method testCreateTransaction.

@Test
public void testCreateTransaction() throws Exception {
    Compiler.Result r = Compiler.getInstance().compile(ContractUtils.readContract("Ticker.sol"), Options.ABI, Options.BIN);
    CompilationResult cr = CompilationResult.parse(r.output);
    String deployer = cr.contracts.get("Ticker").bin;
    System.out.println(deployer);
    byte[] txNonce = BigInteger.ZERO.toByteArray();
    AionAddress to = null;
    byte[] value = BigInteger.ZERO.toByteArray();
    byte[] data = Hex.decode(deployer);
    long nrg = 500_000L;
    long nrgPrice = 1;
    AionTransaction tx = AionTransaction.create(deployerKey, txNonce, to, value, data, nrg, nrgPrice, TransactionTypes.DEFAULT, null);
    MiningBlock block = createDummyBlock();
    AionRepositoryImpl repoTop = blockchain.getRepository();
    RepositoryCache repo = repoTop.startTracking();
    repo.addBalance(tx.getSenderAddress(), BigInteger.valueOf(500_000L).multiply(BigInteger.valueOf(tx.getEnergyPrice())));
    AionTxReceipt receipt = executeTransaction(repo, block, tx).getReceipt();
    System.out.println(receipt);
    assertArrayEquals(Hex.decode(deployer.substring(deployer.indexOf("60506040", 1))), receipt.getTransactionOutput());
}
Also used : Compiler(org.aion.solidity.Compiler) AionAddress(org.aion.types.AionAddress) RepositoryCache(org.aion.base.db.RepositoryCache) AionTransaction(org.aion.base.AionTransaction) AionRepositoryImpl(org.aion.zero.impl.db.AionRepositoryImpl) CompilationResult(org.aion.solidity.CompilationResult) AionTxReceipt(org.aion.base.AionTxReceipt) MiningBlock(org.aion.zero.impl.types.MiningBlock) Test(org.junit.Test)

Aggregations

CompilationResult (org.aion.solidity.CompilationResult)9 Compiler (org.aion.solidity.Compiler)7 AionTransaction (org.aion.base.AionTransaction)4 RepositoryCache (org.aion.base.db.RepositoryCache)4 AionAddress (org.aion.types.AionAddress)4 AionRepositoryImpl (org.aion.zero.impl.db.AionRepositoryImpl)4 Test (org.junit.Test)4 AionTxReceipt (org.aion.base.AionTxReceipt)3 MiningBlock (org.aion.zero.impl.types.MiningBlock)3 CompiContrInfo (org.aion.api.server.types.CompiContrInfo)2 CompiledContr (org.aion.api.server.types.CompiledContr)2 ECKey (org.aion.crypto.ECKey)2 DataWord (org.aion.util.types.DataWord)2 IOException (java.io.IOException)1 BigInteger (java.math.BigInteger)1 HashMap (java.util.HashMap)1 Entry (java.util.Map.Entry)1 TxRecptLg (org.aion.api.server.types.TxRecptLg)1 Contract (org.aion.solidity.CompilationResult.Contract)1 Result (org.aion.solidity.Compiler.Result)1