use of org.aion.zero.types.AionTransaction in project aion by aionnetwork.
the class NrgBlockPriceStrategy method getBlkPrice.
// notion of "block price" = lowest gas price for all transactions in a block, exluding miner's own transactions
// returns null if block is empty, invalid input, block filled only with miner's own transactions
private Long getBlkPrice(AionBlock blk) {
if (blk == null)
return null;
List<AionTransaction> txns = blk.getTransactionsList();
Address coinbase = blk.getCoinbase();
// there is nothing stopping nrg price to be 0. don't explicitly enforce non-zero nrg.
Long minNrg = null;
for (AionTransaction txn : txns) {
if (coinbase.compareTo(txn.getSignature().getPubkey(null)) != 0) {
long nrg = txn.getNrgPrice();
if (minNrg == null || nrg < minNrg)
minNrg = nrg;
}
}
return minNrg;
}
use of org.aion.zero.types.AionTransaction 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.types.AionTransaction in project aion by aionnetwork.
the class TxnPoolTest method addRepeatedTxn.
@Test
public void addRepeatedTxn() {
Properties config = new Properties();
config.put("txn-timeout", "10");
ITxPool<ITransaction> tp = new TxPoolA0<>(config);
ITransaction txn = new AionTransaction(ByteUtils.fromHexString("0000000000000001"), Address.wrap(key.get(0).getAddress()), Address.wrap(key.get(0).getAddress()), ByteUtils.fromHexString("1"), ByteUtils.fromHexString("1"), 10000L, 1L);
((AionTransaction) txn).sign(key.get(0));
List<ITransaction> txnl = new ArrayList<>();
txnl.add(txn);
txnl.add(txn);
tp.add(txnl);
assertTrue(tp.size() == 1);
}
use of org.aion.zero.types.AionTransaction in project aion by aionnetwork.
the class TxnPoolTest method feemapTest.
@Test
public void feemapTest() {
Properties config = new Properties();
config.put("txn-timeout", "10");
TxPoolA0<ITransaction> tp = new TxPoolA0<>(config);
List<ITransaction> txnl = new ArrayList<>();
int cnt = 100;
byte[] nonce = new byte[Long.BYTES];
for (int i = 0; i < cnt; i++) {
nonce[Long.BYTES - 1] = 1;
Address addr = Address.wrap(key2.get(i).getAddress());
ITransaction txn = new AionTransaction(nonce, addr, Address.wrap("0000000000000000000000000000000000000000000000000000000000000001"), ByteUtils.fromHexString("1"), ByteUtils.fromHexString("1"), 10000L, 1L);
((AionTransaction) txn).sign(key.get(0));
txn.setNrgConsume(i + 1);
txnl.add(txn);
}
tp.add(txnl);
assertTrue(tp.size() == cnt);
// sort the inserted txs
tp.snapshot();
List<BigInteger> nl = tp.getFeeList();
long val = 100;
for (int i = 0; i < cnt; i++) {
assertTrue(nl.get(i).compareTo(BigInteger.valueOf(val--)) == 0);
}
}
use of org.aion.zero.types.AionTransaction in project aion by aionnetwork.
the class TxnPoolTest method benchmarkSnapshot.
@Test
public /* 100K new transactions in pool around 1200ms (cold-call)
*/
void benchmarkSnapshot() {
Properties config = new Properties();
config.put("txn-timeout", "100");
TxPoolA0<ITransaction> tp = new TxPoolA0<>(config);
List<ITransaction> txnl = new ArrayList<>();
int cnt = 10000;
for (ECKey aKey1 : key) {
Address acc = Address.wrap(aKey1.getAddress());
for (int i = 0; i < cnt; i++) {
ITransaction txn = new AionTransaction(BigInteger.valueOf(i).toByteArray(), acc, Address.wrap("0000000000000000000000000000000000000000000000000000000000000001"), ByteUtils.fromHexString("1"), ByteUtils.fromHexString("1"), 10000L, 1L);
((AionTransaction) txn).sign(aKey1);
txn.setNrgConsume(100L);
txnl.add(txn);
}
}
tp.add(txnl);
assertTrue(tp.size() == cnt * key.size());
// sort the inserted txs
long start = System.currentTimeMillis();
tp.snapshot();
System.out.println("time spent: " + (System.currentTimeMillis() - start) + " ms.");
for (ECKey aKey : key) {
List<BigInteger> nl = tp.getNonceList(Address.wrap(aKey.getAddress()));
for (int i = 0; i < cnt; i++) {
assertTrue(nl.get(i).equals(BigInteger.valueOf(i)));
}
}
}
Aggregations