Skip to content
This repository was archived by the owner on Dec 5, 2024. It is now read-only.

Commit be261fa

Browse files
committed
Polishing RecommendedGasPriceTracker
1 parent 738f265 commit be261fa

File tree

2 files changed

+12
-16
lines changed

2 files changed

+12
-16
lines changed

ethereumj-core/src/main/java/org/ethereum/facade/Ethereum.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,7 @@ ProgramResult callConstantFunction(String receiveAddress, ECKey senderPrivateKey
202202
void initSyncing();
203203

204204
/**
205+
* @deprecated
205206
* Calculates a 'reasonable' Gas price based on statistics of the latest transaction's Gas prices
206207
* Normally the price returned should be sufficient to execute a transaction since ~25% of the latest
207208
* transactions were executed at this or lower price.

ethereumj-core/src/main/java/org/ethereum/listener/RecommendedGasPriceTracker.java

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,14 @@
1717
*/
1818
package org.ethereum.listener;
1919

20-
import org.apache.commons.collections4.queue.CircularFifoQueue;
2120
import org.ethereum.core.Block;
2221
import org.ethereum.core.BlockSummary;
2322
import org.ethereum.core.Transaction;
2423
import org.ethereum.util.ByteUtil;
2524

2625
import java.lang.reflect.Array;
2726
import java.util.Arrays;
27+
import java.util.LinkedList;
2828
import java.util.List;
2929

3030

@@ -46,15 +46,11 @@ public class RecommendedGasPriceTracker extends EthereumListenerAdapter {
4646
private static final int MIN_TRANSACTIONS = 512;
4747
private static final int PERCENTILE_SHARE = 4;
4848

49-
private CircularFifoQueue<long[]> blockGasPrices;
49+
private LinkedList<long[]> blockGasPrices = new LinkedList<>();
5050

5151
private int idx = 0;
5252
private Long recommendedGasPrice = getDefaultPrice();
5353

54-
public RecommendedGasPriceTracker() {
55-
blockGasPrices = new CircularFifoQueue<>(Math.max(getMinTransactions(), getMinBlocks()));
56-
}
57-
5854
@Override
5955
public void onBlock(BlockSummary blockSummary) {
6056
onBlock(blockSummary.getBlock());
@@ -63,7 +59,7 @@ public void onBlock(BlockSummary blockSummary) {
6359
private void onBlock(Block block) {
6460
if (onTransactions(block.getTransactionsList())) {
6561
++idx;
66-
if (idx == getBlocksRecount()) {
62+
if (idx >= getBlocksRecount()) {
6763
Long newGasPrice = getGasPrice();
6864
if (newGasPrice != null) {
6965
this.recommendedGasPrice = newGasPrice;
@@ -81,11 +77,11 @@ private synchronized boolean onTransactions(List<Transaction> txs) {
8177
gasPrices[i] = ByteUtil.byteArrayToLong(txs.get(i).getGasPrice());
8278
}
8379

84-
while (blockGasPrices.size() >= getMinBlocks() &&
85-
(calcGasPricesSize() - blockGasPrices.get(0).length + gasPrices.length) >= getMinTransactions()) {
86-
blockGasPrices.remove(blockGasPrices.get(0));
87-
}
8880
blockGasPrices.add(gasPrices);
81+
while (blockGasPrices.size() > getMinBlocks() &&
82+
(calcGasPricesSize() - blockGasPrices.getFirst().length + gasPrices.length) >= getMinTransactions()) {
83+
blockGasPrices.removeFirst();
84+
}
8985
return true;
9086
}
9187

@@ -99,11 +95,10 @@ private synchronized Long getGasPrice() {
9995
if (size < getMinTransactions() ||
10096
blockGasPrices.size() < getMinBlocks()) return null;
10197

102-
long[] difficulties = new long[size > getMinTransactions() ? size : getMinTransactions()];
98+
long[] difficulties = new long[size];
10399
int index = 0;
104-
for (int i = 0; i < blockGasPrices.size(); ++i) {
105-
long[] current = blockGasPrices.get(i);
106-
for (long currentDifficulty : current) {
100+
for (long[] currentBlock : blockGasPrices) {
101+
for (long currentDifficulty : currentBlock) {
107102
difficulties[index] = currentDifficulty;
108103
++index;
109104
}
@@ -176,4 +171,4 @@ public static int getMinTransactions() {
176171
public static int getPercentileShare() {
177172
return PERCENTILE_SHARE;
178173
}
179-
}
174+
}

0 commit comments

Comments
 (0)