Search in sources :

Example 26 with Log

use of org.aion.types.Log 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)

Example 27 with Log

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

the class OpsRPCImplTest method setup.

@Before
public void setup() {
    emptyPowBlock = MiningBlock.newEmptyBlock();
    emptyPowBlock.setMainChain();
    AionTxReceipt receipt = new AionTxReceipt();
    ECKey ecKey = ECKeyFac.inst().create();
    receipt.setError("");
    receipt.setExecutionResult(HashUtil.h256(BigInteger.ONE.toByteArray()));
    List<Log> infos = new ArrayList<>();
    receipt.setLogs(infos);
    receipt.setPostTxState(HashUtil.h256(BigInteger.ONE.toByteArray()));
    txInfo = AionTxInfo.newInstanceWithInternalTransactions(receipt, ByteArrayWrapper.wrap(HashUtil.h256(BigInteger.ZERO.toByteArray())), 0, Collections.emptyList());
    txInfo.getReceipt().setTransaction(AionTransaction.create(ecKey, BigInteger.ZERO.toByteArray(), new AionAddress(ecKey.getAddress()), BigInteger.ZERO.toByteArray(), BigInteger.ZERO.toByteArray(), 10, 10, (byte) 0b1, HashUtil.h256(BigInteger.ZERO.toByteArray())));
    transactionHash = ByteArray.wrap(txInfo.getReceipt().getTransaction().getTransactionHash());
    txList.add(txInfo.getReceipt().getTransaction());
    StakingBlockHeader.Builder builder = StakingBlockHeader.Builder.newInstance().withDefaultCoinbase().withDefaultDifficulty().withDefaultExtraData().withDefaultLogsBloom().withDefaultParentHash().withDefaultReceiptTrieRoot().withDefaultSeed().withDefaultSignature().withDefaultSigningPublicKey().withDefaultStateRoot().withDefaultTxTrieRoot();
    emptyPosBlock = new StakingBlock(builder.build(), txList);
    doReturn(BigInteger.ONE).when(holder).calculateReward(any());
    doReturn(emptyPowBlock).when(holder).getBlockByNumber(1);
    doReturn(emptyPosBlock).when(holder).getBlockByNumber(2);
    doReturn(emptyPosBlock).when(holder).getBlockByHash(emptyPosBlock.getHash());
    doReturn(emptyPowBlock).when(holder).getBlockByHash(emptyPowBlock.getHash());
    doReturn(emptyPowBlock).when(holder).getBestBlock();
    doReturn(txInfo).when(holder).getTransactionInfo(any());
    doReturn(emptyPosBlock).when(holder).getBlockByHash(eq(ByteArrayWrapper.wrap(HashUtil.h256(BigInteger.ZERO.toByteArray())).toBytes()));
    doReturn(BigInteger.ONE).when(holder).getTotalDifficultyByHash(any());
    doReturn(new AccountState(BigInteger.TEN, BigInteger.TEN)).when(holder).getAccountState(any());
}
Also used : AionAddress(org.aion.types.AionAddress) Log(org.aion.types.Log) StakingBlockHeader(org.aion.zero.impl.types.StakingBlockHeader) ArrayList(java.util.ArrayList) ECKey(org.aion.crypto.ECKey) AionTxReceipt(org.aion.base.AionTxReceipt) AccountState(org.aion.base.AccountState) StakingBlock(org.aion.zero.impl.types.StakingBlock) Before(org.junit.Before)

Example 28 with Log

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

the class FltrLg method onBlock.

// inelegant (distributing chain singleton ref. into here), tradeoff for efficiency and ease of
// impl.
// rationale: this way, we only retrieve logs from DB for transactions that the bloom
// filter gives a positive match for;
public boolean onBlock(Block blk, IAionBlockchain chain) {
    if (matchBloom(new Bloom(blk.getLogBloom()))) {
        int txIndex = 0;
        for (AionTransaction txn : blk.getTransactionsList()) {
            // The if condition checks the contract create and call transactions
            if ((txn.getDestinationAddress() != null && matchesContractAddress(txn.getDestinationAddress().toByteArray())) || txn.isContractCreationTransaction()) {
                // now that we know that our filter might match with some logs in this
                // transaction, go ahead
                // and retrieve the txReceipt from the chain
                AionTxInfo txInfo = chain.getTransactionInfo(txn.getTransactionHash());
                AionTxReceipt receipt = txInfo.getReceipt();
                if (matchBloom(receipt.getBloomFilter())) {
                    int logIndex = 0;
                    for (Log logInfo : receipt.getLogInfoList()) {
                        if (matchBloom(LogUtility.createBloomFilterForLog(logInfo)) && matchesExactly(logInfo)) {
                            add(new EvtLg(new TxRecptLg(logInfo, blk, txIndex, txn, logIndex, true)));
                        }
                        logIndex++;
                    }
                }
            }
            txIndex++;
        }
    }
    return true;
}
Also used : AionTxInfo(org.aion.zero.impl.types.AionTxInfo) Log(org.aion.types.Log) Bloom(org.aion.base.Bloom) AionTransaction(org.aion.base.AionTransaction) AionTxReceipt(org.aion.base.AionTxReceipt)

