Search in sources :

Example 6 with CompilationResult

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

the class ContractUtils method getContractBody.

/**
 * Compiles the given solidity source file and returns the contract code for the given contract.
 *
 * <p>NOTE: This method assumes the constructor is empty.
 *
 * @param fileName
 * @param contractName
 * @return
 * @throws IOException
 */
public static byte[] getContractBody(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;
    String contract = deployer.substring(deployer.indexOf("60506040", 1));
    return Hex.decode(contract);
}
Also used : Compiler(org.aion.solidity.Compiler) CompilationResult(org.aion.solidity.CompilationResult)

Example 7 with CompilationResult

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

the class Api method processCompileRsp.

private Map<String, CompiledContr> processCompileRsp(Compiler.Result res, String source) {
    Map<String, CompiledContr> compiledContracts = new HashMap<String, CompiledContr>();
    if (res.isFailed()) {
        LOG.info("contract compile error: [{}]", res.errors);
        /*
             Enhance performance by separating the log threads and kernel TODO: Implement a
             queue for strings TODO: Put every LOG message onto the queue TODO: Use a thread
             service to process these message
            */
        CompiledContr ret = new CompiledContr();
        ret.error = res.errors;
        compiledContracts.put("compile-error", ret);
        return compiledContracts;
    }
    CompilationResult result = CompilationResult.parse(res.output);
    for (Entry<String, Contract> stringContractEntry : result.contracts.entrySet()) {
        CompiledContr ret = new CompiledContr();
        Contract Contract = stringContractEntry.getValue();
        ret.code = StringUtils.toJsonHex(Contract.bin);
        ret.info = new CompiContrInfo();
        ret.info.source = source;
        ret.info.language = "Solidity";
        ret.info.languageVersion = "0";
        ret.info.compilerVersion = result.version;
        ret.info.abiDefinition = Abi.fromJSON(Contract.abi, new ExternalCapabilities()).getEntries();
        compiledContracts.put(stringContractEntry.getKey(), ret);
    }
    return compiledContracts;
}
Also used : HashMap(java.util.HashMap) CompiContrInfo(org.aion.api.server.types.CompiContrInfo) CompilationResult(org.aion.solidity.CompilationResult) Contract(org.aion.solidity.CompilationResult.Contract) CompiledContr(org.aion.api.server.types.CompiledContr)

Example 8 with CompilationResult

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

the class SolidityTypeTest method createRepository.

private RepositoryCache createRepository(AionTransaction tx) throws IOException {
    Result r = Compiler.getInstance().compile(ContractUtils.readContract("SolidityType.sol"), Options.BIN);
    CompilationResult cr = CompilationResult.parse(r.output);
    String deployer = cr.contracts.get("SolidityType").bin;
    String contract = deployer.substring(deployer.indexOf("60506040", 1));
    AionRepositoryImpl repo = blockchain.getRepository();
    RepositoryCache track = repo.startTracking();
    track.addBalance(tx.getSenderAddress(), BigInteger.valueOf(tx.getEnergyPrice()).multiply(BigInteger.valueOf(500_000L)));
    track.createAccount(tx.getDestinationAddress());
    track.saveCode(tx.getDestinationAddress(), Hex.decode(contract));
    track.saveVmType(tx.getDestinationAddress(), InternalVmType.FVM);
    track.flushTo(repo, true);
    return track;
}
Also used : RepositoryCache(org.aion.base.db.RepositoryCache) AionRepositoryImpl(org.aion.zero.impl.db.AionRepositoryImpl) CompilationResult(org.aion.solidity.CompilationResult) Result(org.aion.solidity.Compiler.Result) CompilationResult(org.aion.solidity.CompilationResult)

Example 9 with CompilationResult

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

the class TxRecptLgTest method TestTxRecptLg.

