use of org.aion.zero.impl.types.AionBlock in project aion by aionnetwork.
the class ApiWeb3Aion method eth_getTransactionByBlockHashAndIndex.
public Object eth_getTransactionByBlockHashAndIndex(String _blockHash, String _index) {
byte[] hash = ByteUtil.hexStringToBytes(_blockHash);
if (_blockHash == null || hash == null)
return null;
AionBlock b = this.ac.getBlockchain().getBlockByHash(hash);
if (b == null)
return null;
List<AionTransaction> txs = b.getTransactionsList();
int idx = Integer.decode(_index);
if (idx >= txs.size())
return null;
return Tx.AionTransactionToJSON(txs.get(idx), b, idx);
}
use of org.aion.zero.impl.types.AionBlock in project aion by aionnetwork.
the class ApiWeb3Aion method eth_call.
public Object eth_call(JSONObject _tx, Object _bnOrId) {
ArgTxCall txParams = ArgTxCall.fromJSON(_tx, getRecommendedNrgPrice(), getDefaultNrgLimit());
String bnOrId = "latest";
if (_bnOrId != null && !_bnOrId.equals(null))
bnOrId = _bnOrId + "";
Long bn = parseBnOrId(bnOrId);
if (bn == null || bn < 0)
return null;
AionTransaction tx = new AionTransaction(txParams.getNonce().toByteArray(), txParams.getTo(), txParams.getValue().toByteArray(), txParams.getData(), txParams.getNrg(), txParams.getNrgPrice());
AionBlock b = this.ac.getBlockchain().getBlockByNumber(bn);
AionTxReceipt receipt = this.ac.callConstant(tx, b);
return TypeConverter.toJsonHex(receipt.getExecutionResult());
}
use of org.aion.zero.impl.types.AionBlock in project aion by aionnetwork.
the class NrgOracle method processBlock.
private void processBlock(AionBlockSummary blockSummary) {
AionBlock blk = (AionBlock) blockSummary.getBlock();
advisor.processBlock(blk);
cacheFlushCounter--;
}
use of org.aion.zero.impl.types.AionBlock in project aion by aionnetwork.
the class ApiAion0 method getRsp_getBlockDetails.
private List<Message.t_BlockDetail> getRsp_getBlockDetails(List<Map.Entry<AionBlock, BigInteger>> blks) {
List<Message.t_BlockDetail> bds = blks.parallelStream().filter(Objects::nonNull).map(blk -> {
AionBlock b = blk.getKey();
Message.t_BlockDetail.Builder builder = Message.t_BlockDetail.newBuilder().setBlockNumber(b.getNumber()).setDifficulty(ByteString.copyFrom(b.getDifficulty())).setExtraData(ByteString.copyFrom(b.getExtraData())).setHash(ByteString.copyFrom(b.getHash())).setLogsBloom(ByteString.copyFrom(b.getLogBloom())).setMinerAddress(ByteString.copyFrom(b.getCoinbase().toBytes())).setNonce(ByteString.copyFrom(b.getNonce())).setNrgConsumed(b.getNrgConsumed()).setNrgLimit(b.getNrgLimit()).setParentHash(ByteString.copyFrom(b.getParentHash())).setTimestamp(b.getTimestamp()).setTxTrieRoot(ByteString.copyFrom(b.getTxTrieRoot())).setReceiptTrieRoot(ByteString.copyFrom(b.getReceiptsRoot())).setStateRoot(ByteString.copyFrom(b.getStateRoot())).setSize(b.getEncoded().length).setSolution(ByteString.copyFrom(b.getHeader().getSolution())).setTotalDifficulty(ByteString.copyFrom(blk.getValue().toByteArray()));
List<AionTransaction> txs = b.getTransactionsList();
List<Message.t_TxDetail> tds = new ArrayList<>();
long cumulativeNrg = 0L;
// Can't use parallel streams here since we need to compute cumulativeNrg, which is done iteratively
for (AionTransaction tx : txs) {
AionTxInfo ti = this.ac.getAionHub().getBlockchain().getTransactionInfo(tx.getHash());
cumulativeNrg += ti.getReceipt().getEnergyUsed();
TxRecpt rt = new TxRecpt(b, ti, cumulativeNrg, true);
List<Message.t_LgEle> tles = Arrays.asList(rt.logs).parallelStream().map(log -> {
return Message.t_LgEle.newBuilder().setData(ByteString.copyFrom(ByteUtil.hexStringToBytes(log.data))).setAddress(ByteString.copyFrom(Address.wrap(log.address).toBytes())).addAllTopics(Arrays.asList(log.topics)).build();
}).filter(Objects::nonNull).collect(Collectors.toList());
Message.t_TxDetail.Builder tdBuilder = Message.t_TxDetail.newBuilder().setData(ByteString.copyFrom(tx.getData())).setTo(ByteString.copyFrom(tx.getTo().toBytes())).setFrom(ByteString.copyFrom(tx.getFrom().toBytes())).setNonce(ByteString.copyFrom(tx.getNonce())).setValue(ByteString.copyFrom(tx.getValue())).setNrgConsumed(rt.nrgUsed).setNrgPrice(tx.getNrgPrice()).setTxHash(ByteString.copyFrom(tx.getHash())).setTxIndex(rt.transactionIndex).addAllLogs(tles);
if (rt.contractAddress != null)
tdBuilder.setContract(ByteString.copyFrom(ByteUtil.hexStringToBytes(rt.contractAddress)));
Message.t_TxDetail td = tdBuilder.build();
tds.add(td);
}
return builder.addAllTx(tds).build();
}).filter(Objects::nonNull).collect(Collectors.toList());
return bds;
}
use of org.aion.zero.impl.types.AionBlock in project aion by aionnetwork.
the class BlockchainConcurrencyTest method testPublishBestBlockSafely.
@Test
public void testPublishBestBlockSafely() {
ExecutorService blockCreationService = Executors.newSingleThreadExecutor();
ExecutorService getBlockService = Executors.newSingleThreadExecutor();
StandaloneBlockchain.Bundle bundle = new StandaloneBlockchain.Builder().withDefaultAccounts().withValidatorConfiguration("simple").build();
StandaloneBlockchain bc = bundle.bc;
final int MAX_COUNT = 100000;
final CountDownLatch endLatch = new CountDownLatch(2);
// this will not definitively prove
try {
blockCreationService.submit(() -> {
int count = 0;
List<AionTransaction> txList = Collections.emptyList();
AionBlock block = bc.createNewBlock(bc.genesis, Collections.emptyList(), false);
while (!Thread.currentThread().isInterrupted() && count < MAX_COUNT) {
block = bc.createNewBlock(block, txList, false);
count++;
}
System.out.println("completed block creation");
endLatch.countDown();
});
getBlockService.submit(() -> {
int count = 0;
long prevNumber = bc.getBestBlock().getNumber();
while (!Thread.currentThread().isInterrupted() && count < MAX_COUNT) {
// all three of these methods use {@link AionBlockchainImpl#pubBestBlock}
assertThat(bc.getBestBlockHash()).isNotNull();
bc.getSize();
AionBlock block = bc.getBestBlock();
assertThat(block).isNotNull();
assertThat(block.getNumber()).isAtLeast(prevNumber);
prevNumber = block.getNumber();
count++;
}
endLatch.countDown();
});
} finally {
blockCreationService.shutdown();
getBlockService.shutdown();
}
}
Aggregations