Example 29 with Log

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

the class RPCMethods method serializeTxLogsDetails.

private List<TxLogDetails> serializeTxLogsDetails(AionTxReceipt receipt, int index, long blockNumber) {
    List<Log> logs = receipt.getLogInfoList();
    if (logs == null)
        return Collections.emptyList();
    else {
        List<TxLogDetails> logDetails = new ArrayList<>();
        for (int i = 0; i < logs.size(); i++) {
            Log log = logs.get(i);
            logDetails.add(new TxLogDetails(new AionAddress(log.copyOfAddress()), index, ByteArray.wrap(log.copyOfData()), log.copyOfTopics().stream().map(ByteArray::new).collect(Collectors.toUnmodifiableList()), blockNumber));
        }
        return Collections.unmodifiableList(logDetails);
    }
}
Also used : TxLogDetails(org.aion.rpc.types.RPCTypes.TxLogDetails) AionAddress(org.aion.types.AionAddress) Log(org.aion.types.Log) TxLog(org.aion.rpc.types.RPCTypes.TxLog) ArrayList(java.util.ArrayList)

Example 30 with Log

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

the class ApiAion0 method generateTransactionSqlStatement.

private String generateTransactionSqlStatement(Block b, AionTransaction t, List<Log> _logs, int txIndex, long nrgConsumed) {
    JSONArray logs = new JSONArray();
    for (Log l : _logs) {
        JSONArray log = new JSONArray();
        AionAddress aionAddress = new AionAddress(l.copyOfAddress());
        // address
        log.put(aionAddress.toString());
        // data
        log.put(ByteUtil.toHexString(l.copyOfData()));
        JSONArray topics = new JSONArray();
        for (byte[] topic : l.copyOfTopics()) {
            topics.put(ByteUtil.toHexString(topic));
        }
        // topics
        log.put(topics);
        logs.put(log);
    }
    return "'" + ByteUtil.toHexString(t.getTransactionHash()) + "'," + "'" + ByteUtil.toHexString(b.getHash()) + "'," + b.getNumber() + "," + txIndex + "," + "'" + ByteUtil.toHexString(t.getSenderAddress().toByteArray()) + "'," + "'" + ByteUtil.toHexString(t.getDestinationAddress() == null ? EMPTY_BYTE_ARRAY : t.getDestinationAddress().toByteArray()) + "'," + nrgConsumed + "," + t.getEnergyPrice() + "," + ByteUtil.byteArrayToLong(t.getTimestamp()) + "," + b.getTimestamp() + "," + "'" + ByteUtil.toHexString(t.getValue()) + "'," + "'" + logs.toString() + "'," + "'" + ByteUtil.toHexString(t.getData()) + "'," + "'" + ByteUtil.toHexString(t.getNonce()) + "'";
}
Also used : AionAddress(org.aion.types.AionAddress) Log(org.aion.types.Log) JSONArray(org.json.JSONArray)

Aggregations

Log (org.aion.types.Log)31 AionAddress (org.aion.types.AionAddress)21 Test (org.junit.Test)18 BigInteger (java.math.BigInteger)14 AionTransaction (org.aion.base.AionTransaction)11 AionTxReceipt (org.aion.base.AionTxReceipt)8 InternalTransaction (org.aion.types.InternalTransaction)8 ArrayList (java.util.ArrayList)6 AionTxExecSummary (org.aion.base.AionTxExecSummary)5 PrecompiledTransactionResult (org.aion.precompiled.PrecompiledTransactionResult)5 PrecompiledTransactionContext (org.aion.precompiled.type.PrecompiledTransactionContext)5 BlockContext (org.aion.zero.impl.types.BlockContext)5 RepositoryCache (org.aion.base.db.RepositoryCache)4 BridgeTransfer (org.aion.precompiled.contracts.ATB.BridgeTransfer)4 AionBlockSummary (org.aion.zero.impl.types.AionBlockSummary)4 AionTxInfo (org.aion.zero.impl.types.AionTxInfo)4 JSONArray (org.json.JSONArray)4 List (java.util.List)3 ECKey (org.aion.crypto.ECKey)3 DataWord (org.aion.util.types.DataWord)3