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);
}
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;
}
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;
}
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);
}
Aggregations