1717 */
1818package org .ethereum .listener ;
1919
20- import org .apache .commons .collections4 .queue .CircularFifoQueue ;
2120import org .ethereum .core .Block ;
2221import org .ethereum .core .BlockSummary ;
2322import org .ethereum .core .Transaction ;
2423import org .ethereum .util .ByteUtil ;
2524
2625import java .lang .reflect .Array ;
2726import java .util .Arrays ;
27+ import java .util .LinkedList ;
2828import 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