@Test
public void TestTxRecptLg() throws InterruptedException, IOException {
    StandaloneBlockchain.Bundle bundle = new StandaloneBlockchain.Builder().withValidatorConfiguration("simple").withDefaultAccounts().build();
    StandaloneBlockchain bc = bundle.bc;
    ECKey deployerAccount = bundle.privateKeys.get(0);
    // ======================
    // DEPLOY contract A & B
    // ======================
    Compiler.Result r = Compiler.getInstance().compile(readContract("contract/contract.sol"), Compiler.Options.ABI, Compiler.Options.BIN);
    CompilationResult cr = CompilationResult.parse(r.output);
    String contractA = cr.contracts.get("A").bin;
    String contractB = cr.contracts.get("B").bin;
    BigInteger nonce = BigInteger.ZERO;
    AionTransaction tx1 = AionTransaction.create(deployerAccount, nonce.toByteArray(), null, new byte[0], ByteUtil.hexStringToBytes(contractA), 1_000_000L, energyPrice, TransactionTypes.DEFAULT, null);
    nonce = nonce.add(BigInteger.ONE);
    AionTransaction tx2 = AionTransaction.create(deployerAccount, nonce.toByteArray(), null, new byte[0], ByteUtil.hexStringToBytes(contractB), 1_000_000L, energyPrice, TransactionTypes.DEFAULT, null);
    BlockContext context = bc.createNewMiningBlockContext(bc.getBestBlock(), List.of(tx1, tx2), false);
    ImportResult result = bc.tryToConnect(context.block);
    assertEquals(result, ImportResult.IMPORTED_BEST);
    AionAddress addressA = TxUtil.calculateContractAddress(tx1);
    System.out.println("contract A address = " + addressA);
    AionAddress addressB = TxUtil.calculateContractAddress(tx2);
    System.out.println("contract B address = " + addressB);
    Thread.sleep(1000);
    // ======================
    // CALL function A.AA
    // ======================
    nonce = nonce.add(BigInteger.ONE);
    byte[] functionAA = new byte[4];
    System.arraycopy(HashUtil.keccak256("AA(address)".getBytes()), 0, functionAA, 0, 4);
    AionTransaction tx3 = AionTransaction.create(deployerAccount, nonce.toByteArray(), addressA, new byte[0], ByteUtil.merge(functionAA, addressB.toByteArray()), 1_000_000L, energyPrice, TransactionTypes.DEFAULT, null);
    context = bc.createNewMiningBlockContext(bc.getBestBlock(), List.of(tx3), false);
    result = bc.tryToConnect(context.block);
    assertEquals(result, ImportResult.IMPORTED_BEST);
    AionTxInfo info = bc.getTransactionInfo(tx3.getTransactionHash());
    AionTxReceipt receipt = info.getReceipt();
    System.out.println(receipt);
    assertEquals(4, receipt.getLogInfoList().size());
    // ======================
    // Test
    // ======================
    TxRecptLg[] logs = new TxRecptLg[receipt.getLogInfoList().size()];
    for (int i = 0; i < logs.length; i++) {
        Log logInfo = receipt.getLogInfoList().get(i);
        logs[i] = new TxRecptLg(logInfo, context.block, info.getIndex(), receipt.getTransaction(), i, true);
    }
    String ctAddrA = "0x" + addressA.toString();
    String ctAddrB = "0x" + addressB.toString();
    // AE
    assertEquals(ctAddrA, logs[0].address);
    // AEA
    assertEquals(ctAddrA, logs[1].address);
    // b.BB
    assertEquals(ctAddrB, logs[2].address);
    // AEB
    assertEquals(ctAddrA, logs[3].address);
}
Also used : Compiler(org.aion.solidity.Compiler) ImportResult(org.aion.zero.impl.core.ImportResult) AionAddress(org.aion.types.AionAddress) AionTxInfo(org.aion.zero.impl.types.AionTxInfo) Log(org.aion.types.Log) BlockContext(org.aion.zero.impl.types.BlockContext) StandaloneBlockchain(org.aion.zero.impl.blockchain.StandaloneBlockchain) ECKey(org.aion.crypto.ECKey) AionTransaction(org.aion.base.AionTransaction) TxRecptLg(org.aion.api.server.types.TxRecptLg) BigInteger(java.math.BigInteger) CompilationResult(org.aion.solidity.CompilationResult) AionTxReceipt(org.aion.base.AionTxReceipt) 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