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);
}
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());
}
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;
}
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);
}
}
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()) + "'";
}
Aggregations